-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64.c
executable file
·72 lines (59 loc) · 2.54 KB
/
base64.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
->====================================<-
->= SvTX - © Copyright 2022 OnyxSoft =<-
->====================================<-
->= Version : 1.0 =<-
->= File : base64.c =<-
->= Author : Stefan Blixth =<-
->= Compiled : 2022-06-16 =<-
->====================================<-
*/
#include "main.h"
/*=----------------------------- build_decoding_table() ----------------------*
* *
*----------------------------------------------------------------------------*/
void build_decoding_table(void)
{
decoding_table = AllocVec(256, MEMF_ANY);
for (int i = 0; i < 64; i++)
decoding_table[(unsigned char) encoding_table[i]] = i;
}
/*=*/
/*=----------------------------- base64_cleanup() ----------------------------*
* *
*----------------------------------------------------------------------------*/
void base64_cleanup(void)
{
FreeVec(decoding_table);
decoding_table = NULL;
}
/*=*/
/*=----------------------------- base64_decode() -----------------------------*
* *
*----------------------------------------------------------------------------*/
unsigned char *base64_decode(const char *data, unsigned long input_length, unsigned long *output_length)
{
if (decoding_table == NULL) build_decoding_table();
if (input_length % 4 != 0) return NULL;
*output_length = input_length / 4 * 3;
if (data[input_length - 1] == '=') (*output_length)--;
if (data[input_length - 2] == '=') (*output_length)--;
unsigned char *decoded_data = AllocVec(*output_length, MEMF_ANY);
if (decoded_data == NULL) return NULL;
for (int i = 0, j = 0; i < input_length;)
{
uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t triple = (sextet_a << 3 * 6)
+ (sextet_b << 2 * 6)
+ (sextet_c << 1 * 6)
+ (sextet_d << 0 * 6);
if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
}
return decoded_data;
}
/*=*/