From cebeb518675846a9a0a570838dbe7e405087db28 Mon Sep 17 00:00:00 2001 From: Gregory Date: Sun, 10 Feb 2019 23:11:01 +0200 Subject: [PATCH] basic base64 encoding --- Makefile | 16 +++++++----- inc/ft_base64.h | 25 ++++++++++++++++--- inc/tests.h | 3 --- src/base64/ft_base64.c | 24 +++++++++--------- src/base64/ft_base64_encode.c | 37 ++++++++++++++++++++++++++-- src/base64/ft_base64_encode_finish.c | 16 ++++++++++++ src/base64/ft_base64_encode_step.c | 19 ++++++++++++++ src/base64/ft_base64_fill_buffer.c | 20 --------------- src/base64/ft_base64_init.c | 2 -- src/base64/ft_base64_write.c | 7 ++++++ t/base64_tests.c | 22 ----------------- t/cases/Ma.txt | 1 + t/cases/Man.txt | 1 + t/tests.c | 1 - 14 files changed, 124 insertions(+), 70 deletions(-) create mode 100644 src/base64/ft_base64_encode_finish.c create mode 100644 src/base64/ft_base64_encode_step.c delete mode 100644 src/base64/ft_base64_fill_buffer.c create mode 100644 src/base64/ft_base64_write.c create mode 100644 t/cases/Ma.txt create mode 100644 t/cases/Man.txt diff --git a/Makefile b/Makefile index d82be069..5565aeae 100644 --- a/Makefile +++ b/Makefile @@ -31,9 +31,11 @@ TST_DIR := $(ROOT)/t/ MD5_HEADER := $(INC_DIR)/ft_md5.h SHA_HEADER := $(INC_DIR)/ft_sha.h SSL_HEADER := $(INC_DIR)/ft_ssl.h +B64_HEADER := $(INC_DIR)/ft_base64.h HEADERS := $(MD5_HEADER) \ $(SHA_HEADER) \ - $(SSL_HEADER) + $(SSL_HEADER) \ + $(B64_HEADER) # libraries LIBFT_DIR := $(LIB_DIR)libft/ @@ -88,10 +90,12 @@ SHA_SRC = ft_sha256.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 + ft_base64_encode.c \ + ft_base64_encode_step.c \ + ft_base64_encode_finish.c \ + ft_base64_write.c SRC = main.c \ ft_ssl_usage.c @@ -166,10 +170,10 @@ CC := clang # rules -$(NAME): $(LIBFT) $(OBJ) $(HEADERS) +$(NAME): $(LIBFT) $(OBJ) $(CC) $(OBJ) $(LINK_FLAGS) -o $(NAME) -$(TEST_BIN): $(LIBFT) $(TEST_OBJ) $(HEADERS) +$(TEST_BIN): $(LIBFT) $(TEST_OBJ) $(CC) $(TEST_OBJ) $(LINK_FLAGS) -o $(TEST_BIN) $(TEST_OBJ) $(OBJ): | $(OBJ_DIR) @@ -177,7 +181,7 @@ $(TEST_OBJ) $(OBJ): | $(OBJ_DIR) $(OBJ_DIR): mkdir $(OBJ_DIR) -$(OBJ_DIR)%.o: %.c +$(OBJ_DIR)%.o: %.c $(HEADERS) $(CC) -c $< -o $@ $(CC_FLAGS) $(HEADER_FLAGS) $(LIBFT): diff --git a/inc/ft_base64.h b/inc/ft_base64.h index 46f14f4d..59bdd87b 100644 --- a/inc/ft_base64.h +++ b/inc/ft_base64.h @@ -15,7 +15,8 @@ # include -# define FT_BASE64_GLUE_BLOCK_SIZE 3 +# define FT_BASE64_READ_SIZE 3 +# define FT_BASE64_TRANS_SIZE 3 # define FT_BASE64_CHARS_SIZE 4 # define FT_BASE64_ALPHABET_LENGTH 64 @@ -26,8 +27,6 @@ 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; @@ -58,6 +57,21 @@ void ft_base64_encode t_base64_ctx *ctx ); +t_byte8 ft_base64_encode_step +( + t_base64_ctx *ctx, + t_byte8 readed, + t_byte1 *buff +); + +void ft_base64_encode_finish +( + + t_base64_ctx *ctx, + t_byte8 reminder, + t_byte1 *buff +); + void ft_base64_fill_buffer ( t_base64_ctx *ctx, @@ -71,4 +85,9 @@ void ft_base64_transform t_byte1 *data ); +void ft_base64_write +( + t_base64_ctx *ctx +); + #endif diff --git a/inc/tests.h b/inc/tests.h index f8da8294..fbda3867 100644 --- a/inc/tests.h +++ b/inc/tests.h @@ -72,9 +72,6 @@ MunitResult create_digest_string_sha224(const MunitParameter test_params[], MunitResult should_init_base64_ctx(const MunitParameter test_params[], void *test_data); -MunitResult should_fill_base64_buffer(const MunitParameter test_params[], - void *test_data); - 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 index 19f7b98f..9952a551 100644 --- a/src/base64/ft_base64.c +++ b/src/base64/ft_base64.c @@ -1,16 +1,17 @@ +#include #include "libft.h" #include "ft_base64.h" +#include "fcntl.h" -static void open_input_stream(t_base64_ctx *ctx, char *filename) +static int open_stream(char *filename) { - (void)ctx; - (void)filename; -} - -static void open_output_stream(t_base64_ctx *ctx, char *filename) -{ - (void)ctx; - (void)filename; + int fd; + if ((fd = open((const char *)filename, O_RDONLY)) == -1) + { + perror("base64"); + exit(1); + } + return fd; } static void init_flags(t_base64_flags *flags) @@ -40,9 +41,9 @@ static void read_args else if (ft_strcmp(current_arg, "-e") == 0 && i++) continue; else if (ft_strcmp(current_arg, "-i") == 0) - open_input_stream(ctx, next_arg); + ctx->input_fd = open_stream(next_arg); else if (ft_strcmp(current_arg, "-o") == 0) - open_output_stream(ctx, next_arg); + ctx->output_fd = open_stream(next_arg); else if (i++) continue; i++; @@ -55,6 +56,7 @@ void ft_base64(int argc, char **argv) t_base64_ctx ctx; init_flags(&flags); + ft_base64_init(&ctx); read_args(argc, argv, &flags, &ctx); if (flags.decode) ft_base64_decode(&ctx); diff --git a/src/base64/ft_base64_encode.c b/src/base64/ft_base64_encode.c index f0efb1aa..67922c3f 100644 --- a/src/base64/ft_base64_encode.c +++ b/src/base64/ft_base64_encode.c @@ -1,6 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_base64_encode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/10 20:52:07 by gtertysh #+# #+# */ +/* Updated: 2019/02/10 20:52:59 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include #include "ft_base64.h" +#include "libft.h" void ft_base64_encode(t_base64_ctx *ctx) { - (void)ctx; -} \ No newline at end of file + t_byte1 buff[FT_BASE64_READ_SIZE + FT_BASE64_TRANS_SIZE]; + t_byte8 readed; + t_byte8 reminder; + int fd; + + fd = ctx->input_fd; + reminder = 0; + while ((readed = read(fd, buff + reminder, FT_BASE64_READ_SIZE)) > 0) + { + if (readed >= FT_BASE64_TRANS_SIZE) + { + reminder = ft_base64_encode_step(ctx, readed, buff); + ft_memmove(buff, buff + readed - reminder, reminder); + } + else + { + reminder += readed; + } + } + // ft_base64_encode_finish(ctx, reminder, buff); +} diff --git a/src/base64/ft_base64_encode_finish.c b/src/base64/ft_base64_encode_finish.c new file mode 100644 index 00000000..89c321c6 --- /dev/null +++ b/src/base64/ft_base64_encode_finish.c @@ -0,0 +1,16 @@ +#include "ft_base64.h" +#include "libft.h" + +void ft_base64_encode_finish +( + t_base64_ctx *ctx, + t_byte8 reminder, + t_byte1 *buffer +) +{ + ft_bzero(buffer + reminder, FT_BASE64_TRANS_SIZE - reminder); + ft_base64_transform(ctx, buffer); + ft_memset(ctx->chars + FT_BASE64_CHARS_SIZE - reminder, '=', FT_BASE64_TRANS_SIZE - reminder); + ft_base64_write(ctx); + ft_putstr("\n"); +} \ No newline at end of file diff --git a/src/base64/ft_base64_encode_step.c b/src/base64/ft_base64_encode_step.c new file mode 100644 index 00000000..35a62d3c --- /dev/null +++ b/src/base64/ft_base64_encode_step.c @@ -0,0 +1,19 @@ +#include "ft_base64.h" +#include "libft.h" + +t_byte8 ft_base64_encode_step +( + t_base64_ctx *ctx, + t_byte8 readed, + t_byte1 *buffer +) +{ + while(readed >= FT_BASE64_TRANS_SIZE) + { + ft_base64_transform(ctx, buffer); + ft_base64_write(ctx); + readed -= FT_BASE64_TRANS_SIZE; + buffer += FT_BASE64_TRANS_SIZE; + } + return readed; +} diff --git a/src/base64/ft_base64_fill_buffer.c b/src/base64/ft_base64_fill_buffer.c deleted file mode 100644 index 301040da..00000000 --- a/src/base64/ft_base64_fill_buffer.c +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_base64_fill_buffer.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: gtertysh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/02/02 18:27:34 by gtertysh #+# #+# */ -/* Updated: 2019/02/02 18:27:39 by gtertysh ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include "ft_base64.h" - -void ft_base64_fill_buffer(t_base64_ctx *ctx, t_byte1 *data, t_byte8 size) -{ - ft_memcpy(ctx->glue_block, data, size); - ctx->glue_block_bytes_count = size; -} diff --git a/src/base64/ft_base64_init.c b/src/base64/ft_base64_init.c index f47cc6e3..5e0facc7 100644 --- a/src/base64/ft_base64_init.c +++ b/src/base64/ft_base64_init.c @@ -16,10 +16,8 @@ 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); ft_memcpy(ctx->alphabet + 26, "abcdefghijklmnopqrstuvwxyz", 26); diff --git a/src/base64/ft_base64_write.c b/src/base64/ft_base64_write.c new file mode 100644 index 00000000..ff988c5a --- /dev/null +++ b/src/base64/ft_base64_write.c @@ -0,0 +1,7 @@ +# include "ft_base64.h" +# include + +void ft_base64_write(t_base64_ctx *ctx) +{ + write(ctx->output_fd, ctx->chars, FT_BASE64_CHARS_SIZE); +} \ No newline at end of file diff --git a/t/base64_tests.c b/t/base64_tests.c index ca1513aa..d91984f7 100644 --- a/t/base64_tests.c +++ b/t/base64_tests.c @@ -16,37 +16,15 @@ abcdefghijklmnopqrstuvwxyz0123456789+/"; 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); for (int i = 0; i < FT_BASE64_CHARS_SIZE; i++) munit_assert_uchar(ctx.chars[i], ==, 0); munit_assert_string_equal(alphabet, (char *)ctx.alphabet); - munit_assert_true(ctx.glue_block_bytes_count == 0); return MUNIT_OK; } -TEST_RESULT should_fill_base64_buffer(TEST_PARAMS, TEST_DATA) -{ - UNUSED(test_params); - UNUSED(test_data); - - t_base64_ctx ctx; - - ft_base64_init(&ctx); - - ft_base64_fill_buffer(&ctx, (t_byte1 *)"123", 3); - munit_assert_memory_equal(3, ctx.glue_block, "123"); - munit_assert_true(ctx.glue_block_bytes_count == 3); - - ft_base64_fill_buffer(&ctx, (t_byte1 *)"22", 2); - munit_assert_memory_equal(2, ctx.glue_block, "22"); - munit_assert_true(ctx.glue_block_bytes_count == 2); - return MUNIT_OK; -} - TEST_RESULT should_transform_base64_block(TEST_PARAMS, TEST_DATA) { UNUSED(test_params); diff --git a/t/cases/Ma.txt b/t/cases/Ma.txt new file mode 100644 index 00000000..e290d5c5 --- /dev/null +++ b/t/cases/Ma.txt @@ -0,0 +1 @@ +Ma \ No newline at end of file diff --git a/t/cases/Man.txt b/t/cases/Man.txt new file mode 100644 index 00000000..233c4e17 --- /dev/null +++ b/t/cases/Man.txt @@ -0,0 +1 @@ +Man \ No newline at end of file diff --git a/t/tests.c b/t/tests.c index 3a691cdd..062b1f5d 100644 --- a/t/tests.c +++ b/t/tests.c @@ -42,7 +42,6 @@ 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("/transform_block", should_transform_base64_block, NULL, NULL, 0, NULL), END_IT };