1 /*
2 * Copyright (C) 1984-2025 Mark Nudelman
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
6 *
7 * For more information, see the README file.
8 */
9
10
11 /*
12 * Functions to define the character set
13 * and do things specific to the character set.
14 */
15
16 #include "less.h"
17 #if HAVE_LOCALE
18 #include <locale.h>
19 #include <ctype.h>
20 #include <langinfo.h>
21 #endif
22
23 #include "charset.h"
24 #include "xbuf.h"
25
26 #if MSDOS_COMPILER==WIN32C
27 #define WIN32_LEAN_AND_MEAN
28 #include <windows.h>
29 #endif
30
31 extern int bs_mode;
32
33 public int utf_mode = 0;
34
35 /*
36 * Predefined character sets,
37 * selected by the LESSCHARSET environment variable.
38 */
39 struct charset {
40 char *name;
41 int *p_flag;
42 char *desc;
43 } charsets[] = {
44 { "ascii", NULL, "8bcccbcc18b95.b" },
45 { "utf-8", &utf_mode, "8bcccbcc18b95.b126.bb" },
46 { "iso8859", NULL, "8bcccbcc18b95.33b." },
47 { "latin3", NULL, "8bcccbcc18b95.33b5.b8.b15.b4.b12.b18.b12.b." },
48 { "arabic", NULL, "8bcccbcc18b95.33b.3b.7b2.13b.3b.b26.5b19.b" },
49 { "greek", NULL, "8bcccbcc18b95.33b4.2b4.b3.b35.b44.b" },
50 { "greek2005", NULL, "8bcccbcc18b95.33b14.b35.b44.b" },
51 { "hebrew", NULL, "8bcccbcc18b95.33b.b29.32b28.2b2.b" },
52 { "koi8-r", NULL, "8bcccbcc18b95.b." },
53 { "KOI8-T", NULL, "8bcccbcc18b95.b8.b6.b8.b.b.5b7.3b4.b4.b3.b.b.3b." },
54 { "georgianps", NULL, "8bcccbcc18b95.3b11.4b12.2b." },
55 { "tcvn", NULL, "b..b...bcccbccbbb7.8b95.b48.5b." },
56 { "TIS-620", NULL, "8bcccbcc18b95.b.4b.11b7.8b." },
57 { "next", NULL, "8bcccbcc18b95.bb125.bb" },
58 { "dos", NULL, "8bcccbcc12bc5b95.b." },
59 { "windows-1251", NULL, "8bcccbcc12bc5b95.b24.b." },
60 { "windows-1252", NULL, "8bcccbcc12bc5b95.b.b11.b.2b12.b." },
61 { "windows-1255", NULL, "8bcccbcc12bc5b95.b.b8.b.5b9.b.4b." },
62 { "ebcdic", NULL, "5bc6bcc7bcc41b.9b7.9b5.b..8b6.10b6.b9.7b9.8b8.17b3.3b9.7b9.8b8.6b10.b.b.b." },
63 { "IBM-1047", NULL, "4cbcbc3b9cbccbccbb4c6bcc5b3cbbc4bc4bccbc191.b" },
64 { NULL, NULL, NULL }
65 };
66
67 /*
68 * Support "locale charmap"/nl_langinfo(CODESET) values, as well as others.
69 */
70 struct cs_alias {
71 char *name;
72 char *oname;
73 } cs_aliases[] = {
74 { "UTF-8", "utf-8" },
75 { "utf8", "utf-8" },
76 { "UTF8", "utf-8" },
77 { "ANSI_X3.4-1968", "ascii" },
78 { "US-ASCII", "ascii" },
79 { "latin1", "iso8859" },
80 { "ISO-8859-1", "iso8859" },
81 { "latin9", "iso8859" },
82 { "ISO-8859-15", "iso8859" },
83 { "latin2", "iso8859" },
84 { "ISO-8859-2", "iso8859" },
85 { "ISO-8859-3", "latin3" },
86 { "latin4", "iso8859" },
87 { "ISO-8859-4", "iso8859" },
88 { "cyrillic", "iso8859" },
89 { "ISO-8859-5", "iso8859" },
90 { "ISO-8859-6", "arabic" },
91 { "ISO-8859-7", "greek" },
92 { "IBM9005", "greek2005" },
93 { "ISO-8859-8", "hebrew" },
94 { "latin5", "iso8859" },
95 { "ISO-8859-9", "iso8859" },
96 { "latin6", "iso8859" },
97 { "ISO-8859-10", "iso8859" },
98 { "latin7", "iso8859" },
99 { "ISO-8859-13", "iso8859" },
100 { "latin8", "iso8859" },
101 { "ISO-8859-14", "iso8859" },
102 { "latin10", "iso8859" },
103 { "ISO-8859-16", "iso8859" },
104 { "IBM437", "dos" },
105 { "EBCDIC-US", "ebcdic" },
106 { "IBM1047", "IBM-1047" },
107 { "KOI8-R", "koi8-r" },
108 { "KOI8-U", "koi8-r" },
109 { "GEORGIAN-PS", "georgianps" },
110 { "TCVN5712-1", "tcvn" },
111 { "NEXTSTEP", "next" },
112 { "windows", "windows-1252" }, /* backward compatibility */
113 { "CP1251", "windows-1251" },
114 { "CP1252", "windows-1252" },
115 { "CP1255", "windows-1255" },
116 { NULL, NULL }
117 };
118
119 #define IS_BINARY_CHAR 01
120 #define IS_CONTROL_CHAR 02
121
122 static char chardef[256];
123 static constant char *binfmt = NULL;
124 static constant char *utfbinfmt = NULL;
125 public int binattr = AT_STANDOUT|AT_COLOR_BIN;
126
127 static struct xbuffer user_wide_array;
128 static struct xbuffer user_ubin_array;
129 static struct xbuffer user_compose_array;
130 static struct xbuffer user_prt_array;
131 static struct xbuffer user_omit_array;
132 static struct wchar_range_table user_wide_table;
133 static struct wchar_range_table user_ubin_table;
134 static struct wchar_range_table user_compose_table;
135 static struct wchar_range_table user_prt_table;
136 static struct wchar_range_table user_omit_table;
137
138 /*
139 * Set a wchar_range_table to the table in an xbuffer.
140 */
wchar_range_table_set(struct wchar_range_table * tbl,struct xbuffer * arr)141 static void wchar_range_table_set(struct wchar_range_table *tbl, struct xbuffer *arr)
142 {
143 tbl->table = (struct wchar_range *) arr->data;
144 tbl->count = (unsigned int) (arr->end / sizeof(struct wchar_range));
145 }
146
147 /*
148 * Skip over a "U" or "U+" prefix before a hex codepoint.
149 */
skip_uprefix(constant char * s)150 static constant char * skip_uprefix(constant char *s)
151 {
152 if (*s == 'U' || *s == 'u')
153 if (*++s == '+') ++s;
154 return s;
155 }
156
157 /*
158 * Parse a dash-separated range of hex values.
159 */
wchar_range_get(constant char ** ss,struct wchar_range * range)160 static void wchar_range_get(constant char **ss, struct wchar_range *range)
161 {
162 constant char *s = skip_uprefix(*ss);
163 range->first = lstrtoulc(s, &s, 16);
164 if (s[0] == '-')
165 {
166 s = skip_uprefix(&s[1]);
167 range->last = lstrtoulc(s, &s, 16);
168 } else
169 {
170 range->last = range->first;
171 }
172 *ss = s;
173 }
174
175 /*
176 * Parse the LESSUTFCHARDEF variable.
177 */
ichardef_utf(constant char * s)178 static void ichardef_utf(constant char *s)
179 {
180 xbuf_init(&user_wide_array);
181 xbuf_init(&user_ubin_array);
182 xbuf_init(&user_compose_array);
183 xbuf_init(&user_prt_array);
184 xbuf_init(&user_omit_array);
185
186 if (s != NULL)
187 {
188 while (s[0] != '\0')
189 {
190 struct wchar_range range;
191 wchar_range_get(&s, &range);
192 if (range.last == 0)
193 {
194 error("invalid hex number(s) in LESSUTFCHARDEF", NULL_PARG);
195 quit(QUIT_ERROR);
196 }
197 if (*s++ != ':')
198 {
199 error("missing colon in LESSUTFCHARDEF", NULL_PARG);
200 quit(QUIT_ERROR);
201 }
202 switch (*s++)
203 {
204 case 'b':
205 xbuf_add_data(&user_ubin_array, (unsigned char *) &range, sizeof(range));
206 break;
207 case 'c':
208 xbuf_add_data(&user_compose_array, (unsigned char *) &range, sizeof(range));
209 break;
210 case 'd':
211 xbuf_add_data(&user_omit_array, (unsigned char *) &range, sizeof(range));
212 break;
213 case 'w':
214 xbuf_add_data(&user_wide_array, (unsigned char *) &range, sizeof(range));
215 xbuf_add_data(&user_prt_array, (unsigned char *) &range, sizeof(range));
216 break;
217 case 'p': case '.':
218 xbuf_add_data(&user_prt_array, (unsigned char *) &range, sizeof(range));
219 break;
220 case '\0':
221 s--;
222 break;
223 default:
224 /* Ignore unknown character attribute. */
225 break;
226 }
227 if (s[0] == ',') ++s;
228 }
229 }
230 wchar_range_table_set(&user_wide_table, &user_wide_array);
231 wchar_range_table_set(&user_ubin_table, &user_ubin_array);
232 wchar_range_table_set(&user_compose_table, &user_compose_array);
233 wchar_range_table_set(&user_prt_table, &user_prt_array);
234 wchar_range_table_set(&user_omit_table, &user_omit_array);
235 }
236
237 /*
238 * Define a charset, given a description string.
239 * The string consists of 256 letters,
240 * one for each character in the charset.
241 * If the string is shorter than 256 letters, missing letters
242 * are taken to be identical to the last one.
243 * A decimal number followed by a letter is taken to be a
244 * repetition of the letter.
245 *
246 * Each letter is one of:
247 * . normal character
248 * b binary character
249 * c control character
250 */
ichardef(constant char * s)251 static void ichardef(constant char *s)
252 {
253 char *cp;
254 int n;
255 char v;
256
257 n = 0;
258 v = 0;
259 cp = chardef;
260 while (*s != '\0')
261 {
262 switch (*s++)
263 {
264 case '.':
265 v = 0;
266 break;
267 case 'c':
268 v = IS_CONTROL_CHAR;
269 break;
270 case 'b':
271 v = IS_BINARY_CHAR|IS_CONTROL_CHAR;
272 break;
273
274 case '0': case '1': case '2': case '3': case '4':
275 case '5': case '6': case '7': case '8': case '9':
276 if (ckd_mul(&n, n, 10) || ckd_add(&n, n, s[-1] - '0'))
277 goto invalid_chardef;
278 continue;
279
280 default:
281 invalid_chardef:
282 error("invalid chardef", NULL_PARG);
283 quit(QUIT_ERROR);
284 /*NOTREACHED*/
285 }
286
287 do
288 {
289 if (cp >= chardef + sizeof(chardef))
290 {
291 error("chardef longer than 256", NULL_PARG);
292 quit(QUIT_ERROR);
293 /*NOTREACHED*/
294 }
295 *cp++ = v;
296 } while (--n > 0);
297 n = 0;
298 }
299
300 while (cp < chardef + sizeof(chardef))
301 *cp++ = v;
302 }
303
304 /*
305 * Define a charset, given a charset name.
306 * The valid charset names are listed in the "charsets" array.
307 */
icharset(constant char * name,int no_error)308 static int icharset(constant char *name, int no_error)
309 {
310 struct charset *p;
311 struct cs_alias *a;
312
313 if (name == NULL || *name == '\0')
314 return (0);
315
316 /* First see if the name is an alias. */
317 for (a = cs_aliases; a->name != NULL; a++)
318 {
319 if (strcmp(name, a->name) == 0)
320 {
321 name = a->oname;
322 break;
323 }
324 }
325
326 for (p = charsets; p->name != NULL; p++)
327 {
328 if (strcmp(name, p->name) == 0)
329 {
330 ichardef(p->desc);
331 if (p->p_flag != NULL)
332 {
333 #if MSDOS_COMPILER==WIN32C
334 *(p->p_flag) = 1 + (GetConsoleOutputCP() != CP_UTF8);
335 #else
336 *(p->p_flag) = 1;
337 #endif
338 }
339 return (1);
340 }
341 }
342
343 if (!no_error) {
344 error("invalid charset name", NULL_PARG);
345 quit(QUIT_ERROR);
346 }
347 return (0);
348 }
349
350 #if HAVE_LOCALE
351 /*
352 * Define a charset, given a locale name.
353 */
ilocale(void)354 static void ilocale(void)
355 {
356 int c;
357
358 for (c = 0; c < (int) sizeof(chardef); c++)
359 {
360 if (isprint(c))
361 chardef[c] = 0;
362 else if (iscntrl(c))
363 chardef[c] = IS_CONTROL_CHAR;
364 else
365 chardef[c] = IS_BINARY_CHAR|IS_CONTROL_CHAR;
366 }
367 }
368 #endif
369
370 /*
371 * Define the printing format for control (or binary utf) chars.
372 */
setfmt(constant char * s,constant char ** fmtvarptr,int * attrptr,constant char * default_fmt,lbool for_printf)373 public void setfmt(constant char *s, constant char **fmtvarptr, int *attrptr, constant char *default_fmt, lbool for_printf)
374 {
375 if (s == NULL || *s == '\0')
376 s = default_fmt;
377 else if (for_printf &&
378 ((*s == '*' && (s[1] == '\0' || s[2] == '\0' || strchr(s + 2, 'n'))) ||
379 (*s != '*' && strchr(s, 'n'))))
380 /* %n is evil */
381 s = default_fmt;
382
383 /*
384 * Select the attributes if it starts with "*".
385 */
386 if (*s == '*' && s[1] != '\0')
387 {
388 switch (s[1])
389 {
390 case 'd': *attrptr = AT_BOLD; break;
391 case 'k': *attrptr = AT_BLINK; break;
392 case 's': *attrptr = AT_STANDOUT; break;
393 case 'u': *attrptr = AT_UNDERLINE; break;
394 default: *attrptr = AT_NORMAL; break;
395 }
396 s += 2;
397 }
398 *fmtvarptr = s;
399 }
400
401 /*
402 *
403 */
set_charset(void)404 static void set_charset(void)
405 {
406 constant char *s;
407
408 ichardef_utf(lgetenv("LESSUTFCHARDEF"));
409
410 /*
411 * See if environment variable LESSCHARSET is defined.
412 */
413 s = lgetenv("LESSCHARSET");
414 if (icharset(s, 0))
415 return;
416
417 /*
418 * LESSCHARSET is not defined: try LESSCHARDEF.
419 */
420 s = lgetenv("LESSCHARDEF");
421 if (!isnullenv(s))
422 {
423 ichardef(s);
424 return;
425 }
426
427 #if HAVE_LOCALE
428 #ifdef CODESET
429 /*
430 * Try using the codeset name as the charset name.
431 */
432 s = nl_langinfo(CODESET);
433 if (icharset(s, 1))
434 return;
435 #endif
436 #endif
437
438 #if HAVE_STRSTR
439 /*
440 * Check whether LC_ALL, LC_CTYPE or LANG look like UTF-8 is used.
441 */
442 if ((s = lgetenv("LC_ALL")) != NULL ||
443 (s = lgetenv("LC_CTYPE")) != NULL ||
444 (s = lgetenv("LANG")) != NULL)
445 {
446 if ( strstr(s, "UTF-8") != NULL || strstr(s, "utf-8") != NULL
447 || strstr(s, "UTF8") != NULL || strstr(s, "utf8") != NULL)
448 if (icharset("utf-8", 1))
449 return;
450 }
451 #endif
452
453 #if HAVE_LOCALE
454 /*
455 * Get character definitions from locale functions,
456 * rather than from predefined charset entry.
457 */
458 ilocale();
459 #else
460 #if MSDOS_COMPILER
461 #if MSDOS_COMPILER==WIN32C
462 (void) icharset("utf-8", 1);
463 #else
464 (void) icharset("dos", 1);
465 #endif
466 #else
467 (void) icharset("utf-8", 1);
468 #endif
469 #endif
470 }
471
472 /*
473 * Initialize charset data structures.
474 */
init_charset(void)475 public void init_charset(void)
476 {
477 constant char *s;
478
479 #if HAVE_LOCALE
480 setlocale(LC_ALL, "");
481 #endif
482
483 set_charset();
484
485 s = lgetenv("LESSBINFMT");
486 setfmt(s, &binfmt, &binattr, "*s<%02X>", TRUE);
487
488 s = lgetenv("LESSUTFBINFMT");
489 setfmt(s, &utfbinfmt, &binattr, "<U+%04lX>", TRUE);
490 }
491
492 /*
493 * Is a given character a "binary" character?
494 */
binary_char(LWCHAR c)495 public lbool binary_char(LWCHAR c)
496 {
497 if (utf_mode)
498 return (is_ubin_char(c));
499 if (c >= sizeof(chardef))
500 return TRUE;
501 return ((chardef[c] & IS_BINARY_CHAR) != 0);
502 }
503
504 /*
505 * Is a given character a "control" character?
506 */
control_char(LWCHAR c)507 public lbool control_char(LWCHAR c)
508 {
509 if (c >= sizeof(chardef))
510 return TRUE;
511 return (chardef[c] & IS_CONTROL_CHAR);
512 }
513
514 /*
515 * Return the printable form of a character.
516 * For example, in the "ascii" charset '\3' is printed as "^C".
517 */
prchar(LWCHAR c)518 public constant char * prchar(LWCHAR c)
519 {
520 /* {{ Fixed buffer size means LESSBINFMT etc can be truncated. }} */
521 static char buf[MAX_PRCHAR_LEN+1];
522
523 c &= 0377; /*{{type-issue}}*/
524 if ((c < 128 || !utf_mode) && !control_char(c))
525 SNPRINTF1(buf, sizeof(buf), "%c", (int) c);
526 else if (c == ESC)
527 strcpy(buf, "ESC");
528 #if IS_EBCDIC_HOST
529 else if (!binary_char(c) && c < 64)
530 SNPRINTF1(buf, sizeof(buf), "^%c",
531 /*
532 * This array roughly inverts CONTROL() #defined in less.h,
533 * and should be kept in sync with CONTROL() and IBM-1047.
534 */
535 "@ABC.I.?...KLMNO"
536 "PQRS.JH.XY.."
537 "\\]^_"
538 "......W[.....EFG"
539 "..V....D....TU.Z"[c]);
540 #else
541 else if (c < 128 && !control_char(c ^ 0100))
542 SNPRINTF1(buf, sizeof(buf), "^%c", (int) (c ^ 0100));
543 #endif
544 else
545 SNPRINTF1(buf, sizeof(buf), binfmt, c);
546 return (buf);
547 }
548
549 /*
550 * Return the printable form of a UTF-8 character.
551 */
prutfchar(LWCHAR ch)552 public constant char * prutfchar(LWCHAR ch)
553 {
554 static char buf[MAX_PRCHAR_LEN+1];
555
556 if (ch == ESC)
557 strcpy(buf, "ESC");
558 else if (ch < 128 && control_char(ch))
559 {
560 if (!control_char(ch ^ 0100))
561 SNPRINTF1(buf, sizeof(buf), "^%c", ((char) ch) ^ 0100);
562 else
563 SNPRINTF1(buf, sizeof(buf), binfmt, (char) ch);
564 } else if (is_ubin_char(ch) || is_omit_char(ch))
565 {
566 SNPRINTF1(buf, sizeof(buf), utfbinfmt, ch);
567 } else
568 {
569 char *p = buf;
570 if (ch >= 0x80000000)
571 ch = 0xFFFD; /* REPLACEMENT CHARACTER */
572 put_wchar(&p, ch);
573 *p = '\0';
574 }
575 return (buf);
576 }
577
578 /*
579 * Get the length of a UTF-8 character in bytes.
580 */
utf_len(char ch)581 public int utf_len(char ch)
582 {
583 if ((ch & 0x80) == 0)
584 return 1;
585 if ((ch & 0xE0) == 0xC0)
586 return 2;
587 if ((ch & 0xF0) == 0xE0)
588 return 3;
589 if ((ch & 0xF8) == 0xF0)
590 return 4;
591 #if 0
592 if ((ch & 0xFC) == 0xF8)
593 return 5;
594 if ((ch & 0xFE) == 0xFC)
595 return 6;
596 #endif
597 /* Invalid UTF-8 encoding. */
598 return 1;
599 }
600
601 /*
602 * Does the parameter point to the lead byte of a well-formed UTF-8 character?
603 */
is_utf8_well_formed(constant char * ss,int slen)604 public lbool is_utf8_well_formed(constant char *ss, int slen)
605 {
606 int i;
607 int len;
608 unsigned char s0 = (unsigned char) ss[0];
609
610 if (IS_UTF8_INVALID(s0))
611 return (FALSE);
612
613 len = utf_len(ss[0]);
614 if (len > slen)
615 return (FALSE);
616 if (len == 1)
617 return (TRUE);
618 if (len == 2)
619 {
620 if (s0 < 0xC2)
621 return (FALSE);
622 } else
623 {
624 unsigned char mask = (unsigned char) (~((1 << (8-len)) - 1));
625 if (s0 == mask && (ss[1] & mask) == 0x80)
626 return (FALSE);
627 }
628
629 for (i = 1; i < len; i++)
630 if (!IS_UTF8_TRAIL(ss[i]))
631 return (FALSE);
632 return (TRUE);
633 }
634
635 /*
636 * Skip bytes until a UTF-8 lead byte (11xxxxxx) or ASCII byte (0xxxxxxx) is found.
637 */
utf_skip_to_lead(constant char ** pp,constant char * limit)638 public void utf_skip_to_lead(constant char **pp, constant char *limit)
639 {
640 do {
641 ++(*pp);
642 } while (*pp < limit && !IS_UTF8_LEAD((*pp)[0] & 0377) && !IS_ASCII_OCTET((*pp)[0]));
643 }
644
645
646 /*
647 * Get the value of a UTF-8 character.
648 */
get_wchar(constant char * sp)649 public LWCHAR get_wchar(constant char *sp)
650 {
651 constant unsigned char *p = (constant unsigned char *) sp;
652 switch (utf_len(sp[0]))
653 {
654 case 1:
655 default:
656 /* 0xxxxxxx */
657 return (LWCHAR)
658 (p[0] & 0xFF);
659 case 2:
660 /* 110xxxxx 10xxxxxx */
661 return (LWCHAR) (
662 ((p[0] & 0x1F) << 6) |
663 (p[1] & 0x3F));
664 case 3:
665 /* 1110xxxx 10xxxxxx 10xxxxxx */
666 return (LWCHAR) (
667 ((p[0] & 0x0F) << 12) |
668 ((p[1] & 0x3F) << 6) |
669 (p[2] & 0x3F));
670 case 4:
671 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
672 return (LWCHAR) (
673 ((p[0] & 0x07) << 18) |
674 ((p[1] & 0x3F) << 12) |
675 ((p[2] & 0x3F) << 6) |
676 (p[3] & 0x3F));
677 #if 0
678 case 5:
679 /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
680 return (LWCHAR) (
681 ((p[0] & 0x03) << 24) |
682 ((p[1] & 0x3F) << 18) |
683 ((p[2] & 0x3F) << 12) |
684 ((p[3] & 0x3F) << 6) |
685 (p[4] & 0x3F));
686 case 6:
687 /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
688 return (LWCHAR) (
689 ((p[0] & 0x01) << 30) |
690 ((p[1] & 0x3F) << 24) |
691 ((p[2] & 0x3F) << 18) |
692 ((p[3] & 0x3F) << 12) |
693 ((p[4] & 0x3F) << 6) |
694 (p[5] & 0x3F));
695 #endif
696 }
697 }
698
699 /*
700 * Store a character into a UTF-8 string.
701 */
put_wchar(mutable char ** pp,LWCHAR ch)702 public void put_wchar(mutable char **pp, LWCHAR ch)
703 {
704 if (!utf_mode || ch < 0x80)
705 {
706 /* 0xxxxxxx */
707 *(*pp)++ = (char) ch;
708 } else if (ch < 0x800)
709 {
710 /* 110xxxxx 10xxxxxx */
711 *(*pp)++ = (char) (0xC0 | ((ch >> 6) & 0x1F));
712 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
713 } else if (ch < 0x10000)
714 {
715 /* 1110xxxx 10xxxxxx 10xxxxxx */
716 *(*pp)++ = (char) (0xE0 | ((ch >> 12) & 0x0F));
717 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
718 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
719 } else if (ch < 0x200000)
720 {
721 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
722 *(*pp)++ = (char) (0xF0 | ((ch >> 18) & 0x07));
723 *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
724 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
725 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
726 #if 0
727 } else if (ch < 0x4000000)
728 {
729 /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
730 *(*pp)++ = (char) (0xF0 | ((ch >> 24) & 0x03));
731 *(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
732 *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
733 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
734 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
735 } else
736 {
737 /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
738 *(*pp)++ = (char) (0xF0 | ((ch >> 30) & 0x01));
739 *(*pp)++ = (char) (0x80 | ((ch >> 24) & 0x3F));
740 *(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
741 *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
742 *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
743 *(*pp)++ = (char) (0x80 | (ch & 0x3F));
744 #endif
745 }
746 }
747
748 /*
749 * Step forward or backward one character in a string.
750 */
step_charc(constant char ** pp,signed int dir,constant char * limit)751 public LWCHAR step_charc(constant char **pp, signed int dir, constant char *limit)
752 {
753 LWCHAR ch;
754 int len;
755 constant char *p = *pp;
756
757 if (!utf_mode)
758 {
759 /* It's easy if chars are one byte. */
760 if (dir > 0)
761 ch = (LWCHAR) (unsigned char) ((p < limit) ? *p++ : 0);
762 else
763 ch = (LWCHAR) (unsigned char) ((p > limit) ? *--p : 0);
764 } else if (dir > 0)
765 {
766 if (p >= limit)
767 ch = 0;
768 else
769 {
770 len = utf_len(*p);
771 if (p + len > limit || !is_utf8_well_formed(p, len))
772 {
773 ch = (LWCHAR) (unsigned char) *p++;
774 } else
775 {
776 ch = get_wchar(p);
777 p += len;
778 }
779 }
780 } else
781 {
782 while (p > limit && IS_UTF8_TRAIL(p[-1]))
783 p--;
784 if (p <= limit)
785 ch = 0;
786 else
787 {
788 len = utf_len(*--p);
789 if (p + len != *pp || !is_utf8_well_formed(p, len))
790 {
791 p = *pp - 1;
792 ch = (LWCHAR) (unsigned char) *p;
793 } else
794 {
795 ch = get_wchar(p);
796 }
797 }
798 }
799 *pp = p;
800 return ch;
801 }
802
step_char(char ** pp,signed int dir,constant char * limit)803 public LWCHAR step_char(char **pp, signed int dir, constant char *limit)
804 {
805 constant char *p = (constant char *) *pp;
806 LWCHAR ch = step_charc(&p, dir, limit);
807 *pp = (char *) p;
808 return ch;
809 }
810
811 /*
812 * Unicode characters data
813 * Actual data is in the generated *.uni files.
814 */
815
816 #define DECLARE_RANGE_TABLE_START(name) \
817 static struct wchar_range name##_array[] = {
818 #define DECLARE_RANGE_TABLE_END(name) \
819 }; struct wchar_range_table name##_table = { name##_array, countof(name##_array) };
820
821 DECLARE_RANGE_TABLE_START(compose)
822 #include "compose.uni"
823 DECLARE_RANGE_TABLE_END(compose)
824
825 DECLARE_RANGE_TABLE_START(ubin)
826 #include "ubin.uni"
827 DECLARE_RANGE_TABLE_END(ubin)
828
829 DECLARE_RANGE_TABLE_START(wide)
830 #include "wide.uni"
831 DECLARE_RANGE_TABLE_END(wide)
832
833 DECLARE_RANGE_TABLE_START(fmt)
834 #include "fmt.uni"
835 DECLARE_RANGE_TABLE_END(fmt)
836
837 DECLARE_RANGE_TABLE_START(omit)
838 #include "omit.uni"
839 DECLARE_RANGE_TABLE_END(omit)
840
841 /* comb_table is special pairs, not ranges. */
842 static struct wchar_range comb_table[] = {
843 {0x0644,0x0622}, {0x0644,0x0623}, {0x0644,0x0625}, {0x0644,0x0627},
844 };
845
846
is_in_table(LWCHAR ch,struct wchar_range_table * table)847 static lbool is_in_table(LWCHAR ch, struct wchar_range_table *table)
848 {
849 unsigned int hi;
850 unsigned int lo;
851
852 /* Binary search in the table. */
853 if (table->table == NULL || table->count == 0 || ch < table->table[0].first)
854 return FALSE;
855 lo = 0;
856 hi = table->count - 1;
857 while (lo <= hi)
858 {
859 unsigned int mid = (lo + hi) / 2;
860 if (ch > table->table[mid].last)
861 lo = mid + 1;
862 else if (ch < table->table[mid].first)
863 hi = mid - 1;
864 else
865 return TRUE;
866 }
867 return FALSE;
868 }
869
870 /*
871 * Is a character in none of a set of specified user tables?
872 */
not_user_defined(LWCHAR ch,struct wchar_range_table * tbl1,struct wchar_range_table * tbl2,struct wchar_range_table * tbl3)873 static lbool not_user_defined(LWCHAR ch, struct wchar_range_table *tbl1, struct wchar_range_table *tbl2, struct wchar_range_table *tbl3)
874 {
875 if (is_in_table(ch, tbl1)) return FALSE;
876 if (is_in_table(ch, tbl2)) return FALSE;
877 if (is_in_table(ch, tbl3)) return FALSE;
878 return TRUE;
879 }
880
881 /*
882 * Is a character a UTF-8 composing character?
883 * If a composing character follows any char, the two combine into one glyph.
884 */
is_composing_char(LWCHAR ch)885 public lbool is_composing_char(LWCHAR ch)
886 {
887 if (is_in_table(ch, &user_prt_table)) return FALSE;
888 return is_in_table(ch, &user_compose_table) ||
889 (is_in_table(ch, &compose_table) ||
890 (bs_mode != BS_CONTROL && is_in_table(ch, &fmt_table) &&
891 not_user_defined(ch, &user_prt_table, &user_ubin_table, &user_omit_table)));
892 }
893
894 /*
895 * Should this UTF-8 character be treated as binary?
896 */
is_ubin_char(LWCHAR ch)897 public lbool is_ubin_char(LWCHAR ch)
898 {
899 if (is_in_table(ch, &user_prt_table)) return FALSE;
900 return is_in_table(ch, &user_ubin_table) ||
901 (is_in_table(ch, &ubin_table) ||
902 (bs_mode == BS_CONTROL && is_in_table(ch, &fmt_table) &&
903 not_user_defined(ch, &user_prt_table, &user_compose_table, &user_omit_table)));
904 }
905
906 /*
907 * Is this a double width UTF-8 character?
908 */
is_wide_char(LWCHAR ch)909 public lbool is_wide_char(LWCHAR ch)
910 {
911 return is_in_table(ch, &user_wide_table) ||
912 (is_in_table(ch, &wide_table) &&
913 not_user_defined(ch, &user_compose_table, &user_ubin_table, &user_omit_table));
914 }
915
916 /*
917 * Is this an omittable character?
918 */
is_omit_char(LWCHAR ch)919 public lbool is_omit_char(LWCHAR ch)
920 {
921 return is_in_table(ch, &user_omit_table) ||
922 (is_in_table(ch, &omit_table) &&
923 not_user_defined(ch, &user_prt_table, &user_compose_table, &user_ubin_table));
924 }
925
926 /*
927 * Is a character a UTF-8 combining character?
928 * A combining char acts like an ordinary char, but if it follows
929 * a specific char (not any char), the two combine into one glyph.
930 */
is_combining_char(LWCHAR ch1,LWCHAR ch2)931 public lbool is_combining_char(LWCHAR ch1, LWCHAR ch2)
932 {
933 /* The table is small; use linear search. */
934 int i;
935 for (i = 0; i < countof(comb_table); i++)
936 {
937 if (ch1 == comb_table[i].first &&
938 ch2 == comb_table[i].last)
939 return TRUE;
940 }
941 return FALSE;
942 }
943