2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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: ucdata.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>
51 #define HARDCODE_DATA 1
58 /**************************************************************************
60 * Miscellaneous types, data, and support functions.
62 **************************************************************************/
74 * A simple array of 32-bit masks for lookup.
76 static ac_uint4 masks32[32] = {
77 0x00000001UL, 0x00000002UL, 0x00000004UL, 0x00000008UL,
78 0x00000010UL, 0x00000020UL, 0x00000040UL, 0x00000080UL,
79 0x00000100UL, 0x00000200UL, 0x00000400UL, 0x00000800UL,
80 0x00001000UL, 0x00002000UL, 0x00004000UL, 0x00008000UL,
81 0x00010000UL, 0x00020000UL, 0x00040000UL, 0x00080000UL,
82 0x00100000UL, 0x00200000UL, 0x00400000UL, 0x00800000UL,
83 0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL,
84 0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL
87 #define endian_short(cc) (((cc) >> 8) | (((cc) & 0xff) << 8))
88 #define endian_long(cc) ((((cc) & 0xff) << 24)|((((cc) >> 8) & 0xff) << 16)|\
89 ((((cc) >> 16) & 0xff) << 8)|((cc) >> 24))
93 _ucopenfile(char *paths, char *filename, char *mode)
96 char *fp, *dp, *pp, path[BUFSIZ];
98 if (filename == 0 || *filename == 0)
104 while (*dp && *dp != ':')
106 *pp++ = *LDAP_DIRSEP;
113 if ((f = fopen(path, mode)) != 0)
124 /**************************************************************************
126 * Support for the character properties.
128 **************************************************************************/
132 static ac_uint4 _ucprop_size;
133 static ac_uint2 *_ucprop_offsets;
134 static ac_uint4 *_ucprop_ranges;
137 * Return -1 on error, 0 if okay
140 _ucprop_load(char *paths, int reload)
146 if (_ucprop_size > 0) {
149 * The character properties have already been loaded.
154 * Unload the current character property data in preparation for
155 * loading a new copy. Only the first array has to be deallocated
156 * because all the memory for the arrays is allocated as a single
159 free((char *) _ucprop_offsets);
163 if ((in = _ucopenfile(paths, "ctype.dat", "rb")) == 0)
169 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
171 if (hdr.bom == 0xfffe) {
172 hdr.cnt = endian_short(hdr.cnt);
173 hdr.size.bytes = endian_long(hdr.size.bytes);
176 if ((_ucprop_size = hdr.cnt) == 0) {
182 * Allocate all the storage needed for the lookup table.
184 _ucprop_offsets = (ac_uint2 *) malloc(hdr.size.bytes);
187 * Calculate the offset into the storage for the ranges. The offsets
188 * array is on a 4-byte boundary and one larger than the value provided in
189 * the header count field. This means the offset to the ranges must be
190 * calculated after aligning the count to a 4-byte boundary.
192 if ((size = ((hdr.cnt + 1) * sizeof(ac_uint2))) & 3)
193 size += 4 - (size & 3);
195 _ucprop_ranges = (ac_uint4 *) (_ucprop_offsets + size);
198 * Load the offset array.
200 fread((char *) _ucprop_offsets, sizeof(ac_uint2), size, in);
203 * Do an endian swap if necessary. Don't forget there is an extra node on
204 * the end with the final index.
206 if (hdr.bom == 0xfffe) {
207 for (i = 0; i <= _ucprop_size; i++)
208 _ucprop_offsets[i] = endian_short(_ucprop_offsets[i]);
212 * Load the ranges. The number of elements is in the last array position
215 fread((char *) _ucprop_ranges, sizeof(ac_uint4),
216 _ucprop_offsets[_ucprop_size], in);
221 * Do an endian swap if necessary.
223 if (hdr.bom == 0xfffe) {
224 for (i = 0; i < _ucprop_offsets[_ucprop_size]; i++)
225 _ucprop_ranges[i] = endian_long(_ucprop_ranges[i]);
233 if (_ucprop_size == 0)
237 * Only need to free the offsets because the memory is allocated as a
240 free((char *) _ucprop_offsets);
246 _ucprop_lookup(ac_uint4 code, ac_uint4 n)
250 if (_ucprop_size == 0)
254 * There is an extra node on the end of the offsets to allow this routine
255 * to work right. If the index is 0xffff, then there are no nodes for the
258 if ((l = _ucprop_offsets[n]) == 0xffff)
262 * Locate the next offset that is not 0xffff. The sentinel at the end of
263 * the array is the max index value.
266 n + m < _ucprop_size && _ucprop_offsets[n + m] == 0xffff; m++) ;
268 r = _ucprop_offsets[n + m] - 1;
272 * Determine a "mid" point and adjust to make sure the mid point is at
273 * the beginning of a range pair.
277 if (code > _ucprop_ranges[m + 1])
279 else if (code < _ucprop_ranges[m])
281 else if (code >= _ucprop_ranges[m] && code <= _ucprop_ranges[m + 1])
288 ucisprop(ac_uint4 code, ac_uint4 mask1, ac_uint4 mask2)
292 if (mask1 == 0 && mask2 == 0)
295 for (i = 0; mask1 && i < 32; i++) {
296 if ((mask1 & masks32[i]) && _ucprop_lookup(code, i))
300 for (i = 32; mask2 && i < _ucprop_size; i++) {
301 if ((mask2 & masks32[i & 31]) && _ucprop_lookup(code, i))
308 /**************************************************************************
310 * Support for case mapping.
312 **************************************************************************/
316 static ac_uint4 _uccase_size;
317 static ac_uint2 _uccase_len[2];
318 static ac_uint4 *_uccase_map;
321 * Return -1 on error, 0 if okay
324 _uccase_load(char *paths, int reload)
330 if (_uccase_size > 0) {
333 * The case mappings have already been loaded.
337 free((char *) _uccase_map);
341 if ((in = _ucopenfile(paths, "case.dat", "rb")) == 0)
347 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
349 if (hdr.bom == 0xfffe) {
350 hdr.cnt = endian_short(hdr.cnt);
351 hdr.size.len[0] = endian_short(hdr.size.len[0]);
352 hdr.size.len[1] = endian_short(hdr.size.len[1]);
356 * Set the node count and lengths of the upper and lower case mapping
359 _uccase_size = hdr.cnt * 3;
360 _uccase_len[0] = hdr.size.len[0] * 3;
361 _uccase_len[1] = hdr.size.len[1] * 3;
363 _uccase_map = (ac_uint4 *)
364 malloc(_uccase_size * sizeof(ac_uint4));
367 * Load the case mapping table.
369 fread((char *) _uccase_map, sizeof(ac_uint4), _uccase_size, in);
372 * Do an endian swap if necessary.
374 if (hdr.bom == 0xfffe) {
375 for (i = 0; i < _uccase_size; i++)
376 _uccase_map[i] = endian_long(_uccase_map[i]);
385 if (_uccase_size == 0)
388 free((char *) _uccase_map);
394 _uccase_lookup(ac_uint4 code, long l, long r, int field)
399 * Do the binary search.
403 * Determine a "mid" point and adjust to make sure the mid point is at
404 * the beginning of a case mapping triple.
408 if (code > _uccase_map[m])
410 else if (code < _uccase_map[m])
412 else if (code == _uccase_map[m])
413 return _uccase_map[m + field];
420 uctoupper(ac_uint4 code)
428 if (ucislower(code)) {
430 * The character is lower case.
434 r = (l + _uccase_len[1]) - 3;
437 * The character is title case.
440 l = _uccase_len[0] + _uccase_len[1];
441 r = _uccase_size - 3;
443 return _uccase_lookup(code, l, r, field);
447 uctolower(ac_uint4 code)
455 if (ucisupper(code)) {
457 * The character is upper case.
461 r = _uccase_len[0] - 3;
464 * The character is title case.
467 l = _uccase_len[0] + _uccase_len[1];
468 r = _uccase_size - 3;
470 return _uccase_lookup(code, l, r, field);
474 uctotitle(ac_uint4 code)
483 * The offset will always be the same for converting to title case.
487 if (ucisupper(code)) {
489 * The character is upper case.
492 r = _uccase_len[0] - 3;
495 * The character is lower case.
498 r = (l + _uccase_len[1]) - 3;
500 return _uccase_lookup(code, l, r, field);
503 /**************************************************************************
505 * Support for compositions.
507 **************************************************************************/
511 static ac_uint4 _uccomp_size;
512 static ac_uint4 *_uccomp_data;
515 * Return -1 on error, 0 if okay
518 _uccomp_load(char *paths, int reload)
524 if (_uccomp_size > 0) {
527 * The compositions have already been loaded.
531 free((char *) _uccomp_data);
535 if ((in = _ucopenfile(paths, "comp.dat", "rb")) == 0)
541 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
543 if (hdr.bom == 0xfffe) {
544 hdr.cnt = endian_short(hdr.cnt);
545 hdr.size.bytes = endian_long(hdr.size.bytes);
548 _uccomp_size = hdr.cnt;
549 _uccomp_data = (ac_uint4 *) malloc(hdr.size.bytes);
552 * Read the composition data in.
554 size = hdr.size.bytes / sizeof(ac_uint4);
555 fread((char *) _uccomp_data, sizeof(ac_uint4), size, in);
558 * Do an endian swap if necessary.
560 if (hdr.bom == 0xfffe) {
561 for (i = 0; i < size; i++)
562 _uccomp_data[i] = endian_long(_uccomp_data[i]);
566 * Assume that the data is ordered on count, so that all compositions
567 * of length 2 come first. Only handling length 2 for now.
569 for (i = 1; i < size; i += 4)
570 if (_uccomp_data[i] != 2)
572 _uccomp_size = i - 1;
581 if (_uccomp_size == 0)
584 free((char *) _uccomp_data);
590 uccomp(ac_uint4 node1, ac_uint4 node2, ac_uint4 *comp)
595 r = _uccomp_size - 1;
600 if (node1 > _uccomp_data[m+2])
602 else if (node1 < _uccomp_data[m+2])
604 else if (node2 > _uccomp_data[m+3])
606 else if (node2 < _uccomp_data[m+3])
609 *comp = _uccomp_data[m];
617 uccomp_hangul(ac_uint4 *str, int len)
619 const int SBase = 0xAC00, LBase = 0x1100,
620 VBase = 0x1161, TBase = 0x11A7,
621 LCount = 19, VCount = 21, TCount = 28,
622 NCount = VCount * TCount, /* 588 */
623 SCount = LCount * NCount; /* 11172 */
626 ac_uint4 ch, last, lindex, sindex;
630 for ( i = 1; i < len; i++ ) {
633 /* check if two current characters are L and V */
634 lindex = last - LBase;
635 if (lindex < (ac_uint4) LCount) {
636 ac_uint4 vindex = ch - VBase;
637 if (vindex < (ac_uint4) VCount) {
638 /* make syllable of form LV */
639 last = SBase + (lindex * VCount + vindex) * TCount;
640 str[rlen-1] = last; /* reset last */
645 /* check if two current characters are LV and T */
646 sindex = last - SBase;
647 if (sindex < (ac_uint4) SCount
648 && (sindex % TCount) == 0)
650 ac_uint4 tindex = ch - TBase;
651 if (tindex <= (ac_uint4) TCount) {
652 /* make syllable of form LVT */
654 str[rlen-1] = last; /* reset last */
659 /* if neither case was true, just add the character */
668 uccanoncomp(ac_uint4 *str, int len)
671 ac_uint4 cl, prevcl, st, ch, co;
676 prevcl = uccombining_class(st) == 0 ? 0 : 256;
678 for (i = 1; i < len; i++) {
680 cl = uccombining_class(ch);
681 if (uccomp(st, ch, &co) && (prevcl < cl || prevcl == 0))
682 st = str[stpos] = co;
693 return uccomp_hangul(str, copos);
696 /**************************************************************************
698 * Support for decompositions.
700 **************************************************************************/
704 static ac_uint4 _ucdcmp_size;
705 static ac_uint4 *_ucdcmp_nodes;
706 static ac_uint4 *_ucdcmp_decomp;
708 static ac_uint4 _uckdcmp_size;
709 static ac_uint4 *_uckdcmp_nodes;
710 static ac_uint4 *_uckdcmp_decomp;
713 * Return -1 on error, 0 if okay
716 _ucdcmp_load(char *paths, int reload)
722 if (_ucdcmp_size > 0) {
725 * The decompositions have already been loaded.
729 free((char *) _ucdcmp_nodes);
733 if ((in = _ucopenfile(paths, "decomp.dat", "rb")) == 0)
739 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
741 if (hdr.bom == 0xfffe) {
742 hdr.cnt = endian_short(hdr.cnt);
743 hdr.size.bytes = endian_long(hdr.size.bytes);
746 _ucdcmp_size = hdr.cnt << 1;
747 _ucdcmp_nodes = (ac_uint4 *) malloc(hdr.size.bytes);
748 _ucdcmp_decomp = _ucdcmp_nodes + (_ucdcmp_size + 1);
751 * Read the decomposition data in.
753 size = hdr.size.bytes / sizeof(ac_uint4);
754 fread((char *) _ucdcmp_nodes, sizeof(ac_uint4), size, in);
757 * Do an endian swap if necessary.
759 if (hdr.bom == 0xfffe) {
760 for (i = 0; i < size; i++)
761 _ucdcmp_nodes[i] = endian_long(_ucdcmp_nodes[i]);
768 * Return -1 on error, 0 if okay
771 _uckdcmp_load(char *paths, int reload)
777 if (_uckdcmp_size > 0) {
780 * The decompositions have already been loaded.
784 free((char *) _uckdcmp_nodes);
788 if ((in = _ucopenfile(paths, "kdecomp.dat", "rb")) == 0)
794 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
796 if (hdr.bom == 0xfffe) {
797 hdr.cnt = endian_short(hdr.cnt);
798 hdr.size.bytes = endian_long(hdr.size.bytes);
801 _uckdcmp_size = hdr.cnt << 1;
802 _uckdcmp_nodes = (ac_uint4 *) malloc(hdr.size.bytes);
803 _uckdcmp_decomp = _uckdcmp_nodes + (_uckdcmp_size + 1);
806 * Read the decomposition data in.
808 size = hdr.size.bytes / sizeof(ac_uint4);
809 fread((char *) _uckdcmp_nodes, sizeof(ac_uint4), size, in);
812 * Do an endian swap if necessary.
814 if (hdr.bom == 0xfffe) {
815 for (i = 0; i < size; i++)
816 _uckdcmp_nodes[i] = endian_long(_uckdcmp_nodes[i]);
825 if (_ucdcmp_size == 0)
829 * Only need to free the offsets because the memory is allocated as a
832 free((char *) _ucdcmp_nodes);
837 _uckdcmp_unload(void)
839 if (_uckdcmp_size == 0)
843 * Only need to free the offsets because the memory is allocated as a
846 free((char *) _uckdcmp_nodes);
852 ucdecomp(ac_uint4 code, ac_uint4 *num, ac_uint4 **decomp)
856 if (code < _ucdcmp_nodes[0]) {
861 r = _ucdcmp_nodes[_ucdcmp_size] - 1;
865 * Determine a "mid" point and adjust to make sure the mid point is at
866 * the beginning of a code+offset pair.
870 if (code > _ucdcmp_nodes[m])
872 else if (code < _ucdcmp_nodes[m])
874 else if (code == _ucdcmp_nodes[m]) {
875 *num = _ucdcmp_nodes[m + 3] - _ucdcmp_nodes[m + 1];
876 *decomp = (ac_uint4*)&_ucdcmp_decomp[_ucdcmp_nodes[m + 1]];
884 uckdecomp(ac_uint4 code, ac_uint4 *num, ac_uint4 **decomp)
888 if (code < _uckdcmp_nodes[0]) {
893 r = _uckdcmp_nodes[_uckdcmp_size] - 1;
897 * Determine a "mid" point and adjust to make sure the mid point is at
898 * the beginning of a code+offset pair.
902 if (code > _uckdcmp_nodes[m])
904 else if (code < _uckdcmp_nodes[m])
906 else if (code == _uckdcmp_nodes[m]) {
907 *num = _uckdcmp_nodes[m + 3] - _uckdcmp_nodes[m + 1];
908 *decomp = (ac_uint4*)&_uckdcmp_decomp[_uckdcmp_nodes[m + 1]];
916 ucdecomp_hangul(ac_uint4 code, ac_uint4 *num, ac_uint4 decomp[])
918 if (!ucishangul(code))
922 decomp[0] = 0x1100 + (ac_uint4) (code / 588);
923 decomp[1] = 0x1161 + (ac_uint4) ((code % 588) / 28);
924 decomp[2] = 0x11a7 + (ac_uint4) (code % 28);
925 *num = (decomp[2] != 0x11a7) ? 3 : 2;
930 /* mode == 0 for canonical, mode == 1 for compatibility */
932 uccanoncompatdecomp(const ac_uint4 *in, int inlen,
933 ac_uint4 **out, int *outlen, short mode, void *ctx)
937 ac_uint4 num, class, *decomp, hangdecomp[3];
940 *out = (ac_uint4 *) ber_memalloc_x(size * sizeof(**out), ctx);
945 for (j = 0; j < (unsigned) inlen; j++) {
946 if (mode ? uckdecomp(in[j], &num, &decomp) : ucdecomp(in[j], &num, &decomp)) {
947 if ( size - i < num) {
948 size = inlen + i - j + num - 1;
949 *out = (ac_uint4 *) ber_memrealloc_x(*out, size * sizeof(**out), ctx );
953 for (k = 0; k < num; k++) {
954 class = uccombining_class(decomp[k]);
956 (*out)[i] = decomp[k];
958 for (l = i; l > 0; l--)
959 if (class >= uccombining_class((*out)[l-1]))
961 AC_MEMCPY(*out + l + 1, *out + l, (i - l) * sizeof(**out));
962 (*out)[l] = decomp[k];
966 } else if (ucdecomp_hangul(in[j], &num, hangdecomp)) {
967 if (size - i < num) {
968 size = inlen + i - j + num - 1;
969 *out = (ac_uint4 *) ber_memrealloc_x(*out, size * sizeof(**out), ctx);
973 for (k = 0; k < num; k++) {
974 (*out)[i] = hangdecomp[k];
979 size = inlen + i - j;
980 *out = (ac_uint4 *) ber_memrealloc_x(*out, size * sizeof(**out), ctx);
984 class = uccombining_class(in[j]);
988 for (l = i; l > 0; l--)
989 if (class >= uccombining_class((*out)[l-1]))
991 AC_MEMCPY(*out + l + 1, *out + l, (i - l) * sizeof(**out));
1001 uccanondecomp(const ac_uint4 *in, int inlen,
1002 ac_uint4 **out, int *outlen, void *ctx)
1004 return uccanoncompatdecomp(in, inlen, out, outlen, 0, ctx);
1008 uccompatdecomp(const ac_uint4 *in, int inlen,
1009 ac_uint4 **out, int *outlen, void *ctx)
1011 return uccanoncompatdecomp(in, inlen, out, outlen, 1, ctx);
1014 /**************************************************************************
1016 * Support for combining classes.
1018 **************************************************************************/
1021 static ac_uint4 _uccmcl_size;
1022 static ac_uint4 *_uccmcl_nodes;
1025 * Return -1 on error, 0 if okay
1028 _uccmcl_load(char *paths, int reload)
1034 if (_uccmcl_size > 0) {
1037 * The combining classes have already been loaded.
1041 free((char *) _uccmcl_nodes);
1045 if ((in = _ucopenfile(paths, "cmbcl.dat", "rb")) == 0)
1051 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
1053 if (hdr.bom == 0xfffe) {
1054 hdr.cnt = endian_short(hdr.cnt);
1055 hdr.size.bytes = endian_long(hdr.size.bytes);
1058 _uccmcl_size = hdr.cnt * 3;
1059 _uccmcl_nodes = (ac_uint4 *) malloc(hdr.size.bytes);
1062 * Read the combining classes in.
1064 fread((char *) _uccmcl_nodes, sizeof(ac_uint4), _uccmcl_size, in);
1067 * Do an endian swap if necessary.
1069 if (hdr.bom == 0xfffe) {
1070 for (i = 0; i < _uccmcl_size; i++)
1071 _uccmcl_nodes[i] = endian_long(_uccmcl_nodes[i]);
1078 _uccmcl_unload(void)
1080 if (_uccmcl_size == 0)
1083 free((char *) _uccmcl_nodes);
1089 uccombining_class(ac_uint4 code)
1094 r = _uccmcl_size - 1;
1099 if (code > _uccmcl_nodes[m + 1])
1101 else if (code < _uccmcl_nodes[m])
1103 else if (code >= _uccmcl_nodes[m] && code <= _uccmcl_nodes[m + 1])
1104 return _uccmcl_nodes[m + 2];
1109 /**************************************************************************
1111 * Support for numeric values.
1113 **************************************************************************/
1116 static ac_uint4 *_ucnum_nodes;
1117 static ac_uint4 _ucnum_size;
1118 static short *_ucnum_vals;
1121 * Return -1 on error, 0 if okay
1124 _ucnumb_load(char *paths, int reload)
1130 if (_ucnum_size > 0) {
1133 * The numbers have already been loaded.
1137 free((char *) _ucnum_nodes);
1141 if ((in = _ucopenfile(paths, "num.dat", "rb")) == 0)
1147 fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
1149 if (hdr.bom == 0xfffe) {
1150 hdr.cnt = endian_short(hdr.cnt);
1151 hdr.size.bytes = endian_long(hdr.size.bytes);
1154 _ucnum_size = hdr.cnt;
1155 _ucnum_nodes = (ac_uint4 *) malloc(hdr.size.bytes);
1156 _ucnum_vals = (short *) (_ucnum_nodes + _ucnum_size);
1159 * Read the combining classes in.
1161 fread((char *) _ucnum_nodes, sizeof(unsigned char), hdr.size.bytes, in);
1164 * Do an endian swap if necessary.
1166 if (hdr.bom == 0xfffe) {
1167 for (i = 0; i < _ucnum_size; i++)
1168 _ucnum_nodes[i] = endian_long(_ucnum_nodes[i]);
1171 * Determine the number of values that have to be adjusted.
1173 size = (hdr.size.bytes -
1174 (_ucnum_size * (sizeof(ac_uint4) << 1))) /
1177 for (i = 0; i < size; i++)
1178 _ucnum_vals[i] = endian_short(_ucnum_vals[i]);
1185 _ucnumb_unload(void)
1187 if (_ucnum_size == 0)
1190 free((char *) _ucnum_nodes);
1196 ucnumber_lookup(ac_uint4 code, struct ucnumber *num)
1202 r = _ucnum_size - 1;
1205 * Determine a "mid" point and adjust to make sure the mid point is at
1206 * the beginning of a code+offset pair.
1210 if (code > _ucnum_nodes[m])
1212 else if (code < _ucnum_nodes[m])
1215 vp = (short *)_ucnum_vals + _ucnum_nodes[m + 1];
1216 num->numerator = (int) *vp++;
1217 num->denominator = (int) *vp;
1225 ucdigit_lookup(ac_uint4 code, int *digit)
1231 r = _ucnum_size - 1;
1234 * Determine a "mid" point and adjust to make sure the mid point is at
1235 * the beginning of a code+offset pair.
1239 if (code > _ucnum_nodes[m])
1241 else if (code < _ucnum_nodes[m])
1244 vp = (short *)_ucnum_vals + _ucnum_nodes[m + 1];
1245 if (*vp == *(vp + 1)) {
1256 ucgetnumber(ac_uint4 code)
1258 struct ucnumber num;
1261 * Initialize with some arbitrary value, because the caller simply cannot
1262 * tell for sure if the code is a number without calling the ucisnumber()
1263 * macro before calling this function.
1265 num.numerator = num.denominator = -111;
1267 (void) ucnumber_lookup(code, &num);
1273 ucgetdigit(ac_uint4 code)
1278 * Initialize with some arbitrary value, because the caller simply cannot
1279 * tell for sure if the code is a number without calling the ucisdigit()
1280 * macro before calling this function.
1284 (void) ucdigit_lookup(code, &dig);
1289 /**************************************************************************
1291 * Setup and cleanup routines.
1293 **************************************************************************/
1296 int ucdata_load(char *paths, int masks) { return 0; }
1297 void ucdata_unload(int masks) { }
1298 int ucdata_reload(char *paths, int masks) { return 0; }
1301 * Return 0 if okay, negative on error
1304 ucdata_load(char *paths, int masks)
1308 if (masks & UCDATA_CTYPE)
1309 error |= _ucprop_load(paths, 0) < 0 ? UCDATA_CTYPE : 0;
1310 if (masks & UCDATA_CASE)
1311 error |= _uccase_load(paths, 0) < 0 ? UCDATA_CASE : 0;
1312 if (masks & UCDATA_DECOMP)
1313 error |= _ucdcmp_load(paths, 0) < 0 ? UCDATA_DECOMP : 0;
1314 if (masks & UCDATA_CMBCL)
1315 error |= _uccmcl_load(paths, 0) < 0 ? UCDATA_CMBCL : 0;
1316 if (masks & UCDATA_NUM)
1317 error |= _ucnumb_load(paths, 0) < 0 ? UCDATA_NUM : 0;
1318 if (masks & UCDATA_COMP)
1319 error |= _uccomp_load(paths, 0) < 0 ? UCDATA_COMP : 0;
1320 if (masks & UCDATA_KDECOMP)
1321 error |= _uckdcmp_load(paths, 0) < 0 ? UCDATA_KDECOMP : 0;
1327 ucdata_unload(int masks)
1329 if (masks & UCDATA_CTYPE)
1331 if (masks & UCDATA_CASE)
1333 if (masks & UCDATA_DECOMP)
1335 if (masks & UCDATA_CMBCL)
1337 if (masks & UCDATA_NUM)
1339 if (masks & UCDATA_COMP)
1341 if (masks & UCDATA_KDECOMP)
1346 * Return 0 if okay, negative on error
1349 ucdata_reload(char *paths, int masks)
1353 if (masks & UCDATA_CTYPE)
1354 error |= _ucprop_load(paths, 1) < 0 ? UCDATA_CTYPE : 0;
1355 if (masks & UCDATA_CASE)
1356 error |= _uccase_load(paths, 1) < 0 ? UCDATA_CASE : 0;
1357 if (masks & UCDATA_DECOMP)
1358 error |= _ucdcmp_load(paths, 1) < 0 ? UCDATA_DECOMP : 0;
1359 if (masks & UCDATA_CMBCL)
1360 error |= _uccmcl_load(paths, 1) < 0 ? UCDATA_CMBCL : 0;
1361 if (masks & UCDATA_NUM)
1362 error |= _ucnumb_load(paths, 1) < 0 ? UCDATA_NUM : 0;
1363 if (masks & UCDATA_COMP)
1364 error |= _uccomp_load(paths, 1) < 0 ? UCDATA_COMP : 0;
1365 if (masks & UCDATA_KDECOMP)
1366 error |= _uckdcmp_load(paths, 1) < 0 ? UCDATA_KDECOMP : 0;
1378 ac_uint4 i, lo, *dec;
1379 struct ucnumber num;
1381 /* ucdata_setup("."); */
1386 printf("NOT WEAK\n");
1388 printf("LOWER 0x%04lX\n", uctolower(0xff3a));
1389 printf("UPPER 0x%04lX\n", uctoupper(0xff5a));
1391 if (ucisalpha(0x1d5))
1394 printf("NOT ALPHA\n");
1396 if (ucisupper(0x1d5)) {
1398 lo = uctolower(0x1d5);
1399 printf("0x%04lx\n", lo);
1400 lo = uctotitle(0x1d5);
1401 printf("0x%04lx\n", lo);
1403 printf("NOT UPPER\n");
1405 if (ucistitle(0x1d5))
1408 printf("NOT TITLE\n");
1410 if (uciscomposite(0x1d5))
1411 printf("COMPOSITE\n");
1413 printf("NOT COMPOSITE\n");
1415 if (ucdecomp(0x1d5, &lo, &dec)) {
1416 for (i = 0; i < lo; i++)
1417 printf("0x%04lx ", dec[i]);
1421 if ((lo = uccombining_class(0x41)) != 0)
1422 printf("0x41 CCL %ld\n", lo);
1424 if (ucisxdigit(0xfeff))
1425 printf("0xFEFF HEX DIGIT\n");
1427 printf("0xFEFF NOT HEX DIGIT\n");
1429 if (ucisdefined(0x10000))
1430 printf("0x10000 DEFINED\n");
1432 printf("0x10000 NOT DEFINED\n");
1434 if (ucnumber_lookup(0x30, &num)) {
1435 if (num.denominator != 1)
1436 printf("UCNUMBER: 0x30 = %d/%d\n", num.numerator, num.denominator);
1438 printf("UCNUMBER: 0x30 = %d\n", num.numerator);
1440 printf("UCNUMBER: 0x30 NOT A NUMBER\n");
1442 if (ucnumber_lookup(0xbc, &num)) {
1443 if (num.denominator != 1)
1444 printf("UCNUMBER: 0xbc = %d/%d\n", num.numerator, num.denominator);
1446 printf("UCNUMBER: 0xbc = %d\n", num.numerator);
1448 printf("UCNUMBER: 0xbc NOT A NUMBER\n");
1451 if (ucnumber_lookup(0xff19, &num)) {
1452 if (num.denominator != 1)
1453 printf("UCNUMBER: 0xff19 = %d/%d\n", num.numerator, num.denominator);
1455 printf("UCNUMBER: 0xff19 = %d\n", num.numerator);
1457 printf("UCNUMBER: 0xff19 NOT A NUMBER\n");
1459 if (ucnumber_lookup(0x4e00, &num)) {
1460 if (num.denominator != 1)
1461 printf("UCNUMBER: 0x4e00 = %d/%d\n", num.numerator, num.denominator);
1463 printf("UCNUMBER: 0x4e00 = %d\n", num.numerator);
1465 printf("UCNUMBER: 0x4e00 NOT A NUMBER\n");
1467 if (ucdigit_lookup(0x06f9, &dig))
1468 printf("UCDIGIT: 0x6f9 = %d\n", dig);
1470 printf("UCDIGIT: 0x6f9 NOT A NUMBER\n");
1472 dig = ucgetdigit(0x0969);
1473 printf("UCGETDIGIT: 0x969 = %d\n", dig);
1475 num = ucgetnumber(0x30);
1476 if (num.denominator != 1)
1477 printf("UCGETNUMBER: 0x30 = %d/%d\n", num.numerator, num.denominator);
1479 printf("UCGETNUMBER: 0x30 = %d\n", num.numerator);
1481 num = ucgetnumber(0xbc);
1482 if (num.denominator != 1)
1483 printf("UCGETNUMBER: 0xbc = %d/%d\n", num.numerator, num.denominator);
1485 printf("UCGETNUMBER: 0xbc = %d\n", num.numerator);
1487 num = ucgetnumber(0xff19);
1488 if (num.denominator != 1)
1489 printf("UCGETNUMBER: 0xff19 = %d/%d\n", num.numerator, num.denominator);
1491 printf("UCGETNUMBER: 0xff19 = %d\n", num.numerator);
1493 /* ucdata_cleanup(); */