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

View file

@ -90,12 +90,12 @@ SHA_SRC = ft_sha256.c \
BASE64_SRC = ft_base64.c \
ft_base64_init.c \
ft_base64_transform.c \
ft_base64_decode.c \
ft_base64_encode.c \
ft_base64_chunk.c \
ft_base64_encode_finish.c \
ft_base64_write.c
ft_base64_init_encode_buffer.c \
ft_base64_encode_chunk.c \
ft_base64_encode_transform.c \
ft_base64_encode_finish.c
SRC = main.c \
ft_ssl_usage.c

View file

@ -15,7 +15,7 @@
# include <stdint.h>
# define FT_BASE64_READ_SIZE 1
# define FT_BASE64_READ_SIZE 1024
# define FT_BASE64_TRANS_SIZE 3
# define FT_BASE64_CHARS_SIZE 4
# define FT_BASE64_ALPHABET_LENGTH 64
@ -23,14 +23,17 @@
typedef uint64_t t_byte8;
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
{
int input_fd;
int output_fd;
t_byte8 in_buffer;
t_byte1 buffer[FT_BASE64_TRANS_SIZE];
t_byte1 alphabet[FT_BASE64_ALPHABET_LENGTH];
t_byte1 chars[FT_BASE64_CHARS_SIZE];
} t_base64_ctx;
typedef struct s_base64_flags
@ -69,8 +72,7 @@ void ft_base64_encode_finish
(
t_base64_ctx *ctx,
t_byte8 reminder,
t_byte1 *buff
t_base64_encode_buffer *buff
);
void ft_base64_fill_buffer
@ -80,23 +82,24 @@ void ft_base64_fill_buffer
t_byte8 size
);
void ft_base64_transform
void ft_base64_encode_transform
(
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
(
t_base64_ctx *ctx
);
t_byte8 ft_base64_chunk
void ft_base64_encode_chunk
(
t_base64_ctx *ctx,
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

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)
{
t_byte1 buffer[FT_BASE64_READ_SIZE];
t_byte1 read_buff[FT_BASE64_READ_SIZE];
t_base64_encode_buffer enc_buff;
int readed;
int head;
int reminder;
reminder = 0;
head = 0;
while ((readed = read(c->input_fd, buffer, FT_BASE64_READ_SIZE)) > 0)
ft_base64_chunk(c, readed, buffer);
ft_base64_encode_finish(c, reminder, buffer);
ft_base64_init_encode_buffer(&enc_buff);
while ((readed = read(c->input_fd, read_buff, FT_BASE64_READ_SIZE)) > 0)
ft_base64_encode_chunk(c, readed, read_buff, &enc_buff);
ft_base64_encode_finish(c, &enc_buff);
}

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
(
t_base64_ctx *ctx,
t_byte8 reminder,
t_byte1 *buffer
t_base64_encode_buffer *buff
)
{
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");
return ;
}
padding_size = reminder == 2 ? 1 : 2;
ft_bzero(buffer + reminder, FT_BASE64_TRANS_SIZE - reminder);
ft_base64_transform(ctx, buffer);
ft_memset(ctx->chars + FT_BASE64_CHARS_SIZE - padding_size, '=', FT_BASE64_CHARS_SIZE - padding_size);
ft_base64_write(ctx);
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_putstr("\n");
}

View file

@ -13,7 +13,12 @@
#include "libft.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 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);
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];
chars[0] = ctx->alphabet[first_char];
chars[1] = ctx->alphabet[second_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->output_fd = STDOUT_FILENO;
ft_bzero(ctx->chars, FT_BASE64_CHARS_SIZE);
ft_memcpy(ctx->alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26);
ft_memcpy(ctx->alphabet + 26, "abcdefghijklmnopqrstuvwxyz", 26);
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;
}