diff --git a/Makefile b/Makefile index e98c4206..da642dfc 100644 --- a/Makefile +++ b/Makefile @@ -32,9 +32,9 @@ 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) \ +HEADERS := $(MD5_HEADER) \ + $(SHA_HEADER) \ + $(SSL_HEADER) \ $(B64_HEADER) # libraries @@ -48,60 +48,60 @@ MUINUT_INC := -I $(MUINUT_DIR) # project source files -MD5_SRC = ft_md5.c \ - ft_md5_stdin.c \ - ft_md5_string.c \ - ft_md5_file.c \ - ft_md5_print.c \ - ft_md5_init.c \ - ft_md5_update.c \ - ft_md5_transform.c \ - ft_md5_decode.c \ - ft_md5_encode_len.c \ - ft_md5_encode_register.c \ - ft_md5_final.c \ - ft_md5_padding.c \ +MD5_SRC = ft_md5.c \ + ft_md5_stdin.c \ + ft_md5_string.c \ + ft_md5_file.c \ + ft_md5_print.c \ + ft_md5_init.c \ + ft_md5_update.c \ + ft_md5_transform.c \ + ft_md5_decode.c \ + ft_md5_encode_len.c \ + ft_md5_encode_register.c \ + ft_md5_final.c \ + ft_md5_padding.c \ ft_md5_digest_string.c -SHA_SRC = ft_sha256.c \ - ft_sha256_string.c \ - ft_sha256_file.c \ - ft_sha256_stdin.c \ - ft_sha256_init.c \ - ft_sha256_update.c \ - ft_sha256_decode.c \ - ft_sha256_encode_len.c \ - ft_sha256_encode_register.c \ - ft_sha256_transform.c \ - ft_sha256_padding.c \ - ft_sha256_final.c \ - ft_sha256_digest_string.c \ - ft_sha256_constants.c \ - ft_sha224_string.c \ - ft_sha224_file.c \ - ft_sha224_stdin.c \ - ft_sha224.c \ - ft_sha224_init.c \ - ft_sha224_update.c \ - ft_sha224_final.c \ - ft_sha224_digest_string.c \ - ft_sha_print.c \ +SHA_SRC = ft_sha256.c \ + ft_sha256_string.c \ + ft_sha256_file.c \ + ft_sha256_stdin.c \ + ft_sha256_init.c \ + ft_sha256_update.c \ + ft_sha256_decode.c \ + ft_sha256_encode_len.c \ + ft_sha256_encode_register.c \ + ft_sha256_transform.c \ + ft_sha256_padding.c \ + ft_sha256_final.c \ + ft_sha256_digest_string.c \ + ft_sha256_constants.c \ + ft_sha224_string.c \ + ft_sha224_file.c \ + ft_sha224_stdin.c \ + ft_sha224.c \ + ft_sha224_init.c \ + ft_sha224_update.c \ + ft_sha224_final.c \ + ft_sha224_digest_string.c \ + ft_sha_print.c \ ft_sha_usage.c -BASE64_SRC = ft_base64.c \ - ft_base64_init.c \ - ft_base64_transform.c \ - ft_base64_decode.c \ - ft_base64_encode.c \ - ft_base64_chunk.c \ - ft_base64_encode_finish.c \ - ft_base64_write.c +BASE64_SRC = ft_base64.c \ + ft_base64_init.c \ + ft_base64_decode.c \ + ft_base64_encode.c \ + ft_base64_init_encode_buffer.c \ + ft_base64_encode_chunk.c \ + ft_base64_encode_transform.c \ + ft_base64_encode_finish.c -SRC = main.c \ +SRC = main.c \ ft_ssl_usage.c -SRC += $(MD5_SRC) \ - $(SHA_SRC) \ +SRC += $(MD5_SRC) \ + $(SHA_SRC) \ $(BASE64_SRC) # project object files @@ -121,11 +121,11 @@ SHA_TESTS += $(SHA_SRC) BASE64_TESTS = base64_tests.c BASE64_TESTS += $(BASE64_SRC) -TEST_SRC = tests.c \ +TEST_SRC = tests.c \ munit.c -TEST_SRC += $(MD5_TESTS) \ - $(SHA_TESTS) \ +TEST_SRC += $(MD5_TESTS) \ + $(SHA_TESTS) \ $(BASE64_TESTS) TEST_OBJ = $(addprefix $(OBJ_DIR), $(TEST_SRC:.c=.o)) @@ -160,8 +160,8 @@ LINK_FLAGS = $(LIBFT_LIB) # header flags -HEADER_FLAGS = -I $(INC_DIR) \ - $(MUINUT_INC) \ +HEADER_FLAGS = -I $(INC_DIR) \ + $(MUINUT_INC) \ $(LIBFT_INC) # compiler @@ -212,11 +212,11 @@ multi: # special stuff -vpath %.c $(SRC_DIR) \ - $(MD5_DIR) \ - $(SHA_DIR) \ - $(B64_DIR) \ - $(TST_DIR) \ +vpath %.c $(SRC_DIR) \ + $(MD5_DIR) \ + $(SHA_DIR) \ + $(B64_DIR) \ + $(TST_DIR) \ $(MUINUT_DIR) diff --git a/inc/ft_base64.h b/inc/ft_base64.h index 411b400a..7c5ef338 100644 --- a/inc/ft_base64.h +++ b/inc/ft_base64.h @@ -15,7 +15,7 @@ # include -# define FT_BASE64_READ_SIZE 1 +# define FT_BASE64_READ_SIZE 1024 # define FT_BASE64_TRANS_SIZE 3 # define FT_BASE64_CHARS_SIZE 4 # define FT_BASE64_ALPHABET_LENGTH 64 @@ -23,14 +23,17 @@ typedef uint64_t t_byte8; typedef unsigned char t_byte1; +typedef struct s_base64_encode_buffer +{ + t_byte1 block[FT_BASE64_TRANS_SIZE]; + t_byte8 readed; +} t_base64_encode_buffer; + typedef struct s_base64_ctx { int input_fd; int output_fd; - t_byte8 in_buffer; - t_byte1 buffer[FT_BASE64_TRANS_SIZE]; t_byte1 alphabet[FT_BASE64_ALPHABET_LENGTH]; - t_byte1 chars[FT_BASE64_CHARS_SIZE]; } t_base64_ctx; typedef struct s_base64_flags @@ -69,8 +72,7 @@ void ft_base64_encode_finish ( t_base64_ctx *ctx, - t_byte8 reminder, - t_byte1 *buff + t_base64_encode_buffer *buff ); void ft_base64_fill_buffer @@ -80,23 +82,24 @@ void ft_base64_fill_buffer t_byte8 size ); -void ft_base64_transform +void ft_base64_encode_transform ( t_base64_ctx *ctx, - t_byte1 *data + t_byte1 data[FT_BASE64_TRANS_SIZE], + t_byte1 chars[FT_BASE64_CHARS_SIZE] ); -void ft_base64_write -( - t_base64_ctx *ctx -); - - -t_byte8 ft_base64_chunk +void ft_base64_encode_chunk ( t_base64_ctx *ctx, t_byte8 len, - t_byte1 *message + t_byte1 *message, + t_base64_encode_buffer *enc_buff +); + +void ft_base64_init_encode_buffer +( + t_base64_encode_buffer *buff ); #endif diff --git a/src/base64/ft_base64_chunk.c b/src/base64/ft_base64_chunk.c deleted file mode 100644 index ed8d04ae..00000000 --- a/src/base64/ft_base64_chunk.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "ft_base64.h" -#include "libft.h" - -t_byte8 ft_base64_chunk -( - t_base64_ctx *ctx, - t_byte8 len, - t_byte1 *message -) -{ - t_byte8 free_space_in_buffer; - t_byte8 i; - - free_space_in_buffer = FT_BASE64_TRANS_SIZE - ctx->in_buffer; - if (len >= free_space_in_buffer) - { - ft_memcpy(&ctx->buffer[ctx->in_buffer], message, free_space_in_buffer); - ft_base64_transform(ctx, ctx->buffer); - ft_base64_write(ctx); - i = free_space_in_buffer; - while(i + FT_BASE64_TRANS_SIZE <= len) - { - ft_base64_transform(ctx, &message[i]); - ft_base64_write(ctx); - i += FT_BASE64_TRANS_SIZE; - } - - } -} \ No newline at end of file diff --git a/src/base64/ft_base64_encode.c b/src/base64/ft_base64_encode.c index 44872f00..26dd87ac 100644 --- a/src/base64/ft_base64_encode.c +++ b/src/base64/ft_base64_encode.c @@ -16,14 +16,12 @@ void ft_base64_encode(t_base64_ctx *c) { - t_byte1 buffer[FT_BASE64_READ_SIZE]; - int readed; - int head; - int reminder; + t_byte1 read_buff[FT_BASE64_READ_SIZE]; + t_base64_encode_buffer enc_buff; + int readed; - reminder = 0; - head = 0; - while ((readed = read(c->input_fd, buffer, FT_BASE64_READ_SIZE)) > 0) - ft_base64_chunk(c, readed, buffer); - ft_base64_encode_finish(c, reminder, buffer); + ft_base64_init_encode_buffer(&enc_buff); + while ((readed = read(c->input_fd, read_buff, FT_BASE64_READ_SIZE)) > 0) + ft_base64_encode_chunk(c, readed, read_buff, &enc_buff); + ft_base64_encode_finish(c, &enc_buff); } diff --git a/src/base64/ft_base64_encode_chunk.c b/src/base64/ft_base64_encode_chunk.c new file mode 100644 index 00000000..443380bf --- /dev/null +++ b/src/base64/ft_base64_encode_chunk.c @@ -0,0 +1,46 @@ +#include "ft_base64.h" +#include "libft.h" + +static void ft_base64_encode_write +( + t_base64_ctx *ctx, + t_byte1 buff[FT_BASE64_TRANS_SIZE] +) +{ + t_byte1 chars[FT_BASE64_CHARS_SIZE]; + + ft_base64_encode_transform(ctx, buff, chars); + write(ctx->output_fd, chars, FT_BASE64_CHARS_SIZE); +} + +void ft_base64_encode_chunk +( + t_base64_ctx *ctx, + t_byte8 message_len, + t_byte1 *message, + t_base64_encode_buffer *buff +) +{ + t_byte8 free_space_in_buffer; + t_byte8 buffer_index; + t_byte8 idx; + + buffer_index = buff->readed % FT_BASE64_TRANS_SIZE; + buff->readed += message_len; + free_space_in_buffer = FT_BASE64_TRANS_SIZE - buffer_index; + if (message_len >= free_space_in_buffer) + { + ft_memcpy(&buff->block[buffer_index], message, free_space_in_buffer); + ft_base64_encode_write(ctx, buff->block); + idx = free_space_in_buffer; + while(idx + FT_BASE64_TRANS_SIZE <= message_len) + { + ft_base64_encode_write(ctx, &message[idx]); + idx += FT_BASE64_TRANS_SIZE; + } + buffer_index = 0; + } + else + idx = 0; + ft_memcpy(&buff->block[buffer_index], &message[idx], message_len - idx); +} \ No newline at end of file diff --git a/src/base64/ft_base64_encode_finish.c b/src/base64/ft_base64_encode_finish.c index 6feb3295..53b59a65 100644 --- a/src/base64/ft_base64_encode_finish.c +++ b/src/base64/ft_base64_encode_finish.c @@ -4,21 +4,25 @@ void ft_base64_encode_finish ( t_base64_ctx *ctx, - t_byte8 reminder, - t_byte1 *buffer + t_base64_encode_buffer *buff ) { - t_byte8 padding_size; + t_byte8 buffer_index; + t_byte8 padding_size; + t_byte1 chars[FT_BASE64_CHARS_SIZE]; - if (!reminder) + buffer_index = buff->readed % FT_BASE64_TRANS_SIZE; + padding_size = FT_BASE64_TRANS_SIZE - buffer_index; + if (!buff->readed) + return ; + if (!buffer_index) { ft_putstr("\n"); return ; } - padding_size = reminder == 2 ? 1 : 2; - ft_bzero(buffer + reminder, FT_BASE64_TRANS_SIZE - reminder); - ft_base64_transform(ctx, buffer); - ft_memset(ctx->chars + FT_BASE64_CHARS_SIZE - padding_size, '=', FT_BASE64_CHARS_SIZE - padding_size); - ft_base64_write(ctx); + ft_bzero(&buff->block[buffer_index], padding_size); + ft_base64_encode_transform(ctx, buff->block, chars); + ft_memset(chars + FT_BASE64_CHARS_SIZE - padding_size, '=', padding_size); + write(ctx->output_fd, chars, FT_BASE64_CHARS_SIZE); ft_putstr("\n"); } \ No newline at end of file diff --git a/src/base64/ft_base64_transform.c b/src/base64/ft_base64_encode_transform.c similarity index 80% rename from src/base64/ft_base64_transform.c rename to src/base64/ft_base64_encode_transform.c index 05fa48be..fd320daa 100644 --- a/src/base64/ft_base64_transform.c +++ b/src/base64/ft_base64_encode_transform.c @@ -13,7 +13,12 @@ #include "libft.h" #include "ft_base64.h" -void ft_base64_transform(t_base64_ctx *ctx, t_byte1 *data) +void ft_base64_encode_transform +( + t_base64_ctx *ctx, + t_byte1 data[FT_BASE64_TRANS_SIZE], + t_byte1 chars[FT_BASE64_CHARS_SIZE] +) { t_byte1 first_char; t_byte1 second_char; @@ -24,8 +29,10 @@ void ft_base64_transform(t_base64_ctx *ctx, t_byte1 *data) 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]; + + chars[0] = ctx->alphabet[first_char]; + chars[1] = ctx->alphabet[second_char]; + chars[2] = ctx->alphabet[third_char]; + chars[3] = ctx->alphabet[fourth_char]; + } diff --git a/src/base64/ft_base64_init.c b/src/base64/ft_base64_init.c index 5e0facc7..f38a6c96 100644 --- a/src/base64/ft_base64_init.c +++ b/src/base64/ft_base64_init.c @@ -18,7 +18,6 @@ void ft_base64_init(t_base64_ctx *ctx) { ctx->input_fd = STDIN_FILENO; ctx->output_fd = STDOUT_FILENO; - ft_bzero(ctx->chars, FT_BASE64_CHARS_SIZE); ft_memcpy(ctx->alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26); ft_memcpy(ctx->alphabet + 26, "abcdefghijklmnopqrstuvwxyz", 26); ft_memcpy(ctx->alphabet + 52, "0123456789+/", 12); diff --git a/src/base64/ft_base64_init_encode_buffer.c b/src/base64/ft_base64_init_encode_buffer.c new file mode 100644 index 00000000..6d01942d --- /dev/null +++ b/src/base64/ft_base64_init_encode_buffer.c @@ -0,0 +1,11 @@ +#include "ft_base64.h" +#include "libft.h" + +void ft_base64_init_encode_buffer +( + t_base64_encode_buffer *buff +) +{ + ft_bzero(buff->block, FT_BASE64_TRANS_SIZE); + buff->readed = 0; +} \ No newline at end of file