diff --git a/inc/ft_base64.h b/inc/ft_base64.h index 59bdd87b..2891a7e6 100644 --- a/inc/ft_base64.h +++ b/inc/ft_base64.h @@ -15,7 +15,7 @@ # include -# define FT_BASE64_READ_SIZE 3 +# define FT_BASE64_READ_SIZE 1048576 # define FT_BASE64_TRANS_SIZE 3 # define FT_BASE64_CHARS_SIZE 4 # define FT_BASE64_ALPHABET_LENGTH 64 diff --git a/src/base64/ft_base64_encode.c b/src/base64/ft_base64_encode.c index 67922c3f..a7edcc61 100644 --- a/src/base64/ft_base64_encode.c +++ b/src/base64/ft_base64_encode.c @@ -14,26 +14,27 @@ #include "ft_base64.h" #include "libft.h" -void ft_base64_encode(t_base64_ctx *ctx) +void ft_base64_encode(t_base64_ctx *c) { - t_byte1 buff[FT_BASE64_READ_SIZE + FT_BASE64_TRANS_SIZE]; + t_byte1 buffer[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) + fd = c->input_fd; + while ((readed = read(fd, buffer + reminder, FT_BASE64_READ_SIZE)) > 0) { - if (readed >= FT_BASE64_TRANS_SIZE) + if (readed + reminder >= FT_BASE64_TRANS_SIZE) { - reminder = ft_base64_encode_step(ctx, readed, buff); - ft_memmove(buff, buff + readed - reminder, reminder); + reminder = ft_base64_encode_step(c, readed + reminder, buffer); + ft_memmove(buffer, buffer + readed - reminder, reminder); } else { reminder += readed; } + } - // ft_base64_encode_finish(ctx, reminder, buff); + ft_base64_encode_finish(c, reminder, buffer); } diff --git a/src/base64/ft_base64_encode_finish.c b/src/base64/ft_base64_encode_finish.c index 89c321c6..6feb3295 100644 --- a/src/base64/ft_base64_encode_finish.c +++ b/src/base64/ft_base64_encode_finish.c @@ -8,9 +8,17 @@ void ft_base64_encode_finish t_byte1 *buffer ) { + t_byte8 padding_size; + + if (!reminder) + { + 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 - reminder, '=', FT_BASE64_TRANS_SIZE - reminder); + ft_memset(ctx->chars + FT_BASE64_CHARS_SIZE - padding_size, '=', FT_BASE64_CHARS_SIZE - padding_size); ft_base64_write(ctx); ft_putstr("\n"); } \ No newline at end of file diff --git a/t/cases/M.txt b/t/cases/M.txt new file mode 100644 index 00000000..ef6bce1d --- /dev/null +++ b/t/cases/M.txt @@ -0,0 +1 @@ +M \ No newline at end of file diff --git a/t/cases/Man_is_distinguished.txt b/t/cases/Man_is_distinguished.txt new file mode 100644 index 00000000..aed8215f --- /dev/null +++ b/t/cases/Man_is_distinguished.txt @@ -0,0 +1 @@ +Man is distinguished \ No newline at end of file diff --git a/t/cases/long_text.txt b/t/cases/long_text.txt new file mode 100644 index 00000000..5feaed1e --- /dev/null +++ b/t/cases/long_text.txt @@ -0,0 +1 @@ +Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure. \ No newline at end of file