-
Notifications
You must be signed in to change notification settings - Fork 0
/
charset.c
908 lines (831 loc) · 20.8 KB
/
charset.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
/*
* Copyright (C) 1984-2024 Mark Nudelman
*
* You may distribute under the terms of either the GNU General Public
* License or the Less License, as specified in the README file.
*
* For more information, see the README file.
*/
/*
* Functions to define the character set
* and do things specific to the character set.
*/
#include "less.h"
#if HAVE_LOCALE
#include <locale.h>
#include <ctype.h>
#include <langinfo.h>
#endif
#include "charset.h"
#include "xbuf.h"
#if MSDOS_COMPILER==WIN32C
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
extern int bs_mode;
public int utf_mode = 0;
/*
* Predefined character sets,
* selected by the LESSCHARSET environment variable.
*/
struct charset {
char *name;
int *p_flag;
char *desc;
} charsets[] = {
{ "ascii", NULL, "8bcccbcc18b95.b" },
{ "utf-8", &utf_mode, "8bcccbcc18b95.b126.bb" },
{ "iso8859", NULL, "8bcccbcc18b95.33b." },
{ "latin3", NULL, "8bcccbcc18b95.33b5.b8.b15.b4.b12.b18.b12.b." },
{ "arabic", NULL, "8bcccbcc18b95.33b.3b.7b2.13b.3b.b26.5b19.b" },
{ "greek", NULL, "8bcccbcc18b95.33b4.2b4.b3.b35.b44.b" },
{ "greek2005", NULL, "8bcccbcc18b95.33b14.b35.b44.b" },
{ "hebrew", NULL, "8bcccbcc18b95.33b.b29.32b28.2b2.b" },
{ "koi8-r", NULL, "8bcccbcc18b95.b." },
{ "KOI8-T", NULL, "8bcccbcc18b95.b8.b6.b8.b.b.5b7.3b4.b4.b3.b.b.3b." },
{ "georgianps", NULL, "8bcccbcc18b95.3b11.4b12.2b." },
{ "tcvn", NULL, "b..b...bcccbccbbb7.8b95.b48.5b." },
{ "TIS-620", NULL, "8bcccbcc18b95.b.4b.11b7.8b." },
{ "next", NULL, "8bcccbcc18b95.bb125.bb" },
{ "dos", NULL, "8bcccbcc12bc5b95.b." },
{ "windows-1251", NULL, "8bcccbcc12bc5b95.b24.b." },
{ "windows-1252", NULL, "8bcccbcc12bc5b95.b.b11.b.2b12.b." },
{ "windows-1255", NULL, "8bcccbcc12bc5b95.b.b8.b.5b9.b.4b." },
{ "ebcdic", NULL, "5bc6bcc7bcc41b.9b7.9b5.b..8b6.10b6.b9.7b9.8b8.17b3.3b9.7b9.8b8.6b10.b.b.b." },
{ "IBM-1047", NULL, "4cbcbc3b9cbccbccbb4c6bcc5b3cbbc4bc4bccbc191.b" },
{ NULL, NULL, NULL }
};
/*
* Support "locale charmap"/nl_langinfo(CODESET) values, as well as others.
*/
struct cs_alias {
char *name;
char *oname;
} cs_aliases[] = {
{ "UTF-8", "utf-8" },
{ "utf8", "utf-8" },
{ "UTF8", "utf-8" },
{ "ANSI_X3.4-1968", "ascii" },
{ "US-ASCII", "ascii" },
{ "latin1", "iso8859" },
{ "ISO-8859-1", "iso8859" },
{ "latin9", "iso8859" },
{ "ISO-8859-15", "iso8859" },
{ "latin2", "iso8859" },
{ "ISO-8859-2", "iso8859" },
{ "ISO-8859-3", "latin3" },
{ "latin4", "iso8859" },
{ "ISO-8859-4", "iso8859" },
{ "cyrillic", "iso8859" },
{ "ISO-8859-5", "iso8859" },
{ "ISO-8859-6", "arabic" },
{ "ISO-8859-7", "greek" },
{ "IBM9005", "greek2005" },
{ "ISO-8859-8", "hebrew" },
{ "latin5", "iso8859" },
{ "ISO-8859-9", "iso8859" },
{ "latin6", "iso8859" },
{ "ISO-8859-10", "iso8859" },
{ "latin7", "iso8859" },
{ "ISO-8859-13", "iso8859" },
{ "latin8", "iso8859" },
{ "ISO-8859-14", "iso8859" },
{ "latin10", "iso8859" },
{ "ISO-8859-16", "iso8859" },
{ "IBM437", "dos" },
{ "EBCDIC-US", "ebcdic" },
{ "IBM1047", "IBM-1047" },
{ "KOI8-R", "koi8-r" },
{ "KOI8-U", "koi8-r" },
{ "GEORGIAN-PS", "georgianps" },
{ "TCVN5712-1", "tcvn" },
{ "NEXTSTEP", "next" },
{ "windows", "windows-1252" }, /* backward compatibility */
{ "CP1251", "windows-1251" },
{ "CP1252", "windows-1252" },
{ "CP1255", "windows-1255" },
{ NULL, NULL }
};
#define IS_BINARY_CHAR 01
#define IS_CONTROL_CHAR 02
static char chardef[256];
static constant char *binfmt = NULL;
static constant char *utfbinfmt = NULL;
public int binattr = AT_STANDOUT|AT_COLOR_BIN;
static struct xbuffer user_wide_array;
static struct xbuffer user_ubin_array;
static struct xbuffer user_compose_array;
static struct xbuffer user_prt_array;
static struct wchar_range_table user_wide_table;
static struct wchar_range_table user_ubin_table;
static struct wchar_range_table user_compose_table;
static struct wchar_range_table user_prt_table;
/*
* Set a wchar_range_table to the table in an xbuffer.
*/
static void wchar_range_table_set(struct wchar_range_table *tbl, struct xbuffer *arr)
{
tbl->table = (struct wchar_range *) arr->data;
tbl->count = (unsigned int) (arr->end / sizeof(struct wchar_range));
}
/*
* Skip over a "U" or "U+" prefix before a hex codepoint.
*/
static constant char * skip_uprefix(constant char *s)
{
if (*s == 'U' || *s == 'u')
if (*++s == '+') ++s;
return s;
}
/*
* Parse a dash-separated range of hex values.
*/
static void wchar_range_get(constant char **ss, struct wchar_range *range)
{
constant char *s = skip_uprefix(*ss);
range->first = lstrtoulc(s, &s, 16);
if (s[0] == '-')
{
s = skip_uprefix(&s[1]);
range->last = lstrtoulc(s, &s, 16);
} else
{
range->last = range->first;
}
*ss = s;
}
/*
* Parse the LESSUTFCHARDEF variable.
*/
static void ichardef_utf(constant char *s)
{
xbuf_init(&user_wide_array);
xbuf_init(&user_ubin_array);
xbuf_init(&user_compose_array);
xbuf_init(&user_prt_array);
if (s != NULL)
{
while (s[0] != '\0')
{
struct wchar_range range;
wchar_range_get(&s, &range);
if (range.last == 0)
{
error("invalid hex number(s) in LESSUTFCHARDEF", NULL_PARG);
quit(QUIT_ERROR);
}
if (*s++ != ':')
{
error("missing colon in LESSUTFCHARDEF", NULL_PARG);
quit(QUIT_ERROR);
}
switch (*s++)
{
case 'b':
xbuf_add_data(&user_ubin_array, (unsigned char *) &range, sizeof(range));
break;
case 'c':
xbuf_add_data(&user_compose_array, (unsigned char *) &range, sizeof(range));
break;
case 'w':
xbuf_add_data(&user_wide_array, (unsigned char *) &range, sizeof(range));
xbuf_add_data(&user_prt_array, (unsigned char *) &range, sizeof(range));
break;
case 'p': case '.':
xbuf_add_data(&user_prt_array, (unsigned char *) &range, sizeof(range));
break;
case '\0':
s--;
break;
default:
/* Ignore unknown character attribute. */
break;
}
if (s[0] == ',') ++s;
}
}
wchar_range_table_set(&user_wide_table, &user_wide_array);
wchar_range_table_set(&user_ubin_table, &user_ubin_array);
wchar_range_table_set(&user_compose_table, &user_compose_array);
wchar_range_table_set(&user_prt_table, &user_prt_array);
}
/*
* Define a charset, given a description string.
* The string consists of 256 letters,
* one for each character in the charset.
* If the string is shorter than 256 letters, missing letters
* are taken to be identical to the last one.
* A decimal number followed by a letter is taken to be a
* repetition of the letter.
*
* Each letter is one of:
* . normal character
* b binary character
* c control character
*/
static void ichardef(constant char *s)
{
char *cp;
int n;
char v;
n = 0;
v = 0;
cp = chardef;
while (*s != '\0')
{
switch (*s++)
{
case '.':
v = 0;
break;
case 'c':
v = IS_CONTROL_CHAR;
break;
case 'b':
v = IS_BINARY_CHAR|IS_CONTROL_CHAR;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (ckd_mul(&n, n, 10) || ckd_add(&n, n, s[-1] - '0'))
goto invalid_chardef;
continue;
default:
invalid_chardef:
error("invalid chardef", NULL_PARG);
quit(QUIT_ERROR);
/*NOTREACHED*/
}
do
{
if (cp >= chardef + sizeof(chardef))
{
error("chardef longer than 256", NULL_PARG);
quit(QUIT_ERROR);
/*NOTREACHED*/
}
*cp++ = v;
} while (--n > 0);
n = 0;
}
while (cp < chardef + sizeof(chardef))
*cp++ = v;
}
/*
* Define a charset, given a charset name.
* The valid charset names are listed in the "charsets" array.
*/
static int icharset(constant char *name, int no_error)
{
struct charset *p;
struct cs_alias *a;
if (name == NULL || *name == '\0')
return (0);
/* First see if the name is an alias. */
for (a = cs_aliases; a->name != NULL; a++)
{
if (strcmp(name, a->name) == 0)
{
name = a->oname;
break;
}
}
for (p = charsets; p->name != NULL; p++)
{
if (strcmp(name, p->name) == 0)
{
ichardef(p->desc);
if (p->p_flag != NULL)
{
#if MSDOS_COMPILER==WIN32C
*(p->p_flag) = 1 + (GetConsoleOutputCP() != CP_UTF8);
#else
*(p->p_flag) = 1;
#endif
}
return (1);
}
}
if (!no_error) {
error("invalid charset name", NULL_PARG);
quit(QUIT_ERROR);
}
return (0);
}
#if HAVE_LOCALE
/*
* Define a charset, given a locale name.
*/
static void ilocale(void)
{
int c;
for (c = 0; c < (int) sizeof(chardef); c++)
{
if (isprint(c))
chardef[c] = 0;
else if (iscntrl(c))
chardef[c] = IS_CONTROL_CHAR;
else
chardef[c] = IS_BINARY_CHAR|IS_CONTROL_CHAR;
}
}
#endif
/*
* Define the printing format for control (or binary utf) chars.
*/
public void setfmt(constant char *s, constant char **fmtvarptr, int *attrptr, constant char *default_fmt, lbool for_printf)
{
if (s == NULL || *s == '\0')
s = default_fmt;
else if (for_printf &&
((*s == '*' && (s[1] == '\0' || s[2] == '\0' || strchr(s + 2, 'n'))) ||
(*s != '*' && strchr(s, 'n'))))
/* %n is evil */
s = default_fmt;
/*
* Select the attributes if it starts with "*".
*/
if (*s == '*' && s[1] != '\0')
{
switch (s[1])
{
case 'd': *attrptr = AT_BOLD; break;
case 'k': *attrptr = AT_BLINK; break;
case 's': *attrptr = AT_STANDOUT; break;
case 'u': *attrptr = AT_UNDERLINE; break;
default: *attrptr = AT_NORMAL; break;
}
s += 2;
}
*fmtvarptr = s;
}
/*
*
*/
static void set_charset(void)
{
constant char *s;
ichardef_utf(lgetenv("LESSUTFCHARDEF"));
/*
* See if environment variable LESSCHARSET is defined.
*/
s = lgetenv("LESSCHARSET");
if (icharset(s, 0))
return;
/*
* LESSCHARSET is not defined: try LESSCHARDEF.
*/
s = lgetenv("LESSCHARDEF");
if (!isnullenv(s))
{
ichardef(s);
return;
}
#if HAVE_LOCALE
#ifdef CODESET
/*
* Try using the codeset name as the charset name.
*/
s = nl_langinfo(CODESET);
if (icharset(s, 1))
return;
#endif
#endif
#if HAVE_STRSTR
/*
* Check whether LC_ALL, LC_CTYPE or LANG look like UTF-8 is used.
*/
if ((s = lgetenv("LC_ALL")) != NULL ||
(s = lgetenv("LC_CTYPE")) != NULL ||
(s = lgetenv("LANG")) != NULL)
{
if ( strstr(s, "UTF-8") != NULL || strstr(s, "utf-8") != NULL
|| strstr(s, "UTF8") != NULL || strstr(s, "utf8") != NULL)
if (icharset("utf-8", 1))
return;
}
#endif
#if HAVE_LOCALE
/*
* Get character definitions from locale functions,
* rather than from predefined charset entry.
*/
ilocale();
#else
#if MSDOS_COMPILER
#if MSDOS_COMPILER==WIN32C
(void) icharset("utf-8", 1);
#else
(void) icharset("dos", 1);
#endif
#else
(void) icharset("utf-8", 1);
#endif
#endif
}
/*
* Initialize charset data structures.
*/
public void init_charset(void)
{
constant char *s;
#if HAVE_LOCALE
setlocale(LC_ALL, "");
#endif
set_charset();
s = lgetenv("LESSBINFMT");
setfmt(s, &binfmt, &binattr, "*s<%02X>", TRUE);
s = lgetenv("LESSUTFBINFMT");
setfmt(s, &utfbinfmt, &binattr, "<U+%04lX>", TRUE);
}
/*
* Is a given character a "binary" character?
*/
public lbool binary_char(LWCHAR c)
{
if (utf_mode)
return (is_ubin_char(c));
if (c >= sizeof(chardef))
return TRUE;
return ((chardef[c] & IS_BINARY_CHAR) != 0);
}
/*
* Is a given character a "control" character?
*/
public lbool control_char(LWCHAR c)
{
if (c >= sizeof(chardef))
return TRUE;
return (chardef[c] & IS_CONTROL_CHAR);
}
/*
* Return the printable form of a character.
* For example, in the "ascii" charset '\3' is printed as "^C".
*/
public constant char * prchar(LWCHAR c)
{
/* {{ Fixed buffer size means LESSBINFMT etc can be truncated. }} */
static char buf[MAX_PRCHAR_LEN+1];
c &= 0377; /*{{type-issue}}*/
if ((c < 128 || !utf_mode) && !control_char(c))
SNPRINTF1(buf, sizeof(buf), "%c", (int) c);
else if (c == ESC)
strcpy(buf, "ESC");
#if IS_EBCDIC_HOST
else if (!binary_char(c) && c < 64)
SNPRINTF1(buf, sizeof(buf), "^%c",
/*
* This array roughly inverts CONTROL() #defined in less.h,
* and should be kept in sync with CONTROL() and IBM-1047.
*/
"@ABC.I.?...KLMNO"
"PQRS.JH.XY.."
"\\]^_"
"......W[.....EFG"
"..V....D....TU.Z"[c]);
#else
else if (c < 128 && !control_char(c ^ 0100))
SNPRINTF1(buf, sizeof(buf), "^%c", (int) (c ^ 0100));
#endif
else
SNPRINTF1(buf, sizeof(buf), binfmt, c);
return (buf);
}
/*
* Return the printable form of a UTF-8 character.
*/
public constant char * prutfchar(LWCHAR ch)
{
static char buf[MAX_PRCHAR_LEN+1];
if (ch == ESC)
strcpy(buf, "ESC");
else if (ch < 128 && control_char(ch))
{
if (!control_char(ch ^ 0100))
SNPRINTF1(buf, sizeof(buf), "^%c", ((char) ch) ^ 0100);
else
SNPRINTF1(buf, sizeof(buf), binfmt, (char) ch);
} else if (is_ubin_char(ch))
{
SNPRINTF1(buf, sizeof(buf), utfbinfmt, ch);
} else
{
char *p = buf;
if (ch >= 0x80000000)
ch = 0xFFFD; /* REPLACEMENT CHARACTER */
put_wchar(&p, ch);
*p = '\0';
}
return (buf);
}
/*
* Get the length of a UTF-8 character in bytes.
*/
public int utf_len(char ch)
{
if ((ch & 0x80) == 0)
return 1;
if ((ch & 0xE0) == 0xC0)
return 2;
if ((ch & 0xF0) == 0xE0)
return 3;
if ((ch & 0xF8) == 0xF0)
return 4;
#if 0
if ((ch & 0xFC) == 0xF8)
return 5;
if ((ch & 0xFE) == 0xFC)
return 6;
#endif
/* Invalid UTF-8 encoding. */
return 1;
}
/*
* Does the parameter point to the lead byte of a well-formed UTF-8 character?
*/
public lbool is_utf8_well_formed(constant char *ss, int slen)
{
int i;
int len;
unsigned char s0 = (unsigned char) ss[0];
if (IS_UTF8_INVALID(s0))
return (FALSE);
len = utf_len(ss[0]);
if (len > slen)
return (FALSE);
if (len == 1)
return (TRUE);
if (len == 2)
{
if (s0 < 0xC2)
return (FALSE);
} else
{
unsigned char mask = (unsigned char) (~((1 << (8-len)) - 1));
if (s0 == mask && (ss[1] & mask) == 0x80)
return (FALSE);
}
for (i = 1; i < len; i++)
if (!IS_UTF8_TRAIL(ss[i]))
return (FALSE);
return (TRUE);
}
/*
* Skip bytes until a UTF-8 lead byte (11xxxxxx) or ASCII byte (0xxxxxxx) is found.
*/
public void utf_skip_to_lead(constant char **pp, constant char *limit)
{
do {
++(*pp);
} while (*pp < limit && !IS_UTF8_LEAD((*pp)[0] & 0377) && !IS_ASCII_OCTET((*pp)[0]));
}
/*
* Get the value of a UTF-8 character.
*/
public LWCHAR get_wchar(constant char *sp)
{
constant unsigned char *p = (constant unsigned char *) sp;
switch (utf_len(sp[0]))
{
case 1:
default:
/* 0xxxxxxx */
return (LWCHAR)
(p[0] & 0xFF);
case 2:
/* 110xxxxx 10xxxxxx */
return (LWCHAR) (
((p[0] & 0x1F) << 6) |
(p[1] & 0x3F));
case 3:
/* 1110xxxx 10xxxxxx 10xxxxxx */
return (LWCHAR) (
((p[0] & 0x0F) << 12) |
((p[1] & 0x3F) << 6) |
(p[2] & 0x3F));
case 4:
/* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
return (LWCHAR) (
((p[0] & 0x07) << 18) |
((p[1] & 0x3F) << 12) |
((p[2] & 0x3F) << 6) |
(p[3] & 0x3F));
#if 0
case 5:
/* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
return (LWCHAR) (
((p[0] & 0x03) << 24) |
((p[1] & 0x3F) << 18) |
((p[2] & 0x3F) << 12) |
((p[3] & 0x3F) << 6) |
(p[4] & 0x3F));
case 6:
/* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
return (LWCHAR) (
((p[0] & 0x01) << 30) |
((p[1] & 0x3F) << 24) |
((p[2] & 0x3F) << 18) |
((p[3] & 0x3F) << 12) |
((p[4] & 0x3F) << 6) |
(p[5] & 0x3F));
#endif
}
}
/*
* Store a character into a UTF-8 string.
*/
public void put_wchar(mutable char **pp, LWCHAR ch)
{
if (!utf_mode || ch < 0x80)
{
/* 0xxxxxxx */
*(*pp)++ = (char) ch;
} else if (ch < 0x800)
{
/* 110xxxxx 10xxxxxx */
*(*pp)++ = (char) (0xC0 | ((ch >> 6) & 0x1F));
*(*pp)++ = (char) (0x80 | (ch & 0x3F));
} else if (ch < 0x10000)
{
/* 1110xxxx 10xxxxxx 10xxxxxx */
*(*pp)++ = (char) (0xE0 | ((ch >> 12) & 0x0F));
*(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
*(*pp)++ = (char) (0x80 | (ch & 0x3F));
} else if (ch < 0x200000)
{
/* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
*(*pp)++ = (char) (0xF0 | ((ch >> 18) & 0x07));
*(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
*(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
*(*pp)++ = (char) (0x80 | (ch & 0x3F));
#if 0
} else if (ch < 0x4000000)
{
/* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
*(*pp)++ = (char) (0xF0 | ((ch >> 24) & 0x03));
*(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
*(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
*(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
*(*pp)++ = (char) (0x80 | (ch & 0x3F));
} else
{
/* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
*(*pp)++ = (char) (0xF0 | ((ch >> 30) & 0x01));
*(*pp)++ = (char) (0x80 | ((ch >> 24) & 0x3F));
*(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
*(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
*(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
*(*pp)++ = (char) (0x80 | (ch & 0x3F));
#endif
}
}
/*
* Step forward or backward one character in a string.
*/
public LWCHAR step_charc(constant char **pp, signed int dir, constant char *limit)
{
LWCHAR ch;
int len;
constant char *p = *pp;
if (!utf_mode)
{
/* It's easy if chars are one byte. */
if (dir > 0)
ch = (LWCHAR) (unsigned char) ((p < limit) ? *p++ : 0);
else
ch = (LWCHAR) (unsigned char) ((p > limit) ? *--p : 0);
} else if (dir > 0)
{
if (p >= limit)
ch = 0;
else
{
len = utf_len(*p);
if (p + len > limit || !is_utf8_well_formed(p, len))
{
ch = (LWCHAR) (unsigned char) *p++;
} else
{
ch = get_wchar(p);
p += len;
}
}
} else
{
while (p > limit && IS_UTF8_TRAIL(p[-1]))
p--;
if (p <= limit)
ch = 0;
else
{
len = utf_len(*--p);
if (p + len != *pp || !is_utf8_well_formed(p, len))
{
p = *pp - 1;
ch = (LWCHAR) (unsigned char) *p;
} else
{
ch = get_wchar(p);
}
}
}
*pp = p;
return ch;
}
public LWCHAR step_char(char **pp, signed int dir, constant char *limit)
{
constant char *p = (constant char *) *pp;
LWCHAR ch = step_charc(&p, dir, limit);
*pp = (char *) p;
return ch;
}
/*
* Unicode characters data
* Actual data is in the generated *.uni files.
*/
#define DECLARE_RANGE_TABLE_START(name) \
static struct wchar_range name##_array[] = {
#define DECLARE_RANGE_TABLE_END(name) \
}; struct wchar_range_table name##_table = { name##_array, countof(name##_array) };
DECLARE_RANGE_TABLE_START(compose)
#include "compose.uni"
DECLARE_RANGE_TABLE_END(compose)
DECLARE_RANGE_TABLE_START(ubin)
#include "ubin.uni"
DECLARE_RANGE_TABLE_END(ubin)
DECLARE_RANGE_TABLE_START(wide)
#include "wide.uni"
DECLARE_RANGE_TABLE_END(wide)
DECLARE_RANGE_TABLE_START(fmt)
#include "fmt.uni"
DECLARE_RANGE_TABLE_END(fmt)
/* comb_table is special pairs, not ranges. */
static struct wchar_range comb_table[] = {
{0x0644,0x0622}, {0x0644,0x0623}, {0x0644,0x0625}, {0x0644,0x0627},
};
static lbool is_in_table(LWCHAR ch, struct wchar_range_table *table)
{
unsigned int hi;
unsigned int lo;
/* Binary search in the table. */
if (table->table == NULL || table->count == 0 || ch < table->table[0].first)
return FALSE;
lo = 0;
hi = table->count - 1;
while (lo <= hi)
{
unsigned int mid = (lo + hi) / 2;
if (ch > table->table[mid].last)
lo = mid + 1;
else if (ch < table->table[mid].first)
hi = mid - 1;
else
return TRUE;
}
return FALSE;
}
/*
* Is a character a UTF-8 composing character?
* If a composing character follows any char, the two combine into one glyph.
*/
public lbool is_composing_char(LWCHAR ch)
{
if (is_in_table(ch, &user_prt_table)) return FALSE;
return is_in_table(ch, &user_compose_table) ||
is_in_table(ch, &compose_table) ||
(bs_mode != BS_CONTROL && is_in_table(ch, &fmt_table));
}
/*
* Should this UTF-8 character be treated as binary?
*/
public lbool is_ubin_char(LWCHAR ch)
{
if (is_in_table(ch, &user_prt_table)) return FALSE;
return is_in_table(ch, &user_ubin_table) ||
is_in_table(ch, &ubin_table) ||
(bs_mode == BS_CONTROL && is_in_table(ch, &fmt_table));
}
/*
* Is this a double width UTF-8 character?
*/
public lbool is_wide_char(LWCHAR ch)
{
return is_in_table(ch, &user_wide_table) ||
is_in_table(ch, &wide_table);
}
/*
* Is a character a UTF-8 combining character?
* A combining char acts like an ordinary char, but if it follows
* a specific char (not any char), the two combine into one glyph.
*/
public lbool is_combining_char(LWCHAR ch1, LWCHAR ch2)
{
/* The table is small; use linear search. */
int i;
for (i = 0; i < countof(comb_table); i++)
{
if (ch1 == comb_table[i].first &&
ch2 == comb_table[i].last)
return TRUE;
}
return FALSE;
}