Debian Codegraph @

garglk/cheapglk/cgunicod.cpp

raw symbols SCIP
1 /******************************************************************************
2 * *
3 * Copyright (C) 2006-2011 by Andrew Plotkin, Jesse McGrew. *
4 * Copyright (C) 2010 by Ben Cressey, Chris Spiegel. *
5 * *
6 * This file is part of Gargoyle. *
7 * *
8 * Gargoyle is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * Gargoyle is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with Gargoyle; if not, write to the Free Software *
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
21 * *
22 *****************************************************************************/
23
24 /* cgunicod.c: Unicode helper functions for Glk API, version 0.7.5.
25 Designed by Andrew Plotkin <[email protected]>
26 http://www.eblong.com/zarf/glk/index.html
27
28 Portions of this file are copyright (c) 1998-2016, Andrew Plotkin
29 It is distributed under the MIT license; see the "LICENSE" file.
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <memory.h>
35
36 #include "glk.h"
37 #include "garglk.h"
38
39 #define TRUE true
40 #define FALSE false
41
42 void gli_putchar_utf8(glui32 val, FILE *fl)
43 {
44 if (val < 0x80) {
45 putc(val, fl);
46 }
47 else if (val < 0x800) {
48 putc((0xC0 | ((val & 0x7C0) >> 6)), fl);
49 putc((0x80 | (val & 0x03F) ), fl);
50 }
51 else if (val < 0x10000) {
52 putc((0xE0 | ((val & 0xF000) >> 12)), fl);
53 putc((0x80 | ((val & 0x0FC0) >> 6)), fl);
54 putc((0x80 | (val & 0x003F) ), fl);
55 }
56 else if (val < 0x200000) {
57 putc((0xF0 | ((val & 0x1C0000) >> 18)), fl);
58 putc((0x80 | ((val & 0x03F000) >> 12)), fl);
59 putc((0x80 | ((val & 0x000FC0) >> 6)), fl);
60 putc((0x80 | (val & 0x00003F) ), fl);
61 }
62 else {
63 putc('?', fl);
64 }
65 }
66
67 int gli_encode_utf8(glui32 val, char *buf, int len)
68 {
69 /* return the number of bytes (actually) generated */
70 char *ptr = buf;
71 char *end = buf+len;
72
73 if (val < 0x80) {
74 if (ptr < end)
75 *ptr++ = val;
76 }
77 else if (val < 0x800) {
78 if (ptr < end)
79 *ptr++ = (0xC0 | ((val & 0x7C0) >> 6));
80 if (ptr < end)
81 *ptr++ = (0x80 | (val & 0x03F) );
82 }
83 else if (val < 0x10000) {
84 if (ptr < end)
85 *ptr++ = (0xE0 | ((val & 0xF000) >> 12));
86 if (ptr < end)
87 *ptr++ = (0x80 | ((val & 0x0FC0) >> 6));
88 if (ptr < end)
89 *ptr++ = (0x80 | (val & 0x003F) );
90 }
91 else if (val < 0x200000) {
92 if (ptr < end)
93 *ptr++ = (0xF0 | ((val & 0x1C0000) >> 18));
94 if (ptr < end)
95 *ptr++ = (0x80 | ((val & 0x03F000) >> 12));
96 if (ptr < end)
97 *ptr++ = (0x80 | ((val & 0x000FC0) >> 6));
98 if (ptr < end)
99 *ptr++ = (0x80 | (val & 0x00003F) );
100 }
101 else {
102 if (ptr < end)
103 *ptr++ = '?';
104 }
105
106 return (ptr - buf);
107 }
108
109 glui32 gli_parse_utf8(const unsigned char *buf, glui32 buflen,
110 glui32 *out, glui32 outlen)
111 {
112 glui32 pos = 0;
113 glui32 outpos = 0;
114 glui32 res;
115 glui32 val0, val1, val2, val3;
116
117 while (outpos < outlen) {
118 if (pos >= buflen)
119 break;
120
121 val0 = buf[pos++];
122
123 if (val0 < 0x80) {
124 res = val0;
125 out[outpos++] = res;
126 continue;
127 }
128
129 if ((val0 & 0xe0) == 0xc0) {
130 if (pos+1 > buflen) {
131 gli_strict_warning("incomplete two-byte character");
132 break;
133 }
134 val1 = buf[pos++];
135 if ((val1 & 0xc0) != 0x80) {
136 gli_strict_warning("malformed two-byte character");
137 break;
138 }
139 res = (val0 & 0x1f) << 6;
140 res |= (val1 & 0x3f);
141 out[outpos++] = res;
142 continue;
143 }
144
145 if ((val0 & 0xf0) == 0xe0) {
146 if (pos+2 > buflen) {
147 gli_strict_warning("incomplete three-byte character");
148 break;
149 }
150 val1 = buf[pos++];
151 val2 = buf[pos++];
152 if ((val1 & 0xc0) != 0x80) {
153 gli_strict_warning("malformed three-byte character");
154 break;
155 }
156 if ((val2 & 0xc0) != 0x80) {
157 gli_strict_warning("malformed three-byte character");
158 break;
159 }
160 res = (((val0 & 0xf)<<12) & 0x0000f000);
161 res |= (((val1 & 0x3f)<<6) & 0x00000fc0);
162 res |= (((val2 & 0x3f)) & 0x0000003f);
163 out[outpos++] = res;
164 continue;
165 }
166
167 if ((val0 & 0xf0) == 0xf0) {
168 if ((val0 & 0xf8) != 0xf0) {
169 gli_strict_warning("malformed four-byte character");
170 break;
171 }
172 if (pos+3 > buflen) {
173 gli_strict_warning("incomplete four-byte character");
174 break;
175 }
176 val1 = buf[pos++];
177 val2 = buf[pos++];
178 val3 = buf[pos++];
179 if ((val1 & 0xc0) != 0x80) {
180 gli_strict_warning("malformed four-byte character");
181 break;
182 }
183 if ((val2 & 0xc0) != 0x80) {
184 gli_strict_warning("malformed four-byte character");
185 break;
186 }
187 if ((val3 & 0xc0) != 0x80) {
188 gli_strict_warning("malformed four-byte character");
189 break;
190 }
191 res = (((val0 & 0x7)<<18) & 0x1c0000);
192 res |= (((val1 & 0x3f)<<12) & 0x03f000);
193 res |= (((val2 & 0x3f)<<6) & 0x000fc0);
194 res |= (((val3 & 0x3f)) & 0x00003f);
195 out[outpos++] = res;
196 continue;
197 }
198
199 gli_strict_warning("malformed character");
200 }
201
202 return outpos;
203 }
204
205 #ifdef GLK_MODULE_UNICODE
206
207 /* The cgunigen.c file is generated from Unicode data tables, and it's
208 sort of enormous. Feel free to implement all these case-changing and
209 normalization functions using your OS's native facilities. */
210
211 #include "cgunigen.c"
212
213 #define CASE_UPPER (0)
214 #define CASE_LOWER (1)
215 #define CASE_TITLE (2)
216 #define CASE_IDENT (3)
217
218 #define COND_ALL (0)
219 #define COND_LINESTART (1)
220
221 /* Apply a case change to the buffer. The len is the length of the buffer
222 array; numchars is the number of characters originally in it. (This
223 may be less than len.) The result will be clipped to fit len, but
224 the return value will be the full number of characters that the
225 converted string should have contained.
226 */
227 static glui32 gli_buffer_change_case(glui32 *buf, glui32 len,
228 glui32 numchars, int destcase, int cond, int changerest)
229 {
230 glui32 ix, jx;
231 glui32 *outbuf;
232 glui32 *newoutbuf;
233 glui32 outcount;
234 int dest_block_rest, dest_block_first;
235 int dest_spec_rest, dest_spec_first;
236
237 switch (cond) {
238 case COND_ALL:
239 dest_spec_rest = destcase;
240 dest_spec_first = destcase;
241 break;
242 case COND_LINESTART:
243 if (changerest)
244 dest_spec_rest = CASE_LOWER;
245 else
246 dest_spec_rest = CASE_IDENT;
247 dest_spec_first = destcase;
248 break;
249 }
250
251 dest_block_rest = dest_spec_rest;
252 if (dest_block_rest == CASE_TITLE)
253 dest_block_rest = CASE_UPPER;
254 dest_block_first = dest_spec_first;
255 if (dest_block_first == CASE_TITLE)
256 dest_block_first = CASE_UPPER;
257
258 newoutbuf = NULL;
259 outcount = 0;
260 outbuf = buf;
261
262 for (ix=0; ix<numchars; ix++) {
263 int target;
264 int isfirst;
265 glui32 res;
266 glui32 *special;
267 glui32 *ptr;
268 glui32 speccount;
269 glui32 ch = buf[ix];
270
271 isfirst = (ix == 0);
272
273 target = (isfirst ? dest_block_first : dest_block_rest);
274
275 if (target == CASE_IDENT) {
276 res = ch;
277 }
278 else {
279 gli_case_block_t *block;
280
281 GET_CASE_BLOCK(ch, &block);
282 if (!block)
283 res = ch;
284 else
285 res = block[ch & 0xFF][target];
286 }
287
288 if (res != 0xFFFFFFFF || res == ch) {
289 /* simple case */
290 if (outcount < len)
291 outbuf[outcount] = res;
292 outcount++;
293 continue;
294 }
295
296 target = (isfirst ? dest_spec_first : dest_spec_rest);
297
298 /* complicated cases */
299 GET_CASE_SPECIAL(ch, &special);
300 if (!special) {
301 gli_strict_warning("inconsistency in cgunigen.c");
302 continue;
303 }
304 ptr = &unigen_special_array[special[target]];
305 speccount = *(ptr++);
306
307 if (speccount == 1) {
308 /* simple after all */
309 if (outcount < len)
310 outbuf[outcount] = ptr[0];
311 outcount++;
312 continue;
313 }
314
315 /* Now we have to allocate a new buffer, if we haven't already. */
316 if (!newoutbuf) {
317 newoutbuf = static_cast<glui32 *>(malloc((len+1) * sizeof(glui32)));
318 if (!newoutbuf)
319 return 0;
320 if (outcount)
321 memcpy(newoutbuf, buf, outcount * sizeof(glui32));
322 outbuf = newoutbuf;
323 }
324
325 for (jx=0; jx<speccount; jx++) {
326 if (outcount < len)
327 outbuf[outcount] = ptr[jx];
328 outcount++;
329 }
330 }
331
332 if (newoutbuf) {
333 glui32 finallen = outcount;
334 if (finallen > len)
335 finallen = len;
336 if (finallen)
337 memcpy(buf, newoutbuf, finallen * sizeof(glui32));
338 free(newoutbuf);
339 }
340
341 return outcount;
342 }
343
344 glui32 glk_buffer_to_lower_case_uni(glui32 *buf, glui32 len,
345 glui32 numchars)
346 {
347 return gli_buffer_change_case(buf, len, numchars,
348 CASE_LOWER, COND_ALL, TRUE);
349 }
350
351 glui32 glk_buffer_to_upper_case_uni(glui32 *buf, glui32 len,
352 glui32 numchars)
353 {
354 return gli_buffer_change_case(buf, len, numchars,
355 CASE_UPPER, COND_ALL, TRUE);
356 }
357
358 glui32 glk_buffer_to_title_case_uni(glui32 *buf, glui32 len,
359 glui32 numchars, glui32 lowerrest)
360 {
361 return gli_buffer_change_case(buf, len, numchars,
362 CASE_TITLE, COND_LINESTART, lowerrest);
363 }
364
365 #endif /* GLK_MODULE_UNICODE */
366
367 #ifdef GLK_MODULE_UNICODE_NORM
368
369 /* We're relying on the fact that cgunigen.c has already been included.
370 So don't try to use GLK_MODULE_UNICODE_NORM without GLK_MODULE_UNICODE.
371 */
372
373 static glui32 combining_class(glui32 ch)
374 {
375 RETURN_COMBINING_CLASS(ch);
376 }
377
378 /* This returns a new buffer (possibly longer), containing the decomposed
379 form of the original buffer. The caller must free the returned buffer.
380 On exit, *numcharsref contains the size of the returned buffer.
381 The original buffer is unchanged.
382 */
383 static glui32 *gli_buffer_canon_decompose_uni(glui32 *buf,
384 glui32 *numcharsref)
385 {
386 /* The algorithm for the canonical decomposition of a string: For
387 each character, look up the decomposition in the decomp table.
388 Append the decomposition to the buffer. Finally, sort every
389 substring of the buffer which is made up of combining
390 characters (characters with a nonzero combining class). */
391
392 glui32 numchars = *numcharsref;
393 glui32 destsize = numchars * 2 + 16;
394 glui32 *dest = (glui32 *)malloc(destsize * sizeof(glui32));
395 glui32 destlen = 0;
396 glui32 ix, jx;
397 int anycombining = FALSE;
398
399 if (!dest)
400 return NULL;
401
402 for (ix=0; ix<numchars; ix++) {
403 glui32 ch = buf[ix];
404 gli_decomp_block_t *block;
405 glui32 count, pos;
406
407 if (combining_class(ch))
408 anycombining = TRUE;
409
410 GET_DECOMP_BLOCK(ch, &block);
411 if (block) {
412 block += (ch & 0xFF);
413 count = (*block)[0];
414 pos = (*block)[1];
415 }
416 else {
417 GET_DECOMP_SPECIAL(ch, &count, &pos);
418 }
419
420 if (!count) {
421 /* The simple case: this character doesn't decompose. Push
422 it straight into the destination. */
423 if (destlen >= destsize) {
424 destsize = destsize * 2;
425 dest = (glui32 *)realloc(dest, destsize * sizeof(glui32));
426 if (!dest)
427 return NULL;
428 }
429 dest[destlen] = ch;
430 destlen++;
431 continue;
432 }
433
434 /* Assume that a character with a decomposition has a
435 combining class somewhere in there. Not always true, but
436 it's simpler to assume it. */
437 anycombining = TRUE;
438
439 /* We now append count characters to the buffer, reading from
440 unigen_decomp_data[pos] onwards. None of these characters
441 are decomposable; that was already recursively expanded when
442 unigen_decomp_data was generated. */
443
444 if (destlen+count >= destsize) {
445 /* Okay, that wasn't enough. Expand more. */
446 destsize = destsize * 2 + count;
447 dest = (glui32 *)realloc(dest, destsize * sizeof(glui32));
448 if (!dest)
449 return NULL;
450 }
451 for (jx=0; jx<count; jx++) {
452 dest[destlen] = unigen_decomp_data[pos+jx];
453 destlen++;
454 }
455 }
456
457 if (anycombining) {
458 /* Now we sort groups of combining characters. This should be a
459 stable sort by the combining-class number. We're lazy and
460 nearly all groups are short, so we'll just bubble-sort. */
461 glui32 grpstart, grpend, kx;
462 ix = 0;
463 while (ix < destlen) {
464 if (!combining_class(dest[ix])) {
465 ix++;
466 continue;
467 }
468 if (ix >= destlen)
469 break;
470 grpstart = ix;
471 while (ix < destlen && combining_class(dest[ix]))
472 ix++;
473 grpend = ix;
474 if (grpend - grpstart >= 2) {
475 /* Sort this group. */
476 for (jx = grpend-1; jx > grpstart; jx--) {
477 for (kx = grpstart; kx < jx; kx++) {
478 if (combining_class(dest[kx]) > combining_class(dest[kx+1])) {
479 glui32 tmp = dest[kx];
480 dest[kx] = dest[kx+1];
481 dest[kx+1] = tmp;
482 }
483 }
484 }
485 }
486 }
487 }
488
489 *numcharsref = destlen;
490 return dest;
491 }
492
493 static glui32 check_composition(glui32 ch1, glui32 ch2)
494 {
495 RETURN_COMPOSITION(ch1, ch2);
496 }
497
498 /* This composes characters in the given buffer, in place. It returns the
499 number of characters in the result, which will be less than or equal
500 to len.
501 */
502 static glui32 gli_buffer_canon_compose_uni(glui32 *buf, glui32 len)
503 {
504 /* This algorithm is lifted from the Java sample code at
505 <http://www.unicode.org/reports/tr15/Normalizer.html>.
506 I apologize for the ugly.
507
508 Roughly, pos is the position of the last base character;
509 curch is that character in progress; ix is the next character
510 to write (which may fly ahead of pos, as we encounter a string of
511 combining chars); and jx is the position that we're scanning.
512 In the simplest case, jx and ix stay together, with pos one behind. */
513
514 glui32 curch, newch, curclass, newclass, res;
515 glui32 ix, jx, pos;
516
517 if (len == 0)
518 return 0;
519
520 pos = 0;
521 curch = buf[0];
522 curclass = combining_class(curch);
523 if (curclass)
524 curclass = 999; /* just in case the first character is a combiner */
525 ix = 1;
526 jx = ix;
527 while (1) {
528 if (jx >= len) {
529 buf[pos] = curch;
530 pos = ix;
531 break;
532 }
533 newch = buf[jx];
534 newclass = combining_class(newch);
535 res = check_composition(curch, newch);
536 if (res && (!curclass || curclass < newclass)) {
537 curch = res;
538 buf[pos] = curch;
539 }
540 else {
541 if (!newclass) {
542 pos = ix;
543 curch = newch;
544 }
545 curclass = newclass;
546 buf[ix] = newch;
547 ix++;
548 }
549 jx++;
550 }
551
552 return pos;
553 }
554
555 glui32 glk_buffer_canon_decompose_uni(glui32 *buf, glui32 len,
556 glui32 numchars)
557 {
558 glui32 *dest = gli_buffer_canon_decompose_uni(buf, &numchars);
559 glui32 newlen;
560
561 if (!dest)
562 return 0;
563
564 /* Copy the data back. */
565 newlen = numchars;
566 if (newlen > len)
567 newlen = len;
568 if (newlen)
569 memcpy(buf, dest, newlen * sizeof(glui32));
570 free(dest);
571
572 return numchars;
573 }
574
575 glui32 glk_buffer_canon_normalize_uni(glui32 *buf, glui32 len,
576 glui32 numchars)
577 {
578 glui32 newlen;
579 glui32 *dest = gli_buffer_canon_decompose_uni(buf, &numchars);
580
581 if (!dest)
582 return 0;
583
584 numchars = gli_buffer_canon_compose_uni(dest, numchars);
585
586 /* Copy the data back. */
587 newlen = numchars;
588 if (newlen > len)
589 newlen = len;
590 if (newlen)
591 memcpy(buf, dest, newlen * sizeof(glui32));
592 free(dest);
593
594 return numchars;
595 }
596
597 #endif /* GLK_MODULE_UNICODE_NORM */
598