3 * Copyright 2001 Computing Research Labs, New Mexico State University
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
20 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 /* $Id: ucgendat.c,v 1.4 2001/01/02 18:46:20 mleisher Exp $" */
26 #include "ldap_config.h"
29 #include <ac/stdlib.h>
30 #include <ac/string.h>
31 #include <ac/unistd.h>
34 #define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\
35 ((cc) >= 'A' && (cc) <= 'F') ||\
36 ((cc) >= 'a' && (cc) <= 'f'))
39 * A header written to the output file with the byte-order-mark and the number
42 static unsigned short hdr[2] = {0xfeff, 0};
45 #define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3)))
53 * List of properties expected to be found in the Unicode Character Database
54 * including some implementation specific properties.
56 * The implementation specific properties are:
57 * Cm = Composed (can be decomposed)
59 * Sy = Symmetric (has left and right forms)
64 * Cp = Defined character
66 static _prop_t props[NUMPROPS] = {
67 {"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2},
68 {"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2},
69 {"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2},
70 {"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2},
71 {"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L", 1}, {"R", 1},
72 {"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B", 1},
73 {"S", 1}, {"WS", 2}, {"ON", 2},
74 {"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2},
75 {"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2}, {"AL", 2}
79 unsigned long *ranges;
84 static _ranges_t proptbl[NUMPROPS];
87 * Make sure this array is sized to be on a 4-byte boundary at compile time.
89 static unsigned short propcnt[NEEDPROPS];
92 * Array used to collect a decomposition before adding it to the decomposition
95 static unsigned long dectmp[64];
96 static unsigned long dectmp_size;
102 unsigned long *decomp;
106 * List of decomposition. Created and expanded in order as the characters are
109 static _decomp_t *decomps;
110 static unsigned long decomps_used;
111 static unsigned long decomps_size;
114 * Composition exclusion table stuff.
116 #define COMPEX_SET(c) (compexs[(c) >> 5] |= (1 << ((c) & 31)))
117 #define COMPEX_TEST(c) (compexs[(c) >> 5] & (1 << ((c) & 31)))
118 static unsigned long compexs[2048];
121 * Struct for holding a composition pair, and array of composition pairs
130 static _comp_t *comps;
131 static unsigned long comps_used;
134 * Types and lists for handling lists of case mappings.
138 unsigned long other1;
139 unsigned long other2;
142 static _case_t *upper;
143 static _case_t *lower;
144 static _case_t *title;
145 static unsigned long upper_used;
146 static unsigned long upper_size;
147 static unsigned long lower_used;
148 static unsigned long lower_size;
149 static unsigned long title_used;
150 static unsigned long title_size;
153 * Array used to collect case mappings before adding them to a list.
155 static unsigned long cases[3];
158 * An array to hold ranges for combining classes.
160 static unsigned long *ccl;
161 static unsigned long ccl_used;
162 static unsigned long ccl_size;
165 * Structures for handling numbers.
178 * Arrays to hold the mapping of codes to numbers.
180 static _codeidx_t *ncodes;
181 static unsigned long ncodes_used;
182 static unsigned long ncodes_size;
185 static unsigned long nums_used;
186 static unsigned long nums_size;
189 * Array for holding numbers.
192 static unsigned long nums_used;
193 static unsigned long nums_size;
196 add_range(unsigned long start, unsigned long end, char *p1, char *p2)
202 for (k = 0; k < 2; k++) {
214 for (i = 0; i < NUMPROPS; i++) {
215 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
225 * Resize the range list if necessary.
227 if (rlp->used == rlp->size) {
229 rlp->ranges = (unsigned long *)
230 malloc(sizeof(unsigned long) << 3);
232 rlp->ranges = (unsigned long *)
233 realloc((char *) rlp->ranges,
234 sizeof(unsigned long) * (rlp->size + 8));
239 * If this is the first code for this property list, just add it
242 if (rlp->used == 0) {
243 rlp->ranges[0] = start;
244 rlp->ranges[1] = end;
250 * Optimize the case of adding the range to the end.
253 if (start > rlp->ranges[j]) {
255 rlp->ranges[j++] = start;
256 rlp->ranges[j++] = end;
262 * Need to locate the insertion point.
265 i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
268 * If the start value lies in the current range, then simply set the
269 * new end point of the range to the end value passed as a parameter.
271 if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
272 rlp->ranges[i + 1] = end;
277 * Shift following values up by two.
279 for (j = rlp->used; j > i; j -= 2) {
280 rlp->ranges[j] = rlp->ranges[j - 2];
281 rlp->ranges[j + 1] = rlp->ranges[j - 1];
285 * Add the new range at the insertion point.
287 rlp->ranges[i] = start;
288 rlp->ranges[i + 1] = end;
294 ordered_range_insert(unsigned long c, char *name, int len)
304 * Deal with directionality codes introduced in Unicode 3.0.
306 if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
308 (memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
309 memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
310 memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
312 * Mark all of these as Other Neutral to preserve compatibility with
319 for (i = 0; i < NUMPROPS; i++) {
320 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
328 * Have a match, so insert the code in order.
333 * Resize the range list if necessary.
335 if (rlp->used == rlp->size) {
337 rlp->ranges = (unsigned long *)
338 malloc(sizeof(unsigned long) << 3);
340 rlp->ranges = (unsigned long *)
341 realloc((char *) rlp->ranges,
342 sizeof(unsigned long) * (rlp->size + 8));
347 * If this is the first code for this property list, just add it
350 if (rlp->used == 0) {
351 rlp->ranges[0] = rlp->ranges[1] = c;
357 * Optimize the cases of extending the last range and adding new ranges to
362 s = rlp->ranges[j - 1];
366 * Extend the last range.
374 * Start another range on the end.
377 rlp->ranges[j] = rlp->ranges[j + 1] = c;
384 * The code is a duplicate of a code in the last range, so just return.
389 * The code should be inserted somewhere before the last range in the
390 * list. Locate the insertion point.
393 i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
396 e = rlp->ranges[i + 1];
400 * Simply extend the current range.
402 rlp->ranges[i + 1] = c;
405 * Add a new entry before the current location. Shift all entries
406 * before the current one up by one to make room.
408 for (j = rlp->used; j > i; j -= 2) {
409 rlp->ranges[j] = rlp->ranges[j - 2];
410 rlp->ranges[j + 1] = rlp->ranges[j - 1];
412 rlp->ranges[i] = rlp->ranges[i + 1] = c;
419 add_decomp(unsigned long code)
421 unsigned long i, j, size;
424 * Add the code to the composite property.
426 ordered_range_insert(code, "Cm", 2);
429 * Locate the insertion point for the code.
431 for (i = 0; i < decomps_used && code > decomps[i].code; i++) ;
434 * Allocate space for a new decomposition.
436 if (decomps_used == decomps_size) {
437 if (decomps_size == 0)
438 decomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
440 decomps = (_decomp_t *)
441 realloc((char *) decomps,
442 sizeof(_decomp_t) * (decomps_size + 8));
443 (void) memset((char *) (decomps + decomps_size), '\0',
444 sizeof(_decomp_t) << 3);
448 if (i < decomps_used && code != decomps[i].code) {
450 * Shift the decomps up by one if the codes don't match.
452 for (j = decomps_used; j > i; j--)
453 (void) AC_MEMCPY((char *) &decomps[j], (char *) &decomps[j - 1],
458 * Insert or replace a decomposition.
460 size = dectmp_size + (4 - (dectmp_size & 3));
461 if (decomps[i].size < size) {
462 if (decomps[i].size == 0)
463 decomps[i].decomp = (unsigned long *)
464 malloc(sizeof(unsigned long) * size);
466 decomps[i].decomp = (unsigned long *)
467 realloc((char *) decomps[i].decomp,
468 sizeof(unsigned long) * size);
469 decomps[i].size = size;
472 if (decomps[i].code != code)
475 decomps[i].code = code;
476 decomps[i].used = dectmp_size;
477 (void) AC_MEMCPY((char *) decomps[i].decomp, (char *) dectmp,
478 sizeof(unsigned long) * dectmp_size);
481 * NOTICE: This needs changing later so it is more general than simply
482 * pairs. This calculation is done here to simplify allocation elsewhere.
484 if (dectmp_size == 2)
489 add_title(unsigned long code)
494 * Always map the code to itself.
498 if (title_used == title_size) {
500 title = (_case_t *) malloc(sizeof(_case_t) << 3);
502 title = (_case_t *) realloc((char *) title,
503 sizeof(_case_t) * (title_size + 8));
508 * Locate the insertion point.
510 for (i = 0; i < title_used && code > title[i].key; i++) ;
512 if (i < title_used) {
514 * Shift the array up by one.
516 for (j = title_used; j > i; j--)
517 (void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
521 title[i].key = cases[2]; /* Title */
522 title[i].other1 = cases[0]; /* Upper */
523 title[i].other2 = cases[1]; /* Lower */
529 add_upper(unsigned long code)
534 * Always map the code to itself.
539 * If the title case character is not present, then make it the same as
545 if (upper_used == upper_size) {
547 upper = (_case_t *) malloc(sizeof(_case_t) << 3);
549 upper = (_case_t *) realloc((char *) upper,
550 sizeof(_case_t) * (upper_size + 8));
555 * Locate the insertion point.
557 for (i = 0; i < upper_used && code > upper[i].key; i++) ;
559 if (i < upper_used) {
561 * Shift the array up by one.
563 for (j = upper_used; j > i; j--)
564 (void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
568 upper[i].key = cases[0]; /* Upper */
569 upper[i].other1 = cases[1]; /* Lower */
570 upper[i].other2 = cases[2]; /* Title */
576 add_lower(unsigned long code)
581 * Always map the code to itself.
586 * If the title case character is empty, then make it the same as the
592 if (lower_used == lower_size) {
594 lower = (_case_t *) malloc(sizeof(_case_t) << 3);
596 lower = (_case_t *) realloc((char *) lower,
597 sizeof(_case_t) * (lower_size + 8));
602 * Locate the insertion point.
604 for (i = 0; i < lower_used && code > lower[i].key; i++) ;
606 if (i < lower_used) {
608 * Shift the array up by one.
610 for (j = lower_used; j > i; j--)
611 (void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
615 lower[i].key = cases[1]; /* Lower */
616 lower[i].other1 = cases[0]; /* Upper */
617 lower[i].other2 = cases[2]; /* Title */
623 ordered_ccl_insert(unsigned long c, unsigned long ccl_code)
627 if (ccl_used == ccl_size) {
629 ccl = (unsigned long *) malloc(sizeof(unsigned long) * 24);
631 ccl = (unsigned long *)
632 realloc((char *) ccl, sizeof(unsigned long) * (ccl_size + 24));
637 * Optimize adding the first item.
647 * Handle the special case of extending the range on the end. This
648 * requires that the combining class codes are the same.
650 if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
651 ccl[ccl_used - 2] = c;
656 * Handle the special case of adding another range on the end.
658 if (c > ccl[ccl_used - 2] + 1 ||
659 (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
662 ccl[ccl_used++] = ccl_code;
667 * Locate either the insertion point or range for the code.
669 for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
671 if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
673 * Extend an existing range.
677 } else if (c < ccl[i]) {
679 * Start a new range before the current location.
681 for (j = ccl_used; j > i; j -= 3) {
683 ccl[j - 1] = ccl[j - 4];
684 ccl[j - 2] = ccl[j - 5];
686 ccl[i] = ccl[i + 1] = c;
687 ccl[i + 2] = ccl_code;
692 * Adds a number if it does not already exist and returns an index value
696 make_number(short num, short denom)
701 * Determine if the number already exists.
703 for (n = 0; n < nums_used; n++) {
704 if (nums[n].numerator == num && nums[n].denominator == denom)
708 if (nums_used == nums_size) {
710 nums = (_num_t *) malloc(sizeof(_num_t) << 3);
712 nums = (_num_t *) realloc((char *) nums,
713 sizeof(_num_t) * (nums_size + 8));
718 nums[n].numerator = num;
719 nums[n].denominator = denom;
725 add_number(unsigned long code, short num, short denom)
730 * Insert the code in order.
732 for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
735 * Handle the case of the codes matching and simply replace the number
736 * that was there before.
738 if (ncodes_used > 0 && code == ncodes[i].code) {
739 ncodes[i].idx = make_number(num, denom);
744 * Resize the array if necessary.
746 if (ncodes_used == ncodes_size) {
747 if (ncodes_size == 0)
748 ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
750 ncodes = (_codeidx_t *)
751 realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
757 * Shift things around to insert the code if necessary.
759 if (i < ncodes_used) {
760 for (j = ncodes_used; j > i; j--) {
761 ncodes[j].code = ncodes[j - 1].code;
762 ncodes[j].idx = ncodes[j - 1].idx;
765 ncodes[i].code = code;
766 ncodes[i].idx = make_number(num, denom);
772 * This routine assumes that the line is a valid Unicode Character Database
778 unsigned long i, lineno, skip, code, ccl_code;
779 short wnum, neg, number[2];
780 char line[512], *s, *e;
783 while (fscanf(in, "%[^\n]\n", line) != EOF) {
787 * Skip blank lines and lines that start with a '#'.
789 if (line[0] == 0 || line[0] == '#')
793 * If lines need to be skipped, do it here.
801 * Collect the code. The code can be up to 6 hex digits in length to
802 * allow surrogates to be specified.
804 for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
806 if (*s >= '0' && *s <= '9')
808 else if (*s >= 'A' && *s <= 'F')
809 code += (*s - 'A') + 10;
810 else if (*s >= 'a' && *s <= 'f')
811 code += (*s - 'a') + 10;
815 * Handle the following special cases:
816 * 1. 4E00-9FA5 CJK Ideographs.
817 * 2. AC00-D7A3 Hangul Syllables.
818 * 3. D800-DFFF Surrogates.
819 * 4. E000-F8FF Private Use Area.
820 * 5. F900-FA2D Han compatibility.
825 * The Han ideographs.
827 add_range(0x4e00, 0x9fff, "Lo", "L");
830 * Add the characters to the defined category.
832 add_range(0x4e00, 0x9fa5, "Cp", 0);
838 * The Hangul syllables.
840 add_range(0xac00, 0xd7a3, "Lo", "L");
843 * Add the characters to the defined category.
845 add_range(0xac00, 0xd7a3, "Cp", 0);
851 * Make a range of all surrogates and assume some default
854 add_range(0x010000, 0x10ffff, "Cs", "L");
859 * The Private Use area. Add with a default set of properties.
861 add_range(0xe000, 0xf8ff, "Co", "L");
866 * The CJK compatibility area.
868 add_range(0xf900, 0xfaff, "Lo", "L");
871 * Add the characters to the defined category.
873 add_range(0xf900, 0xfaff, "Cp", 0);
882 * Add the code to the defined category.
884 ordered_range_insert(code, "Cp", 2);
887 * Locate the first character property field.
889 for (i = 0; *s != 0 && i < 2; s++) {
893 for (e = s; *e && *e != ';'; e++) ;
895 ordered_range_insert(code, s, e - s);
898 * Locate the combining class code.
900 for (s = e; *s != 0 && i < 3; s++) {
906 * Convert the combining class code from decimal.
908 for (ccl_code = 0, e = s; *e && *e != ';'; e++)
909 ccl_code = (ccl_code * 10) + (*e - '0');
912 * Add the code if it not 0.
915 ordered_ccl_insert(code, ccl_code);
918 * Locate the second character property field.
920 for (s = e; *s != 0 && i < 4; s++) {
924 for (e = s; *e && *e != ';'; e++) ;
926 ordered_range_insert(code, s, e - s);
929 * Check for a decomposition.
932 if (*s != ';' && *s != '<') {
934 * Collect the codes of the decomposition.
936 for (dectmp_size = 0; *s != ';'; ) {
938 * Skip all leading non-hex digits.
940 while (!ishdigit(*s))
943 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
944 dectmp[dectmp_size] <<= 4;
945 if (*s >= '0' && *s <= '9')
946 dectmp[dectmp_size] += *s - '0';
947 else if (*s >= 'A' && *s <= 'F')
948 dectmp[dectmp_size] += (*s - 'A') + 10;
949 else if (*s >= 'a' && *s <= 'f')
950 dectmp[dectmp_size] += (*s - 'a') + 10;
956 * If there are any codes in the temporary decomposition array,
957 * then add the character with its decomposition.
964 * Skip to the number field.
966 for (i = 0; i < 3 && *s; s++) {
972 * Scan the number in.
974 number[0] = number[1] = 0;
975 for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
983 * Move the the denominator of the fraction.
991 number[wnum] = (number[wnum] * 10) + (*e - '0');
996 * Adjust the denominator in case of integers and add the number.
999 number[1] = number[0];
1001 add_number(code, number[0], number[1]);
1005 * Skip to the start of the possible case mappings.
1007 for (s = e, i = 0; i < 4 && *s; s++) {
1013 * Collect the case mappings.
1015 cases[0] = cases[1] = cases[2] = 0;
1016 for (i = 0; i < 3; i++) {
1017 while (ishdigit(*s)) {
1019 if (*s >= '0' && *s <= '9')
1020 cases[i] += *s - '0';
1021 else if (*s >= 'A' && *s <= 'F')
1022 cases[i] += (*s - 'A') + 10;
1023 else if (*s >= 'a' && *s <= 'f')
1024 cases[i] += (*s - 'a') + 10;
1030 if (cases[0] && cases[1])
1032 * Add the upper and lower mappings for a title case character.
1037 * Add the lower and title case mappings for the upper case
1043 * Add the upper and title case mappings for the lower case
1051 find_decomp(unsigned long code)
1056 r = decomps_used - 1;
1059 if (code > decomps[m].code)
1061 else if (code < decomps[m].code)
1070 decomp_it(_decomp_t *d)
1075 for (i = 0; i < d->used; i++) {
1076 if ((dp = find_decomp(d->decomp[i])) != 0)
1079 dectmp[dectmp_size++] = d->decomp[i];
1084 * Expand all decompositions by recursively decomposing each character
1085 * in the decomposition.
1092 for (i = 0; i < decomps_used; i++) {
1094 decomp_it(&decomps[i]);
1095 if (dectmp_size > 0)
1096 add_decomp(decomps[i].code);
1101 cmpcomps(_comp_t *comp1, _comp_t *comp2)
1103 long diff = comp1->code1 - comp2->code1;
1106 diff = comp1->code2 - comp2->code2;
1111 * Load composition exclusion data
1114 read_compexdata(FILE *in)
1116 unsigned short i, code;
1119 (void) memset((char *) compexs, 0, sizeof(unsigned long) << 11);
1121 while (fscanf(in, "%[^\n]\n", line) != EOF) {
1123 * Skip blank lines and lines that start with a '#'.
1125 if (line[0] == 0 || line[0] == '#')
1129 * Collect the code. Assume max 4 digits
1132 for (s = line, i = code = 0; *s != '#' && i < 4; i++, s++) {
1134 if (*s >= '0' && *s <= '9')
1136 else if (*s >= 'A' && *s <= 'F')
1137 code += (*s - 'A') + 10;
1138 else if (*s >= 'a' && *s <= 'f')
1139 code += (*s - 'a') + 10;
1146 * Creates array of compositions from decomposition array
1151 unsigned long i, cu;
1153 comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
1155 for (i = cu = 0; i < decomps_used; i++) {
1156 if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
1158 comps[cu].comp = decomps[i].code;
1159 comps[cu].count = 2;
1160 comps[cu].code1 = decomps[i].decomp[0];
1161 comps[cu].code2 = decomps[i].decomp[1];
1165 qsort(comps, comps_used, sizeof(_comp_t),
1166 (int (*)(const void *, const void *)) cmpcomps);
1170 write_cdata(char *opath)
1173 unsigned long i, idx, bytes, nprops;
1174 unsigned short casecnt[2];
1177 /*****************************************************************
1179 * Generate the ctype data.
1181 *****************************************************************/
1184 * Open the ctype.dat file.
1186 sprintf(path, "%s%sctype.dat", opath, LDAP_DIRSEP);
1187 if ((out = fopen(path, "wb")) == 0)
1191 * Collect the offsets for the properties. The offsets array is
1192 * on a 4-byte boundary to keep things efficient for architectures
1193 * that need such a thing.
1195 for (i = idx = 0; i < NUMPROPS; i++) {
1196 propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
1197 idx += proptbl[i].used;
1201 * Add the sentinel index which is used by the binary search as the upper
1202 * bound for a search.
1207 * Record the actual number of property lists. This may be different than
1208 * the number of offsets actually written because of aligning on a 4-byte
1214 * Calculate the byte count needed and pad the property counts array to a
1217 if ((bytes = sizeof(unsigned short) * (NUMPROPS + 1)) & 3)
1218 bytes += 4 - (bytes & 3);
1219 nprops = bytes / sizeof(unsigned short);
1220 bytes += sizeof(unsigned long) * idx;
1225 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1228 * Write the byte count.
1230 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1233 * Write the property list counts.
1235 fwrite((char *) propcnt, sizeof(unsigned short), nprops, out);
1238 * Write the property lists.
1240 for (i = 0; i < NUMPROPS; i++) {
1241 if (proptbl[i].used > 0)
1242 fwrite((char *) proptbl[i].ranges, sizeof(unsigned long),
1243 proptbl[i].used, out);
1248 /*****************************************************************
1250 * Generate the case mapping data.
1252 *****************************************************************/
1255 * Open the case.dat file.
1257 sprintf(path, "%s%scase.dat", opath, LDAP_DIRSEP);
1258 if ((out = fopen(path, "wb")) == 0)
1262 * Write the case mapping tables.
1264 hdr[1] = upper_used + lower_used + title_used;
1265 casecnt[0] = upper_used;
1266 casecnt[1] = lower_used;
1271 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1274 * Write the upper and lower case table sizes.
1276 fwrite((char *) casecnt, sizeof(unsigned short), 2, out);
1280 * Write the upper case table.
1282 fwrite((char *) upper, sizeof(_case_t), upper_used, out);
1286 * Write the lower case table.
1288 fwrite((char *) lower, sizeof(_case_t), lower_used, out);
1292 * Write the title case table.
1294 fwrite((char *) title, sizeof(_case_t), title_used, out);
1298 /*****************************************************************
1300 * Generate the composition data.
1302 *****************************************************************/
1305 * Create compositions from decomposition data
1310 * Open the comp.dat file.
1312 sprintf(path, "%s%scomp.dat", opath, LDAP_DIRSEP);
1313 if ((out = fopen(path, "wb")) == 0)
1319 hdr[1] = (unsigned short) comps_used * 4;
1320 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1323 * Write out the byte count to maintain header size.
1325 bytes = comps_used * sizeof(_comp_t);
1326 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1329 * Now, if comps exist, write them out.
1332 fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
1336 /*****************************************************************
1338 * Generate the decomposition data.
1340 *****************************************************************/
1343 * Fully expand all decompositions before generating the output file.
1348 * Open the decomp.dat file.
1350 sprintf(path, "%s%sdecomp.dat", opath, LDAP_DIRSEP);
1351 if ((out = fopen(path, "wb")) == 0)
1354 hdr[1] = decomps_used;
1359 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1362 * Write a temporary byte count which will be calculated as the
1363 * decompositions are written out.
1366 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1370 * Write the list of decomp nodes.
1372 for (i = idx = 0; i < decomps_used; i++) {
1373 fwrite((char *) &decomps[i].code, sizeof(unsigned long), 1, out);
1374 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1375 idx += decomps[i].used;
1379 * Write the sentinel index as the last decomp node.
1381 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1384 * Write the decompositions themselves.
1386 for (i = 0; i < decomps_used; i++)
1387 fwrite((char *) decomps[i].decomp, sizeof(unsigned long),
1388 decomps[i].used, out);
1391 * Seek back to the beginning and write the byte count.
1393 bytes = (sizeof(unsigned long) * idx) +
1394 (sizeof(unsigned long) * ((hdr[1] << 1) + 1));
1395 fseek(out, sizeof(unsigned short) << 1, 0L);
1396 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1401 /*****************************************************************
1403 * Generate the combining class data.
1405 *****************************************************************/
1408 * Open the cmbcl.dat file.
1410 sprintf(path, "%s%scmbcl.dat", opath, LDAP_DIRSEP);
1411 if ((out = fopen(path, "wb")) == 0)
1415 * Set the number of ranges used. Each range has a combining class which
1416 * means each entry is a 3-tuple.
1418 hdr[1] = ccl_used / 3;
1423 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1426 * Write out the byte count to maintain header size.
1428 bytes = ccl_used * sizeof(unsigned long);
1429 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1433 * Write the combining class ranges out.
1435 fwrite((char *) ccl, sizeof(unsigned long), ccl_used, out);
1439 /*****************************************************************
1441 * Generate the number data.
1443 *****************************************************************/
1446 * Open the num.dat file.
1448 sprintf(path, "%s%snum.dat", opath, LDAP_DIRSEP);
1449 if ((out = fopen(path, "wb")) == 0)
1453 * The count part of the header will be the total number of codes that
1456 hdr[1] = (unsigned short) (ncodes_used << 1);
1457 bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
1462 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1465 * Write out the byte count to maintain header size.
1467 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1470 * Now, if number mappings exist, write them out.
1472 if (ncodes_used > 0) {
1473 fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
1474 fwrite((char *) nums, sizeof(_num_t), nums_used, out);
1484 "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
1485 fprintf(stderr, " datafile1 datafile2 ...\n\n");
1487 "-o output-directory\n\t\tWrite the output files to a different");
1488 fprintf(stderr, " directory (default: .).\n");
1490 "-x composition-exclusion\n\t\tFile of composition codes");
1491 fprintf(stderr, " that should be excluded.\n");
1496 main(int argc, char *argv[])
1501 if ((prog = strrchr(argv[0], *LDAP_DIRSEP)) != 0)
1513 if (argv[0][0] == '-') {
1514 switch (argv[0][1]) {
1523 if ((in = fopen(argv[0], "rb")) == 0)
1525 "%s: unable to open composition exclusion file %s\n",
1528 read_compexdata(in);
1537 if (in != stdin && in != NULL)
1539 if ((in = fopen(argv[0], "rb")) == 0)
1540 fprintf(stderr, "%s: unable to open ctype file %s\n",