Debian Codegraph @

src/request.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1990-2018 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 /* Quality handling. */
24
25 /*---------------------------------------.
26 | Return a string describing a quality. |
27 `---------------------------------------*/
28
29 static const char *
30 quality_to_string (struct recode_quality quality)
31 {
32 static char buffer[100];
33
34 if (quality.reversible)
35 return _("reversible");
36
37 sprintf (buffer, _("%s to %s"),
38 (quality.in_size == RECODE_1 ? _("byte")
39 : quality.in_size == RECODE_2 ? _("ucs2") : _("variable")),
40 (quality.out_size == RECODE_1 ? _("byte")
41 : quality.out_size == RECODE_2 ? _("ucs2") : _("variable")));
42 return buffer;
43 }
44
45 /*-----------------------------------------------------------------------.
46 | Adjust the FIRST quality of a step for the result of merging this step |
47 | with another succeeding one, given its SECOND quality. |
48 `-----------------------------------------------------------------------*/
49
50 static void
51 merge_qualities (struct recode_quality *first,
52 const struct recode_quality second)
53 {
54 first->out_size = second.out_size;
55 first->reversible = first->reversible && second.reversible;
56 first->slower = first->slower || second.slower;
57 first->faster = first->faster && second.faster;
58 }
59
60 /* Editing out sequences. */
61
62 /*---------------------------------------------.
63 | Add CHARACTER to the generated work string. |
64 `---------------------------------------------*/
65
66 static void
67 add_work_character (RECODE_REQUEST request, int character)
68 {
69 if (request->work_string_length + 1 >= request->work_string_allocated)
70 {
71 char *new_work_string;
72
73 request->work_string_allocated += 100;
74 new_work_string = (char *)
75 realloc (request->work_string, request->work_string_allocated);
76 if (new_work_string)
77 request->work_string = new_work_string;
78 else
79 return; /* the diagnostic gets truncated, no need to fuss about it. */
80 }
81 request->work_string[request->work_string_length++] = character;
82 }
83
84 /*----------------------------------.
85 | Add a STRING to the work string. |
86 `----------------------------------*/
87
88 static void
89 add_work_string (RECODE_REQUEST request, const char *string)
90 {
91 while (*string)
92 add_work_character (request, *string++);
93 }
94
95 /*----------------------------------------------------------------------.
96 | Generate a string describing the current sequence and return it. |
97 | Include a description of recoding quality only if EDIT_QUALITY is not |
98 | zero. |
99 `----------------------------------------------------------------------*/
100
101 char *
102 recode_edit_sequence (RECODE_REQUEST request, bool edit_quality)
103 {
104 RECODE_OUTER outer = request->outer;
105
106 request->work_string_length = 0;
107
108 if (request->sequence_length < 0)
109 add_work_string (request, _("*Unachievable*"));
110 else if (request->sequence_length == 0)
111 add_work_string (request, _("*mere copy*"));
112 else
113 {
114 RECODE_SYMBOL last_charset_printed = NULL;
115 RECODE_STEP step = request->sequence_array;
116
117 while (step < request->sequence_array + request->sequence_length)
118 {
119 RECODE_STEP unsurfacer_start = step;
120 RECODE_STEP unsurfacer_end;
121
122 /* Find unsurfacers. */
123
124 while (step < request->sequence_array + request->sequence_length
125 && step->after == outer->data_symbol)
126 step++;
127 unsurfacer_end = step;
128
129 /* Print BEFORE, sparing it if syntax permits. */
130
131 if (step != unsurfacer_start
132 || step == request->sequence_array + request->sequence_length
133 || step->before != last_charset_printed)
134 {
135 if (unsurfacer_start != request->sequence_array)
136 add_work_character (request, ',');
137 if (step < request->sequence_array + request->sequence_length)
138 {
139 last_charset_printed = step->before;
140 add_work_string (request, last_charset_printed->name);
141 }
142 }
143
144 /* Print unsurfacers. */
145
146 for (step = unsurfacer_end - 1; step >= unsurfacer_start; step--)
147 {
148 add_work_character (request, '/');
149 add_work_string (request, step->before->name);
150 }
151 step = unsurfacer_end;
152
153 /* Print AFTER. */
154
155 add_work_string (request, "..");
156 if (step < request->sequence_array + request->sequence_length
157 && step->before != outer->data_symbol)
158 {
159 last_charset_printed = step->after;
160 add_work_string (request, last_charset_printed->name);
161 step++;
162 }
163 else
164 {
165 last_charset_printed = outer->data_symbol;
166 add_work_string (request, last_charset_printed->name);
167 }
168
169 /* Print resurfacers. */
170
171 while (step < request->sequence_array + request->sequence_length
172 && step->before == outer->data_symbol)
173 {
174 add_work_character (request, '/');
175 last_charset_printed = NULL;
176 add_work_string (request, step->after->name);
177 step++;
178 }
179 }
180
181 if (edit_quality)
182 {
183 struct recode_quality quality = outer->quality_byte_reversible;
184 RECODE_CONST_STEP step2;
185
186 for (step2 = request->sequence_array;
187 step2 < request->sequence_array + request->sequence_length;
188 step2++)
189 merge_qualities (&quality, step2->quality);
190 add_work_character (request, ' ');
191 add_work_character (request, '(');
192 add_work_string (request, quality_to_string (quality));
193 add_work_character (request, ')');
194 }
195 }
196
197 add_work_character (request, NUL);
198 return request->work_string;
199 }
200
201 /* Sequence construction and optimisation. */
202
203 /*-------------------------------------------------------------------------.
204 | To the end of a given SEQUENCE, add a new recoding step initialised from |
205 | SINGLE. |
206 `-------------------------------------------------------------------------*/
207
208 static bool
209 add_to_sequence (RECODE_REQUEST request, RECODE_SINGLE single,
210 RECODE_CONST_OPTION_LIST before_options,
211 RECODE_CONST_OPTION_LIST after_options)
212 {
213 RECODE_OUTER outer = request->outer;
214 RECODE_STEP step;
215
216 if ((unsigned)request->sequence_length == request->sequence_allocated)
217 {
218 unsigned old_allocated = request->sequence_allocated;
219
220 if (request->sequence_allocated == 0)
221 request->sequence_allocated = 16;
222 else
223 request->sequence_allocated *= 2;
224
225 if (!REALLOC (request->sequence_array, request->sequence_allocated,
226 struct recode_step))
227 {
228 recode_error (outer, _("Virtual memory exhausted!"));
229 return false;
230 }
231
232 memset (request->sequence_array + old_allocated, 0,
233 (request->sequence_allocated - old_allocated) * sizeof (struct recode_step));
234 }
235
236 step = request->sequence_array + request->sequence_length++;
237 step->before = single->before;
238 step->after = single->after;
239 step->step_table = single->initial_step_table;
240 step->step_table_term_routine = NULL;
241 step->step_type
242 = step->step_table ? RECODE_COMBINE_EXPLODE : RECODE_NO_STEP_TABLE;
243 step->transform_routine = single->transform_routine;
244 step->fallback_routine = single->fallback_routine;
245 step->term_routine = NULL;
246
247 if (single->init_routine)
248 {
249 if (!(*single->init_routine) (step, request,
250 before_options, after_options))
251 {
252 recode_error (outer, _("Step initialisation failed"));
253 return false;
254 }
255 }
256 else if (before_options || after_options)
257 {
258 recode_error (outer,
259 _("Step initialisation failed (unprocessed options)"));
260 return false;
261 }
262
263 return true;
264 }
265
266 /*----------------------------------------------------------------------.
267 | Find a SEQUENCE of single steps to achieve a conversion from charset |
268 | BEFORE to charset AFTER. Return false only if no sequence could been |
269 | found. Explain what was selected if VERBOSE. |
270 `----------------------------------------------------------------------*/
271
272 /* Cost corresponding to an impossible conversion. */
273 #define UNREACHABLE 30000
274
275 static bool
276 find_sequence (RECODE_REQUEST request,
277 RECODE_CONST_SYMBOL before,
278 RECODE_CONST_OPTION_LIST before_options,
279 RECODE_CONST_SYMBOL after,
280 RECODE_CONST_OPTION_LIST after_options)
281 {
282 RECODE_OUTER outer = request->outer;
283 struct search
284 {
285 RECODE_SINGLE single; /* single step aiming towards after */
286 int cost; /* cost from here through after */
287 };
288 struct search *search_array; /* critical path search tree */
289 struct search *search; /* item in search_array for charset */
290 RECODE_SINGLE single; /* cursor in possible single_singles */
291 int cost; /* cost under consideration */
292 bool modified; /* if modified since last iteration */
293 RECODE_CONST_SYMBOL charset; /* charset while reconstructing */
294
295 if (!ALLOC (search_array, outer->number_of_symbols, struct search))
296 return false;
297
298 /* Search for an economical route, looking our way backward from the after
299 towards the before. */
300
301 for (search = search_array;
302 search < search_array + outer->number_of_symbols;
303 search++)
304 {
305 search->single = NULL;
306 search->cost = UNREACHABLE;
307 }
308 search_array[after->ordinal].cost = 0;
309
310 modified = true;
311 while (modified)
312 {
313 modified = false;
314 for (single = outer->single_list; single; single = single->next)
315 if (!single->before->ignore)
316 {
317 cost = search_array[single->after->ordinal].cost;
318 if (cost != UNREACHABLE)
319 {
320 cost += single->conversion_cost;
321 search = search_array + single->before->ordinal;
322 if (cost < search->cost)
323 {
324 search->single = single;
325 search->cost = cost;
326 modified = true;
327 }
328 }
329 }
330 }
331
332 if (search_array[before->ordinal].cost == UNREACHABLE)
333 {
334 /* No path has been found. */
335
336 free (search_array);
337 return false;
338 }
339
340 /* Save the retained best path in the sequence array. */
341
342 for (charset = before; charset != after; charset = single->after)
343 {
344 single = search_array[charset->ordinal].single;
345 if (!add_to_sequence (request, single,
346 charset == before ? before_options : NULL,
347 single->after == after ? after_options : NULL))
348 break;
349 }
350
351 free (search_array);
352 return charset == after;
353 }
354
355 /*---------------------------------------------------------------------------.
356 | If the step table is usable for step merging, return its type. Otherwise, |
357 | return RECODE_NO_STEP_TABLE. Any non-trivial algorithm for the step |
358 | prevents step merging. |
359 `---------------------------------------------------------------------------*/
360
361 static enum recode_step_type
362 table_type (RECODE_CONST_REQUEST request,
363 RECODE_CONST_STEP step)
364 {
365 /* When producing source headers, we do not care about algorithms. */
366
367 if (!request->make_header_flag)
368
369 switch (step->step_type)
370 {
371 case RECODE_BYTE_TO_BYTE:
372 if (step->transform_routine != recode_transform_byte_to_byte)
373 return RECODE_NO_STEP_TABLE;
374 break;
375
376 case RECODE_BYTE_TO_STRING:
377 if (step->transform_routine != recode_transform_byte_to_variable)
378 return RECODE_NO_STEP_TABLE;
379 break;
380
381 default:
382 return RECODE_NO_STEP_TABLE;
383 }
384
385 return step->step_type;
386 }
387
388 /*---------------------------------------------------------------.
389 | Order two struct item's lexicographically of their key value. |
390 `---------------------------------------------------------------*/
391
392 struct item
393 {
394 unsigned short code; /* UCS-2 value */
395 unsigned char byte; /* charset code [0..255] */
396 };
397
398 static int
399 compare_struct_item (const void *void_first, const void *void_second)
400 {
401 return (((const struct item *) void_first)->code
402 - ((const struct item *) void_second)->code);
403 }
404
405 /*------------------------------------------------------------------------.
406 | Complete the initialisation of a double step which just has been merged |
407 | into a single STEP. Establish known pairings by comparing UCS-2 values |
408 | between the before and after charsets. Create new pairs only when |
409 | fallback is recode_reversibility. |
410 `------------------------------------------------------------------------*/
411
412 static bool
413 complete_double_ucs2_step (RECODE_OUTER outer, RECODE_STEP step)
414 {
415 struct side
416 {
417 RECODE_SYMBOL charset; /* charset */
418 struct item item[256]; /* array of binding items */
419 size_t number_of_items; /* number of binding items in array */
420 };
421
422 const struct strip_data *data; /* UCS-2 data table */
423 struct side side_array[2]; /* information for each side */
424 struct side *side; /* cursor into side_array */
425 bool reversed; /* if both sides reversed */
426 const recode_ucs2 *pool; /* pool for fetching UCS-2 characters */
427 unsigned offset; /* cursor in double table strings */
428 unsigned byte; /* character code */
429 unsigned row_counter; /* double table row counter */
430 unsigned position_counter; /* double table column counter */
431 struct item *item_cursor; /* cursor in arrays of binding items */
432 struct item *left; /* left binding items cursor */
433 struct item *left_limit; /* limit value for left */
434 struct item *right; /* right binding items cursor */
435 struct item *right_limit; /* limit value for right */
436 struct recode_known_pair pair_array[256]; /* obtained pairings */
437 struct recode_known_pair *pair_cursor; /* cursor in array of pairings */
438
439 /* For ensuring recode_reversibility, known pairs should be computed the same
440 way regardless of the direction of recoding. This canonalisation is
441 ensured through the charset values, which are increasing along the
442 initialisation order. This should also reflect the charset order in
443 rfc1345.txt. */
444
445 if (step->before < step->after)
446 {
447 side_array[0].charset = step->before;
448 side_array[1].charset = step->after;
449 reversed = false;
450 }
451 else
452 {
453 side_array[0].charset = step->after;
454 side_array[1].charset = step->before;
455 reversed = true;
456 }
457
458 for (side = side_array; side < side_array + 2; side++)
459 {
460 /* Construct the array of binding items for the charset. */
461
462 data = (const struct strip_data *) side->charset->data;
463 pool = data->pool;
464 item_cursor = side->item;
465 byte = 0;
466
467 for (row_counter = 0;
468 row_counter < (256 / STRIP_SIZE);
469 row_counter++)
470 if (offset = data->offset[row_counter], offset)
471 for (position_counter = 0;
472 position_counter < STRIP_SIZE;
473 position_counter++)
474 {
475 unsigned code = pool[offset + position_counter];
476
477 if (code != BIT_MASK (16))
478 {
479 /* Establish a new binding item. */
480 item_cursor->byte = byte;
481 item_cursor->code = code;
482 item_cursor++;
483 }
484 byte++;
485 }
486 else
487 byte += STRIP_SIZE;
488
489 side->number_of_items = item_cursor - side->item;
490 }
491
492 /* Sort both arrays of binding items into lexicographical order. The
493 taken time, which is O(n.log(n)), is gained back when the further
494 pairing is completed in a time which is linear instead of quadratic. */
495
496 qsort (side_array[0].item, side_array[0].number_of_items,
497 sizeof (struct item), compare_struct_item);
498 qsort (side_array[1].item, side_array[1].number_of_items,
499 sizeof (struct item), compare_struct_item);
500
501 /* Scan both arrays of binding items simultaneously, saving as pairs
502 those codes having the same UCS-2 value. */
503
504 left = side_array[0].item;
505 left_limit = left + side_array[0].number_of_items;
506 right = side_array[1].item;
507 right_limit = right + side_array[1].number_of_items;
508 pair_cursor = pair_array;
509
510 while (left < left_limit && right < right_limit)
511 {
512 int value = left->code - right->code;
513
514 if (value < 0)
515 left++;
516 else if (value > 0)
517 right++;
518 else
519 {
520 pair_cursor->left = (left++)->byte;
521 pair_cursor->right = (right++)->byte;
522 pair_cursor++;
523 }
524 }
525
526 /* Complete the recoding table out of this. */
527
528 return
529 recode_complete_pairs (outer, step,
530 pair_array, pair_cursor - pair_array, false, reversed);
531 }
532
533 static bool
534 delete_compressed_one_to_many (RECODE_STEP step)
535 {
536 free (step->local);
537 return true;
538 }
539
540 static void
541 delete_step (RECODE_STEP step)
542 {
543 /* Run step destructor, and delete step_table if allocated. */
544 if (step->term_routine)
545 (*step->term_routine) (step);
546
547 if (step->step_table_term_routine != NULL)
548 (*step->step_table_term_routine) (step->step_table);
549 }
550
551 /*---------------------------------------------------------------------.
552 | Optimize a SEQUENCE of single steps by creating new single steps, if |
553 | this can be done by merging adjacent steps which are simple enough. |
554 `---------------------------------------------------------------------*/
555
556 static bool
557 simplify_sequence (RECODE_REQUEST request)
558 {
559 RECODE_OUTER outer = request->outer;
560
561 unsigned saved_steps; /* number of saved steps */
562 RECODE_STEP in; /* next studied sequence step */
563 RECODE_STEP out; /* next rewritten sequence step */
564 RECODE_STEP limit; /* last value for IN */
565 unsigned char *accum; /* byte_to_byte accumulated recoding */
566 const char **string; /* byte_to_variable recoding */
567 unsigned char temp[256]; /* temporary value for accum array */
568 unsigned counter; /* all purpose counter */
569
570 /* Tell users what is the goal. */
571
572 if (request->verbose_flag)
573 fprintf (stderr, _("Request: %s\n"), recode_edit_sequence (request, 0));
574
575 saved_steps = 0;
576
577 /* See if there are some double steps to merge. */
578
579 in = request->sequence_array;
580 out = request->sequence_array;
581 limit = in + request->sequence_length;
582
583 while (in < limit)
584 if (in < limit - 1
585 && in[0].before->data_type == RECODE_STRIP_DATA
586 && in[0].after == outer->ucs2_charset
587 && in[1].before == outer->ucs2_charset
588 && in[1].after->data_type == RECODE_STRIP_DATA)
589 {
590 /* Free old steps before overwriting anything. */
591 delete_step (in);
592 delete_step (in + 1);
593
594 /* This is a double UCS-2 step. */
595 out->before = in[0].before;
596 out->after = in[1].after;
597 out->quality = in[0].quality;
598 merge_qualities (&out->quality, in[1].quality);
599 out->transform_routine = recode_transform_byte_to_byte;
600
601 /* Initialize the new single step, so it can be later merged with
602 others. */
603 if (!complete_double_ucs2_step (outer, out))
604 return false;
605
606 in += 2;
607 saved_steps++;
608 out++;
609 }
610 else if (in < limit - 1
611 && in[0].after == outer->iconv_pivot
612 && in[1].before == outer->iconv_pivot)
613 {
614 /* Free old steps before overwriting anything. */
615 delete_step (in);
616 delete_step (in + 1);
617
618 /* This is a double `iconv' step. */
619 out->before = in[0].before;
620 out->after = in[1].after;
621 out->quality = in[0].quality;
622 merge_qualities (&out->quality, in[1].quality);
623 out->transform_routine = recode_transform_with_iconv;
624
625 in += 2;
626 saved_steps++;
627 out++;
628 }
629 else if (out != in)
630 *out++ = *in++;
631 else
632 out++, in++;
633
634 request->sequence_length = out - request->sequence_array;
635
636 /* Recopy the sequence array over itself, while merging subsequences of
637 one or more consecutive one-to-one recodings, including an optional
638 final one-to-many recoding. */
639
640 in = request->sequence_array;
641 out = request->sequence_array;
642 limit = in + request->sequence_length;
643
644 while (in < limit)
645 if (in < limit - 1
646 && table_type (request, in) == RECODE_BYTE_TO_BYTE
647 && table_type (request, in + 1) != RECODE_NO_STEP_TABLE
648
649 /* Initialise a cumulative one-to-one recoding with the identity
650 permutation. Just avoid doing it if not enough memory. */
651
652 && ALLOC (accum, 256, unsigned char))
653 {
654 memcpy (accum, in->step_table, 256);
655 out->before = in->before;
656 out->after = in->after;
657 out->quality = in->quality;
658 delete_step (in++);
659
660 /* Merge in all consecutive one-to-one recodings. */
661
662 while (in < limit
663 && (table_type (request, in) == RECODE_BYTE_TO_BYTE))
664 {
665 const unsigned char *table = (const unsigned char *) in->step_table;
666
667 for (counter = 0; counter < 256; counter++)
668 temp[counter] = table[accum[counter]];
669 memcpy (accum, temp, 256);
670
671 out->after = in->after;
672 merge_qualities (&out->quality, in->quality);
673 delete_step (in++);
674 saved_steps++;
675 }
676
677 /* Check for *one* possible one-to-many recoding. */
678
679 if (in < limit && (table_type (request, in) == RECODE_BYTE_TO_STRING)
680
681 /* Merge in the one-to-many recoding. Just avoid doing it if not
682 enough memory. */
683
684 && (ALLOC (string, 256, const char *)))
685 {
686 const char *const *table = (const char *const *) in->step_table;
687
688 for (counter = 0; counter < 256; counter++)
689 string[counter] = table[accum[counter]];
690 free (accum);
691 out->step_type = RECODE_BYTE_TO_STRING;
692 out->step_table = string;
693 if (in->step_table_term_routine)
694 {
695 out->local = (void *) table; /* Save reference to old table for destructor. */
696 out->term_routine = delete_compressed_one_to_many;
697 }
698 out->step_table_term_routine = free;
699 out->transform_routine = recode_transform_byte_to_variable;
700 out->after = in->after;
701 merge_qualities (&out->quality, in->quality);
702 in++;
703 saved_steps++;
704 }
705 else
706 {
707 /* Make the new single step be a one-to-one recoding. */
708
709 out->step_type = RECODE_BYTE_TO_BYTE;
710 out->step_table = accum;
711 out->transform_routine = recode_transform_byte_to_byte;
712 }
713
714 out++;
715 }
716 else if (out != in)
717 *out++ = *in++;
718 else
719 out++, in++;
720
721 request->sequence_length = out - request->sequence_array;
722
723 /* Delete a single remaining step, if it happens to be the identity
724 one-to-one recoding. */
725
726 in = request->sequence_array;
727
728 if (request->sequence_length == 1
729 && table_type (request, in) == RECODE_BYTE_TO_BYTE
730 && memcmp (in->step_table, outer->one_to_same, 256) == 0)
731 {
732 request->sequence_length = 0;
733 saved_steps++;
734 delete_step (in);
735 }
736
737 /* Tell the user if something changed. */
738
739 if (saved_steps > 0 && request->verbose_flag)
740 fprintf (stderr, _("Shrunk to: %s\n"), recode_edit_sequence (request, 0));
741 return true;
742 }
743
744 /* Decoding step sequence parameters. */
745
746 /*-------------------------------------.
747 | Scan next name into SCANNED_STRING. |
748 `-------------------------------------*/
749
750 static bool
751 scan_identifier (RECODE_REQUEST request)
752 {
753 char *cursor = request->scanned_string;
754
755 while (*request->scan_cursor && *request->scan_cursor != ','
756 && (request->scan_cursor[0] != '.' || request->scan_cursor[1] != '.')
757 && *request->scan_cursor != '/' && *request->scan_cursor != '+')
758 *cursor++ = *request->scan_cursor++;
759 *cursor = NUL;
760
761 return true;
762 }
763
764 /*----------------------------------------------------------------.
765 | Scan all options and return a list of them. |
766 | FIXME: Need to be able to differentiate empty list from error. |
767 `----------------------------------------------------------------*/
768
769 static RECODE_OPTION_LIST
770 scan_options (RECODE_REQUEST request)
771 {
772 RECODE_OUTER outer = request->outer;
773 RECODE_OPTION_LIST list = NULL;
774 RECODE_OPTION_LIST last = NULL;
775
776 while (*request->scan_cursor == '+')
777 {
778 RECODE_OPTION_LIST new_
779 = ALLOC (new_, 1, struct recode_option_list);
780 char *copy;
781
782 if (!new_)
783 break; /* FIXME: should interrupt decoding */
784
785 request->scan_cursor++;
786 scan_identifier (request);
787 ALLOC (copy, strlen (request->scanned_string) + 1, char);
788 if (!copy)
789 {
790 free (new_);
791 break; /* FIXME: should interrupt decoding */
792 }
793 strcpy (copy, request->scanned_string);
794
795 new_->option = copy;
796 if (!list)
797 list = new_;
798 new_->next = last;
799 last = new_;
800 }
801 return list;
802 }
803
804 /*--------------------------------------------------------------------------.
805 | See if there is another charset after the current one in the subrequest. |
806 | In such case, skip and ignore current surfaces, and return false. |
807 `--------------------------------------------------------------------------*/
808
809 static bool
810 scan_check_if_last_charset (RECODE_REQUEST request)
811 {
812 const char *cursor = request->scan_cursor;
813
814 while (*cursor && *cursor != ',' && !(cursor[0] == '.' && cursor[1] != '.'))
815 cursor++;
816
817 if (*cursor && *cursor != ',')
818 {
819 request->scan_cursor = cursor;
820 return false;
821 }
822
823 return true;
824 }
825
826 /*------------------------------------------------------------------------.
827 | Program, in the SEQUENCE under construction, the removal of surfaces in |
828 | reverse order of their appearance, using recursion. |
829 `------------------------------------------------------------------------*/
830
831 static bool
832 scan_unsurfacers (RECODE_REQUEST request)
833 {
834 RECODE_OUTER outer = request->outer;
835 RECODE_SYMBOL surface = NULL;
836 RECODE_OPTION_LIST surface_options = NULL;
837
838 request->scan_cursor++;
839 scan_identifier (request);
840 if (*request->scanned_string)
841 {
842 RECODE_ALIAS alias = recode_find_alias (outer, request->scanned_string,
843 ALIAS_FIND_AS_SURFACE);
844
845 if (!alias)
846 {
847 recode_error (outer, _("Unrecognised surface name `%s'"),
848 request->scanned_string);
849 return false;
850 }
851 surface = alias->symbol;
852 /* FIXME: Should check that it does not itself have implied surfaces? */
853 }
854 if (*request->scan_cursor == '+')
855 surface_options = scan_options (request);
856
857 if (*request->scan_cursor == '/')
858 if (!scan_unsurfacers (request))
859 return false;
860
861 if (surface && surface->unsurfacer)
862 return
863 add_to_sequence (request, surface->unsurfacer, surface_options, NULL);
864
865 return true;
866 }
867
868 /*------------------------------------------------------------------------.
869 | Program, in the SEQUENCE under construction, the removal of surfaces in |
870 | reverse order of the given list, using recursion. |
871 `------------------------------------------------------------------------*/
872
873 static bool
874 add_unsurfacers_to_sequence (RECODE_REQUEST request,
875 struct recode_surface_list *list)
876 {
877 if (list->next)
878 if (!add_unsurfacers_to_sequence (request, list->next))
879 return false;
880
881 if (list->surface->unsurfacer)
882 return
883 add_to_sequence (request, list->surface->unsurfacer, NULL, NULL);
884
885 return true;
886 }
887
888 /*---------------------------------------------------------------------------.
889 | Scan next surfaced charset of the form CHARSET[/SURFACE]*, and return this |
890 | charset after having added it to the SEQUENCE under construction. If |
891 | BEFORE is not NULL (BEFORE_OPTIONS then holds associated options), this |
892 | charset is in another position then the first, and then, a sequence of |
893 | steps will be generated. If BEFORE is NULL, then this charset is in a |
894 | before position (seen options will be returned at the position indicated |
895 | by OPTIONS_POINTER), surfaces should be removed from right to left instead |
896 | of applied from left to right. If neither in an initial position nor in a |
897 | final position, surfaces are ignored, that is optimised out. Tell what is |
898 | going on if VERBOSE. |
899 `---------------------------------------------------------------------------*/
900
901 static RECODE_SYMBOL
902 scan_charset_before (RECODE_REQUEST request, RECODE_OPTION_LIST *options_pointer)
903 {
904 RECODE_OUTER outer = request->outer;
905 RECODE_ALIAS alias;
906
907 *options_pointer = NULL;
908 scan_identifier (request);
909 alias = recode_find_alias (outer, request->scanned_string, ALIAS_FIND_AS_EITHER);
910 if (*request->scan_cursor == '+')
911 *options_pointer = scan_options (request);
912 if (!alias)
913 return NULL;
914
915 /* We are scanning in a BEFORE position. */
916
917 if (*request->scan_cursor == '/')
918 {
919 if (!scan_unsurfacers (request))
920 return NULL;
921 }
922 else if (alias->implied_surfaces && !request->make_header_flag)
923 {
924 if (!add_unsurfacers_to_sequence (request, alias->implied_surfaces))
925 return NULL;
926 }
927
928 return alias->symbol;
929 }
930
931 static RECODE_SYMBOL
932 scan_charset_after (RECODE_REQUEST request,
933 RECODE_CONST_SYMBOL before,
934 RECODE_CONST_OPTION_LIST before_options)
935 {
936 RECODE_OUTER outer = request->outer;
937 RECODE_ALIAS alias;
938 RECODE_SYMBOL charset;
939 RECODE_OPTION_LIST charset_options = NULL;
940
941 scan_identifier (request);
942 alias = recode_find_alias (outer, request->scanned_string, ALIAS_FIND_AS_EITHER);
943 if (*request->scan_cursor == '+')
944 charset_options = scan_options (request);
945 if (!alias)
946 return NULL;
947 charset = alias->symbol;
948
949 /* We are scanning in an AFTER position. */
950
951 if (!find_sequence (request, before, before_options,
952 charset, charset_options))
953 {
954 recode_error (outer, _("No way to recode from `%s' to `%s'"),
955 before->name, charset->name);
956 return NULL;
957 }
958
959 /* Ignore everything about surfaces, except in last position of a
960 subrequest. This optimises out the application of surfaces, when
961 these would be immediately followed by their removal. */
962
963 if (scan_check_if_last_charset (request))
964 {
965 if (*request->scan_cursor == '/')
966 {
967 while (*request->scan_cursor == '/')
968 {
969 RECODE_SYMBOL surface = NULL;
970 RECODE_OPTION_LIST surface_options = NULL;
971
972 request->scan_cursor++;
973 scan_identifier (request);
974 if (*request->scanned_string)
975 {
976 RECODE_ALIAS alias2
977 = recode_find_alias (outer, request->scanned_string,
978 ALIAS_FIND_AS_SURFACE);
979
980 if (!alias2)
981 {
982 recode_error (outer,
983 _("Unrecognised surface name `%s'"),
984 request->scanned_string);
985 return NULL;
986 }
987 surface = alias2->symbol;
988 /* FIXME: Should check that it does not itself have
989 implied surfaces? */
990 }
991 if (*request->scan_cursor == '+')
992 surface_options = scan_options (request);
993
994 if (surface && surface->resurfacer)
995 if (!add_to_sequence (request, surface->resurfacer,
996 NULL, surface_options))
997 return NULL;
998 }
999 }
1000 else if (alias->implied_surfaces && !request->make_header_flag)
1001 {
1002 struct recode_surface_list *list;
1003
1004 for (list = alias->implied_surfaces; list; list = list->next)
1005 if (list->surface->resurfacer)
1006 if (!add_to_sequence (request, list->surface->resurfacer,
1007 NULL, NULL))
1008 return NULL;
1009 }
1010 }
1011
1012 return charset;
1013 }
1014
1015 /*-------------------------------------------------------------------.
1016 | Into SEQUENCE, scan next request of the form CHARSET[..CHARSET]*. |
1017 `-------------------------------------------------------------------*/
1018
1019 static bool
1020 scan_request (RECODE_REQUEST request)
1021 {
1022 RECODE_OUTER outer = request->outer;
1023 RECODE_OPTION_LIST before_options;
1024 RECODE_SYMBOL charset = scan_charset_before (request, &before_options);
1025
1026 if (!charset)
1027 return false;
1028
1029 if (request->scan_cursor[0] == '.' && request->scan_cursor[1] == '.')
1030 while (request->scan_cursor[0] == '.' && request->scan_cursor[1] == '.')
1031 {
1032 request->scan_cursor += 2;
1033 charset = scan_charset_after (request, charset, before_options);
1034 if (!charset)
1035 return false;
1036 }
1037 else if (*request->scan_cursor == NUL)
1038 {
1039 /* No `..' at all implies a conversion to the default charset. */
1040 charset = scan_charset_after (request, charset, before_options);
1041 if (!charset)
1042 return false;
1043 }
1044 else
1045 {
1046 recode_error (outer, _("Expecting `..' in request"));
1047 return false;
1048 }
1049
1050 return true;
1051 }
1052
1053 /*-------------------------------------------------------------------------.
1054 | Establish a SEQUENCE of recoding steps described by the STRING argument, |
1055 | which should have the form: REQUEST[,REQUEST]*. |
1056 `-------------------------------------------------------------------------*/
1057
1058 static bool
1059 decode_request (RECODE_REQUEST request, const char *string)
1060 {
1061 RECODE_OUTER outer = request->outer;
1062
1063 request->scan_cursor = string;
1064 if (!ALLOC (request->scanned_string, strlen (string) + 1, char))
1065 return false;
1066 request->sequence_length = 0;
1067
1068 if (*request->scan_cursor)
1069 {
1070 if (!scan_request (request))
1071 {
1072 free (request->scanned_string);
1073 return false;
1074 }
1075 while (*request->scan_cursor == ',')
1076 {
1077 request->scan_cursor++;
1078 if (!scan_request (request))
1079 {
1080 free (request->scanned_string);
1081 return false;
1082 }
1083 }
1084 }
1085
1086 free (request->scanned_string);
1087 return true;
1088 }
1089
1090 /* Library interface. */
1091
1092 /* See the recode manual for a more detailed description of the library
1093 interface. */
1094
1095 /*--------------------------.
1096 | REQUEST level functions. |
1097 `--------------------------*/
1098
1099 /* Guarantee four NULs at the end of the output memory buffer for TASK, yet
1100 not counting them as data. (Four is the maximum, needed for UCS-4.) */
1101
1102 static bool
1103 guarantee_nul_terminator (RECODE_TASK task)
1104 {
1105 if (task->output.cursor + 4 >= task->output.limit)
1106 {
1107 RECODE_OUTER outer = task->request->outer;
1108 size_t size = task->output.cursor - task->output.buffer;
1109
1110 if (REALLOC (task->output.buffer, size + 4, char))
1111 {
1112 task->output.cursor = task->output.buffer + size;
1113 task->output.limit = task->output.buffer + size + 4;
1114 }
1115 else
1116 return false;
1117 }
1118 task->output.cursor[0] = NUL;
1119 task->output.cursor[1] = NUL;
1120 task->output.cursor[2] = NUL;
1121 task->output.cursor[3] = NUL;
1122
1123 return true;
1124 }
1125
1126 RECODE_REQUEST
1127 recode_new_request (RECODE_OUTER outer)
1128 {
1129 RECODE_REQUEST request;
1130
1131 if (!ALLOC (request, 1, struct recode_request))
1132 return NULL;
1133
1134 request->outer = outer;
1135 request->diaeresis_char = '"';
1136
1137 request->work_string_allocated = 0;
1138 return request;
1139 }
1140
1141 bool
1142 recode_delete_request (RECODE_REQUEST request)
1143 {
1144 for (RECODE_STEP step = request->sequence_array;
1145 step < request->sequence_array + request->sequence_length;
1146 step++)
1147 delete_step (step);
1148 free (request->sequence_array);
1149 free (request->work_string);
1150 free (request);
1151 return true;
1152 }
1153
1154 bool
1155 recode_scan_request (RECODE_REQUEST request, const char *string)
1156 {
1157 return
1158 decode_request (request, string)
1159 && simplify_sequence (request);
1160 }
1161
1162 char *
1163 recode_string (RECODE_CONST_REQUEST request, const char *input_string)
1164 {
1165 char *output_buffer = NULL;
1166 size_t output_length = 0;
1167 size_t output_allocated = 0;
1168
1169 recode_buffer_to_buffer (request, input_string, strlen (input_string),
1170 &output_buffer, &output_length, &output_allocated);
1171 return output_buffer;
1172 }
1173
1174 bool
1175 recode_string_to_buffer (RECODE_CONST_REQUEST request,
1176 const char *input_string,
1177 char **output_buffer_pointer,
1178 size_t *output_length_pointer,
1179 size_t *output_allocated_pointer)
1180 {
1181 return
1182 recode_buffer_to_buffer (request, input_string, strlen (input_string),
1183 output_buffer_pointer, output_length_pointer,
1184 output_allocated_pointer);
1185 }
1186
1187 bool
1188 recode_string_to_file (RECODE_CONST_REQUEST request,
1189 const char *input_string,
1190 FILE *output_file)
1191 {
1192 return
1193 recode_buffer_to_file (request, input_string, strlen (input_string),
1194 output_file);
1195 }
1196
1197 bool
1198 recode_buffer_to_buffer (RECODE_CONST_REQUEST request,
1199 const char *input_buffer,
1200 size_t input_length,
1201 char **output_buffer_pointer,
1202 size_t *output_length_pointer,
1203 size_t *output_allocated_pointer)
1204 {
1205 RECODE_TASK task = recode_new_task (request);
1206 bool success;
1207
1208 if (!task)
1209 return false;
1210
1211 task->input.buffer = input_buffer;
1212 task->input.cursor = input_buffer;
1213 task->input.limit = input_buffer + input_length;
1214 task->output.buffer = *output_buffer_pointer;
1215 task->output.cursor = *output_buffer_pointer;
1216 task->output.limit = *output_buffer_pointer + *output_allocated_pointer;
1217
1218 success = recode_perform_task (task) && guarantee_nul_terminator (task);
1219 *output_buffer_pointer = task->output.buffer;
1220 *output_length_pointer = task->output.cursor - task->output.buffer;
1221 *output_allocated_pointer = task->output.limit - task->output.buffer;
1222
1223 recode_delete_task (task);
1224 return success;
1225 }
1226
1227 bool
1228 recode_buffer_to_file (RECODE_CONST_REQUEST request,
1229 const char *input_buffer,
1230 size_t input_length,
1231 FILE *output_file)
1232 {
1233 RECODE_TASK task = recode_new_task (request);
1234 bool success;
1235
1236 if (!task)
1237 return false;
1238
1239 task->input.buffer = input_buffer;
1240 task->input.cursor = input_buffer;
1241 task->input.limit = input_buffer + input_length;
1242 task->output.file = output_file;
1243
1244 success = recode_perform_task (task);
1245
1246 recode_delete_task (task);
1247 return success;
1248 }
1249
1250 bool
1251 recode_file_to_buffer (RECODE_CONST_REQUEST request,
1252 FILE *input_file,
1253 char **output_buffer_pointer,
1254 size_t *output_length_pointer,
1255 size_t *output_allocated_pointer)
1256 {
1257 RECODE_TASK task = recode_new_task (request);
1258 bool success;
1259
1260 if (!task)
1261 return false;
1262
1263 task->input.file = input_file;
1264 task->output.buffer = *output_buffer_pointer;
1265 task->output.cursor = *output_buffer_pointer;
1266 task->output.limit = *output_buffer_pointer + *output_allocated_pointer;
1267
1268 success = recode_perform_task (task) && guarantee_nul_terminator (task);
1269 *output_buffer_pointer = task->output.buffer;
1270 *output_length_pointer = task->output.cursor - task->output.buffer;
1271 *output_allocated_pointer = task->output.limit - task->output.buffer;
1272
1273 recode_delete_task (task);
1274 return success;
1275 }
1276
1277 bool
1278 recode_file_to_file (RECODE_CONST_REQUEST request,
1279 FILE *input_file,
1280 FILE *output_file)
1281 {
1282 RECODE_TASK task = recode_new_task (request);
1283 bool success;
1284
1285 if (!task)
1286 return false;
1287
1288 task->input.file = input_file;
1289 task->output.file = output_file;
1290
1291 success = recode_perform_task (task);
1292
1293 recode_delete_task (task);
1294 return success;
1295 }