2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Copyright 2001 Computing Research Labs, New Mexico State University
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
24 * The above copyright notice and this permission notice shall be included in
25 * all copies or substantial portions of the Software.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
31 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
32 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
33 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 /* $Id: ucgendat.c,v 1.4 2001/01/02 18:46:20 mleisher Exp $" */
38 #include "ldap_config.h"
41 #include <ac/stdlib.h>
42 #include <ac/string.h>
43 #include <ac/unistd.h>
50 #define HARDCODE_DATA 1
54 #define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\
55 ((cc) >= 'A' && (cc) <= 'F') ||\
56 ((cc) >= 'a' && (cc) <= 'f'))
59 * A header written to the output file with the byte-order-mark and the number
62 static ac_uint2 hdr[2] = {0xfeff, 0};
65 #define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3)))
73 * List of properties expected to be found in the Unicode Character Database
74 * including some implementation specific properties.
76 * The implementation specific properties are:
77 * Cm = Composed (can be decomposed)
79 * Sy = Symmetric (has left and right forms)
84 * Cp = Defined character
86 static _prop_t props[NUMPROPS] = {
87 {"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2},
88 {"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2},
89 {"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2},
90 {"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2},
91 {"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L", 1}, {"R", 1},
92 {"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B", 1},
93 {"S", 1}, {"WS", 2}, {"ON", 2},
94 {"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2},
95 {"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2}, {"AL", 2}
104 static _ranges_t proptbl[NUMPROPS];
107 * Make sure this array is sized to be on a 4-byte boundary at compile time.
109 static ac_uint2 propcnt[NEEDPROPS];
112 * Array used to collect a decomposition before adding it to the decomposition
115 static ac_uint4 dectmp[64];
116 static ac_uint4 dectmp_size;
126 * List of decomposition. Created and expanded in order as the characters are
127 * encountered. First list contains canonical mappings, second also includes
128 * compatibility mappings.
130 static _decomp_t *decomps;
131 static ac_uint4 decomps_used;
132 static ac_uint4 decomps_size;
134 static _decomp_t *kdecomps;
135 static ac_uint4 kdecomps_used;
136 static ac_uint4 kdecomps_size;
139 * Composition exclusion table stuff.
141 #define COMPEX_SET(c) (compexs[(c) >> 5] |= (1 << ((c) & 31)))
142 #define COMPEX_TEST(c) (compexs[(c) >> 5] & (1 << ((c) & 31)))
143 static ac_uint4 compexs[2048];
146 * Struct for holding a composition pair, and array of composition pairs
155 static _comp_t *comps;
156 static ac_uint4 comps_used;
159 * Types and lists for handling lists of case mappings.
167 static _case_t *upper;
168 static _case_t *lower;
169 static _case_t *title;
170 static ac_uint4 upper_used;
171 static ac_uint4 upper_size;
172 static ac_uint4 lower_used;
173 static ac_uint4 lower_size;
174 static ac_uint4 title_used;
175 static ac_uint4 title_size;
178 * Array used to collect case mappings before adding them to a list.
180 static ac_uint4 cases[3];
183 * An array to hold ranges for combining classes.
185 static ac_uint4 *ccl;
186 static ac_uint4 ccl_used;
187 static ac_uint4 ccl_size;
190 * Structures for handling numbers.
203 * Arrays to hold the mapping of codes to numbers.
205 static _codeidx_t *ncodes;
206 static ac_uint4 ncodes_used;
207 static ac_uint4 ncodes_size;
210 static ac_uint4 nums_used;
211 static ac_uint4 nums_size;
214 * Array for holding numbers.
217 static ac_uint4 nums_used;
218 static ac_uint4 nums_size;
221 add_range(ac_uint4 start, ac_uint4 end, char *p1, char *p2)
227 for (k = 0; k < 2; k++) {
239 for (i = 0; i < NUMPROPS; i++) {
240 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
250 * Resize the range list if necessary.
252 if (rlp->used == rlp->size) {
254 rlp->ranges = (ac_uint4 *)
255 malloc(sizeof(ac_uint4) << 3);
257 rlp->ranges = (ac_uint4 *)
258 realloc((char *) rlp->ranges,
259 sizeof(ac_uint4) * (rlp->size + 8));
264 * If this is the first code for this property list, just add it
267 if (rlp->used == 0) {
268 rlp->ranges[0] = start;
269 rlp->ranges[1] = end;
275 * Optimize the case of adding the range to the end.
278 if (start > rlp->ranges[j]) {
280 rlp->ranges[j++] = start;
281 rlp->ranges[j++] = end;
287 * Need to locate the insertion point.
290 i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
293 * If the start value lies in the current range, then simply set the
294 * new end point of the range to the end value passed as a parameter.
296 if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
297 rlp->ranges[i + 1] = end;
302 * Shift following values up by two.
304 for (j = rlp->used; j > i; j -= 2) {
305 rlp->ranges[j] = rlp->ranges[j - 2];
306 rlp->ranges[j + 1] = rlp->ranges[j - 1];
310 * Add the new range at the insertion point.
312 rlp->ranges[i] = start;
313 rlp->ranges[i + 1] = end;
319 ordered_range_insert(ac_uint4 c, char *name, int len)
329 * Deal with directionality codes introduced in Unicode 3.0.
331 if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
333 (memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
334 memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
335 memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
337 * Mark all of these as Other Neutral to preserve compatibility with
344 for (i = 0; i < NUMPROPS; i++) {
345 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
353 * Have a match, so insert the code in order.
358 * Resize the range list if necessary.
360 if (rlp->used == rlp->size) {
362 rlp->ranges = (ac_uint4 *)
363 malloc(sizeof(ac_uint4) << 3);
365 rlp->ranges = (ac_uint4 *)
366 realloc((char *) rlp->ranges,
367 sizeof(ac_uint4) * (rlp->size + 8));
372 * If this is the first code for this property list, just add it
375 if (rlp->used == 0) {
376 rlp->ranges[0] = rlp->ranges[1] = c;
382 * Optimize the cases of extending the last range and adding new ranges to
387 s = rlp->ranges[j - 1];
391 * Extend the last range.
399 * Start another range on the end.
402 rlp->ranges[j] = rlp->ranges[j + 1] = c;
409 * The code is a duplicate of a code in the last range, so just return.
414 * The code should be inserted somewhere before the last range in the
415 * list. Locate the insertion point.
418 i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
421 e = rlp->ranges[i + 1];
425 * Simply extend the current range.
427 rlp->ranges[i + 1] = c;
430 * Add a new entry before the current location. Shift all entries
431 * before the current one up by one to make room.
433 for (j = rlp->used; j > i; j -= 2) {
434 rlp->ranges[j] = rlp->ranges[j - 2];
435 rlp->ranges[j + 1] = rlp->ranges[j - 1];
437 rlp->ranges[i] = rlp->ranges[i + 1] = c;
444 add_decomp(ac_uint4 code, short compat)
447 _decomp_t **pdecomps;
448 ac_uint4 *pdecomps_used;
449 ac_uint4 *pdecomps_size;
452 pdecomps = &kdecomps;
453 pdecomps_used = &kdecomps_used;
454 pdecomps_size = &kdecomps_size;
457 pdecomps_used = &decomps_used;
458 pdecomps_size = &decomps_size;
462 * Add the code to the composite property.
465 ordered_range_insert(code, "Cm", 2);
469 * Locate the insertion point for the code.
471 for (i = 0; i < *pdecomps_used && code > (*pdecomps)[i].code; i++) ;
474 * Allocate space for a new decomposition.
476 if (*pdecomps_used == *pdecomps_size) {
477 if (*pdecomps_size == 0)
478 *pdecomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
480 *pdecomps = (_decomp_t *)
481 realloc((char *) *pdecomps,
482 sizeof(_decomp_t) * (*pdecomps_size + 8));
483 (void) memset((char *) (*pdecomps + *pdecomps_size), '\0',
484 sizeof(_decomp_t) << 3);
488 if (i < *pdecomps_used && code != (*pdecomps)[i].code) {
490 * Shift the decomps up by one if the codes don't match.
492 for (j = *pdecomps_used; j > i; j--)
493 (void) AC_MEMCPY((char *) &(*pdecomps)[j], (char *) &(*pdecomps)[j - 1],
498 * Insert or replace a decomposition.
500 size = dectmp_size + (4 - (dectmp_size & 3));
501 if ((*pdecomps)[i].size < size) {
502 if ((*pdecomps)[i].size == 0)
503 (*pdecomps)[i].decomp = (ac_uint4 *)
504 malloc(sizeof(ac_uint4) * size);
506 (*pdecomps)[i].decomp = (ac_uint4 *)
507 realloc((char *) (*pdecomps)[i].decomp,
508 sizeof(ac_uint4) * size);
509 (*pdecomps)[i].size = size;
512 if ((*pdecomps)[i].code != code)
515 (*pdecomps)[i].code = code;
516 (*pdecomps)[i].used = dectmp_size;
517 (void) AC_MEMCPY((char *) (*pdecomps)[i].decomp, (char *) dectmp,
518 sizeof(ac_uint4) * dectmp_size);
521 * NOTICE: This needs changing later so it is more general than simply
522 * pairs. This calculation is done here to simplify allocation elsewhere.
524 if (!compat && dectmp_size == 2)
529 add_title(ac_uint4 code)
534 * Always map the code to itself.
538 if (title_used == title_size) {
540 title = (_case_t *) malloc(sizeof(_case_t) << 3);
542 title = (_case_t *) realloc((char *) title,
543 sizeof(_case_t) * (title_size + 8));
548 * Locate the insertion point.
550 for (i = 0; i < title_used && code > title[i].key; i++) ;
552 if (i < title_used) {
554 * Shift the array up by one.
556 for (j = title_used; j > i; j--)
557 (void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
561 title[i].key = cases[2]; /* Title */
562 title[i].other1 = cases[0]; /* Upper */
563 title[i].other2 = cases[1]; /* Lower */
569 add_upper(ac_uint4 code)
574 * Always map the code to itself.
579 * If the title case character is not present, then make it the same as
585 if (upper_used == upper_size) {
587 upper = (_case_t *) malloc(sizeof(_case_t) << 3);
589 upper = (_case_t *) realloc((char *) upper,
590 sizeof(_case_t) * (upper_size + 8));
595 * Locate the insertion point.
597 for (i = 0; i < upper_used && code > upper[i].key; i++) ;
599 if (i < upper_used) {
601 * Shift the array up by one.
603 for (j = upper_used; j > i; j--)
604 (void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
608 upper[i].key = cases[0]; /* Upper */
609 upper[i].other1 = cases[1]; /* Lower */
610 upper[i].other2 = cases[2]; /* Title */
616 add_lower(ac_uint4 code)
621 * Always map the code to itself.
626 * If the title case character is empty, then make it the same as the
632 if (lower_used == lower_size) {
634 lower = (_case_t *) malloc(sizeof(_case_t) << 3);
636 lower = (_case_t *) realloc((char *) lower,
637 sizeof(_case_t) * (lower_size + 8));
642 * Locate the insertion point.
644 for (i = 0; i < lower_used && code > lower[i].key; i++) ;
646 if (i < lower_used) {
648 * Shift the array up by one.
650 for (j = lower_used; j > i; j--)
651 (void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
655 lower[i].key = cases[1]; /* Lower */
656 lower[i].other1 = cases[0]; /* Upper */
657 lower[i].other2 = cases[2]; /* Title */
663 ordered_ccl_insert(ac_uint4 c, ac_uint4 ccl_code)
667 if (ccl_used == ccl_size) {
669 ccl = (ac_uint4 *) malloc(sizeof(ac_uint4) * 24);
672 realloc((char *) ccl, sizeof(ac_uint4) * (ccl_size + 24));
677 * Optimize adding the first item.
687 * Handle the special case of extending the range on the end. This
688 * requires that the combining class codes are the same.
690 if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
691 ccl[ccl_used - 2] = c;
696 * Handle the special case of adding another range on the end.
698 if (c > ccl[ccl_used - 2] + 1 ||
699 (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
702 ccl[ccl_used++] = ccl_code;
707 * Locate either the insertion point or range for the code.
709 for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
711 if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
713 * Extend an existing range.
717 } else if (c < ccl[i]) {
719 * Start a new range before the current location.
721 for (j = ccl_used; j > i; j -= 3) {
723 ccl[j - 1] = ccl[j - 4];
724 ccl[j - 2] = ccl[j - 5];
726 ccl[i] = ccl[i + 1] = c;
727 ccl[i + 2] = ccl_code;
732 * Adds a number if it does not already exist and returns an index value
736 make_number(short num, short denom)
741 * Determine if the number already exists.
743 for (n = 0; n < nums_used; n++) {
744 if (nums[n].numerator == num && nums[n].denominator == denom)
748 if (nums_used == nums_size) {
750 nums = (_num_t *) malloc(sizeof(_num_t) << 3);
752 nums = (_num_t *) realloc((char *) nums,
753 sizeof(_num_t) * (nums_size + 8));
758 nums[n].numerator = num;
759 nums[n].denominator = denom;
765 add_number(ac_uint4 code, short num, short denom)
770 * Insert the code in order.
772 for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
775 * Handle the case of the codes matching and simply replace the number
776 * that was there before.
778 if (i < ncodes_used && code == ncodes[i].code) {
779 ncodes[i].idx = make_number(num, denom);
784 * Resize the array if necessary.
786 if (ncodes_used == ncodes_size) {
787 if (ncodes_size == 0)
788 ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
790 ncodes = (_codeidx_t *)
791 realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
797 * Shift things around to insert the code if necessary.
799 if (i < ncodes_used) {
800 for (j = ncodes_used; j > i; j--) {
801 ncodes[j].code = ncodes[j - 1].code;
802 ncodes[j].idx = ncodes[j - 1].idx;
805 ncodes[i].code = code;
806 ncodes[i].idx = make_number(num, denom);
812 * This routine assumes that the line is a valid Unicode Character Database
818 ac_uint4 i, lineno, skip, code, ccl_code;
819 short wnum, neg, number[2], compat;
820 char line[512], *s, *e;
823 while (fgets(line, sizeof(line), in)) {
824 if( (s=strchr(line, '\n')) ) *s = '\0';
828 * Skip blank lines and lines that start with a '#'.
830 if (line[0] == 0 || line[0] == '#')
834 * If lines need to be skipped, do it here.
842 * Collect the code. The code can be up to 6 hex digits in length to
843 * allow surrogates to be specified.
845 for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
847 if (*s >= '0' && *s <= '9')
849 else if (*s >= 'A' && *s <= 'F')
850 code += (*s - 'A') + 10;
851 else if (*s >= 'a' && *s <= 'f')
852 code += (*s - 'a') + 10;
856 * Handle the following special cases:
857 * 1. 4E00-9FA5 CJK Ideographs.
858 * 2. AC00-D7A3 Hangul Syllables.
859 * 3. D800-DFFF Surrogates.
860 * 4. E000-F8FF Private Use Area.
861 * 5. F900-FA2D Han compatibility.
862 * ...Plus additional ranges in newer Unicode versions...
866 /* CJK Ideograph Extension A */
867 add_range(0x3400, 0x4db5, "Lo", "L");
869 add_range(0x3400, 0x4db5, "Cp", 0);
875 * The Han ideographs.
877 add_range(0x4e00, 0x9fff, "Lo", "L");
880 * Add the characters to the defined category.
882 add_range(0x4e00, 0x9fa5, "Cp", 0);
888 * The Hangul syllables.
890 add_range(0xac00, 0xd7a3, "Lo", "L");
893 * Add the characters to the defined category.
895 add_range(0xac00, 0xd7a3, "Cp", 0);
901 * Make a range of all surrogates and assume some default
904 add_range(0x010000, 0x10ffff, "Cs", "L");
909 * The Private Use area. Add with a default set of properties.
911 add_range(0xe000, 0xf8ff, "Co", "L");
916 * The CJK compatibility area.
918 add_range(0xf900, 0xfaff, "Lo", "L");
921 * Add the characters to the defined category.
923 add_range(0xf900, 0xfaff, "Cp", 0);
928 /* CJK Ideograph Extension B */
929 add_range(0x20000, 0x2a6d6, "Lo", "L");
931 add_range(0x20000, 0x2a6d6, "Cp", 0);
936 /* Plane 15 private use */
937 add_range(0xf0000, 0xffffd, "Co", "L");
942 /* Plane 16 private use */
943 add_range(0x100000, 0x10fffd, "Co", "L");
952 * Add the code to the defined category.
954 ordered_range_insert(code, "Cp", 2);
957 * Locate the first character property field.
959 for (i = 0; *s != 0 && i < 2; s++) {
963 for (e = s; *e && *e != ';'; e++) ;
965 ordered_range_insert(code, s, e - s);
968 * Locate the combining class code.
970 for (s = e; *s != 0 && i < 3; s++) {
976 * Convert the combining class code from decimal.
978 for (ccl_code = 0, e = s; *e && *e != ';'; e++)
979 ccl_code = (ccl_code * 10) + (*e - '0');
982 * Add the code if it not 0.
985 ordered_ccl_insert(code, ccl_code);
988 * Locate the second character property field.
990 for (s = e; *s != 0 && i < 4; s++) {
994 for (e = s; *e && *e != ';'; e++) ;
996 ordered_range_insert(code, s, e - s);
999 * Check for a decomposition.
1006 * Skip compatibility formatting tag.
1008 while (*s++ != '>');
1011 * Collect the codes of the decomposition.
1013 for (dectmp_size = 0; *s != ';'; ) {
1015 * Skip all leading non-hex digits.
1017 while (!ishdigit(*s))
1020 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
1021 dectmp[dectmp_size] <<= 4;
1022 if (*s >= '0' && *s <= '9')
1023 dectmp[dectmp_size] += *s - '0';
1024 else if (*s >= 'A' && *s <= 'F')
1025 dectmp[dectmp_size] += (*s - 'A') + 10;
1026 else if (*s >= 'a' && *s <= 'f')
1027 dectmp[dectmp_size] += (*s - 'a') + 10;
1033 * If there are any codes in the temporary decomposition array,
1034 * then add the character with its decomposition.
1036 if (dectmp_size > 0) {
1038 add_decomp(code, 0);
1040 add_decomp(code, 1);
1045 * Skip to the number field.
1047 for (i = 0; i < 3 && *s; s++) {
1053 * Scan the number in.
1055 number[0] = number[1] = 0;
1056 for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
1064 * Move the the denominator of the fraction.
1072 number[wnum] = (number[wnum] * 10) + (*e - '0');
1077 * Adjust the denominator in case of integers and add the number.
1082 add_number(code, number[0], number[1]);
1086 * Skip to the start of the possible case mappings.
1088 for (s = e, i = 0; i < 4 && *s; s++) {
1094 * Collect the case mappings.
1096 cases[0] = cases[1] = cases[2] = 0;
1097 for (i = 0; i < 3; i++) {
1098 while (ishdigit(*s)) {
1100 if (*s >= '0' && *s <= '9')
1101 cases[i] += *s - '0';
1102 else if (*s >= 'A' && *s <= 'F')
1103 cases[i] += (*s - 'A') + 10;
1104 else if (*s >= 'a' && *s <= 'f')
1105 cases[i] += (*s - 'a') + 10;
1111 if (cases[0] && cases[1])
1113 * Add the upper and lower mappings for a title case character.
1118 * Add the lower and title case mappings for the upper case
1124 * Add the upper and title case mappings for the lower case
1132 find_decomp(ac_uint4 code, short compat)
1138 r = (compat ? kdecomps_used : decomps_used) - 1;
1139 decs = compat ? kdecomps : decomps;
1142 if (code > decs[m].code)
1144 else if (code < decs[m].code)
1153 decomp_it(_decomp_t *d, short compat)
1158 for (i = 0; i < d->used; i++) {
1159 if ((dp = find_decomp(d->decomp[i], compat)) != 0)
1160 decomp_it(dp, compat);
1162 dectmp[dectmp_size++] = d->decomp[i];
1167 * Expand all decompositions by recursively decomposing each character
1168 * in the decomposition.
1175 for (i = 0; i < decomps_used; i++) {
1177 decomp_it(&decomps[i], 0);
1178 if (dectmp_size > 0)
1179 add_decomp(decomps[i].code, 0);
1182 for (i = 0; i < kdecomps_used; i++) {
1184 decomp_it(&kdecomps[i], 1);
1185 if (dectmp_size > 0)
1186 add_decomp(kdecomps[i].code, 1);
1191 cmpcomps(const void *v_comp1, const void *v_comp2)
1193 const _comp_t *comp1 = v_comp1, *comp2 = v_comp2;
1194 long diff = comp1->code1 - comp2->code1;
1197 diff = comp1->code2 - comp2->code2;
1202 * Load composition exclusion data
1205 read_compexdata(FILE *in)
1210 (void) memset((char *) compexs, 0, sizeof(ac_uint4) << 11);
1212 while (fgets(line, sizeof(line), in)) {
1213 if( (s=strchr(line, '\n')) ) *s = '\0';
1215 * Skip blank lines and lines that start with a '#'.
1217 if (line[0] == 0 || line[0] == '#')
1221 * Collect the code. Assume max 4 digits
1224 for (s = line, i = code = 0; *s != '#' && i < 4; i++, s++) {
1226 if (*s >= '0' && *s <= '9')
1228 else if (*s >= 'A' && *s <= 'F')
1229 code += (*s - 'A') + 10;
1230 else if (*s >= 'a' && *s <= 'f')
1231 code += (*s - 'a') + 10;
1238 * Creates array of compositions from decomposition array
1245 comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
1247 for (i = cu = 0; i < decomps_used; i++) {
1248 if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
1250 comps[cu].comp = decomps[i].code;
1251 comps[cu].count = 2;
1252 comps[cu].code1 = decomps[i].decomp[0];
1253 comps[cu].code2 = decomps[i].decomp[1];
1257 qsort(comps, comps_used, sizeof(_comp_t), cmpcomps);
1262 write_case(FILE *out, _case_t *tab, int num, int first)
1266 for (i=0; i<num; i++) {
1267 if (first) first = 0;
1268 else fprintf(out, ",");
1269 fprintf(out, "\n\t0x%08x, 0x%08x, 0x%08x",
1270 tab[i].key, tab[i].other1, tab[i].other2);
1274 #define PREF "static const "
1279 write_cdata(char *opath)
1283 ac_uint4 i, idx, nprops;
1284 ac_uint2 casecnt[2];
1289 /*****************************************************************
1291 * Generate the ctype data.
1293 *****************************************************************/
1296 * Open the output file.
1298 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "uctable.h", opath);
1299 if ((out = fopen(path, "w")) == 0)
1303 * Open the ctype.dat file.
1305 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "ctype.dat", opath);
1306 if ((out = fopen(path, "wb")) == 0)
1311 * Collect the offsets for the properties. The offsets array is
1312 * on a 4-byte boundary to keep things efficient for architectures
1313 * that need such a thing.
1315 for (i = idx = 0; i < NUMPROPS; i++) {
1316 propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
1317 idx += proptbl[i].used;
1321 * Add the sentinel index which is used by the binary search as the upper
1322 * bound for a search.
1327 * Record the actual number of property lists. This may be different than
1328 * the number of offsets actually written because of aligning on a 4-byte
1334 * Calculate the byte count needed and pad the property counts array to a
1337 if ((bytes = sizeof(ac_uint2) * (NUMPROPS + 1)) & 3)
1338 bytes += 4 - (bytes & 3);
1339 nprops = bytes / sizeof(ac_uint2);
1340 bytes += sizeof(ac_uint4) * idx;
1343 fprintf(out, PREF "ac_uint4 _ucprop_size = %d;\n\n", NUMPROPS);
1345 fprintf(out, PREF "ac_uint2 _ucprop_offsets[] = {");
1347 for (i = 0; i<nprops; i++) {
1348 if (i) fprintf(out, ",");
1349 if (!(i&7)) fprintf(out, "\n\t");
1350 else fprintf(out, " ");
1351 fprintf(out, "0x%04x", propcnt[i]);
1353 fprintf(out, "\n};\n\n");
1355 fprintf(out, PREF "ac_uint4 _ucprop_ranges[] = {");
1358 for (i = 0; i < NUMPROPS; i++) {
1359 if (proptbl[i].used > 0) {
1360 for (j=0; j<proptbl[i].used; j++) {
1361 if (k) fprintf(out, ",");
1362 if (!(k&3)) fprintf(out,"\n\t");
1363 else fprintf(out, " ");
1365 fprintf(out, "0x%08x", proptbl[i].ranges[j]);
1369 fprintf(out, "\n};\n\n");
1374 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1377 * Write the byte count.
1379 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1382 * Write the property list counts.
1384 fwrite((char *) propcnt, sizeof(ac_uint2), nprops, out);
1387 * Write the property lists.
1389 for (i = 0; i < NUMPROPS; i++) {
1390 if (proptbl[i].used > 0)
1391 fwrite((char *) proptbl[i].ranges, sizeof(ac_uint4),
1392 proptbl[i].used, out);
1398 /*****************************************************************
1400 * Generate the case mapping data.
1402 *****************************************************************/
1405 fprintf(out, PREF "ac_uint4 _uccase_size = %d;\n\n",
1406 upper_used + lower_used + title_used);
1408 fprintf(out, PREF "ac_uint2 _uccase_len[2] = {%d, %d};\n\n",
1409 upper_used, lower_used);
1410 fprintf(out, PREF "ac_uint4 _uccase_map[] = {");
1414 * Write the upper case table.
1416 write_case(out, upper, upper_used, 1);
1420 * Write the lower case table.
1422 write_case(out, lower, lower_used, !upper_used);
1426 * Write the title case table.
1428 write_case(out, title, title_used, !(upper_used||lower_used));
1430 if (!(upper_used || lower_used || title_used))
1431 fprintf(out, "\t0");
1433 fprintf(out, "\n};\n\n");
1436 * Open the case.dat file.
1438 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "case.dat", opath);
1439 if ((out = fopen(path, "wb")) == 0)
1443 * Write the case mapping tables.
1445 hdr[1] = upper_used + lower_used + title_used;
1446 casecnt[0] = upper_used;
1447 casecnt[1] = lower_used;
1452 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1455 * Write the upper and lower case table sizes.
1457 fwrite((char *) casecnt, sizeof(ac_uint2), 2, out);
1461 * Write the upper case table.
1463 fwrite((char *) upper, sizeof(_case_t), upper_used, out);
1467 * Write the lower case table.
1469 fwrite((char *) lower, sizeof(_case_t), lower_used, out);
1473 * Write the title case table.
1475 fwrite((char *) title, sizeof(_case_t), title_used, out);
1480 /*****************************************************************
1482 * Generate the composition data.
1484 *****************************************************************/
1487 * Create compositions from decomposition data
1492 fprintf(out, PREF "ac_uint4 _uccomp_size = %d;\n\n",
1495 fprintf(out, PREF "ac_uint4 _uccomp_data[] = {");
1498 * Now, if comps exist, write them out.
1500 if (comps_used > 0) {
1501 for (i=0; i<comps_used; i++) {
1502 if (i) fprintf(out, ",");
1503 fprintf(out, "\n\t0x%08x, 0x%08x, 0x%08x, 0x%08x",
1504 comps[i].comp, comps[i].count, comps[i].code1, comps[i].code2);
1507 fprintf(out, "\t0");
1509 fprintf(out, "\n};\n\n");
1512 * Open the comp.dat file.
1514 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "comp.dat", opath);
1515 if ((out = fopen(path, "wb")) == 0)
1521 hdr[1] = (ac_uint2) comps_used * 4;
1522 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1525 * Write out the byte count to maintain header size.
1527 bytes = comps_used * sizeof(_comp_t);
1528 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1531 * Now, if comps exist, write them out.
1534 fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
1539 /*****************************************************************
1541 * Generate the decomposition data.
1543 *****************************************************************/
1546 * Fully expand all decompositions before generating the output file.
1551 fprintf(out, PREF "ac_uint4 _ucdcmp_size = %d;\n\n",
1554 fprintf(out, PREF "ac_uint4 _ucdcmp_nodes[] = {");
1558 * Write the list of decomp nodes.
1560 for (i = idx = 0; i < decomps_used; i++) {
1561 fprintf(out, "\n\t0x%08x, 0x%08x,", decomps[i].code, idx);
1562 idx += decomps[i].used;
1566 * Write the sentinel index as the last decomp node.
1568 fprintf(out, "\n\t0x%08x\n};\n\n", idx);
1570 fprintf(out, PREF "ac_uint4 _ucdcmp_decomp[] = {");
1572 * Write the decompositions themselves.
1575 for (i = 0; i < decomps_used; i++)
1576 for (j=0; j<decomps[i].used; j++) {
1577 if (k) fprintf(out, ",");
1578 if (!(k&3)) fprintf(out,"\n\t");
1579 else fprintf(out, " ");
1581 fprintf(out, "0x%08x", decomps[i].decomp[j]);
1583 fprintf(out, "\n};\n\n");
1587 * Open the decomp.dat file.
1589 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "decomp.dat", opath);
1590 if ((out = fopen(path, "wb")) == 0)
1593 hdr[1] = decomps_used;
1598 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1601 * Write a temporary byte count which will be calculated as the
1602 * decompositions are written out.
1605 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1609 * Write the list of decomp nodes.
1611 for (i = idx = 0; i < decomps_used; i++) {
1612 fwrite((char *) &decomps[i].code, sizeof(ac_uint4), 1, out);
1613 fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
1614 idx += decomps[i].used;
1618 * Write the sentinel index as the last decomp node.
1620 fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
1623 * Write the decompositions themselves.
1625 for (i = 0; i < decomps_used; i++)
1626 fwrite((char *) decomps[i].decomp, sizeof(ac_uint4),
1627 decomps[i].used, out);
1630 * Seek back to the beginning and write the byte count.
1632 bytes = (sizeof(ac_uint4) * idx) +
1633 (sizeof(ac_uint4) * ((hdr[1] << 1) + 1));
1634 fseek(out, sizeof(ac_uint2) << 1, 0L);
1635 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1641 #ifdef HARDCODE_DATA
1642 fprintf(out, PREF "ac_uint4 _uckdcmp_size = %d;\n\n",
1645 fprintf(out, PREF "ac_uint4 _uckdcmp_nodes[] = {");
1647 if (kdecomps_used) {
1649 * Write the list of kdecomp nodes.
1651 for (i = idx = 0; i < kdecomps_used; i++) {
1652 fprintf(out, "\n\t0x%08x, 0x%08x,", kdecomps[i].code, idx);
1653 idx += kdecomps[i].used;
1657 * Write the sentinel index as the last decomp node.
1659 fprintf(out, "\n\t0x%08x\n};\n\n", idx);
1661 fprintf(out, PREF "ac_uint4 _uckdcmp_decomp[] = {");
1664 * Write the decompositions themselves.
1667 for (i = 0; i < kdecomps_used; i++)
1668 for (j=0; j<kdecomps[i].used; j++) {
1669 if (k) fprintf(out, ",");
1670 if (!(k&3)) fprintf(out,"\n\t");
1671 else fprintf(out, " ");
1673 fprintf(out, "0x%08x", kdecomps[i].decomp[j]);
1675 fprintf(out, "\n};\n\n");
1679 * Open the kdecomp.dat file.
1681 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "kdecomp.dat", opath);
1682 if ((out = fopen(path, "wb")) == 0)
1685 hdr[1] = kdecomps_used;
1690 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1693 * Write a temporary byte count which will be calculated as the
1694 * decompositions are written out.
1697 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1699 if (kdecomps_used) {
1701 * Write the list of kdecomp nodes.
1703 for (i = idx = 0; i < kdecomps_used; i++) {
1704 fwrite((char *) &kdecomps[i].code, sizeof(ac_uint4), 1, out);
1705 fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
1706 idx += kdecomps[i].used;
1710 * Write the sentinel index as the last decomp node.
1712 fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
1715 * Write the decompositions themselves.
1717 for (i = 0; i < kdecomps_used; i++)
1718 fwrite((char *) kdecomps[i].decomp, sizeof(ac_uint4),
1719 kdecomps[i].used, out);
1722 * Seek back to the beginning and write the byte count.
1724 bytes = (sizeof(ac_uint4) * idx) +
1725 (sizeof(ac_uint4) * ((hdr[1] << 1) + 1));
1726 fseek(out, sizeof(ac_uint2) << 1, 0L);
1727 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1733 /*****************************************************************
1735 * Generate the combining class data.
1737 *****************************************************************/
1738 #ifdef HARDCODE_DATA
1739 fprintf(out, PREF "ac_uint4 _uccmcl_size = %d;\n\n", ccl_used);
1741 fprintf(out, PREF "ac_uint4 _uccmcl_nodes[] = {");
1745 * Write the combining class ranges out.
1747 for (i = 0; i<ccl_used; i++) {
1748 if (i) fprintf(out, ",");
1749 if (!(i&3)) fprintf(out, "\n\t");
1750 else fprintf(out, " ");
1751 fprintf(out, "0x%08x", ccl[i]);
1754 fprintf(out, "\t0");
1756 fprintf(out, "\n};\n\n");
1759 * Open the cmbcl.dat file.
1761 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "cmbcl.dat", opath);
1762 if ((out = fopen(path, "wb")) == 0)
1766 * Set the number of ranges used. Each range has a combining class which
1767 * means each entry is a 3-tuple.
1769 hdr[1] = ccl_used / 3;
1774 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1777 * Write out the byte count to maintain header size.
1779 bytes = ccl_used * sizeof(ac_uint4);
1780 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1784 * Write the combining class ranges out.
1786 fwrite((char *) ccl, sizeof(ac_uint4), ccl_used, out);
1791 /*****************************************************************
1793 * Generate the number data.
1795 *****************************************************************/
1798 fprintf(out, PREF "ac_uint4 _ucnum_size = %d;\n\n", ncodes_used<<1);
1800 fprintf(out, PREF "ac_uint4 _ucnum_nodes[] = {");
1803 * Now, if number mappings exist, write them out.
1805 if (ncodes_used > 0) {
1806 for (i = 0; i<ncodes_used; i++) {
1807 if (i) fprintf(out, ",");
1808 if (!(i&1)) fprintf(out, "\n\t");
1809 else fprintf(out, " ");
1810 fprintf(out, "0x%08x, 0x%08x", ncodes[i].code, ncodes[i].idx);
1812 fprintf(out, "\n};\n\n");
1814 fprintf(out, PREF "short _ucnum_vals[] = {");
1815 for (i = 0; i<nums_used; i++) {
1816 if (i) fprintf(out, ",");
1817 if (!(i&3)) fprintf(out, "\n\t");
1818 else fprintf(out, " ");
1819 if (nums[i].numerator < 0) {
1820 fprintf(out, "%6d, 0x%04x",
1821 nums[i].numerator, nums[i].denominator);
1823 fprintf(out, "0x%04x, 0x%04x",
1824 nums[i].numerator, nums[i].denominator);
1827 fprintf(out, "\n};\n\n");
1831 * Open the num.dat file.
1833 snprintf(path, sizeof path, "%s" LDAP_DIRSEP "num.dat", opath);
1834 if ((out = fopen(path, "wb")) == 0)
1838 * The count part of the header will be the total number of codes that
1841 hdr[1] = (ac_uint2) (ncodes_used << 1);
1842 bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
1847 fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
1850 * Write out the byte count to maintain header size.
1852 fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
1855 * Now, if number mappings exist, write them out.
1857 if (ncodes_used > 0) {
1858 fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
1859 fwrite((char *) nums, sizeof(_num_t), nums_used, out);
1870 "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
1871 fprintf(stderr, " datafile1 datafile2 ...\n\n");
1873 "-o output-directory\n\t\tWrite the output files to a different");
1874 fprintf(stderr, " directory (default: .).\n");
1876 "-x composition-exclusion\n\t\tFile of composition codes");
1877 fprintf(stderr, " that should be excluded.\n");
1882 main(int argc, char *argv[])
1887 prog = lutil_progname( "ucgendat", argc, argv );
1896 if (argv[0][0] == '-') {
1897 switch (argv[0][1]) {
1906 if ((in = fopen(argv[0], "r")) == 0)
1908 "%s: unable to open composition exclusion file %s\n",
1911 read_compexdata(in);
1920 if (in != stdin && in != NULL)
1922 if ((in = fopen(argv[0], "r")) == 0)
1923 fprintf(stderr, "%s: unable to open ctype file %s\n",