1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <stdint.h>
7
8 static const unsigned char base64_encode_map[] = {
9 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
10 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
11 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
12 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
13 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
14 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
15 'w', 'x', 'y', 'z', '0', '1', '2', '3',
16 '4', '5', '6', '7', '8', '9', '+', '/'
17 };
18
19 static const unsigned char base64_decode_map[] = {
20 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
23 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
24 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
25 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
26 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
27 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0,
28 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
36 };
37
38 static const unsigned char mod_table[] = { 0, 2, 1 };
39
uvoice_base64_encode(const uint8_t * input,uint32_t input_len,uint8_t * output,uint32_t * output_len)40 int uvoice_base64_encode(const uint8_t *input, uint32_t input_len, uint8_t *output, uint32_t *output_len)
41 {
42 int i, j;
43 uint32_t o_len = 4 * ((input_len + 2) / 3);
44
45 if (!input || !output || !output_len) {
46 return -1;
47 }
48
49 if (output_len)
50 {
51 *output_len = o_len;
52 }
53
54 for (i = 0, j = 0; i < input_len;)
55 {
56 unsigned int octet_a = i < input_len ? input[i++] : 0;
57 unsigned int octet_b = i < input_len ? input[i++] : 0;
58 unsigned int octet_c = i < input_len ? input[i++] : 0;
59
60 unsigned int triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
61
62 output[j++] = base64_encode_map[(triple >> 18) & 0x3F];
63 output[j++] = base64_encode_map[(triple >> 12) & 0x3F];
64 output[j++] = base64_encode_map[(triple >> 6) & 0x3F];
65 output[j++] = base64_encode_map[(triple >> 0) & 0x3F];
66 }
67
68 for (i = 0; i < mod_table[input_len % 3]; i++)
69 {
70 output[o_len - 1 - i] = '=';
71 }
72
73 return 0;
74 }
75
uvoice_basex64_decode(const uint8_t * input,uint32_t input_len,uint8_t * output,uint32_t * output_len)76 int uvoice_basex64_decode(const uint8_t* input, uint32_t input_len, uint8_t* output, uint32_t* output_len)
77 {
78 int i, j;
79 uint32_t o_len = input_len / 4 * 3;
80
81 if (!input || !output || !output_len)
82 {
83 return -1;
84 }
85
86 if (input[input_len - 1] == '=')
87 {
88 o_len--;
89 }
90 if (input[input_len - 2] == '=')
91 {
92 o_len--;
93 }
94
95 if (output_len)
96 {
97 *output_len = o_len;
98 }
99
100 for (i = 0, j = 0; i < input_len;)
101 {
102 unsigned int sextet_a = base64_decode_map[input[i++]];
103 unsigned int sextet_b = base64_decode_map[input[i++]];
104 unsigned int sextet_c = base64_decode_map[input[i++]];
105 unsigned int sextet_d = base64_decode_map[input[i++]];
106
107 unsigned int triple = (sextet_a << 18)
108 + (sextet_b << 12)
109 + (sextet_c << 6)
110 + (sextet_d << 0);
111
112 if (j < o_len)
113 {
114 output[j++] = (triple >> 16) & 0xFF;
115 }
116 if (j < o_len)
117 {
118 output[j++] = (triple >> 8) & 0xFF;
119 }
120 if (j < o_len)
121 {
122 output[j++] = (triple >> 0) & 0xFF;
123 }
124 }
125
126 return 0;
127 }
128