3 * Copyright 2000 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
113 static _decomp_t *decomps;
114 static unsigned long decomps_used;
115 static unsigned long decomps_size;
118 * Composition exclusion table stuff.
120 #define COMPEX_SET(c) (compexs[(c) >> 5] |= (1 << ((c) & 31)))
121 #define COMPEX_TEST(c) (compexs[(c) >> 5] & (1 << ((c) & 31)))
122 static unsigned long compexs[2048];
125 * Struct for holding a composition pair, and array of composition pairs
134 static _comp_t *comps;
135 static unsigned long comps_used;
138 * Types and lists for handling lists of case mappings.
142 unsigned long other1;
143 unsigned long other2;
146 static _case_t *upper;
147 static _case_t *lower;
148 static _case_t *title;
149 static unsigned long upper_used;
150 static unsigned long upper_size;
151 static unsigned long lower_used;
152 static unsigned long lower_size;
153 static unsigned long title_used;
154 static unsigned long title_size;
157 * Array used to collect case mappings before adding them to a list.
159 static unsigned long cases[3];
162 * An array to hold ranges for combining classes.
164 static unsigned long *ccl;
165 static unsigned long ccl_used;
166 static unsigned long ccl_size;
169 * Structures for handling numbers.
182 * Arrays to hold the mapping of codes to numbers.
184 static _codeidx_t *ncodes;
185 static unsigned long ncodes_used;
186 static unsigned long ncodes_size;
189 static unsigned long nums_used;
190 static unsigned long nums_size;
193 * Array for holding numbers.
196 static unsigned long nums_used;
197 static unsigned long nums_size;
200 add_range(unsigned long start, unsigned long end, char *p1, char *p2)
206 for (k = 0; k < 2; k++) {
218 for (i = 0; i < NUMPROPS; i++) {
219 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
229 * Resize the range list if necessary.
231 if (rlp->used == rlp->size) {
233 rlp->ranges = (unsigned long *)
234 malloc(sizeof(unsigned long) << 3);
236 rlp->ranges = (unsigned long *)
237 realloc((char *) rlp->ranges,
238 sizeof(unsigned long) * (rlp->size + 8));
243 * If this is the first code for this property list, just add it
246 if (rlp->used == 0) {
247 rlp->ranges[0] = start;
248 rlp->ranges[1] = end;
254 * Optimize the case of adding the range to the end.
257 if (start > rlp->ranges[j]) {
259 rlp->ranges[j++] = start;
260 rlp->ranges[j++] = end;
266 * Need to locate the insertion point.
269 i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
272 * If the start value lies in the current range, then simply set the
273 * new end point of the range to the end value passed as a parameter.
275 if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
276 rlp->ranges[i + 1] = end;
281 * Shift following values up by two.
283 for (j = rlp->used; j > i; j -= 2) {
284 rlp->ranges[j] = rlp->ranges[j - 2];
285 rlp->ranges[j + 1] = rlp->ranges[j - 1];
289 * Add the new range at the insertion point.
291 rlp->ranges[i] = start;
292 rlp->ranges[i + 1] = end;
298 ordered_range_insert(unsigned long c, char *name, int len)
308 * Deal with directionality codes introduced in Unicode 3.0.
310 if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
312 (memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
313 memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
314 memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
316 * Mark all of these as Other Neutral to preserve compatibility with
323 for (i = 0; i < NUMPROPS; i++) {
324 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
332 * Have a match, so insert the code in order.
337 * Resize the range list if necessary.
339 if (rlp->used == rlp->size) {
341 rlp->ranges = (unsigned long *)
342 malloc(sizeof(unsigned long) << 3);
344 rlp->ranges = (unsigned long *)
345 realloc((char *) rlp->ranges,
346 sizeof(unsigned long) * (rlp->size + 8));
351 * If this is the first code for this property list, just add it
354 if (rlp->used == 0) {
355 rlp->ranges[0] = rlp->ranges[1] = c;
361 * Optimize the cases of extending the last range and adding new ranges to
366 s = rlp->ranges[j - 1];
370 * Extend the last range.
378 * Start another range on the end.
381 rlp->ranges[j] = rlp->ranges[j + 1] = c;
388 * The code is a duplicate of a code in the last range, so just return.
393 * The code should be inserted somewhere before the last range in the
394 * list. Locate the insertion point.
397 i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
400 e = rlp->ranges[i + 1];
404 * Simply extend the current range.
406 rlp->ranges[i + 1] = c;
409 * Add a new entry before the current location. Shift all entries
410 * before the current one up by one to make room.
412 for (j = rlp->used; j > i; j -= 2) {
413 rlp->ranges[j] = rlp->ranges[j - 2];
414 rlp->ranges[j + 1] = rlp->ranges[j - 1];
416 rlp->ranges[i] = rlp->ranges[i + 1] = c;
423 add_decomp(unsigned long code)
425 unsigned long i, j, size;
428 * Add the code to the composite property.
430 ordered_range_insert(code, "Cm", 2);
433 * Locate the insertion point for the code.
435 for (i = 0; i < decomps_used && code > decomps[i].code; i++) ;
438 * Allocate space for a new decomposition.
440 if (decomps_used == decomps_size) {
441 if (decomps_size == 0)
442 decomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
444 decomps = (_decomp_t *)
445 realloc((char *) decomps,
446 sizeof(_decomp_t) * (decomps_size + 8));
447 (void) memset((char *) (decomps + decomps_size), '\0',
448 sizeof(_decomp_t) << 3);
452 if (i < decomps_used && code != decomps[i].code) {
454 * Shift the decomps up by one if the codes don't match.
456 for (j = decomps_used; j > i; j--)
457 (void) AC_MEMCPY((char *) &decomps[j], (char *) &decomps[j - 1],
462 * Insert or replace a decomposition.
464 size = dectmp_size + (4 - (dectmp_size & 3));
465 if (decomps[i].size < size) {
466 if (decomps[i].size == 0)
467 decomps[i].decomp = (unsigned long *)
468 malloc(sizeof(unsigned long) * size);
470 decomps[i].decomp = (unsigned long *)
471 realloc((char *) decomps[i].decomp,
472 sizeof(unsigned long) * size);
473 decomps[i].size = size;
476 if (decomps[i].code != code)
479 decomps[i].code = code;
480 decomps[i].used = dectmp_size;
481 (void) AC_MEMCPY((char *) decomps[i].decomp, (char *) dectmp,
482 sizeof(unsigned long) * dectmp_size);
485 * NOTICE: This needs changing later so it is more general than simply
486 * pairs. This calculation is done here to simplify allocation elsewhere.
488 if (dectmp_size == 2)
493 add_title(unsigned long code)
498 * Always map the code to itself.
502 if (title_used == title_size) {
504 title = (_case_t *) malloc(sizeof(_case_t) << 3);
506 title = (_case_t *) realloc((char *) title,
507 sizeof(_case_t) * (title_size + 8));
512 * Locate the insertion point.
514 for (i = 0; i < title_used && code > title[i].key; i++) ;
516 if (i < title_used) {
518 * Shift the array up by one.
520 for (j = title_used; j > i; j--)
521 (void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
525 title[i].key = cases[2]; /* Title */
526 title[i].other1 = cases[0]; /* Upper */
527 title[i].other2 = cases[1]; /* Lower */
533 add_upper(unsigned long code)
538 * Always map the code to itself.
543 * If the title case character is not present, then make it the same as
549 if (upper_used == upper_size) {
551 upper = (_case_t *) malloc(sizeof(_case_t) << 3);
553 upper = (_case_t *) realloc((char *) upper,
554 sizeof(_case_t) * (upper_size + 8));
559 * Locate the insertion point.
561 for (i = 0; i < upper_used && code > upper[i].key; i++) ;
563 if (i < upper_used) {
565 * Shift the array up by one.
567 for (j = upper_used; j > i; j--)
568 (void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
572 upper[i].key = cases[0]; /* Upper */
573 upper[i].other1 = cases[1]; /* Lower */
574 upper[i].other2 = cases[2]; /* Title */
580 add_lower(unsigned long code)
585 * Always map the code to itself.
590 * If the title case character is empty, then make it the same as the
596 if (lower_used == lower_size) {
598 lower = (_case_t *) malloc(sizeof(_case_t) << 3);
600 lower = (_case_t *) realloc((char *) lower,
601 sizeof(_case_t) * (lower_size + 8));
606 * Locate the insertion point.
608 for (i = 0; i < lower_used && code > lower[i].key; i++) ;
610 if (i < lower_used) {
612 * Shift the array up by one.
614 for (j = lower_used; j > i; j--)
615 (void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
619 lower[i].key = cases[1]; /* Lower */
620 lower[i].other1 = cases[0]; /* Upper */
621 lower[i].other2 = cases[2]; /* Title */
627 ordered_ccl_insert(unsigned long c, unsigned long ccl_code)
631 if (ccl_used == ccl_size) {
633 ccl = (unsigned long *) malloc(sizeof(unsigned long) * 24);
635 ccl = (unsigned long *)
636 realloc((char *) ccl, sizeof(unsigned long) * (ccl_size + 24));
641 * Optimize adding the first item.
651 * Handle the special case of extending the range on the end. This
652 * requires that the combining class codes are the same.
654 if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
655 ccl[ccl_used - 2] = c;
660 * Handle the special case of adding another range on the end.
662 if (c > ccl[ccl_used - 2] + 1 ||
663 (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
666 ccl[ccl_used++] = ccl_code;
671 * Locate either the insertion point or range for the code.
673 for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
675 if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
677 * Extend an existing range.
681 } else if (c < ccl[i]) {
683 * Start a new range before the current location.
685 for (j = ccl_used; j > i; j -= 3) {
687 ccl[j - 1] = ccl[j - 4];
688 ccl[j - 2] = ccl[j - 5];
690 ccl[i] = ccl[i + 1] = c;
691 ccl[i + 2] = ccl_code;
696 * Adds a number if it does not already exist and returns an index value
700 make_number(short num, short denom)
705 * Determine if the number already exists.
707 for (n = 0; n < nums_used; n++) {
708 if (nums[n].numerator == num && nums[n].denominator == denom)
712 if (nums_used == nums_size) {
714 nums = (_num_t *) malloc(sizeof(_num_t) << 3);
716 nums = (_num_t *) realloc((char *) nums,
717 sizeof(_num_t) * (nums_size + 8));
722 nums[n].numerator = num;
723 nums[n].denominator = denom;
729 add_number(unsigned long code, short num, short denom)
734 * Insert the code in order.
736 for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
739 * Handle the case of the codes matching and simply replace the number
740 * that was there before.
742 if (ncodes_used > 0 && code == ncodes[i].code) {
743 ncodes[i].idx = make_number(num, denom);
748 * Resize the array if necessary.
750 if (ncodes_used == ncodes_size) {
751 if (ncodes_size == 0)
752 ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
754 ncodes = (_codeidx_t *)
755 realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
761 * Shift things around to insert the code if necessary.
763 if (i < ncodes_used) {
764 for (j = ncodes_used; j > i; j--) {
765 ncodes[j].code = ncodes[j - 1].code;
766 ncodes[j].idx = ncodes[j - 1].idx;
769 ncodes[i].code = code;
770 ncodes[i].idx = make_number(num, denom);
776 * This routine assumes that the line is a valid Unicode Character Database
782 unsigned long i, lineno, skip, code, ccl_code;
783 short wnum, neg, number[2];
784 char line[512], *s, *e;
787 while (fscanf(in, "%[^\n]\n", line) != EOF) {
791 * Skip blank lines and lines that start with a '#'.
793 if (line[0] == 0 || line[0] == '#')
797 * If lines need to be skipped, do it here.
805 * Collect the code. The code can be up to 6 hex digits in length to
806 * allow surrogates to be specified.
808 for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
810 if (*s >= '0' && *s <= '9')
812 else if (*s >= 'A' && *s <= 'F')
813 code += (*s - 'A') + 10;
814 else if (*s >= 'a' && *s <= 'f')
815 code += (*s - 'a') + 10;
819 * Handle the following special cases:
820 * 1. 4E00-9FA5 CJK Ideographs.
821 * 2. AC00-D7A3 Hangul Syllables.
822 * 3. D800-DFFF Surrogates.
823 * 4. E000-F8FF Private Use Area.
824 * 5. F900-FA2D Han compatibility.
829 * The Han ideographs.
831 add_range(0x4e00, 0x9fff, "Lo", "L");
834 * Add the characters to the defined category.
836 add_range(0x4e00, 0x9fa5, "Cp", 0);
842 * The Hangul syllables.
844 add_range(0xac00, 0xd7a3, "Lo", "L");
847 * Add the characters to the defined category.
849 add_range(0xac00, 0xd7a3, "Cp", 0);
855 * Make a range of all surrogates and assume some default
858 add_range(0x010000, 0x10ffff, "Cs", "L");
863 * The Private Use area. Add with a default set of properties.
865 add_range(0xe000, 0xf8ff, "Co", "L");
870 * The CJK compatibility area.
872 add_range(0xf900, 0xfaff, "Lo", "L");
875 * Add the characters to the defined category.
877 add_range(0xf900, 0xfaff, "Cp", 0);
886 * Add the code to the defined category.
888 ordered_range_insert(code, "Cp", 2);
891 * Locate the first character property field.
893 for (i = 0; *s != 0 && i < 2; s++) {
897 for (e = s; *e && *e != ';'; e++) ;
899 ordered_range_insert(code, s, e - s);
902 * Locate the combining class code.
904 for (s = e; *s != 0 && i < 3; s++) {
910 * Convert the combining class code from decimal.
912 for (ccl_code = 0, e = s; *e && *e != ';'; e++)
913 ccl_code = (ccl_code * 10) + (*e - '0');
916 * Add the code if it not 0.
919 ordered_ccl_insert(code, ccl_code);
922 * Locate the second character property field.
924 for (s = e; *s != 0 && i < 4; s++) {
928 for (e = s; *e && *e != ';'; e++) ;
930 ordered_range_insert(code, s, e - s);
933 * Check for a decomposition.
936 if (*s != ';' && *s != '<') {
938 * Collect the codes of the decomposition.
940 for (dectmp_size = 0; *s != ';'; ) {
942 * Skip all leading non-hex digits.
944 while (!ishdigit(*s))
947 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
948 dectmp[dectmp_size] <<= 4;
949 if (*s >= '0' && *s <= '9')
950 dectmp[dectmp_size] += *s - '0';
951 else if (*s >= 'A' && *s <= 'F')
952 dectmp[dectmp_size] += (*s - 'A') + 10;
953 else if (*s >= 'a' && *s <= 'f')
954 dectmp[dectmp_size] += (*s - 'a') + 10;
960 * If there are any codes in the temporary decomposition array,
961 * then add the character with its decomposition.
968 * Skip to the number field.
970 for (i = 0; i < 3 && *s; s++) {
976 * Scan the number in.
978 number[0] = number[1] = 0;
979 for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
987 * Move the the denominator of the fraction.
995 number[wnum] = (number[wnum] * 10) + (*e - '0');
1000 * Adjust the denominator in case of integers and add the number.
1003 number[1] = number[0];
1005 add_number(code, number[0], number[1]);
1009 * Skip to the start of the possible case mappings.
1011 for (s = e, i = 0; i < 4 && *s; s++) {
1017 * Collect the case mappings.
1019 cases[0] = cases[1] = cases[2] = 0;
1020 for (i = 0; i < 3; i++) {
1021 while (ishdigit(*s)) {
1023 if (*s >= '0' && *s <= '9')
1024 cases[i] += *s - '0';
1025 else if (*s >= 'A' && *s <= 'F')
1026 cases[i] += (*s - 'A') + 10;
1027 else if (*s >= 'a' && *s <= 'f')
1028 cases[i] += (*s - 'a') + 10;
1034 if (cases[0] && cases[1])
1036 * Add the upper and lower mappings for a title case character.
1041 * Add the lower and title case mappings for the upper case
1047 * Add the upper and title case mappings for the lower case
1055 find_decomp(unsigned long code)
1060 r = decomps_used - 1;
1063 if (code > decomps[m].code)
1065 else if (code < decomps[m].code)
1074 decomp_it(_decomp_t *d)
1079 for (i = 0; i < d->used; i++) {
1080 if ((dp = find_decomp(d->decomp[i])) != 0)
1083 dectmp[dectmp_size++] = d->decomp[i];
1088 * Expand all decompositions by recursively decomposing each character
1089 * in the decomposition.
1096 for (i = 0; i < decomps_used; i++) {
1098 decomp_it(&decomps[i]);
1099 if (dectmp_size > 0)
1100 add_decomp(decomps[i].code);
1105 cmpcomps(_comp_t *comp1, _comp_t *comp2)
1107 long diff = comp1->code1 - comp2->code1;
1110 diff = comp1->code2 - comp2->code2;
1115 * Load composition exclusion data
1118 read_compexdata(FILE *in)
1120 unsigned short i, code;
1123 (void) memset((char *) compexs, 0, sizeof(unsigned long) << 11);
1125 while (fscanf(in, "%[^\n]\n", line) != EOF) {
1127 * Skip blank lines and lines that start with a '#'.
1129 if (line[0] == 0 || line[0] == '#')
1133 * Collect the code. Assume max 4 digits
1136 for (s = line, i = code = 0; *s != '#' && i < 4; i++, s++) {
1138 if (*s >= '0' && *s <= '9')
1140 else if (*s >= 'A' && *s <= 'F')
1141 code += (*s - 'A') + 10;
1142 else if (*s >= 'a' && *s <= 'f')
1143 code += (*s - 'a') + 10;
1150 * Creates array of compositions from decomposition array
1155 unsigned long i, cu;
1157 comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
1159 for (i = cu = 0; i < decomps_used; i++) {
1160 if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
1162 comps[cu].comp = decomps[i].code;
1163 comps[cu].count = 2;
1164 comps[cu].code1 = decomps[i].decomp[0];
1165 comps[cu].code2 = decomps[i].decomp[1];
1169 qsort(comps, comps_used, sizeof(_comp_t),
1170 (int (*)(const void *, const void *)) cmpcomps);
1174 write_cdata(char *opath)
1177 unsigned long i, idx, bytes, nprops;
1178 unsigned short casecnt[2];
1181 /*****************************************************************
1183 * Generate the ctype data.
1185 *****************************************************************/
1188 * Open the ctype.dat file.
1190 sprintf(path, "%s%sctype.dat", opath, LDAP_DIRSEP);
1191 if ((out = fopen(path, "wb")) == 0)
1195 * Collect the offsets for the properties. The offsets array is
1196 * on a 4-byte boundary to keep things efficient for architectures
1197 * that need such a thing.
1199 for (i = idx = 0; i < NUMPROPS; i++) {
1200 propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
1201 idx += proptbl[i].used;
1205 * Add the sentinel index which is used by the binary search as the upper
1206 * bound for a search.
1211 * Record the actual number of property lists. This may be different than
1212 * the number of offsets actually written because of aligning on a 4-byte
1218 * Calculate the byte count needed and pad the property counts array to a
1221 if ((bytes = sizeof(unsigned short) * (NUMPROPS + 1)) & 3)
1222 bytes += 4 - (bytes & 3);
1223 nprops = bytes / sizeof(unsigned short);
1224 bytes += sizeof(unsigned long) * idx;
1229 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1232 * Write the byte count.
1234 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1237 * Write the property list counts.
1239 fwrite((char *) propcnt, sizeof(unsigned short), nprops, out);
1242 * Write the property lists.
1244 for (i = 0; i < NUMPROPS; i++) {
1245 if (proptbl[i].used > 0)
1246 fwrite((char *) proptbl[i].ranges, sizeof(unsigned long),
1247 proptbl[i].used, out);
1252 /*****************************************************************
1254 * Generate the case mapping data.
1256 *****************************************************************/
1259 * Open the case.dat file.
1261 sprintf(path, "%s%scase.dat", opath, LDAP_DIRSEP);
1262 if ((out = fopen(path, "wb")) == 0)
1266 * Write the case mapping tables.
1268 hdr[1] = upper_used + lower_used + title_used;
1269 casecnt[0] = upper_used;
1270 casecnt[1] = lower_used;
1275 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1278 * Write the upper and lower case table sizes.
1280 fwrite((char *) casecnt, sizeof(unsigned short), 2, out);
1284 * Write the upper case table.
1286 fwrite((char *) upper, sizeof(_case_t), upper_used, out);
1290 * Write the lower case table.
1292 fwrite((char *) lower, sizeof(_case_t), lower_used, out);
1296 * Write the title case table.
1298 fwrite((char *) title, sizeof(_case_t), title_used, out);
1302 /*****************************************************************
1304 * Generate the composition data.
1306 *****************************************************************/
1309 * Create compositions from decomposition data
1314 * Open the comp.dat file.
1316 sprintf(path, "%s%scomp.dat", opath, LDAP_DIRSEP);
1317 if ((out = fopen(path, "wb")) == 0)
1323 hdr[1] = (unsigned short) comps_used * 4;
1324 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1327 * Write out the byte count to maintain header size.
1329 bytes = comps_used * sizeof(_comp_t);
1330 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1333 * Now, if comps exist, write them out.
1336 fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
1340 /*****************************************************************
1342 * Generate the decomposition data.
1344 *****************************************************************/
1347 * Fully expand all decompositions before generating the output file.
1352 * Open the decomp.dat file.
1354 sprintf(path, "%s%sdecomp.dat", opath, LDAP_DIRSEP);
1355 if ((out = fopen(path, "wb")) == 0)
1358 hdr[1] = decomps_used;
1363 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1366 * Write a temporary byte count which will be calculated as the
1367 * decompositions are written out.
1370 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1374 * Write the list of decomp nodes.
1376 for (i = idx = 0; i < decomps_used; i++) {
1377 fwrite((char *) &decomps[i].code, sizeof(unsigned long), 1, out);
1378 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1379 idx += decomps[i].used;
1383 * Write the sentinel index as the last decomp node.
1385 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1388 * Write the decompositions themselves.
1390 for (i = 0; i < decomps_used; i++)
1391 fwrite((char *) decomps[i].decomp, sizeof(unsigned long),
1392 decomps[i].used, out);
1395 * Seek back to the beginning and write the byte count.
1397 bytes = (sizeof(unsigned long) * idx) +
1398 (sizeof(unsigned long) * ((hdr[1] << 1) + 1));
1399 fseek(out, sizeof(unsigned short) << 1, 0L);
1400 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1405 /*****************************************************************
1407 * Generate the combining class data.
1409 *****************************************************************/
1412 * Open the cmbcl.dat file.
1414 sprintf(path, "%s%scmbcl.dat", opath, LDAP_DIRSEP);
1415 if ((out = fopen(path, "wb")) == 0)
1419 * Set the number of ranges used. Each range has a combining class which
1420 * means each entry is a 3-tuple.
1422 hdr[1] = ccl_used / 3;
1427 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1430 * Write out the byte count to maintain header size.
1432 bytes = ccl_used * sizeof(unsigned long);
1433 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1437 * Write the combining class ranges out.
1439 fwrite((char *) ccl, sizeof(unsigned long), ccl_used, out);
1443 /*****************************************************************
1445 * Generate the number data.
1447 *****************************************************************/
1450 * Open the num.dat file.
1452 sprintf(path, "%s%snum.dat", opath, LDAP_DIRSEP);
1453 if ((out = fopen(path, "wb")) == 0)
1457 * The count part of the header will be the total number of codes that
1460 hdr[1] = (unsigned short) (ncodes_used << 1);
1461 bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
1466 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1469 * Write out the byte count to maintain header size.
1471 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1474 * Now, if number mappings exist, write them out.
1476 if (ncodes_used > 0) {
1477 fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
1478 fwrite((char *) nums, sizeof(_num_t), nums_used, out);
1488 "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
1489 fprintf(stderr, " datafile1 datafile2 ...\n\n");
1491 "-o output-directory\n\t\tWrite the output files to a different");
1492 fprintf(stderr, " directory (default: .).\n");
1494 "-x composition-exclusion\n\t\tFile of composition codes");
1495 fprintf(stderr, " that should be excluded.\n");
1500 main(int argc, char *argv[])
1505 if ((prog = strrchr(argv[0], *LDAP_DIRSEP)) != 0)
1517 if (argv[0][0] == '-') {
1518 switch (argv[0][1]) {
1527 if ((in = fopen(argv[0], "rb")) == 0)
1529 "%s: unable to open composition exclusion file %s\n",
1532 read_compexdata(in);
1541 if (in != stdin && in != NULL)
1543 if ((in = fopen(argv[0], "rb")) == 0)
1544 fprintf(stderr, "%s: unable to open ctype file %s\n",