From 9f93b552304ef3bfb2229324b37a4b802ba414c0 Mon Sep 17 00:00:00 2001 From: Gregory Date: Sat, 23 Feb 2019 23:56:27 +0200 Subject: [PATCH] add decode. base64 without error management done --- Makefile | 7 +++- inc/ft_base64.h | 48 ++++++++++++++++++++--- src/base64/ft_base64.c | 4 +- src/base64/ft_base64_decode.c | 13 +++++- src/base64/ft_base64_decode_chunk.c | 47 ++++++++++++++++++++++ src/base64/ft_base64_decode_filter.c | 25 ++++++++++++ src/base64/ft_base64_decode_finish.c | 29 ++++++++++++++ src/base64/ft_base64_decode_transform.c | 26 ++++++++++++ src/base64/ft_base64_encode_chunk.c | 14 +++---- src/base64/ft_base64_encode_finish.c | 11 +++--- src/base64/ft_base64_encode_transform.c | 4 +- src/base64/ft_base64_init_decode_buffer.c | 11 ++++++ src/base64/ft_base64_init_encode_buffer.c | 2 +- t/base64_tests.c | 13 +++--- 14 files changed, 222 insertions(+), 32 deletions(-) create mode 100644 src/base64/ft_base64_decode_chunk.c create mode 100644 src/base64/ft_base64_decode_filter.c create mode 100644 src/base64/ft_base64_decode_finish.c create mode 100644 src/base64/ft_base64_decode_transform.c create mode 100644 src/base64/ft_base64_init_decode_buffer.c diff --git a/Makefile b/Makefile index da642dfc..44c4b195 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,12 @@ BASE64_SRC = ft_base64.c \ ft_base64_init_encode_buffer.c \ ft_base64_encode_chunk.c \ ft_base64_encode_transform.c \ - ft_base64_encode_finish.c + ft_base64_encode_finish.c \ + ft_base64_init_decode_buffer.c \ + ft_base64_decode_chunk.c \ + ft_base64_decode_filter.c \ + ft_base64_decode_transform.c \ + ft_base64_decode_finish.c SRC = main.c \ ft_ssl_usage.c diff --git a/inc/ft_base64.h b/inc/ft_base64.h index 7c5ef338..fcf9e7a7 100644 --- a/inc/ft_base64.h +++ b/inc/ft_base64.h @@ -16,8 +16,8 @@ # include # define FT_BASE64_READ_SIZE 1024 -# define FT_BASE64_TRANS_SIZE 3 -# define FT_BASE64_CHARS_SIZE 4 +# define FT_BASE64_ENCODE_BLOCK_SIZE 3 +# define FT_BASE64_DECODE_BLOCK_SIZE 4 # define FT_BASE64_ALPHABET_LENGTH 64 typedef uint64_t t_byte8; @@ -25,10 +25,16 @@ typedef unsigned char t_byte1; typedef struct s_base64_encode_buffer { - t_byte1 block[FT_BASE64_TRANS_SIZE]; + t_byte1 block[FT_BASE64_ENCODE_BLOCK_SIZE]; t_byte8 readed; } t_base64_encode_buffer; +typedef struct s_base64_decode_buffer +{ + t_byte1 block[FT_BASE64_DECODE_BLOCK_SIZE]; + t_byte8 readed; +} t_base64_decode_buffer; + typedef struct s_base64_ctx { int input_fd; @@ -85,8 +91,8 @@ void ft_base64_fill_buffer 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 data[FT_BASE64_ENCODE_BLOCK_SIZE], + t_byte1 chars[FT_BASE64_DECODE_BLOCK_SIZE] ); void ft_base64_encode_chunk @@ -102,4 +108,36 @@ void ft_base64_init_encode_buffer t_base64_encode_buffer *buff ); +void ft_base64_init_decode_buffer +( + t_base64_decode_buffer *buff +); + +void ft_base64_decode_chunk +( + t_base64_ctx *ctx, + t_byte8 len, + t_byte1 *message, + t_base64_decode_buffer *dec_buff +); + +void ft_base64_decode_transform +( + t_base64_ctx *ctx, + t_byte1 *message, + t_byte1 decoded_block[FT_BASE64_ENCODE_BLOCK_SIZE] +); + +void ft_base64_decode_finish +( + t_base64_ctx *ctx, + t_base64_decode_buffer *buff +); + +t_byte8 ft_base64_decode_filter +( + t_byte8 readed, + t_byte1 *buff +); + #endif diff --git a/src/base64/ft_base64.c b/src/base64/ft_base64.c index ed822ea2..f9fdf404 100644 --- a/src/base64/ft_base64.c +++ b/src/base64/ft_base64.c @@ -1,8 +1,8 @@ #include +#include +#include #include "libft.h" #include "ft_base64.h" -#include "fcntl.h" -#include "sys/stat.h" static int open_stream(char *filename, int flags, int mode) { diff --git a/src/base64/ft_base64_decode.c b/src/base64/ft_base64_decode.c index aed29d83..00ac0903 100644 --- a/src/base64/ft_base64_decode.c +++ b/src/base64/ft_base64_decode.c @@ -1,6 +1,17 @@ +#include #include "ft_base64.h" void ft_base64_decode(t_base64_ctx *ctx) { - (void)ctx; + t_byte1 read_buff[FT_BASE64_READ_SIZE]; + t_base64_decode_buffer decode_buff; + t_byte8 readed; + + ft_base64_init_decode_buffer(&decode_buff); + while((readed = read(ctx->input_fd, read_buff, FT_BASE64_READ_SIZE)) > 0) + { + readed = ft_base64_decode_filter(readed, read_buff); + ft_base64_decode_chunk(ctx, readed, read_buff, &decode_buff); + } + ft_base64_decode_finish(ctx, &decode_buff); } \ No newline at end of file diff --git a/src/base64/ft_base64_decode_chunk.c b/src/base64/ft_base64_decode_chunk.c new file mode 100644 index 00000000..a800d452 --- /dev/null +++ b/src/base64/ft_base64_decode_chunk.c @@ -0,0 +1,47 @@ +#include +#include "libft.h" +#include "ft_base64.h" + +static void ft_base64_decode_write +( + t_base64_ctx *ctx, + t_byte1 *block +) +{ + t_byte1 decoded_block[FT_BASE64_ENCODE_BLOCK_SIZE]; + + ft_base64_decode_transform(ctx, block, decoded_block); + write(ctx->output_fd, decoded_block, FT_BASE64_ENCODE_BLOCK_SIZE); +} + +void ft_base64_decode_chunk +( + t_base64_ctx *ctx, + t_byte8 message_len, + t_byte1 *message, + t_base64_decode_buffer *buff +) +{ + t_byte8 free_space_in_buffer; + t_byte8 buffer_index; + t_byte8 idx; + + buffer_index = buff->readed % FT_BASE64_DECODE_BLOCK_SIZE; + buff->readed += message_len; + free_space_in_buffer = FT_BASE64_DECODE_BLOCK_SIZE - buffer_index; + if (message_len >= free_space_in_buffer) + { + ft_memcpy(&buff->block[buffer_index], message, free_space_in_buffer); + ft_base64_decode_write(ctx, buff->block); + idx = free_space_in_buffer; + while(idx + FT_BASE64_DECODE_BLOCK_SIZE <= message_len) + { + ft_base64_decode_write(ctx, &message[idx]); + idx += FT_BASE64_DECODE_BLOCK_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_decode_filter.c b/src/base64/ft_base64_decode_filter.c new file mode 100644 index 00000000..9d74a138 --- /dev/null +++ b/src/base64/ft_base64_decode_filter.c @@ -0,0 +1,25 @@ +#include "ft_base64.h" +#include "libft.h" + +t_byte8 ft_base64_decode_filter +( + t_byte8 readed, + t_byte1 *buffer +) +{ + t_byte8 i; + t_byte8 last_free_index; + + i = 0; + last_free_index = 0; + while(i < readed) + { + if (!ft_isspace(buffer[i])) + { + buffer[last_free_index] = buffer[i]; + last_free_index++; + } + i++; + } + return (last_free_index); +} \ No newline at end of file diff --git a/src/base64/ft_base64_decode_finish.c b/src/base64/ft_base64_decode_finish.c new file mode 100644 index 00000000..e72fa6bf --- /dev/null +++ b/src/base64/ft_base64_decode_finish.c @@ -0,0 +1,29 @@ +#include "ft_base64.h" +#include "libft.h" + +void ft_base64_decode_finish +( + t_base64_ctx *ctx, + t_base64_decode_buffer *buff +) +{ + t_byte8 buffer_index; + t_byte8 padding_size; + // t_byte1 chars[FT_BASE64_ENCODE_BLOCK_SIZE]; + + buffer_index = buff->readed % FT_BASE64_DECODE_BLOCK_SIZE; + padding_size = FT_BASE64_DECODE_BLOCK_SIZE - buffer_index; + if (!buff->readed) + return ; + if (!buffer_index) + { + write(ctx->output_fd, "\n", 1); + return ; + } + // ft_bzero(&buff->block[buffer_index], padding_size); + // ft_base64_encode_transform(ctx, buff->block, chars); + // ft_memset(chars + FT_BASE64_DECODE_BLOCK_SIZE - padding_size, '=', + // padding_size); + // write(ctx->output_fd, chars, FT_BASE64_DECODE_BLOCK_SIZE); + // write(ctx->output_fd, "\n", 1); +} \ No newline at end of file diff --git a/src/base64/ft_base64_decode_transform.c b/src/base64/ft_base64_decode_transform.c new file mode 100644 index 00000000..c6a26396 --- /dev/null +++ b/src/base64/ft_base64_decode_transform.c @@ -0,0 +1,26 @@ +#include "ft_base64.h" +#include "libft.h" + +void ft_base64_decode_transform +( + t_base64_ctx *ctx, + t_byte1 blk[FT_BASE64_DECODE_BLOCK_SIZE], + t_byte1 decoded_block[FT_BASE64_ENCODE_BLOCK_SIZE] +) +{ + t_byte1 first_index; + t_byte1 second_index; + t_byte1 third_index; + t_byte1 fourth_index; + const char *alphabet; + + alphabet = (const char *)ctx->alphabet; + first_index = ft_strchr(alphabet, blk[0]) - alphabet; + second_index = ft_strchr(alphabet, blk[1]) - alphabet; + third_index = blk[2] == '=' ? 0 : ft_strchr(alphabet, blk[2]) - alphabet; + fourth_index = blk[3] == '=' ? 0 : ft_strchr(alphabet, blk[3]) - alphabet; + + decoded_block[0] = first_index << 2 | second_index >> 4; + decoded_block[1] = second_index << 4 | third_index >> 2; + decoded_block[2] = third_index << 6 | fourth_index; +} \ No newline at end of file diff --git a/src/base64/ft_base64_encode_chunk.c b/src/base64/ft_base64_encode_chunk.c index 443380bf..bb40023b 100644 --- a/src/base64/ft_base64_encode_chunk.c +++ b/src/base64/ft_base64_encode_chunk.c @@ -4,13 +4,13 @@ static void ft_base64_encode_write ( t_base64_ctx *ctx, - t_byte1 buff[FT_BASE64_TRANS_SIZE] + t_byte1 buff[FT_BASE64_ENCODE_BLOCK_SIZE] ) { - t_byte1 chars[FT_BASE64_CHARS_SIZE]; + t_byte1 chars[FT_BASE64_DECODE_BLOCK_SIZE]; ft_base64_encode_transform(ctx, buff, chars); - write(ctx->output_fd, chars, FT_BASE64_CHARS_SIZE); + write(ctx->output_fd, chars, FT_BASE64_DECODE_BLOCK_SIZE); } void ft_base64_encode_chunk @@ -25,18 +25,18 @@ void ft_base64_encode_chunk t_byte8 buffer_index; t_byte8 idx; - buffer_index = buff->readed % FT_BASE64_TRANS_SIZE; + buffer_index = buff->readed % FT_BASE64_ENCODE_BLOCK_SIZE; buff->readed += message_len; - free_space_in_buffer = FT_BASE64_TRANS_SIZE - buffer_index; + free_space_in_buffer = FT_BASE64_ENCODE_BLOCK_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) + while(idx + FT_BASE64_ENCODE_BLOCK_SIZE <= message_len) { ft_base64_encode_write(ctx, &message[idx]); - idx += FT_BASE64_TRANS_SIZE; + idx += FT_BASE64_ENCODE_BLOCK_SIZE; } buffer_index = 0; } diff --git a/src/base64/ft_base64_encode_finish.c b/src/base64/ft_base64_encode_finish.c index 0a93591b..fcdb58a4 100644 --- a/src/base64/ft_base64_encode_finish.c +++ b/src/base64/ft_base64_encode_finish.c @@ -9,10 +9,10 @@ void ft_base64_encode_finish { t_byte8 buffer_index; t_byte8 padding_size; - t_byte1 chars[FT_BASE64_CHARS_SIZE]; + t_byte1 chars[FT_BASE64_DECODE_BLOCK_SIZE]; - buffer_index = buff->readed % FT_BASE64_TRANS_SIZE; - padding_size = FT_BASE64_TRANS_SIZE - buffer_index; + buffer_index = buff->readed % FT_BASE64_ENCODE_BLOCK_SIZE; + padding_size = FT_BASE64_ENCODE_BLOCK_SIZE - buffer_index; if (!buff->readed) return ; if (!buffer_index) @@ -22,7 +22,8 @@ void ft_base64_encode_finish } 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_memset(chars + FT_BASE64_DECODE_BLOCK_SIZE - padding_size, '=', + padding_size); + write(ctx->output_fd, chars, FT_BASE64_DECODE_BLOCK_SIZE); write(ctx->output_fd, "\n", 1); } \ No newline at end of file diff --git a/src/base64/ft_base64_encode_transform.c b/src/base64/ft_base64_encode_transform.c index fd320daa..134b5d67 100644 --- a/src/base64/ft_base64_encode_transform.c +++ b/src/base64/ft_base64_encode_transform.c @@ -16,8 +16,8 @@ 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 data[FT_BASE64_ENCODE_BLOCK_SIZE], + t_byte1 chars[FT_BASE64_DECODE_BLOCK_SIZE] ) { t_byte1 first_char; diff --git a/src/base64/ft_base64_init_decode_buffer.c b/src/base64/ft_base64_init_decode_buffer.c new file mode 100644 index 00000000..1699e7d4 --- /dev/null +++ b/src/base64/ft_base64_init_decode_buffer.c @@ -0,0 +1,11 @@ +#include "ft_base64.h" +#include "libft.h" + +void ft_base64_init_decode_buffer +( + t_base64_decode_buffer *buff +) +{ + ft_bzero(buff->block, FT_BASE64_DECODE_BLOCK_SIZE); + buff->readed = 0; +} \ No newline at end of file diff --git a/src/base64/ft_base64_init_encode_buffer.c b/src/base64/ft_base64_init_encode_buffer.c index 6d01942d..2a4351b4 100644 --- a/src/base64/ft_base64_init_encode_buffer.c +++ b/src/base64/ft_base64_init_encode_buffer.c @@ -6,6 +6,6 @@ void ft_base64_init_encode_buffer t_base64_encode_buffer *buff ) { - ft_bzero(buff->block, FT_BASE64_TRANS_SIZE); + ft_bzero(buff->block, FT_BASE64_ENCODE_BLOCK_SIZE); buff->readed = 0; } \ No newline at end of file diff --git a/t/base64_tests.c b/t/base64_tests.c index d91984f7..26431f5c 100644 --- a/t/base64_tests.c +++ b/t/base64_tests.c @@ -16,10 +16,6 @@ abcdefghijklmnopqrstuvwxyz0123456789+/"; munit_assert_true(ctx.input_fd == STDIN_FILENO); munit_assert_true(ctx.output_fd == STDOUT_FILENO); - - 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); return MUNIT_OK; @@ -31,13 +27,14 @@ TEST_RESULT should_transform_base64_block(TEST_PARAMS, TEST_DATA) UNUSED(test_data); t_base64_ctx ctx; + t_byte1 buff[FT_BASE64_DECODE_BLOCK_SIZE]; ft_base64_init(&ctx); - ft_base64_transform(&ctx, (t_byte1 *)"Man"); - munit_assert_string_equal((char *)ctx.chars, "TWFu"); + ft_base64_encode_transform(&ctx, (t_byte1 *)"Man", buff); + munit_assert_string_equal((char *)buff, "TWFu"); - ft_base64_transform(&ctx, (t_byte1 *)"LOL"); - munit_assert_string_equal((char *)ctx.chars, "TE9M"); + ft_base64_encode_transform(&ctx, (t_byte1 *)"LOL", buff); + munit_assert_string_equal((char *)buff, "TE9M"); return MUNIT_OK; } \ No newline at end of file