From 413e7fabd3b9678136e4797082aaad78289c59b6 Mon Sep 17 00:00:00 2001 From: Gregory Date: Sun, 24 Feb 2019 23:44:43 +0200 Subject: [PATCH] base64 without error management done --- src/base64/ft_base64_decode_transform.c | 30 ++++++++++++++++++------- t/cases/TWE=.decode.txt | 1 + t/cases/TWFu.decode.txt | 1 + t/cases/TWFu_whitespaces.decode.txt | 4 ++++ t/cases/invalid.decode.txt | 1 + 5 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 t/cases/TWE=.decode.txt create mode 100644 t/cases/TWFu.decode.txt create mode 100644 t/cases/TWFu_whitespaces.decode.txt create mode 100644 t/cases/invalid.decode.txt diff --git a/src/base64/ft_base64_decode_transform.c b/src/base64/ft_base64_decode_transform.c index c6a26396..53f1c118 100644 --- a/src/base64/ft_base64_decode_transform.c +++ b/src/base64/ft_base64_decode_transform.c @@ -1,9 +1,26 @@ #include "ft_base64.h" #include "libft.h" +static t_byte1 get_alphabet_index +( + t_byte1 *alphabet, + t_byte1 ch +) +{ + char *alphabet_addr; + + alphabet_addr = ft_strchr((const char*)alphabet, ch); + if (!alphabet_addr) + { + ft_putstr("base64: invalid input\n"); + exit(1); + } + return (alphabet_addr - (char *)alphabet); +} + void ft_base64_decode_transform ( - t_base64_ctx *ctx, + t_base64_ctx *c, t_byte1 blk[FT_BASE64_DECODE_BLOCK_SIZE], t_byte1 decoded_block[FT_BASE64_ENCODE_BLOCK_SIZE] ) @@ -12,14 +29,11 @@ void ft_base64_decode_transform 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; + first_index = get_alphabet_index(c->alphabet, blk[0]); + second_index = get_alphabet_index(c->alphabet, blk[1]); + third_index = blk[2] == '=' ? 0 : get_alphabet_index(c->alphabet, blk[2]); + fourth_index = blk[3] == '=' ? 0 : get_alphabet_index(c->alphabet, blk[3]); 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; diff --git a/t/cases/TWE=.decode.txt b/t/cases/TWE=.decode.txt new file mode 100644 index 00000000..9774b356 --- /dev/null +++ b/t/cases/TWE=.decode.txt @@ -0,0 +1 @@ +TWE= diff --git a/t/cases/TWFu.decode.txt b/t/cases/TWFu.decode.txt new file mode 100644 index 00000000..3ee09ff7 --- /dev/null +++ b/t/cases/TWFu.decode.txt @@ -0,0 +1 @@ +TWFu diff --git a/t/cases/TWFu_whitespaces.decode.txt b/t/cases/TWFu_whitespaces.decode.txt new file mode 100644 index 00000000..38cff4e8 --- /dev/null +++ b/t/cases/TWFu_whitespaces.decode.txt @@ -0,0 +1,4 @@ +T +W F +u + diff --git a/t/cases/invalid.decode.txt b/t/cases/invalid.decode.txt new file mode 100644 index 00000000..3bc83fb6 --- /dev/null +++ b/t/cases/invalid.decode.txt @@ -0,0 +1 @@ +$TWFu