add decode. base64 without error management done

This commit is contained in:
Gregory 2019-02-23 23:56:27 +02:00
parent a6a88824df
commit 9f93b55230
14 changed files with 222 additions and 32 deletions

View file

@ -95,7 +95,12 @@ BASE64_SRC = ft_base64.c \
ft_base64_init_encode_buffer.c \
ft_base64_encode_chunk.c \
ft_base64_encode_transform.c \
ft_base64_encode_finish.c
ft_base64_encode_finish.c \
ft_base64_init_decode_buffer.c \
ft_base64_decode_chunk.c \
ft_base64_decode_filter.c \
ft_base64_decode_transform.c \
ft_base64_decode_finish.c
SRC = main.c \
ft_ssl_usage.c

View file

@ -16,8 +16,8 @@
# include <stdint.h>
# define FT_BASE64_READ_SIZE 1024
# define FT_BASE64_TRANS_SIZE 3
# define FT_BASE64_CHARS_SIZE 4
# define FT_BASE64_ENCODE_BLOCK_SIZE 3
# define FT_BASE64_DECODE_BLOCK_SIZE 4
# define FT_BASE64_ALPHABET_LENGTH 64
typedef uint64_t t_byte8;
@ -25,10 +25,16 @@ typedef unsigned char t_byte1;
typedef struct s_base64_encode_buffer
{
t_byte1 block[FT_BASE64_TRANS_SIZE];
t_byte1 block[FT_BASE64_ENCODE_BLOCK_SIZE];
t_byte8 readed;
} t_base64_encode_buffer;
typedef struct s_base64_decode_buffer
{
t_byte1 block[FT_BASE64_DECODE_BLOCK_SIZE];
t_byte8 readed;
} t_base64_decode_buffer;
typedef struct s_base64_ctx
{
int input_fd;
@ -85,8 +91,8 @@ void ft_base64_fill_buffer
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 data[FT_BASE64_ENCODE_BLOCK_SIZE],
t_byte1 chars[FT_BASE64_DECODE_BLOCK_SIZE]
);
void ft_base64_encode_chunk
@ -102,4 +108,36 @@ void ft_base64_init_encode_buffer
t_base64_encode_buffer *buff
);
void ft_base64_init_decode_buffer
(
t_base64_decode_buffer *buff
);
void ft_base64_decode_chunk
(
t_base64_ctx *ctx,
t_byte8 len,
t_byte1 *message,
t_base64_decode_buffer *dec_buff
);
void ft_base64_decode_transform
(
t_base64_ctx *ctx,
t_byte1 *message,
t_byte1 decoded_block[FT_BASE64_ENCODE_BLOCK_SIZE]
);
void ft_base64_decode_finish
(
t_base64_ctx *ctx,
t_base64_decode_buffer *buff
);
t_byte8 ft_base64_decode_filter
(
t_byte8 readed,
t_byte1 *buff
);
#endif

View file

@ -1,8 +1,8 @@
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "libft.h"
#include "ft_base64.h"
#include "fcntl.h"
#include "sys/stat.h"
static int open_stream(char *filename, int flags, int mode)
{

View file

@ -1,6 +1,17 @@
#include <unistd.h>
#include "ft_base64.h"
void ft_base64_decode(t_base64_ctx *ctx)
{
(void)ctx;
t_byte1 read_buff[FT_BASE64_READ_SIZE];
t_base64_decode_buffer decode_buff;
t_byte8 readed;
ft_base64_init_decode_buffer(&decode_buff);
while((readed = read(ctx->input_fd, read_buff, FT_BASE64_READ_SIZE)) > 0)
{
readed = ft_base64_decode_filter(readed, read_buff);
ft_base64_decode_chunk(ctx, readed, read_buff, &decode_buff);
}
ft_base64_decode_finish(ctx, &decode_buff);
}

View file

@ -0,0 +1,47 @@
#include <unistd.h>
#include "libft.h"
#include "ft_base64.h"
static void ft_base64_decode_write
(
t_base64_ctx *ctx,
t_byte1 *block
)
{
t_byte1 decoded_block[FT_BASE64_ENCODE_BLOCK_SIZE];
ft_base64_decode_transform(ctx, block, decoded_block);
write(ctx->output_fd, decoded_block, FT_BASE64_ENCODE_BLOCK_SIZE);
}
void ft_base64_decode_chunk
(
t_base64_ctx *ctx,
t_byte8 message_len,
t_byte1 *message,
t_base64_decode_buffer *buff
)
{
t_byte8 free_space_in_buffer;
t_byte8 buffer_index;
t_byte8 idx;
buffer_index = buff->readed % FT_BASE64_DECODE_BLOCK_SIZE;
buff->readed += message_len;
free_space_in_buffer = FT_BASE64_DECODE_BLOCK_SIZE - buffer_index;
if (message_len >= free_space_in_buffer)
{
ft_memcpy(&buff->block[buffer_index], message, free_space_in_buffer);
ft_base64_decode_write(ctx, buff->block);
idx = free_space_in_buffer;
while(idx + FT_BASE64_DECODE_BLOCK_SIZE <= message_len)
{
ft_base64_decode_write(ctx, &message[idx]);
idx += FT_BASE64_DECODE_BLOCK_SIZE;
}
buffer_index = 0;
}
else
idx = 0;
ft_memcpy(&buff->block[buffer_index], &message[idx], message_len - idx);
}

View file

@ -0,0 +1,25 @@
#include "ft_base64.h"
#include "libft.h"
t_byte8 ft_base64_decode_filter
(
t_byte8 readed,
t_byte1 *buffer
)
{
t_byte8 i;
t_byte8 last_free_index;
i = 0;
last_free_index = 0;
while(i < readed)
{
if (!ft_isspace(buffer[i]))
{
buffer[last_free_index] = buffer[i];
last_free_index++;
}
i++;
}
return (last_free_index);
}

View file

@ -0,0 +1,29 @@
#include "ft_base64.h"
#include "libft.h"
void ft_base64_decode_finish
(
t_base64_ctx *ctx,
t_base64_decode_buffer *buff
)
{
t_byte8 buffer_index;
t_byte8 padding_size;
// t_byte1 chars[FT_BASE64_ENCODE_BLOCK_SIZE];
buffer_index = buff->readed % FT_BASE64_DECODE_BLOCK_SIZE;
padding_size = FT_BASE64_DECODE_BLOCK_SIZE - buffer_index;
if (!buff->readed)
return ;
if (!buffer_index)
{
write(ctx->output_fd, "\n", 1);
return ;
}
// ft_bzero(&buff->block[buffer_index], padding_size);
// ft_base64_encode_transform(ctx, buff->block, chars);
// ft_memset(chars + FT_BASE64_DECODE_BLOCK_SIZE - padding_size, '=',
// padding_size);
// write(ctx->output_fd, chars, FT_BASE64_DECODE_BLOCK_SIZE);
// write(ctx->output_fd, "\n", 1);
}

View file

@ -0,0 +1,26 @@
#include "ft_base64.h"
#include "libft.h"
void ft_base64_decode_transform
(
t_base64_ctx *ctx,
t_byte1 blk[FT_BASE64_DECODE_BLOCK_SIZE],
t_byte1 decoded_block[FT_BASE64_ENCODE_BLOCK_SIZE]
)
{
t_byte1 first_index;
t_byte1 second_index;
t_byte1 third_index;
t_byte1 fourth_index;
const char *alphabet;
alphabet = (const char *)ctx->alphabet;
first_index = ft_strchr(alphabet, blk[0]) - alphabet;
second_index = ft_strchr(alphabet, blk[1]) - alphabet;
third_index = blk[2] == '=' ? 0 : ft_strchr(alphabet, blk[2]) - alphabet;
fourth_index = blk[3] == '=' ? 0 : ft_strchr(alphabet, blk[3]) - alphabet;
decoded_block[0] = first_index << 2 | second_index >> 4;
decoded_block[1] = second_index << 4 | third_index >> 2;
decoded_block[2] = third_index << 6 | fourth_index;
}

View file

@ -4,13 +4,13 @@
static void ft_base64_encode_write
(
t_base64_ctx *ctx,
t_byte1 buff[FT_BASE64_TRANS_SIZE]
t_byte1 buff[FT_BASE64_ENCODE_BLOCK_SIZE]
)
{
t_byte1 chars[FT_BASE64_CHARS_SIZE];
t_byte1 chars[FT_BASE64_DECODE_BLOCK_SIZE];
ft_base64_encode_transform(ctx, buff, chars);
write(ctx->output_fd, chars, FT_BASE64_CHARS_SIZE);
write(ctx->output_fd, chars, FT_BASE64_DECODE_BLOCK_SIZE);
}
void ft_base64_encode_chunk
@ -25,18 +25,18 @@ void ft_base64_encode_chunk
t_byte8 buffer_index;
t_byte8 idx;
buffer_index = buff->readed % FT_BASE64_TRANS_SIZE;
buffer_index = buff->readed % FT_BASE64_ENCODE_BLOCK_SIZE;
buff->readed += message_len;
free_space_in_buffer = FT_BASE64_TRANS_SIZE - buffer_index;
free_space_in_buffer = FT_BASE64_ENCODE_BLOCK_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)
while(idx + FT_BASE64_ENCODE_BLOCK_SIZE <= message_len)
{
ft_base64_encode_write(ctx, &message[idx]);
idx += FT_BASE64_TRANS_SIZE;
idx += FT_BASE64_ENCODE_BLOCK_SIZE;
}
buffer_index = 0;
}

View file

@ -9,10 +9,10 @@ void ft_base64_encode_finish
{
t_byte8 buffer_index;
t_byte8 padding_size;
t_byte1 chars[FT_BASE64_CHARS_SIZE];
t_byte1 chars[FT_BASE64_DECODE_BLOCK_SIZE];
buffer_index = buff->readed % FT_BASE64_TRANS_SIZE;
padding_size = FT_BASE64_TRANS_SIZE - buffer_index;
buffer_index = buff->readed % FT_BASE64_ENCODE_BLOCK_SIZE;
padding_size = FT_BASE64_ENCODE_BLOCK_SIZE - buffer_index;
if (!buff->readed)
return ;
if (!buffer_index)
@ -22,7 +22,8 @@ void ft_base64_encode_finish
}
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_memset(chars + FT_BASE64_DECODE_BLOCK_SIZE - padding_size, '=',
padding_size);
write(ctx->output_fd, chars, FT_BASE64_DECODE_BLOCK_SIZE);
write(ctx->output_fd, "\n", 1);
}

View file

@ -16,8 +16,8 @@
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 data[FT_BASE64_ENCODE_BLOCK_SIZE],
t_byte1 chars[FT_BASE64_DECODE_BLOCK_SIZE]
)
{
t_byte1 first_char;

View file

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

View file

@ -6,6 +6,6 @@ void ft_base64_init_encode_buffer
t_base64_encode_buffer *buff
)
{
ft_bzero(buff->block, FT_BASE64_TRANS_SIZE);
ft_bzero(buff->block, FT_BASE64_ENCODE_BLOCK_SIZE);
buff->readed = 0;
}

View file

@ -16,10 +16,6 @@ abcdefghijklmnopqrstuvwxyz0123456789+/";
munit_assert_true(ctx.input_fd == STDIN_FILENO);
munit_assert_true(ctx.output_fd == STDOUT_FILENO);
for (int i = 0; i < FT_BASE64_CHARS_SIZE; i++)
munit_assert_uchar(ctx.chars[i], ==, 0);
munit_assert_string_equal(alphabet, (char *)ctx.alphabet);
return MUNIT_OK;
@ -31,13 +27,14 @@ TEST_RESULT should_transform_base64_block(TEST_PARAMS, TEST_DATA)
UNUSED(test_data);
t_base64_ctx ctx;
t_byte1 buff[FT_BASE64_DECODE_BLOCK_SIZE];
ft_base64_init(&ctx);
ft_base64_transform(&ctx, (t_byte1 *)"Man");
munit_assert_string_equal((char *)ctx.chars, "TWFu");
ft_base64_encode_transform(&ctx, (t_byte1 *)"Man", buff);
munit_assert_string_equal((char *)buff, "TWFu");
ft_base64_transform(&ctx, (t_byte1 *)"LOL");
munit_assert_string_equal((char *)ctx.chars, "TE9M");
ft_base64_encode_transform(&ctx, (t_byte1 *)"LOL", buff);
munit_assert_string_equal((char *)buff, "TE9M");
return MUNIT_OK;
}