stable stdin encode

This commit is contained in:
Gregory 2019-02-20 22:11:29 +02:00
parent e34551286f
commit 48c1c4f34c
9 changed files with 168 additions and 129 deletions

120
Makefile
View file

@ -32,9 +32,9 @@ 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 B64_HEADER := $(INC_DIR)/ft_base64.h
HEADERS := $(MD5_HEADER) \ HEADERS := $(MD5_HEADER) \
$(SHA_HEADER) \ $(SHA_HEADER) \
$(SSL_HEADER) \ $(SSL_HEADER) \
$(B64_HEADER) $(B64_HEADER)
# libraries # libraries
@ -48,60 +48,60 @@ MUINUT_INC := -I $(MUINUT_DIR)
# project source files # project source files
MD5_SRC = ft_md5.c \ MD5_SRC = ft_md5.c \
ft_md5_stdin.c \ ft_md5_stdin.c \
ft_md5_string.c \ ft_md5_string.c \
ft_md5_file.c \ ft_md5_file.c \
ft_md5_print.c \ ft_md5_print.c \
ft_md5_init.c \ ft_md5_init.c \
ft_md5_update.c \ ft_md5_update.c \
ft_md5_transform.c \ ft_md5_transform.c \
ft_md5_decode.c \ ft_md5_decode.c \
ft_md5_encode_len.c \ ft_md5_encode_len.c \
ft_md5_encode_register.c \ ft_md5_encode_register.c \
ft_md5_final.c \ ft_md5_final.c \
ft_md5_padding.c \ ft_md5_padding.c \
ft_md5_digest_string.c ft_md5_digest_string.c
SHA_SRC = ft_sha256.c \ SHA_SRC = ft_sha256.c \
ft_sha256_string.c \ ft_sha256_string.c \
ft_sha256_file.c \ ft_sha256_file.c \
ft_sha256_stdin.c \ ft_sha256_stdin.c \
ft_sha256_init.c \ ft_sha256_init.c \
ft_sha256_update.c \ ft_sha256_update.c \
ft_sha256_decode.c \ ft_sha256_decode.c \
ft_sha256_encode_len.c \ ft_sha256_encode_len.c \
ft_sha256_encode_register.c \ ft_sha256_encode_register.c \
ft_sha256_transform.c \ ft_sha256_transform.c \
ft_sha256_padding.c \ ft_sha256_padding.c \
ft_sha256_final.c \ ft_sha256_final.c \
ft_sha256_digest_string.c \ ft_sha256_digest_string.c \
ft_sha256_constants.c \ ft_sha256_constants.c \
ft_sha224_string.c \ ft_sha224_string.c \
ft_sha224_file.c \ ft_sha224_file.c \
ft_sha224_stdin.c \ ft_sha224_stdin.c \
ft_sha224.c \ ft_sha224.c \
ft_sha224_init.c \ ft_sha224_init.c \
ft_sha224_update.c \ ft_sha224_update.c \
ft_sha224_final.c \ ft_sha224_final.c \
ft_sha224_digest_string.c \ ft_sha224_digest_string.c \
ft_sha_print.c \ ft_sha_print.c \
ft_sha_usage.c ft_sha_usage.c
BASE64_SRC = ft_base64.c \ BASE64_SRC = ft_base64.c \
ft_base64_init.c \ ft_base64_init.c \
ft_base64_transform.c \ ft_base64_decode.c \
ft_base64_decode.c \ ft_base64_encode.c \
ft_base64_encode.c \ ft_base64_init_encode_buffer.c \
ft_base64_chunk.c \ ft_base64_encode_chunk.c \
ft_base64_encode_finish.c \ ft_base64_encode_transform.c \
ft_base64_write.c ft_base64_encode_finish.c
SRC = main.c \ SRC = main.c \
ft_ssl_usage.c ft_ssl_usage.c
SRC += $(MD5_SRC) \ SRC += $(MD5_SRC) \
$(SHA_SRC) \ $(SHA_SRC) \
$(BASE64_SRC) $(BASE64_SRC)
# project object files # project object files
@ -121,11 +121,11 @@ SHA_TESTS += $(SHA_SRC)
BASE64_TESTS = base64_tests.c BASE64_TESTS = base64_tests.c
BASE64_TESTS += $(BASE64_SRC) BASE64_TESTS += $(BASE64_SRC)
TEST_SRC = tests.c \ TEST_SRC = tests.c \
munit.c munit.c
TEST_SRC += $(MD5_TESTS) \ TEST_SRC += $(MD5_TESTS) \
$(SHA_TESTS) \ $(SHA_TESTS) \
$(BASE64_TESTS) $(BASE64_TESTS)
TEST_OBJ = $(addprefix $(OBJ_DIR), $(TEST_SRC:.c=.o)) TEST_OBJ = $(addprefix $(OBJ_DIR), $(TEST_SRC:.c=.o))
@ -160,8 +160,8 @@ LINK_FLAGS = $(LIBFT_LIB)
# header flags # header flags
HEADER_FLAGS = -I $(INC_DIR) \ HEADER_FLAGS = -I $(INC_DIR) \
$(MUINUT_INC) \ $(MUINUT_INC) \
$(LIBFT_INC) $(LIBFT_INC)
# compiler # compiler
@ -212,11 +212,11 @@ multi:
# special stuff # special stuff
vpath %.c $(SRC_DIR) \ vpath %.c $(SRC_DIR) \
$(MD5_DIR) \ $(MD5_DIR) \
$(SHA_DIR) \ $(SHA_DIR) \
$(B64_DIR) \ $(B64_DIR) \
$(TST_DIR) \ $(TST_DIR) \
$(MUINUT_DIR) $(MUINUT_DIR)

View file

@ -15,7 +15,7 @@
# include <stdint.h> # include <stdint.h>
# define FT_BASE64_READ_SIZE 1 # define FT_BASE64_READ_SIZE 1024
# define FT_BASE64_TRANS_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
@ -23,14 +23,17 @@
typedef uint64_t t_byte8; typedef uint64_t t_byte8;
typedef unsigned char t_byte1; typedef unsigned char t_byte1;
typedef struct s_base64_encode_buffer
{
t_byte1 block[FT_BASE64_TRANS_SIZE];
t_byte8 readed;
} t_base64_encode_buffer;
typedef struct s_base64_ctx typedef struct s_base64_ctx
{ {
int input_fd; int input_fd;
int output_fd; int output_fd;
t_byte8 in_buffer;
t_byte1 buffer[FT_BASE64_TRANS_SIZE];
t_byte1 alphabet[FT_BASE64_ALPHABET_LENGTH]; t_byte1 alphabet[FT_BASE64_ALPHABET_LENGTH];
t_byte1 chars[FT_BASE64_CHARS_SIZE];
} t_base64_ctx; } t_base64_ctx;
typedef struct s_base64_flags typedef struct s_base64_flags
@ -69,8 +72,7 @@ void ft_base64_encode_finish
( (
t_base64_ctx *ctx, t_base64_ctx *ctx,
t_byte8 reminder, t_base64_encode_buffer *buff
t_byte1 *buff
); );
void ft_base64_fill_buffer void ft_base64_fill_buffer
@ -80,23 +82,24 @@ void ft_base64_fill_buffer
t_byte8 size t_byte8 size
); );
void ft_base64_transform void ft_base64_encode_transform
( (
t_base64_ctx *ctx, t_base64_ctx *ctx,
t_byte1 *data t_byte1 data[FT_BASE64_TRANS_SIZE],
t_byte1 chars[FT_BASE64_CHARS_SIZE]
); );
void ft_base64_write void ft_base64_encode_chunk
(
t_base64_ctx *ctx
);
t_byte8 ft_base64_chunk
( (
t_base64_ctx *ctx, t_base64_ctx *ctx,
t_byte8 len, t_byte8 len,
t_byte1 *message t_byte1 *message,
t_base64_encode_buffer *enc_buff
);
void ft_base64_init_encode_buffer
(
t_base64_encode_buffer *buff
); );
#endif #endif

View file

@ -1,29 +0,0 @@
#include "ft_base64.h"
#include "libft.h"
t_byte8 ft_base64_chunk
(
t_base64_ctx *ctx,
t_byte8 len,
t_byte1 *message
)
{
t_byte8 free_space_in_buffer;
t_byte8 i;
free_space_in_buffer = FT_BASE64_TRANS_SIZE - ctx->in_buffer;
if (len >= free_space_in_buffer)
{
ft_memcpy(&ctx->buffer[ctx->in_buffer], message, free_space_in_buffer);
ft_base64_transform(ctx, ctx->buffer);
ft_base64_write(ctx);
i = free_space_in_buffer;
while(i + FT_BASE64_TRANS_SIZE <= len)
{
ft_base64_transform(ctx, &message[i]);
ft_base64_write(ctx);
i += FT_BASE64_TRANS_SIZE;
}
}
}

View file

@ -16,14 +16,12 @@
void ft_base64_encode(t_base64_ctx *c) void ft_base64_encode(t_base64_ctx *c)
{ {
t_byte1 buffer[FT_BASE64_READ_SIZE]; t_byte1 read_buff[FT_BASE64_READ_SIZE];
int readed; t_base64_encode_buffer enc_buff;
int head; int readed;
int reminder;
reminder = 0; ft_base64_init_encode_buffer(&enc_buff);
head = 0; while ((readed = read(c->input_fd, read_buff, FT_BASE64_READ_SIZE)) > 0)
while ((readed = read(c->input_fd, buffer, FT_BASE64_READ_SIZE)) > 0) ft_base64_encode_chunk(c, readed, read_buff, &enc_buff);
ft_base64_chunk(c, readed, buffer); ft_base64_encode_finish(c, &enc_buff);
ft_base64_encode_finish(c, reminder, buffer);
} }

View file

@ -0,0 +1,46 @@
#include "ft_base64.h"
#include "libft.h"
static void ft_base64_encode_write
(
t_base64_ctx *ctx,
t_byte1 buff[FT_BASE64_TRANS_SIZE]
)
{
t_byte1 chars[FT_BASE64_CHARS_SIZE];
ft_base64_encode_transform(ctx, buff, chars);
write(ctx->output_fd, chars, FT_BASE64_CHARS_SIZE);
}
void ft_base64_encode_chunk
(
t_base64_ctx *ctx,
t_byte8 message_len,
t_byte1 *message,
t_base64_encode_buffer *buff
)
{
t_byte8 free_space_in_buffer;
t_byte8 buffer_index;
t_byte8 idx;
buffer_index = buff->readed % FT_BASE64_TRANS_SIZE;
buff->readed += message_len;
free_space_in_buffer = FT_BASE64_TRANS_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)
{
ft_base64_encode_write(ctx, &message[idx]);
idx += FT_BASE64_TRANS_SIZE;
}
buffer_index = 0;
}
else
idx = 0;
ft_memcpy(&buff->block[buffer_index], &message[idx], message_len - idx);
}

View file

@ -4,21 +4,25 @@
void ft_base64_encode_finish void ft_base64_encode_finish
( (
t_base64_ctx *ctx, t_base64_ctx *ctx,
t_byte8 reminder, t_base64_encode_buffer *buff
t_byte1 *buffer
) )
{ {
t_byte8 padding_size; t_byte8 buffer_index;
t_byte8 padding_size;
t_byte1 chars[FT_BASE64_CHARS_SIZE];
if (!reminder) buffer_index = buff->readed % FT_BASE64_TRANS_SIZE;
padding_size = FT_BASE64_TRANS_SIZE - buffer_index;
if (!buff->readed)
return ;
if (!buffer_index)
{ {
ft_putstr("\n"); ft_putstr("\n");
return ; return ;
} }
padding_size = reminder == 2 ? 1 : 2; ft_bzero(&buff->block[buffer_index], padding_size);
ft_bzero(buffer + reminder, FT_BASE64_TRANS_SIZE - reminder); ft_base64_encode_transform(ctx, buff->block, chars);
ft_base64_transform(ctx, buffer); ft_memset(chars + FT_BASE64_CHARS_SIZE - padding_size, '=', padding_size);
ft_memset(ctx->chars + FT_BASE64_CHARS_SIZE - padding_size, '=', FT_BASE64_CHARS_SIZE - padding_size); write(ctx->output_fd, chars, FT_BASE64_CHARS_SIZE);
ft_base64_write(ctx);
ft_putstr("\n"); ft_putstr("\n");
} }

View file

@ -13,7 +13,12 @@
#include "libft.h" #include "libft.h"
#include "ft_base64.h" #include "ft_base64.h"
void ft_base64_transform(t_base64_ctx *ctx, t_byte1 *data) 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 first_char; t_byte1 first_char;
t_byte1 second_char; t_byte1 second_char;
@ -24,8 +29,10 @@ void ft_base64_transform(t_base64_ctx *ctx, t_byte1 *data)
second_char = ((data[0] << 4) & 0x30) | ((data[1] >> 4) & 0xf); second_char = ((data[0] << 4) & 0x30) | ((data[1] >> 4) & 0xf);
third_char = ((data[1] << 2) & 0x3c) | ((data[2] >> 6) & 0x3); third_char = ((data[1] << 2) & 0x3c) | ((data[2] >> 6) & 0x3);
fourth_char = data[2] & 0x3F; fourth_char = data[2] & 0x3F;
ctx->chars[0] = ctx->alphabet[first_char];
ctx->chars[1] = ctx->alphabet[second_char]; chars[0] = ctx->alphabet[first_char];
ctx->chars[2] = ctx->alphabet[third_char]; chars[1] = ctx->alphabet[second_char];
ctx->chars[3] = ctx->alphabet[fourth_char]; chars[2] = ctx->alphabet[third_char];
chars[3] = ctx->alphabet[fourth_char];
} }

View file

@ -18,7 +18,6 @@ void ft_base64_init(t_base64_ctx *ctx)
{ {
ctx->input_fd = STDIN_FILENO; ctx->input_fd = STDIN_FILENO;
ctx->output_fd = STDOUT_FILENO; ctx->output_fd = STDOUT_FILENO;
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);
ft_memcpy(ctx->alphabet + 52, "0123456789+/", 12); ft_memcpy(ctx->alphabet + 52, "0123456789+/", 12);

View file

@ -0,0 +1,11 @@
#include "ft_base64.h"
#include "libft.h"
void ft_base64_init_encode_buffer
(
t_base64_encode_buffer *buff
)
{
ft_bzero(buff->block, FT_BASE64_TRANS_SIZE);
buff->readed = 0;
}