diff --git a/Makefile b/Makefile index 3b059203..d82be069 100644 --- a/Makefile +++ b/Makefile @@ -86,8 +86,11 @@ SHA_SRC = ft_sha256.c \ ft_sha_print.c \ ft_sha_usage.c -BASE64_SRC = ft_base64_init.c \ +BASE64_SRC = ft_base64.c \ + ft_base64_init.c \ ft_base64_fill_buffer.c \ + ft_base64_transform.c \ + ft_base64_decode.c \ ft_base64_encode.c SRC = main.c \ @@ -163,10 +166,10 @@ CC := clang # rules -$(NAME): $(LIBFT) $(OBJ) +$(NAME): $(LIBFT) $(OBJ) $(HEADERS) $(CC) $(OBJ) $(LINK_FLAGS) -o $(NAME) -$(TEST_BIN): $(LIBFT) $(TEST_OBJ) +$(TEST_BIN): $(LIBFT) $(TEST_OBJ) $(HEADERS) $(CC) $(TEST_OBJ) $(LINK_FLAGS) -o $(TEST_BIN) $(TEST_OBJ) $(OBJ): | $(OBJ_DIR) @@ -174,7 +177,7 @@ $(TEST_OBJ) $(OBJ): | $(OBJ_DIR) $(OBJ_DIR): mkdir $(OBJ_DIR) -$(OBJ_DIR)%.o: %.c $(HEADERS) +$(OBJ_DIR)%.o: %.c $(CC) -c $< -o $@ $(CC_FLAGS) $(HEADER_FLAGS) $(LIBFT): diff --git a/inc/ft_base64.h b/inc/ft_base64.h index 975d75d9..46f14f4d 100644 --- a/inc/ft_base64.h +++ b/inc/ft_base64.h @@ -24,15 +24,51 @@ typedef unsigned char t_byte1; typedef struct s_base64_ctx { + int input_fd; + int output_fd; t_byte8 glue_block_bytes_count; t_byte1 glue_block[FT_BASE64_GLUE_BLOCK_SIZE]; t_byte1 alphabet[FT_BASE64_ALPHABET_LENGTH]; t_byte1 chars[FT_BASE64_CHARS_SIZE]; } t_base64_ctx; -void ft_base64_init(t_base64_ctx *ctx); -void ft_base64_fill_buffer( - t_base64_ctx *ctx, t_byte1 *data, t_byte8 size); -void ft_base64_encode(t_base64_ctx *ctx, t_byte1 *data); +typedef struct s_base64_flags +{ + int decode; +} t_base64_flags; + +void ft_base64 +( + int argc, + char **argv +); + +void ft_base64_init +( + t_base64_ctx *ctx +); + +void ft_base64_decode +( + t_base64_ctx *ctx +); + +void ft_base64_encode +( + t_base64_ctx *ctx +); + +void ft_base64_fill_buffer +( + t_base64_ctx *ctx, + t_byte1 *data, + t_byte8 size +); + +void ft_base64_transform +( + t_base64_ctx *ctx, + t_byte1 *data +); #endif diff --git a/inc/tests.h b/inc/tests.h index 1323f668..f8da8294 100644 --- a/inc/tests.h +++ b/inc/tests.h @@ -75,6 +75,6 @@ MunitResult should_init_base64_ctx(const MunitParameter test_params[], MunitResult should_fill_base64_buffer(const MunitParameter test_params[], void *test_data); -MunitResult should_encode_base64_data(const MunitParameter test_params[], +MunitResult should_transform_base64_block(const MunitParameter test_params[], void *test_data); #endif diff --git a/src/base64/ft_base64.c b/src/base64/ft_base64.c new file mode 100644 index 00000000..19f7b98f --- /dev/null +++ b/src/base64/ft_base64.c @@ -0,0 +1,64 @@ +#include "libft.h" +#include "ft_base64.h" + +static void open_input_stream(t_base64_ctx *ctx, char *filename) +{ + (void)ctx; + (void)filename; +} + +static void open_output_stream(t_base64_ctx *ctx, char *filename) +{ + (void)ctx; + (void)filename; +} + +static void init_flags(t_base64_flags *flags) +{ + flags->decode = 0; +} + +static void read_args +( + int argc, + char **argv, + t_base64_flags *flags, + t_base64_ctx *ctx +) +{ + int i; + char *current_arg; + char *next_arg; + + i = 0; + while(i < argc) + { + current_arg = argv[i]; + next_arg = i + 1 < argc ? argv[i + 1] : NULL; + if (ft_strcmp(current_arg, "-d") == 0) + flags->decode = 1; + else if (ft_strcmp(current_arg, "-e") == 0 && i++) + continue; + else if (ft_strcmp(current_arg, "-i") == 0) + open_input_stream(ctx, next_arg); + else if (ft_strcmp(current_arg, "-o") == 0) + open_output_stream(ctx, next_arg); + else if (i++) + continue; + i++; + } +} + +void ft_base64(int argc, char **argv) +{ + t_base64_flags flags; + t_base64_ctx ctx; + + init_flags(&flags); + read_args(argc, argv, &flags, &ctx); + if (flags.decode) + ft_base64_decode(&ctx); + else + ft_base64_encode(&ctx); + exit(0); +} \ No newline at end of file diff --git a/src/base64/ft_base64_decode.c b/src/base64/ft_base64_decode.c new file mode 100644 index 00000000..aed29d83 --- /dev/null +++ b/src/base64/ft_base64_decode.c @@ -0,0 +1,6 @@ +#include "ft_base64.h" + +void ft_base64_decode(t_base64_ctx *ctx) +{ + (void)ctx; +} \ No newline at end of file diff --git a/src/base64/ft_base64_encode.c b/src/base64/ft_base64_encode.c index 8b7736e1..f0efb1aa 100644 --- a/src/base64/ft_base64_encode.c +++ b/src/base64/ft_base64_encode.c @@ -1,31 +1,6 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_base64_encode.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: gtertysh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/02/02 18:26:32 by gtertysh #+# #+# */ -/* Updated: 2019/02/02 18:26:51 by gtertysh ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" #include "ft_base64.h" -void ft_base64_encode(t_base64_ctx *ctx, t_byte1 *data) +void ft_base64_encode(t_base64_ctx *ctx) { - t_byte1 first_char; - t_byte1 second_char; - t_byte1 third_char; - t_byte1 fourth_char; - - first_char = (data[0] >> 2) & 0x3f; - second_char = ((data[0] << 4) & 0x30) | ((data[1] >> 4) & 0xf); - third_char = ((data[1] << 2) & 0x3c) | ((data[2] >> 6) & 0x3); - fourth_char = data[2] & 0x3F; - ctx->chars[0] = ctx->alphabet[first_char]; - ctx->chars[1] = ctx->alphabet[second_char]; - ctx->chars[2] = ctx->alphabet[third_char]; - ctx->chars[3] = ctx->alphabet[fourth_char]; -} + (void)ctx; +} \ No newline at end of file diff --git a/src/base64/ft_base64_init.c b/src/base64/ft_base64_init.c index 675055b3..f47cc6e3 100644 --- a/src/base64/ft_base64_init.c +++ b/src/base64/ft_base64_init.c @@ -12,10 +12,13 @@ #include "ft_base64.h" #include "libft.h" +#include void ft_base64_init(t_base64_ctx *ctx) { ctx->glue_block_bytes_count = 0; + ctx->input_fd = STDIN_FILENO; + ctx->output_fd = STDOUT_FILENO; ft_bzero(ctx->glue_block, FT_BASE64_GLUE_BLOCK_SIZE); ft_bzero(ctx->chars, FT_BASE64_CHARS_SIZE); ft_memcpy(ctx->alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26); diff --git a/src/base64/ft_base64_transform.c b/src/base64/ft_base64_transform.c new file mode 100644 index 00000000..05fa48be --- /dev/null +++ b/src/base64/ft_base64_transform.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_base64_transform.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/02 18:26:32 by gtertysh #+# #+# */ +/* Updated: 2019/02/02 18:26:51 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "ft_base64.h" + +void ft_base64_transform(t_base64_ctx *ctx, t_byte1 *data) +{ + t_byte1 first_char; + t_byte1 second_char; + t_byte1 third_char; + t_byte1 fourth_char; + + first_char = (data[0] >> 2) & 0x3f; + second_char = ((data[0] << 4) & 0x30) | ((data[1] >> 4) & 0xf); + third_char = ((data[1] << 2) & 0x3c) | ((data[2] >> 6) & 0x3); + fourth_char = data[2] & 0x3F; + ctx->chars[0] = ctx->alphabet[first_char]; + ctx->chars[1] = ctx->alphabet[second_char]; + ctx->chars[2] = ctx->alphabet[third_char]; + ctx->chars[3] = ctx->alphabet[fourth_char]; +} diff --git a/src/main.c b/src/main.c index 887aff4c..7a7ebf17 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,7 @@ #include "ft_ssl.h" #include "ft_md5.h" #include "ft_sha.h" +#include "ft_base64.h" #include "libft.h" t_algorithm g_algorithms[] = { @@ -28,6 +29,10 @@ t_algorithm g_algorithms[] = { "sha224", ft_sha224, }, + { + "base64", + ft_base64, + }, {NULL, NULL} }; diff --git a/t/base64_tests.c b/t/base64_tests.c index 59f1828f..ca1513aa 100644 --- a/t/base64_tests.c +++ b/t/base64_tests.c @@ -1,3 +1,4 @@ +#include #include "tests.h" #include "tests_macros.h" #include "ft_base64.h" @@ -12,6 +13,9 @@ abcdefghijklmnopqrstuvwxyz0123456789+/"; ft_base64_init(&ctx); + munit_assert_true(ctx.input_fd == STDIN_FILENO); + munit_assert_true(ctx.output_fd == STDOUT_FILENO); + for (int i = 0; i < FT_BASE64_GLUE_BLOCK_SIZE; i++) munit_assert_uchar(ctx.glue_block[i], ==, 0); @@ -43,7 +47,7 @@ TEST_RESULT should_fill_base64_buffer(TEST_PARAMS, TEST_DATA) return MUNIT_OK; } -TEST_RESULT should_encode_base64_data(TEST_PARAMS, TEST_DATA) +TEST_RESULT should_transform_base64_block(TEST_PARAMS, TEST_DATA) { UNUSED(test_params); UNUSED(test_data); @@ -52,10 +56,10 @@ TEST_RESULT should_encode_base64_data(TEST_PARAMS, TEST_DATA) ft_base64_init(&ctx); - ft_base64_encode(&ctx, (t_byte1 *)"Man"); + ft_base64_transform(&ctx, (t_byte1 *)"Man"); munit_assert_string_equal((char *)ctx.chars, "TWFu"); - ft_base64_encode(&ctx, (t_byte1 *)"LOL"); + ft_base64_transform(&ctx, (t_byte1 *)"LOL"); munit_assert_string_equal((char *)ctx.chars, "TE9M"); return MUNIT_OK; } \ No newline at end of file diff --git a/t/tests.c b/t/tests.c index 9999ab95..3a691cdd 100644 --- a/t/tests.c +++ b/t/tests.c @@ -43,7 +43,7 @@ MunitTest g_sha_tests[] = { MunitTest g_base64_tests[] = { IT("/init_ctx", should_init_base64_ctx, NULL, NULL, 0, NULL), IT("/fills_buffer", should_fill_base64_buffer, NULL, NULL, 0, NULL), - IT("/encodes_data", should_encode_base64_data, NULL, NULL, 0, NULL), + IT("/transform_block", should_transform_base64_block, NULL, NULL, 0, NULL), END_IT };