2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Copyright 2001 Computing Research Labs, New Mexico State University
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
24 * The above copyright notice and this permission notice shall be included in
25 * all copies or substantial portions of the Software.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
31 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
32 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
33 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 /* $Id: ucgendat.c,v 1.4 2001/01/02 18:46:20 mleisher Exp $" */
38 #include "ldap_config.h"
41 #include <ac/stdlib.h>
42 #include <ac/string.h>
43 #include <ac/unistd.h>
50 #define HARDCODE_DATA 1
54 #define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\
55 ((cc) >= 'A' && (cc) <= 'F') ||\
56 ((cc) >= 'a' && (cc) <= 'f'))
59 * A header written to the output file with the byte-order-mark and the number
62 static ac_uint2 hdr[2] = {0xfeff, 0};
65 #define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3)))
73 * List of properties expected to be found in the Unicode Character Database
74 * including some implementation specific properties.
76 * The implementation specific properties are:
77 * Cm = Composed (can be decomposed)
79 * Sy = Symmetric (has left and right forms)
84 * Cp = Defined character
86 static _prop_t props[NUMPROPS] = {
87 {"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2},
88 {"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2},
89 {"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2},
90 {"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2},
91 {"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L", 1}, {"R", 1},
92 {"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B", 1},
93 {"S", 1}, {"WS", 2}, {"ON", 2},
94 {"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2},
95 {"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2}, {"AL", 2}
104 static _ranges_t proptbl[NUMPROPS];
107 * Make sure this array is sized to be on a 4-byte boundary at compile time.
109 static ac_uint2 propcnt[NEEDPROPS];
112 * Array used to collect a decomposition before adding it to the decomposition
115 static ac_uint4 dectmp[64];
116 static ac_uint4 dectmp_size;
126 * List of decomposition. Created and expanded in order as the characters are
127 * encountered. First list contains canonical mappings, second also includes
128 * compatibility mappings.
130 static _decomp_t *decomps;
131 static ac_uint4 decomps_used;
132 static ac_uint4 decomps_size;
134 static _decomp_t *kdecomps;
135 static ac_uint4 kdecomps_used;
136 static ac_uint4 kdecomps_size;
139 * Composition exclusion table stuff.
141 #define COMPEX_SET(c) (compexs[(c) >> 5] |= (1 << ((c) & 31)))
142 #define COMPEX_TEST(c) (compexs[(c) >> 5] & (1 << ((c) & 31)))
143 static ac_uint4 compexs[8192];
146 * Struct for holding a composition pair, and array of composition pairs
155 static _comp_t *comps;
156 static ac_uint4 comps_used;
159 * Types and lists for handling lists of case mappings.
167 static _case_t *upper;
168 static _case_t *lower;
169 static _case_t *title;
170 static ac_uint4 upper_used;
171 static ac_uint4 upper_size;
172 static ac_uint4 lower_used;
173 static ac_uint4 lower_size;
174 static ac_uint4 title_used;
175 static ac_uint4 title_size;
178 * Array used to collect case mappings before adding them to a list.
180 static ac_uint4 cases[3];
183 * An array to hold ranges for combining classes.
185 static ac_uint4 *ccl;
186 static ac_uint4 ccl_used;
187 static ac_uint4 ccl_size;
190 * Structures for handling numbers.
203 * Arrays to hold the mapping of codes to numbers.
205 static _codeidx_t *ncodes;
206 static ac_uint4 ncodes_used;
207 static ac_uint4 ncodes_size;
210 static ac_uint4 nums_used;
211 static ac_uint4 nums_size;
214 * Array for holding numbers.
217 static ac_uint4 nums_used;
218 static ac_uint4 nums_size;
221 add_range(ac_uint4 start, ac_uint4 end, char *p1, char *p2)
227 for (k = 0; k < 2; k++) {
239 for (i = 0; i < NUMPROPS; i++) {
240 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
250 * Resize the range list if necessary.
252 if (rlp->used == rlp->size) {
254 rlp->ranges = (ac_uint4 *)
255 malloc(sizeof(ac_uint4) << 3);
257 rlp->ranges = (ac_uint4 *)
258 realloc((char *) rlp->ranges,
259 sizeof(ac_uint4) * (rlp->size + 8));
264 * If this is the first code for this property list, just add it
267 if (rlp->used == 0) {
268 rlp->ranges[0] = start;
269 rlp->ranges[1] = end;
275 * Optimize the case of adding the range to the end.
278 if (start > rlp->ranges[j]) {
280 rlp->ranges[j++] = start;
281 rlp->ranges[j++] = end;
287 * Need to locate the insertion point.
290 i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
293 * If the start value lies in the current range, then simply set the
294 * new end point of the range to the end value passed as a parameter.
296 if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
297 rlp->ranges[i + 1] = end;
302 * Shift following values up by two.
304 for (j = rlp->used; j > i; j -= 2) {
305 rlp->ranges[j] = rlp->ranges[j - 2];
306 rlp->ranges[j + 1] = rlp->ranges[j - 1];
310 * Add the new range at the insertion point.
312 rlp->ranges[i] = start;
313 rlp->ranges[i + 1] = end;
319 ordered_range_insert(ac_uint4 c, char *name, int len)
329 * Deal with directionality codes introduced in Unicode 3.0.
331 if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
333 (memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
334 memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
335 memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
337 * Mark all of these as Other Neutral to preserve compatibility with
344 for (i = 0; i < NUMPROPS; i++) {
345 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
353 * Have a match, so insert the code in order.
358 * Resize the range list if necessary.
360 if (rlp->used == rlp->size) {
362 rlp->ranges = (ac_uint4 *)
363 malloc(sizeof(ac_uint4) << 3);
365 rlp->ranges = (ac_uint4 *)
366 realloc((char *) rlp->ranges,
367 sizeof(ac_uint4) * (rlp->size + 8));
372 * If this is the first code for this property list, just add it
375 if (rlp->used == 0) {
376 rlp->ranges[0] = rlp->ranges[1] = c;
382 * Optimize the cases of extending the last range and adding new ranges to
387 s = rlp->ranges[j - 1];
391 * Extend the last range.
399 * Start another range on the end.
402 rlp->ranges[j] = rlp->ranges[j + 1] = c;
409 * The code is a duplicate of a code in the last range, so just return.
414 * The code should be inserted somewhere before the last range in the
415 * list. Locate the insertion point.
418 i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
421 e = rlp->ranges[i + 1];
425 * Simply extend the current range.
427 rlp->ranges[i + 1] = c;
430 * Add a new entry before the current location. Shift all entries
431 * before the current one up by one to make room.
433 for (j = rlp->used; j > i; j -= 2) {
434 rlp->ranges[j] = rlp->ranges[j - 2];
435 rlp->ranges[j + 1] = rlp->ranges[j - 1];
437 rlp->ranges[i] = rlp->ranges[i + 1] = c;
444 add_decomp(ac_uint4 code, short compat)
447 _decomp_t **pdecomps;
448 ac_uint4 *pdecomps_used;
449 ac_uint4 *pdecomps_size;
452 pdecomps = &kdecomps;
453 pdecomps_used = &kdecomps_used;
454 pdecomps_size = &kdecomps_size;
457 pdecomps_used = &decomps_used;
458 pdecomps_size = &decomps_size;
462 * Add the code to the composite property.
465 ordered_range_insert(code, "Cm", 2);
469 * Locate the insertion point for the code.
471 for (i = 0; i < *pdecomps_used && code > (*pdecomps)[i].code; i++) ;
474 * Allocate space for a new decomposition.
476 if (*pdecomps_used == *pdecomps_size) {
477 if (*pdecomps_size == 0)
478 *pdecomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
480 *pdecomps = (_decomp_t *)
481 realloc((char *) *pdecomps,
482 sizeof(_decomp_t) * (*pdecomps_size + 8));
483 (void) memset((char *) (*pdecomps + *pdecomps_size), '\0',
484 sizeof(_decomp_t) << 3);
488 if (i < *pdecomps_used && code != (*pdecomps)[i].code) {
490 * Shift the decomps up by one if the codes don't match.
492 for (j = *pdecomps_used; j > i; j--)
493 (void) AC_MEMCPY((char *) &(*pdecomps)[j], (char *) &(*pdecomps)[j - 1],
498 * Insert or replace a decomposition.
500 size = dectmp_size + (4 - (dectmp_size & 3));
501 if ((*pdecomps)[i].size < size) {
502 if ((*pdecomps)[i].size == 0)
503 (*pdecomps)[i].decomp = (ac_uint4 *)
504 malloc(sizeof(ac_uint4) * size);
506 (*pdecomps)[i].decomp = (ac_uint4 *)
507 realloc((char *) (*pdecomps)[i].decomp,
508 sizeof(ac_uint4) * size);
509 (*pdecomps)[i].size = size;
512 if ((*pdecomps)[i].code != code)
515 (*pdecomps)[i].code = code;
516 (*pdecomps)[i].used = dectmp_size;
517 (void) AC_MEMCPY((char *) (*pdecomps)[i].decomp, (char *) dectmp,
518 sizeof(ac_uint4) * dectmp_size);
521 * NOTICE: This needs changing later so it is more general than simply
522 * pairs. This calculation is done here to simplify allocation elsewhere.
524 if (!compat && dectmp_size == 2)
529 add_title(ac_uint4 code)
534 * Always map the code to itself.
538 if (title_used == title_size) {
540 title = (_case_t *) malloc(sizeof(_case_t) << 3);
542 title = (_case_t *) realloc((char *) title,
543 sizeof(_case_t) * (title_size + 8));
548 * Locate the insertion point.
550 for (i = 0; i < title_used && code > title[i].key; i++) ;
552 if (i < title_used) {
554 * Shift the array up by one.
556 for (j = title_used; j > i; j--)
557 (void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
561 title[i].key = cases[2]; /* Title */
562 title[i].other1 = cases[0]; /* Upper */
563 title[i].other2 = cases[1]; /* Lower */
569 add_upper(ac_uint4 code)
574 * Always map the code to itself.
579 * If the title case character is not present, then make it the same as
585 if (upper_used == upper_size) {
587 upper = (_case_t *) malloc(sizeof(_case_t) << 3);
589 upper = (_case_t *) realloc((char *) upper,
590 sizeof(_case_t) * (upper_size + 8));
595 * Locate the insertion point.
597 for (i = 0; i < upper_used && code > upper[i].key; i++) ;
599 if (i < upper_used) {
601 * Shift the array up by one.
603 for (j = upper_used; j > i; j--)
604 (void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
608 upper[i].key = cases[0]; /* Upper */
609 upper[i].other1 = cases[1]; /* Lower */
610 upper[i].other2 = cases[2]; /* Title */
616 add_lower(ac_uint4 code)
621 * Always map the code to itself.
626 * If the title case character is empty, then make it the same as the
632 if (lower_used == lower_size) {
634 lower = (_case_t *) malloc(sizeof(_case_t) << 3);
636 lower = (_case_t *) realloc((char *) lower,
637 sizeof(_case_t) * (lower_size + 8));
642 * Locate the insertion point.
644 for (i = 0; i < lower_used && code > lower[i].key; i++) ;
646 if (i < lower_used) {
648 * Shift the array up by one.
650 for (j = lower_used; j > i; j--)
651 (void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
655 lower[i].key = cases[1]; /* Lower */
656 lower[i].other1 = cases[0]; /* Upper */
657 lower[i].other2 = cases[2]; /* Title */
663 ordered_ccl_insert(ac_uint4 c, ac_uint4 ccl_code)
667 if (ccl_used == ccl_size) {
669 ccl = (ac_uint4 *) malloc(sizeof(ac_uint4) * 24);
672 realloc((char *) ccl, sizeof(ac_uint4) * (ccl_size + 24));
677 * Optimize adding the first item.
687 * Handle the special case of extending the range on the end. This
688 * requires that the combining class codes are the same.
690 if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
691 ccl[ccl_used - 2] = c;
696 * Handle the special case of adding another range on the end.
698 if (c > ccl[ccl_used - 2] + 1 ||
699 (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
702 ccl[ccl_used++] = ccl_code;
707 * Locate either the insertion point or range for the code.
709 for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
711 if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
713 * Extend an existing range.
717 } else if (c < ccl[i]) {
719 * Start a new range before the current location.
721 for (j = ccl_used; j > i; j -= 3) {
723 ccl[j - 1] = ccl[j - 4];
724 ccl[j - 2] = ccl[j - 5];
726 ccl[i] = ccl[i + 1] = c;
727 ccl[i + 2] = ccl_code;
732 * Adds a number if it does not already exist and returns an index value
736 make_number(short num, short denom)
741 * Determine if the number already exists.
743 for (n = 0; n < nums_used; n++) {
744 if (nums[n].numerator == num && nums[n].denominator == denom)
748 if (nums_used == nums_size) {
750 nums = (_num_t *) malloc(sizeof(_num_t) << 3);
752 nums = (_num_t *) realloc((char *) nums,
753 sizeof(_num_t) * (nums_size + 8));
758 nums[n].numerator = num;
759 nums[n].denominator = denom;
765 add_number(ac_uint4 code, short num, short denom)
770 * Insert the code in order.
772 for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
775 * Handle the case of the codes matching and simply replace the number
776 * that was there before.
778 if (i < ncodes_used && code == ncodes[i].code) {
779 ncodes[i].idx = make_number(num, denom);
784 * Resize the array if necessary.
786 if (ncodes_used == ncodes_size) {
787 if (ncodes_size == 0)
788 ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
790 ncodes = (_codeidx_t *)
791 realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
797 * Shift things around to insert the code if necessary.
799 if (i < ncodes_used) {
800 for (j = ncodes_used; j > i; j--) {
801 ncodes[j].code = ncodes[j - 1].code;
802 ncodes[j].idx = ncodes[j - 1].idx;
805 ncodes[i].code = code;
806 ncodes[i].idx = make_number(num, denom);
812 * This routine assumes that the line is a valid Unicode Character Database
818 ac_uint4 i, lineno, skip, code, ccl_code;
819 short wnum, neg, number[2], compat;
820 char line[512], *s, *e;
823 while (fgets(line, sizeof(line), in)) {
824 if( (s=strchr(line, '\n')) ) *s = '\0';
828 * Skip blank lines and lines that start with a '#'.
830 if (line[0] == 0 || line[0] == '#')
834 * If lines need to be skipped, do it here.
842 * Collect the code. The code can be up to 6 hex digits in length to
843 * allow surrogates to be specified.
845 for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
847 if (*s >= '0' && *s <= '9')
849 else if (*s >= 'A' && *s <= 'F')
850 code += (*s - 'A') + 10;
851 else if (*s >= 'a' && *s <= 'f')
852 code += (*s - 'a') + 10;
856 * Handle the following special cases:
857 * 1. 4E00-9FA5 CJK Ideographs.
858 * 2. AC00-D7A3 Hangul Syllables.
859 * 3. D800-DFFF Surrogates.
860 * 4. E000-F8FF Private Use Area.
861 * 5. F900-FA2D Han compatibility.
862 * ...Plus additional ranges in newer Unicode versions...
866 /* CJK Ideograph Extension A */
867 add_range(0x3400, 0x4db5, "Lo", "L");
869 add_range(0x3400, 0x4db5, "Cp", 0);
875 * The Han ideographs.
877 add_range(0x4e00, 0x9fff, "Lo", "L");
880 * Add the characters to the defined category.
882 add_range(0x4e00, 0x9fa5, "Cp", 0);
888 * The Hangul syllables.
890 add_range(0xac00, 0xd7a3, "Lo", "L");
893 * Add the characters to the defined category.
895 add_range(0xac00, 0xd7a3, "Cp", 0);
901 * Make a range of all surrogates and assume some default
904 add_range(0x010000, 0x10ffff, "Cs", "L");
909 * The Private Use area. Add with a default set of properties.
911 add_range(0xe000, 0xf8ff, "Co", "L");
916 * The CJK compatibility area.
918 add_range(0xf900, 0xfaff, "Lo", "L");
921 * Add the characters to the defined category.
923 add_range(0xf900, 0xfaff, "Cp", 0);
928 /* CJK Ideograph Extension B */
929 add_range(0x20000, 0x2a6d6, "Lo", "L");
931 add_range(0x20000, 0x2a6d6, "Cp", 0);
936 /* Plane 15 private use */
937 add_range(0xf0000, 0xffffd, "Co", "L");
942 /* Plane 16 private use */
943 add_range(0x100000, 0x10fffd, "Co", "L");
952 * Add the code to the defined category.
954 ordered_range_insert(code, "Cp", 2);
957 * Locate the first character property field.
959 for (i = 0; *s != 0 && i < 2; s++) {
963 for (e = s; *e && *e != ';'; e++) ;
965 ordered_range_insert(code, s, e - s);
968 * Locate the combining class code.
970 for (s = e; *s != 0 && i < 3; s++) {
976 * Convert the combining class code from decimal.
978 for (ccl_code = 0, e = s; *e && *e != ';'; e++)
979 ccl_code = (ccl_code * 10) + (*e - '0');
982 * Add the code if it not 0.
985 ordered_ccl_insert(code, ccl_code);
988 * Locate the second character property field.
990 for (s = e; *s != 0 && i < 4; s++) {
994 for (e = s; *e && *e != ';'; e++) ;
996 ordered_range_insert(code, s, e - s);
999 * Check for a decomposition.
1006 * Skip compatibility formatting tag.
1008 while (*s++ != '>');
1011 * Collect the codes of the decomposition.
1013 for (dectmp_size = 0; *s != ';'; ) {
1015 * Skip all leading non-hex digits.
1017 while (!ishdigit(*s))
1020 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
1021 dectmp[dectmp_size] <<= 4;
1022 if (*s >= '0' && *s <= '9')
1023 dectmp[dectmp_size] += *s - '0';
1024 else if (*s >= 'A' && *s <= 'F')
1025 dectmp[dectmp_size] += (*s - 'A') + 10;
1026 else if (*s >= 'a' && *s <= 'f')
1027 dectmp[dectmp_size] += (*s - 'a') + 10;
1033 * If there are any codes in the temporary decomposition array,
1034 * then add the character with its decomposition.
1036 if (dectmp_size > 0) {
1038 add_decomp(code, 0);
1040 add_decomp(code, 1);
1045 * Skip to the number field.
1047 for (i = 0; i < 3 && *s; s++) {
1053 * Scan the number in.
1055 number[0] = number[1] = 0;
1056 for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
1064 * Move the the denominator of the fraction.
1072 number[wnum] = (number[wnum] * 10) + (*e - '0');
1077 * Adjust the denominator in case of integers and add the number.
1082 add_number(code, number[0], number[1]);
1086 * Skip to the start of the possible case mappings.
1088 for (s = e, i = 0; i < 4 && *s; s++) {
1094 * Collect the case mappings.
1096 cases[0] = cases[1] = cases[2] = 0;
1097 for (i = 0; i < 3; i++) {
1098 while (ishdigit(*s)) {
1100 if (*s >= '0' && *s <= '9')
1101 cases[i] += *s - '0';
1102 else if (*s >= 'A' && *s <= 'F')
1103 cases[i] += (*s - 'A') + 10;
1104 else if (*s >= 'a' && *s <= 'f')
1105 cases[i] += (*s - 'a') + 10;
1111 if (cases[0] && cases[1])
1113 * Add the upper and lower mappings for a title case character.
1118 * Add the lower and title case mappings for the upper case
1124 * Add the upper and title case mappings for the lower case
1132 find_decomp(ac_uint4 code, short compat)
1138 r = (compat ? kdecomps_used : decomps_used) - 1;
1139 decs = compat ? kdecomps : decomps;
1142 if (code > decs[m].code)
1144 else if (code < decs[m].code)
1153 decomp_it(_decomp_t *d, short compat)
1158 for (i = 0; i < d->used; i++) {
1159 if ((dp = find_decomp(d->decomp[i], compat)) != 0)
1160 decomp_it(dp, compat);
1162 dectmp[dectmp_size++] = d->decomp[i];
1167 * Expand all decompositions by recursively decomposing each character
1168 * in the decomposition.
1175 for (i = 0; i < decomps_used; i++) {
1177 decomp_it(&decomps[i], 0);
1178 if (dectmp_size > 0)
1179 add_decomp(decomps[i].code, 0);
1182 for (i = 0; i < kdecomps_used; i++) {
1184 decomp_it(&kdecomps[i], 1);
1185 if (dectmp_size > 0)
1186 add_decomp(kdecomps[i].code, 1);
1191 cmpcomps(const void *v_comp1, const void *v_comp2)
1193 const _comp_t *comp1 = v_comp1, *comp2 = v_comp2;
1194 long diff = comp1->code1 - comp2->code1;
1197 diff = comp1->code2 - comp2->code2;
1202 * Load composition exclusion data
1205 read_compexdata(FILE *in)
1211 (void) memset((char *) compexs, 0, sizeof(compexs));
1213 while (fgets(line, sizeof(line), in)) {
1214 if( (s=strchr(line, '\n')) ) *s = '\0';
1216 * Skip blank lines and lines that start with a '#'.
1218 if (line[0] == 0 || line[0] == '#')
1222 * Collect the code. Assume max 6 digits
1225 for (s = line, i = code = 0; *s != '#' && i < 6; i++, s++) {
1226 if (isspace(*s)) break;
1228 if (*s >= '0' && *s <= '9')
1230 else if (*s >= 'A' && *s <= 'F')
1231 code += (*s - 'A') + 10;
1232 else if (*s >= 'a' && *s <= 'f')
1233 code += (*s - 'a') + 10;
1240 * Creates array of compositions from decomposition array
1247 comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
1249 for (i = cu = 0; i < decomps_used; i++) {
1250 if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
1252 comps[cu].comp = decomps[i].code;
1253 comps[cu].count = 2;
1254 comps[cu].code1 = decomps[i].decomp[0];
1255 comps[cu].code2 = decomps[i].decomp[1];
1259 qsort(comps, comps_used, sizeof(_comp_t), cmpcomps);
1264 write_case(FILE *out, _case_t *tab, int num, int first)
1268 for (i=0; i<num; i++) {
1269 if (first) first = 0;
1270 else fprintf(out, ",");
1271 fprintf(out, "\n\t0x%08x, 0x%08x, 0x%08x",
1272 tab[i].key, tab[i].other1, tab[i].other2);
1276 #define PREF "static const "
1281 write_cdata(char *opath)
1285 ac_uint4 i, idx, nprops;
1286 ac_uint2 casecnt[2];
1291 /*****************************************************************
1293 * Generate the ctype data.
1295 *****************************************************************/
1298 * Open the output file.
1300 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "uctable.h", opath);
1301 if ((out = fopen(path, "w")) == 0)
1305 * Open the ctype.dat file.
1307 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "ctype.dat", opath);
1308 if ((out = fopen(path, "wb")) == 0)
1313 * Collect the offsets for the properties. The offsets array is
1314 * on a 4-byte boundary to keep things efficient for architectures
1315 * that need such a thing.
1317 for (i = idx = 0; i < NUMPROPS; i++) {
1318 propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
1319 idx += proptbl[i].used;
1323 * Add the sentinel index which is used by the binary search as the upper
1324 * bound for a search.
1329 * Record the actual number of property lists. This may be different than
1330 * the number of offsets actually written because of aligning on a 4-byte
1336 * Calculate the byte count needed and pad the property counts array to a
1339 if ((bytes = sizeof(ac_uint2) * (NUMPROPS + 1)) & 3)
1340 bytes += 4 - (bytes & 3);
1341 nprops = bytes / sizeof(ac_uint2);
1342 bytes += sizeof(ac_uint4) * idx;
1345 fprintf(out, PREF "ac_uint4 _ucprop_size = %d;\n\n", NUMPROPS);
1347 fprintf(out, PREF "ac_uint2 _ucprop_offsets[] = {");
1349 for (i = 0; i<nprops; i++) {
1350 if (i) fprintf(out, ",");
1351 if (!(i&7)) fprintf(out, "\n\t");
1352 else fprintf(out, " ");
1353 fprintf(out, "0x%04x", propcnt[i]);
1355 fprintf(out, "\n};\n\n");
1357 fprintf(out, PREF "ac_uint4 _ucprop_ranges[] = {");
1360 for (i = 0; i < NUMPROPS; i++) {
1361 if (proptbl[i].used > 0) {
1362 for (j=0; j<proptbl[i].used; j++) {
1363 if (k) fprintf(out, ",");
1364 if (!(k&3)) fprintf(out,"\n\t");
1365 else fprintf(out, " ");
1367 fprintf(out, "0x%08x", proptbl[i].ranges[j]);
1371 fprintf(out, "\n};\n\n");
1376 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1379 * Write the byte count.
1381 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1384 * Write the property list counts.
1386 fwrite((char *) propcnt, sizeof(ac_uint2), nprops, out);
1389 * Write the property lists.
1391 for (i = 0; i < NUMPROPS; i++) {
1392 if (proptbl[i].used > 0)
1393 fwrite((char *) proptbl[i].ranges, sizeof(ac_uint4),
1394 proptbl[i].used, out);
1400 /*****************************************************************
1402 * Generate the case mapping data.
1404 *****************************************************************/
1407 fprintf(out, PREF "ac_uint4 _uccase_size = %d;\n\n",
1408 upper_used + lower_used + title_used);
1410 fprintf(out, PREF "ac_uint2 _uccase_len[2] = {%d, %d};\n\n",
1411 upper_used, lower_used);
1412 fprintf(out, PREF "ac_uint4 _uccase_map[] = {");
1416 * Write the upper case table.
1418 write_case(out, upper, upper_used, 1);
1422 * Write the lower case table.
1424 write_case(out, lower, lower_used, !upper_used);
1428 * Write the title case table.
1430 write_case(out, title, title_used, !(upper_used||lower_used));
1432 if (!(upper_used || lower_used || title_used))
1433 fprintf(out, "\t0");
1435 fprintf(out, "\n};\n\n");
1438 * Open the case.dat file.
1440 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "case.dat", opath);
1441 if ((out = fopen(path, "wb")) == 0)
1445 * Write the case mapping tables.
1447 hdr[1] = upper_used + lower_used + title_used;
1448 casecnt[0] = upper_used;
1449 casecnt[1] = lower_used;
1454 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1457 * Write the upper and lower case table sizes.
1459 fwrite((char *) casecnt, sizeof(ac_uint2), 2, out);
1463 * Write the upper case table.
1465 fwrite((char *) upper, sizeof(_case_t), upper_used, out);
1469 * Write the lower case table.
1471 fwrite((char *) lower, sizeof(_case_t), lower_used, out);
1475 * Write the title case table.
1477 fwrite((char *) title, sizeof(_case_t), title_used, out);
1482 /*****************************************************************
1484 * Generate the composition data.
1486 *****************************************************************/
1489 * Create compositions from decomposition data
1494 fprintf(out, PREF "ac_uint4 _uccomp_size = %d;\n\n",
1497 fprintf(out, PREF "ac_uint4 _uccomp_data[] = {");
1500 * Now, if comps exist, write them out.
1502 if (comps_used > 0) {
1503 for (i=0; i<comps_used; i++) {
1504 if (i) fprintf(out, ",");
1505 fprintf(out, "\n\t0x%08x, 0x%08x, 0x%08x, 0x%08x",
1506 comps[i].comp, comps[i].count, comps[i].code1, comps[i].code2);
1509 fprintf(out, "\t0");
1511 fprintf(out, "\n};\n\n");
1514 * Open the comp.dat file.
1516 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "comp.dat", opath);
1517 if ((out = fopen(path, "wb")) == 0)
1523 hdr[1] = (ac_uint2) comps_used * 4;
1524 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1527 * Write out the byte count to maintain header size.
1529 bytes = comps_used * sizeof(_comp_t);
1530 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1533 * Now, if comps exist, write them out.
1536 fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
1541 /*****************************************************************
1543 * Generate the decomposition data.
1545 *****************************************************************/
1548 * Fully expand all decompositions before generating the output file.
1553 fprintf(out, PREF "ac_uint4 _ucdcmp_size = %d;\n\n",
1556 fprintf(out, PREF "ac_uint4 _ucdcmp_nodes[] = {");
1560 * Write the list of decomp nodes.
1562 for (i = idx = 0; i < decomps_used; i++) {
1563 fprintf(out, "\n\t0x%08x, 0x%08x,", decomps[i].code, idx);
1564 idx += decomps[i].used;
1568 * Write the sentinel index as the last decomp node.
1570 fprintf(out, "\n\t0x%08x\n};\n\n", idx);
1572 fprintf(out, PREF "ac_uint4 _ucdcmp_decomp[] = {");
1574 * Write the decompositions themselves.
1577 for (i = 0; i < decomps_used; i++)
1578 for (j=0; j<decomps[i].used; j++) {
1579 if (k) fprintf(out, ",");
1580 if (!(k&3)) fprintf(out,"\n\t");
1581 else fprintf(out, " ");
1583 fprintf(out, "0x%08x", decomps[i].decomp[j]);
1585 fprintf(out, "\n};\n\n");
1589 * Open the decomp.dat file.
1591 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "decomp.dat", opath);
1592 if ((out = fopen(path, "wb")) == 0)
1595 hdr[1] = decomps_used;
1600 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1603 * Write a temporary byte count which will be calculated as the
1604 * decompositions are written out.
1607 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1611 * Write the list of decomp nodes.
1613 for (i = idx = 0; i < decomps_used; i++) {
1614 fwrite((char *) &decomps[i].code, sizeof(ac_uint4), 1, out);
1615 fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
1616 idx += decomps[i].used;
1620 * Write the sentinel index as the last decomp node.
1622 fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
1625 * Write the decompositions themselves.
1627 for (i = 0; i < decomps_used; i++)
1628 fwrite((char *) decomps[i].decomp, sizeof(ac_uint4),
1629 decomps[i].used, out);
1632 * Seek back to the beginning and write the byte count.
1634 bytes = (sizeof(ac_uint4) * idx) +
1635 (sizeof(ac_uint4) * ((hdr[1] << 1) + 1));
1636 fseek(out, sizeof(ac_uint2) << 1, 0L);
1637 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1643 #ifdef HARDCODE_DATA
1644 fprintf(out, PREF "ac_uint4 _uckdcmp_size = %d;\n\n",
1647 fprintf(out, PREF "ac_uint4 _uckdcmp_nodes[] = {");
1649 if (kdecomps_used) {
1651 * Write the list of kdecomp nodes.
1653 for (i = idx = 0; i < kdecomps_used; i++) {
1654 fprintf(out, "\n\t0x%08x, 0x%08x,", kdecomps[i].code, idx);
1655 idx += kdecomps[i].used;
1659 * Write the sentinel index as the last decomp node.
1661 fprintf(out, "\n\t0x%08x\n};\n\n", idx);
1663 fprintf(out, PREF "ac_uint4 _uckdcmp_decomp[] = {");
1666 * Write the decompositions themselves.
1669 for (i = 0; i < kdecomps_used; i++)
1670 for (j=0; j<kdecomps[i].used; j++) {
1671 if (k) fprintf(out, ",");
1672 if (!(k&3)) fprintf(out,"\n\t");
1673 else fprintf(out, " ");
1675 fprintf(out, "0x%08x", kdecomps[i].decomp[j]);
1677 fprintf(out, "\n};\n\n");
1681 * Open the kdecomp.dat file.
1683 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "kdecomp.dat", opath);
1684 if ((out = fopen(path, "wb")) == 0)
1687 hdr[1] = kdecomps_used;
1692 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1695 * Write a temporary byte count which will be calculated as the
1696 * decompositions are written out.
1699 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1701 if (kdecomps_used) {
1703 * Write the list of kdecomp nodes.
1705 for (i = idx = 0; i < kdecomps_used; i++) {
1706 fwrite((char *) &kdecomps[i].code, sizeof(ac_uint4), 1, out);
1707 fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
1708 idx += kdecomps[i].used;
1712 * Write the sentinel index as the last decomp node.
1714 fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
1717 * Write the decompositions themselves.
1719 for (i = 0; i < kdecomps_used; i++)
1720 fwrite((char *) kdecomps[i].decomp, sizeof(ac_uint4),
1721 kdecomps[i].used, out);
1724 * Seek back to the beginning and write the byte count.
1726 bytes = (sizeof(ac_uint4) * idx) +
1727 (sizeof(ac_uint4) * ((hdr[1] << 1) + 1));
1728 fseek(out, sizeof(ac_uint2) << 1, 0L);
1729 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1735 /*****************************************************************
1737 * Generate the combining class data.
1739 *****************************************************************/
1740 #ifdef HARDCODE_DATA
1741 fprintf(out, PREF "ac_uint4 _uccmcl_size = %d;\n\n", ccl_used);
1743 fprintf(out, PREF "ac_uint4 _uccmcl_nodes[] = {");
1747 * Write the combining class ranges out.
1749 for (i = 0; i<ccl_used; i++) {
1750 if (i) fprintf(out, ",");
1751 if (!(i&3)) fprintf(out, "\n\t");
1752 else fprintf(out, " ");
1753 fprintf(out, "0x%08x", ccl[i]);
1756 fprintf(out, "\t0");
1758 fprintf(out, "\n};\n\n");
1761 * Open the cmbcl.dat file.
1763 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "cmbcl.dat", opath);
1764 if ((out = fopen(path, "wb")) == 0)
1768 * Set the number of ranges used. Each range has a combining class which
1769 * means each entry is a 3-tuple.
1771 hdr[1] = ccl_used / 3;
1776 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1779 * Write out the byte count to maintain header size.
1781 bytes = ccl_used * sizeof(ac_uint4);
1782 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1786 * Write the combining class ranges out.
1788 fwrite((char *) ccl, sizeof(ac_uint4), ccl_used, out);
1793 /*****************************************************************
1795 * Generate the number data.
1797 *****************************************************************/
1800 fprintf(out, PREF "ac_uint4 _ucnum_size = %d;\n\n", ncodes_used<<1);
1802 fprintf(out, PREF "ac_uint4 _ucnum_nodes[] = {");
1805 * Now, if number mappings exist, write them out.
1807 if (ncodes_used > 0) {
1808 for (i = 0; i<ncodes_used; i++) {
1809 if (i) fprintf(out, ",");
1810 if (!(i&1)) fprintf(out, "\n\t");
1811 else fprintf(out, " ");
1812 fprintf(out, "0x%08x, 0x%08x", ncodes[i].code, ncodes[i].idx);
1814 fprintf(out, "\n};\n\n");
1816 fprintf(out, PREF "short _ucnum_vals[] = {");
1817 for (i = 0; i<nums_used; i++) {
1818 if (i) fprintf(out, ",");
1819 if (!(i&3)) fprintf(out, "\n\t");
1820 else fprintf(out, " ");
1821 if (nums[i].numerator < 0) {
1822 fprintf(out, "%6d, 0x%04x",
1823 nums[i].numerator, nums[i].denominator);
1825 fprintf(out, "0x%04x, 0x%04x",
1826 nums[i].numerator, nums[i].denominator);
1829 fprintf(out, "\n};\n\n");
1833 * Open the num.dat file.
1835 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "num.dat", opath);
1836 if ((out = fopen(path, "wb")) == 0)
1840 * The count part of the header will be the total number of codes that
1843 hdr[1] = (ac_uint2) (ncodes_used << 1);
1844 bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
1849 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1852 * Write out the byte count to maintain header size.
1854 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1857 * Now, if number mappings exist, write them out.
1859 if (ncodes_used > 0) {
1860 fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
1861 fwrite((char *) nums, sizeof(_num_t), nums_used, out);
1872 "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
1873 fprintf(stderr, " datafile1 datafile2 ...\n\n");
1875 "-o output-directory\n\t\tWrite the output files to a different");
1876 fprintf(stderr, " directory (default: .).\n");
1878 "-x composition-exclusion\n\t\tFile of composition codes");
1879 fprintf(stderr, " that should be excluded.\n");
1884 main(int argc, char *argv[])
1889 prog = lutil_progname( "ucgendat", argc, argv );
1898 if (argv[0][0] == '-') {
1899 switch (argv[0][1]) {
1908 if ((in = fopen(argv[0], "r")) == 0)
1910 "%s: unable to open composition exclusion file %s\n",
1913 read_compexdata(in);
1922 if (in != stdin && in != NULL)
1924 if ((in = fopen(argv[0], "r")) == 0)
1925 fprintf(stderr, "%s: unable to open ctype file %s\n",