base64 without error management done
This commit is contained in:
parent
9f93b55230
commit
413e7fabd3
5 changed files with 29 additions and 8 deletions
|
@ -1,9 +1,26 @@
|
||||||
#include "ft_base64.h"
|
#include "ft_base64.h"
|
||||||
#include "libft.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
|
void ft_base64_decode_transform
|
||||||
(
|
(
|
||||||
t_base64_ctx *ctx,
|
t_base64_ctx *c,
|
||||||
t_byte1 blk[FT_BASE64_DECODE_BLOCK_SIZE],
|
t_byte1 blk[FT_BASE64_DECODE_BLOCK_SIZE],
|
||||||
t_byte1 decoded_block[FT_BASE64_ENCODE_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 second_index;
|
||||||
t_byte1 third_index;
|
t_byte1 third_index;
|
||||||
t_byte1 fourth_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[0] = first_index << 2 | second_index >> 4;
|
||||||
decoded_block[1] = second_index << 4 | third_index >> 2;
|
decoded_block[1] = second_index << 4 | third_index >> 2;
|
||||||
decoded_block[2] = third_index << 6 | fourth_index;
|
decoded_block[2] = third_index << 6 | fourth_index;
|
||||||
|
|
1
t/cases/TWE=.decode.txt
Normal file
1
t/cases/TWE=.decode.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
TWE=
|
1
t/cases/TWFu.decode.txt
Normal file
1
t/cases/TWFu.decode.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
TWFu
|
4
t/cases/TWFu_whitespaces.decode.txt
Normal file
4
t/cases/TWFu_whitespaces.decode.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
T
|
||||||
|
W F
|
||||||
|
u
|
||||||
|
|
1
t/cases/invalid.decode.txt
Normal file
1
t/cases/invalid.decode.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
$TWFu
|
Loading…
Reference in a new issue