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: ucdata.c,v 1.4 2001/01/02 18:46:20 mleisher Exp $" */
30 #include "ldap_config.h"
34 #include <ac/stdlib.h>
35 #include <ac/string.h>
36 #include <ac/unistd.h>
41 /**************************************************************************
43 * Miscellaneous types, data, and support functions.
45 **************************************************************************/
57 * A simple array of 32-bit masks for lookup.
59 static unsigned long masks32[32] = {
60 0x00000001UL, 0x00000002UL, 0x00000004UL, 0x00000008UL,
61 0x00000010UL, 0x00000020UL, 0x00000040UL, 0x00000080UL,
62 0x00000100UL, 0x00000200UL, 0x00000400UL, 0x00000800UL,
63 0x00001000UL, 0x00002000UL, 0x00004000UL, 0x00008000UL,
64 0x00010000UL, 0x00020000UL, 0x00040000UL, 0x00080000UL,
65 0x00100000UL, 0x00200000UL, 0x00400000UL, 0x00800000UL,
66 0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL,
67 0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL
70 #define endian_short(cc) (((cc) >> 8) | (((cc) & 0xff) << 8))
71 #define endian_long(cc) ((((cc) & 0xff) << 24)|((((cc) >> 8) & 0xff) << 16)|\
72 ((((cc) >> 16) & 0xff) << 8)|((cc) >> 24))
75 _ucopenfile(char *paths, char *filename, char *mode)
78 char *fp, *dp, *pp, path[BUFSIZ];
80 if (filename == 0 || *filename == 0)
86 while (*dp && *dp != ':')
95 if ((f = fopen(path, mode)) != 0)
105 /**************************************************************************
107 * Support for the character properties.
109 **************************************************************************/
111 static unsigned long _ucprop_size;
112 static unsigned short *_ucprop_offsets;
113 static unsigned long *_ucprop_ranges;
116 * Return -1 on error, 0 if okay
119 _ucprop_load(char *paths, int reload)
122 unsigned long size, i;
125 if (_ucprop_size > 0) {
128 * The character properties have already been loaded.
133 * Unload the current character property data in preparation for
134 * loading a new copy. Only the first array has to be deallocated
135 * because all the memory for the arrays is allocated as a single
138 free((char *) _ucprop_offsets);
142 if ((in = _ucopenfile(paths, "ctype.dat", "rb")) == 0)
148 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
150 if (hdr.bom == 0xfffe) {
151 hdr.cnt = endian_short(hdr.cnt);
152 hdr.size.bytes = endian_long(hdr.size.bytes);
155 if ((_ucprop_size = hdr.cnt) == 0) {
161 * Allocate all the storage needed for the lookup table.
163 _ucprop_offsets = (unsigned short *) malloc(hdr.size.bytes);
166 * Calculate the offset into the storage for the ranges. The offsets
167 * array is on a 4-byte boundary and one larger than the value provided in
168 * the header count field. This means the offset to the ranges must be
169 * calculated after aligning the count to a 4-byte boundary.
171 if ((size = ((hdr.cnt + 1) * sizeof(unsigned short))) & 3)
172 size += 4 - (size & 3);
174 _ucprop_ranges = (unsigned long *) (_ucprop_offsets + size);
177 * Load the offset array.
179 fread((char *) _ucprop_offsets, sizeof(unsigned short), size, in);
182 * Do an endian swap if necessary. Don't forget there is an extra node on
183 * the end with the final index.
185 if (hdr.bom == 0xfffe) {
186 for (i = 0; i <= _ucprop_size; i++)
187 _ucprop_offsets[i] = endian_short(_ucprop_offsets[i]);
191 * Load the ranges. The number of elements is in the last array position
194 fread((char *) _ucprop_ranges, sizeof(unsigned long),
195 _ucprop_offsets[_ucprop_size], in);
200 * Do an endian swap if necessary.
202 if (hdr.bom == 0xfffe) {
203 for (i = 0; i < _ucprop_offsets[_ucprop_size]; i++)
204 _ucprop_ranges[i] = endian_long(_ucprop_ranges[i]);
212 if (_ucprop_size == 0)
216 * Only need to free the offsets because the memory is allocated as a
219 free((char *) _ucprop_offsets);
224 _ucprop_lookup(unsigned long code, unsigned long n)
228 if (_ucprop_size == 0)
232 * There is an extra node on the end of the offsets to allow this routine
233 * to work right. If the index is 0xffff, then there are no nodes for the
236 if ((l = _ucprop_offsets[n]) == 0xffff)
240 * Locate the next offset that is not 0xffff. The sentinel at the end of
241 * the array is the max index value.
244 n + m < _ucprop_size && _ucprop_offsets[n + m] == 0xffff; m++) ;
246 r = _ucprop_offsets[n + m] - 1;
250 * Determine a "mid" point and adjust to make sure the mid point is at
251 * the beginning of a range pair.
255 if (code > _ucprop_ranges[m + 1])
257 else if (code < _ucprop_ranges[m])
259 else if (code >= _ucprop_ranges[m] && code <= _ucprop_ranges[m + 1])
266 ucisprop(unsigned long code, unsigned long mask1, unsigned long mask2)
270 if (mask1 == 0 && mask2 == 0)
273 for (i = 0; mask1 && i < 32; i++) {
274 if ((mask1 & masks32[i]) && _ucprop_lookup(code, i))
278 for (i = 32; mask2 && i < _ucprop_size; i++) {
279 if ((mask2 & masks32[i & 31]) && _ucprop_lookup(code, i))
286 /**************************************************************************
288 * Support for case mapping.
290 **************************************************************************/
292 static unsigned long _uccase_size;
293 static unsigned short _uccase_len[2];
294 static unsigned long *_uccase_map;
297 * Return -1 on error, 0 if okay
300 _uccase_load(char *paths, int reload)
306 if (_uccase_size > 0) {
309 * The case mappings have already been loaded.
313 free((char *) _uccase_map);
317 if ((in = _ucopenfile(paths, "case.dat", "rb")) == 0)
323 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
325 if (hdr.bom == 0xfffe) {
326 hdr.cnt = endian_short(hdr.cnt);
327 hdr.size.len[0] = endian_short(hdr.size.len[0]);
328 hdr.size.len[1] = endian_short(hdr.size.len[1]);
332 * Set the node count and lengths of the upper and lower case mapping
335 _uccase_size = hdr.cnt * 3;
336 _uccase_len[0] = hdr.size.len[0] * 3;
337 _uccase_len[1] = hdr.size.len[1] * 3;
339 _uccase_map = (unsigned long *)
340 malloc(_uccase_size * sizeof(unsigned long));
343 * Load the case mapping table.
345 fread((char *) _uccase_map, sizeof(unsigned long), _uccase_size, in);
348 * Do an endian swap if necessary.
350 if (hdr.bom == 0xfffe) {
351 for (i = 0; i < _uccase_size; i++)
352 _uccase_map[i] = endian_long(_uccase_map[i]);
361 if (_uccase_size == 0)
364 free((char *) _uccase_map);
369 _uccase_lookup(unsigned long code, long l, long r, int field)
374 * Do the binary search.
378 * Determine a "mid" point and adjust to make sure the mid point is at
379 * the beginning of a case mapping triple.
383 if (code > _uccase_map[m])
385 else if (code < _uccase_map[m])
387 else if (code == _uccase_map[m])
388 return _uccase_map[m + field];
395 uctoupper(unsigned long code)
403 if (ucislower(code)) {
405 * The character is lower case.
409 r = (l + _uccase_len[1]) - 3;
412 * The character is title case.
415 l = _uccase_len[0] + _uccase_len[1];
416 r = _uccase_size - 3;
418 return _uccase_lookup(code, l, r, field);
422 uctolower(unsigned long code)
430 if (ucisupper(code)) {
432 * The character is upper case.
436 r = _uccase_len[0] - 3;
439 * The character is title case.
442 l = _uccase_len[0] + _uccase_len[1];
443 r = _uccase_size - 3;
445 return _uccase_lookup(code, l, r, field);
449 uctotitle(unsigned long code)
458 * The offset will always be the same for converting to title case.
462 if (ucisupper(code)) {
464 * The character is upper case.
467 r = _uccase_len[0] - 3;
470 * The character is lower case.
473 r = (l + _uccase_len[1]) - 3;
475 return _uccase_lookup(code, l, r, field);
478 /**************************************************************************
480 * Support for compositions.
482 **************************************************************************/
484 static unsigned long _uccomp_size;
485 static unsigned long *_uccomp_data;
488 * Return -1 on error, 0 if okay
491 _uccomp_load(char *paths, int reload)
494 unsigned long size, i;
497 if (_uccomp_size > 0) {
500 * The compositions have already been loaded.
504 free((char *) _uccomp_data);
508 if ((in = _ucopenfile(paths, "comp.dat", "rb")) == 0)
514 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
516 if (hdr.bom == 0xfffe) {
517 hdr.cnt = endian_short(hdr.cnt);
518 hdr.size.bytes = endian_long(hdr.size.bytes);
521 _uccomp_size = hdr.cnt;
522 _uccomp_data = (unsigned long *) malloc(hdr.size.bytes);
525 * Read the composition data in.
527 size = hdr.size.bytes / sizeof(unsigned long);
528 fread((char *) _uccomp_data, sizeof(unsigned long), size, in);
531 * Do an endian swap if necessary.
533 if (hdr.bom == 0xfffe) {
534 for (i = 0; i < size; i++)
535 _uccomp_data[i] = endian_long(_uccomp_data[i]);
539 * Assume that the data is ordered on count, so that all compositions
540 * of length 2 come first. Only handling length 2 for now.
542 for (i = 1; i < size; i += 4)
543 if (_uccomp_data[i] != 2)
545 _uccomp_size = i - 1;
554 if (_uccomp_size == 0)
557 free((char *) _uccomp_data);
562 uccomp(unsigned long node1, unsigned long node2, unsigned long *comp)
567 r = _uccomp_size - 1;
572 if (node1 > _uccomp_data[m+2])
574 else if (node1 < _uccomp_data[m+2])
576 else if (node2 > _uccomp_data[m+3])
578 else if (node2 < _uccomp_data[m+3])
581 *comp = _uccomp_data[m];
589 uccomp_hangul(unsigned long *str, int len)
591 const int SBase = 0xAC00, LBase = 0x1100,
592 VBase = 0x1161, TBase = 0x11A7,
593 LCount = 19, VCount = 21, TCount = 28,
594 NCount = VCount * TCount, /* 588 */
595 SCount = LCount * NCount; /* 11172 */
598 unsigned long ch, last, lindex, sindex;
602 for ( i = 1; i < len; i++ ) {
605 /* check if two current characters are L and V */
606 lindex = last - LBase;
607 if (lindex < (unsigned long) LCount) {
608 unsigned long vindex = ch - VBase;
609 if (vindex < (unsigned long) VCount) {
610 /* make syllable of form LV */
611 last = SBase + (lindex * VCount + vindex) * TCount;
612 str[rlen-1] = last; /* reset last */
617 /* check if two current characters are LV and T */
618 sindex = last - SBase;
619 if (sindex < (unsigned long) SCount
620 && (sindex % TCount) == 0)
622 unsigned long tindex = ch - TBase;
623 if (tindex <= (unsigned long) TCount) {
624 /* make syllable of form LVT */
626 str[rlen-1] = last; /* reset last */
631 /* if neither case was true, just add the character */
640 uccanoncomp(unsigned long *str, int len)
643 unsigned long cl, prevcl, st, ch, co;
648 prevcl = uccombining_class(st) == 0 ? 0 : 256;
650 for (i = 1; i < len; i++) {
652 cl = uccombining_class(ch);
653 if (uccomp(st, ch, &co) && (prevcl < cl || prevcl == 0))
654 st = str[stpos] = co;
665 return uccomp_hangul(str, copos);
668 /**************************************************************************
670 * Support for decompositions.
672 **************************************************************************/
674 static unsigned long _ucdcmp_size;
675 static unsigned long *_ucdcmp_nodes;
676 static unsigned long *_ucdcmp_decomp;
678 static unsigned long _uckdcmp_size;
679 static unsigned long *_uckdcmp_nodes;
680 static unsigned long *_uckdcmp_decomp;
683 * Return -1 on error, 0 if okay
686 _ucdcmp_load(char *paths, int reload)
689 unsigned long size, i;
692 if (_ucdcmp_size > 0) {
695 * The decompositions have already been loaded.
699 free((char *) _ucdcmp_nodes);
703 if ((in = _ucopenfile(paths, "decomp.dat", "rb")) == 0)
709 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
711 if (hdr.bom == 0xfffe) {
712 hdr.cnt = endian_short(hdr.cnt);
713 hdr.size.bytes = endian_long(hdr.size.bytes);
716 _ucdcmp_size = hdr.cnt << 1;
717 _ucdcmp_nodes = (unsigned long *) malloc(hdr.size.bytes);
718 _ucdcmp_decomp = _ucdcmp_nodes + (_ucdcmp_size + 1);
721 * Read the decomposition data in.
723 size = hdr.size.bytes / sizeof(unsigned long);
724 fread((char *) _ucdcmp_nodes, sizeof(unsigned long), size, in);
727 * Do an endian swap if necessary.
729 if (hdr.bom == 0xfffe) {
730 for (i = 0; i < size; i++)
731 _ucdcmp_nodes[i] = endian_long(_ucdcmp_nodes[i]);
738 * Return -1 on error, 0 if okay
741 _uckdcmp_load(char *paths, int reload)
744 unsigned long size, i;
747 if (_uckdcmp_size > 0) {
750 * The decompositions have already been loaded.
754 free((char *) _uckdcmp_nodes);
758 if ((in = _ucopenfile(paths, "kdecomp.dat", "rb")) == 0)
764 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
766 if (hdr.bom == 0xfffe) {
767 hdr.cnt = endian_short(hdr.cnt);
768 hdr.size.bytes = endian_long(hdr.size.bytes);
771 _uckdcmp_size = hdr.cnt << 1;
772 _uckdcmp_nodes = (unsigned long *) malloc(hdr.size.bytes);
773 _uckdcmp_decomp = _uckdcmp_nodes + (_uckdcmp_size + 1);
776 * Read the decomposition data in.
778 size = hdr.size.bytes / sizeof(unsigned long);
779 fread((char *) _uckdcmp_nodes, sizeof(unsigned long), size, in);
782 * Do an endian swap if necessary.
784 if (hdr.bom == 0xfffe) {
785 for (i = 0; i < size; i++)
786 _uckdcmp_nodes[i] = endian_long(_uckdcmp_nodes[i]);
795 if (_ucdcmp_size == 0)
799 * Only need to free the offsets because the memory is allocated as a
802 free((char *) _ucdcmp_nodes);
807 _uckdcmp_unload(void)
809 if (_uckdcmp_size == 0)
813 * Only need to free the offsets because the memory is allocated as a
816 free((char *) _uckdcmp_nodes);
821 ucdecomp(unsigned long code, unsigned long *num, unsigned long **decomp)
825 if (code < _ucdcmp_nodes[0]) {
830 r = _ucdcmp_nodes[_ucdcmp_size] - 1;
834 * Determine a "mid" point and adjust to make sure the mid point is at
835 * the beginning of a code+offset pair.
839 if (code > _ucdcmp_nodes[m])
841 else if (code < _ucdcmp_nodes[m])
843 else if (code == _ucdcmp_nodes[m]) {
844 *num = _ucdcmp_nodes[m + 3] - _ucdcmp_nodes[m + 1];
845 *decomp = &_ucdcmp_decomp[_ucdcmp_nodes[m + 1]];
853 uckdecomp(unsigned long code, unsigned long *num, unsigned long **decomp)
857 if (code < _uckdcmp_nodes[0]) {
862 r = _uckdcmp_nodes[_uckdcmp_size] - 1;
866 * Determine a "mid" point and adjust to make sure the mid point is at
867 * the beginning of a code+offset pair.
871 if (code > _uckdcmp_nodes[m])
873 else if (code < _uckdcmp_nodes[m])
875 else if (code == _uckdcmp_nodes[m]) {
876 *num = _uckdcmp_nodes[m + 3] - _uckdcmp_nodes[m + 1];
877 *decomp = &_uckdcmp_decomp[_uckdcmp_nodes[m + 1]];
885 ucdecomp_hangul(unsigned long code, unsigned long *num, unsigned long decomp[])
887 if (!ucishangul(code))
891 decomp[0] = 0x1100 + (unsigned long) (code / 588);
892 decomp[1] = 0x1161 + (unsigned long) ((code % 588) / 28);
893 decomp[2] = 0x11a7 + (unsigned long) (code % 28);
894 *num = (decomp[2] != 0x11a7) ? 3 : 2;
899 /* mode == 0 for canonical, mode == 1 for compatibility */
901 uccanoncompatdecomp(const unsigned long *in, int inlen,
902 unsigned long **out, int *outlen, short mode)
906 unsigned long num, class, *decomp, hangdecomp[3];
909 *out = (unsigned long *) malloc(size * sizeof(**out));
914 for (j = 0; j < (unsigned) inlen; j++) {
915 if (mode ? uckdecomp(in[j], &num, &decomp) : ucdecomp(in[j], &num, &decomp)) {
916 if ( size - i < num) {
917 size = inlen + i - j + num - 1;
918 *out = (unsigned long *) realloc(*out, size * sizeof(**out));
922 for (k = 0; k < num; k++) {
923 class = uccombining_class(decomp[k]);
925 (*out)[i] = decomp[k];
927 for (l = i; l > 0; l--)
928 if (class >= uccombining_class((*out)[l-1]))
930 AC_MEMCPY(*out + l + 1, *out + l, (i - l) * sizeof(**out));
931 (*out)[l] = decomp[k];
935 } else if (ucdecomp_hangul(in[j], &num, hangdecomp)) {
936 if (size - i < num) {
937 size = inlen + i - j + num - 1;
938 *out = (unsigned long *) realloc(*out, size * sizeof(**out));
942 for (k = 0; k < num; k++) {
943 (*out)[i] = hangdecomp[k];
948 size = inlen + i - j;
949 *out = (unsigned long *) realloc(*out, size * sizeof(**out));
953 class = uccombining_class(in[j]);
957 for (l = i; l > 0; l--)
958 if (class >= uccombining_class((*out)[l-1]))
960 AC_MEMCPY(*out + l + 1, *out + l, (i - l) * sizeof(**out));
970 uccanondecomp(const unsigned long *in, int inlen,
971 unsigned long **out, int *outlen)
973 return uccanoncompatdecomp(in, inlen, out, outlen, 0);
977 uccompatdecomp(const unsigned long *in, int inlen,
978 unsigned long **out, int *outlen)
980 return uccanoncompatdecomp(in, inlen, out, outlen, 1);
983 /**************************************************************************
985 * Support for combining classes.
987 **************************************************************************/
989 static unsigned long _uccmcl_size;
990 static unsigned long *_uccmcl_nodes;
993 * Return -1 on error, 0 if okay
996 _uccmcl_load(char *paths, int reload)
1002 if (_uccmcl_size > 0) {
1005 * The combining classes have already been loaded.
1009 free((char *) _uccmcl_nodes);
1013 if ((in = _ucopenfile(paths, "cmbcl.dat", "rb")) == 0)
1019 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
1021 if (hdr.bom == 0xfffe) {
1022 hdr.cnt = endian_short(hdr.cnt);
1023 hdr.size.bytes = endian_long(hdr.size.bytes);
1026 _uccmcl_size = hdr.cnt * 3;
1027 _uccmcl_nodes = (unsigned long *) malloc(hdr.size.bytes);
1030 * Read the combining classes in.
1032 fread((char *) _uccmcl_nodes, sizeof(unsigned long), _uccmcl_size, in);
1035 * Do an endian swap if necessary.
1037 if (hdr.bom == 0xfffe) {
1038 for (i = 0; i < _uccmcl_size; i++)
1039 _uccmcl_nodes[i] = endian_long(_uccmcl_nodes[i]);
1046 _uccmcl_unload(void)
1048 if (_uccmcl_size == 0)
1051 free((char *) _uccmcl_nodes);
1056 uccombining_class(unsigned long code)
1061 r = _uccmcl_size - 1;
1066 if (code > _uccmcl_nodes[m + 1])
1068 else if (code < _uccmcl_nodes[m])
1070 else if (code >= _uccmcl_nodes[m] && code <= _uccmcl_nodes[m + 1])
1071 return _uccmcl_nodes[m + 2];
1076 /**************************************************************************
1078 * Support for numeric values.
1080 **************************************************************************/
1082 static unsigned long *_ucnum_nodes;
1083 static unsigned long _ucnum_size;
1084 static short *_ucnum_vals;
1087 * Return -1 on error, 0 if okay
1090 _ucnumb_load(char *paths, int reload)
1093 unsigned long size, i;
1096 if (_ucnum_size > 0) {
1099 * The numbers have already been loaded.
1103 free((char *) _ucnum_nodes);
1107 if ((in = _ucopenfile(paths, "num.dat", "rb")) == 0)
1113 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
1115 if (hdr.bom == 0xfffe) {
1116 hdr.cnt = endian_short(hdr.cnt);
1117 hdr.size.bytes = endian_long(hdr.size.bytes);
1120 _ucnum_size = hdr.cnt;
1121 _ucnum_nodes = (unsigned long *) malloc(hdr.size.bytes);
1122 _ucnum_vals = (short *) (_ucnum_nodes + _ucnum_size);
1125 * Read the combining classes in.
1127 fread((char *) _ucnum_nodes, sizeof(unsigned char), hdr.size.bytes, in);
1130 * Do an endian swap if necessary.
1132 if (hdr.bom == 0xfffe) {
1133 for (i = 0; i < _ucnum_size; i++)
1134 _ucnum_nodes[i] = endian_long(_ucnum_nodes[i]);
1137 * Determine the number of values that have to be adjusted.
1139 size = (hdr.size.bytes -
1140 (_ucnum_size * (sizeof(unsigned long) << 1))) /
1143 for (i = 0; i < size; i++)
1144 _ucnum_vals[i] = endian_short(_ucnum_vals[i]);
1151 _ucnumb_unload(void)
1153 if (_ucnum_size == 0)
1156 free((char *) _ucnum_nodes);
1161 ucnumber_lookup(unsigned long code, struct ucnumber *num)
1167 r = _ucnum_size - 1;
1170 * Determine a "mid" point and adjust to make sure the mid point is at
1171 * the beginning of a code+offset pair.
1175 if (code > _ucnum_nodes[m])
1177 else if (code < _ucnum_nodes[m])
1180 vp = _ucnum_vals + _ucnum_nodes[m + 1];
1181 num->numerator = (int) *vp++;
1182 num->denominator = (int) *vp;
1190 ucdigit_lookup(unsigned long code, int *digit)
1196 r = _ucnum_size - 1;
1199 * Determine a "mid" point and adjust to make sure the mid point is at
1200 * the beginning of a code+offset pair.
1204 if (code > _ucnum_nodes[m])
1206 else if (code < _ucnum_nodes[m])
1209 vp = _ucnum_vals + _ucnum_nodes[m + 1];
1210 if (*vp == *(vp + 1)) {
1221 ucgetnumber(unsigned long code)
1223 struct ucnumber num;
1226 * Initialize with some arbitrary value, because the caller simply cannot
1227 * tell for sure if the code is a number without calling the ucisnumber()
1228 * macro before calling this function.
1230 num.numerator = num.denominator = -111;
1232 (void) ucnumber_lookup(code, &num);
1238 ucgetdigit(unsigned long code)
1243 * Initialize with some arbitrary value, because the caller simply cannot
1244 * tell for sure if the code is a number without calling the ucisdigit()
1245 * macro before calling this function.
1249 (void) ucdigit_lookup(code, &dig);
1254 /**************************************************************************
1256 * Setup and cleanup routines.
1258 **************************************************************************/
1261 * Return 0 if okay, negative on error
1264 ucdata_load(char *paths, int masks)
1268 if (masks & UCDATA_CTYPE)
1269 error |= _ucprop_load(paths, 0) < 0 ? UCDATA_CTYPE : 0;
1270 if (masks & UCDATA_CASE)
1271 error |= _uccase_load(paths, 0) < 0 ? UCDATA_CASE : 0;
1272 if (masks & UCDATA_DECOMP)
1273 error |= _ucdcmp_load(paths, 0) < 0 ? UCDATA_DECOMP : 0;
1274 if (masks & UCDATA_CMBCL)
1275 error |= _uccmcl_load(paths, 0) < 0 ? UCDATA_CMBCL : 0;
1276 if (masks & UCDATA_NUM)
1277 error |= _ucnumb_load(paths, 0) < 0 ? UCDATA_NUM : 0;
1278 if (masks & UCDATA_COMP)
1279 error |= _uccomp_load(paths, 0) < 0 ? UCDATA_COMP : 0;
1280 if (masks & UCDATA_KDECOMP)
1281 error |= _uckdcmp_load(paths, 0) < 0 ? UCDATA_KDECOMP : 0;
1287 ucdata_unload(int masks)
1289 if (masks & UCDATA_CTYPE)
1291 if (masks & UCDATA_CASE)
1293 if (masks & UCDATA_DECOMP)
1295 if (masks & UCDATA_CMBCL)
1297 if (masks & UCDATA_NUM)
1299 if (masks & UCDATA_COMP)
1301 if (masks & UCDATA_KDECOMP)
1306 * Return 0 if okay, negative on error
1309 ucdata_reload(char *paths, int masks)
1313 if (masks & UCDATA_CTYPE)
1314 error |= _ucprop_load(paths, 1) < 0 ? UCDATA_CTYPE : 0;
1315 if (masks & UCDATA_CASE)
1316 error |= _uccase_load(paths, 1) < 0 ? UCDATA_CASE : 0;
1317 if (masks & UCDATA_DECOMP)
1318 error |= _ucdcmp_load(paths, 1) < 0 ? UCDATA_DECOMP : 0;
1319 if (masks & UCDATA_CMBCL)
1320 error |= _uccmcl_load(paths, 1) < 0 ? UCDATA_CMBCL : 0;
1321 if (masks & UCDATA_NUM)
1322 error |= _ucnumb_load(paths, 1) < 0 ? UCDATA_NUM : 0;
1323 if (masks & UCDATA_COMP)
1324 error |= _uccomp_load(paths, 1) < 0 ? UCDATA_COMP : 0;
1325 if (masks & UCDATA_KDECOMP)
1326 error |= _uckdcmp_load(paths, 1) < 0 ? UCDATA_KDECOMP : 0;
1337 unsigned long i, lo, *dec;
1338 struct ucnumber num;
1345 printf("NOT WEAK\n");
1347 printf("LOWER 0x%04lX\n", uctolower(0xff3a));
1348 printf("UPPER 0x%04lX\n", uctoupper(0xff5a));
1350 if (ucisalpha(0x1d5))
1353 printf("NOT ALPHA\n");
1355 if (ucisupper(0x1d5)) {
1357 lo = uctolower(0x1d5);
1358 printf("0x%04lx\n", lo);
1359 lo = uctotitle(0x1d5);
1360 printf("0x%04lx\n", lo);
1362 printf("NOT UPPER\n");
1364 if (ucistitle(0x1d5))
1367 printf("NOT TITLE\n");
1369 if (uciscomposite(0x1d5))
1370 printf("COMPOSITE\n");
1372 printf("NOT COMPOSITE\n");
1374 if (ucdecomp(0x1d5, &lo, &dec)) {
1375 for (i = 0; i < lo; i++)
1376 printf("0x%04lx ", dec[i]);
1380 if ((lo = uccombining_class(0x41)) != 0)
1381 printf("0x41 CCL %ld\n", lo);
1383 if (ucisxdigit(0xfeff))
1384 printf("0xFEFF HEX DIGIT\n");
1386 printf("0xFEFF NOT HEX DIGIT\n");
1388 if (ucisdefined(0x10000))
1389 printf("0x10000 DEFINED\n");
1391 printf("0x10000 NOT DEFINED\n");
1393 if (ucnumber_lookup(0x30, &num)) {
1394 if (num.numerator != num.denominator)
1395 printf("UCNUMBER: 0x30 = %d/%d\n", num.numerator, num.denominator);
1397 printf("UCNUMBER: 0x30 = %d\n", num.numerator);
1399 printf("UCNUMBER: 0x30 NOT A NUMBER\n");
1401 if (ucnumber_lookup(0xbc, &num)) {
1402 if (num.numerator != num.denominator)
1403 printf("UCNUMBER: 0xbc = %d/%d\n", num.numerator, num.denominator);
1405 printf("UCNUMBER: 0xbc = %d\n", num.numerator);
1407 printf("UCNUMBER: 0xbc NOT A NUMBER\n");
1410 if (ucnumber_lookup(0xff19, &num)) {
1411 if (num.numerator != num.denominator)
1412 printf("UCNUMBER: 0xff19 = %d/%d\n", num.numerator, num.denominator);
1414 printf("UCNUMBER: 0xff19 = %d\n", num.numerator);
1416 printf("UCNUMBER: 0xff19 NOT A NUMBER\n");
1418 if (ucnumber_lookup(0x4e00, &num)) {
1419 if (num.numerator != num.denominator)
1420 printf("UCNUMBER: 0x4e00 = %d/%d\n", num.numerator, num.denominator);
1422 printf("UCNUMBER: 0x4e00 = %d\n", num.numerator);
1424 printf("UCNUMBER: 0x4e00 NOT A NUMBER\n");
1426 if (ucdigit_lookup(0x06f9, &dig))
1427 printf("UCDIGIT: 0x6f9 = %d\n", dig);
1429 printf("UCDIGIT: 0x6f9 NOT A NUMBER\n");
1431 dig = ucgetdigit(0x0969);
1432 printf("UCGETDIGIT: 0x969 = %d\n", dig);
1434 num = ucgetnumber(0x30);
1435 if (num.numerator != num.denominator)
1436 printf("UCGETNUMBER: 0x30 = %d/%d\n", num.numerator, num.denominator);
1438 printf("UCGETNUMBER: 0x30 = %d\n", num.numerator);
1440 num = ucgetnumber(0xbc);
1441 if (num.numerator != num.denominator)
1442 printf("UCGETNUMBER: 0xbc = %d/%d\n", num.numerator, num.denominator);
1444 printf("UCGETNUMBER: 0xbc = %d\n", num.numerator);
1446 num = ucgetnumber(0xff19);
1447 if (num.numerator != num.denominator)
1448 printf("UCGETNUMBER: 0xff19 = %d/%d\n", num.numerator, num.denominator);
1450 printf("UCGETNUMBER: 0xff19 = %d\n", num.numerator);