Debian Codegraph @

src/dump.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1997-2018 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1997.
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
24 /* Constants for the possible bases. If these are reordered, so should be
25 the initialisers for the three tables which follow. */
26 enum base
27 {
28 OCTAL,
29 DECIMAL,
30 HEXADECIMAL
31 };
32
33 /* Printing format to use, depending on both the base and the actual number
34 of bytes. The zero byte case is unused. */
35 static const char *format_table[3][5]
36 = {{ NULL, "0%03o", "0%06o", "0%08o", "0%011o" },
37 { NULL, "%3u", "%5u", "%8u", "%10u" },
38 { NULL, "0x%02X", "0x%04X", "0x%06X", "0x%08X" }};
39
40 /* Number of active digits to expect, depending on both the base and the
41 actual number of bytes. The zero byte case is unused. The values do not
42 include the `0' octal prefix nor the `0x' for hexadecimal. */
43 static unsigned width_table[3][5]
44 = {{ 0, 3, 6, 8, 11 },
45 { 0, 3, 5, 8, 10 },
46 { 0, 2, 4, 6, 8 }};
47
48 /* Number of printed values per output line, depending on both the base and
49 the actual number of bytes. The zero byte case is unused. The three
50 byte case is also unused, even if the table offers a value for it. */
51 static unsigned per_line_table[3][5]
52 = {{ 0, 12, 8, 6, 4 },
53 { 0, 15, 10, 7, 5 },
54 { 0, 12, 8, 7, 6 }};
55
56 static bool
57 dump (RECODE_SUBTASK subtask,
58 enum base base, unsigned size)
59 {
60 unsigned per_line = per_line_table[base][size];
61 unsigned column = 0;
62 int character = recode_get_byte (subtask);
63
64 while (character != EOF)
65 {
66 unsigned value = BIT_MASK (8) & character;
67 unsigned byte_count;
68 char buffer[14];
69 const char *cursor;
70
71 for (byte_count = 1; byte_count < size; byte_count++)
72 {
73 character = recode_get_byte (subtask);
74 if (character == EOF)
75 break;
76 value = (value << 8) | (BIT_MASK (8) & character);
77 }
78
79 /* Write delimiters. */
80
81 if (column == per_line)
82 {
83 recode_put_byte (',', subtask);
84 recode_put_byte ('\n', subtask);
85 column = 1;
86 }
87 else if (column == 0)
88 column = 1;
89 else
90 {
91 recode_put_byte (',', subtask);
92 recode_put_byte (' ', subtask);
93 column++;
94 }
95
96 /* Write formatted value. */
97
98 #pragma GCC diagnostic push
99 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
100 sprintf (buffer, format_table[base][byte_count], value);
101 #pragma GCC diagnostic pop
102 for (cursor = buffer; *cursor; cursor++)
103 recode_put_byte (*cursor, subtask);
104
105 /* Prepare for next iteration. */
106
107 if (character != EOF)
108 character = recode_get_byte (subtask);
109 }
110
111 recode_put_byte ('\n', subtask);
112 SUBTASK_RETURN (subtask);
113 }
114
115 static bool
116 undump (RECODE_SUBTASK subtask,
117 enum base expected_base, unsigned expected_size)
118 {
119 unsigned per_line = per_line_table[expected_base][expected_size];
120 unsigned column = 0;
121 int character = recode_get_byte (subtask);
122 bool last_is_short = false;
123 bool comma_on_last = character != EOF;
124
125 while (character != EOF)
126 {
127 unsigned width = 0;
128 unsigned value = 0;
129 enum base base;
130 unsigned size;
131
132 /* Skip whitespace. But count it, in case of a decimal number. */
133
134 while (character == ' ' || character == '\t' || character =='\n')
135 {
136 if (character == ' ')
137 width++;
138 else
139 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
140
141 character = recode_get_byte (subtask);
142 }
143 if (character == EOF)
144 {
145 if (width != 0)
146 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
147 break;
148 }
149
150 /* Decide the base for the incoming character, possibly abort the
151 recoding if not the proper base. */
152
153 if (character == '0')
154 {
155 character = recode_get_byte (subtask);
156
157 if (character == 'x')
158 {
159 if (width != 0)
160 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
161 width = 0;
162 base = HEXADECIMAL;
163 character = recode_get_byte (subtask);
164 }
165 else if (character >= '0' && character <= '9')
166 {
167 /* If followed by 8 or 9, declare it octal nevertheless. It
168 is the proper thing to do, a reject might occur below. */
169 if (width != 0)
170 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
171 width = 0;
172 base = OCTAL;
173 }
174 else
175 {
176 /* Otherwise, this is a simple, mere, happy decimal zero. */
177 width++;
178 base = DECIMAL;
179 }
180 }
181 else if (character >= '1' && character <= '9')
182 base = DECIMAL;
183 else
184 {
185 /* This might be some other line of a C header. Merely skip the
186 line. FIXME: This should probably skip multi-line comments or
187 string constants, so to not get fooled by them. */
188
189 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
190 while (character != '\n' && character != EOF)
191 character = recode_get_byte (subtask);
192 if (character == '\n')
193 character = recode_get_byte (subtask);
194
195 continue;
196 }
197
198 if (base != expected_base)
199 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
200
201 /* Decode one value. */
202
203 switch (base)
204 {
205 case OCTAL:
206 while (character >= '0' && character <= '7')
207 {
208 value = (value << 3) | (character - '0');
209 width++;
210 character = recode_get_byte (subtask);
211 }
212 break;
213
214 case DECIMAL:
215 while (character >= '0' && character <= '9')
216 {
217 value = value * 10 + character - '0';
218 width++;
219 character = recode_get_byte (subtask);
220 }
221 break;
222
223 case HEXADECIMAL:
224 while (true)
225 if (character >= '0' && character <= '9')
226 {
227 value = (value << 4) | (character - '0');
228 width++;
229 character = recode_get_byte (subtask);
230 }
231 else if (character >= 'A' && character <= 'F')
232 {
233 value = (value << 4) | (character - 'A' + 10);
234 width++;
235 character = recode_get_byte (subtask);
236 }
237 else if (character >= 'a' && character <= 'f')
238 {
239 value = (value << 4) | (character - 'a' + 10);
240 width++;
241 character = recode_get_byte (subtask);
242 }
243 else
244 break;
245 break;
246
247 default:
248 break;
249 }
250
251 if (width == 0 || width > width_table[base][expected_size])
252 {
253 /* If the observed width is greater than expected, the input is
254 invalid, even if the value is in range. */
255
256 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
257 }
258 else
259 {
260 if (last_is_short)
261 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
262 if (!comma_on_last)
263 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
264
265 /* Decide how many bytes to produce. */
266
267 last_is_short = width < width_table[base][expected_size];
268 if (last_is_short)
269 {
270 /* If the observed width is smaller than expected, accept the
271 value as representing less than the expected number of
272 bytes. However, the width has to match very exactly. */
273
274 for (size = 1; size < 4; size++)
275 if (width_table[base][size] == width)
276 break;
277 if (size == 4)
278 {
279 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
280 continue;
281 }
282 }
283 else
284 size = expected_size;
285
286 /* Produce the output bytes. */
287 for (unsigned shift = size; shift != 0; shift--)
288 recode_put_byte (BIT_MASK (8) & value >> ((shift * 8) - 8), subtask);
289 }
290
291 /* Skip separators. */
292
293 comma_on_last = character == ',';
294 if (!comma_on_last)
295 {
296 if (character == '\n')
297 character = recode_get_byte (subtask);
298 else
299 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
300 }
301 else
302 {
303 character = recode_get_byte (subtask);
304 if (character == ' ')
305 {
306 column++;
307 character = recode_get_byte (subtask);
308 }
309 else if (character == '\n')
310 {
311 if (column + 1!= per_line)
312 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
313 column = 0;
314 character = recode_get_byte (subtask);
315 }
316 }
317 }
318
319 if (comma_on_last)
320 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
321
322 SUBTASK_RETURN (subtask);
323 }
324
325 #define TRANSFORM(Name, Service, Base, Size) \
326 static bool \
327 Name (RECODE_SUBTASK subtask) \
328 { \
329 return Service (subtask, Base, Size); \
330 }
331
332 TRANSFORM (data_oct1, dump, OCTAL, 1)
333 TRANSFORM (data_dec1, dump, DECIMAL, 1)
334 TRANSFORM (data_hex1, dump, HEXADECIMAL, 1)
335 TRANSFORM (data_oct2, dump, OCTAL, 2)
336 TRANSFORM (data_dec2, dump, DECIMAL, 2)
337 TRANSFORM (data_hex2, dump, HEXADECIMAL, 2)
338 TRANSFORM (data_oct4, dump, OCTAL, 4)
339 TRANSFORM (data_dec4, dump, DECIMAL, 4)
340 TRANSFORM (data_hex4, dump, HEXADECIMAL, 4)
341
342 TRANSFORM (oct1_data, undump, OCTAL, 1)
343 TRANSFORM (dec1_data, undump, DECIMAL, 1)
344 TRANSFORM (hex1_data, undump, HEXADECIMAL, 1)
345 TRANSFORM (oct2_data, undump, OCTAL, 2)
346 TRANSFORM (dec2_data, undump, DECIMAL, 2)
347 TRANSFORM (hex2_data, undump, HEXADECIMAL, 2)
348 TRANSFORM (oct4_data, undump, OCTAL, 4)
349 TRANSFORM (dec4_data, undump, DECIMAL, 4)
350 TRANSFORM (hex4_data, undump, HEXADECIMAL, 4)
351
352 bool
353 module_dump (RECODE_OUTER outer)
354 {
355 return
356
357 /* Single bytes. */
358
359 recode_declare_single (outer, "data", "Octal-1",
360 outer->quality_variable_to_variable,
361 NULL, data_oct1)
362 && recode_declare_single (outer, "data", "Decimal-1",
363 outer->quality_variable_to_variable,
364 NULL, data_dec1)
365 && recode_declare_single (outer, "data", "Hexadecimal-1",
366 outer->quality_variable_to_variable,
367 NULL, data_hex1)
368 && recode_declare_single (outer, "Octal-1", "data",
369 outer->quality_variable_to_variable,
370 NULL, oct1_data)
371 && recode_declare_single (outer, "Decimal-1", "data",
372 outer->quality_variable_to_variable,
373 NULL, dec1_data)
374 && recode_declare_single (outer, "Hexadecimal-1", "data",
375 outer->quality_variable_to_variable,
376 NULL, hex1_data)
377
378 && recode_declare_alias (outer, "o1", "Octal-1")
379 && recode_declare_alias (outer, "d1", "Decimal-1")
380 && recode_declare_alias (outer, "x1", "Hexadecimal-1")
381 && recode_declare_alias (outer, "o", "Octal-1")
382 && recode_declare_alias (outer, "d", "Decimal-1")
383 && recode_declare_alias (outer, "x", "Hexadecimal-1")
384
385 /* Double bytes. */
386
387 && recode_declare_single (outer, "data", "Octal-2",
388 outer->quality_variable_to_variable,
389 NULL, data_oct2)
390 && recode_declare_single (outer, "data", "Decimal-2",
391 outer->quality_variable_to_variable,
392 NULL, data_dec2)
393 && recode_declare_single (outer, "data", "Hexadecimal-2",
394 outer->quality_variable_to_variable,
395 NULL, data_hex2)
396 && recode_declare_single (outer, "Octal-2", "data",
397 outer->quality_variable_to_variable,
398 NULL, oct2_data)
399 && recode_declare_single (outer, "Decimal-2", "data",
400 outer->quality_variable_to_variable,
401 NULL, dec2_data)
402 && recode_declare_single (outer, "Hexadecimal-2", "data",
403 outer->quality_variable_to_variable,
404 NULL, hex2_data)
405
406 && recode_declare_alias (outer, "o2", "Octal-2")
407 && recode_declare_alias (outer, "d2", "Decimal-2")
408 && recode_declare_alias (outer, "x2", "Hexadecimal-2")
409
410 /* Quadruple bytes. */
411
412 && recode_declare_single (outer, "data", "Octal-4",
413 outer->quality_variable_to_variable,
414 NULL, data_oct4)
415 && recode_declare_single (outer, "data", "Decimal-4",
416 outer->quality_variable_to_variable,
417 NULL, data_dec4)
418 && recode_declare_single (outer, "data", "Hexadecimal-4",
419 outer->quality_variable_to_variable,
420 NULL, data_hex4)
421 && recode_declare_single (outer, "Octal-4", "data",
422 outer->quality_variable_to_variable,
423 NULL, oct4_data)
424 && recode_declare_single (outer, "Decimal-4", "data",
425 outer->quality_variable_to_variable,
426 NULL, dec4_data)
427 && recode_declare_single (outer, "Hexadecimal-4", "data",
428 outer->quality_variable_to_variable,
429 NULL, hex4_data)
430
431 && recode_declare_alias (outer, "o4", "Octal-4")
432 && recode_declare_alias (outer, "d4", "Decimal-4")
433 && recode_declare_alias (outer, "x4", "Hexadecimal-4")
434
435 ;
436 }
437
438 void
439 delmodule_dump (_GL_UNUSED RECODE_OUTER outer)
440 {
441 }