Debian Codegraph @

libcpp/makeuname2c.cc

raw
1 /* Make uname2c.h from various sources.
2 Copyright (C) 2005-2025 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
8 later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
18
19 /* Run this program as
20 ./makeuname2c UnicodeData.txt NameAliases.txt > uname2c.h
21
22 This program generates 2 big arrays and 2 small ones.
23 The large ones are uname2c_dict, initialized by string literal
24 representing dictionary, and uname2c_tree, which is a space optimized
25 radix tree.
26 The format of the radix tree is:
27 byte 0 either 0x80 + (key[0] - ' ') (if key_len == 1)
28 or key_len (otherwise)
29 either of them ored with 0x40 if it has a codepoint
30 byte 1 LSB of offset into uname2c_dict for key (only if key_len > 1)
31 byte 2 MSB of offset into uname2c_dict for key (only if key_len > 1)
32 if key_len == 1, the above 2 bytes are omitted
33 byte 3 LSB of codepoint (only if it has a codepoint)
34 byte 4 middle byte of codepoint (ditto)
35 byte 5 MSB of codepoint (ditto), ored with 0x80 if node has children
36 ored with 0x40 if it doesn't have siblings
37 if it doesn't have a codepoint, the above 3 bytes are omitted
38 and we assume that the node has children
39 byte 6, 7, 8 uleb128 encoded offset to first child relative to the end
40 of the uleb128 (only if node has children)
41 byte 9 0xff (only if node doesn't have a codepoint and doesn't
42 have siblings)
43
44 For prefixes of Unicode NR1 or NR2 rule generated names, on a node
45 representing end of the prefix codepoint is 0xd800 + index into
46 uname2c_generated array with indexes into uname2c_pairs array of
47 code points (low, high) of the ranges terminated by single 0.
48 0xd800 is NR1 rule (Hangul syllables), rest are NR2 rules.
49 */
50
51 #include <assert.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <stdint.h>
55 #include <ctype.h>
56 #include <limits.h>
57 #include <stdarg.h>
58 #include <stdbool.h>
59 #include <stdlib.h>
60
61 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
62
63 #define NUM_CODE_POINTS 0x110000
64 #define MAX_CODE_POINT 0x10ffff
65 #define NO_VALUE 0xdc00
66 #define GENERATED 0xd800
67
68 struct entry { const char *name; unsigned long codepoint; };
69 static struct entry *entries;
70 static unsigned long num_allocated, num_entries;
71
72 /* Unicode 16.0 Table 4-8. */
73 struct generated {
74 const char *prefix;
75 /* max_high is a workaround for UnicodeData.txt inconsistencies
76 on a few CJK UNIFIED IDEOGRAPH- ranges where the "*, Last>"
77 entry is a few code points above the end of the range. */
78 unsigned long low, high, max_high;
79 int idx, ok;
80 };
81 static struct generated generated_ranges[] =
82 { { "HANGUL SYLLABLE ", 0xac00, 0xd7a3, 0, 0, 0 }, /* NR1 rule */
83 { "CJK UNIFIED IDEOGRAPH-", 0x3400, 0x4dbf, 0, 1, 0 }, /* NR2 rules */
84 { "CJK UNIFIED IDEOGRAPH-", 0x4e00, 0x9fff, 0, 1, 0 },
85 { "CJK UNIFIED IDEOGRAPH-", 0x20000, 0x2a6df, 0, 1, 0 },
86 { "CJK UNIFIED IDEOGRAPH-", 0x2a700, 0x2b739, 0, 1, 0 },
87 { "CJK UNIFIED IDEOGRAPH-", 0x2b740, 0x2b81d, 0, 1, 0 },
88 { "CJK UNIFIED IDEOGRAPH-", 0x2b820, 0x2cea1, 0, 1, 0 },
89 { "CJK UNIFIED IDEOGRAPH-", 0x2ceb0, 0x2ebe0, 0, 1, 0 },
90 { "CJK UNIFIED IDEOGRAPH-", 0x2ebf0, 0x2ee5d, 0, 1, 0 },
91 { "CJK UNIFIED IDEOGRAPH-", 0x30000, 0x3134a, 0, 1, 0 },
92 { "CJK UNIFIED IDEOGRAPH-", 0x31350, 0x323af, 0, 1, 0 },
93 { "EGYPTIAN HIEROGLYPH-", 0x13460, 0x143fa, 0, 2, 0 },
94 { "TANGUT IDEOGRAPH-", 0x17000, 0x187f7, 0, 3, 0 },
95 { "TANGUT IDEOGRAPH-", 0x18d00, 0x18d08, 0, 3, 0 },
96 { "KHITAN SMALL SCRIPT CHARACTER-", 0x18b00, 0x18cd5, 0, 4, 0 },
97 { "NUSHU CHARACTER-", 0x1b170, 0x1b2fb, 0, 5, 0 },
98 { "CJK COMPATIBILITY IDEOGRAPH-", 0xf900, 0xfa6d, 0, 6, 0 },
99 { "CJK COMPATIBILITY IDEOGRAPH-", 0xfa70, 0xfad9, 0, 6, 0 },
100 { "CJK COMPATIBILITY IDEOGRAPH-", 0x2f800, 0x2fa1d, 0, 6, 0 }
101 };
102
103 struct node {
104 struct node *sibling, *child;
105 const char *key;
106 size_t key_len, key_idx, node_size, size_sum, child_off;
107 unsigned long codepoint;
108 bool in_dict;
109 };
110 static struct node *root, **nodes;
111 static unsigned long num_nodes;
112 static size_t dict_size, tree_size, max_entry_len;
113 static char *dict;
114 static unsigned char *tree;
115
116 /* Die! */
117
118 static void
119 fail (const char *s, ...)
120 {
121 va_list ap;
122
123 va_start (ap, s);
124 vfprintf (stderr, s, ap);
125 va_end (ap);
126 fputc ('\n', stderr);
127 exit (1);
128 }
129
130 static void *
131 xmalloc (size_t size)
132 {
133 void *ret = malloc (size);
134
135 if (ret == NULL)
136 fail ("failed to allocate %ld bytes", (long) size);
137 return ret;
138 }
139
140 static void *
141 xrealloc (void *p, size_t size)
142 {
143 void *ret = p ? realloc (p, size) : malloc (size);
144
145 if (ret == NULL)
146 fail ("failed to allocate %ld bytes", (long) size);
147 return ret;
148 }
149
150 static int
151 entrycmp (const void *p1, const void *p2)
152 {
153 const struct entry *e1 = (const struct entry *) p1;
154 const struct entry *e2 = (const struct entry *) p2;
155 int ret = strcmp (e1->name, e2->name);
156
157 if (ret != 0)
158 return ret;
159 if (e1->codepoint < e2->codepoint)
160 return -1;
161 if (e1->codepoint > e2->codepoint)
162 return 1;
163 return 0;
164 }
165
166 static int
167 nodecmp (const void *p1, const void *p2)
168 {
169 const struct node *n1 = *(const struct node *const *) p1;
170 const struct node *n2 = *(const struct node *const *) p2;
171 if (n1->key_len > n2->key_len)
172 return -1;
173 if (n1->key_len < n2->key_len)
174 return 1;
175 return memcmp (n1->key, n2->key, n1->key_len);
176 }
177
178 /* Read UnicodeData.txt and fill in the 'decomp' table to be the
179 decompositions of characters for which both the character
180 decomposed and all the code points in the decomposition are valid
181 for some supported language version, and the 'all_decomp' table to
182 be the decompositions of all characters without those
183 constraints. */
184
185 static void
186 read_table (char *fname, bool aliases_p)
187 {
188 FILE *f = fopen (fname, "r");
189 const char *sname = aliases_p ? "NameAliases.txt" : "UnicodeData.txt";
190
191 if (!f)
192 fail ("opening %s", sname);
193 for (;;)
194 {
195 char line[256];
196 unsigned long codepoint;
197 const char *name, *aname;
198 char *l;
199 size_t i;
200
201 if (!fgets (line, sizeof (line), f))
202 break;
203 codepoint = strtoul (line, &l, 16);
204 if (l == line && aliases_p)
205 {
206 /* NameAliased.txt can contain comments and empty lines. */
207 if (*line == '#' || *line == '\n')
208 continue;
209 }
210 if (l == line || *l != ';')
211 fail ("parsing %s, reading code point", sname);
212 if (codepoint > MAX_CODE_POINT)
213 fail ("parsing %s, code point too large", sname);
214
215 name = l + 1;
216 do {
217 ++l;
218 } while (*l != ';');
219
220 aname = NULL;
221 if (aliases_p)
222 {
223 /* Ignore figment and abbreviation aliases. */
224 if (strcmp (l + 1, "correction\n") != 0
225 && strcmp (l + 1, "control\n") != 0
226 && strcmp (l + 1, "alternate\n") != 0)
227 continue;
228 i = ARRAY_SIZE (generated_ranges);
229 }
230 else
231 {
232 for (i = 0; i < ARRAY_SIZE (generated_ranges); ++i)
233 if (codepoint >= generated_ranges[i].low
234 && codepoint <= generated_ranges[i].max_high)
235 break;
236 if (i != ARRAY_SIZE (generated_ranges))
237 {
238 if (*name == '<' && l[-1] == '>')
239 {
240 if (codepoint == generated_ranges[i].low
241 && l - name >= 9
242 && memcmp (l - 8, ", First>", 8) == 0
243 && generated_ranges[i].ok == 0)
244 {
245 generated_ranges[i].ok = INT_MAX - 1;
246 aname = generated_ranges[i].prefix;
247 codepoint = GENERATED + generated_ranges[i].idx;
248 }
249 /* Unfortunately, UnicodeData.txt isn't consistent
250 with the Table 4-8 range endpoints in 3 cases,
251 the ranges are longer there by a few codepoints.
252 So use the max_high hack to avoid verification
253 failures. */
254 else if (codepoint == generated_ranges[i].max_high
255 && l - name >= 8
256 && memcmp (l - 7, ", Last>", 7) == 0
257 && generated_ranges[i].ok == INT_MAX - 1)
258 {
259 generated_ranges[i].ok = INT_MAX;
260 continue;
261 }
262 else
263 fail ("unexpected generated entry %lx %.*s",
264 codepoint, (int) (l - name), name);
265 }
266 else if (codepoint
267 == generated_ranges[i].low + generated_ranges[i].ok
268 && l - name == (strlen (generated_ranges[i].prefix)
269 + (name - 1 - line))
270 && memcmp (name, generated_ranges[i].prefix,
271 strlen (generated_ranges[i].prefix)) == 0
272 && memcmp (name + strlen (generated_ranges[i].prefix),
273 line, name - 1 - line) == 0)
274 {
275 ++generated_ranges[i].ok;
276 if (codepoint != generated_ranges[i].low)
277 continue;
278 aname = generated_ranges[i].prefix;
279 codepoint = GENERATED + generated_ranges[i].idx;
280 }
281 else
282 fail ("unexpected generated entry %lx %.*s",
283 codepoint, (int) (l - name), name);
284 if (aname == generated_ranges[i].prefix)
285 {
286 size_t j;
287
288 /* Don't add an entry for a generated range where the
289 same prefix has been added already. */
290 for (j = 0; j < i; ++j)
291 if (generated_ranges[j].idx == generated_ranges[i].idx
292 && generated_ranges[j].ok != 0)
293 break;
294 if (j < i)
295 continue;
296 }
297 }
298 else if (*name == '<' && l[-1] == '>')
299 continue;
300 }
301
302 if (num_entries == num_allocated)
303 {
304 num_allocated = num_allocated ? 2 * num_allocated : 65536;
305 entries = (struct entry *) xrealloc (entries, num_allocated
306 * sizeof (entries[0]));
307 }
308
309 if (aname == NULL)
310 {
311 char *a = (char *) xmalloc (l + 1 - name);
312 if (l - name > max_entry_len)
313 max_entry_len = l - name;
314 memcpy (a, name, l - name);
315 a[l - name] = '\0';
316 aname = a;
317 }
318 entries[num_entries].name = aname;
319 entries[num_entries++].codepoint = codepoint;
320 }
321 if (ferror (f))
322 fail ("reading %s", sname);
323 fclose (f);
324 }
325
326 /* Assumes nodes are added from sorted array, so we never
327 add any node before existing one, only after it. */
328
329 static void
330 node_add (struct node **p, const char *key, size_t key_len,
331 unsigned long codepoint)
332 {
333 struct node *n;
334 size_t i;
335
336 do
337 {
338 if (*p == NULL)
339 {
340 *p = n = (struct node *) xmalloc (sizeof (struct node));
341 ++num_nodes;
342 assert (key_len);
343 n->sibling = NULL;
344 n->child = NULL;
345 n->key = key;
346 n->key_len = key_len;
347 n->codepoint = codepoint;
348 return;
349 }
350 n = *p;
351 for (i = 0; i < n->key_len && i < key_len; ++i)
352 if (n->key[i] != key[i])
353 break;
354 if (i == 0)
355 {
356 p = &n->sibling;
357 continue;
358 }
359 if (i == n->key_len)
360 {
361 assert (key_len > n->key_len);
362 p = &n->child;
363 key += n->key_len;
364 key_len -= n->key_len;
365 continue;
366 }
367 /* Need to split the node. */
368 assert (i < key_len);
369 n = (struct node *) xmalloc (sizeof (struct node));
370 ++num_nodes;
371 n->sibling = NULL;
372 n->child = (*p)->child;
373 n->key = (*p)->key + i;
374 n->key_len = (*p)->key_len - i;
375 n->codepoint = (*p)->codepoint;
376 (*p)->child = n;
377 (*p)->key_len = i;
378 (*p)->codepoint = NO_VALUE;
379 key += i;
380 key_len -= i;
381 p = &n->sibling;
382 }
383 while (1);
384 }
385
386 static void
387 append_nodes (struct node *n)
388 {
389 for (; n; n = n->sibling)
390 {
391 nodes[num_nodes++] = n;
392 append_nodes (n->child);
393 }
394 }
395
396 static size_t
397 sizeof_uleb128 (size_t val)
398 {
399 size_t sz = 0;
400 do
401 {
402 val >>= 7;
403 sz += 1;
404 }
405 while (val != 0);
406 return sz;
407 }
408
409 static void
410 size_nodes (struct node *n)
411 {
412 if (n->child)
413 size_nodes (n->child);
414 if (n->sibling)
415 size_nodes (n->sibling);
416 n->node_size = 1 + (n->key_len > 1) * 2;
417 if (n->codepoint != NO_VALUE)
418 n->node_size += 3;
419 else if (n->sibling == NULL)
420 ++n->node_size;
421 n->size_sum = 0;
422 n->child_off = 0;
423 if (n->sibling)
424 n->size_sum += n->sibling->size_sum;
425 if (n->child)
426 {
427 n->child_off = n->size_sum + (n->codepoint == NO_VALUE
428 && n->sibling == NULL);
429 n->node_size += sizeof_uleb128 (n->child_off);
430 }
431 n->size_sum += n->node_size;
432 if (n->child)
433 n->size_sum += n->child->size_sum;
434 tree_size += n->node_size;
435 }
436
437 static void
438 write_uleb128 (unsigned char *p, size_t val)
439 {
440 unsigned char c;
441 do
442 {
443 c = val & 0x7f;
444 val >>= 7;
445 if (val)
446 c |= 0x80;
447 *p++ = c;
448 }
449 while (val);
450 }
451
452 static void
453 write_nodes (struct node *n, size_t off)
454 {
455 for (; n; n = n->sibling)
456 {
457 assert (off < tree_size && tree[off] == 0);
458 if (n->key_len > 1)
459 {
460 assert (n->key_len < 64);
461 tree[off] = n->key_len;
462 }
463 else
464 tree[off] = (n->key[0] - ' ') | 0x80;
465 assert ((tree[off] & 0x40) == 0);
466 if (n->codepoint != NO_VALUE)
467 tree[off] |= 0x40;
468 off++;
469 if (n->key_len > 1)
470 {
471 tree[off++] = n->key_idx & 0xff;
472 tree[off++] = (n->key_idx >> 8) & 0xff;
473 }
474 if (n->codepoint != NO_VALUE)
475 {
476 assert (n->codepoint < (1L << 21));
477 tree[off++] = n->codepoint & 0xff;
478 tree[off++] = (n->codepoint >> 8) & 0xff;
479 tree[off] = (n->codepoint >> 16) & 0xff;
480 if (n->child)
481 tree[off] |= 0x80;
482 if (!n->sibling)
483 tree[off] |= 0x40;
484 off++;
485 }
486 if (n->child)
487 {
488 write_uleb128 (&tree[off], n->child_off);
489 off += sizeof_uleb128 (n->child_off);
490 write_nodes (n->child, off + n->child_off);
491 }
492 if (n->codepoint == NO_VALUE
493 && n->sibling == NULL)
494 tree[off++] = 0xff;
495 }
496 assert (off <= tree_size);
497 }
498
499 static void
500 build_radix_tree (void)
501 {
502 size_t i, j, k, key_idx;
503
504 for (i = 0; i < ARRAY_SIZE (generated_ranges); ++i)
505 if (generated_ranges[i].ok == INT_MAX)
506 {
507 if (generated_ranges[i].max_high - generated_ranges[i].high > 15UL)
508 break;
509 }
510 else if (generated_ranges[i].ok == (generated_ranges[i].high
511 - generated_ranges[i].low + 1))
512 {
513 if (generated_ranges[i].max_high != generated_ranges[i].high)
514 break;
515 }
516 else
517 break;
518 if (i < ARRAY_SIZE (generated_ranges))
519 fail ("uncovered generated range %s %lx %lx",
520 generated_ranges[i].prefix, generated_ranges[i].low,
521 generated_ranges[i].high);
522 /* Sort entries alphabetically, node_add relies on that. */
523 qsort (entries, num_entries, sizeof (struct entry), entrycmp);
524 for (i = 1; i < num_entries; ++i)
525 if (i && strcmp (entries[i].name, entries[i - 1].name) == 0)
526 fail ("multiple entries for name %s", entries[i].name);
527
528 for (i = 0; i < num_entries; ++i)
529 node_add (&root, entries[i].name, strlen (entries[i].name),
530 entries[i].codepoint);
531
532 nodes = (struct node **) xmalloc (num_nodes * sizeof (struct node *));
533 i = num_nodes;
534 num_nodes = 0;
535 append_nodes (root);
536 assert (num_nodes == i);
537 /* Sort node pointers by decreasing string length to handle substrings
538 right. */
539 qsort (nodes, num_nodes, sizeof (struct node *), nodecmp);
540 if (nodes[0]->key_len >= 64)
541 /* We could actually encode even 64 and 65, as key_len 0 and 1 will
542 never appear in the multiple letter key encodings, so could subtract
543 2. */
544 fail ("can't encode key length %d >= 64, so need to split some radix "
545 "tree nodes to ensure length fits", nodes[0]->key_len);
546
547 /* Verify a property charset.cc UAX44-LM2 matching relies on:
548 if - is at the end of key of some node, then all its siblings
549 start with alphanumeric characters.
550 Only 2 character names and 1 alias have - followed by space:
551 U+0F0A TIBETAN MARK BKA- SHOG YIG MGO
552 U+0FD0 TIBETAN MARK BKA- SHOG GI MGO RGYAN
553 U+0FD0 TIBETAN MARK BSKA- SHOG GI MGO RGYAN
554 so the KA- in there will always be followed at least by SHOG
555 in the same node.
556 If this changes, charset.cc needs to change. */
557 for (i = 0; i < num_nodes; ++i)
558 if (nodes[i]->key[nodes[i]->key_len - 1] == '-'
559 && nodes[i]->child)
560 {
561 struct node *n;
562
563 for (n = nodes[i]->child; n; n = n->sibling)
564 if (n->key[0] == ' ')
565 fail ("node with key %.*s followed by node with key %.*s",
566 (int) nodes[i]->key_len, nodes[i]->key,
567 (int) n->key_len, n->key);
568 }
569
570 /* This is expensive, O(num_nodes * num_nodes * nodes[0]->key_len), but
571 fortunately num_nodes is < 64K and key_len < 64. */
572 key_idx = 0;
573 for (i = 0; i < num_nodes; ++i)
574 {
575 nodes[i]->key_idx = SIZE_MAX;
576 nodes[i]->in_dict = false;
577 if (nodes[i]->key_len > 1)
578 {
579 for (j = 0; j < i; ++j)
580 /* Can't rely on memmem unfortunately. */
581 if (nodes[j]->in_dict)
582 {
583 for (k = 0; k <= nodes[j]->key_len - nodes[i]->key_len; ++k)
584 if (nodes[j]->key[k] == nodes[i]->key[0]
585 && memcmp (nodes[j]->key + k + 1, nodes[i]->key + 1,
586 nodes[i]->key_len - 1) == 0)
587 {
588 nodes[i]->key_idx = nodes[j]->key_idx + k;
589 j = i;
590 break;
591 }
592 if (j == i)
593 break;
594 for (; k < nodes[j]->key_len; ++k)
595 if (nodes[j]->key[k] == nodes[i]->key[0]
596 && memcmp (nodes[j]->key + k + 1, nodes[i]->key + 1,
597 nodes[j]->key_len - 1 - k) == 0)
598 {
599 size_t l;
600
601 for (l = j + 1; l < i; ++l)
602 if (nodes[l]->in_dict)
603 break;
604 if (l < i
605 && memcmp (nodes[l]->key,
606 nodes[i]->key + (nodes[j]->key_len - k),
607 nodes[i]->key_len
608 - (nodes[j]->key_len - k)) == 0)
609 {
610 nodes[i]->key_idx = nodes[j]->key_idx + k;
611 j = i;
612 }
613 else
614 j = l - 1;
615 break;
616 }
617 }
618 if (nodes[i]->key_idx == SIZE_MAX)
619 {
620 nodes[i]->key_idx = key_idx;
621 nodes[i]->in_dict = true;
622 key_idx += nodes[i]->key_len;
623 }
624 }
625 }
626 if (key_idx >= 65536)
627 /* We only use 2 bytes for offsets into the dictionary.
628 If it grows more, there is e.g. a possibility to replace
629 most often seen words or substrings in the dictionary
630 with characters other than [A-Z0-9 -] (say LETTER occurs
631 in the dictionary almost 197 times and so by using a
632 instead of LETTER we could save (6 - 1) * 197 bytes,
633 with some on the side table mapping 'a' to "LETTER". */
634 fail ("too large dictionary %ld", (long) key_idx);
635 dict_size = key_idx;
636
637 size_nodes (root);
638
639 dict = (char *) xmalloc (dict_size + 1);
640 for (i = 0; i < num_nodes; ++i)
641 if (nodes[i]->in_dict)
642 memcpy (dict + nodes[i]->key_idx, nodes[i]->key, nodes[i]->key_len);
643 dict[dict_size] = '\0';
644
645 tree = (unsigned char *) xmalloc (tree_size);
646 memset (tree, 0, tree_size);
647 write_nodes (root, 0);
648 }
649
650 /* Print out the huge copyright notice. */
651
652 static void
653 write_copyright (void)
654 {
655 static const char copyright[] = "\
656 /* Unicode name to codepoint.\n\
657 Copyright (C) 2005-2025 Free Software Foundation, Inc.\n\
658 \n\
659 This program is free software; you can redistribute it and/or modify it\n\
660 under the terms of the GNU General Public License as published by the\n\
661 Free Software Foundation; either version 3, or (at your option) any\n\
662 later version.\n\
663 \n\
664 This program is distributed in the hope that it will be useful,\n\
665 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
666 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
667 GNU General Public License for more details.\n\
668 \n\
669 You should have received a copy of the GNU General Public License\n\
670 along with this program; see the file COPYING3. If not see\n\
671 <http://www.gnu.org/licenses/>.\n\
672 \n\
673 \n\
674 Copyright (C) 1991-2024 Unicode, Inc. All rights reserved.\n\
675 Distributed under the Terms of Use in\n\
676 http://www.unicode.org/copyright.html.\n\
677 \n\
678 Permission is hereby granted, free of charge, to any person\n\
679 obtaining a copy of the Unicode data files and any associated\n\
680 documentation (the \"Data Files\") or Unicode software and any\n\
681 associated documentation (the \"Software\") to deal in the Data Files\n\
682 or Software without restriction, including without limitation the\n\
683 rights to use, copy, modify, merge, publish, distribute, and/or\n\
684 sell copies of the Data Files or Software, and to permit persons to\n\
685 whom the Data Files or Software are furnished to do so, provided\n\
686 that (a) the above copyright notice(s) and this permission notice\n\
687 appear with all copies of the Data Files or Software, (b) both the\n\
688 above copyright notice(s) and this permission notice appear in\n\
689 associated documentation, and (c) there is clear notice in each\n\
690 modified Data File or in the Software as well as in the\n\
691 documentation associated with the Data File(s) or Software that the\n\
692 data or software has been modified.\n\
693 \n\
694 THE DATA FILES AND SOFTWARE ARE PROVIDED \"AS IS\", WITHOUT WARRANTY\n\
695 OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE\n\
696 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n\
697 NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE\n\
698 COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR\n\
699 ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY\n\
700 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,\n\
701 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\n\
702 ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE\n\
703 OF THE DATA FILES OR SOFTWARE.\n\
704 \n\
705 Except as contained in this notice, the name of a copyright holder\n\
706 shall not be used in advertising or otherwise to promote the sale,\n\
707 use or other dealings in these Data Files or Software without prior\n\
708 written authorization of the copyright holder. */\n";
709
710 puts (copyright);
711 }
712
713 static void
714 write_dict (void)
715 {
716 size_t i;
717
718 printf ("static const char uname2c_dict[%ld] =\n", (long) (dict_size + 1));
719 for (i = 0; i < dict_size; i += 77)
720 printf ("\"%.77s\"%s\n", dict + i, i + 76 > dict_size ? ";" : "");
721 puts ("");
722 }
723
724 static void
725 write_tree (void)
726 {
727 size_t i, j;
728
729 printf ("static const unsigned char uname2c_tree[%ld] = {\n",
730 (long) tree_size);
731 for (i = 0, j = 0; i < tree_size; ++i)
732 {
733 printf ("%s0x%02x%s", j == 0 ? " " : "", tree[i],
734 i == tree_size - 1 ? " };\n\n" : j == 11 ? ",\n" : ", ");
735 if (j == 11)
736 j = 0;
737 else
738 ++j;
739 }
740 }
741
742 static void
743 write_generated (void)
744 {
745 size_t i, j;
746
747 puts ("static const cppchar_t uname2c_pairs[] = {");
748 for (i = 0; i < ARRAY_SIZE (generated_ranges); ++i)
749 {
750 if (i == 0)
751 ;
752 else if (generated_ranges[i - 1].idx != generated_ranges[i].idx)
753 puts (", 0,");
754 else
755 puts (",");
756 printf (" 0x%lx, 0x%lx /* %s */",
757 generated_ranges[i].low,
758 generated_ranges[i].high,
759 generated_ranges[i].prefix);
760 }
761 puts (", 0 };\n");
762
763 puts ("static const unsigned char uname2c_generated[] = {");
764 for (i = 0, j = -1; i < ARRAY_SIZE (generated_ranges); ++i)
765 {
766 if (i == 0 || generated_ranges[i - 1].idx != generated_ranges[i].idx)
767 printf ("%s %d /* %s */", i ? ",\n" : "",
768 ++j, generated_ranges[i].prefix);
769 j += 2;
770 }
771 puts (" };\n");
772 }
773
774 /* Main program. */
775
776 int
777 main (int argc, char **argv)
778 {
779 size_t i;
780
781 if (argc != 3)
782 fail ("too few arguments to makeradixtree");
783 for (i = 0; i < ARRAY_SIZE (generated_ranges); ++i)
784 if (!generated_ranges[i].max_high)
785 generated_ranges[i].max_high = generated_ranges[i].high;
786 read_table (argv[1], false);
787 read_table (argv[2], true);
788 build_radix_tree ();
789
790 write_copyright ();
791 write_dict ();
792 write_tree ();
793 write_generated ();
794 printf ("static const unsigned int uname2c_max_name_len = %ld;\n\n", max_entry_len);
795 return 0;
796 }