Debian Codegraph @

src/recode.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1990-2022 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1990.
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
23 /* Global declarations and definitions. */
24
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <unistd.h>
28
29 #include "hash.h"
30
31 extern const char *program_name;
32
33 /* Error handling. */
34
35 #include <stdarg.h>
36
37 _GL_ATTRIBUTE_FORMAT_PRINTF_SYSTEM (2, 3) void
38 recode_error (_GL_UNUSED RECODE_OUTER outer, const char *format, ...)
39 {
40 va_list args;
41
42 va_start (args, format);
43 vfprintf (stderr, format, args);
44 va_end (args);
45 putc ('\n', stderr);
46 fflush (stderr);
47 }
48
49 _GL_ATTRIBUTE_FORMAT_PRINTF_SYSTEM (2, 3) void
50 recode_perror (_GL_UNUSED RECODE_OUTER outer, const char *format, ...)
51 {
52 int saved_errno = errno;
53 va_list args;
54
55 va_start (args, format);
56 vfprintf (stderr, format, args);
57 va_end (args);
58 fprintf (stderr, ": %s\n", strerror (saved_errno));
59 fflush (stderr);
60 }
61
62 _GL_ATTRIBUTE_MALLOC void *
63 recode_malloc (RECODE_OUTER outer, size_t size)
64 {
65 void *result;
66
67 result = calloc (1, size);
68 if (!result)
69 recode_error (outer, _("Virtual memory exhausted"));
70
71 return result;
72 }
73
74 void *
75 recode_realloc (RECODE_OUTER outer, void *pointer, size_t size)
76 {
77 void *result;
78
79 result = realloc (pointer, size);
80 if (!result)
81 recode_error (outer, _("Virtual memory exhausted"));
82
83 return result;
84 }
85
86 /* Single step handling. */
87
88 /*------------------------------------------------------------------.
89 | Create a one to one table which is the inverse of the given one. |
90 `------------------------------------------------------------------*/
91
92 unsigned char *
93 recode_invert_table (RECODE_OUTER outer, const unsigned char *table)
94 {
95 unsigned char flag[256];
96 unsigned char *result;
97 bool table_error;
98 unsigned counter;
99
100 if (!ALLOC (result, 256, unsigned char))
101 return NULL;
102 memset (flag, 0, 256);
103 table_error = false;
104
105 for (counter = 0; counter < 256; counter++)
106 {
107 if (flag[table[counter]])
108 {
109 recode_error (outer, _("Codes %3d and %3u both recode to %3d"),
110 result[table[counter]], counter, table[counter]);
111 table_error = true;
112 }
113 else
114 {
115 result[table[counter]] = counter;
116 flag[table[counter]] = 1;
117 }
118 }
119 if (table_error)
120 {
121 for (counter = 0; counter < 256; counter++)
122 if (!flag[counter])
123 recode_error (outer, _("No character recodes to %3u"), counter);
124 recode_error (outer, _("Cannot invert given one-to-one table"));
125 }
126 return result;
127 }
128
129 /*---------------------------------------------------------------------------.
130 | Complete a STEP descriptor by a constructed recoding array for 256 chars |
131 | and the adequate recoding routine. Use a KNOWN_PAIRS array of |
132 | NUMBER_OF_PAIRS constraints. If FIRST_HALF_IMPLIED is not zero, default |
133 | the unconstrained characters of the first 128 to the identity mapping. If |
134 | REVERSE is not zero, use right_table instead of left_table to complete the |
135 | table, yet new pairs are created only when fallback is recode_reversibility. |
136 `---------------------------------------------------------------------------*/
137
138 bool
139 recode_complete_pairs (RECODE_OUTER outer, RECODE_STEP step,
140 const struct recode_known_pair *known_pairs,
141 unsigned number_of_pairs, bool first_half_implied, bool reverse)
142 {
143 unsigned char left_flag[256];
144 unsigned char right_flag[256];
145 unsigned char left_table[256];
146 unsigned char right_table[256];
147 bool table_error;
148
149 unsigned char *flag;
150 unsigned char *table;
151 const char **table2;
152 char *cursor;
153 unsigned char left;
154 unsigned char right;
155 unsigned char search;
156 unsigned counter;
157 unsigned used;
158
159 /* Init tables with zeroes. */
160
161 memset (left_flag, 0, 256);
162 memset (right_flag, 0, 256);
163 memset (left_table, 0, 256);
164 memset (right_table, 0, 256);
165 table_error = false;
166
167 /* Establish known data. */
168
169 for (counter = 0; counter < number_of_pairs; counter++)
170 {
171 left = known_pairs[counter].left;
172 right = known_pairs[counter].right;
173
174 /* Set one known correspondence. */
175
176 if (left_flag[left])
177 {
178 if (!table_error)
179 {
180 recode_error (outer, _("Following diagnostics for `%s' to `%s'"),
181 step->before->name, step->after->name);
182 table_error = true;
183 }
184 recode_error (outer,
185 _("Pair no. %u: <%3d, %3d> conflicts with <%3d, %3d>"),
186 counter, left, right, left, left_table[left]);
187 }
188 else if (right_flag[right])
189 {
190 if (!table_error)
191 {
192 recode_error (outer, _("Following diagnostics for `%s' to `%s'"),
193 step->before->name, step->after->name);
194 table_error = true;
195 }
196 recode_error (outer,
197 _("Pair no. %u: <%3d, %3d> conflicts with <%3d, %3d>"),
198 counter, left, right, right_table[right], right);
199 }
200 else
201 {
202 left_flag[left] = 1;
203 left_table[left] = right;
204 right_flag[right] = 1;
205 right_table[right] = left;
206 }
207 }
208
209 /* Set all the implied correspondances. */
210
211 if (first_half_implied)
212 for (counter = 0; counter < 128; counter++)
213 if (!left_flag[counter] && !right_flag[counter])
214 {
215 left_flag[counter] = 1;
216 left_table[counter] = counter;
217 right_flag[counter] = 1;
218 right_table[counter] = counter;
219 }
220
221 if (step->fallback_routine == recode_reversibility)
222 {
223 /* If the recoding is not strict, compute a reversible one to one
224 table. */
225
226 if (table_error)
227 recode_error (outer,
228 _("Cannot complete table from set of known pairs"));
229
230 /* Close the table with small permutation cycles. */
231
232 for (counter = 0; counter < 256; counter++)
233 if (!right_flag[counter])
234 {
235 search = counter;
236 while (left_flag[search])
237 search = left_table[search];
238 left_flag[search] = 1;
239 left_table[search] = counter;
240 right_flag[counter] = 1;
241 right_table[counter] = search;
242 }
243
244 /* Save a copy of the proper table. */
245
246 step->transform_routine = recode_transform_byte_to_byte;
247 if (!ALLOC (table, 256, unsigned char))
248 return false;
249 memcpy (table, reverse ? right_table : left_table, 256);
250 step->step_type = RECODE_BYTE_TO_BYTE;
251 step->step_table = table;
252 step->step_table_term_routine = free;
253
254 /* Upgrade step quality to reversible. */
255
256 step->quality = outer->quality_byte_reversible;
257 }
258 else
259 {
260 /* If the recoding is strict, prepare a one to many table, each
261 entry being NULL or a string of a single character. */
262
263 /* Select the proper table. */
264
265 if (reverse)
266 {
267 flag = right_flag;
268 table = right_table;
269 }
270 else
271 {
272 flag = left_flag;
273 table = left_table;
274 }
275
276 /* Allocate everything in one blow, so it will be freed likewise. */
277
278 used = 0;
279 for (counter = 0; counter < 256; counter++)
280 if (flag[counter])
281 used++;
282
283 if (!ALLOC_SIZE (table2, 256 * sizeof (char *) + 2 * used, const char *))
284 return false;
285 cursor = (char *) (table2 + 256);
286
287 /* Construct the table and the strings in parallel. */
288
289 for (counter = 0; counter < 256; counter++)
290 if (flag[counter])
291 {
292 table2[counter] = cursor;
293 *cursor++ = table[counter];
294 *cursor++ = NUL;
295 }
296 else
297 table2[counter] = NULL;
298
299 /* Save a one to many recoding table. */
300
301 step->transform_routine = recode_transform_byte_to_variable;
302 step->step_type = RECODE_BYTE_TO_STRING;
303 step->step_table = table2;
304 step->step_table_term_routine = free;
305 }
306
307 return true;
308 }
309
310 /* Special handling for UCS-2 tables. */
311
312 /*-------------------------------------------------------------------------.
313 | Recode a file from one byte characters to double byte UCS-2 characters. |
314 `-------------------------------------------------------------------------*/
315
316 bool
317 recode_transform_byte_to_ucs2 (RECODE_SUBTASK subtask)
318 {
319 int input_char; /* current character */
320 int output_value; /* value being output */
321
322 if (input_char = recode_get_byte (subtask), input_char != EOF)
323 {
324 if (subtask->task->byte_order_mark)
325 recode_put_ucs2 (BYTE_ORDER_MARK, subtask);
326
327 while (input_char != EOF)
328 {
329 output_value = recode_code_to_ucs2 (subtask->step->before, input_char);
330 if (output_value < 0)
331 {
332 RETURN_IF_NOGO (RECODE_UNTRANSLATABLE, subtask);
333 recode_put_ucs2 (REPLACEMENT_CHARACTER, subtask);
334 }
335 else
336 recode_put_ucs2 (output_value, subtask);
337
338 input_char = recode_get_byte (subtask);
339 }
340 }
341
342 SUBTASK_RETURN (subtask);
343 }
344
345 /*-------------------------------------------------------------------------.
346 | Recode a file from double byte UCS-2 characters to one byte characters. |
347 `-------------------------------------------------------------------------*/
348
349 struct ucs2_to_byte
350 {
351 recode_ucs2 code; /* UCS-2 value */
352 unsigned char byte; /* corresponding byte */
353 };
354
355 struct ucs2_to_byte_local
356 {
357 Hash_table *table;
358 struct ucs2_to_byte *data;
359 };
360
361 static size_t
362 ucs2_to_byte_hash (const void *void_data, size_t table_size)
363 {
364 const struct ucs2_to_byte *data = (const struct ucs2_to_byte *) void_data;
365
366 return data->code % table_size;
367 }
368
369 static bool
370 ucs2_to_byte_compare (const void *void_first, const void *void_second)
371 {
372 const struct ucs2_to_byte *first = (const struct ucs2_to_byte *) void_first;
373 const struct ucs2_to_byte *second = (const struct ucs2_to_byte *) void_second;
374
375 return first->code == second->code;
376 }
377
378 static bool
379 term_ucs2_to_byte (RECODE_STEP step)
380 {
381 hash_free (((struct ucs2_to_byte_local *) step->local)->table);
382 free (((struct ucs2_to_byte_local *) step->local)->data);
383 free (step->local);
384 return true;
385 }
386
387 bool
388 recode_init_ucs2_to_byte (RECODE_STEP step,
389 RECODE_CONST_REQUEST request,
390 RECODE_CONST_OPTION_LIST before_options,
391 RECODE_CONST_OPTION_LIST after_options)
392 {
393 RECODE_OUTER outer = request->outer;
394 Hash_table *table;
395 struct ucs2_to_byte *data;
396 unsigned counter;
397
398 if (before_options || after_options)
399 return false;
400
401 table = hash_initialize (0, NULL,
402 ucs2_to_byte_hash, ucs2_to_byte_compare, NULL);
403 if (!table)
404 return false;
405
406 if (!ALLOC (data, 256, struct ucs2_to_byte))
407 {
408 hash_free (table);
409 return false;
410 }
411
412 for (counter = 0; counter < 256; counter++)
413 {
414 data[counter].code = recode_code_to_ucs2 (step->after, counter);
415 data[counter].byte = counter;
416 if (!hash_insert (table, data + counter))
417 {
418 hash_free (table);
419 free (data);
420 return false;
421 }
422 }
423
424 if (!ALLOC (step->local, 1, struct ucs2_to_byte_local))
425 {
426 hash_free (table);
427 free (data);
428 return false;
429 }
430 ((struct ucs2_to_byte_local *) step->local)->table = table;
431 ((struct ucs2_to_byte_local *) step->local)->data = data;
432
433 step->term_routine = term_ucs2_to_byte;
434 return true;
435 }
436
437 bool
438 recode_transform_ucs2_to_byte (RECODE_SUBTASK subtask)
439 {
440 Hash_table *table = ((struct ucs2_to_byte_local *) subtask->step->local)->table;
441 struct ucs2_to_byte lookup;
442 struct ucs2_to_byte *entry;
443 unsigned input_value; /* current UCS-2 character */
444
445 while (recode_get_ucs2 (&input_value, subtask))
446 {
447 lookup.code = input_value;
448 entry = (struct ucs2_to_byte *) hash_lookup (table, &lookup);
449 if (entry)
450 recode_put_byte (entry->byte, subtask);
451 else
452 RETURN_IF_NOGO (RECODE_UNTRANSLATABLE, subtask);
453 }
454
455 SUBTASK_RETURN (subtask);
456 }
457
458 /* Table editing on stdout. */
459
460 /*------------------------------------------------------------------------.
461 | Produce an include file representing the recoding, on standard output. |
462 `------------------------------------------------------------------------*/
463
464 bool
465 recode_format_table (RECODE_REQUEST request,
466 enum recode_programming_language header_language,
467 const char *header_name)
468 {
469 RECODE_OUTER outer = request->outer;
470
471 RECODE_CONST_STEP step; /* step being analysed */
472 unsigned column; /* column counter */
473 char *name; /* constructed name */
474 char *cursor; /* cursor in constructed name */
475 const char *cursor2; /* cursor to study strings */
476 unsigned counter; /* general purpose counter */
477 bool underline; /* previous character was underline */
478
479 const char *start_comment; /* string starting a comment block */
480 const char *wrap_comment; /* string separating two comment lines */
481 const char *end_comment; /* string ending a comment block */
482
483 if (request->sequence_length == 0)
484 {
485 recode_error (outer, _("Identity recoding, not worth a table"));
486 return false;
487 }
488
489 if (request->sequence_length > 1
490 || request->sequence_array[0].step_type == RECODE_NO_STEP_TABLE)
491 {
492 recode_error (outer, _("Recoding is too complex for a mere table"));
493 return false;
494 }
495
496 switch (header_language)
497 {
498 case RECODE_LANGUAGE_C:
499 start_comment = "/* ";
500 wrap_comment = "\n ";
501 end_comment = " */\n";
502 break;
503
504 case RECODE_LANGUAGE_PERL:
505 start_comment = "# ";
506 wrap_comment = "\n# ";
507 end_comment = "\n";
508 break;
509
510 default:
511 /* So lint is happy! */
512 start_comment = NULL;
513 wrap_comment = NULL;
514 end_comment = NULL;
515 }
516
517 /* This function is called only when the recoding sequence contains a single
518 step, so it is safe to use request->sequence_array[0] for the step. */
519
520 step = request->sequence_array;
521
522 /* Print the header of the header file. */
523
524 printf (_("%sConversion table generated mechanically by %s %s"),
525 start_comment, PACKAGE, VERSION);
526 printf (_("%sfor sequence %s.%s"),
527 wrap_comment, recode_edit_sequence (request, 1), end_comment);
528 printf ("\n");
529
530 /* Construct the name of the resulting table. */
531
532 if (header_name)
533 {
534 if (!ALLOC (name, strlen (header_name) + 1, char))
535 return false;
536 strcpy (name, header_name);
537 }
538 else
539 name = recode_edit_sequence (request, 0);
540
541 /* Ensure the table name contains only valid characters for a C identifier.
542 */
543
544 underline = false;
545 cursor = name;
546 for (cursor2 = name; *cursor2; cursor2++)
547 if ((*cursor2 >= 'a' && *cursor2 <= 'z')
548 || (*cursor2 >= 'A' && *cursor2 <= 'Z')
549 || (*cursor2 >= '0' && *cursor2 <= '9'))
550 {
551 if (underline)
552 {
553 *cursor++ = '_';
554 underline = false;
555 }
556 *cursor++ = *cursor2;
557 }
558 else if (cursor != name)
559 underline = true;
560 *cursor = NUL;
561
562 /* Produce the recoding table in the correct format. */
563
564 if (step->step_type == RECODE_BYTE_TO_BYTE)
565 {
566 const unsigned char *table = (const unsigned char *) step->step_table;
567
568 /* Produce a one to one recoding table. */
569
570 switch (header_language)
571 {
572 case RECODE_NO_LANGUAGE:
573 assert (0);
574
575 case RECODE_LANGUAGE_C:
576 printf ("unsigned char const %s[256] =\n", name);
577 printf (" {\n");
578 break;
579
580 case RECODE_LANGUAGE_PERL:
581 printf ("@%s =\n", name);
582 printf (" (\n");
583 break;
584
585 default:
586 break;
587 }
588 for (counter = 0; counter < 256; counter++)
589 {
590 printf ("%s%3d,", counter % 8 == 0 ? " " : " ", table[counter]);
591 if (counter % 8 == 7)
592 printf ("\t%s%3u - %3u%s",
593 start_comment, counter - 7, counter, end_comment);
594 }
595 switch (header_language)
596 {
597 case RECODE_NO_LANGUAGE:
598 assert (0);
599
600 case RECODE_LANGUAGE_C:
601 printf (" };\n");
602 break;
603
604 case RECODE_LANGUAGE_PERL:
605 printf (" );\n");
606 break;
607
608 default:
609 break;
610 }
611 }
612 else if (step->step_type == RECODE_BYTE_TO_STRING)
613 {
614 const char *const *table = (const char *const *) step->step_table;
615
616 /* Produce a one to many recoding table. */
617
618 switch (header_language)
619 {
620 case RECODE_NO_LANGUAGE:
621 assert (0);
622
623 case RECODE_LANGUAGE_C:
624 printf ("const char *%s[256] =\n", name);
625 printf (" {\n");
626 break;
627
628 case RECODE_LANGUAGE_PERL:
629 printf ("@%s =\n", name);
630 printf (" (\n");
631 break;
632
633 default:
634 break;
635 }
636 for (counter = 0; counter < 256; counter++)
637 {
638 printf (" ");
639 column = 4;
640 if (table[counter])
641 {
642 printf ("\"");
643 column++;
644 for (cursor2 = table[counter]; *cursor2; cursor2++)
645 switch (*cursor2)
646 {
647 case ' ':
648 printf (" ");
649 column++;
650 break;
651
652 case '\b':
653 printf ("\\b");
654 column += 2;
655 break;
656
657 case '\t':
658 printf ("\\t");
659 column += 2;
660 break;
661
662 case '\n':
663 printf ("\\n");
664 column += 2;
665 break;
666
667 case '"':
668 printf ("\\\"");
669 column += 2;
670 break;
671
672 case '\\':
673 printf ("\\\\");
674 column += 2;
675 break;
676
677 case '$':
678 if (header_language == RECODE_LANGUAGE_PERL)
679 {
680 printf ("\\$");
681 column += 2;
682 break;
683 }
684 FALLTHROUGH;
685
686 default:
687 if (isprint (*cursor2))
688 {
689 printf ("%c", *cursor2);
690 column++;
691 }
692 else
693 {
694 printf ("\\%.3o", *(const unsigned char *) cursor2);
695 column += 4;
696 }
697 }
698 printf ("\"");
699 column++;
700 }
701 else
702 switch (header_language)
703 {
704 case RECODE_NO_LANGUAGE:
705 assert (0);
706
707 case RECODE_LANGUAGE_C:
708 printf ("0");
709 column++;
710 break;
711
712 case RECODE_LANGUAGE_PERL:
713 printf ("''");
714 column += 2;
715 break;
716
717 default:
718 break;
719 }
720 printf (",");
721 column++;
722 while (column < 32)
723 {
724 printf ("\t");
725 column += 8 - column % 8;
726 }
727 printf ("%s%3u%s", start_comment, counter, end_comment);
728 }
729 switch (header_language)
730 {
731 case RECODE_NO_LANGUAGE:
732 assert (0);
733
734 case RECODE_LANGUAGE_C:
735 printf (" };\n");
736 break;
737
738 case RECODE_LANGUAGE_PERL:
739 printf (" );\n");
740 break;
741
742 default:
743 break;
744 }
745 }
746 else
747 {
748 recode_error (outer, _("No table to print"));
749 free (name);
750 return false;
751 }
752
753 free (name);
754 return true;
755 }