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>
38 #define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\
39 ((cc) >= 'A' && (cc) <= 'F') ||\
40 ((cc) >= 'a' && (cc) <= 'f'))
43 * A header written to the output file with the byte-order-mark and the number
46 static unsigned short hdr[2] = {0xfeff, 0};
49 #define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3)))
57 * List of properties expected to be found in the Unicode Character Database
58 * including some implementation specific properties.
60 * The implementation specific properties are:
61 * Cm = Composed (can be decomposed)
63 * Sy = Symmetric (has left and right forms)
68 * Cp = Defined character
70 static _prop_t props[NUMPROPS] = {
71 {"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2},
72 {"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2},
73 {"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2},
74 {"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2},
75 {"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L", 1}, {"R", 1},
76 {"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B", 1},
77 {"S", 1}, {"WS", 2}, {"ON", 2},
78 {"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2},
79 {"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2}, {"AL", 2}
83 unsigned long *ranges;
88 static _ranges_t proptbl[NUMPROPS];
91 * Make sure this array is sized to be on a 4-byte boundary at compile time.
93 static unsigned short propcnt[NEEDPROPS];
96 * Array used to collect a decomposition before adding it to the decomposition
99 static unsigned long dectmp[64];
100 static unsigned long dectmp_size;
106 unsigned long *decomp;
110 * List of decomposition. Created and expanded in order as the characters are
111 * encountered. First list contains canonical mappings, second also includes
112 * compatibility mappings.
114 static _decomp_t *decomps;
115 static unsigned long decomps_used;
116 static unsigned long decomps_size;
118 static _decomp_t *kdecomps;
119 static unsigned long kdecomps_used;
120 static unsigned long kdecomps_size;
123 * Composition exclusion table stuff.
125 #define COMPEX_SET(c) (compexs[(c) >> 5] |= (1 << ((c) & 31)))
126 #define COMPEX_TEST(c) (compexs[(c) >> 5] & (1 << ((c) & 31)))
127 static unsigned long compexs[2048];
130 * Struct for holding a composition pair, and array of composition pairs
139 static _comp_t *comps;
140 static unsigned long comps_used;
143 * Types and lists for handling lists of case mappings.
147 unsigned long other1;
148 unsigned long other2;
151 static _case_t *upper;
152 static _case_t *lower;
153 static _case_t *title;
154 static unsigned long upper_used;
155 static unsigned long upper_size;
156 static unsigned long lower_used;
157 static unsigned long lower_size;
158 static unsigned long title_used;
159 static unsigned long title_size;
162 * Array used to collect case mappings before adding them to a list.
164 static unsigned long cases[3];
167 * An array to hold ranges for combining classes.
169 static unsigned long *ccl;
170 static unsigned long ccl_used;
171 static unsigned long ccl_size;
174 * Structures for handling numbers.
187 * Arrays to hold the mapping of codes to numbers.
189 static _codeidx_t *ncodes;
190 static unsigned long ncodes_used;
191 static unsigned long ncodes_size;
194 static unsigned long nums_used;
195 static unsigned long nums_size;
198 * Array for holding numbers.
201 static unsigned long nums_used;
202 static unsigned long nums_size;
205 add_range(unsigned long start, unsigned long end, char *p1, char *p2)
211 for (k = 0; k < 2; k++) {
223 for (i = 0; i < NUMPROPS; i++) {
224 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
234 * Resize the range list if necessary.
236 if (rlp->used == rlp->size) {
238 rlp->ranges = (unsigned long *)
239 malloc(sizeof(unsigned long) << 3);
241 rlp->ranges = (unsigned long *)
242 realloc((char *) rlp->ranges,
243 sizeof(unsigned long) * (rlp->size + 8));
248 * If this is the first code for this property list, just add it
251 if (rlp->used == 0) {
252 rlp->ranges[0] = start;
253 rlp->ranges[1] = end;
259 * Optimize the case of adding the range to the end.
262 if (start > rlp->ranges[j]) {
264 rlp->ranges[j++] = start;
265 rlp->ranges[j++] = end;
271 * Need to locate the insertion point.
274 i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
277 * If the start value lies in the current range, then simply set the
278 * new end point of the range to the end value passed as a parameter.
280 if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
281 rlp->ranges[i + 1] = end;
286 * Shift following values up by two.
288 for (j = rlp->used; j > i; j -= 2) {
289 rlp->ranges[j] = rlp->ranges[j - 2];
290 rlp->ranges[j + 1] = rlp->ranges[j - 1];
294 * Add the new range at the insertion point.
296 rlp->ranges[i] = start;
297 rlp->ranges[i + 1] = end;
303 ordered_range_insert(unsigned long c, char *name, int len)
313 * Deal with directionality codes introduced in Unicode 3.0.
315 if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
317 (memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
318 memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
319 memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
321 * Mark all of these as Other Neutral to preserve compatibility with
328 for (i = 0; i < NUMPROPS; i++) {
329 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
337 * Have a match, so insert the code in order.
342 * Resize the range list if necessary.
344 if (rlp->used == rlp->size) {
346 rlp->ranges = (unsigned long *)
347 malloc(sizeof(unsigned long) << 3);
349 rlp->ranges = (unsigned long *)
350 realloc((char *) rlp->ranges,
351 sizeof(unsigned long) * (rlp->size + 8));
356 * If this is the first code for this property list, just add it
359 if (rlp->used == 0) {
360 rlp->ranges[0] = rlp->ranges[1] = c;
366 * Optimize the cases of extending the last range and adding new ranges to
371 s = rlp->ranges[j - 1];
375 * Extend the last range.
383 * Start another range on the end.
386 rlp->ranges[j] = rlp->ranges[j + 1] = c;
393 * The code is a duplicate of a code in the last range, so just return.
398 * The code should be inserted somewhere before the last range in the
399 * list. Locate the insertion point.
402 i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
405 e = rlp->ranges[i + 1];
409 * Simply extend the current range.
411 rlp->ranges[i + 1] = c;
414 * Add a new entry before the current location. Shift all entries
415 * before the current one up by one to make room.
417 for (j = rlp->used; j > i; j -= 2) {
418 rlp->ranges[j] = rlp->ranges[j - 2];
419 rlp->ranges[j + 1] = rlp->ranges[j - 1];
421 rlp->ranges[i] = rlp->ranges[i + 1] = c;
428 add_decomp(unsigned long code, short compat)
430 unsigned long i, j, size;
431 _decomp_t **pdecomps;
432 unsigned long *pdecomps_used;
433 unsigned long *pdecomps_size;
436 pdecomps = &kdecomps;
437 pdecomps_used = &kdecomps_used;
438 pdecomps_size = &kdecomps_size;
441 pdecomps_used = &decomps_used;
442 pdecomps_size = &decomps_size;
446 * Add the code to the composite property.
449 ordered_range_insert(code, "Cm", 2);
453 * Locate the insertion point for the code.
455 for (i = 0; i < *pdecomps_used && code > (*pdecomps)[i].code; i++) ;
458 * Allocate space for a new decomposition.
460 if (*pdecomps_used == *pdecomps_size) {
461 if (*pdecomps_size == 0)
462 *pdecomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
464 *pdecomps = (_decomp_t *)
465 realloc((char *) *pdecomps,
466 sizeof(_decomp_t) * (*pdecomps_size + 8));
467 (void) memset((char *) (*pdecomps + *pdecomps_size), '\0',
468 sizeof(_decomp_t) << 3);
472 if (i < *pdecomps_used && code != (*pdecomps)[i].code) {
474 * Shift the decomps up by one if the codes don't match.
476 for (j = *pdecomps_used; j > i; j--)
477 (void) AC_MEMCPY((char *) &(*pdecomps)[j], (char *) &(*pdecomps)[j - 1],
482 * Insert or replace a decomposition.
484 size = dectmp_size + (4 - (dectmp_size & 3));
485 if ((*pdecomps)[i].size < size) {
486 if ((*pdecomps)[i].size == 0)
487 (*pdecomps)[i].decomp = (unsigned long *)
488 malloc(sizeof(unsigned long) * size);
490 (*pdecomps)[i].decomp = (unsigned long *)
491 realloc((char *) (*pdecomps)[i].decomp,
492 sizeof(unsigned long) * size);
493 (*pdecomps)[i].size = size;
496 if ((*pdecomps)[i].code != code)
499 (*pdecomps)[i].code = code;
500 (*pdecomps)[i].used = dectmp_size;
501 (void) AC_MEMCPY((char *) (*pdecomps)[i].decomp, (char *) dectmp,
502 sizeof(unsigned long) * dectmp_size);
505 * NOTICE: This needs changing later so it is more general than simply
506 * pairs. This calculation is done here to simplify allocation elsewhere.
508 if (!compat && dectmp_size == 2)
513 add_title(unsigned long code)
518 * Always map the code to itself.
522 if (title_used == title_size) {
524 title = (_case_t *) malloc(sizeof(_case_t) << 3);
526 title = (_case_t *) realloc((char *) title,
527 sizeof(_case_t) * (title_size + 8));
532 * Locate the insertion point.
534 for (i = 0; i < title_used && code > title[i].key; i++) ;
536 if (i < title_used) {
538 * Shift the array up by one.
540 for (j = title_used; j > i; j--)
541 (void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
545 title[i].key = cases[2]; /* Title */
546 title[i].other1 = cases[0]; /* Upper */
547 title[i].other2 = cases[1]; /* Lower */
553 add_upper(unsigned long code)
558 * Always map the code to itself.
563 * If the title case character is not present, then make it the same as
569 if (upper_used == upper_size) {
571 upper = (_case_t *) malloc(sizeof(_case_t) << 3);
573 upper = (_case_t *) realloc((char *) upper,
574 sizeof(_case_t) * (upper_size + 8));
579 * Locate the insertion point.
581 for (i = 0; i < upper_used && code > upper[i].key; i++) ;
583 if (i < upper_used) {
585 * Shift the array up by one.
587 for (j = upper_used; j > i; j--)
588 (void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
592 upper[i].key = cases[0]; /* Upper */
593 upper[i].other1 = cases[1]; /* Lower */
594 upper[i].other2 = cases[2]; /* Title */
600 add_lower(unsigned long code)
605 * Always map the code to itself.
610 * If the title case character is empty, then make it the same as the
616 if (lower_used == lower_size) {
618 lower = (_case_t *) malloc(sizeof(_case_t) << 3);
620 lower = (_case_t *) realloc((char *) lower,
621 sizeof(_case_t) * (lower_size + 8));
626 * Locate the insertion point.
628 for (i = 0; i < lower_used && code > lower[i].key; i++) ;
630 if (i < lower_used) {
632 * Shift the array up by one.
634 for (j = lower_used; j > i; j--)
635 (void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
639 lower[i].key = cases[1]; /* Lower */
640 lower[i].other1 = cases[0]; /* Upper */
641 lower[i].other2 = cases[2]; /* Title */
647 ordered_ccl_insert(unsigned long c, unsigned long ccl_code)
651 if (ccl_used == ccl_size) {
653 ccl = (unsigned long *) malloc(sizeof(unsigned long) * 24);
655 ccl = (unsigned long *)
656 realloc((char *) ccl, sizeof(unsigned long) * (ccl_size + 24));
661 * Optimize adding the first item.
671 * Handle the special case of extending the range on the end. This
672 * requires that the combining class codes are the same.
674 if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
675 ccl[ccl_used - 2] = c;
680 * Handle the special case of adding another range on the end.
682 if (c > ccl[ccl_used - 2] + 1 ||
683 (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
686 ccl[ccl_used++] = ccl_code;
691 * Locate either the insertion point or range for the code.
693 for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
695 if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
697 * Extend an existing range.
701 } else if (c < ccl[i]) {
703 * Start a new range before the current location.
705 for (j = ccl_used; j > i; j -= 3) {
707 ccl[j - 1] = ccl[j - 4];
708 ccl[j - 2] = ccl[j - 5];
710 ccl[i] = ccl[i + 1] = c;
711 ccl[i + 2] = ccl_code;
716 * Adds a number if it does not already exist and returns an index value
720 make_number(short num, short denom)
725 * Determine if the number already exists.
727 for (n = 0; n < nums_used; n++) {
728 if (nums[n].numerator == num && nums[n].denominator == denom)
732 if (nums_used == nums_size) {
734 nums = (_num_t *) malloc(sizeof(_num_t) << 3);
736 nums = (_num_t *) realloc((char *) nums,
737 sizeof(_num_t) * (nums_size + 8));
742 nums[n].numerator = num;
743 nums[n].denominator = denom;
749 add_number(unsigned long code, short num, short denom)
754 * Insert the code in order.
756 for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
759 * Handle the case of the codes matching and simply replace the number
760 * that was there before.
762 if (i < ncodes_used && code == ncodes[i].code) {
763 ncodes[i].idx = make_number(num, denom);
768 * Resize the array if necessary.
770 if (ncodes_used == ncodes_size) {
771 if (ncodes_size == 0)
772 ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
774 ncodes = (_codeidx_t *)
775 realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
781 * Shift things around to insert the code if necessary.
783 if (i < ncodes_used) {
784 for (j = ncodes_used; j > i; j--) {
785 ncodes[j].code = ncodes[j - 1].code;
786 ncodes[j].idx = ncodes[j - 1].idx;
789 ncodes[i].code = code;
790 ncodes[i].idx = make_number(num, denom);
796 * This routine assumes that the line is a valid Unicode Character Database
802 unsigned long i, lineno, skip, code, ccl_code;
803 short wnum, neg, number[2], compat;
804 char line[512], *s, *e;
807 while (fscanf(in, "%[^\n]\n", line) != EOF) {
811 * Skip blank lines and lines that start with a '#'.
813 if (line[0] == 0 || line[0] == '#')
817 * If lines need to be skipped, do it here.
825 * Collect the code. The code can be up to 6 hex digits in length to
826 * allow surrogates to be specified.
828 for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
830 if (*s >= '0' && *s <= '9')
832 else if (*s >= 'A' && *s <= 'F')
833 code += (*s - 'A') + 10;
834 else if (*s >= 'a' && *s <= 'f')
835 code += (*s - 'a') + 10;
839 * Handle the following special cases:
840 * 1. 4E00-9FA5 CJK Ideographs.
841 * 2. AC00-D7A3 Hangul Syllables.
842 * 3. D800-DFFF Surrogates.
843 * 4. E000-F8FF Private Use Area.
844 * 5. F900-FA2D Han compatibility.
849 * The Han ideographs.
851 add_range(0x4e00, 0x9fff, "Lo", "L");
854 * Add the characters to the defined category.
856 add_range(0x4e00, 0x9fa5, "Cp", 0);
862 * The Hangul syllables.
864 add_range(0xac00, 0xd7a3, "Lo", "L");
867 * Add the characters to the defined category.
869 add_range(0xac00, 0xd7a3, "Cp", 0);
875 * Make a range of all surrogates and assume some default
878 add_range(0x010000, 0x10ffff, "Cs", "L");
883 * The Private Use area. Add with a default set of properties.
885 add_range(0xe000, 0xf8ff, "Co", "L");
890 * The CJK compatibility area.
892 add_range(0xf900, 0xfaff, "Lo", "L");
895 * Add the characters to the defined category.
897 add_range(0xf900, 0xfaff, "Cp", 0);
906 * Add the code to the defined category.
908 ordered_range_insert(code, "Cp", 2);
911 * Locate the first character property field.
913 for (i = 0; *s != 0 && i < 2; s++) {
917 for (e = s; *e && *e != ';'; e++) ;
919 ordered_range_insert(code, s, e - s);
922 * Locate the combining class code.
924 for (s = e; *s != 0 && i < 3; s++) {
930 * Convert the combining class code from decimal.
932 for (ccl_code = 0, e = s; *e && *e != ';'; e++)
933 ccl_code = (ccl_code * 10) + (*e - '0');
936 * Add the code if it not 0.
939 ordered_ccl_insert(code, ccl_code);
942 * Locate the second character property field.
944 for (s = e; *s != 0 && i < 4; s++) {
948 for (e = s; *e && *e != ';'; e++) ;
950 ordered_range_insert(code, s, e - s);
953 * Check for a decomposition.
960 * Skip compatibility formatting tag.
965 * Collect the codes of the decomposition.
967 for (dectmp_size = 0; *s != ';'; ) {
969 * Skip all leading non-hex digits.
971 while (!ishdigit(*s))
974 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
975 dectmp[dectmp_size] <<= 4;
976 if (*s >= '0' && *s <= '9')
977 dectmp[dectmp_size] += *s - '0';
978 else if (*s >= 'A' && *s <= 'F')
979 dectmp[dectmp_size] += (*s - 'A') + 10;
980 else if (*s >= 'a' && *s <= 'f')
981 dectmp[dectmp_size] += (*s - 'a') + 10;
987 * If there are any codes in the temporary decomposition array,
988 * then add the character with its decomposition.
990 if (dectmp_size > 0) {
999 * Skip to the number field.
1001 for (i = 0; i < 3 && *s; s++) {
1007 * Scan the number in.
1009 number[0] = number[1] = 0;
1010 for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
1018 * Move the the denominator of the fraction.
1026 number[wnum] = (number[wnum] * 10) + (*e - '0');
1031 * Adjust the denominator in case of integers and add the number.
1034 number[1] = number[0];
1036 add_number(code, number[0], number[1]);
1040 * Skip to the start of the possible case mappings.
1042 for (s = e, i = 0; i < 4 && *s; s++) {
1048 * Collect the case mappings.
1050 cases[0] = cases[1] = cases[2] = 0;
1051 for (i = 0; i < 3; i++) {
1052 while (ishdigit(*s)) {
1054 if (*s >= '0' && *s <= '9')
1055 cases[i] += *s - '0';
1056 else if (*s >= 'A' && *s <= 'F')
1057 cases[i] += (*s - 'A') + 10;
1058 else if (*s >= 'a' && *s <= 'f')
1059 cases[i] += (*s - 'a') + 10;
1065 if (cases[0] && cases[1])
1067 * Add the upper and lower mappings for a title case character.
1072 * Add the lower and title case mappings for the upper case
1078 * Add the upper and title case mappings for the lower case
1086 find_decomp(unsigned long code, short compat)
1092 r = (compat ? kdecomps_used : decomps_used) - 1;
1093 decs = compat ? kdecomps : decomps;
1096 if (code > decs[m].code)
1098 else if (code < decs[m].code)
1107 decomp_it(_decomp_t *d, short compat)
1112 for (i = 0; i < d->used; i++) {
1113 if ((dp = find_decomp(d->decomp[i], compat)) != 0)
1114 decomp_it(dp, compat);
1116 dectmp[dectmp_size++] = d->decomp[i];
1121 * Expand all decompositions by recursively decomposing each character
1122 * in the decomposition.
1129 for (i = 0; i < decomps_used; i++) {
1131 decomp_it(&decomps[i], 0);
1132 if (dectmp_size > 0)
1133 add_decomp(decomps[i].code, 0);
1136 for (i = 0; i < kdecomps_used; i++) {
1138 decomp_it(&kdecomps[i], 1);
1139 if (dectmp_size > 0)
1140 add_decomp(kdecomps[i].code, 1);
1145 cmpcomps(_comp_t *comp1, _comp_t *comp2)
1147 long diff = comp1->code1 - comp2->code1;
1150 diff = comp1->code2 - comp2->code2;
1155 * Load composition exclusion data
1158 read_compexdata(FILE *in)
1160 unsigned short i, code;
1163 (void) memset((char *) compexs, 0, sizeof(unsigned long) << 11);
1165 while (fscanf(in, "%[^\n]\n", line) != EOF) {
1167 * Skip blank lines and lines that start with a '#'.
1169 if (line[0] == 0 || line[0] == '#')
1173 * Collect the code. Assume max 4 digits
1176 for (s = line, i = code = 0; *s != '#' && i < 4; i++, s++) {
1178 if (*s >= '0' && *s <= '9')
1180 else if (*s >= 'A' && *s <= 'F')
1181 code += (*s - 'A') + 10;
1182 else if (*s >= 'a' && *s <= 'f')
1183 code += (*s - 'a') + 10;
1190 * Creates array of compositions from decomposition array
1195 unsigned long i, cu;
1197 comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
1199 for (i = cu = 0; i < decomps_used; i++) {
1200 if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
1202 comps[cu].comp = decomps[i].code;
1203 comps[cu].count = 2;
1204 comps[cu].code1 = decomps[i].decomp[0];
1205 comps[cu].code2 = decomps[i].decomp[1];
1209 qsort(comps, comps_used, sizeof(_comp_t),
1210 (int (*)(const void *, const void *)) cmpcomps);
1214 write_cdata(char *opath)
1217 unsigned long i, idx, bytes, nprops;
1218 unsigned short casecnt[2];
1221 /*****************************************************************
1223 * Generate the ctype data.
1225 *****************************************************************/
1228 * Open the ctype.dat file.
1230 sprintf(path, "%s%sctype.dat", opath, LDAP_DIRSEP);
1231 if ((out = fopen(path, "wb")) == 0)
1235 * Collect the offsets for the properties. The offsets array is
1236 * on a 4-byte boundary to keep things efficient for architectures
1237 * that need such a thing.
1239 for (i = idx = 0; i < NUMPROPS; i++) {
1240 propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
1241 idx += proptbl[i].used;
1245 * Add the sentinel index which is used by the binary search as the upper
1246 * bound for a search.
1251 * Record the actual number of property lists. This may be different than
1252 * the number of offsets actually written because of aligning on a 4-byte
1258 * Calculate the byte count needed and pad the property counts array to a
1261 if ((bytes = sizeof(unsigned short) * (NUMPROPS + 1)) & 3)
1262 bytes += 4 - (bytes & 3);
1263 nprops = bytes / sizeof(unsigned short);
1264 bytes += sizeof(unsigned long) * idx;
1269 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1272 * Write the byte count.
1274 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1277 * Write the property list counts.
1279 fwrite((char *) propcnt, sizeof(unsigned short), nprops, out);
1282 * Write the property lists.
1284 for (i = 0; i < NUMPROPS; i++) {
1285 if (proptbl[i].used > 0)
1286 fwrite((char *) proptbl[i].ranges, sizeof(unsigned long),
1287 proptbl[i].used, out);
1292 /*****************************************************************
1294 * Generate the case mapping data.
1296 *****************************************************************/
1299 * Open the case.dat file.
1301 sprintf(path, "%s%scase.dat", opath, LDAP_DIRSEP);
1302 if ((out = fopen(path, "wb")) == 0)
1306 * Write the case mapping tables.
1308 hdr[1] = upper_used + lower_used + title_used;
1309 casecnt[0] = upper_used;
1310 casecnt[1] = lower_used;
1315 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1318 * Write the upper and lower case table sizes.
1320 fwrite((char *) casecnt, sizeof(unsigned short), 2, out);
1324 * Write the upper case table.
1326 fwrite((char *) upper, sizeof(_case_t), upper_used, out);
1330 * Write the lower case table.
1332 fwrite((char *) lower, sizeof(_case_t), lower_used, out);
1336 * Write the title case table.
1338 fwrite((char *) title, sizeof(_case_t), title_used, out);
1342 /*****************************************************************
1344 * Generate the composition data.
1346 *****************************************************************/
1349 * Create compositions from decomposition data
1354 * Open the comp.dat file.
1356 sprintf(path, "%s%scomp.dat", opath, LDAP_DIRSEP);
1357 if ((out = fopen(path, "wb")) == 0)
1363 hdr[1] = (unsigned short) comps_used * 4;
1364 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1367 * Write out the byte count to maintain header size.
1369 bytes = comps_used * sizeof(_comp_t);
1370 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1373 * Now, if comps exist, write them out.
1376 fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
1380 /*****************************************************************
1382 * Generate the decomposition data.
1384 *****************************************************************/
1387 * Fully expand all decompositions before generating the output file.
1392 * Open the decomp.dat file.
1394 sprintf(path, "%s%sdecomp.dat", opath, LDAP_DIRSEP);
1395 if ((out = fopen(path, "wb")) == 0)
1398 hdr[1] = decomps_used;
1403 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1406 * Write a temporary byte count which will be calculated as the
1407 * decompositions are written out.
1410 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1414 * Write the list of decomp nodes.
1416 for (i = idx = 0; i < decomps_used; i++) {
1417 fwrite((char *) &decomps[i].code, sizeof(unsigned long), 1, out);
1418 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1419 idx += decomps[i].used;
1423 * Write the sentinel index as the last decomp node.
1425 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1428 * Write the decompositions themselves.
1430 for (i = 0; i < decomps_used; i++)
1431 fwrite((char *) decomps[i].decomp, sizeof(unsigned long),
1432 decomps[i].used, out);
1435 * Seek back to the beginning and write the byte count.
1437 bytes = (sizeof(unsigned long) * idx) +
1438 (sizeof(unsigned long) * ((hdr[1] << 1) + 1));
1439 fseek(out, sizeof(unsigned short) << 1, 0L);
1440 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1446 * Open the kdecomp.dat file.
1448 sprintf(path, "%s%skdecomp.dat", opath, LDAP_DIRSEP);
1449 if ((out = fopen(path, "wb")) == 0)
1452 hdr[1] = kdecomps_used;
1457 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1460 * Write a temporary byte count which will be calculated as the
1461 * decompositions are written out.
1464 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1466 if (kdecomps_used) {
1468 * Write the list of kdecomp nodes.
1470 for (i = idx = 0; i < kdecomps_used; i++) {
1471 fwrite((char *) &kdecomps[i].code, sizeof(unsigned long), 1, out);
1472 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1473 idx += kdecomps[i].used;
1477 * Write the sentinel index as the last decomp node.
1479 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1482 * Write the decompositions themselves.
1484 for (i = 0; i < kdecomps_used; i++)
1485 fwrite((char *) kdecomps[i].decomp, sizeof(unsigned long),
1486 kdecomps[i].used, out);
1489 * Seek back to the beginning and write the byte count.
1491 bytes = (sizeof(unsigned long) * idx) +
1492 (sizeof(unsigned long) * ((hdr[1] << 1) + 1));
1493 fseek(out, sizeof(unsigned short) << 1, 0L);
1494 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1499 /*****************************************************************
1501 * Generate the combining class data.
1503 *****************************************************************/
1506 * Open the cmbcl.dat file.
1508 sprintf(path, "%s%scmbcl.dat", opath, LDAP_DIRSEP);
1509 if ((out = fopen(path, "wb")) == 0)
1513 * Set the number of ranges used. Each range has a combining class which
1514 * means each entry is a 3-tuple.
1516 hdr[1] = ccl_used / 3;
1521 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1524 * Write out the byte count to maintain header size.
1526 bytes = ccl_used * sizeof(unsigned long);
1527 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1531 * Write the combining class ranges out.
1533 fwrite((char *) ccl, sizeof(unsigned long), ccl_used, out);
1537 /*****************************************************************
1539 * Generate the number data.
1541 *****************************************************************/
1544 * Open the num.dat file.
1546 sprintf(path, "%s%snum.dat", opath, LDAP_DIRSEP);
1547 if ((out = fopen(path, "wb")) == 0)
1551 * The count part of the header will be the total number of codes that
1554 hdr[1] = (unsigned short) (ncodes_used << 1);
1555 bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
1560 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1563 * Write out the byte count to maintain header size.
1565 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1568 * Now, if number mappings exist, write them out.
1570 if (ncodes_used > 0) {
1571 fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
1572 fwrite((char *) nums, sizeof(_num_t), nums_used, out);
1582 "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
1583 fprintf(stderr, " datafile1 datafile2 ...\n\n");
1585 "-o output-directory\n\t\tWrite the output files to a different");
1586 fprintf(stderr, " directory (default: .).\n");
1588 "-x composition-exclusion\n\t\tFile of composition codes");
1589 fprintf(stderr, " that should be excluded.\n");
1594 main(int argc, char *argv[])
1599 if ((prog = strrchr(argv[0], *LDAP_DIRSEP)) != 0)
1611 if (argv[0][0] == '-') {
1612 switch (argv[0][1]) {
1621 if ((in = fopen(argv[0], "rb")) == 0)
1623 "%s: unable to open composition exclusion file %s\n",
1626 read_compexdata(in);
1635 if (in != stdin && in != NULL)
1637 if ((in = fopen(argv[0], "rb")) == 0)
1638 fprintf(stderr, "%s: unable to open ctype file %s\n",