3 * Copyright 2000-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright 2001 Computing Research Labs, New Mexico State University
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
23 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
24 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
25 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 /* $Id: ucgendat.c,v 1.4 2001/01/02 18:46:20 mleisher Exp $" */
30 #include "ldap_config.h"
33 #include <ac/stdlib.h>
34 #include <ac/string.h>
35 #include <ac/unistd.h>
42 #define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\
43 ((cc) >= 'A' && (cc) <= 'F') ||\
44 ((cc) >= 'a' && (cc) <= 'f'))
47 * A header written to the output file with the byte-order-mark and the number
50 static unsigned short hdr[2] = {0xfeff, 0};
53 #define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3)))
61 * List of properties expected to be found in the Unicode Character Database
62 * including some implementation specific properties.
64 * The implementation specific properties are:
65 * Cm = Composed (can be decomposed)
67 * Sy = Symmetric (has left and right forms)
72 * Cp = Defined character
74 static _prop_t props[NUMPROPS] = {
75 {"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2},
76 {"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2},
77 {"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2},
78 {"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2},
79 {"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L", 1}, {"R", 1},
80 {"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B", 1},
81 {"S", 1}, {"WS", 2}, {"ON", 2},
82 {"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2},
83 {"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2}, {"AL", 2}
87 unsigned long *ranges;
92 static _ranges_t proptbl[NUMPROPS];
95 * Make sure this array is sized to be on a 4-byte boundary at compile time.
97 static unsigned short propcnt[NEEDPROPS];
100 * Array used to collect a decomposition before adding it to the decomposition
103 static unsigned long dectmp[64];
104 static unsigned long dectmp_size;
110 unsigned long *decomp;
114 * List of decomposition. Created and expanded in order as the characters are
115 * encountered. First list contains canonical mappings, second also includes
116 * compatibility mappings.
118 static _decomp_t *decomps;
119 static unsigned long decomps_used;
120 static unsigned long decomps_size;
122 static _decomp_t *kdecomps;
123 static unsigned long kdecomps_used;
124 static unsigned long kdecomps_size;
127 * Composition exclusion table stuff.
129 #define COMPEX_SET(c) (compexs[(c) >> 5] |= (1 << ((c) & 31)))
130 #define COMPEX_TEST(c) (compexs[(c) >> 5] & (1 << ((c) & 31)))
131 static unsigned long compexs[2048];
134 * Struct for holding a composition pair, and array of composition pairs
143 static _comp_t *comps;
144 static unsigned long comps_used;
147 * Types and lists for handling lists of case mappings.
151 unsigned long other1;
152 unsigned long other2;
155 static _case_t *upper;
156 static _case_t *lower;
157 static _case_t *title;
158 static unsigned long upper_used;
159 static unsigned long upper_size;
160 static unsigned long lower_used;
161 static unsigned long lower_size;
162 static unsigned long title_used;
163 static unsigned long title_size;
166 * Array used to collect case mappings before adding them to a list.
168 static unsigned long cases[3];
171 * An array to hold ranges for combining classes.
173 static unsigned long *ccl;
174 static unsigned long ccl_used;
175 static unsigned long ccl_size;
178 * Structures for handling numbers.
191 * Arrays to hold the mapping of codes to numbers.
193 static _codeidx_t *ncodes;
194 static unsigned long ncodes_used;
195 static unsigned long ncodes_size;
198 static unsigned long nums_used;
199 static unsigned long nums_size;
202 * Array for holding numbers.
205 static unsigned long nums_used;
206 static unsigned long nums_size;
209 add_range(unsigned long start, unsigned long end, char *p1, char *p2)
215 for (k = 0; k < 2; k++) {
227 for (i = 0; i < NUMPROPS; i++) {
228 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
238 * Resize the range list if necessary.
240 if (rlp->used == rlp->size) {
242 rlp->ranges = (unsigned long *)
243 malloc(sizeof(unsigned long) << 3);
245 rlp->ranges = (unsigned long *)
246 realloc((char *) rlp->ranges,
247 sizeof(unsigned long) * (rlp->size + 8));
252 * If this is the first code for this property list, just add it
255 if (rlp->used == 0) {
256 rlp->ranges[0] = start;
257 rlp->ranges[1] = end;
263 * Optimize the case of adding the range to the end.
266 if (start > rlp->ranges[j]) {
268 rlp->ranges[j++] = start;
269 rlp->ranges[j++] = end;
275 * Need to locate the insertion point.
278 i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
281 * If the start value lies in the current range, then simply set the
282 * new end point of the range to the end value passed as a parameter.
284 if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
285 rlp->ranges[i + 1] = end;
290 * Shift following values up by two.
292 for (j = rlp->used; j > i; j -= 2) {
293 rlp->ranges[j] = rlp->ranges[j - 2];
294 rlp->ranges[j + 1] = rlp->ranges[j - 1];
298 * Add the new range at the insertion point.
300 rlp->ranges[i] = start;
301 rlp->ranges[i + 1] = end;
307 ordered_range_insert(unsigned long c, char *name, int len)
317 * Deal with directionality codes introduced in Unicode 3.0.
319 if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
321 (memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
322 memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
323 memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
325 * Mark all of these as Other Neutral to preserve compatibility with
332 for (i = 0; i < NUMPROPS; i++) {
333 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
341 * Have a match, so insert the code in order.
346 * Resize the range list if necessary.
348 if (rlp->used == rlp->size) {
350 rlp->ranges = (unsigned long *)
351 malloc(sizeof(unsigned long) << 3);
353 rlp->ranges = (unsigned long *)
354 realloc((char *) rlp->ranges,
355 sizeof(unsigned long) * (rlp->size + 8));
360 * If this is the first code for this property list, just add it
363 if (rlp->used == 0) {
364 rlp->ranges[0] = rlp->ranges[1] = c;
370 * Optimize the cases of extending the last range and adding new ranges to
375 s = rlp->ranges[j - 1];
379 * Extend the last range.
387 * Start another range on the end.
390 rlp->ranges[j] = rlp->ranges[j + 1] = c;
397 * The code is a duplicate of a code in the last range, so just return.
402 * The code should be inserted somewhere before the last range in the
403 * list. Locate the insertion point.
406 i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
409 e = rlp->ranges[i + 1];
413 * Simply extend the current range.
415 rlp->ranges[i + 1] = c;
418 * Add a new entry before the current location. Shift all entries
419 * before the current one up by one to make room.
421 for (j = rlp->used; j > i; j -= 2) {
422 rlp->ranges[j] = rlp->ranges[j - 2];
423 rlp->ranges[j + 1] = rlp->ranges[j - 1];
425 rlp->ranges[i] = rlp->ranges[i + 1] = c;
432 add_decomp(unsigned long code, short compat)
434 unsigned long i, j, size;
435 _decomp_t **pdecomps;
436 unsigned long *pdecomps_used;
437 unsigned long *pdecomps_size;
440 pdecomps = &kdecomps;
441 pdecomps_used = &kdecomps_used;
442 pdecomps_size = &kdecomps_size;
445 pdecomps_used = &decomps_used;
446 pdecomps_size = &decomps_size;
450 * Add the code to the composite property.
453 ordered_range_insert(code, "Cm", 2);
457 * Locate the insertion point for the code.
459 for (i = 0; i < *pdecomps_used && code > (*pdecomps)[i].code; i++) ;
462 * Allocate space for a new decomposition.
464 if (*pdecomps_used == *pdecomps_size) {
465 if (*pdecomps_size == 0)
466 *pdecomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
468 *pdecomps = (_decomp_t *)
469 realloc((char *) *pdecomps,
470 sizeof(_decomp_t) * (*pdecomps_size + 8));
471 (void) memset((char *) (*pdecomps + *pdecomps_size), '\0',
472 sizeof(_decomp_t) << 3);
476 if (i < *pdecomps_used && code != (*pdecomps)[i].code) {
478 * Shift the decomps up by one if the codes don't match.
480 for (j = *pdecomps_used; j > i; j--)
481 (void) AC_MEMCPY((char *) &(*pdecomps)[j], (char *) &(*pdecomps)[j - 1],
486 * Insert or replace a decomposition.
488 size = dectmp_size + (4 - (dectmp_size & 3));
489 if ((*pdecomps)[i].size < size) {
490 if ((*pdecomps)[i].size == 0)
491 (*pdecomps)[i].decomp = (unsigned long *)
492 malloc(sizeof(unsigned long) * size);
494 (*pdecomps)[i].decomp = (unsigned long *)
495 realloc((char *) (*pdecomps)[i].decomp,
496 sizeof(unsigned long) * size);
497 (*pdecomps)[i].size = size;
500 if ((*pdecomps)[i].code != code)
503 (*pdecomps)[i].code = code;
504 (*pdecomps)[i].used = dectmp_size;
505 (void) AC_MEMCPY((char *) (*pdecomps)[i].decomp, (char *) dectmp,
506 sizeof(unsigned long) * dectmp_size);
509 * NOTICE: This needs changing later so it is more general than simply
510 * pairs. This calculation is done here to simplify allocation elsewhere.
512 if (!compat && dectmp_size == 2)
517 add_title(unsigned long code)
522 * Always map the code to itself.
526 if (title_used == title_size) {
528 title = (_case_t *) malloc(sizeof(_case_t) << 3);
530 title = (_case_t *) realloc((char *) title,
531 sizeof(_case_t) * (title_size + 8));
536 * Locate the insertion point.
538 for (i = 0; i < title_used && code > title[i].key; i++) ;
540 if (i < title_used) {
542 * Shift the array up by one.
544 for (j = title_used; j > i; j--)
545 (void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
549 title[i].key = cases[2]; /* Title */
550 title[i].other1 = cases[0]; /* Upper */
551 title[i].other2 = cases[1]; /* Lower */
557 add_upper(unsigned long code)
562 * Always map the code to itself.
567 * If the title case character is not present, then make it the same as
573 if (upper_used == upper_size) {
575 upper = (_case_t *) malloc(sizeof(_case_t) << 3);
577 upper = (_case_t *) realloc((char *) upper,
578 sizeof(_case_t) * (upper_size + 8));
583 * Locate the insertion point.
585 for (i = 0; i < upper_used && code > upper[i].key; i++) ;
587 if (i < upper_used) {
589 * Shift the array up by one.
591 for (j = upper_used; j > i; j--)
592 (void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
596 upper[i].key = cases[0]; /* Upper */
597 upper[i].other1 = cases[1]; /* Lower */
598 upper[i].other2 = cases[2]; /* Title */
604 add_lower(unsigned long code)
609 * Always map the code to itself.
614 * If the title case character is empty, then make it the same as the
620 if (lower_used == lower_size) {
622 lower = (_case_t *) malloc(sizeof(_case_t) << 3);
624 lower = (_case_t *) realloc((char *) lower,
625 sizeof(_case_t) * (lower_size + 8));
630 * Locate the insertion point.
632 for (i = 0; i < lower_used && code > lower[i].key; i++) ;
634 if (i < lower_used) {
636 * Shift the array up by one.
638 for (j = lower_used; j > i; j--)
639 (void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
643 lower[i].key = cases[1]; /* Lower */
644 lower[i].other1 = cases[0]; /* Upper */
645 lower[i].other2 = cases[2]; /* Title */
651 ordered_ccl_insert(unsigned long c, unsigned long ccl_code)
655 if (ccl_used == ccl_size) {
657 ccl = (unsigned long *) malloc(sizeof(unsigned long) * 24);
659 ccl = (unsigned long *)
660 realloc((char *) ccl, sizeof(unsigned long) * (ccl_size + 24));
665 * Optimize adding the first item.
675 * Handle the special case of extending the range on the end. This
676 * requires that the combining class codes are the same.
678 if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
679 ccl[ccl_used - 2] = c;
684 * Handle the special case of adding another range on the end.
686 if (c > ccl[ccl_used - 2] + 1 ||
687 (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
690 ccl[ccl_used++] = ccl_code;
695 * Locate either the insertion point or range for the code.
697 for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
699 if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
701 * Extend an existing range.
705 } else if (c < ccl[i]) {
707 * Start a new range before the current location.
709 for (j = ccl_used; j > i; j -= 3) {
711 ccl[j - 1] = ccl[j - 4];
712 ccl[j - 2] = ccl[j - 5];
714 ccl[i] = ccl[i + 1] = c;
715 ccl[i + 2] = ccl_code;
720 * Adds a number if it does not already exist and returns an index value
724 make_number(short num, short denom)
729 * Determine if the number already exists.
731 for (n = 0; n < nums_used; n++) {
732 if (nums[n].numerator == num && nums[n].denominator == denom)
736 if (nums_used == nums_size) {
738 nums = (_num_t *) malloc(sizeof(_num_t) << 3);
740 nums = (_num_t *) realloc((char *) nums,
741 sizeof(_num_t) * (nums_size + 8));
746 nums[n].numerator = num;
747 nums[n].denominator = denom;
753 add_number(unsigned long code, short num, short denom)
758 * Insert the code in order.
760 for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
763 * Handle the case of the codes matching and simply replace the number
764 * that was there before.
766 if (i < ncodes_used && code == ncodes[i].code) {
767 ncodes[i].idx = make_number(num, denom);
772 * Resize the array if necessary.
774 if (ncodes_used == ncodes_size) {
775 if (ncodes_size == 0)
776 ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
778 ncodes = (_codeidx_t *)
779 realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
785 * Shift things around to insert the code if necessary.
787 if (i < ncodes_used) {
788 for (j = ncodes_used; j > i; j--) {
789 ncodes[j].code = ncodes[j - 1].code;
790 ncodes[j].idx = ncodes[j - 1].idx;
793 ncodes[i].code = code;
794 ncodes[i].idx = make_number(num, denom);
800 * This routine assumes that the line is a valid Unicode Character Database
806 unsigned long i, lineno, skip, code, ccl_code;
807 short wnum, neg, number[2], compat;
808 char line[512], *s, *e;
811 while (fgets(line, sizeof(line), in)) {
812 if( (s=strchr(line, '\n')) ) *s = '\0';
816 * Skip blank lines and lines that start with a '#'.
818 if (line[0] == 0 || line[0] == '#')
822 * If lines need to be skipped, do it here.
830 * Collect the code. The code can be up to 6 hex digits in length to
831 * allow surrogates to be specified.
833 for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
835 if (*s >= '0' && *s <= '9')
837 else if (*s >= 'A' && *s <= 'F')
838 code += (*s - 'A') + 10;
839 else if (*s >= 'a' && *s <= 'f')
840 code += (*s - 'a') + 10;
844 * Handle the following special cases:
845 * 1. 4E00-9FA5 CJK Ideographs.
846 * 2. AC00-D7A3 Hangul Syllables.
847 * 3. D800-DFFF Surrogates.
848 * 4. E000-F8FF Private Use Area.
849 * 5. F900-FA2D Han compatibility.
854 * The Han ideographs.
856 add_range(0x4e00, 0x9fff, "Lo", "L");
859 * Add the characters to the defined category.
861 add_range(0x4e00, 0x9fa5, "Cp", 0);
867 * The Hangul syllables.
869 add_range(0xac00, 0xd7a3, "Lo", "L");
872 * Add the characters to the defined category.
874 add_range(0xac00, 0xd7a3, "Cp", 0);
880 * Make a range of all surrogates and assume some default
883 add_range(0x010000, 0x10ffff, "Cs", "L");
888 * The Private Use area. Add with a default set of properties.
890 add_range(0xe000, 0xf8ff, "Co", "L");
895 * The CJK compatibility area.
897 add_range(0xf900, 0xfaff, "Lo", "L");
900 * Add the characters to the defined category.
902 add_range(0xf900, 0xfaff, "Cp", 0);
911 * Add the code to the defined category.
913 ordered_range_insert(code, "Cp", 2);
916 * Locate the first character property field.
918 for (i = 0; *s != 0 && i < 2; s++) {
922 for (e = s; *e && *e != ';'; e++) ;
924 ordered_range_insert(code, s, e - s);
927 * Locate the combining class code.
929 for (s = e; *s != 0 && i < 3; s++) {
935 * Convert the combining class code from decimal.
937 for (ccl_code = 0, e = s; *e && *e != ';'; e++)
938 ccl_code = (ccl_code * 10) + (*e - '0');
941 * Add the code if it not 0.
944 ordered_ccl_insert(code, ccl_code);
947 * Locate the second character property field.
949 for (s = e; *s != 0 && i < 4; s++) {
953 for (e = s; *e && *e != ';'; e++) ;
955 ordered_range_insert(code, s, e - s);
958 * Check for a decomposition.
965 * Skip compatibility formatting tag.
970 * Collect the codes of the decomposition.
972 for (dectmp_size = 0; *s != ';'; ) {
974 * Skip all leading non-hex digits.
976 while (!ishdigit(*s))
979 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
980 dectmp[dectmp_size] <<= 4;
981 if (*s >= '0' && *s <= '9')
982 dectmp[dectmp_size] += *s - '0';
983 else if (*s >= 'A' && *s <= 'F')
984 dectmp[dectmp_size] += (*s - 'A') + 10;
985 else if (*s >= 'a' && *s <= 'f')
986 dectmp[dectmp_size] += (*s - 'a') + 10;
992 * If there are any codes in the temporary decomposition array,
993 * then add the character with its decomposition.
995 if (dectmp_size > 0) {
1004 * Skip to the number field.
1006 for (i = 0; i < 3 && *s; s++) {
1012 * Scan the number in.
1014 number[0] = number[1] = 0;
1015 for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
1023 * Move the the denominator of the fraction.
1031 number[wnum] = (number[wnum] * 10) + (*e - '0');
1036 * Adjust the denominator in case of integers and add the number.
1039 number[1] = number[0];
1041 add_number(code, number[0], number[1]);
1045 * Skip to the start of the possible case mappings.
1047 for (s = e, i = 0; i < 4 && *s; s++) {
1053 * Collect the case mappings.
1055 cases[0] = cases[1] = cases[2] = 0;
1056 for (i = 0; i < 3; i++) {
1057 while (ishdigit(*s)) {
1059 if (*s >= '0' && *s <= '9')
1060 cases[i] += *s - '0';
1061 else if (*s >= 'A' && *s <= 'F')
1062 cases[i] += (*s - 'A') + 10;
1063 else if (*s >= 'a' && *s <= 'f')
1064 cases[i] += (*s - 'a') + 10;
1070 if (cases[0] && cases[1])
1072 * Add the upper and lower mappings for a title case character.
1077 * Add the lower and title case mappings for the upper case
1083 * Add the upper and title case mappings for the lower case
1091 find_decomp(unsigned long code, short compat)
1097 r = (compat ? kdecomps_used : decomps_used) - 1;
1098 decs = compat ? kdecomps : decomps;
1101 if (code > decs[m].code)
1103 else if (code < decs[m].code)
1112 decomp_it(_decomp_t *d, short compat)
1117 for (i = 0; i < d->used; i++) {
1118 if ((dp = find_decomp(d->decomp[i], compat)) != 0)
1119 decomp_it(dp, compat);
1121 dectmp[dectmp_size++] = d->decomp[i];
1126 * Expand all decompositions by recursively decomposing each character
1127 * in the decomposition.
1134 for (i = 0; i < decomps_used; i++) {
1136 decomp_it(&decomps[i], 0);
1137 if (dectmp_size > 0)
1138 add_decomp(decomps[i].code, 0);
1141 for (i = 0; i < kdecomps_used; i++) {
1143 decomp_it(&kdecomps[i], 1);
1144 if (dectmp_size > 0)
1145 add_decomp(kdecomps[i].code, 1);
1150 cmpcomps(_comp_t *comp1, _comp_t *comp2)
1152 long diff = comp1->code1 - comp2->code1;
1155 diff = comp1->code2 - comp2->code2;
1160 * Load composition exclusion data
1163 read_compexdata(FILE *in)
1165 unsigned short i, code;
1168 (void) memset((char *) compexs, 0, sizeof(unsigned long) << 11);
1170 while (fgets(line, sizeof(line), in)) {
1171 if( (s=strchr(line, '\n')) ) *s = '\0';
1173 * Skip blank lines and lines that start with a '#'.
1175 if (line[0] == 0 || line[0] == '#')
1179 * Collect the code. Assume max 4 digits
1182 for (s = line, i = code = 0; *s != '#' && i < 4; i++, s++) {
1184 if (*s >= '0' && *s <= '9')
1186 else if (*s >= 'A' && *s <= 'F')
1187 code += (*s - 'A') + 10;
1188 else if (*s >= 'a' && *s <= 'f')
1189 code += (*s - 'a') + 10;
1196 * Creates array of compositions from decomposition array
1201 unsigned long i, cu;
1203 comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
1205 for (i = cu = 0; i < decomps_used; i++) {
1206 if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
1208 comps[cu].comp = decomps[i].code;
1209 comps[cu].count = 2;
1210 comps[cu].code1 = decomps[i].decomp[0];
1211 comps[cu].code2 = decomps[i].decomp[1];
1215 qsort(comps, comps_used, sizeof(_comp_t),
1216 (int (*)(const void *, const void *)) cmpcomps);
1220 write_cdata(char *opath)
1224 unsigned long i, idx, nprops;
1225 unsigned short casecnt[2];
1228 /*****************************************************************
1230 * Generate the ctype data.
1232 *****************************************************************/
1235 * Open the ctype.dat file.
1237 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "ctype.dat", opath);
1238 if ((out = fopen(path, "wb")) == 0)
1242 * Collect the offsets for the properties. The offsets array is
1243 * on a 4-byte boundary to keep things efficient for architectures
1244 * that need such a thing.
1246 for (i = idx = 0; i < NUMPROPS; i++) {
1247 propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
1248 idx += proptbl[i].used;
1252 * Add the sentinel index which is used by the binary search as the upper
1253 * bound for a search.
1258 * Record the actual number of property lists. This may be different than
1259 * the number of offsets actually written because of aligning on a 4-byte
1265 * Calculate the byte count needed and pad the property counts array to a
1268 if ((bytes = sizeof(unsigned short) * (NUMPROPS + 1)) & 3)
1269 bytes += 4 - (bytes & 3);
1270 nprops = bytes / sizeof(unsigned short);
1271 bytes += sizeof(unsigned long) * idx;
1276 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1279 * Write the byte count.
1281 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1284 * Write the property list counts.
1286 fwrite((char *) propcnt, sizeof(unsigned short), nprops, out);
1289 * Write the property lists.
1291 for (i = 0; i < NUMPROPS; i++) {
1292 if (proptbl[i].used > 0)
1293 fwrite((char *) proptbl[i].ranges, sizeof(unsigned long),
1294 proptbl[i].used, out);
1299 /*****************************************************************
1301 * Generate the case mapping data.
1303 *****************************************************************/
1306 * Open the case.dat file.
1308 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "case.dat", opath);
1309 if ((out = fopen(path, "wb")) == 0)
1313 * Write the case mapping tables.
1315 hdr[1] = upper_used + lower_used + title_used;
1316 casecnt[0] = upper_used;
1317 casecnt[1] = lower_used;
1322 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1325 * Write the upper and lower case table sizes.
1327 fwrite((char *) casecnt, sizeof(unsigned short), 2, out);
1331 * Write the upper case table.
1333 fwrite((char *) upper, sizeof(_case_t), upper_used, out);
1337 * Write the lower case table.
1339 fwrite((char *) lower, sizeof(_case_t), lower_used, out);
1343 * Write the title case table.
1345 fwrite((char *) title, sizeof(_case_t), title_used, out);
1349 /*****************************************************************
1351 * Generate the composition data.
1353 *****************************************************************/
1356 * Create compositions from decomposition data
1361 * Open the comp.dat file.
1363 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "comp.dat", opath);
1364 if ((out = fopen(path, "wb")) == 0)
1370 hdr[1] = (unsigned short) comps_used * 4;
1371 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1374 * Write out the byte count to maintain header size.
1376 bytes = comps_used * sizeof(_comp_t);
1377 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1380 * Now, if comps exist, write them out.
1383 fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
1387 /*****************************************************************
1389 * Generate the decomposition data.
1391 *****************************************************************/
1394 * Fully expand all decompositions before generating the output file.
1399 * Open the decomp.dat file.
1401 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "decomp.dat", opath);
1402 if ((out = fopen(path, "wb")) == 0)
1405 hdr[1] = decomps_used;
1410 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1413 * Write a temporary byte count which will be calculated as the
1414 * decompositions are written out.
1417 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1421 * Write the list of decomp nodes.
1423 for (i = idx = 0; i < decomps_used; i++) {
1424 fwrite((char *) &decomps[i].code, sizeof(unsigned long), 1, out);
1425 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1426 idx += decomps[i].used;
1430 * Write the sentinel index as the last decomp node.
1432 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1435 * Write the decompositions themselves.
1437 for (i = 0; i < decomps_used; i++)
1438 fwrite((char *) decomps[i].decomp, sizeof(unsigned long),
1439 decomps[i].used, out);
1442 * Seek back to the beginning and write the byte count.
1444 bytes = (sizeof(unsigned long) * idx) +
1445 (sizeof(unsigned long) * ((hdr[1] << 1) + 1));
1446 fseek(out, sizeof(unsigned short) << 1, 0L);
1447 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1453 * Open the kdecomp.dat file.
1455 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "kdecomp.dat", opath);
1456 if ((out = fopen(path, "wb")) == 0)
1459 hdr[1] = kdecomps_used;
1464 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1467 * Write a temporary byte count which will be calculated as the
1468 * decompositions are written out.
1471 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1473 if (kdecomps_used) {
1475 * Write the list of kdecomp nodes.
1477 for (i = idx = 0; i < kdecomps_used; i++) {
1478 fwrite((char *) &kdecomps[i].code, sizeof(unsigned long), 1, out);
1479 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1480 idx += kdecomps[i].used;
1484 * Write the sentinel index as the last decomp node.
1486 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1489 * Write the decompositions themselves.
1491 for (i = 0; i < kdecomps_used; i++)
1492 fwrite((char *) kdecomps[i].decomp, sizeof(unsigned long),
1493 kdecomps[i].used, out);
1496 * Seek back to the beginning and write the byte count.
1498 bytes = (sizeof(unsigned long) * idx) +
1499 (sizeof(unsigned long) * ((hdr[1] << 1) + 1));
1500 fseek(out, sizeof(unsigned short) << 1, 0L);
1501 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1506 /*****************************************************************
1508 * Generate the combining class data.
1510 *****************************************************************/
1513 * Open the cmbcl.dat file.
1515 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "cmbcl.dat", opath);
1516 if ((out = fopen(path, "wb")) == 0)
1520 * Set the number of ranges used. Each range has a combining class which
1521 * means each entry is a 3-tuple.
1523 hdr[1] = ccl_used / 3;
1528 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1531 * Write out the byte count to maintain header size.
1533 bytes = ccl_used * sizeof(unsigned long);
1534 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1538 * Write the combining class ranges out.
1540 fwrite((char *) ccl, sizeof(unsigned long), ccl_used, out);
1544 /*****************************************************************
1546 * Generate the number data.
1548 *****************************************************************/
1551 * Open the num.dat file.
1553 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "num.dat", opath);
1554 if ((out = fopen(path, "wb")) == 0)
1558 * The count part of the header will be the total number of codes that
1561 hdr[1] = (unsigned short) (ncodes_used << 1);
1562 bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
1567 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1570 * Write out the byte count to maintain header size.
1572 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1575 * Now, if number mappings exist, write them out.
1577 if (ncodes_used > 0) {
1578 fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
1579 fwrite((char *) nums, sizeof(_num_t), nums_used, out);
1589 "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
1590 fprintf(stderr, " datafile1 datafile2 ...\n\n");
1592 "-o output-directory\n\t\tWrite the output files to a different");
1593 fprintf(stderr, " directory (default: .).\n");
1595 "-x composition-exclusion\n\t\tFile of composition codes");
1596 fprintf(stderr, " that should be excluded.\n");
1601 main(int argc, char *argv[])
1606 prog = lutil_progname( "ucgendat", argc, argv );
1615 if (argv[0][0] == '-') {
1616 switch (argv[0][1]) {
1625 if ((in = fopen(argv[0], "rb")) == 0)
1627 "%s: unable to open composition exclusion file %s\n",
1630 read_compexdata(in);
1639 if (in != stdin && in != NULL)
1641 if ((in = fopen(argv[0], "rb")) == 0)
1642 fprintf(stderr, "%s: unable to open ctype file %s\n",