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"
34 #include <ac/stdlib.h>
35 #include <ac/string.h>
36 #include <ac/unistd.h>
39 #define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\
40 ((cc) >= 'A' && (cc) <= 'F') ||\
41 ((cc) >= 'a' && (cc) <= 'f'))
44 * A header written to the output file with the byte-order-mark and the number
47 static unsigned short hdr[2] = {0xfeff, 0};
50 #define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3)))
58 * List of properties expected to be found in the Unicode Character Database
59 * including some implementation specific properties.
61 * The implementation specific properties are:
62 * Cm = Composed (can be decomposed)
64 * Sy = Symmetric (has left and right forms)
69 * Cp = Defined character
71 static _prop_t props[NUMPROPS] = {
72 {"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2},
73 {"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2},
74 {"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2},
75 {"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2},
76 {"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L", 1}, {"R", 1},
77 {"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B", 1},
78 {"S", 1}, {"WS", 2}, {"ON", 2},
79 {"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2},
80 {"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2}, {"AL", 2}
84 unsigned long *ranges;
89 static _ranges_t proptbl[NUMPROPS];
92 * Make sure this array is sized to be on a 4-byte boundary at compile time.
94 static unsigned short propcnt[NEEDPROPS];
97 * Array used to collect a decomposition before adding it to the decomposition
100 static unsigned long dectmp[64];
101 static unsigned long dectmp_size;
107 unsigned long *decomp;
111 * List of decomposition. Created and expanded in order as the characters are
112 * encountered. First list contains canonical mappings, second also includes
113 * compatibility mappings.
115 static _decomp_t *decomps;
116 static unsigned long decomps_used;
117 static unsigned long decomps_size;
119 static _decomp_t *kdecomps;
120 static unsigned long kdecomps_used;
121 static unsigned long kdecomps_size;
124 * Composition exclusion table stuff.
126 #define COMPEX_SET(c) (compexs[(c) >> 5] |= (1 << ((c) & 31)))
127 #define COMPEX_TEST(c) (compexs[(c) >> 5] & (1 << ((c) & 31)))
128 static unsigned long compexs[2048];
131 * Struct for holding a composition pair, and array of composition pairs
140 static _comp_t *comps;
141 static unsigned long comps_used;
144 * Types and lists for handling lists of case mappings.
148 unsigned long other1;
149 unsigned long other2;
152 static _case_t *upper;
153 static _case_t *lower;
154 static _case_t *title;
155 static unsigned long upper_used;
156 static unsigned long upper_size;
157 static unsigned long lower_used;
158 static unsigned long lower_size;
159 static unsigned long title_used;
160 static unsigned long title_size;
163 * Array used to collect case mappings before adding them to a list.
165 static unsigned long cases[3];
168 * An array to hold ranges for combining classes.
170 static unsigned long *ccl;
171 static unsigned long ccl_used;
172 static unsigned long ccl_size;
175 * Structures for handling numbers.
188 * Arrays to hold the mapping of codes to numbers.
190 static _codeidx_t *ncodes;
191 static unsigned long ncodes_used;
192 static unsigned long ncodes_size;
195 static unsigned long nums_used;
196 static unsigned long nums_size;
199 * Array for holding numbers.
202 static unsigned long nums_used;
203 static unsigned long nums_size;
206 add_range(unsigned long start, unsigned long end, char *p1, char *p2)
212 for (k = 0; k < 2; k++) {
224 for (i = 0; i < NUMPROPS; i++) {
225 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
235 * Resize the range list if necessary.
237 if (rlp->used == rlp->size) {
239 rlp->ranges = (unsigned long *)
240 malloc(sizeof(unsigned long) << 3);
242 rlp->ranges = (unsigned long *)
243 realloc((char *) rlp->ranges,
244 sizeof(unsigned long) * (rlp->size + 8));
249 * If this is the first code for this property list, just add it
252 if (rlp->used == 0) {
253 rlp->ranges[0] = start;
254 rlp->ranges[1] = end;
260 * Optimize the case of adding the range to the end.
263 if (start > rlp->ranges[j]) {
265 rlp->ranges[j++] = start;
266 rlp->ranges[j++] = end;
272 * Need to locate the insertion point.
275 i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
278 * If the start value lies in the current range, then simply set the
279 * new end point of the range to the end value passed as a parameter.
281 if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
282 rlp->ranges[i + 1] = end;
287 * Shift following values up by two.
289 for (j = rlp->used; j > i; j -= 2) {
290 rlp->ranges[j] = rlp->ranges[j - 2];
291 rlp->ranges[j + 1] = rlp->ranges[j - 1];
295 * Add the new range at the insertion point.
297 rlp->ranges[i] = start;
298 rlp->ranges[i + 1] = end;
304 ordered_range_insert(unsigned long c, char *name, int len)
314 * Deal with directionality codes introduced in Unicode 3.0.
316 if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
318 (memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
319 memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
320 memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
322 * Mark all of these as Other Neutral to preserve compatibility with
329 for (i = 0; i < NUMPROPS; i++) {
330 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
338 * Have a match, so insert the code in order.
343 * Resize the range list if necessary.
345 if (rlp->used == rlp->size) {
347 rlp->ranges = (unsigned long *)
348 malloc(sizeof(unsigned long) << 3);
350 rlp->ranges = (unsigned long *)
351 realloc((char *) rlp->ranges,
352 sizeof(unsigned long) * (rlp->size + 8));
357 * If this is the first code for this property list, just add it
360 if (rlp->used == 0) {
361 rlp->ranges[0] = rlp->ranges[1] = c;
367 * Optimize the cases of extending the last range and adding new ranges to
372 s = rlp->ranges[j - 1];
376 * Extend the last range.
384 * Start another range on the end.
387 rlp->ranges[j] = rlp->ranges[j + 1] = c;
394 * The code is a duplicate of a code in the last range, so just return.
399 * The code should be inserted somewhere before the last range in the
400 * list. Locate the insertion point.
403 i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
406 e = rlp->ranges[i + 1];
410 * Simply extend the current range.
412 rlp->ranges[i + 1] = c;
415 * Add a new entry before the current location. Shift all entries
416 * before the current one up by one to make room.
418 for (j = rlp->used; j > i; j -= 2) {
419 rlp->ranges[j] = rlp->ranges[j - 2];
420 rlp->ranges[j + 1] = rlp->ranges[j - 1];
422 rlp->ranges[i] = rlp->ranges[i + 1] = c;
429 add_decomp(unsigned long code, short compat)
431 unsigned long i, j, size;
432 _decomp_t **pdecomps;
433 unsigned long *pdecomps_used;
434 unsigned long *pdecomps_size;
437 pdecomps = &kdecomps;
438 pdecomps_used = &kdecomps_used;
439 pdecomps_size = &kdecomps_size;
442 pdecomps_used = &decomps_used;
443 pdecomps_size = &decomps_size;
447 * Add the code to the composite property.
450 ordered_range_insert(code, "Cm", 2);
454 * Locate the insertion point for the code.
456 for (i = 0; i < *pdecomps_used && code > (*pdecomps)[i].code; i++) ;
459 * Allocate space for a new decomposition.
461 if (*pdecomps_used == *pdecomps_size) {
462 if (*pdecomps_size == 0)
463 *pdecomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
465 *pdecomps = (_decomp_t *)
466 realloc((char *) *pdecomps,
467 sizeof(_decomp_t) * (*pdecomps_size + 8));
468 (void) memset((char *) (*pdecomps + *pdecomps_size), '\0',
469 sizeof(_decomp_t) << 3);
473 if (i < *pdecomps_used && code != (*pdecomps)[i].code) {
475 * Shift the decomps up by one if the codes don't match.
477 for (j = *pdecomps_used; j > i; j--)
478 (void) AC_MEMCPY((char *) &(*pdecomps)[j], (char *) &(*pdecomps)[j - 1],
483 * Insert or replace a decomposition.
485 size = dectmp_size + (4 - (dectmp_size & 3));
486 if ((*pdecomps)[i].size < size) {
487 if ((*pdecomps)[i].size == 0)
488 (*pdecomps)[i].decomp = (unsigned long *)
489 malloc(sizeof(unsigned long) * size);
491 (*pdecomps)[i].decomp = (unsigned long *)
492 realloc((char *) (*pdecomps)[i].decomp,
493 sizeof(unsigned long) * size);
494 (*pdecomps)[i].size = size;
497 if ((*pdecomps)[i].code != code)
500 (*pdecomps)[i].code = code;
501 (*pdecomps)[i].used = dectmp_size;
502 (void) AC_MEMCPY((char *) (*pdecomps)[i].decomp, (char *) dectmp,
503 sizeof(unsigned long) * dectmp_size);
506 * NOTICE: This needs changing later so it is more general than simply
507 * pairs. This calculation is done here to simplify allocation elsewhere.
509 if (!compat && dectmp_size == 2)
514 add_title(unsigned long code)
519 * Always map the code to itself.
523 if (title_used == title_size) {
525 title = (_case_t *) malloc(sizeof(_case_t) << 3);
527 title = (_case_t *) realloc((char *) title,
528 sizeof(_case_t) * (title_size + 8));
533 * Locate the insertion point.
535 for (i = 0; i < title_used && code > title[i].key; i++) ;
537 if (i < title_used) {
539 * Shift the array up by one.
541 for (j = title_used; j > i; j--)
542 (void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
546 title[i].key = cases[2]; /* Title */
547 title[i].other1 = cases[0]; /* Upper */
548 title[i].other2 = cases[1]; /* Lower */
554 add_upper(unsigned long code)
559 * Always map the code to itself.
564 * If the title case character is not present, then make it the same as
570 if (upper_used == upper_size) {
572 upper = (_case_t *) malloc(sizeof(_case_t) << 3);
574 upper = (_case_t *) realloc((char *) upper,
575 sizeof(_case_t) * (upper_size + 8));
580 * Locate the insertion point.
582 for (i = 0; i < upper_used && code > upper[i].key; i++) ;
584 if (i < upper_used) {
586 * Shift the array up by one.
588 for (j = upper_used; j > i; j--)
589 (void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
593 upper[i].key = cases[0]; /* Upper */
594 upper[i].other1 = cases[1]; /* Lower */
595 upper[i].other2 = cases[2]; /* Title */
601 add_lower(unsigned long code)
606 * Always map the code to itself.
611 * If the title case character is empty, then make it the same as the
617 if (lower_used == lower_size) {
619 lower = (_case_t *) malloc(sizeof(_case_t) << 3);
621 lower = (_case_t *) realloc((char *) lower,
622 sizeof(_case_t) * (lower_size + 8));
627 * Locate the insertion point.
629 for (i = 0; i < lower_used && code > lower[i].key; i++) ;
631 if (i < lower_used) {
633 * Shift the array up by one.
635 for (j = lower_used; j > i; j--)
636 (void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
640 lower[i].key = cases[1]; /* Lower */
641 lower[i].other1 = cases[0]; /* Upper */
642 lower[i].other2 = cases[2]; /* Title */
648 ordered_ccl_insert(unsigned long c, unsigned long ccl_code)
652 if (ccl_used == ccl_size) {
654 ccl = (unsigned long *) malloc(sizeof(unsigned long) * 24);
656 ccl = (unsigned long *)
657 realloc((char *) ccl, sizeof(unsigned long) * (ccl_size + 24));
662 * Optimize adding the first item.
672 * Handle the special case of extending the range on the end. This
673 * requires that the combining class codes are the same.
675 if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
676 ccl[ccl_used - 2] = c;
681 * Handle the special case of adding another range on the end.
683 if (c > ccl[ccl_used - 2] + 1 ||
684 (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
687 ccl[ccl_used++] = ccl_code;
692 * Locate either the insertion point or range for the code.
694 for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
696 if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
698 * Extend an existing range.
702 } else if (c < ccl[i]) {
704 * Start a new range before the current location.
706 for (j = ccl_used; j > i; j -= 3) {
708 ccl[j - 1] = ccl[j - 4];
709 ccl[j - 2] = ccl[j - 5];
711 ccl[i] = ccl[i + 1] = c;
712 ccl[i + 2] = ccl_code;
717 * Adds a number if it does not already exist and returns an index value
721 make_number(short num, short denom)
726 * Determine if the number already exists.
728 for (n = 0; n < nums_used; n++) {
729 if (nums[n].numerator == num && nums[n].denominator == denom)
733 if (nums_used == nums_size) {
735 nums = (_num_t *) malloc(sizeof(_num_t) << 3);
737 nums = (_num_t *) realloc((char *) nums,
738 sizeof(_num_t) * (nums_size + 8));
743 nums[n].numerator = num;
744 nums[n].denominator = denom;
750 add_number(unsigned long code, short num, short denom)
755 * Insert the code in order.
757 for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
760 * Handle the case of the codes matching and simply replace the number
761 * that was there before.
763 if (i < ncodes_used && code == ncodes[i].code) {
764 ncodes[i].idx = make_number(num, denom);
769 * Resize the array if necessary.
771 if (ncodes_used == ncodes_size) {
772 if (ncodes_size == 0)
773 ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
775 ncodes = (_codeidx_t *)
776 realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
782 * Shift things around to insert the code if necessary.
784 if (i < ncodes_used) {
785 for (j = ncodes_used; j > i; j--) {
786 ncodes[j].code = ncodes[j - 1].code;
787 ncodes[j].idx = ncodes[j - 1].idx;
790 ncodes[i].code = code;
791 ncodes[i].idx = make_number(num, denom);
797 * This routine assumes that the line is a valid Unicode Character Database
803 unsigned long i, lineno, skip, code, ccl_code;
804 short wnum, neg, number[2], compat;
805 char line[512], *s, *e;
808 while (fgets(line, sizeof(line), in)) {
809 if( (s=strchr(line, '\n')) ) *s = '\0';
813 * Skip blank lines and lines that start with a '#'.
815 if (line[0] == 0 || line[0] == '#')
819 * If lines need to be skipped, do it here.
827 * Collect the code. The code can be up to 6 hex digits in length to
828 * allow surrogates to be specified.
830 for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
832 if (*s >= '0' && *s <= '9')
834 else if (*s >= 'A' && *s <= 'F')
835 code += (*s - 'A') + 10;
836 else if (*s >= 'a' && *s <= 'f')
837 code += (*s - 'a') + 10;
841 * Handle the following special cases:
842 * 1. 4E00-9FA5 CJK Ideographs.
843 * 2. AC00-D7A3 Hangul Syllables.
844 * 3. D800-DFFF Surrogates.
845 * 4. E000-F8FF Private Use Area.
846 * 5. F900-FA2D Han compatibility.
851 * The Han ideographs.
853 add_range(0x4e00, 0x9fff, "Lo", "L");
856 * Add the characters to the defined category.
858 add_range(0x4e00, 0x9fa5, "Cp", 0);
864 * The Hangul syllables.
866 add_range(0xac00, 0xd7a3, "Lo", "L");
869 * Add the characters to the defined category.
871 add_range(0xac00, 0xd7a3, "Cp", 0);
877 * Make a range of all surrogates and assume some default
880 add_range(0x010000, 0x10ffff, "Cs", "L");
885 * The Private Use area. Add with a default set of properties.
887 add_range(0xe000, 0xf8ff, "Co", "L");
892 * The CJK compatibility area.
894 add_range(0xf900, 0xfaff, "Lo", "L");
897 * Add the characters to the defined category.
899 add_range(0xf900, 0xfaff, "Cp", 0);
908 * Add the code to the defined category.
910 ordered_range_insert(code, "Cp", 2);
913 * Locate the first character property field.
915 for (i = 0; *s != 0 && i < 2; s++) {
919 for (e = s; *e && *e != ';'; e++) ;
921 ordered_range_insert(code, s, e - s);
924 * Locate the combining class code.
926 for (s = e; *s != 0 && i < 3; s++) {
932 * Convert the combining class code from decimal.
934 for (ccl_code = 0, e = s; *e && *e != ';'; e++)
935 ccl_code = (ccl_code * 10) + (*e - '0');
938 * Add the code if it not 0.
941 ordered_ccl_insert(code, ccl_code);
944 * Locate the second character property field.
946 for (s = e; *s != 0 && i < 4; s++) {
950 for (e = s; *e && *e != ';'; e++) ;
952 ordered_range_insert(code, s, e - s);
955 * Check for a decomposition.
962 * Skip compatibility formatting tag.
967 * Collect the codes of the decomposition.
969 for (dectmp_size = 0; *s != ';'; ) {
971 * Skip all leading non-hex digits.
973 while (!ishdigit(*s))
976 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
977 dectmp[dectmp_size] <<= 4;
978 if (*s >= '0' && *s <= '9')
979 dectmp[dectmp_size] += *s - '0';
980 else if (*s >= 'A' && *s <= 'F')
981 dectmp[dectmp_size] += (*s - 'A') + 10;
982 else if (*s >= 'a' && *s <= 'f')
983 dectmp[dectmp_size] += (*s - 'a') + 10;
989 * If there are any codes in the temporary decomposition array,
990 * then add the character with its decomposition.
992 if (dectmp_size > 0) {
1001 * Skip to the number field.
1003 for (i = 0; i < 3 && *s; s++) {
1009 * Scan the number in.
1011 number[0] = number[1] = 0;
1012 for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
1020 * Move the the denominator of the fraction.
1028 number[wnum] = (number[wnum] * 10) + (*e - '0');
1033 * Adjust the denominator in case of integers and add the number.
1036 number[1] = number[0];
1038 add_number(code, number[0], number[1]);
1042 * Skip to the start of the possible case mappings.
1044 for (s = e, i = 0; i < 4 && *s; s++) {
1050 * Collect the case mappings.
1052 cases[0] = cases[1] = cases[2] = 0;
1053 for (i = 0; i < 3; i++) {
1054 while (ishdigit(*s)) {
1056 if (*s >= '0' && *s <= '9')
1057 cases[i] += *s - '0';
1058 else if (*s >= 'A' && *s <= 'F')
1059 cases[i] += (*s - 'A') + 10;
1060 else if (*s >= 'a' && *s <= 'f')
1061 cases[i] += (*s - 'a') + 10;
1067 if (cases[0] && cases[1])
1069 * Add the upper and lower mappings for a title case character.
1074 * Add the lower and title case mappings for the upper case
1080 * Add the upper and title case mappings for the lower case
1088 find_decomp(unsigned long code, short compat)
1094 r = (compat ? kdecomps_used : decomps_used) - 1;
1095 decs = compat ? kdecomps : decomps;
1098 if (code > decs[m].code)
1100 else if (code < decs[m].code)
1109 decomp_it(_decomp_t *d, short compat)
1114 for (i = 0; i < d->used; i++) {
1115 if ((dp = find_decomp(d->decomp[i], compat)) != 0)
1116 decomp_it(dp, compat);
1118 dectmp[dectmp_size++] = d->decomp[i];
1123 * Expand all decompositions by recursively decomposing each character
1124 * in the decomposition.
1131 for (i = 0; i < decomps_used; i++) {
1133 decomp_it(&decomps[i], 0);
1134 if (dectmp_size > 0)
1135 add_decomp(decomps[i].code, 0);
1138 for (i = 0; i < kdecomps_used; i++) {
1140 decomp_it(&kdecomps[i], 1);
1141 if (dectmp_size > 0)
1142 add_decomp(kdecomps[i].code, 1);
1147 cmpcomps(_comp_t *comp1, _comp_t *comp2)
1149 long diff = comp1->code1 - comp2->code1;
1152 diff = comp1->code2 - comp2->code2;
1157 * Load composition exclusion data
1160 read_compexdata(FILE *in)
1162 unsigned short i, code;
1165 (void) memset((char *) compexs, 0, sizeof(unsigned long) << 11);
1167 while (fgets(line, sizeof(line), in)) {
1168 if( (s=strchr(line, '\n')) ) *s = '\0';
1170 * Skip blank lines and lines that start with a '#'.
1172 if (line[0] == 0 || line[0] == '#')
1176 * Collect the code. Assume max 4 digits
1179 for (s = line, i = code = 0; *s != '#' && i < 4; i++, s++) {
1181 if (*s >= '0' && *s <= '9')
1183 else if (*s >= 'A' && *s <= 'F')
1184 code += (*s - 'A') + 10;
1185 else if (*s >= 'a' && *s <= 'f')
1186 code += (*s - 'a') + 10;
1193 * Creates array of compositions from decomposition array
1198 unsigned long i, cu;
1200 comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
1202 for (i = cu = 0; i < decomps_used; i++) {
1203 if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
1205 comps[cu].comp = decomps[i].code;
1206 comps[cu].count = 2;
1207 comps[cu].code1 = decomps[i].decomp[0];
1208 comps[cu].code2 = decomps[i].decomp[1];
1212 qsort(comps, comps_used, sizeof(_comp_t),
1213 (int (*)(const void *, const void *)) cmpcomps);
1217 write_cdata(char *opath)
1221 unsigned long i, idx, nprops;
1222 unsigned short casecnt[2];
1225 /*****************************************************************
1227 * Generate the ctype data.
1229 *****************************************************************/
1232 * Open the ctype.dat file.
1234 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "ctype.dat", opath);
1235 if ((out = fopen(path, "wb")) == 0)
1239 * Collect the offsets for the properties. The offsets array is
1240 * on a 4-byte boundary to keep things efficient for architectures
1241 * that need such a thing.
1243 for (i = idx = 0; i < NUMPROPS; i++) {
1244 propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
1245 idx += proptbl[i].used;
1249 * Add the sentinel index which is used by the binary search as the upper
1250 * bound for a search.
1255 * Record the actual number of property lists. This may be different than
1256 * the number of offsets actually written because of aligning on a 4-byte
1262 * Calculate the byte count needed and pad the property counts array to a
1265 if ((bytes = sizeof(unsigned short) * (NUMPROPS + 1)) & 3)
1266 bytes += 4 - (bytes & 3);
1267 nprops = bytes / sizeof(unsigned short);
1268 bytes += sizeof(unsigned long) * idx;
1273 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1276 * Write the byte count.
1278 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1281 * Write the property list counts.
1283 fwrite((char *) propcnt, sizeof(unsigned short), nprops, out);
1286 * Write the property lists.
1288 for (i = 0; i < NUMPROPS; i++) {
1289 if (proptbl[i].used > 0)
1290 fwrite((char *) proptbl[i].ranges, sizeof(unsigned long),
1291 proptbl[i].used, out);
1296 /*****************************************************************
1298 * Generate the case mapping data.
1300 *****************************************************************/
1303 * Open the case.dat file.
1305 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "case.dat", opath);
1306 if ((out = fopen(path, "wb")) == 0)
1310 * Write the case mapping tables.
1312 hdr[1] = upper_used + lower_used + title_used;
1313 casecnt[0] = upper_used;
1314 casecnt[1] = lower_used;
1319 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1322 * Write the upper and lower case table sizes.
1324 fwrite((char *) casecnt, sizeof(unsigned short), 2, out);
1328 * Write the upper case table.
1330 fwrite((char *) upper, sizeof(_case_t), upper_used, out);
1334 * Write the lower case table.
1336 fwrite((char *) lower, sizeof(_case_t), lower_used, out);
1340 * Write the title case table.
1342 fwrite((char *) title, sizeof(_case_t), title_used, out);
1346 /*****************************************************************
1348 * Generate the composition data.
1350 *****************************************************************/
1353 * Create compositions from decomposition data
1358 * Open the comp.dat file.
1360 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "comp.dat", opath);
1361 if ((out = fopen(path, "wb")) == 0)
1367 hdr[1] = (unsigned short) comps_used * 4;
1368 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1371 * Write out the byte count to maintain header size.
1373 bytes = comps_used * sizeof(_comp_t);
1374 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1377 * Now, if comps exist, write them out.
1380 fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
1384 /*****************************************************************
1386 * Generate the decomposition data.
1388 *****************************************************************/
1391 * Fully expand all decompositions before generating the output file.
1396 * Open the decomp.dat file.
1398 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "decomp.dat", opath);
1399 if ((out = fopen(path, "wb")) == 0)
1402 hdr[1] = decomps_used;
1407 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1410 * Write a temporary byte count which will be calculated as the
1411 * decompositions are written out.
1414 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1418 * Write the list of decomp nodes.
1420 for (i = idx = 0; i < decomps_used; i++) {
1421 fwrite((char *) &decomps[i].code, sizeof(unsigned long), 1, out);
1422 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1423 idx += decomps[i].used;
1427 * Write the sentinel index as the last decomp node.
1429 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1432 * Write the decompositions themselves.
1434 for (i = 0; i < decomps_used; i++)
1435 fwrite((char *) decomps[i].decomp, sizeof(unsigned long),
1436 decomps[i].used, out);
1439 * Seek back to the beginning and write the byte count.
1441 bytes = (sizeof(unsigned long) * idx) +
1442 (sizeof(unsigned long) * ((hdr[1] << 1) + 1));
1443 fseek(out, sizeof(unsigned short) << 1, 0L);
1444 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1450 * Open the kdecomp.dat file.
1452 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "kdecomp.dat", opath);
1453 if ((out = fopen(path, "wb")) == 0)
1456 hdr[1] = kdecomps_used;
1461 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1464 * Write a temporary byte count which will be calculated as the
1465 * decompositions are written out.
1468 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1470 if (kdecomps_used) {
1472 * Write the list of kdecomp nodes.
1474 for (i = idx = 0; i < kdecomps_used; i++) {
1475 fwrite((char *) &kdecomps[i].code, sizeof(unsigned long), 1, out);
1476 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1477 idx += kdecomps[i].used;
1481 * Write the sentinel index as the last decomp node.
1483 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1486 * Write the decompositions themselves.
1488 for (i = 0; i < kdecomps_used; i++)
1489 fwrite((char *) kdecomps[i].decomp, sizeof(unsigned long),
1490 kdecomps[i].used, out);
1493 * Seek back to the beginning and write the byte count.
1495 bytes = (sizeof(unsigned long) * idx) +
1496 (sizeof(unsigned long) * ((hdr[1] << 1) + 1));
1497 fseek(out, sizeof(unsigned short) << 1, 0L);
1498 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1503 /*****************************************************************
1505 * Generate the combining class data.
1507 *****************************************************************/
1510 * Open the cmbcl.dat file.
1512 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "cmbcl.dat", opath);
1513 if ((out = fopen(path, "wb")) == 0)
1517 * Set the number of ranges used. Each range has a combining class which
1518 * means each entry is a 3-tuple.
1520 hdr[1] = ccl_used / 3;
1525 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1528 * Write out the byte count to maintain header size.
1530 bytes = ccl_used * sizeof(unsigned long);
1531 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1535 * Write the combining class ranges out.
1537 fwrite((char *) ccl, sizeof(unsigned long), ccl_used, out);
1541 /*****************************************************************
1543 * Generate the number data.
1545 *****************************************************************/
1548 * Open the num.dat file.
1550 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "num.dat", opath);
1551 if ((out = fopen(path, "wb")) == 0)
1555 * The count part of the header will be the total number of codes that
1558 hdr[1] = (unsigned short) (ncodes_used << 1);
1559 bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
1564 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1567 * Write out the byte count to maintain header size.
1569 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1572 * Now, if number mappings exist, write them out.
1574 if (ncodes_used > 0) {
1575 fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
1576 fwrite((char *) nums, sizeof(_num_t), nums_used, out);
1586 "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
1587 fprintf(stderr, " datafile1 datafile2 ...\n\n");
1589 "-o output-directory\n\t\tWrite the output files to a different");
1590 fprintf(stderr, " directory (default: .).\n");
1592 "-x composition-exclusion\n\t\tFile of composition codes");
1593 fprintf(stderr, " that should be excluded.\n");
1598 main(int argc, char *argv[])
1603 prog = lutil_progname( "ucgendat", argc, argv );
1612 if (argv[0][0] == '-') {
1613 switch (argv[0][1]) {
1622 if ((in = fopen(argv[0], "rb")) == 0)
1624 "%s: unable to open composition exclusion file %s\n",
1627 read_compexdata(in);
1636 if (in != stdin && in != NULL)
1638 if ((in = fopen(argv[0], "rb")) == 0)
1639 fprintf(stderr, "%s: unable to open ctype file %s\n",