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;
808 if( fscanf(in, "%[^\n]\n", line) != 1) break;
812 * Skip blank lines and lines that start with a '#'.
814 if (line[0] == 0 || line[0] == '#')
818 * If lines need to be skipped, do it here.
826 * Collect the code. The code can be up to 6 hex digits in length to
827 * allow surrogates to be specified.
829 for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
831 if (*s >= '0' && *s <= '9')
833 else if (*s >= 'A' && *s <= 'F')
834 code += (*s - 'A') + 10;
835 else if (*s >= 'a' && *s <= 'f')
836 code += (*s - 'a') + 10;
840 * Handle the following special cases:
841 * 1. 4E00-9FA5 CJK Ideographs.
842 * 2. AC00-D7A3 Hangul Syllables.
843 * 3. D800-DFFF Surrogates.
844 * 4. E000-F8FF Private Use Area.
845 * 5. F900-FA2D Han compatibility.
850 * The Han ideographs.
852 add_range(0x4e00, 0x9fff, "Lo", "L");
855 * Add the characters to the defined category.
857 add_range(0x4e00, 0x9fa5, "Cp", 0);
863 * The Hangul syllables.
865 add_range(0xac00, 0xd7a3, "Lo", "L");
868 * Add the characters to the defined category.
870 add_range(0xac00, 0xd7a3, "Cp", 0);
876 * Make a range of all surrogates and assume some default
879 add_range(0x010000, 0x10ffff, "Cs", "L");
884 * The Private Use area. Add with a default set of properties.
886 add_range(0xe000, 0xf8ff, "Co", "L");
891 * The CJK compatibility area.
893 add_range(0xf900, 0xfaff, "Lo", "L");
896 * Add the characters to the defined category.
898 add_range(0xf900, 0xfaff, "Cp", 0);
907 * Add the code to the defined category.
909 ordered_range_insert(code, "Cp", 2);
912 * Locate the first character property field.
914 for (i = 0; *s != 0 && i < 2; s++) {
918 for (e = s; *e && *e != ';'; e++) ;
920 ordered_range_insert(code, s, e - s);
923 * Locate the combining class code.
925 for (s = e; *s != 0 && i < 3; s++) {
931 * Convert the combining class code from decimal.
933 for (ccl_code = 0, e = s; *e && *e != ';'; e++)
934 ccl_code = (ccl_code * 10) + (*e - '0');
937 * Add the code if it not 0.
940 ordered_ccl_insert(code, ccl_code);
943 * Locate the second character property field.
945 for (s = e; *s != 0 && i < 4; s++) {
949 for (e = s; *e && *e != ';'; e++) ;
951 ordered_range_insert(code, s, e - s);
954 * Check for a decomposition.
961 * Skip compatibility formatting tag.
966 * Collect the codes of the decomposition.
968 for (dectmp_size = 0; *s != ';'; ) {
970 * Skip all leading non-hex digits.
972 while (!ishdigit(*s))
975 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
976 dectmp[dectmp_size] <<= 4;
977 if (*s >= '0' && *s <= '9')
978 dectmp[dectmp_size] += *s - '0';
979 else if (*s >= 'A' && *s <= 'F')
980 dectmp[dectmp_size] += (*s - 'A') + 10;
981 else if (*s >= 'a' && *s <= 'f')
982 dectmp[dectmp_size] += (*s - 'a') + 10;
988 * If there are any codes in the temporary decomposition array,
989 * then add the character with its decomposition.
991 if (dectmp_size > 0) {
1000 * Skip to the number field.
1002 for (i = 0; i < 3 && *s; s++) {
1008 * Scan the number in.
1010 number[0] = number[1] = 0;
1011 for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
1019 * Move the the denominator of the fraction.
1027 number[wnum] = (number[wnum] * 10) + (*e - '0');
1032 * Adjust the denominator in case of integers and add the number.
1035 number[1] = number[0];
1037 add_number(code, number[0], number[1]);
1041 * Skip to the start of the possible case mappings.
1043 for (s = e, i = 0; i < 4 && *s; s++) {
1049 * Collect the case mappings.
1051 cases[0] = cases[1] = cases[2] = 0;
1052 for (i = 0; i < 3; i++) {
1053 while (ishdigit(*s)) {
1055 if (*s >= '0' && *s <= '9')
1056 cases[i] += *s - '0';
1057 else if (*s >= 'A' && *s <= 'F')
1058 cases[i] += (*s - 'A') + 10;
1059 else if (*s >= 'a' && *s <= 'f')
1060 cases[i] += (*s - 'a') + 10;
1066 if (cases[0] && cases[1])
1068 * Add the upper and lower mappings for a title case character.
1073 * Add the lower and title case mappings for the upper case
1079 * Add the upper and title case mappings for the lower case
1087 find_decomp(unsigned long code, short compat)
1093 r = (compat ? kdecomps_used : decomps_used) - 1;
1094 decs = compat ? kdecomps : decomps;
1097 if (code > decs[m].code)
1099 else if (code < decs[m].code)
1108 decomp_it(_decomp_t *d, short compat)
1113 for (i = 0; i < d->used; i++) {
1114 if ((dp = find_decomp(d->decomp[i], compat)) != 0)
1115 decomp_it(dp, compat);
1117 dectmp[dectmp_size++] = d->decomp[i];
1122 * Expand all decompositions by recursively decomposing each character
1123 * in the decomposition.
1130 for (i = 0; i < decomps_used; i++) {
1132 decomp_it(&decomps[i], 0);
1133 if (dectmp_size > 0)
1134 add_decomp(decomps[i].code, 0);
1137 for (i = 0; i < kdecomps_used; i++) {
1139 decomp_it(&kdecomps[i], 1);
1140 if (dectmp_size > 0)
1141 add_decomp(kdecomps[i].code, 1);
1146 cmpcomps(_comp_t *comp1, _comp_t *comp2)
1148 long diff = comp1->code1 - comp2->code1;
1151 diff = comp1->code2 - comp2->code2;
1156 * Load composition exclusion data
1159 read_compexdata(FILE *in)
1161 unsigned short i, code;
1164 (void) memset((char *) compexs, 0, sizeof(unsigned long) << 11);
1167 if( fscanf(in, "%[^\n]\n", line) != 1) break;
1169 * Skip blank lines and lines that start with a '#'.
1171 if (line[0] == 0 || line[0] == '#')
1175 * Collect the code. Assume max 4 digits
1178 for (s = line, i = code = 0; *s != '#' && i < 4; i++, s++) {
1180 if (*s >= '0' && *s <= '9')
1182 else if (*s >= 'A' && *s <= 'F')
1183 code += (*s - 'A') + 10;
1184 else if (*s >= 'a' && *s <= 'f')
1185 code += (*s - 'a') + 10;
1192 * Creates array of compositions from decomposition array
1197 unsigned long i, cu;
1199 comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
1201 for (i = cu = 0; i < decomps_used; i++) {
1202 if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
1204 comps[cu].comp = decomps[i].code;
1205 comps[cu].count = 2;
1206 comps[cu].code1 = decomps[i].decomp[0];
1207 comps[cu].code2 = decomps[i].decomp[1];
1211 qsort(comps, comps_used, sizeof(_comp_t),
1212 (int (*)(const void *, const void *)) cmpcomps);
1216 write_cdata(char *opath)
1219 unsigned long i, idx, bytes, nprops;
1220 unsigned short casecnt[2];
1223 /*****************************************************************
1225 * Generate the ctype data.
1227 *****************************************************************/
1230 * Open the ctype.dat file.
1232 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "ctype.dat", opath);
1233 if ((out = fopen(path, "wb")) == 0)
1237 * Collect the offsets for the properties. The offsets array is
1238 * on a 4-byte boundary to keep things efficient for architectures
1239 * that need such a thing.
1241 for (i = idx = 0; i < NUMPROPS; i++) {
1242 propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
1243 idx += proptbl[i].used;
1247 * Add the sentinel index which is used by the binary search as the upper
1248 * bound for a search.
1253 * Record the actual number of property lists. This may be different than
1254 * the number of offsets actually written because of aligning on a 4-byte
1260 * Calculate the byte count needed and pad the property counts array to a
1263 if ((bytes = sizeof(unsigned short) * (NUMPROPS + 1)) & 3)
1264 bytes += 4 - (bytes & 3);
1265 nprops = bytes / sizeof(unsigned short);
1266 bytes += sizeof(unsigned long) * idx;
1271 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1274 * Write the byte count.
1276 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1279 * Write the property list counts.
1281 fwrite((char *) propcnt, sizeof(unsigned short), nprops, out);
1284 * Write the property lists.
1286 for (i = 0; i < NUMPROPS; i++) {
1287 if (proptbl[i].used > 0)
1288 fwrite((char *) proptbl[i].ranges, sizeof(unsigned long),
1289 proptbl[i].used, out);
1294 /*****************************************************************
1296 * Generate the case mapping data.
1298 *****************************************************************/
1301 * Open the case.dat file.
1303 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "case.dat", opath);
1304 if ((out = fopen(path, "wb")) == 0)
1308 * Write the case mapping tables.
1310 hdr[1] = upper_used + lower_used + title_used;
1311 casecnt[0] = upper_used;
1312 casecnt[1] = lower_used;
1317 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1320 * Write the upper and lower case table sizes.
1322 fwrite((char *) casecnt, sizeof(unsigned short), 2, out);
1326 * Write the upper case table.
1328 fwrite((char *) upper, sizeof(_case_t), upper_used, out);
1332 * Write the lower case table.
1334 fwrite((char *) lower, sizeof(_case_t), lower_used, out);
1338 * Write the title case table.
1340 fwrite((char *) title, sizeof(_case_t), title_used, out);
1344 /*****************************************************************
1346 * Generate the composition data.
1348 *****************************************************************/
1351 * Create compositions from decomposition data
1356 * Open the comp.dat file.
1358 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "comp.dat", opath);
1359 if ((out = fopen(path, "wb")) == 0)
1365 hdr[1] = (unsigned short) comps_used * 4;
1366 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1369 * Write out the byte count to maintain header size.
1371 bytes = comps_used * sizeof(_comp_t);
1372 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1375 * Now, if comps exist, write them out.
1378 fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
1382 /*****************************************************************
1384 * Generate the decomposition data.
1386 *****************************************************************/
1389 * Fully expand all decompositions before generating the output file.
1394 * Open the decomp.dat file.
1396 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "decomp.dat", opath);
1397 if ((out = fopen(path, "wb")) == 0)
1400 hdr[1] = decomps_used;
1405 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1408 * Write a temporary byte count which will be calculated as the
1409 * decompositions are written out.
1412 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1416 * Write the list of decomp nodes.
1418 for (i = idx = 0; i < decomps_used; i++) {
1419 fwrite((char *) &decomps[i].code, sizeof(unsigned long), 1, out);
1420 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1421 idx += decomps[i].used;
1425 * Write the sentinel index as the last decomp node.
1427 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1430 * Write the decompositions themselves.
1432 for (i = 0; i < decomps_used; i++)
1433 fwrite((char *) decomps[i].decomp, sizeof(unsigned long),
1434 decomps[i].used, out);
1437 * Seek back to the beginning and write the byte count.
1439 bytes = (sizeof(unsigned long) * idx) +
1440 (sizeof(unsigned long) * ((hdr[1] << 1) + 1));
1441 fseek(out, sizeof(unsigned short) << 1, 0L);
1442 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1448 * Open the kdecomp.dat file.
1450 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "kdecomp.dat", opath);
1451 if ((out = fopen(path, "wb")) == 0)
1454 hdr[1] = kdecomps_used;
1459 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1462 * Write a temporary byte count which will be calculated as the
1463 * decompositions are written out.
1466 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1468 if (kdecomps_used) {
1470 * Write the list of kdecomp nodes.
1472 for (i = idx = 0; i < kdecomps_used; i++) {
1473 fwrite((char *) &kdecomps[i].code, sizeof(unsigned long), 1, out);
1474 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1475 idx += kdecomps[i].used;
1479 * Write the sentinel index as the last decomp node.
1481 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1484 * Write the decompositions themselves.
1486 for (i = 0; i < kdecomps_used; i++)
1487 fwrite((char *) kdecomps[i].decomp, sizeof(unsigned long),
1488 kdecomps[i].used, out);
1491 * Seek back to the beginning and write the byte count.
1493 bytes = (sizeof(unsigned long) * idx) +
1494 (sizeof(unsigned long) * ((hdr[1] << 1) + 1));
1495 fseek(out, sizeof(unsigned short) << 1, 0L);
1496 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1501 /*****************************************************************
1503 * Generate the combining class data.
1505 *****************************************************************/
1508 * Open the cmbcl.dat file.
1510 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "cmbcl.dat", opath);
1511 if ((out = fopen(path, "wb")) == 0)
1515 * Set the number of ranges used. Each range has a combining class which
1516 * means each entry is a 3-tuple.
1518 hdr[1] = ccl_used / 3;
1523 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1526 * Write out the byte count to maintain header size.
1528 bytes = ccl_used * sizeof(unsigned long);
1529 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1533 * Write the combining class ranges out.
1535 fwrite((char *) ccl, sizeof(unsigned long), ccl_used, out);
1539 /*****************************************************************
1541 * Generate the number data.
1543 *****************************************************************/
1546 * Open the num.dat file.
1548 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "num.dat", opath);
1549 if ((out = fopen(path, "wb")) == 0)
1553 * The count part of the header will be the total number of codes that
1556 hdr[1] = (unsigned short) (ncodes_used << 1);
1557 bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
1562 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1565 * Write out the byte count to maintain header size.
1567 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1570 * Now, if number mappings exist, write them out.
1572 if (ncodes_used > 0) {
1573 fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
1574 fwrite((char *) nums, sizeof(_num_t), nums_used, out);
1584 "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
1585 fprintf(stderr, " datafile1 datafile2 ...\n\n");
1587 "-o output-directory\n\t\tWrite the output files to a different");
1588 fprintf(stderr, " directory (default: .).\n");
1590 "-x composition-exclusion\n\t\tFile of composition codes");
1591 fprintf(stderr, " that should be excluded.\n");
1596 main(int argc, char *argv[])
1601 if ((prog = strrchr(argv[0], *LDAP_DIRSEP)) != 0)
1613 if (argv[0][0] == '-') {
1614 switch (argv[0][1]) {
1623 if ((in = fopen(argv[0], "rb")) == 0)
1625 "%s: unable to open composition exclusion file %s\n",
1628 read_compexdata(in);
1637 if (in != stdin && in != NULL)
1639 if ((in = fopen(argv[0], "rb")) == 0)
1640 fprintf(stderr, "%s: unable to open ctype file %s\n",