Debian Codegraph @

src/base64.c

raw symbols SCIP
1 #include <stdlib.h>
2 #include <R.h>
3 #include <Rdefines.h>
4 #include <Rversion.h>
5
6 const char base64_table[65] = {
7 'A','B','C','D','E','F','G','H',
8 'I','J','K','L','M','N','O','P',
9 'Q','R','S','T','U','V','W','X',
10 'Y','Z','a','b','c','d','e','f',
11 'g','h','i','j','k','l','m','n',
12 'o','p','q','r','s','t','u','v',
13 'w','x','y','z','0','1','2','3',
14 '4','5','6','7','8','9','+','/',
15 '%'
16 };
17
18 const char padding = '=';
19 const unsigned char upadding = '=';
20
21 void base64_encode_impl(
22 const unsigned char* input,
23 const R_xlen_t input_len,
24 char* output,
25 const R_xlen_t output_len
26 ) {
27 R_xlen_t input_len_left = input_len;
28 R_xlen_t i1 = 0;
29 R_xlen_t i2 = 0;
30 while(input_len_left > 2) {
31 output[i2++] = base64_table[input[i1] / 4];
32 output[i2++] = base64_table[16 * (input[i1] % 4) + input[ i1 + 1 ] / 16];
33 output[i2++] = base64_table[4 * (input [ i1 + 1] % 16) + input[i1 + 2] / 64];
34 output[i2++] = base64_table[input[i1 + 2] % 64];
35 i1 += 3;
36 input_len_left -= 3;
37 }
38 if (input_len_left) {
39 output[i2++] = base64_table[input[i1] / 4];
40 if (input_len_left > 1) {
41 output[i2++] = base64_table[16 * (input[i1] % 4) + input[ i1 + 1 ] / 16];
42 output[i2++] = base64_table[4 * (input [i1 + 1] % 16)];
43 output[i2++] = padding;
44 } else {
45 output[i2++] = base64_table[16 * (input[i1] % 4)];
46 output[i2++] = padding;
47 output[i2++] = padding;
48
49 }
50 }
51 }
52
53 static const short base64_reverse_table[256] = {
54 -2, -2, -2, -2, -2, -2, -2, -2,
55 -2, -1, -1, -2, -2, -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
56 -2, -2, -2, -2, -2, -2, -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62,
57 -2, -2, -2, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2,
58 -2, -2, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
59 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2, -2, 26, 27, 28,
60 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
61 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
62 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
63 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
64 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
65 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
66 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
67 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
68 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
69 };
70
71 int base64_decode_impl(
72 const unsigned char* input,
73 R_xlen_t input_len,
74 unsigned char* output,
75 R_xlen_t* poutput_len
76 ) {
77 R_xlen_t output_len = *poutput_len;
78 for (R_xlen_t i = 0;i < output_len;i++) {
79 output[i] = 0;
80 }
81 int ch = 0;
82 R_xlen_t i = 0, j = 0, k = 0;
83 while(input_len-- > 0) {
84 ch = *input++;
85 if (ch == padding) {
86 if (*input != upadding && (i % 4) == 1) {
87 return 1;
88 }
89 continue;
90 }
91 ch = base64_reverse_table[ch];
92 if (ch == -1) {
93 continue;
94 }
95 else if (ch == -2) {
96 return 1;
97 }
98 switch (i % 4) {
99 case 0 : {
100 output[j] = ch << 2;
101 break;
102 }
103 case 1 : {
104 output[j++] |= ch >> 4;
105 if (j < output_len) output[j] = (ch & 0x0f) << 4;
106 break;
107 }
108 case 2 : {
109 output[j++] |= ch >> 2;
110 if (j < output_len) output[j] = (ch & 0x03) << 6;
111 break;
112 }
113 case 3 : {
114 output[j++] |= ch;
115 break;
116 }
117 }
118 i++;
119 }
120 k = j;
121 if (ch == padding ) {
122 switch (i % 4) {
123 case 1 : {
124 return 1;
125 }
126 case 2 : k++;
127 case 3 : if (k < output_len) output[k] = 0;
128 }
129 }
130 *poutput_len = j;
131 return 0;
132 }
133
134 SEXP base64_enc(SEXP input) {
135 int rv = 0;
136 // get input
137 R_xlen_t input_len;
138 #if defined(R_VERSION) && R_VERSION >= R_Version(3,0,0)
139 input_len = XLENGTH(input);
140 #else
141 input_len = LENGTH(input);
142 #endif
143 R_xlen_t output_len = input_len / 3 * 4;
144 if (input_len % 3) {
145 output_len += 4;
146 }
147 unsigned char *input_content = (unsigned char*) RAW(input);
148 // declare output
149 // allocate memory
150 SEXP result = PROTECT(NEW_CHARACTER(1));
151 if (result) { // check if memory is allocated successfully
152 char* result_content = (char*) malloc(output_len + 1);
153 if (result_content) { // check if memory is allocated successfully
154 base64_encode_impl(input_content, input_len, result_content, output_len);
155 result_content[output_len] = 0;
156 SET_STRING_ELT(result, 0, mkChar(result_content));
157 free(result_content);
158 } else {
159 rv = 2;
160 }
161 } else {
162 rv = 1;
163 }
164 UNPROTECT(1);
165 switch (rv) {
166 case 1 : {
167 error("Failed to allocate memory for result");
168 break;
169 }
170 case 2 : {
171 error("Failed to allocate memory for result_content");
172 break;
173 }
174 default : { }
175 }
176 return result;
177 }
178
179 SEXP base64_dec(SEXP input) {
180 int rv = 0;
181 R_xlen_t input_len;
182 SEXP result = R_NilValue;
183 #if defined(R_VERSION) && R_VERSION >= R_Version(3,0,0)
184 input_len = XLENGTH(input);
185 #else
186 input_len = LENGTH(input);
187 #endif
188 if (input_len != 1 || TYPEOF(input) != STRSXP) {
189 rv = 1;
190 } else {
191 SEXP input_char = STRING_ELT(input, 0);
192 const unsigned char* input_p = (unsigned char*) CHAR(input_char);
193 const R_xlen_t input_str_size = strlen((const char*) input_p);
194 if (input_str_size % 4 != 0) {
195 rv = 2;
196 } else {
197 R_xlen_t output_len = input_str_size;
198 unsigned char* result_content = malloc(output_len);
199 if (result_content) {
200 if (base64_decode_impl(
201 input_p,
202 input_str_size,
203 result_content,
204 &output_len
205 ) != 0) {
206 rv = 2;
207 } else {
208 result = PROTECT(NEW_RAW(output_len));
209 unsigned char* presult = RAW_POINTER(result);
210 for (R_xlen_t i = 0;i < output_len;i++) {
211 presult[i] = result_content[i];
212 }
213 UNPROTECT(1);
214 }
215 free(result_content);
216 } else {
217 rv = 3;
218 result = R_NilValue;
219 }
220 }
221 }
222 switch (rv) {
223 case 1 : {
224 error("The input should be a character vector with length 1");
225 break;
226 }
227 case 2 : {
228 error("The input string is not a valid base64 encoded string");
229 break;
230 }
231 case 3 : {
232 error("Failed to allocate memory for result");
233 break;
234 }
235 }
236 return result;
237 }