From e34551286f30b1ddb446cf5e0ed34233fc9974d2 Mon Sep 17 00:00:00 2001 From: Gregory Date: Sun, 17 Feb 2019 21:07:19 +0200 Subject: [PATCH] some shit --- Makefile | 2 +- inc/ft_base64.h | 13 +++++++++++-- src/base64/ft_base64_chunk.c | 29 +++++++++++++++++++++++++++++ src/base64/ft_base64_encode.c | 25 +++++++------------------ src/base64/ft_base64_encode_step.c | 19 ------------------- 5 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 src/base64/ft_base64_chunk.c delete mode 100644 src/base64/ft_base64_encode_step.c diff --git a/Makefile b/Makefile index 5565aeae..e98c4206 100644 --- a/Makefile +++ b/Makefile @@ -93,7 +93,7 @@ BASE64_SRC = ft_base64.c \ ft_base64_transform.c \ ft_base64_decode.c \ ft_base64_encode.c \ - ft_base64_encode_step.c \ + ft_base64_chunk.c \ ft_base64_encode_finish.c \ ft_base64_write.c diff --git a/inc/ft_base64.h b/inc/ft_base64.h index 2891a7e6..411b400a 100644 --- a/inc/ft_base64.h +++ b/inc/ft_base64.h @@ -15,7 +15,7 @@ # include -# define FT_BASE64_READ_SIZE 1048576 +# define FT_BASE64_READ_SIZE 1 # define FT_BASE64_TRANS_SIZE 3 # define FT_BASE64_CHARS_SIZE 4 # define FT_BASE64_ALPHABET_LENGTH 64 @@ -27,6 +27,8 @@ 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; @@ -60,7 +62,6 @@ void ft_base64_encode t_byte8 ft_base64_encode_step ( t_base64_ctx *ctx, - t_byte8 readed, t_byte1 *buff ); @@ -90,4 +91,12 @@ void ft_base64_write t_base64_ctx *ctx ); + +t_byte8 ft_base64_chunk +( + t_base64_ctx *ctx, + t_byte8 len, + t_byte1 *message +); + #endif diff --git a/src/base64/ft_base64_chunk.c b/src/base64/ft_base64_chunk.c new file mode 100644 index 00000000..ed8d04ae --- /dev/null +++ b/src/base64/ft_base64_chunk.c @@ -0,0 +1,29 @@ +#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 a7edcc61..44872f00 100644 --- a/src/base64/ft_base64_encode.c +++ b/src/base64/ft_base64_encode.c @@ -16,25 +16,14 @@ void ft_base64_encode(t_base64_ctx *c) { - t_byte1 buffer[FT_BASE64_READ_SIZE + FT_BASE64_TRANS_SIZE]; - t_byte8 readed; - t_byte8 reminder; - int fd; + t_byte1 buffer[FT_BASE64_READ_SIZE]; + int readed; + int head; + int reminder; reminder = 0; - fd = c->input_fd; - while ((readed = read(fd, buffer + reminder, FT_BASE64_READ_SIZE)) > 0) - { - if (readed + reminder >= FT_BASE64_TRANS_SIZE) - { - reminder = ft_base64_encode_step(c, readed + reminder, buffer); - ft_memmove(buffer, buffer + readed - reminder, reminder); - } - else - { - reminder += readed; - } - - } + 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); } diff --git a/src/base64/ft_base64_encode_step.c b/src/base64/ft_base64_encode_step.c deleted file mode 100644 index 35a62d3c..00000000 --- a/src/base64/ft_base64_encode_step.c +++ /dev/null @@ -1,19 +0,0 @@ -#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; -}