basic main for base64

This commit is contained in:
Gregory 2019-02-07 23:22:40 +02:00
parent 8fe226598b
commit 4d13803ce3
11 changed files with 168 additions and 41 deletions

View file

@ -86,8 +86,11 @@ SHA_SRC = ft_sha256.c \
ft_sha_print.c \ ft_sha_print.c \
ft_sha_usage.c ft_sha_usage.c
BASE64_SRC = ft_base64_init.c \ BASE64_SRC = ft_base64.c \
ft_base64_init.c \
ft_base64_fill_buffer.c \ ft_base64_fill_buffer.c \
ft_base64_transform.c \
ft_base64_decode.c \
ft_base64_encode.c ft_base64_encode.c
SRC = main.c \ SRC = main.c \
@ -163,10 +166,10 @@ CC := clang
# rules # rules
$(NAME): $(LIBFT) $(OBJ) $(NAME): $(LIBFT) $(OBJ) $(HEADERS)
$(CC) $(OBJ) $(LINK_FLAGS) -o $(NAME) $(CC) $(OBJ) $(LINK_FLAGS) -o $(NAME)
$(TEST_BIN): $(LIBFT) $(TEST_OBJ) $(TEST_BIN): $(LIBFT) $(TEST_OBJ) $(HEADERS)
$(CC) $(TEST_OBJ) $(LINK_FLAGS) -o $(TEST_BIN) $(CC) $(TEST_OBJ) $(LINK_FLAGS) -o $(TEST_BIN)
$(TEST_OBJ) $(OBJ): | $(OBJ_DIR) $(TEST_OBJ) $(OBJ): | $(OBJ_DIR)
@ -174,7 +177,7 @@ $(TEST_OBJ) $(OBJ): | $(OBJ_DIR)
$(OBJ_DIR): $(OBJ_DIR):
mkdir $(OBJ_DIR) mkdir $(OBJ_DIR)
$(OBJ_DIR)%.o: %.c $(HEADERS) $(OBJ_DIR)%.o: %.c
$(CC) -c $< -o $@ $(CC_FLAGS) $(HEADER_FLAGS) $(CC) -c $< -o $@ $(CC_FLAGS) $(HEADER_FLAGS)
$(LIBFT): $(LIBFT):

View file

@ -24,15 +24,51 @@ typedef unsigned char t_byte1;
typedef struct s_base64_ctx typedef struct s_base64_ctx
{ {
int input_fd;
int output_fd;
t_byte8 glue_block_bytes_count; t_byte8 glue_block_bytes_count;
t_byte1 glue_block[FT_BASE64_GLUE_BLOCK_SIZE]; t_byte1 glue_block[FT_BASE64_GLUE_BLOCK_SIZE];
t_byte1 alphabet[FT_BASE64_ALPHABET_LENGTH]; t_byte1 alphabet[FT_BASE64_ALPHABET_LENGTH];
t_byte1 chars[FT_BASE64_CHARS_SIZE]; t_byte1 chars[FT_BASE64_CHARS_SIZE];
} t_base64_ctx; } t_base64_ctx;
void ft_base64_init(t_base64_ctx *ctx); typedef struct s_base64_flags
void ft_base64_fill_buffer( {
t_base64_ctx *ctx, t_byte1 *data, t_byte8 size); int decode;
void ft_base64_encode(t_base64_ctx *ctx, t_byte1 *data); } t_base64_flags;
void ft_base64
(
int argc,
char **argv
);
void ft_base64_init
(
t_base64_ctx *ctx
);
void ft_base64_decode
(
t_base64_ctx *ctx
);
void ft_base64_encode
(
t_base64_ctx *ctx
);
void ft_base64_fill_buffer
(
t_base64_ctx *ctx,
t_byte1 *data,
t_byte8 size
);
void ft_base64_transform
(
t_base64_ctx *ctx,
t_byte1 *data
);
#endif #endif

View file

@ -75,6 +75,6 @@ MunitResult should_init_base64_ctx(const MunitParameter test_params[],
MunitResult should_fill_base64_buffer(const MunitParameter test_params[], MunitResult should_fill_base64_buffer(const MunitParameter test_params[],
void *test_data); void *test_data);
MunitResult should_encode_base64_data(const MunitParameter test_params[], MunitResult should_transform_base64_block(const MunitParameter test_params[],
void *test_data); void *test_data);
#endif #endif

64
src/base64/ft_base64.c Normal file
View file

@ -0,0 +1,64 @@
#include "libft.h"
#include "ft_base64.h"
static void open_input_stream(t_base64_ctx *ctx, char *filename)
{
(void)ctx;
(void)filename;
}
static void open_output_stream(t_base64_ctx *ctx, char *filename)
{
(void)ctx;
(void)filename;
}
static void init_flags(t_base64_flags *flags)
{
flags->decode = 0;
}
static void read_args
(
int argc,
char **argv,
t_base64_flags *flags,
t_base64_ctx *ctx
)
{
int i;
char *current_arg;
char *next_arg;
i = 0;
while(i < argc)
{
current_arg = argv[i];
next_arg = i + 1 < argc ? argv[i + 1] : NULL;
if (ft_strcmp(current_arg, "-d") == 0)
flags->decode = 1;
else if (ft_strcmp(current_arg, "-e") == 0 && i++)
continue;
else if (ft_strcmp(current_arg, "-i") == 0)
open_input_stream(ctx, next_arg);
else if (ft_strcmp(current_arg, "-o") == 0)
open_output_stream(ctx, next_arg);
else if (i++)
continue;
i++;
}
}
void ft_base64(int argc, char **argv)
{
t_base64_flags flags;
t_base64_ctx ctx;
init_flags(&flags);
read_args(argc, argv, &flags, &ctx);
if (flags.decode)
ft_base64_decode(&ctx);
else
ft_base64_encode(&ctx);
exit(0);
}

View file

@ -0,0 +1,6 @@
#include "ft_base64.h"
void ft_base64_decode(t_base64_ctx *ctx)
{
(void)ctx;
}

View file

@ -1,31 +1,6 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_encode.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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" #include "ft_base64.h"
void ft_base64_encode(t_base64_ctx *ctx, t_byte1 *data) void ft_base64_encode(t_base64_ctx *ctx)
{ {
t_byte1 first_char; (void)ctx;
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];
} }

View file

@ -12,10 +12,13 @@
#include "ft_base64.h" #include "ft_base64.h"
#include "libft.h" #include "libft.h"
#include <unistd.h>
void ft_base64_init(t_base64_ctx *ctx) void ft_base64_init(t_base64_ctx *ctx)
{ {
ctx->glue_block_bytes_count = 0; ctx->glue_block_bytes_count = 0;
ctx->input_fd = STDIN_FILENO;
ctx->output_fd = STDOUT_FILENO;
ft_bzero(ctx->glue_block, FT_BASE64_GLUE_BLOCK_SIZE); ft_bzero(ctx->glue_block, FT_BASE64_GLUE_BLOCK_SIZE);
ft_bzero(ctx->chars, FT_BASE64_CHARS_SIZE); ft_bzero(ctx->chars, FT_BASE64_CHARS_SIZE);
ft_memcpy(ctx->alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26); ft_memcpy(ctx->alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26);

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_transform.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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_transform(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];
}

View file

@ -13,6 +13,7 @@
#include "ft_ssl.h" #include "ft_ssl.h"
#include "ft_md5.h" #include "ft_md5.h"
#include "ft_sha.h" #include "ft_sha.h"
#include "ft_base64.h"
#include "libft.h" #include "libft.h"
t_algorithm g_algorithms[] = { t_algorithm g_algorithms[] = {
@ -28,6 +29,10 @@ t_algorithm g_algorithms[] = {
"sha224", "sha224",
ft_sha224, ft_sha224,
}, },
{
"base64",
ft_base64,
},
{NULL, NULL} {NULL, NULL}
}; };

View file

@ -1,3 +1,4 @@
#include <unistd.h>
#include "tests.h" #include "tests.h"
#include "tests_macros.h" #include "tests_macros.h"
#include "ft_base64.h" #include "ft_base64.h"
@ -12,6 +13,9 @@ abcdefghijklmnopqrstuvwxyz0123456789+/";
ft_base64_init(&ctx); ft_base64_init(&ctx);
munit_assert_true(ctx.input_fd == STDIN_FILENO);
munit_assert_true(ctx.output_fd == STDOUT_FILENO);
for (int i = 0; i < FT_BASE64_GLUE_BLOCK_SIZE; i++) for (int i = 0; i < FT_BASE64_GLUE_BLOCK_SIZE; i++)
munit_assert_uchar(ctx.glue_block[i], ==, 0); munit_assert_uchar(ctx.glue_block[i], ==, 0);
@ -43,7 +47,7 @@ TEST_RESULT should_fill_base64_buffer(TEST_PARAMS, TEST_DATA)
return MUNIT_OK; return MUNIT_OK;
} }
TEST_RESULT should_encode_base64_data(TEST_PARAMS, TEST_DATA) TEST_RESULT should_transform_base64_block(TEST_PARAMS, TEST_DATA)
{ {
UNUSED(test_params); UNUSED(test_params);
UNUSED(test_data); UNUSED(test_data);
@ -52,10 +56,10 @@ TEST_RESULT should_encode_base64_data(TEST_PARAMS, TEST_DATA)
ft_base64_init(&ctx); ft_base64_init(&ctx);
ft_base64_encode(&ctx, (t_byte1 *)"Man"); ft_base64_transform(&ctx, (t_byte1 *)"Man");
munit_assert_string_equal((char *)ctx.chars, "TWFu"); munit_assert_string_equal((char *)ctx.chars, "TWFu");
ft_base64_encode(&ctx, (t_byte1 *)"LOL"); ft_base64_transform(&ctx, (t_byte1 *)"LOL");
munit_assert_string_equal((char *)ctx.chars, "TE9M"); munit_assert_string_equal((char *)ctx.chars, "TE9M");
return MUNIT_OK; return MUNIT_OK;
} }

View file

@ -43,7 +43,7 @@ MunitTest g_sha_tests[] = {
MunitTest g_base64_tests[] = { MunitTest g_base64_tests[] = {
IT("/init_ctx", should_init_base64_ctx, NULL, NULL, 0, NULL), IT("/init_ctx", should_init_base64_ctx, NULL, NULL, 0, NULL),
IT("/fills_buffer", should_fill_base64_buffer, NULL, NULL, 0, NULL), IT("/fills_buffer", should_fill_base64_buffer, NULL, NULL, 0, NULL),
IT("/encodes_data", should_encode_base64_data, NULL, NULL, 0, NULL), IT("/transform_block", should_transform_base64_block, NULL, NULL, 0, NULL),
END_IT END_IT
}; };