basic base64 encoding

This commit is contained in:
Gregory 2019-02-10 23:11:01 +02:00
parent 4d13803ce3
commit cebeb51867
14 changed files with 124 additions and 70 deletions

View file

@ -31,9 +31,11 @@ TST_DIR := $(ROOT)/t/
MD5_HEADER := $(INC_DIR)/ft_md5.h MD5_HEADER := $(INC_DIR)/ft_md5.h
SHA_HEADER := $(INC_DIR)/ft_sha.h SHA_HEADER := $(INC_DIR)/ft_sha.h
SSL_HEADER := $(INC_DIR)/ft_ssl.h SSL_HEADER := $(INC_DIR)/ft_ssl.h
B64_HEADER := $(INC_DIR)/ft_base64.h
HEADERS := $(MD5_HEADER) \ HEADERS := $(MD5_HEADER) \
$(SHA_HEADER) \ $(SHA_HEADER) \
$(SSL_HEADER) $(SSL_HEADER) \
$(B64_HEADER)
# libraries # libraries
LIBFT_DIR := $(LIB_DIR)libft/ LIBFT_DIR := $(LIB_DIR)libft/
@ -88,10 +90,12 @@ SHA_SRC = ft_sha256.c \
BASE64_SRC = ft_base64.c \ BASE64_SRC = ft_base64.c \
ft_base64_init.c \ ft_base64_init.c \
ft_base64_fill_buffer.c \
ft_base64_transform.c \ ft_base64_transform.c \
ft_base64_decode.c \ ft_base64_decode.c \
ft_base64_encode.c ft_base64_encode.c \
ft_base64_encode_step.c \
ft_base64_encode_finish.c \
ft_base64_write.c
SRC = main.c \ SRC = main.c \
ft_ssl_usage.c ft_ssl_usage.c
@ -166,10 +170,10 @@ CC := clang
# rules # rules
$(NAME): $(LIBFT) $(OBJ) $(HEADERS) $(NAME): $(LIBFT) $(OBJ)
$(CC) $(OBJ) $(LINK_FLAGS) -o $(NAME) $(CC) $(OBJ) $(LINK_FLAGS) -o $(NAME)
$(TEST_BIN): $(LIBFT) $(TEST_OBJ) $(HEADERS) $(TEST_BIN): $(LIBFT) $(TEST_OBJ)
$(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)
@ -177,7 +181,7 @@ $(TEST_OBJ) $(OBJ): | $(OBJ_DIR)
$(OBJ_DIR): $(OBJ_DIR):
mkdir $(OBJ_DIR) mkdir $(OBJ_DIR)
$(OBJ_DIR)%.o: %.c $(OBJ_DIR)%.o: %.c $(HEADERS)
$(CC) -c $< -o $@ $(CC_FLAGS) $(HEADER_FLAGS) $(CC) -c $< -o $@ $(CC_FLAGS) $(HEADER_FLAGS)
$(LIBFT): $(LIBFT):

View file

@ -15,7 +15,8 @@
# include <stdint.h> # include <stdint.h>
# define FT_BASE64_GLUE_BLOCK_SIZE 3 # define FT_BASE64_READ_SIZE 3
# define FT_BASE64_TRANS_SIZE 3
# define FT_BASE64_CHARS_SIZE 4 # define FT_BASE64_CHARS_SIZE 4
# define FT_BASE64_ALPHABET_LENGTH 64 # define FT_BASE64_ALPHABET_LENGTH 64
@ -26,8 +27,6 @@ typedef struct s_base64_ctx
{ {
int input_fd; int input_fd;
int output_fd; int output_fd;
t_byte8 glue_block_bytes_count;
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;
@ -58,6 +57,21 @@ void ft_base64_encode
t_base64_ctx *ctx t_base64_ctx *ctx
); );
t_byte8 ft_base64_encode_step
(
t_base64_ctx *ctx,
t_byte8 readed,
t_byte1 *buff
);
void ft_base64_encode_finish
(
t_base64_ctx *ctx,
t_byte8 reminder,
t_byte1 *buff
);
void ft_base64_fill_buffer void ft_base64_fill_buffer
( (
t_base64_ctx *ctx, t_base64_ctx *ctx,
@ -71,4 +85,9 @@ void ft_base64_transform
t_byte1 *data t_byte1 *data
); );
void ft_base64_write
(
t_base64_ctx *ctx
);
#endif #endif

View file

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

View file

@ -1,16 +1,17 @@
#include <stdio.h>
#include "libft.h" #include "libft.h"
#include "ft_base64.h" #include "ft_base64.h"
#include "fcntl.h"
static void open_input_stream(t_base64_ctx *ctx, char *filename) static int open_stream(char *filename)
{ {
(void)ctx; int fd;
(void)filename; if ((fd = open((const char *)filename, O_RDONLY)) == -1)
} {
perror("base64");
static void open_output_stream(t_base64_ctx *ctx, char *filename) exit(1);
{ }
(void)ctx; return fd;
(void)filename;
} }
static void init_flags(t_base64_flags *flags) static void init_flags(t_base64_flags *flags)
@ -40,9 +41,9 @@ static void read_args
else if (ft_strcmp(current_arg, "-e") == 0 && i++) else if (ft_strcmp(current_arg, "-e") == 0 && i++)
continue; continue;
else if (ft_strcmp(current_arg, "-i") == 0) else if (ft_strcmp(current_arg, "-i") == 0)
open_input_stream(ctx, next_arg); ctx->input_fd = open_stream(next_arg);
else if (ft_strcmp(current_arg, "-o") == 0) else if (ft_strcmp(current_arg, "-o") == 0)
open_output_stream(ctx, next_arg); ctx->output_fd = open_stream(next_arg);
else if (i++) else if (i++)
continue; continue;
i++; i++;
@ -55,6 +56,7 @@ void ft_base64(int argc, char **argv)
t_base64_ctx ctx; t_base64_ctx ctx;
init_flags(&flags); init_flags(&flags);
ft_base64_init(&ctx);
read_args(argc, argv, &flags, &ctx); read_args(argc, argv, &flags, &ctx);
if (flags.decode) if (flags.decode)
ft_base64_decode(&ctx); ft_base64_decode(&ctx);

View file

@ -1,6 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_encode.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/10 20:52:07 by gtertysh #+# #+# */
/* Updated: 2019/02/10 20:52:59 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "ft_base64.h" #include "ft_base64.h"
#include "libft.h"
void ft_base64_encode(t_base64_ctx *ctx) void ft_base64_encode(t_base64_ctx *ctx)
{ {
(void)ctx; t_byte1 buff[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)
{
if (readed >= FT_BASE64_TRANS_SIZE)
{
reminder = ft_base64_encode_step(ctx, readed, buff);
ft_memmove(buff, buff + readed - reminder, reminder);
}
else
{
reminder += readed;
}
}
// ft_base64_encode_finish(ctx, reminder, buff);
} }

View file

@ -0,0 +1,16 @@
#include "ft_base64.h"
#include "libft.h"
void ft_base64_encode_finish
(
t_base64_ctx *ctx,
t_byte8 reminder,
t_byte1 *buffer
)
{
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_base64_write(ctx);
ft_putstr("\n");
}

View file

@ -0,0 +1,19 @@
#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;
}

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_fill_buffer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
ft_memcpy(ctx->glue_block, data, size);
ctx->glue_block_bytes_count = size;
}

View file

@ -16,10 +16,8 @@
void ft_base64_init(t_base64_ctx *ctx) void ft_base64_init(t_base64_ctx *ctx)
{ {
ctx->glue_block_bytes_count = 0;
ctx->input_fd = STDIN_FILENO; ctx->input_fd = STDIN_FILENO;
ctx->output_fd = STDOUT_FILENO; ctx->output_fd = STDOUT_FILENO;
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);
ft_memcpy(ctx->alphabet + 26, "abcdefghijklmnopqrstuvwxyz", 26); ft_memcpy(ctx->alphabet + 26, "abcdefghijklmnopqrstuvwxyz", 26);

View file

@ -0,0 +1,7 @@
# include "ft_base64.h"
# include <unistd.h>
void ft_base64_write(t_base64_ctx *ctx)
{
write(ctx->output_fd, ctx->chars, FT_BASE64_CHARS_SIZE);
}

View file

@ -16,37 +16,15 @@ abcdefghijklmnopqrstuvwxyz0123456789+/";
munit_assert_true(ctx.input_fd == STDIN_FILENO); munit_assert_true(ctx.input_fd == STDIN_FILENO);
munit_assert_true(ctx.output_fd == STDOUT_FILENO); munit_assert_true(ctx.output_fd == STDOUT_FILENO);
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++) for (int i = 0; i < FT_BASE64_CHARS_SIZE; i++)
munit_assert_uchar(ctx.chars[i], ==, 0); munit_assert_uchar(ctx.chars[i], ==, 0);
munit_assert_string_equal(alphabet, (char *)ctx.alphabet); munit_assert_string_equal(alphabet, (char *)ctx.alphabet);
munit_assert_true(ctx.glue_block_bytes_count == 0);
return MUNIT_OK; return MUNIT_OK;
} }
TEST_RESULT should_fill_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 *)"123", 3);
munit_assert_memory_equal(3, ctx.glue_block, "123");
munit_assert_true(ctx.glue_block_bytes_count == 3);
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_transform_base64_block(TEST_PARAMS, TEST_DATA) TEST_RESULT should_transform_base64_block(TEST_PARAMS, TEST_DATA)
{ {
UNUSED(test_params); UNUSED(test_params);

1
t/cases/Ma.txt Normal file
View file

@ -0,0 +1 @@
Ma

1
t/cases/Man.txt Normal file
View file

@ -0,0 +1 @@
Man

View file

@ -42,7 +42,6 @@ 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("/transform_block", should_transform_base64_block, NULL, NULL, 0, NULL), IT("/transform_block", should_transform_base64_block, NULL, NULL, 0, NULL),
END_IT END_IT
}; };