summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/luafontloader/fontforge/Unicode/dump.c
blob: 5b0796e2d55760696f91cad18f2e37de60dc86a2 (plain)
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/* Copyright (C) 2000-2008 by George Williams */
/*
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.

 * The name of the author may not be used to endorse or promote products
 * derived from this software without specific prior written permission.

 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../inc/charset.h"
#include "../inc/basics.h"

char *alphabets[] = { "8859-1.TXT", "8859-2.TXT", "8859-3.TXT", "8859-4.TXT",
    "8859-5.TXT", "8859-6.TXT", "8859-7.TXT", "8859-8.TXT", "8859-9.TXT",
    "8859-10.TXT", "8859-11.TXT", "8859-13.TXT", "8859-14.TXT", "8859-15.TXT",
    "koi8r.TXT", "JIS0201.txt", "WIN.TXT", "MacRoman.TXT",
    "MacSYMBOL.TXT", "zapfding.TXT", /*"MacCYRILLIC.TXT",*/ NULL };
char *alnames[] = { "i8859_1", "i8859_2", "i8859_3", "i8859_4",
    "i8859_5", "i8859_6", "i8859_7", "i8859_8", "i8859_9",
    "i8859_10", "i8859_11", "i8859_13", "i8859_14", "i8859_15",
    "koi8_r", "jis201", "win", "mac",
    "MacSymbol", "ZapfDingbats", /*"MacCyrillic",*/ NULL };
int almaps[] = { em_iso8859_1, em_iso8859_2, em_iso8859_3, em_iso8859_4,
    em_iso8859_5, em_iso8859_6, em_iso8859_7, em_iso8859_8, em_iso8859_9,
    em_iso8859_10, em_iso8859_11, em_iso8859_13, em_iso8859_14, em_iso8859_15,
    em_koi8_r, em_jis201, em_win, em_mac, em_symbol, em_zapfding,
    -1 };


char *cjk[] = { "JIS0208.TXT", "JIS0212.TXT", "BIG5.TXT", "GB2312.TXT",
	"HANGUL.TXT", "Big5HKSCS.txt", NULL };
/* I'm only paying attention to Wansung encoding (in HANGUL.TXT) which is 94x94 */
/* I used to look at OLD5601, but that maps to Unicode 1.0, and Hangul's moved*/
char *adobecjk[] = { "aj16cid2code.txt", "aj20cid2code.txt", "ac15cid2code.txt",
	"ag15cid2code.txt", "ak12cid2code.txt", NULL };
/* I'm told that most of the mappings provided on the Unicode site go to */
/*  Unicode 1.* and that CJK have been moved radically since. So instead */
/*  of the unicode site's files, try using adobe's which claim they are */
/*  up to date. These may be found in: */
/*	ftp://ftp.ora.com/pub/examples/nutshell/ujip/adobe/samples/{aj14,aj20,ak12,ac13,ag14}/cid2code.txt */
/* they may be bundled up in a tar file, I forget exactly... */
char *cjknames[] = { "jis208", "jis212", "big5", "gb2312", "ksc5601", "big5hkscs", NULL };
int cjkmaps[] = { em_jis208, em_jis212, em_big5, em_gb2312, em_ksc5601, em_big5hkscs };

unsigned long *used[256];

static void dumpalphas(FILE *output, FILE *header) {
    FILE *file;
    int i,j,k, first, last;
    long _orig, _unicode, mask;
    unichar_t unicode[256];
    unsigned char *table[256], *plane;
    char buffer[200];

    fprintf(output, "#include <chardata.h>\n\n" );
    fprintf(output, "const unsigned char c_allzeros[256] = { 0 };\n\n" );

    for ( k=0; k<256; ++k ) table[k] = NULL;

    for ( j=0; alphabets[j]!=NULL; ++j ) {
	file = fopen( alphabets[j], "r" );
	if ( file==NULL ) {
	    fprintf( stderr, "Can't open %s\n", alphabets[j]);
	} else {
	    for ( i=0; i<160; ++i )
		unicode[i] = i;
	    for ( ; i<256; ++i )
		unicode[i] = 0;
	    while ( fgets(buffer,sizeof(buffer),file)!=NULL ) {
		if ( buffer[0]=='#' )
	    continue;
		sscanf(buffer, "0x%lx 0x%lx", (unsigned long *) &_orig, (unsigned long *) &_unicode);
		unicode[_orig] = _unicode;
		if ( table[_unicode>>8]==NULL ) {
		    plane = table[_unicode>>8] = calloc(256,1);
		    if ( j==0 && (_unicode>>8)==0 )
			for ( k=0; k<256; ++k )
			    plane[k] = k;
		    else if ( j==0 )
			for ( k=0; k<128; ++k )
			    plane[k] = k;
		}
		table[_unicode>>8][_unicode&0xff] = _orig;
		if ( used[_unicode>>8]==NULL ) {
		    used[_unicode>>8] = calloc(256,sizeof(long));
		}
		if ( almaps[j]!=-1 )
		    used[_unicode>>8][_unicode&0xff] |= (1<<almaps[j]);
	    }
	    fclose(file);

	    fprintf( header, "extern const unichar_t unicode_from_%s[];\n", alnames[j] );
	    fprintf( output, "const unichar_t unicode_from_%s[] = {\n", alnames[j] );
	    for ( i=0; i<256-8; i+=8 )
		fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
			unicode[i], unicode[i+1], unicode[i+2], unicode[i+3],
			unicode[i+4], unicode[i+5], unicode[i+6], unicode[i+7]);
	    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
		    unicode[i], unicode[i+1], unicode[i+2], unicode[i+3],
		    unicode[i+4], unicode[i+5], unicode[i+6], unicode[i+7]);

	    first = last = -1;
	    for ( k=0; k<256; ++k ) {
		if ( table[k]!=NULL ) {
		    if ( first==-1 ) first = k;
		    last = k;
		    plane = table[k];
		    fprintf( output, "static const unsigned char %s_from_unicode_%x[] = {\n", alnames[j], k );
		    for ( i=0; i<256-16; i+=16 )
			fprintf( output, "  0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x,\n",
				plane[i], plane[i+1], plane[i+2], plane[i+3],
				plane[i+4], plane[i+5], plane[i+6], plane[i+7],
				plane[i+8], plane[i+9], plane[i+10], plane[i+11],
				plane[i+12], plane[i+13], plane[i+14], plane[i+15]);
		    fprintf( output, "  0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x\n};\n\n",
				plane[i], plane[i+1], plane[i+2], plane[i+3],
				plane[i+4], plane[i+5], plane[i+6], plane[i+7],
				plane[i+8], plane[i+9], plane[i+10], plane[i+11],
				plane[i+12], plane[i+13], plane[i+14], plane[i+15]);
		}
	    }
	    fprintf( output, "static const unsigned char * const %s_from_unicode_[] = {\n", alnames[j] );
	    for ( k=first; k<=last; ++k )
		if ( table[k]!=NULL )
		    fprintf( output, "    %s_from_unicode_%x%s", alnames[j], k, k!=last?",\n":"\n" );
		else
		    fprintf( output, "    c_allzeros,\n" );
	    fprintf( output, "};\n\n" );
	    fprintf( header, "extern struct charmap %s_from_unicode;\n", alnames[j]);
	    fprintf( output, "struct charmap %s_from_unicode = { %d, %d, (unsigned char **) %s_from_unicode_, (unichar_t *) unicode_from_%s };\n\n",
		    alnames[j], first, last, alnames[j], alnames[j]);

	    for ( k=first; k<=last; ++k ) {
		free(table[k]);
		table[k]=NULL;
	    }
	}
    }

/*	Mac Symbol appears as a font even on unix.  Cyrillic does not but so what?
    for ( j=0; alphabets[j]!=NULL; ++j )
	if ( strcmp(alphabets[j],"MacSYMBOL.TXT")==0 ) alphabets[j]=NULL;
*/

    fprintf( header, "\nextern unichar_t *unicode_from_alphabets[];\n" );
#if 0
    fprintf( output, "\n/* the windows charset is a superset of latin1.  Many PC centric users think */\n" );
    fprintf( output, "/*  IT is the standard charset for the web and try to use &#153; for &#2122; */\n" );
    fprintf( output, "/* so even if we expect latin1, let's check for windows too, can't hurt. */\n" );
    fprintf( output, "unichar_t *unicode_from_alphabets[]={\n" );
    fprintf( output, "    unicode_from_win, 0,0, unicode_from_win, \n" );
#else
    fprintf( output, "unichar_t *unicode_from_alphabets[]={\n" );
    fprintf( output, "    (unichar_t *) unicode_from_win, 0,0,\n    (unichar_t *) unicode_from_i8859_1, \n" );
#endif
    for ( j=1; alphabets[j]!=NULL; ++j )
	fprintf( output, "    (unichar_t *) unicode_from_%s,\n", alnames[j] );
    fprintf( output, "    (unichar_t *) unicode_from_%s,\t/* Place holder for user-defined map */\n", alnames[0] );
    fprintf( output, "    0\n" );
    fprintf( output, "};\n" );
    fprintf( header, "extern struct charmap *alphabets_from_unicode[];\n" );
    fprintf( output, "\nstruct charmap *alphabets_from_unicode[]={ 0,0,0,\n" );
    for ( j=0; alphabets[j]!=NULL; ++j )
	fprintf( output, "    &%s_from_unicode,\n", alnames[j] );
    fprintf( output, "    &%s_from_unicode,\t/* Place holder for user-defined map*/\n", alnames[0] );
    fprintf( output, "    0\n" );
    fprintf( output, "};\n" );
	
    fprintf( header, "\n" );

    for ( i=0; i<=255; ++i )
	used[0][i] |= 1;		/* Map this plane entirely to 8859-1 */
    mask = 0;
    for ( j=0; alphabets[j]!=NULL; ++j )
	if ( almaps[j]!=-1 )
	    mask |= (1<<almaps[j]);
    for ( i=0; i<' '; ++i )
	used[0][i] |= mask;
}

#if 0
static char base64[64] = {
 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};

static unsigned char nigori[48] = {
    0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
static unsigned char maru[48] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

static void dumprandom(FILE *output,FILE *header) {
    int inbase64[128], i;

    for ( i=0; i<128; ++i )
	inbase64[i] = -1;
    for ( i=0; i<64; ++i )
	inbase64[base64[i]] = i;
    fprintf( header, "extern signed char inbase64[128];\n" );
    fprintf( output, "signed char inbase64[128] = {\n" );
    for ( i=0; i<128; i+= 16 )
	fprintf( output, "  %2d, %2d, %2d, %2d, %2d, %2d, %2d, %2d, %2d, %2d, %2d, %2d, %2d, %2d, %2d, %2d%s\n",
		inbase64[i], inbase64[i+1], inbase64[i+2], inbase64[i+3],
		inbase64[i+4], inbase64[i+5], inbase64[i+6], inbase64[i+7],
		inbase64[i+8], inbase64[i+9], inbase64[i+10], inbase64[i+11],
		inbase64[i+12], inbase64[i+13], inbase64[i+14], inbase64[i+15],
		i==128-16?"":",");
    fprintf( output, "};\n" );

    fprintf( header, "/* Need to subtract 0xb0 from jis206 before indexing this array */\n" );
    fprintf( header, "extern char nigori[48];\n" );
    fprintf( output, "char nigori[48] = {\n" );
    for ( i=0; i<48; i+= 16 )
	fprintf( output, "  %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d%s\n",
		nigori[i], nigori[i+1], nigori[i+2], nigori[i+3],
		nigori[i+4], nigori[i+5], nigori[i+6], nigori[i+7],
		nigori[i+8], nigori[i+9], nigori[i+10], nigori[i+11],
		nigori[i+12], nigori[i+13], nigori[i+14], nigori[i+15],
		i==48-16?"":",");
    fprintf( output, "};\n" );

    fprintf( header, "/* Need to subtract 0xb0 from jis206 before indexing this array */\n" );
    fprintf( header, "extern char maru[48];\n" );
    fprintf( output, "char maru[48] = {\n" );
    for ( i=0; i<48; i+= 16 )
	fprintf( output, "  %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d%s\n",
		maru[i], maru[i+1], maru[i+2], maru[i+3],
		maru[i+4], maru[i+5], maru[i+6], maru[i+7],
		maru[i+8], maru[i+9], maru[i+10], maru[i+11],
		maru[i+12], maru[i+13], maru[i+14], maru[i+15],
		i==48-16?"":",");
    fprintf( output, "};\n" );

    fprintf( header, "\n" );
}
#endif

#define VERTMARK 0x1000000

static int ucs2_score(int val) {		/* Supplied by KANOU Hiroki */
    if ( val>=0x2e80 && val<=0x2fff )
return( 1 );				/* New CJK Radicals are least important */
    else if ( val>=VERTMARK )
return( 0 );				/* Then vertical guys */
			    /* only we can't handle vertical here */
    else if ( val>=0xf000 && val<=0xffff )
return( 3 );
/*    else if (( val>=0x3400 && val<0x3dff ) || (val>=0x4000 && val<=0x4dff))*/
    else if ( val>=0x3400 && val<=0x4dff )
return( 4 );
    else
return( 5 );
}

static int getnth(char *buffer, int col) {
    int i, val=0, best;
    char *end;
    int vals[10];

    if ( col==1 ) {
	/* first column is decimal, others are hex */
	if ( !isdigit(*buffer))
return( -1 );
	while ( isdigit(*buffer))
	    val = 10*val + *buffer++-'0';
return( val );
    }
    for ( i=1; i<col; ++buffer ) {
	if ( *buffer=='\t' )
	    ++i;
	else if ( *buffer=='\0' )
return( -1 );
    }
    val = strtol(buffer,&end,16);
    if ( end==buffer )
return( -1 );
    if ( *end=='v' ) {
	val += VERTMARK;
	++end;
    }
    if ( *end==',' ) {
	/* Multiple guess... now we've got to pick one */
	vals[0] = val;
	i = 1;
	while ( *end==',' && i<9 ) {
	    buffer = end+1;
	    vals[i] = strtol(buffer,&end,16);
	    if ( *end=='v' ) {
		vals[i] += VERTMARK;
		++end;
	    }
	    ++i;
	}
	vals[i] = 0;
	best = 0; val = -1;
	for ( i=0; vals[i]!=0; ++i ) {
	    if ( ucs2_score(vals[i])>best ) {
		val = vals[i];
		best = ucs2_score(vals[i]);
	    }
	}
    }

    if ( val >= VERTMARK )
return( -1 );

return( val );
}

static void dumpjis(FILE *output,FILE *header) {
    FILE *file;
    int i,j,k, first, last;
    long _orig, _unicode;
    unichar_t unicode208[94*94], unicode212[94*94];
    unichar_t *table[256], *plane;
    char buffer[400];

    memset(table,0,sizeof(table));

    j=0;
    file = fopen( adobecjk[j], "r" );
    if ( file==NULL ) {
	fprintf( stderr, "Can't open %s\n", adobecjk[j]);
    } else {
	memset(unicode208,0,sizeof(unicode208));
	while ( fgets(buffer,sizeof(buffer),file)!=NULL ) {
	    if ( buffer[0]=='#' )
	continue;
	    _orig = getnth(buffer,2);
	    if ( _orig==-1 )
	continue;
	    _unicode = getnth(buffer,22);
	    if ( _unicode==-1 ) {
		fprintf( stderr, "Eh? JIS 208-1997 %lx is unencoded\n", _orig );
	continue;
	    }
	    if ( _unicode>0xffff ) {
		fprintf( stderr, "Eh? JIS 208-1997 %lx is outside of BMP\n", _orig );
	continue;
	    }
	    if ( table[_unicode>>8]==NULL )
		table[_unicode>>8] = calloc(256,sizeof(unichar_t));
	    table[_unicode>>8][_unicode&0xff] = _orig;
	    _orig -= 0x2121;
	    _orig = (_orig>>8)*94 + (_orig&0xff);
	    if ( _orig>=94*94 )
		fprintf( stderr, "Attempt to index with %ld\n", _orig );
	    else {
		unicode208[_orig] = _unicode;
		if ( used[_unicode>>8]==NULL ) {
		    used[_unicode>>8] = calloc(256,sizeof(long));
		}
		used[_unicode>>8][_unicode&0xff] |= (1<<em_jis208);
	    }
	}
	fclose(file);
    }

    j=1;
    file = fopen( adobecjk[j], "r" );
    if ( file==NULL ) {
	fprintf( stderr, "Can't open %s\n", adobecjk[j]);
    } else {
	memset(unicode212,0,sizeof(unicode212));
	while ( fgets(buffer,sizeof(buffer),file)!=NULL ) {
	    if ( buffer[0]=='#' )
	continue;
	    _orig = getnth(buffer,2);
	    if ( _orig==-1 )
	continue;
	    _unicode = getnth(buffer,7);
	    if ( _unicode==-1 ) {
		fprintf( stderr, "Eh? JIS 212-1990 %lx is unencoded\n", _orig );
	continue;
	    }
	    if ( _unicode>0xffff ) {
		fprintf( stderr, "Eh? JIS 212-1990 %lx is out of BMP U+%lx\n", _orig, _unicode );
	continue;
	    }
	    if ( table[_unicode>>8]==NULL )
		table[_unicode>>8] = calloc(256,sizeof(unichar_t));
	    if ( table[_unicode>>8][_unicode&0xff]==0 )
		table[_unicode>>8][_unicode&0xff] = _orig|0x8000;
	    else
		fprintf( stderr, "JIS clash at JIS212 %lx, unicode %lx\n", _orig, _unicode );	/* there are said to be a few of these, I'll just always map to 208 */
	    _orig -= 0x2121;
	    _orig = (_orig>>8)*94 + (_orig&0xff);
	    if ( _orig>=94*94 )
		fprintf( stderr, "Attempt to index JIS212 with %ld\n", _orig );
	    else {
		unicode212[_orig] = _unicode;
		if ( used[_unicode>>8]==NULL ) {
		    used[_unicode>>8] = calloc(256,sizeof(long));
		}
		used[_unicode>>8][_unicode&0xff] |= (1<<em_jis212);
	    }
	}
	fclose(file);
    }

    j=0;
    fprintf( header, "extern const unichar_t unicode_from_%s[];\n", cjknames[j] );
    fprintf( output, "const unichar_t unicode_from_%s[] = {\n", cjknames[j] );
    for ( i=0; i<sizeof(unicode208)/sizeof(unicode208[0]); i+=8 )
	fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
		unicode208[i], unicode208[i+1], unicode208[i+2], unicode208[i+3],
		unicode208[i+4], unicode208[i+5], unicode208[i+6], unicode208[i+7]);
    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
	    unicode208[i], unicode208[i+1], unicode208[i+2], unicode208[i+3],
	    unicode208[i+4], unicode208[i+5], unicode208[i+6], unicode208[i+7]);
    j=1;
    fprintf( header, "extern const unichar_t unicode_from_%s[];\n", cjknames[j] );
    fprintf( output, "const unichar_t unicode_from_%s[] = {\n", cjknames[j] );
    for ( i=0; i<sizeof(unicode212)/sizeof(unicode212[0]); i+=8 )
	fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
		unicode212[i], unicode212[i+1], unicode212[i+2], unicode212[i+3],
		unicode212[i+4], unicode212[i+5], unicode212[i+6], unicode212[i+7]);
    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
	    unicode212[i], unicode212[i+1], unicode212[i+2], unicode212[i+3],
	    unicode212[i+4], unicode212[i+5], unicode212[i+6], unicode212[i+7]);

    first = last = -1;
    for ( k=0; k<256; ++k ) {
	if ( table[k]!=NULL ) {
	    if ( first==-1 ) first = k;
	    last = k;
	    plane = table[k];
	    fprintf( output, "static const unsigned short jis_from_unicode_%x[] = {\n", k );
	    for ( i=0; i<256-8; i+=8 )
		fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
			plane[i], plane[i+1], plane[i+2], plane[i+3],
			plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
	    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
			plane[i], plane[i+1], plane[i+2], plane[i+3],
			plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
	}
    }
    fprintf( output, "static const unsigned short * const jis_from_unicode_[] = {\n" );
    for ( k=first; k<=last; ++k )
	if ( table[k]!=NULL )
	    fprintf( output, "    jis_from_unicode_%x%s", k, k!=last?",\n":"\n" );
	else
	    fprintf( output, "    u_allzeros,\n" );
    fprintf( output, "};\n\n" );
    fprintf( header, "extern struct charmap2 jis_from_unicode;\n" );
    fprintf( output, "struct charmap2 jis_from_unicode = { %d, %d, (unsigned short **) jis_from_unicode_, (unichar_t *) unicode_from_%s };\n\n",
	    first, last, cjknames[j]);

    for ( k=first; k<=last; ++k )
	free(table[k]);
}

static void dumpbig5(FILE *output,FILE *header) {
    FILE *file;
    int i,j,k, first, last;
    long _orig, _unicode;
    unichar_t unicode[0x6000];
    unichar_t *table[256], *plane;
    char buffer[400];

    j = 2;

    memset(table,0,sizeof(table));

    file = fopen( adobecjk[j], "r" );
    if ( file==NULL ) {
	fprintf( stderr, "Can't open %s\n", adobecjk[j]);
    } else {
	memset(unicode,0,sizeof(unicode));
	while ( fgets(buffer,sizeof(buffer),file)!=NULL ) {
	    if ( buffer[0]=='#' )
	continue;
	    _orig = getnth(buffer,3);
	    if ( /*_orig==-1*/ _orig<0xa100 )
	continue;
	    _unicode = getnth(buffer,11);
	    if ( _unicode==-1 ) {
		if ( _orig==0xa1c3 )
		    _unicode = 0xFFE3;
		else if ( _orig==0xa1c5 )
		    _unicode = 0x2cd;
		else if ( _orig==0xa1fe )
		    _unicode = 0xff0f;
		else if ( _orig==0xa240 )
		    _unicode = 0xff3c;
		else if ( _orig==0xa2cc )
		    _unicode = 0x5341;
		else if ( _orig==0xa2ce )
		    _unicode = 0x5345;
		else if ( _orig==0xa15a )
		    _unicode = 0x2574;
	    }
	    if ( _unicode==-1 ) {
		fprintf( stderr, "Eh? BIG5 %lx is unencoded\n", _orig );
	continue;
	    }
	    if ( _unicode>0xffff ) {
		fprintf( stderr, "Eh? BIG5 %lx is out of BMP U+%lx\n", _orig, _unicode );
	continue;
	    }
	    unicode[_orig-0xa100] = _unicode;
	    if ( table[_unicode>>8]==NULL )
		table[_unicode>>8] = calloc(256,sizeof(unichar_t));
	    table[_unicode>>8][_unicode&0xff] = _orig;
	    if ( used[_unicode>>8]==NULL ) {
		used[_unicode>>8] = calloc(256,sizeof(long));
	    }
	    used[_unicode>>8][_unicode&0xff] |= (1<<em_big5);
	}
	fclose(file);

	fprintf( header, "/* Subtract 0xa100 before indexing this array */\n" );
	fprintf( header, "extern const unichar_t unicode_from_%s[];\n", cjknames[j] );
	fprintf( output, "const unichar_t unicode_from_%s[] = {\n", cjknames[j] );
	for ( i=0; i<sizeof(unicode)/sizeof(unicode[0]); i+=8 )
	    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
		    unicode[i], unicode[i+1], unicode[i+2], unicode[i+3],
		    unicode[i+4], unicode[i+5], unicode[i+6], unicode[i+7]);
	fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
		unicode[i], unicode[i+1], unicode[i+2], unicode[i+3],
		unicode[i+4], unicode[i+5], unicode[i+6], unicode[i+7]);

	first = last = -1;
	for ( k=0; k<256; ++k ) {
	    if ( table[k]!=NULL ) {
		if ( first==-1 ) first = k;
		last = k;
		plane = table[k];
		fprintf( output, "static const unsigned short %s_from_unicode_%x[] = {\n", cjknames[j], k );
		for ( i=0; i<256-8; i+=8 )
		    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
			    plane[i], plane[i+1], plane[i+2], plane[i+3],
			    plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
		fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
			    plane[i], plane[i+1], plane[i+2], plane[i+3],
			    plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
	    }
	}
	fprintf( output, "static const unsigned short * const %s_from_unicode_[] = {\n", cjknames[j] );
	for ( k=first; k<=last; ++k )
	    if ( table[k]!=NULL )
		fprintf( output, "    %s_from_unicode_%x%s", cjknames[j], k, k!=last?",\n":"\n" );
	    else
		fprintf( output, "    u_allzeros,\n" );
	fprintf( output, "};\n\n" );
	fprintf( header, "extern struct charmap2 %s_from_unicode;\n", cjknames[j]);
	fprintf( output, "struct charmap2 %s_from_unicode = { %d, %d, (unsigned short **) %s_from_unicode_, (unichar_t *) unicode_from_%s };\n\n",
		cjknames[j], first, last, cjknames[j], cjknames[j]);

	for ( k=first; k<=last; ++k )
	    free(table[k]);
    }
}

static void dumpbig5hkscs(FILE *output,FILE *header) {
    FILE *file;
    int i,j,k, first, last;
    long _orig, _unicode;
    unichar_t unicode[0x8000];
    unichar_t *table[256], *plane;
    char buffer[400];

    j=5;

    memset(table,0,sizeof(table));

    file = fopen( cjk[j], "r" );
    if ( file==NULL ) {
	fprintf( stderr, "Can't open %s\n", cjk[j] );
    } else {
	memset(unicode,0,sizeof(unicode));
	while ( fgets(buffer,sizeof(buffer),file)!=NULL ) {
	    if ( buffer[0]=='#' )
	continue;
	    if ( sscanf( buffer, "U+%lx: %lx", (unsigned long *) &_unicode, (unsigned long *) &_orig )!=2 )
	continue;
	    if ( _orig<0x8140 )
	continue;
	    if ( _unicode==-1 ) {
		fprintf( stderr, "Eh? BIG5HKSCS %lx is unencoded\n", _orig );
	continue;
	    }
	    unicode[_orig-0x8100] = _unicode;
	    if ( table[_unicode>>8]==NULL )
		table[_unicode>>8] = calloc(256,sizeof(unichar_t));
	    table[_unicode>>8][_unicode&0xff] = _orig;
	    if ( used[_unicode>>8]==NULL ) {
		used[_unicode>>8] = calloc(256,sizeof(long));
	    }
	    used[_unicode>>8][_unicode&0xff] |= (1<<em_big5hkscs);
	}
	fclose(file);

	fprintf( header, "/* Subtract 0x8100 before indexing this array */\n" );
	fprintf( header, "extern const unichar_t unicode_from_%s[];\n", cjknames[j] );
	fprintf( output, "const unichar_t unicode_from_%s[] = {\n", cjknames[j] );
	for ( i=0; i<sizeof(unicode)/sizeof(unicode[0]); i+=8 )
	    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
		    unicode[i], unicode[i+1], unicode[i+2], unicode[i+3],
		    unicode[i+4], unicode[i+5], unicode[i+6], unicode[i+7]);
	fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
		unicode[i], unicode[i+1], unicode[i+2], unicode[i+3],
		unicode[i+4], unicode[i+5], unicode[i+6], unicode[i+7]);

	first = last = -1;
	for ( k=0; k<256; ++k ) {
	    if ( table[k]!=NULL ) {
		if ( first==-1 ) first = k;
		last = k;
		plane = table[k];
		fprintf( output, "static const unsigned short %s_from_unicode_%x[] = {\n", cjknames[j], k );
		for ( i=0; i<256-8; i+=8 )
		    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
			    plane[i], plane[i+1], plane[i+2], plane[i+3],
			    plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
		fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
			    plane[i], plane[i+1], plane[i+2], plane[i+3],
			    plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
	    }
	}
	fprintf( output, "static const unsigned short * const %s_from_unicode_[] = {\n", cjknames[j] );
	for ( k=first; k<=last; ++k )
	    if ( table[k]!=NULL )
		fprintf( output, "    %s_from_unicode_%x%s", cjknames[j], k, k!=last?",\n":"\n" );
	    else
		fprintf( output, "    u_allzeros,\n" );
	fprintf( output, "};\n\n" );
	fprintf( header, "extern struct charmap2 %s_from_unicode;\n", cjknames[j]);
	fprintf( output, "struct charmap2 %s_from_unicode = { %d, %d, (unsigned short **) %s_from_unicode_, (unichar_t *) unicode_from_%s };\n\n",
		cjknames[j], first, last, cjknames[j], cjknames[j]);

	for ( k=first; k<=last; ++k )
	    free(table[k]);
    }
}

static void dumpWansung(FILE *output,FILE *header) {
    FILE *file;
    int i,j=4,k, first, last;
    long _orig, _unicode, _johab;
    unichar_t unicode[94*94], junicode[0x7c00];
    unichar_t *table[256], *plane, *jtable[256];
    char buffer[400];
    /* Johab high=[0x84-0xf9] low=[0x31-0xfe] */

	memset(table,0,sizeof(table));
	memset(jtable,0,sizeof(jtable));

	file = fopen( adobecjk[j], "r" );
	if ( file==NULL ) {
	    fprintf( stderr, "Can't open %s\n", adobecjk[j]);
	} else {
	    memset(unicode,0,sizeof(unicode));
	    memset(junicode,0,sizeof(junicode));
	    while ( fgets(buffer,sizeof(buffer),file)!=NULL ) {
		if ( buffer[0]=='#' )
	    continue;
		_johab = getnth(buffer,7);
		_orig = getnth(buffer,2);
		_unicode = getnth(buffer,11);
		if ( _unicode==-1 ) {
		    if ( _orig>=0x2121 && (_orig&0xff)>=0x21 && _orig<=0x7e7e && (_orig&0xff)<=0x7e )
			fprintf( stderr, "Eh? Wansung %lx is unencoded\n", _orig );
		    else if ( _johab>=0x8431 && _johab<=0xf9fe )
			fprintf( stderr, "Eh? Johab %lx is unencoded\n", _johab );
	    continue;
		}
		if ( _unicode>0xffff ) {
		    if ( _orig>=0x2121 && (_orig&0xff)>=0x21 && _orig<=0x7e7e && (_orig&0xff)<=0x7e )
			fprintf( stderr, "Eh? Wansung %lx is out of BMP U+%lx\n", _orig, _unicode );
		    else if ( _johab>=0x8431 && _johab<=0xf9fe )
			fprintf( stderr, "Eh? Johab %lx is out of BMP U+%lx\n", _johab, _unicode );
	    continue;
		}
		if ( _orig>=0x2121 && (_orig&0xff)>=0x21 && _orig<=0x7e7e && (_orig&0xff)<=0x7e ) {
		    if ( table[_unicode>>8]==NULL )
			table[_unicode>>8] = calloc(256,sizeof(unichar_t));
		    table[_unicode>>8][_unicode&0xff] = _orig;
		    _orig -= 0x2121;
		    _orig = (_orig>>8)*94 + (_orig&0xff);
		    if ( _orig>=94*94 ) {
			fprintf( stderr, "Not 94x94\n" );
	    continue;
		    }
		    unicode[_orig] = _unicode;
		    if ( used[_unicode>>8]==NULL ) {
			used[_unicode>>8] = calloc(256,sizeof(long));
		    }
		    used[_unicode>>8][_unicode&0xff] |= (1<<cjkmaps[j]);
		}
		if ( _johab>=0x8431 && _johab<=0xf9fe ) {
		    if ( jtable[_unicode>>8]==NULL )
			jtable[_unicode>>8] = calloc(256,sizeof(unichar_t));
		    jtable[_unicode>>8][_unicode&0xff] = _johab;
		    _johab -= 0x8400;
		    junicode[_johab] = _unicode;
		    if ( used[_unicode>>8]==NULL ) {
			used[_unicode>>8] = calloc(256,sizeof(long));
		    }
		    used[_unicode>>8][_unicode&0xff] |= (1<<em_johab);
		}
	    }
	    fclose(file);
	}

	/* First Wansung */
	fprintf( header, "extern const unichar_t unicode_from_%s[];\n", cjknames[j] );
	fprintf( output, "const unichar_t unicode_from_%s[] = {\n", cjknames[j] );
	for ( i=0; i<sizeof(unicode)/sizeof(unicode[0]); i+=8 )
	    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
		    unicode[i], unicode[i+1], unicode[i+2], unicode[i+3],
		    unicode[i+4], unicode[i+5], unicode[i+6], unicode[i+7]);
	fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
		unicode[i], unicode[i+1], unicode[i+2], unicode[i+3],
		unicode[i+4], unicode[i+5], unicode[i+6], unicode[i+7]);

	first = last = -1;
	for ( k=0; k<256; ++k ) {
	    if ( table[k]!=NULL ) {
		if ( first==-1 ) first = k;
		last = k;
		plane = table[k];
		fprintf( output, "static unsigned short %s_from_unicode_%x[] = {\n", cjknames[j], k );
		for ( i=0; i<256-8; i+=8 )
		    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
			    plane[i], plane[i+1], plane[i+2], plane[i+3],
			    plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
		fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
			    plane[i], plane[i+1], plane[i+2], plane[i+3],
			    plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
	    }
	}
	fprintf( output, "static const unsigned short * const %s_from_unicode_[] = {\n", cjknames[j] );
	for ( k=first; k<=last; ++k )
	    if ( table[k]!=NULL )
		fprintf( output, "    %s_from_unicode_%x%s", cjknames[j], k, k!=last?",\n":"\n" );
	    else
		fprintf( output, "    u_allzeros,\n" );
	fprintf( output, "};\n\n" );
	fprintf( header, "extern struct charmap2 %s_from_unicode;\n", cjknames[j]);
	fprintf( output, "struct charmap2 %s_from_unicode = { %d, %d, (unsigned short **) %s_from_unicode_, (unichar_t *) unicode_from_%s };\n\n",
		cjknames[j], first, last, cjknames[j], cjknames[j]);

	if ( first==-1 )
	    fprintf( stderr, "No Hangul\n" );
	else for ( k=first; k<=last; ++k ) {
	    free(table[k]);
	    table[k]=NULL;
	}

	/* Then Johab */
	fprintf( header, "/* Subtract 0x8400 before indexing this array */\n" );
	fprintf( header, "extern const unichar_t unicode_from_johab[];\n" );
	fprintf( output, "const unichar_t unicode_from_johab[] = {\n" );
	for ( i=0; i<sizeof(junicode)/sizeof(junicode[0]); i+=8 )
	    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
		    junicode[i], junicode[i+1], junicode[i+2], junicode[i+3],
		    junicode[i+4], junicode[i+5], junicode[i+6], junicode[i+7]);
	fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
		junicode[i], junicode[i+1], junicode[i+2], junicode[i+3],
		junicode[i+4], junicode[i+5], junicode[i+6], junicode[i+7]);

	first = last = -1;
	for ( k=0; k<256; ++k ) {
	    if ( jtable[k]!=NULL ) {
		if ( first==-1 ) first = k;
		last = k;
		plane = jtable[k];
		fprintf( output, "static unsigned short johab_from_unicode_%x[] = {\n", k );
		for ( i=0; i<256-8; i+=8 )
		    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
			    plane[i], plane[i+1], plane[i+2], plane[i+3],
			    plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
		fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
			    plane[i], plane[i+1], plane[i+2], plane[i+3],
			    plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
	    }
	}
	fprintf( output, "static const unsigned short * const johab_from_unicode_[] = {\n" );
	for ( k=first; k<=last; ++k )
	    if ( jtable[k]!=NULL )
		fprintf( output, "    johab_from_unicode_%x%s", k, k!=last?",\n":"\n" );
	    else
		fprintf( output, "    u_allzeros,\n" );
	fprintf( output, "};\n\n" );
	fprintf( header, "extern struct charmap2 johab_from_unicode;\n" );
	fprintf( output, "struct charmap2 johab_from_unicode = { %d, %d, (unsigned short **) johab_from_unicode_, (unichar_t *) unicode_from_johab };\n\n",
		first, last );

	if ( first==-1 )
	    fprintf( stderr, "No Johab\n" );
	else for ( k=first; k<=last; ++k ) {
	    free(table[k]);
	    table[k]=NULL;
	}
}

static void dumpgb2312(FILE *output,FILE *header) {
    FILE *file;
    int i,j,k, first, last;
    long _orig, _unicode;
    unichar_t unicode[94*94];
    unichar_t *table[256], *plane;
    char buffer[400];

    memset(table,0,sizeof(table));

    j = 3;
	file = fopen( adobecjk[j], "r" );
	if ( file==NULL ) {
	    fprintf( stderr, "Can't open %s\n", adobecjk[j]);
	} else {
	    memset(unicode,0,sizeof(unicode));
	    while ( fgets(buffer,sizeof(buffer),file)!=NULL ) {
		if ( buffer[0]=='#' )
	    continue;
		_orig = getnth(buffer,2);
		if ( _orig==-1 )
	    continue;
		_unicode = getnth(buffer,14);
		if ( _unicode==-1 ) {
		    fprintf( stderr, "Eh? GB2312-80 %lx is unencoded\n", _orig );
	    continue;
		}
		if ( _unicode>0xffff ) {
		    fprintf( stderr, "Eh? GB2312-80 %lx is out of BMP U+%lx\n", _orig, _unicode );
	    continue;
		}
		if ( table[_unicode>>8]==NULL )
		    table[_unicode>>8] = calloc(256,sizeof(unichar_t));
		table[_unicode>>8][_unicode&0xff] = _orig;
		_orig -= 0x2121;
		_orig = (_orig>>8)*94 + (_orig&0xff);
		unicode[_orig] = _unicode;
		if ( used[_unicode>>8]==NULL ) {
		    used[_unicode>>8] = calloc(256,sizeof(long));
		}
		used[_unicode>>8][_unicode&0xff] |= (1<<cjkmaps[j]);
	    }
	    fclose(file);
	}

	fprintf( header, "extern const unichar_t unicode_from_%s[];\n", cjknames[j] );
	fprintf( output, "const unichar_t unicode_from_%s[] = {\n", cjknames[j] );
	for ( i=0; i<sizeof(unicode)/sizeof(unicode[0]); i+=8 )
	    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
		    unicode[i], unicode[i+1], unicode[i+2], unicode[i+3],
		    unicode[i+4], unicode[i+5], unicode[i+6], unicode[i+7]);
	fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
		unicode[i], unicode[i+1], unicode[i+2], unicode[i+3],
		unicode[i+4], unicode[i+5], unicode[i+6], unicode[i+7]);

	first = last = -1;
	for ( k=0; k<256; ++k ) {
	    if ( table[k]!=NULL ) {
		if ( first==-1 ) first = k;
		last = k;
		plane = table[k];
		fprintf( output, "static unsigned short %s_from_unicode_%x[] = {\n", cjknames[j], k );
		for ( i=0; i<256-8; i+=8 )
		    fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n",
			    plane[i], plane[i+1], plane[i+2], plane[i+3],
			    plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
		fprintf( output, "  0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n};\n\n",
			    plane[i], plane[i+1], plane[i+2], plane[i+3],
			    plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
	    }
	}
	fprintf( output, "static const unsigned short * const %s_from_unicode_[] = {\n", cjknames[j] );
	for ( k=first; k<=last; ++k )
	    if ( table[k]!=NULL )
		fprintf( output, "    %s_from_unicode_%x%s", cjknames[j], k, k!=last?",\n":"\n" );
	    else
		fprintf( output, "    u_allzeros,\n" );
	fprintf( output, "};\n\n" );
	fprintf( header, "extern struct charmap2 %s_from_unicode;\n", cjknames[j]);
	fprintf( output, "struct charmap2 %s_from_unicode = { %d, %d, (unsigned short **) %s_from_unicode_, (unichar_t *) unicode_from_%s };\n\n",
		cjknames[j], first, last, cjknames[j], cjknames[j]);

	if ( first==-1 )
	    fprintf( stderr, "No 94x94\n" );
	else for ( k=first; k<=last; ++k ) {
	    free(table[k]);
	    table[k]=NULL;
	}
}

static void dumpcjks(FILE *output,FILE *header) {

    fprintf(output, "#include <chardata.h>\n\n" );
    fprintf(output, "const unsigned short u_allzeros[256] = { 0 };\n\n" );

    dumpjis(output,header);
    dumpbig5(output,header);
    dumpbig5hkscs(output,header);
    dumpWansung(output,header);
    dumpgb2312(output,header);
}

static void dumptrans(FILE *output, FILE *header) {
    unsigned long *plane;
    int k, i;

    fprintf(output, "static const unsigned long l_allzeros[256] = { 0 };\n" );
    for ( k=0; k<256; ++k ) {
	if ( used[k]!=NULL ) {
	    plane = used[k];
	    fprintf( output, "static const unsigned long unicode_backtrans_%x[] = {\n", k );
	    for ( i=0; i<256-8; i+=8 )
		fprintf( output, "  0x%06lx, 0x%06lx, 0x%06lx, 0x%06lx, 0x%06lx, 0x%06lx, 0x%06lx, 0x%06lx,\n",
			plane[i], plane[i+1], plane[i+2], plane[i+3],
			plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
	    fprintf( output, "  0x%06lx, 0x%06lx, 0x%06lx, 0x%06lx, 0x%06lx, 0x%06lx, 0x%06lx, 0x%06lx\n};\n\n",
			plane[i], plane[i+1], plane[i+2], plane[i+3],
			plane[i+4], plane[i+5], plane[i+6], plane[i+7]);
	}
    }
    fprintf( header, "\n/* a mask for each character saying what charset(s) it may be found in */\n" );
    fprintf( header, "extern const unsigned long * const unicode_backtrans[];\n" );
    fprintf( output, "const unsigned long *const unicode_backtrans[] = {\n" );
    for ( k=0; k<256; ++k )
	if ( used[k]!=NULL )
	    fprintf( output, "    unicode_backtrans_%x%s", k, k!=255?",\n":"\n" );
	else
	    fprintf( output, "    l_allzeros,\n" );
    fprintf( output, "};\n" );
}

int main(int argc, char **argv) {
    FILE *output, *header;

    if (( output = fopen( "alphabet.c", "w" ))==NULL ) {
	fprintf( stderr, "Can't open %s\n", "alphabet.c" );
return 1;
    }
    if (( header = fopen( "chardata.h", "w" ))==NULL ) {
	fprintf( stderr, "Can't open %s\n", "chardata.h" );
return 1;
    }

    fprintf( header, "#include \"basics.h\"\n\n" );
    fprintf( header, "struct charmap {\n    int first, last;\n    unsigned char **table;\n    unichar_t *totable;\n};\n" );
    fprintf( header, "struct charmap2 {\n    int first, last;\n    unsigned short **table;\n    unichar_t *totable;\n};\n\n" );

    dumpalphas(output,header);
    /*dumprandom(output,header);*/
    fclose(output);

    if (( output = fopen( "cjk.c", "w" ))==NULL ) {
	fprintf( stderr, "Can't open %s\n", "cjk.c" );
return 1;
    }
    dumpcjks(output,header);
    if (( output = fopen( "backtrns.c", "w" ))==NULL ) {
	fprintf( stderr, "Can't open %s\n", "cjk.c" );
return 1;
    }
    dumptrans(output,header);

    /* This really should be in make ctype, but putting it there causes all */
    /*  sorts of build problems in things happen out of order */
    fprintf( header,"\nextern const unichar_t *const * const unicode_alternates[];\n" );

    fclose(output); fclose(header);
return 0;
}