File: charset.c

package info (click to toggle)
recode 3.4.1-11
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,560 kB
  • ctags: 622
  • sloc: ansic: 10,572; perl: 339; makefile: 317; lisp: 243; sh: 173; lex: 165; awk: 127; sed: 10
file content (765 lines) | stat: -rw-r--r-- 19,369 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/* Conversion of files between different charsets and usages.
   Copyright (C) 1993, 1994 Free Software Foundation, Inc.
   Francois Pinard <pinard@iro.umontreal.ca>, 1993.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "recode.h"

/* Maximum number of charset values.  */
#define MAX_CHARSETS 200

/* Hash table size for charset names.  */
#define HASH_TABLE_SIZE 997

/* Maximum number of characters per 10646 symbol.  */
#define MAX_SYMBOL_SIZE 9

/* Known pairs (for restricting listing).  */

static struct known_pair *pair_restriction = NULL;
static int pair_restrictions = 0;

/* Known character sets.  */

struct hash
  {
    const char *name;		/* charset or alias name, or NULL */
    CHARSET *charset;		/* associated charset */
    struct hash *next;		/* next index in table, or NULL */
  };
struct hash hash_table[HASH_TABLE_SIZE];
CHARSET charset_array[MAX_CHARSETS];
int number_of_charsets;

/* Array of strings ready for argmatch.  */
static const char **argmatch_array;

/* Character names.  */

/* This module takes care only of short 10646 forms.  Module charname.c
   takes care of the full descriptive name for characters.  */

/*--------------------------------------------------------------------.
| Return a statically allocated 10646 symbol in a CHARSET for a given |
| CODE, or NULL if this symbol is not defined.  There are two static  |
| buffers used in alternance.					      |
`--------------------------------------------------------------------*/

static char *
code_to_symbol (CHARSET *charset, int code)
{
  static char buffer[2][MAX_SYMBOL_SIZE + 1];
  static int which = 0;
  const char *in;
  char *out;
  int counter;

  if (in = (*charset->table)[code / 32], !in)
    return NULL;

  in += charset->size * (code % 32);
  if (*in == ' ')
    return NULL;

  which = !which;
  out = buffer[which];
  for (counter = 0; counter < charset->size; counter++)
    if (*in == ' ')
      in++;
    else
      *out++ = *in++;
  *out = '\0'; 
  return buffer[which];
}

/*------------------------------------------------------------------------.
| Print a 10646 symbol in a CHARSET for a given CODE, padding with spaces |
| after to the proper width.						  |
`------------------------------------------------------------------------*/

static void
print_symbol (CHARSET *charset, int code)
{
  int counter;
  char *cursor;

  counter = 0;
  cursor = code_to_symbol (charset, code);
  
  if (cursor)
    for (; *cursor && counter < charset->size; counter++)
      {
	putchar (*cursor);
	cursor++;
      }
  for (; counter < charset->size; counter++)
    putchar (' ');
}

/*-----------------------------------------------------------------.
| Decode a known PAIRS argument, given in STRING, constructing the |
| pair_restriction array out of it.				   |
`-----------------------------------------------------------------*/

void
decode_known_pairs (const char *string)
{
  struct known_pair pair;
  const char *cursor;
  int value;

  pair_restriction = (struct known_pair *)
    xmalloc (16 * sizeof (struct known_pair));

  value = -1;
  for (cursor = string; *cursor; cursor++)
    switch (*cursor)
      {
      default:
	usage (EXIT_FAILURE);

      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
	if (value < 0)
	  value = *cursor - '0';
	else
	  value = 10 * value + *cursor - '0';
	break;

      case ':':
	if (value < 0 || value > 255)
	  usage (EXIT_FAILURE);
	pair_restriction[pair_restrictions].left = (unsigned char) value;
	value = -1;
	break;

      case ',':
	if (value < 0 || value > 255)
	  usage (EXIT_FAILURE);
	pair_restriction[pair_restrictions++].right = (unsigned char) value;
	if (pair_restrictions % 16 == 0)
	  pair_restriction = (struct known_pair *)
	    xrealloc (pair_restriction,
		      (pair_restrictions + 16) * sizeof (struct known_pair));
	value = -1;
	break;
      }

  if (value < 0 || value > 255)
    usage (EXIT_FAILURE);
  pair_restriction[pair_restrictions++].right = (unsigned char) value;
}

/*-------------------------------------------------------------.
| Return non-zero if BEFORE to AFTER is currently restricted.  |
`-------------------------------------------------------------*/

static int
check_restricted (CHARSET *before, CHARSET *after)
{
  struct known_pair *pair;
  const char *left;
  const char *right;

  /* Reject the charset if not RFC1345.  */

  if (!before->table || !after->table)
    return 1;

  for (pair = pair_restriction;
       pair < pair_restriction + pair_restrictions;
       pair++)
    {

      /* Reject the charset if the characters in the pair do not exist of
	 if their respective definition do not match.  */

      left = code_to_symbol (before, pair->left);
      if (!left)
	return 1;
      right = code_to_symbol (after, pair->right);
      if (!right)
	return 1;
      if (strcmp (left, right))
	return 1;
    }

  /* No restriction found.  */

  return 0;
}

/* Charset names.  */

/*--------------------------------------.
| Prepare charsets for initialization.  |
`--------------------------------------*/

void
prepare_charset_initialization (void)
{
  int counter;

  for (counter = 0; counter < HASH_TABLE_SIZE; counter++)
    {
      hash_table[counter].name = NULL;
      hash_table[counter].next = NULL;
    }
  number_of_charsets = 0;
}

/*-----------------------------------------------------------------------.
| Return a newly allocated copy of charset NAME, with upper case letters |
| turned into lower case, and all non alphanumeric discarded.		 |
`-----------------------------------------------------------------------*/

static char *
cleanup_charset_name (const char *name)
{
  char *result;
  char *out;
  const char *in;
  int character;

  result = xmalloc (strlen (name) + 1);
  out = result;
  for (in = name; *in; in++)
    {
      character = *(const unsigned char *) in;
      if ((character >= 'a' && character <= 'z')
	  || (character >= '0' && character <= '9'))
	*out++ = character;
      else if (character >= 'A' && character <= 'Z')
	*out++ = character - 'A' + 'a';
    }
  *out = '\0';
  return result;
}

/*-----------------------------------.
| Return a hash index for a STRING.  |
`-----------------------------------*/

#if DIFF_HASH

/* Given a hash value and a new character, return a new hash value.  */

#define UINT_BIT (sizeof (unsigned) * CHAR_BIT)
#define ROTATE_LEFT(v, n) ((v) << (n) | (v) >> (UINT_BIT - (n)))
#define HASH(h, c) ((c) + ROTATE_LEFT (h, 7))

static int
hash_string (const char *string)
{
  unsigned value;

  value = 0;
  for (; *string; string++)
    value = HASH (value, *(const unsigned char *) string);
  return value % HASH_TABLE_SIZE;
}

#else /* not DIFF_HASH */

static int
hash_string (const char *string)
{
  unsigned value;

  value = 0;
  for (; *string; string++)
    value = ((value * 31 + (int) *(const unsigned char *) string)
	     % HASH_TABLE_SIZE);
  return value;
}

#endif /* not DIFF_HASH */

/*--------------------------------------------------------------------------.
| Return the charset from its NAME or alias name.  If it does not already   |
| exist, add a new charset entry and initialize it with a brand new value.  |
`--------------------------------------------------------------------------*/

CHARSET *
find_charset (const char *name)
{
  char *hashname;
  struct hash *hash;
  CHARSET *charset;

  /* Search the whole hash bucket and return any match.  */

  hashname = cleanup_charset_name (name);
  for (hash = hash_table + hash_string (hashname);
       hash->name;
       hash = hash->next)
    {
      if (strcmp (hashname, hash->name) == 0)
	{
	  free (hashname);
	  return hash->charset;
	}
      if (!hash->next)
	break;
    }

  /* A new charset has to be created.  */

  if (number_of_charsets == MAX_CHARSETS)
    error (EXIT_FAILURE, 0, _("MAX_CHARSETS is too small"));

  charset = charset_array + number_of_charsets++;

  /* If the current slot is already used, create an overflow entry and
     initialize it enough so it could be taken for the current slot.  */

  if (hash->name)
    {
      hash->next = (struct hash *) xmalloc (sizeof (struct hash));
      hash = hash->next;
      hash->next = NULL;
    }

  /* Initialize the current slot with the new charset.  */

  hash->name = hashname;
  hash->charset = charset;

  charset->name = name;
  charset->ignore = 0;
  charset->table = NULL;

  return charset;
}

/*-------------------------------------------------------------------------.
| Have NAME as an alternate charset name for OLD_NAME.  Create OLD_NAME if |
| it does not exist already.						   |
`-------------------------------------------------------------------------*/

void
declare_alias (const char *name, const char *old_name)
{
  char *hashname;
  struct hash *hash;
  CHARSET *old_charset;

  /* Find the old value.  */

  old_charset = find_charset (old_name);

  /* Search the whole hash bucket.  */

  hashname = cleanup_charset_name (name);
  for (hash = hash_table + hash_string (hashname);
       hash->name;
       hash = hash->next)
    {
      if (strcmp (hashname, hash->name) == 0)
	{
	  if (hash->charset != old_charset)
	    error (EXIT_FAILURE, 0,
		   _("Charset %s already exists and is not %s"),
		   name, old_name);
	  free (hashname);
	  return;
	}
      if (!hash->next)
	break;
    }

  /* If the current slot is already used, create an overflow entry and
     initialize it enough so it could be taken for the current slot.  */

  if (hash->name)
    {
      hash->next = (struct hash *) xmalloc (sizeof (struct hash));
      hash = hash->next;
      hash->next = NULL;
    }

  /* Initialize the current slot with the old charset.  */

  hash->name = hashname;
  hash->charset = old_charset;
}

/*------------------------------------------.
| Construct the string array for argmatch.  |
`------------------------------------------*/

void
make_argmatch_array (void)
{
  struct hash *hash;		/* cursor in charsets */
  int number;			/* number of strings */
  int counter;			/* all purpose counter */
#if HASH_STATS
  int buckets;			/* number of non-empty buckets */
#endif

  /* Count how many strings we need.  */

  number = 0;
  for (counter = 0; counter < HASH_TABLE_SIZE; counter++)
    for (hash = hash_table + counter;
	 hash && hash->name;
	 hash = hash->next)
      number++;

#if HASH_STATS
  buckets = 0;
  for (counter = 0; counter < HASH_TABLE_SIZE; counter++)
    if (hash_table[counter].name)
      buckets++;

  fprintf (stderr, _("Hash stats: %d names using %d buckets out of %d\n"),
	   number, buckets, HASH_TABLE_SIZE);
#endif

  /* Allocate the argmatch array, with place for a NULL sentinel.  */

  argmatch_array
    = (const char **) xmalloc ((number + 1) * sizeof (const char *));

  /* Fill in the array.  */

  number = 0;
  for (counter = 0; counter < HASH_TABLE_SIZE; counter++)
    for (hash = hash_table + counter;
	 hash && hash->name;
	 hash = hash->next)
      argmatch_array[number++] = hash->name;

  argmatch_array[number] = NULL;
}

/*-----------------------------------------------------------------------.
| Return the NAME of a charset, un-abbreviated and cleaned up.  Diagnose |
| and abort if this cannot be done successfully.  A NULL or empty string |
| means the default charset, if this default charset is defined.	 |
`-----------------------------------------------------------------------*/

const char *
clean_charset_name (const char *name)
{
  char *hashname;
  int ordinal;

  /* Look for a match.  */

  if (!name)
    name = "";
#ifdef DEFAULT_CHARSET
  if (!*name)
    name = DEFAULT_CHARSET;
#endif
  hashname = cleanup_charset_name (name);
  ordinal = argmatch (hashname, argmatch_array);
  free (hashname);

  /* Diagnose any match error, notifying usage that we are decoding
     charsets.  */

  switch (ordinal)
    {
    case -2:
      error (0, 0, _("Ambiguous charset `%s'"), name);
      decoding_charset_flag = 1;
      usage (EXIT_FAILURE);

    case -1:
      error (0, 0, _("Unknown charset `%s'"), name);
      decoding_charset_flag = 1;
      usage (EXIT_FAILURE);
    }

  return argmatch_array[ordinal];
}

/*----------------------------------------------------------------------.
| Order two struct hash's, using the true charset name as the first key |
| and the current name as the second key.			        |
`----------------------------------------------------------------------*/

static int
compare_struct_hash (const void *void_first, const void *void_second)
{
  int value;

  value = strcmp (((const struct hash *) void_first)->charset->name,
		  ((const struct hash *) void_second)->charset->name);
  if (value != 0)
    return value;
  
  value = strcmp (((const struct hash *) void_first)->name,
		  ((const struct hash *) void_second)->name);
  return value;
}

/*-----------------------------------------------------------------------.
| List all available charsets, obeying restrictions for an AFTER charset |
| if any.								 |
`-----------------------------------------------------------------------*/

void
list_all_charsets (CHARSET *after)
{
  struct hash *array;
  struct hash *hash;
  int number;
  int counter;
  int list_flag;

  /* Count how many charsets we have.  */

  number = 0;
  for (counter = 0; counter < HASH_TABLE_SIZE; counter++)
    for (hash = hash_table + counter;
	 hash && hash->name;
	 hash = hash->next)
      number++;

  /* Allocate a structure to hold them.  */

  array = (struct hash *) xmalloc (number * sizeof (struct hash));

  /* Copy all charsets in it.  */

  number = 0;
  for (counter = 0; counter < HASH_TABLE_SIZE; counter++)
    for (hash = hash_table + counter;
	 hash && hash->name;
	 hash = hash->next)
      array[number++] = *hash;

  /* Sort it.  */

  qsort (array, number, sizeof (struct hash), compare_struct_hash);

  /* Print it, one line per charset, giving the true charset name first,
     followed by all its alias in lexicographic order.  */

  for (hash = array; hash < array + number; hash++)
    {

      /* Begin a new line with the true charset name when it changes.  */

      if (hash == array || hash->charset->name != (hash - 1)->charset->name)
	{
	  if (list_flag && hash != array)
	    printf ("\n");

	  list_flag = !after || !check_restricted (hash->charset, after);

	  if (list_flag)
	    printf ("%s", hash->charset->name);
	}

      /* Print the charset name or alias in its cleaned up form.  */

      if (list_flag)
	printf (" %s", hash->name);
    }
  if (list_flag)
    printf ("\n");

  /* Release the work array.  */

  free (array);
}

/* Charset contents.  */

/*-----------------------------------------------------------------------.
| For a given STEP recoding into a RFC 1345 charset, produce an explicit |
| recoding table.							 |
`-----------------------------------------------------------------------*/

void
init_table_for_rfc1345 (STEP *step)
{
  const char *symbol;
  char *pool;
  const char **table;
  int length;
  int counter;

  /* First compute how much memory is needed.  */

  length = 0;
  for (counter = 0; counter < 256; counter++)
    {
      symbol = code_to_symbol (step->before, counter);
      if (symbol)
	length += strlen (symbol) + 1;
    }

  /* Do the actual allocation and filling.  */

  table = (const char **) xmalloc (256 * sizeof (char *) + length);
  pool = (char *) (table + 256);

  for (counter = 0; counter < 256; counter++)
    {
      symbol = code_to_symbol (step->before, counter);
      if (symbol)
	{
	  if (strcmp (symbol, "SP") == 0)
	    symbol = " ";
	  else if (strcmp (symbol, "LF") == 0)
	    symbol = "\n";

	  table[counter] = pool;
	  /* FIXME: pool is loosing the const specification of symbol.  */
	  while (*pool++ = *symbol++)
	    ;
	}
      else
	table[counter] = NULL;
    }

  step->one_to_many = table;
}

/*------------------------------------------------------------------.
| Print a concise, tabular CHARSET description on standard output.  |
`------------------------------------------------------------------*/

void
list_concise_charset (CHARSET *charset)
{
  DOUBLE_TABLE *table;		/* double table */
  int half;			/* half 0, half 1 of the table */
  const char *format;		/* format string */
  int counter;			/* code counter */
  int counter2;			/* code counter */
  int code;			/* code value */

  /* Ensure we have a double table to play with.  */

  if (charset->table)
    table = charset->table;
  else
    error (EXIT_FAILURE, 0,
	   _("Cannot list `%s', no 10646 names available for this charset"),
	   charset->name);

  printf ("%s\n", charset->name);

  /* Select format for numbers.  */

  switch (list_format)
    {
    default:
      return;			/* cannot happen */

    case NO_FORMAT:
    case DECIMAL_FORMAT:
      format = "%3d";
      break;

    case OCTAL_FORMAT:
      format = "%0.3o";
      break;

    case HEXADECIMAL_FORMAT:
      format = "%0.2x";
      break;
    }

  /* Print both halves of the table.  */

  for (half = 0; half < 2; half++)
    {

      /* Skip printing this half if it is empty.  */

      for (counter = 4 * half; counter < 4 * half + 4; counter++)
	if ((*table)[counter])
	  break;
      if (counter == 4 * half + 4)
	continue;

      /* Print this half.  */

      printf ("\n");
      for (counter = 128 * half; counter < 128 * half + 16; counter++)
	for (counter2 = 0; counter2 < 128; counter2 += 16)
	  {
	    if (counter2 > 0)
	      printf ("  ");

	    code = counter + counter2;
	    printf (format, code);
	    printf (" ");
	    print_symbol (charset, code);

	    if (counter2 == 112)
	      printf ("\n");
	  }
    }
}

/*------------------------------------------------------.
| Print a full CHARSET description on standard output.  |
`------------------------------------------------------*/

void
list_full_charset (CHARSET *charset)
{
  int insert_white;		/* insert a while line before printing */
  int code;			/* code counter */
  const char *symbol;		/* symbol for code */
  const char *charname;		/* charname for code */

  /* Ensure we have a double table to play with.  */

  if (!charset->table)
    error (EXIT_FAILURE, 0,
	   _("Sorry, no 10646 names available for `%s'"), charset->name);

  /* Print the long table.  */

  printf (_("dec  oct hex    ch   %s\n"), charset->name);
  insert_white = 1;

  for (code = 0; code < 256; code++)
    if ((symbol = code_to_symbol (charset, code)), symbol)
      {
	if (insert_white)
	  {
	    printf ("\n");
	    insert_white = 0;
	  }
	printf ("%3d  %0.3o  %0.2x    ", code, code, code);
	print_symbol (charset, code);
	if ((charname = symbol_to_charname (symbol)), charname)
	  printf ("   %s", charname);
	printf ("\n");
      }
    else
      insert_white = 1;
}