forked from ThomasDickey/luit-snapshots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluitconv.c
1521 lines (1369 loc) · 36.2 KB
/
luitconv.c
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
995
996
997
998
999
1000
/* $XTermId: luitconv.c,v 1.123 2020/10/03 14:23:42 tom Exp $ */
/*
Copyright 2010-2019,2020 by Thomas E. Dickey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <iso2022.h>
#include <sys.h>
#ifdef HAVE_LANGINFO_CODESET
#include <locale.h>
#include <langinfo.h>
#endif
#ifndef ICONV_CONST
#define ICONV_CONST /* nothing */
#endif
/*
* This uses a similar approach to vile's support for wide/narrow locales.
*
* We use the iconv library to construct a mapping forward for each byte,
* into UTF-8, and then use that mapping to construct the reverse mapping
* from UTF-8 into the original set of single byte values.
*/
#define MAX8 0x100
#define MAX16 0x10000
#define NO_ICONV (iconv_t)(-1)
static LuitConv *all_conversions;
/******************************************************************************/
static int
ConvToUTF32(unsigned *target, const char *source, size_t limit)
{
#define CH(n) (UCHAR)((*target) >> ((n) * 8))
int rc = 0;
int j;
UINT mask = 0;
/*
* Find the number of bytes we will need from the source.
*/
if ((*source & 0x80) == 0) {
rc = 1;
mask = (UINT) * source;
} else if ((*source & 0xe0) == 0xc0) {
rc = 2;
mask = (UINT) (*source & 0x1f);
} else if ((*source & 0xf0) == 0xe0) {
rc = 3;
mask = (UINT) (*source & 0x0f);
} else if ((*source & 0xf8) == 0xf0) {
rc = 4;
mask = (UINT) (*source & 0x07);
} else if ((*source & 0xfc) == 0xf8) {
rc = 5;
mask = (UINT) (*source & 0x03);
} else if ((*source & 0xfe) == 0xfc) {
rc = 6;
mask = (UINT) (*source & 0x01);
}
if ((size_t) rc > limit) { /* whatever it is, we cannot decode it */
TRACE2(("limit failed %d/%ld in vl_conv_to_utf32\n", rc, limit));
rc = 0;
}
/*
* sanity-check.
*/
if (rc > 1) {
for (j = 1; j < rc; j++) {
if ((source[j] & 0xc0) != 0x80)
break;
}
if (j != rc) {
TRACE2(("check failed %d/%d in vl_conv_to_utf32\n", j, rc));
rc = 0;
}
}
if (target != 0) {
int shift = 0;
*target = 0;
for (j = 1; j < rc; j++) {
*target |= (UINT) (source[rc - j] & 0x3f) << shift;
shift += 6;
}
*target |= mask << shift;
TRACE2(("encode %2d:%.*s -> %#08x %02X.%02X.%02X.%02X\n",
rc, rc, source,
*target,
CH(3), CH(2), CH(1), CH(0)));
}
return rc;
#undef CH
}
static int
ConvToUTF8(UCHAR * target, UINT source, size_t limit)
{
#define CH(n) (UCHAR)((source) >> ((n) * 8))
int rc = 0;
if (source <= 0x0000007f)
rc = 1;
else if (source <= 0x000007ff)
rc = 2;
else if (source <= 0x0000ffff)
rc = 3;
else if (source <= 0x001fffff)
rc = 4;
else if (source <= 0x03ffffff)
rc = 5;
else /* (source <= 0x7fffffff) */
rc = 6;
if ((size_t) rc > limit) { /* whatever it is, we cannot decode it */
TRACE2(("limit failed in vl_conv_to_utf8 %d/%ld %#06x\n",
rc, limit, source));
rc = 0;
}
if (target != 0) {
switch (rc) {
case 1:
target[0] = (UCHAR) CH(0);
break;
case 2:
target[1] = (UCHAR) (0x80 | (CH(0) & 0x3f));
target[0] = (UCHAR) (0xc0 | (CH(0) >> 6) | ((CH(1) & 0x07) << 2));
break;
case 3:
target[2] = (UCHAR) (0x80 | (CH(0) & 0x3f));
target[1] = (UCHAR) (0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
target[0] = (UCHAR) (0xe0 | ((int) (CH(1) & 0xf0) >> 4));
break;
case 4:
target[3] = (UCHAR) (0x80 | (CH(0) & 0x3f));
target[2] = (UCHAR) (0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
target[1] = (UCHAR) (0x80 |
((int) (CH(1) & 0xf0) >> 4) |
((int) (CH(2) & 0x03) << 4));
target[0] = (UCHAR) (0xf0 | ((int) (CH(2) & 0x1f) >> 2));
break;
case 5:
target[4] = (UCHAR) (0x80 | (CH(0) & 0x3f));
target[3] = (UCHAR) (0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
target[2] = (UCHAR) (0x80 |
((int) (CH(1) & 0xf0) >> 4) |
((int) (CH(2) & 0x03) << 4));
target[1] = (UCHAR) (0x80 | (CH(2) >> 2));
target[0] = (UCHAR) (0xf8 | (CH(3) & 0x03));
break;
case 6:
target[5] = (UCHAR) (0x80 | (CH(0) & 0x3f));
target[4] = (UCHAR) (0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
target[3] = (UCHAR) (0x80 | (CH(1) >> 4) | ((CH(2) & 0x03) << 4));
target[2] = (UCHAR) (0x80 | (CH(2) >> 2));
target[1] = (UCHAR) (0x80 | (CH(3) & 0x3f));
target[0] = (UCHAR) (0xfc | ((int) (CH(3) & 0x40) >> 6));
break;
}
TRACE2(("decode %#08x %02X.%02X.%02X.%02X %d:%.*s\n", source,
CH(3), CH(2), CH(1), CH(0), rc, rc, target));
}
return rc; /* number of bytes needed in target */
#undef CH
}
/******************************************************************************/
static LuitConv *
newLuitConv(size_t elts)
{
LuitConv *result = TypeCalloc(LuitConv);
if (result != 0) {
TRACE(("newLuitConv(%u)\n", (unsigned) elts));
result->table_size = elts;
result->table_utf8 = TypeCallocN(MappingData, elts);
result->rev_index = TypeCallocN(ReverseData, elts);
}
return result;
}
/*
* Try to open a conversion from UTF-8 to the given encoding name. This is
* iconv(), and different implementations expect different syntax for the
* name. So if we do not at first succeed, try permuting the common
* variations.
*/
static iconv_t
try_iconv_open(const char *guess, char **alias)
{
int chcase;
int mkcase;
char *encoding_name = malloc(strlen(guess) + 2);
char *encoding_temp = malloc(strlen(guess) + 2);
char *p;
iconv_t result;
strcpy(encoding_name, guess);
TRACE(("try_iconv_open(%s)\n", NonNull(encoding_name)));
result = iconv_open("UTF-8", encoding_name);
/*
* If the first try did not succeed, retry after changing the case of
* the name and/or inserting a marker between the leading alphabetic
* prefix and number.
*/
if (result == NO_ICONV) {
for (chcase = 0; chcase <= 2; ++chcase) {
strcpy(encoding_name, guess);
switch (chcase) {
case 0: /* no change */
break;
case 1: /* uppercase */
for (p = encoding_name; *p != '\0'; ++p) {
*p = (char) toupper(UChar(*p));
}
break;
case 2: /* lowercase */
for (p = encoding_name; *p != '\0'; ++p) {
*p = (char) tolower(UChar(*p));
}
break;
}
for (mkcase = 0; mkcase <= 3; ++mkcase) {
switch (mkcase) {
case 0: /* no change */
break;
case 1: /* remove delimiter */
for (p = encoding_name; *p != '\0'; ++p) {
if (*p == ' ' || *p == '-') {
if (p != encoding_name &&
isalpha(UChar(p[-1])) &&
isdigit(UChar(p[1]))) {
while ((p[0] = p[1]) != '\0') {
++p;
}
}
break;
}
}
break;
case 2: /* insert '-' */
for (p = encoding_name; *p != '\0'; ++p) {
if (*p == '-')
break;
if (isalpha(UChar(p[0])) &&
isdigit(UChar(p[1]))) {
strcpy(encoding_temp, p + 1);
p[1] = '-';
strcpy(p + 2, encoding_temp);
break;
}
}
break;
case 3: /* change to ' ' */
for (p = encoding_name; *p != '\0'; ++p) {
if (*p == '-') {
*p = ' ';
break;
}
}
break;
}
result = iconv_open("UTF-8", encoding_name);
if (result != NO_ICONV) {
TRACE(("...iconv_open'd with different name \"%s\"\n",
NonNull(encoding_name)));
break;
}
}
if (result != NO_ICONV) {
break;
}
}
}
if (strcmp(encoding_name, guess)) {
if (result != NO_ICONV) {
*alias = encoding_name;
} else {
free(encoding_name);
}
} else {
free(encoding_name);
}
free(encoding_temp);
return result;
}
/******************************************************************************/
static int
cmp_rindex(const void *a, const void *b)
{
const ReverseData *p = (const ReverseData *) a;
const ReverseData *q = (const ReverseData *) b;
return (int) (p)->ucs - (int) (q)->ucs;
}
#ifdef OPT_TRACE
static void
trace_convert(LuitConv * data, size_t which, unsigned gs)
{
size_t j;
char gsbuf[20];
if (gs) {
sprintf(gsbuf, "G%u ", gs);
} else {
gsbuf[0] = '\0';
}
TRACE(("convert %s%04X:%d:%04X:",
gsbuf,
(unsigned) which,
(int) data->table_utf8[which].size,
data->table_utf8[which].ucs));
if (data->table_utf8[which].size) {
for (j = 0; j < data->table_utf8[which].size; ++j) {
TRACE(("%c", data->table_utf8[which].text[j]));
}
}
TRACE(("\n"));
}
#else
#define trace_convert(data,n,gs) /* nothing */
#endif
/*
* Assuming single-byte encoding, count the number of successful translations
* to UTF-8 from the 0..255 range.
*/
static unsigned
count8bitIconv(iconv_t my_desc)
{
unsigned result = 0;
int n;
for (n = 0; n < MAX8; ++n) {
size_t converted;
char input[80];
ICONV_CONST char *ip = input;
char output[80];
char *op = output;
size_t in_bytes = 1;
size_t out_bytes = sizeof(output);
input[0] = (char) n;
input[1] = 0;
(void) iconv(my_desc, NULL, NULL, NULL, NULL);
converted = iconv(my_desc, &ip, &in_bytes, &op, &out_bytes);
if (converted != (size_t) (-1)) {
++result;
}
}
TRACE(("count8bitIconv -> %u\n", result));
return result;
}
#define legalUCode(n) ((n) < 0xd800 || (n) > 0xdfff)
/*
* Given an encoding name, check to see if it is a single-byte encoding.
* Return a suitable table-size, depending.
*
* iconv() provides no function for this purpose. For demonstration purposes
* (though it is claimed to be slow...) we can use iconv to convert a series
* of UTF-8 codes to the given encoding, checking if any of those give more
* than one byte.
*
* If we knew this was always ISO-2022 encoding, we could shorten the scan;
* however luit does handle a few which are not.
*
* As a quick check, we first count the number of codes going _to_ UTF-8 in the
* series 0..255, and if that is 256 there is no need for this function.
*
* FIXME: improve this by constructing a table of known single-byte encoding
* names.
*/
static unsigned
sizeofIconvTable(const char *encoding_name, unsigned limit)
{
unsigned result = MAX8;
iconv_t my_desc = iconv_open(encoding_name, "UTF-8");
if (my_desc != NO_ICONV) {
unsigned n;
unsigned total = 0;
size_t in_bytes;
UCHAR input[80];
ICONV_CONST char *ip;
size_t out_bytes;
char output[80];
char *op;
TRACE(("sizeofIconvTable(%s, %u) opened...\n",
NonNull(encoding_name), limit));
for (n = 0; n < MAX16; ++n) {
if (!legalUCode(n))
continue;
if ((in_bytes = (size_t) ConvToUTF8(input, n, sizeof(input))) == 0) {
continue;
}
ip = (ICONV_CONST char *) input;
op = output;
input[in_bytes] = 0;
out_bytes = sizeof(output);
(void) iconv(my_desc, NULL, NULL, NULL, NULL);
if (iconv(my_desc, &ip, &in_bytes, &op, &out_bytes) == (size_t) -1) {
continue;
}
++total;
/* if we have found all codes that the fast check could, quit */
if ((limit == 256) && (total >= limit)) {
result = limit;
break;
}
}
iconv_close(my_desc);
TRACE(("...total codes %u\n", total));
if (total > 256)
result = MAX16;
}
TRACE(("sizeofIconvTable(%s, %u) = %u\n", NonNull(encoding_name), limit, result));
return result;
}
/*
* Build forward/reverse mappings for single-byte encoding.
*/
static void
initialize8bitTable(LuitConv * data)
{
unsigned n;
TRACE(("initialize8bitTable\n"));
data->len_index = 0;
for (n = 0; n < MAX8; ++n) {
size_t converted;
char input[80];
ICONV_CONST char *ip = input;
char output[80];
char *op = output;
size_t in_bytes = 1;
size_t out_bytes = sizeof(output);
input[0] = (char) n;
input[1] = 0;
(void) iconv(data->iconv_desc, NULL, NULL, NULL, NULL);
converted = iconv(data->iconv_desc, &ip, &in_bytes, &op, &out_bytes);
if (converted == (size_t) (-1)) {
TRACE(("convert err %d\n", n));
} else {
output[sizeof(output) - out_bytes] = 0;
data->table_utf8[n].size = sizeof(output) - out_bytes;
data->table_utf8[n].text = malloc(data->table_utf8[n].size);
memcpy(data->table_utf8[n].text,
output,
data->table_utf8[n].size);
if (ConvToUTF32((UINT *) 0,
data->table_utf8[n].text,
data->table_utf8[n].size)) {
ConvToUTF32(&(data->table_utf8[n].ucs),
data->table_utf8[n].text,
data->table_utf8[n].size);
}
trace_convert(data, (size_t) n, 0);
data->rev_index[data->len_index].ucs = data->table_utf8[n].ucs;
data->rev_index[data->len_index].ch = n;
data->len_index++;
}
}
}
static UINT
dbcsDecode(const char *buffer, int length, int euc, unsigned *gs)
{
UINT result = UChar(buffer[0]);
switch (result) {
case SS2:
*gs = (unsigned) ((length > 1) ? 2 : 1);
break;
case SS3:
*gs = (unsigned) ((length > 1) ? 3 : 1);
break;
default:
*gs = (unsigned) ((result >= 128));
break;
}
switch (*gs) {
case 2:
/* FALLTHRU */
case 3:
++buffer;
--length;
/* FALLTHRU */
default:
switch (length) {
case 0:
break;
case 1:
result = UChar(buffer[0]);
break;
default:
result = (UINT) (((UChar(buffer[0]) << 8)
| UChar(buffer[1])));
if (euc)
result ^= 0x8080;
break;
}
break;
}
return result;
}
/*
* Build forward/reverse mappings for multi-byte encoding.
*
* As in sizeofIconvTable(), scan the encodings using a translation from UTF-8
* to the target. That gives us the reverse-mapping information, from which
* we later construct the forward-mapping.
*
* TODO: update charset size as needed for -show-iconv
*/
static void
initialize16bitTable(const char *charset, LuitConv ** datap, unsigned gmax)
{
unsigned n;
unsigned gs;
LuitConv *data;
iconv_t my_desc = iconv_open(charset, "UTF-8");
TRACE(("initialize16bitTable(%s) gmax %d\n", NonNull(charset), gmax));
for (n = 0; n < gmax; ++n) {
if ((data = datap[n]) != 0) {
datap[n]->len_index = 0;
}
}
if (my_desc != NO_ICONV) {
int euc = !isOtherCharset(charset);
TRACE(("...assume %s index\n", euc ? "EUC" : "non-EUC"));
for (n = 0; n < MAX16; ++n) {
UCHAR input[80];
ICONV_CONST char *ip;
char output[80];
char *op = output;
size_t in_bytes;
size_t out_bytes = sizeof(output);
unsigned my_code;
if (!legalUCode(n))
continue;
if ((in_bytes = (size_t) ConvToUTF8(input, n, sizeof(input))) == 0) {
continue;
}
ip = (ICONV_CONST char *) input;
op = output;
input[in_bytes] = 0;
out_bytes = sizeof(output);
(void) iconv(my_desc, NULL, NULL, NULL, NULL);
if (iconv(my_desc, &ip, &in_bytes, &op, &out_bytes) == (size_t) -1) {
continue;
}
my_code = dbcsDecode(output, (int) (op - output), euc, &gs);
if (gs >= gmax) {
data = (gs == 1) ? datap[0] : 0;
} else {
data = datap[gs];
}
if ((data == 0)
|| (my_code >= data->table_size)) {
TRACE(("skip %d:%#x\n", gs, my_code));
continue;
}
data->table_utf8[my_code].size = strlen((char *) input);
data->table_utf8[my_code].text = strmalloc((char *) input);
data->table_utf8[my_code].ucs = n;
trace_convert(data, (size_t) my_code, gs);
data->rev_index[data->len_index].ucs = n;
data->rev_index[data->len_index].ch = my_code;
data->len_index++;
}
iconv_close(my_desc);
}
}
static unsigned
luitReverse(unsigned code, void *client_data GCC_UNUSED)
{
unsigned result = code;
LuitConv *data = (LuitConv *) client_data;
TRACE(("luitReverse 0x%04X %p\n", code, (void *) data));
if (data != 0) {
ReverseData *p;
ReverseData key;
key.ucs = (UINT) code;
p = (ReverseData *) bsearch(&key,
data->rev_index,
data->len_index,
sizeof(data->rev_index[0]),
cmp_rindex);
if (p != 0) {
result = p->ch;
TRACE(("...mapped %#x\n", result));
}
}
return result;
}
/*
* Translate one of luit's encoding names to one which is more likely to
* work with iconv.
*/
static const char *
findEncodingAlias(const char *encoding_name)
{
/* *INDENT-OFF* */
static const struct {
const char *luit_name;
const char *iconv_name;
} table[] = {
/* 8-bit character sets */
{ "KOI8-E", "ISO-IR-111" },
{ "TCVN-0", "TCVN5712-1:1993" },
{ "ibm-cp437", "cp437" },
{ "ibm-cp850", "cp850" },
{ "ibm-cp865", "cp865" },
{ "ibm-cp866", "cp866" },
{ "iso646.1973-0", "US-ASCII" },
{ "ISO 646 (1973)", "US-ASCII" },
{ "microsoft-cp1250", "windows-1250" },
{ "microsoft-cp1251", "windows-1251" },
{ "microsoft-cp1252", "windows-1252" },
/* EUC aliases */
{ "ksx1001.1997-0", "eucKR" }, /* fontenc -> ksc5601.1987-0 */
{ "ksxjohab-1", "JOHAB" },
/* other (non-ISO-2022) character sets */
{ "gb2312.1980-0", "GB2312" },
{ "big5.eten-0", "BIG-5" },
{ "big5hkscs-0", "BIG5-HKSCS" },
{ "gbk-0", "GBK" },
{ "gb18030.2000-0", "GB18030" },
#if 0
{ "gb18030.2000-1", "GB18030" },
#endif
};
/* *INDENT-ON* */
size_t n;
const char *result = 0;
TRACE(("findEncodingAlias(%s)\n", NonNull(encoding_name)));
for (n = 0; n < SizeOf(table); ++n) {
if (!lcStrCmp(encoding_name, table[n].luit_name)) {
result = table[n].iconv_name;
TRACE(("... matched '%s'\n", NonNull(result)));
break;
}
}
return result;
}
static void
initializeBuiltInTable(LuitConv * data,
const BuiltInCharsetRec * builtIn,
int enc_file)
{
UCHAR buffer[20];
size_t n;
size_t need;
TRACE(("initializing %s '%s'\n",
enc_file ? "external" : "built-in",
NonNull(builtIn->name)));
(void) enc_file;
data->len_index = 0;
for (n = 0; n < builtIn->length; ++n) {
data->table_utf8[n].ucs = (unsigned) n;
}
for (n = 0; n < builtIn->length; ++n) {
if (builtIn->table[n].source < data->table_size) {
size_t j = builtIn->table[n].source;
data->table_utf8[j].ucs = builtIn->table[n].target;
if ((need = (size_t) ConvToUTF8(buffer,
data->table_utf8[j].ucs,
sizeof(buffer) - 1)) != 0) {
data->table_utf8[j].text = malloc(need + 1);
data->table_utf8[j].size = need;
memcpy(data->table_utf8[j].text, buffer, need);
}
trace_convert(data, j, 0);
data->rev_index[data->len_index].ucs = data->table_utf8[j].ucs;
data->rev_index[data->len_index].ch = (unsigned) j;
data->len_index++;
}
}
}
static const BuiltInCharsetRec *
findBuiltinEncoding(const char *encoding_name)
{
size_t n;
const BuiltInCharsetRec *result = 0;
for (n = 0; builtin_encodings[n].name != 0; ++n) {
if (!lcStrCmp(encoding_name, builtin_encodings[n].name)) {
result = &(builtin_encodings[n]);
break;
}
}
return result;
}
/******************************************************************************/
LuitConv *
luitLookupEncoding(FontMapPtr mapping)
{
LuitConv *latest;
LuitConv *result = 0;
for (latest = all_conversions; latest != 0; latest = latest->next) {
if (&(latest->mapping) == mapping) {
result = latest;
}
}
return result;
}
/*
* Provide all of the data, needed for -show-iconv option to construct a ".enc"
* representation.
*/
FontEncPtr
luitGetFontEnc(const char *name, UM_MODE mode)
{
FontEncPtr result = 0;
FontMapPtr mp = 0;
FontMapPtr mp2 = 0;
FontEncSimpleMapPtr mq = 0;
UCode *map = 0;
LuitConv *lc;
int n;
if ((mp = luitLookupMapping(name, mode, usANY)) != 0
&& (lc = luitLookupEncoding(mp)) != 0
&& (mp2 = TypeCalloc(FontMapRec)) != 0
&& (mq = TypeCalloc(FontEncSimpleMapRec)) != 0
&& (map = TypeCallocN(UCode, lc->table_size)) != 0
&& (result = TypeCalloc(FontEncRec)) != 0) {
int max_chr = (MIN_UCODE - 1);
int min_chr = (MAX_UCODE + 1);
result->name = strmalloc(name);
result->size = (int) lc->table_size;
result->mappings = mp2;
*mp2 = *mp;
mp2->client_data = mq;
mp2->next = 0;
mq->len = (unsigned) lc->table_size;
mq->map = map;
for (n = 0; n < (int) lc->table_size; ++n) {
unsigned ch = lc->rev_index[n].ch;
if (ch < mq->len) {
map[ch] = (UCode) lc->rev_index[n].ucs;
if (ch != lc->rev_index[n].ucs) {
if ((int) ch < min_chr)
min_chr = (int) ch;
if ((int) ch > max_chr)
max_chr = (int) ch;
}
}
}
result->size = max_chr + 1;
if (result->size > 256)
result->first = min_chr;
else
result->size = 256;
} else {
if (mp2)
free(mp2);
if (mq)
free(mq);
if (map)
free(map);
}
return result;
}
/*
* Free the data allocated in luitGetFontEnc().
*/
void
luitFreeFontEnc(FontEncPtr data)
{
if (data != 0) {
FontMapPtr mp;
FontEncSimpleMapPtr mq;
if ((mp = data->mappings) != 0) {
if ((mq = mp->client_data) != 0) {
free(mq->map);
free(mq);
}
free(mp);
}
free(data->name);
free(data);
}
}
static void
finishIconvTable(LuitConv * latest)
{
latest->next = all_conversions;
latest->mapping.type = FONT_ENCODING_UNICODE;
latest->mapping.recode = luitRecode;
latest->reverse.reverse = luitReverse;
latest->reverse.data = latest;
all_conversions = latest;
TRACE(("...finished LuitConv table for \"%s\"\n", NonNull(latest->encoding_name)));
}
static FontMapPtr
initLuitConv(const char *encoding_name,
iconv_t my_desc,
const BuiltInCharsetRec * builtIn,
int enc_file,
US_SIZE size)
{
FontMapPtr result = 0;
LuitConv *latest;
unsigned fast;
size_t length = MAX8;
switch (size) {
case us8BIT:
length = MAX8;
break;
case us16BIT:
length = MAX16;
break;
default:
if (builtIn) {
if (builtIn->length != 0) {
size_t n;
for (n = 0; n < builtIn->length; ++n) {
if (builtIn->table[n].source >= MAX8) {
length = MAX16;
break;
}
}
} else {
length = MAX16;
}
} else if ((fast = count8bitIconv(my_desc)) < 256) {
length = sizeofIconvTable(encoding_name, fast);
}
break;
}
TRACE(("initLuitConv(%s) %u\n", NonNull(encoding_name), (unsigned) length));
if ((latest = newLuitConv(length)) != 0) {
latest->encoding_name = strmalloc(encoding_name);
latest->iconv_desc = my_desc;
if (builtIn != 0) {
initializeBuiltInTable(latest, builtIn, enc_file);
} else if (length == MAX16) {
initialize16bitTable(latest->encoding_name, &latest, 1);
} else {
initialize8bitTable(latest);
}
finishIconvTable(latest);
result = &(latest->mapping);
/* sort the reverse-index, to allow using bsearch */
if (result != 0) {
qsort(latest->rev_index,
latest->len_index,
sizeof(latest->rev_index[0]),
cmp_rindex);
}
}
return result;
}
/*
* This uses as input the data loaded from an external ".enc" file to construct
* a table that initLuitConv() can use.
*/
static FontMapPtr
convertFontEnc(FontEncPtr fontenc)
{
FontMapPtr mp = fontenc->mappings;
FontEncSimpleMapPtr mq;
BuiltInMapping *mapping;
FontMapPtr result = 0;
TRACE(("convertFontEnc: %s\n", NonNull(fontenc->name)));
while (mp != 0) {
if (mp->type == FONT_ENCODING_UNICODE)
break;
mp = mp->next;
}
if (mp != 0
&& (mq = mp->client_data) != 0
&& mq->len
&& (mapping = TypeCallocN(BuiltInMapping, mq->len + 1)) != 0) {
unsigned j, k;
BuiltInCharsetRec builtIn;
US_SIZE size = ((fontenc->size <= 256 && fontenc->row_size == 0)
? us8BIT
: us16BIT);
TRACE(("...found mapping for %d items (size:%d)\n", mq->len, size));
memset(&builtIn, 0, sizeof(builtIn));
builtIn.name = fontenc->name;
builtIn.table = mapping;
builtIn.length = mq->len;
for (j = k = 0; j < mq->len; ++j) {
unsigned code = luitRecode(j, mq);
mapping[k].source = j;
mapping[k].target = code ? code : j;
++k;
}
result = initLuitConv(fontenc->name, NO_ICONV, &builtIn, 1, size);
free(mapping);
}
return result;
}
static int
knownCharset(const FontencCharsetRec * fc)
{
int result = 0;