some shit

This commit is contained in:
Gregory 2019-02-17 21:07:19 +02:00
parent f01037d6ff
commit e34551286f
5 changed files with 48 additions and 40 deletions

View file

@ -93,7 +93,7 @@ BASE64_SRC = ft_base64.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_chunk.c \
ft_base64_encode_finish.c \ ft_base64_encode_finish.c \
ft_base64_write.c ft_base64_write.c

View file

@ -15,7 +15,7 @@
# include <stdint.h> # include <stdint.h>
# define FT_BASE64_READ_SIZE 1048576 # define FT_BASE64_READ_SIZE 1
# 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
@ -27,6 +27,8 @@ 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_byte1 chars[FT_BASE64_CHARS_SIZE];
} t_base64_ctx; } t_base64_ctx;
@ -60,7 +62,6 @@ void ft_base64_encode
t_byte8 ft_base64_encode_step t_byte8 ft_base64_encode_step
( (
t_base64_ctx *ctx, t_base64_ctx *ctx,
t_byte8 readed,
t_byte1 *buff t_byte1 *buff
); );
@ -90,4 +91,12 @@ void ft_base64_write
t_base64_ctx *ctx t_base64_ctx *ctx
); );
t_byte8 ft_base64_chunk
(
t_base64_ctx *ctx,
t_byte8 len,
t_byte1 *message
);
#endif #endif

View file

@ -0,0 +1,29 @@
#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,25 +16,14 @@
void ft_base64_encode(t_base64_ctx *c) void ft_base64_encode(t_base64_ctx *c)
{ {
t_byte1 buffer[FT_BASE64_READ_SIZE + FT_BASE64_TRANS_SIZE]; t_byte1 buffer[FT_BASE64_READ_SIZE];
t_byte8 readed; int readed;
t_byte8 reminder; int head;
int fd; int reminder;
reminder = 0; reminder = 0;
fd = c->input_fd; head = 0;
while ((readed = read(fd, buffer + reminder, FT_BASE64_READ_SIZE)) > 0) while ((readed = read(c->input_fd, buffer, FT_BASE64_READ_SIZE)) > 0)
{ ft_base64_chunk(c, readed, buffer);
if (readed + reminder >= FT_BASE64_TRANS_SIZE)
{
reminder = ft_base64_encode_step(c, readed + reminder, buffer);
ft_memmove(buffer, buffer + readed - reminder, reminder);
}
else
{
reminder += readed;
}
}
ft_base64_encode_finish(c, reminder, buffer); ft_base64_encode_finish(c, reminder, buffer);
} }

View file

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