base64 work on small text

This commit is contained in:
Gregory 2019-02-16 22:10:21 +02:00
parent cebeb51867
commit f01037d6ff
6 changed files with 22 additions and 10 deletions

View file

@ -15,7 +15,7 @@
# include <stdint.h>
# define FT_BASE64_READ_SIZE 3
# define FT_BASE64_READ_SIZE 1048576
# define FT_BASE64_TRANS_SIZE 3
# define FT_BASE64_CHARS_SIZE 4
# define FT_BASE64_ALPHABET_LENGTH 64

View file

@ -14,26 +14,27 @@
#include "ft_base64.h"
#include "libft.h"
void ft_base64_encode(t_base64_ctx *ctx)
void ft_base64_encode(t_base64_ctx *c)
{
t_byte1 buff[FT_BASE64_READ_SIZE + FT_BASE64_TRANS_SIZE];
t_byte1 buffer[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)
fd = c->input_fd;
while ((readed = read(fd, buffer + reminder, FT_BASE64_READ_SIZE)) > 0)
{
if (readed >= FT_BASE64_TRANS_SIZE)
if (readed + reminder >= FT_BASE64_TRANS_SIZE)
{
reminder = ft_base64_encode_step(ctx, readed, buff);
ft_memmove(buff, buff + readed - reminder, reminder);
reminder = ft_base64_encode_step(c, readed + reminder, buffer);
ft_memmove(buffer, buffer + readed - reminder, reminder);
}
else
{
reminder += readed;
}
}
// ft_base64_encode_finish(ctx, reminder, buff);
ft_base64_encode_finish(c, reminder, buffer);
}

View file

@ -8,9 +8,17 @@ void ft_base64_encode_finish
t_byte1 *buffer
)
{
t_byte8 padding_size;
if (!reminder)
{
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 - reminder, '=', FT_BASE64_TRANS_SIZE - reminder);
ft_memset(ctx->chars + FT_BASE64_CHARS_SIZE - padding_size, '=', FT_BASE64_CHARS_SIZE - padding_size);
ft_base64_write(ctx);
ft_putstr("\n");
}

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

@ -0,0 +1 @@
M

View file

@ -0,0 +1 @@
Man is distinguished

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

@ -0,0 +1 @@
Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.