diff --git a/Makefile b/Makefile index be36aef6..db41f018 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,7 @@ SHA_SRC = ft_sha256_init.c \ BASE64_SRC = ft_base64_init.c \ ft_base64_fill_buffer.c \ - ft_base64_encode_buffer.c + ft_base64_encode.c SRC = main.c \ ft_ssl_init.c \ diff --git a/inc/ft_base64.h b/inc/ft_base64.h index 2af0a84e..975d75d9 100644 --- a/inc/ft_base64.h +++ b/inc/ft_base64.h @@ -15,7 +15,7 @@ # include -# define FT_BASE64_BLOCK_SIZE 3 +# define FT_BASE64_GLUE_BLOCK_SIZE 3 # define FT_BASE64_CHARS_SIZE 4 # define FT_BASE64_ALPHABET_LENGTH 64 @@ -24,7 +24,8 @@ typedef unsigned char t_byte1; typedef struct s_base64_ctx { - t_byte1 block[FT_BASE64_BLOCK_SIZE]; + 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; @@ -32,6 +33,6 @@ typedef struct s_base64_ctx void ft_base64_init(t_base64_ctx *ctx); void ft_base64_fill_buffer( t_base64_ctx *ctx, t_byte1 *data, t_byte8 size); -void ft_base64_encode_buffer(t_base64_ctx *ctx, t_byte8 len); +void ft_base64_encode(t_base64_ctx *ctx, t_byte1 *data); #endif diff --git a/inc/ft_ssl.h b/inc/ft_ssl.h index 32c019b6..cca128af 100644 --- a/inc/ft_ssl.h +++ b/inc/ft_ssl.h @@ -13,9 +13,18 @@ #ifndef FT_SSL_H # define FT_SSL_H +# include + # define FT_SSL_ALGS_COUNT 3 # define FT_SSL_BUFFER_SIZE 1024 +typedef enum e_alg_type +{ + DIGEST, + CIPHER, + END +} t_alg_type; + typedef struct s_ft_ssl t_ft_ssl; typedef void (*t_process_stdin)(t_ft_ssl *ft_ssl); @@ -38,6 +47,7 @@ typedef struct s_alorithm t_process_stdin process_stdin; t_process_file process_file; t_process_string process_string; + t_alg_type type; } t_algorithm; struct s_ft_ssl @@ -46,6 +56,7 @@ struct s_ft_ssl t_process_stdin process_stdin; t_process_file process_file; t_process_string process_string; + t_alg_type type; }; void ft_ssl_usage(void); diff --git a/inc/tests.h b/inc/tests.h index 3d2c97f4..1323f668 100644 --- a/inc/tests.h +++ b/inc/tests.h @@ -75,7 +75,6 @@ MunitResult should_init_base64_ctx(const MunitParameter test_params[], MunitResult should_fill_base64_buffer(const MunitParameter test_params[], void *test_data); -MunitResult should_clean_base64_buffer(const MunitParameter [], void *); -MunitResult should_encode_base64_buffer(const MunitParameter test_params[], +MunitResult should_encode_base64_data(const MunitParameter test_params[], void *test_data); #endif diff --git a/src/base64/ft_base64_encode.c b/src/base64/ft_base64_encode.c new file mode 100644 index 00000000..8b7736e1 --- /dev/null +++ b/src/base64/ft_base64_encode.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_base64_encode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/02 18:26:32 by gtertysh #+# #+# */ +/* Updated: 2019/02/02 18:26:51 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "ft_base64.h" + +void ft_base64_encode(t_base64_ctx *ctx, t_byte1 *data) +{ + t_byte1 first_char; + t_byte1 second_char; + t_byte1 third_char; + t_byte1 fourth_char; + + first_char = (data[0] >> 2) & 0x3f; + 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]; +} diff --git a/src/base64/ft_base64_encode_buffer.c b/src/base64/ft_base64_encode_buffer.c deleted file mode 100644 index 42a5138b..00000000 --- a/src/base64/ft_base64_encode_buffer.c +++ /dev/null @@ -1,27 +0,0 @@ -#include "libft.h" -#include "ft_base64.h" - -void ft_base64_encode_buffer(t_base64_ctx *ctx, t_byte8 len) -{ - t_byte1 first_char; - t_byte1 second_char; - t_byte1 third_char; - t_byte1 fourth_char; - - first_char = (ctx->block[0] >> 2) & 0x3f; - second_char = ((ctx->block[0] << 4) & 0x30) | ((ctx->block[1] >> 4) & 0xf); - third_char = ((ctx->block[1] << 2) & 0x3c) | ((ctx->block[2] >> 6) & 0x3); - fourth_char = ctx->block[2] & 0x3F; - - ctx->chars[0] = ctx->alphabet[first_char]; - ctx->chars[1] = ctx->alphabet[second_char]; - if (len < 2) - ctx->chars[2] = '='; - else - ctx->chars[2] = ctx->alphabet[third_char]; - if (len < 3) - ctx->chars[3] = '='; - else - ctx->chars[3] = ctx->alphabet[fourth_char]; - ft_bzero(ctx->block, FT_BASE64_BLOCK_SIZE); -} \ No newline at end of file diff --git a/src/base64/ft_base64_fill_buffer.c b/src/base64/ft_base64_fill_buffer.c index 2e5b2829..301040da 100644 --- a/src/base64/ft_base64_fill_buffer.c +++ b/src/base64/ft_base64_fill_buffer.c @@ -1,7 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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) +void ft_base64_fill_buffer(t_base64_ctx *ctx, t_byte1 *data, t_byte8 size) { - ft_memcpy(ctx->block, data, size); -} \ No newline at end of file + 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 6eb4dcf8..675055b3 100644 --- a/src/base64/ft_base64_init.c +++ b/src/base64/ft_base64_init.c @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/01/19 17:57:47 by gtertysh #+# #+# */ -/* Updated: 2019/01/19 17:58:16 by gtertysh ### ########.fr */ +/* Updated: 2019/02/02 18:29:20 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,14 +15,10 @@ void ft_base64_init(t_base64_ctx *ctx) { - char capital_case[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - char lower_case[] = "abcdefghijklmnopqrstuvwxyz"; - char rest[] = "0123456789+/"; - - ft_bzero(ctx->block, FT_BASE64_BLOCK_SIZE); + ctx->glue_block_bytes_count = 0; + ft_bzero(ctx->glue_block, FT_BASE64_GLUE_BLOCK_SIZE); ft_bzero(ctx->chars, FT_BASE64_CHARS_SIZE); - ft_memcpy(ctx->alphabet, capital_case, 26); - ft_memcpy(ctx->alphabet + 26, lower_case, 26); - ft_memcpy(ctx->alphabet + 52, rest, 12); - + ft_memcpy(ctx->alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26); + ft_memcpy(ctx->alphabet + 26, "abcdefghijklmnopqrstuvwxyz", 26); + ft_memcpy(ctx->alphabet + 52, "0123456789+/", 12); } diff --git a/src/ft_ssl_init.c b/src/ft_ssl_init.c index b8f66705..5ebc3cde 100644 --- a/src/ft_ssl_init.c +++ b/src/ft_ssl_init.c @@ -13,16 +13,30 @@ #include "ft_ssl.h" #include "libft.h" -static t_algorithm *init_algorithms(void) -{ - static t_algorithm algs[FT_SSL_ALGS_COUNT] = { - {"md5", ft_ssl_md5_stdin, ft_ssl_md5_file, ft_ssl_md5_string}, - {"sha256", ft_ssl_sha256_stdin, ft_ssl_sha256_file, ft_ssl_sha256_string}, - {"sha224", ft_ssl_sha224_stdin, ft_ssl_sha224_file, ft_ssl_sha224_string} - }; - - return (algs); -} +t_algorithm g_algorithms[] = { + { + "md5", + ft_ssl_md5_stdin, + ft_ssl_md5_file, + ft_ssl_md5_string, + DIGEST + }, + { + "sha256", + ft_ssl_sha256_stdin, + ft_ssl_sha256_file, + ft_ssl_sha256_string, + DIGEST + }, + { + "sha224", + ft_ssl_sha224_stdin, + ft_ssl_sha224_file, + ft_ssl_sha224_string, + DIGEST + }, + {NULL, NULL, NULL, NULL, END} +}; static void init_flags(t_ft_ssl *ft_ssl) { @@ -48,9 +62,11 @@ void ft_ssl_init(char *alg_name, t_ft_ssl *ft_ssl) i = 0; alg = 0; init_flags(ft_ssl); - alg_walker = init_algorithms(); - while (i < FT_SSL_ALGS_COUNT) + alg_walker = g_algorithms; + while (alg_walker) { + if (alg_walker[i].type == END) + break ; if (ft_strcmp(alg_name, alg_walker[i].name) == 0) { alg = &alg_walker[i]; diff --git a/src/ft_ssl_usage.c b/src/ft_ssl_usage.c index f30e8134..37141119 100644 --- a/src/ft_ssl_usage.c +++ b/src/ft_ssl_usage.c @@ -15,9 +15,13 @@ void ft_ssl_usage(void) { - ft_putstr("\n\nusage: \n"); - ft_putstr("ft_ssl algorithm [-p|-q|-r] [[-s string...] [file...]]\n\n"); - ft_putstr("algorithms:\n"); - ft_putstr("md5 sha256 sha224\n\n\n"); + ft_putstr("Message Digest commands:\n"); + ft_putstr("md5\nsha256\nsha224\n\n"); + ft_putstr("Digest command usage:\n"); + ft_putstr("ft_ssl digest [-p|-q|-r] [[-s string...] [file...]]\n\n"); + ft_putstr("Cipher commands:\n"); + ft_putstr("base64\n"); + ft_putstr("Cipher command usage:\n"); + ft_putstr("ft_ssl cipher [-p|-q|-r] [[-s string...] [file...]]\n\n"); exit(1); } diff --git a/t/base64_tests.c b/t/base64_tests.c index 158af5d7..59f1828f 100644 --- a/t/base64_tests.c +++ b/t/base64_tests.c @@ -7,17 +7,19 @@ TEST_RESULT should_init_base64_ctx(TEST_PARAMS, TEST_DATA) UNUSED(test_params); UNUSED(test_data); t_base64_ctx ctx; - char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\ +abcdefghijklmnopqrstuvwxyz0123456789+/"; ft_base64_init(&ctx); - for (int i = 0; i < FT_BASE64_BLOCK_SIZE; i++) - munit_assert_uchar(ctx.block[i], ==, 0); + 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; } @@ -31,14 +33,17 @@ TEST_RESULT should_fill_base64_buffer(TEST_PARAMS, TEST_DATA) ft_base64_init(&ctx); - t_byte1 data[] = "123"; - ft_base64_fill_buffer(&ctx, data, 3); + 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); - munit_assert_memory_equal(3, ctx.block, data); + 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_clean_base64_buffer(TEST_PARAMS, TEST_DATA) +TEST_RESULT should_encode_base64_data(TEST_PARAMS, TEST_DATA) { UNUSED(test_params); UNUSED(test_data); @@ -47,32 +52,10 @@ TEST_RESULT should_clean_base64_buffer(TEST_PARAMS, TEST_DATA) ft_base64_init(&ctx); - ft_base64_fill_buffer(&ctx, (t_byte1 *)"Man", 3); - ft_base64_encode_buffer(&ctx, 3); - for (int i = 0; i < FT_BASE64_BLOCK_SIZE; i++) - munit_assert_uchar(ctx.block[i], ==, 0); - return MUNIT_OK; -} - -TEST_RESULT should_encode_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 *)"Man", 3); - ft_base64_encode_buffer(&ctx, 3); + ft_base64_encode(&ctx, (t_byte1 *)"Man"); munit_assert_string_equal((char *)ctx.chars, "TWFu"); - ft_base64_fill_buffer(&ctx, (t_byte1 *)"Ma", 2); - ft_base64_encode_buffer(&ctx, 2); - munit_assert_string_equal((char *)ctx.chars, "TWE="); - - ft_base64_fill_buffer(&ctx, (t_byte1 *)"M", 1); - ft_base64_encode_buffer(&ctx, 1); - munit_assert_string_equal((char *)ctx.chars, "TQ=="); + ft_base64_encode(&ctx, (t_byte1 *)"LOL"); + munit_assert_string_equal((char *)ctx.chars, "TE9M"); return MUNIT_OK; } \ No newline at end of file diff --git a/t/tests.c b/t/tests.c index 2dc6cb0d..9999ab95 100644 --- a/t/tests.c +++ b/t/tests.c @@ -43,8 +43,7 @@ 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("/cleans_buffer", should_clean_base64_buffer, NULL, NULL, 0, NULL), - IT("/encodes_buffer", should_encode_base64_buffer, NULL, NULL, 0, NULL), + IT("/encodes_data", should_encode_base64_data, NULL, NULL, 0, NULL), END_IT };