Debian Codegraph @

src/base64.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1996-2018 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1996.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 3 of the
8 License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the Recode Library; see the file `COPYING.LIB'.
17 If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include "common.h"
22 #include "decsteps.h"
23 #include "base64.h"
24
25 /* Table of characters coding the 64 values. */
26 char base64_value_to_char[64] =
27 {
28 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
29 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
30 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
31 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
32 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
33 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
34 '8', '9', '+', '/' /* 60-63 */
35 };
36
37 /* Table of base64 values for first 128 characters. */
38 #define z -1
39 short base64_char_to_value[128] =
40 {
41 z, z, z, z, z, z, z, z, z, z, /* 0- 9 */
42 z, z, z, z, z, z, z, z, z, z, /* 10- 19 */
43 z, z, z, z, z, z, z, z, z, z, /* 20- 29 */
44 z, z, z, z, z, z, z, z, z, z, /* 30- 39 */
45 z, z, z, 62, z, z, z, 63, 52, 53, /* 40- 49 */
46 54, 55, 56, 57, 58, 59, 60, 61, z, z, /* 50- 59 */
47 z, z, z, z, z, 0, 1, 2, 3, 4, /* 60- 69 */
48 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
49 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
50 25, z, z, z, z, z, z, 26, 27, 28, /* 90- 99 */
51 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
52 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
53 49, 50, 51, z, z, z, z, z /* 120-127 */
54 };
55 #undef z
56
57 /* The following diagram shows the logical steps by which three octets
58 get transformed into eight Base64 characters. It helps understanding
59 shifts and masks in the transformation functions.
60
61 .--------. .--------. .--------.
62 |aaaaaabb| |bbbbcccc| |ccdddddd|
63 `--------' `--------' `--------'
64 6 2 4 4 2 6
65 .--------+--------+--------+--------.
66 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
67 `--------+--------+--------+--------'
68
69 .--------+--------+--------+--------.
70 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
71 `--------+--------+--------+--------'
72
73 The octets are divided into 6 bit chunks, which are then encoded into
74 Base64 characters. */
75
76 static bool
77 transform_data_base64 (RECODE_SUBTASK subtask)
78 {
79 int counter;
80 int character;
81 unsigned value;
82
83 counter = 0;
84 while (true)
85 {
86 character = recode_get_byte (subtask);
87 if (character == EOF)
88 break;
89
90 /* Wrap line every 76 characters. */
91
92 if (counter < MIME_LINE_LENGTH / 4)
93 counter++;
94 else
95 {
96 recode_put_byte ('\n', subtask);
97 counter = 1;
98 }
99
100 /* Process first byte of a triplet. */
101
102 recode_put_byte (base64_value_to_char[BIT_MASK (6) & character >> 2], subtask);
103 value = (BIT_MASK (2) & character) << 4;
104
105 /* Process second byte of a triplet. */
106
107 character = recode_get_byte (subtask);
108 if (character == EOF)
109 {
110 recode_put_byte (base64_value_to_char[value], subtask);
111 recode_put_byte ('=', subtask);
112 recode_put_byte ('=', subtask);
113 break;
114 }
115 recode_put_byte (base64_value_to_char[value | (BIT_MASK (4) & character >> 4)],
116 subtask);
117 value = (BIT_MASK (4) & character) << 2;
118
119 /* Process third byte of a triplet. */
120
121 character = recode_get_byte (subtask);
122 if (character == EOF)
123 {
124 recode_put_byte (base64_value_to_char[value], subtask);
125 recode_put_byte ('=', subtask);
126 break;
127 }
128 recode_put_byte (base64_value_to_char[value | (BIT_MASK (2) & character >> 6)],
129 subtask);
130 recode_put_byte (base64_value_to_char[BIT_MASK (6) & character], subtask);
131 }
132
133 /* Complete last partial line. */
134
135 if (counter > 0)
136 recode_put_byte ('\n', subtask);
137
138 SUBTASK_RETURN (subtask);
139 }
140
141 static bool
142 transform_base64_data (RECODE_SUBTASK subtask)
143 {
144 int counter = 0;
145 int character;
146 unsigned value;
147
148 while (true)
149 {
150 /* Accept wrapping lines, reversibly if at each 76 characters. */
151
152 character = recode_get_byte (subtask);
153
154 top:
155 if (character == EOF)
156 SUBTASK_RETURN (subtask);
157
158 if (character == '\n')
159 {
160 character = recode_get_byte (subtask);
161 if (character == EOF)
162 SUBTASK_RETURN (subtask);
163 if (counter != MIME_LINE_LENGTH / 4)
164 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
165 counter = 0;
166 goto top;
167 }
168
169 if (character == '\r')
170 {
171 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
172 counter = 0;
173 continue;
174 }
175
176 /* Process first byte of a quadruplet. */
177
178 counter++;
179
180 if (IS_BASE64 (character))
181 value = base64_char_to_value[character] << 18;
182 else
183 {
184 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
185 value = 0;
186 }
187
188 /* Process second byte of a quadruplet. */
189
190 character = recode_get_byte (subtask);
191 if (character == EOF)
192 {
193 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
194 SUBTASK_RETURN (subtask);
195 }
196
197 if (IS_BASE64 (character))
198 value |= base64_char_to_value[character] << 12;
199 else
200 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
201
202 recode_put_byte (value >> 16, subtask);
203
204 /* Process third byte of a quadruplet. */
205
206 character = recode_get_byte (subtask);
207 if (character == EOF)
208 {
209 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
210 SUBTASK_RETURN (subtask);
211 }
212
213 if (character == '=')
214 {
215 character = recode_get_byte (subtask);
216 if (character != '=')
217 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
218 continue;
219 }
220
221 if (IS_BASE64 (character))
222 value |= base64_char_to_value[character] << 6;
223 else
224 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
225
226 recode_put_byte (BIT_MASK (8) & value >> 8, subtask);
227
228 /* Process fourth byte of a quadruplet. */
229
230 character = recode_get_byte (subtask);
231 if (character == EOF)
232 {
233 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
234 SUBTASK_RETURN (subtask);
235 }
236
237 if (character == '=')
238 continue;
239
240 if (IS_BASE64 (character))
241 value |= base64_char_to_value[character];
242 else
243 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
244
245 recode_put_byte (BIT_MASK (8) & value, subtask);
246 }
247 }
248
249 bool
250 module_base64 (RECODE_OUTER outer)
251 {
252 return
253 recode_declare_single (outer, "data", "Base64",
254 outer->quality_variable_to_variable,
255 NULL, transform_data_base64)
256 && recode_declare_single (outer, "Base64", "data",
257 outer->quality_variable_to_variable,
258 NULL, transform_base64_data)
259 && recode_declare_alias (outer, "b64", "Base64")
260 && recode_declare_alias (outer, "64", "Base64");
261 }
262
263 void
264 delmodule_base64 (_GL_UNUSED RECODE_OUTER outer)
265 {
266 }