]> git.sur5r.net Git - openldap/blob - libraries/liblunicode/ucdata/ucdata.c
Fix ldaps / TLS processing...
[openldap] / libraries / liblunicode / ucdata / ucdata.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 1999 Computing Research Labs, New Mexico State University
8  *
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:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
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.
26  */
27 #ifndef lint
28 #ifdef __GNUC__
29 static char rcsid[] __attribute__ ((unused)) = "$Id: ucdata.c,v 1.3 1999/08/23 16:14:09 mleisher Exp $";
30 #else
31 static char rcsid[] = "$Id: ucdata.c,v 1.3 1999/08/23 16:14:09 mleisher Exp $";
32 #endif
33 #endif
34
35 #include "portable.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <ac/string.h>
40 #include <ac/unistd.h>
41
42
43 #include "ucdata.h"
44
45 /**************************************************************************
46  *
47  * Miscellaneous types, data, and support functions.
48  *
49  **************************************************************************/
50
51 typedef struct {
52     unsigned short bom;
53     unsigned short cnt;
54     union {
55         unsigned long bytes;
56         unsigned short len[2];
57     } size;
58 } _ucheader_t;
59
60 /*
61  * A simple array of 32-bit masks for lookup.
62  */
63 static unsigned long masks32[32] = {
64     0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010, 0x00000020,
65     0x00000040, 0x00000080, 0x00000100, 0x00000200, 0x00000400, 0x00000800,
66     0x00001000, 0x00002000, 0x00004000, 0x00008000, 0x00010000, 0x00020000,
67     0x00040000, 0x00080000, 0x00100000, 0x00200000, 0x00400000, 0x00800000,
68     0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000,
69     0x40000000, 0x80000000
70 };
71
72 #define endian_short(cc) (((cc) >> 8) | (((cc) & 0xff) << 8))
73 #define endian_long(cc) ((((cc) & 0xff) << 24)|((((cc) >> 8) & 0xff) << 16)|\
74                         ((((cc) >> 16) & 0xff) << 8)|((cc) >> 24))
75
76 static FILE *
77 _ucopenfile(char *paths, char *filename, char *mode)
78 {
79     FILE *f;
80     char *fp, *dp, *pp, path[BUFSIZ];
81
82     if (filename == 0 || *filename == 0)
83       return 0;
84
85     dp = paths;
86     while (dp && *dp) {
87         pp = path;
88         while (*dp && *dp != ':')
89           *pp++ = *dp++;
90         *pp++ = '/';
91
92         fp = filename;
93         while (*fp)
94           *pp++ = *fp++;
95         *pp = 0;
96
97         if ((f = fopen(path, mode)) != 0)
98           return f;
99
100         if (*dp == ':')
101           dp++;
102     }
103
104     return 0;
105 }
106
107 /**************************************************************************
108  *
109  * Support for the character properties.
110  *
111  **************************************************************************/
112
113 static unsigned long  _ucprop_size;
114 static unsigned short *_ucprop_offsets;
115 static unsigned long  *_ucprop_ranges;
116
117 static void
118 _ucprop_load(char *paths, int reload)
119 {
120     FILE *in;
121     unsigned long size, i;
122     _ucheader_t hdr;
123
124     if (_ucprop_size > 0) {
125         if (!reload)
126           /*
127            * The character properties have already been loaded.
128            */
129           return;
130
131         /*
132          * Unload the current character property data in preparation for
133          * loading a new copy.  Only the first array has to be deallocated
134          * because all the memory for the arrays is allocated as a single
135          * block.
136          */
137         free((char *) _ucprop_offsets);
138         _ucprop_size = 0;
139     }
140
141     if ((in = _ucopenfile(paths, "ctype.dat", "rb")) == 0)
142       return;
143
144     /*
145      * Load the header.
146      */
147     fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
148
149     if (hdr.bom == 0xfffe) {
150         hdr.cnt = endian_short(hdr.cnt);
151         hdr.size.bytes = endian_long(hdr.size.bytes);
152     }
153
154     if ((_ucprop_size = hdr.cnt) == 0) {
155         fclose(in);
156         return;
157     }
158
159     /*
160      * Allocate all the storage needed for the lookup table.
161      */
162     _ucprop_offsets = (unsigned short *) malloc(hdr.size.bytes);
163
164     /*
165      * Calculate the offset into the storage for the ranges.  The offsets
166      * array is on a 4-byte boundary and one larger than the value provided in
167      * the header count field.  This means the offset to the ranges must be
168      * calculated after aligning the count to a 4-byte boundary.
169      */
170     if ((size = ((hdr.cnt + 1) * sizeof(unsigned short))) & 3)
171       size += 4 - (size & 3);
172     size >>= 1;
173     _ucprop_ranges = (unsigned long *) (_ucprop_offsets + size);
174
175     /*
176      * Load the offset array.
177      */
178     fread((char *) _ucprop_offsets, sizeof(unsigned short), size, in);
179
180     /*
181      * Do an endian swap if necessary.  Don't forget there is an extra node on
182      * the end with the final index.
183      */
184     if (hdr.bom == 0xfffe) {
185         for (i = 0; i <= _ucprop_size; i++)
186           _ucprop_offsets[i] = endian_short(_ucprop_offsets[i]);
187     }
188
189     /*
190      * Load the ranges.  The number of elements is in the last array position
191      * of the offsets.
192      */
193     fread((char *) _ucprop_ranges, sizeof(unsigned long),
194           _ucprop_offsets[_ucprop_size], in);
195
196     fclose(in);
197
198     /*
199      * Do an endian swap if necessary.
200      */
201     if (hdr.bom == 0xfffe) {
202         for (i = 0; i < _ucprop_offsets[_ucprop_size]; i++)
203           _ucprop_ranges[i] = endian_long(_ucprop_ranges[i]);
204     }
205 }
206
207 static void
208 _ucprop_unload(void)
209 {
210     if (_ucprop_size == 0)
211       return;
212
213     /*
214      * Only need to free the offsets because the memory is allocated as a
215      * single block.
216      */
217     free((char *) _ucprop_offsets);
218     _ucprop_size = 0;
219 }
220
221 static int
222 _ucprop_lookup(unsigned long code, unsigned long n)
223 {
224     long l, r, m;
225
226     /*
227      * There is an extra node on the end of the offsets to allow this routine
228      * to work right.  If the index is 0xffff, then there are no nodes for the
229      * property.
230      */
231     if ((l = _ucprop_offsets[n]) == 0xffff)
232       return 0;
233
234     /*
235      * Locate the next offset that is not 0xffff.  The sentinel at the end of
236      * the array is the max index value.
237      */
238     for (m = 1;
239          n + m < _ucprop_size && _ucprop_offsets[n + m] == 0xffff; m++) ;
240
241     r = _ucprop_offsets[n + m] - 1;
242
243     while (l <= r) {
244         /*
245          * Determine a "mid" point and adjust to make sure the mid point is at
246          * the beginning of a range pair.
247          */
248         m = (l + r) >> 1;
249         m -= (m & 1);
250         if (code > _ucprop_ranges[m + 1])
251           l = m + 2;
252         else if (code < _ucprop_ranges[m])
253           r = m - 2;
254         else if (code >= _ucprop_ranges[m] && code <= _ucprop_ranges[m + 1])
255           return 1;
256     }
257     return 0;
258 }
259
260 int
261 ucisprop(unsigned long code, unsigned long mask1, unsigned long mask2)
262 {
263     unsigned long i;
264
265     if (mask1 == 0 && mask2 == 0)
266       return 0;
267
268     for (i = 0; mask1 && i < 32; i++) {
269         if ((mask1 & masks32[i]) && _ucprop_lookup(code, i))
270           return 1;
271     }
272
273     for (i = 32; mask2 && i < _ucprop_size; i++) {
274         if ((mask2 & masks32[i & 31]) && _ucprop_lookup(code, i))
275           return 1;
276     }
277
278     return 0;
279 }
280
281 /**************************************************************************
282  *
283  * Support for case mapping.
284  *
285  **************************************************************************/
286
287 static unsigned long _uccase_size;
288 static unsigned short _uccase_len[2];
289 static unsigned long *_uccase_map;
290
291 static void
292 _uccase_load(char *paths, int reload)
293 {
294     FILE *in;
295     unsigned long i;
296     _ucheader_t hdr;
297
298     if (_uccase_size > 0) {
299         if (!reload)
300           /*
301            * The case mappings have already been loaded.
302            */
303           return;
304
305         free((char *) _uccase_map);
306         _uccase_size = 0;
307     }
308
309     if ((in = _ucopenfile(paths, "case.dat", "rb")) == 0)
310       return;
311
312     /*
313      * Load the header.
314      */
315     fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
316
317     if (hdr.bom == 0xfffe) {
318         hdr.cnt = endian_short(hdr.cnt);
319         hdr.size.len[0] = endian_short(hdr.size.len[0]);
320         hdr.size.len[1] = endian_short(hdr.size.len[1]);
321     }
322
323     /*
324      * Set the node count and lengths of the upper and lower case mapping
325      * tables.
326      */
327     _uccase_size = hdr.cnt * 3;
328     _uccase_len[0] = hdr.size.len[0] * 3;
329     _uccase_len[1] = hdr.size.len[1] * 3;
330
331     _uccase_map = (unsigned long *)
332         malloc(_uccase_size * sizeof(unsigned long));
333
334     /*
335      * Load the case mapping table.
336      */
337     fread((char *) _uccase_map, sizeof(unsigned long), _uccase_size, in);
338
339     /*
340      * Do an endian swap if necessary.
341      */
342     if (hdr.bom == 0xfffe) {
343         for (i = 0; i < _uccase_size; i++)
344           _uccase_map[i] = endian_long(_uccase_map[i]);
345     }
346 }
347
348 static void
349 _uccase_unload(void)
350 {
351     if (_uccase_size == 0)
352       return;
353
354     free((char *) _uccase_map);
355     _uccase_size = 0;
356 }
357
358 static unsigned long
359 _uccase_lookup(unsigned long code, long l, long r, int field)
360 {
361     long m;
362
363     /*
364      * Do the binary search.
365      */
366     while (l <= r) {
367         /*
368          * Determine a "mid" point and adjust to make sure the mid point is at
369          * the beginning of a case mapping triple.
370          */
371         m = (l + r) >> 1;
372         m -= (m % 3);
373         if (code > _uccase_map[m])
374           l = m + 3;
375         else if (code < _uccase_map[m])
376           r = m - 3;
377         else if (code == _uccase_map[m])
378           return _uccase_map[m + field];
379     }
380
381     return code;
382 }
383
384 unsigned long
385 uctoupper(unsigned long code)
386 {
387     int field;
388     long l, r;
389
390     if (ucisupper(code))
391       return code;
392
393     if (ucislower(code)) {
394         /*
395          * The character is lower case.
396          */
397         field = 2;
398         l = _uccase_len[0];
399         r = (l + _uccase_len[1]) - 3;
400     } else {
401         /*
402          * The character is title case.
403          */
404         field = 1;
405         l = _uccase_len[0] + _uccase_len[1];
406         r = _uccase_size - 3;
407     }
408     return _uccase_lookup(code, l, r, field);
409 }
410
411 unsigned long
412 uctolower(unsigned long code)
413 {
414     int field;
415     long l, r;
416
417     if (ucislower(code))
418       return code;
419
420     if (ucisupper(code)) {
421         /*
422          * The character is upper case.
423          */
424         field = 1;
425         l = 0;
426         r = _uccase_len[0] - 3;
427     } else {
428         /*
429          * The character is title case.
430          */
431         field = 2;
432         l = _uccase_len[0] + _uccase_len[1];
433         r = _uccase_size - 3;
434     }
435     return _uccase_lookup(code, l, r, field);
436 }
437
438 unsigned long
439 uctotitle(unsigned long code)
440 {
441     int field;
442     long l, r;
443
444     if (ucistitle(code))
445       return code;
446
447     /*
448      * The offset will always be the same for converting to title case.
449      */
450     field = 2;
451
452     if (ucisupper(code)) {
453         /*
454          * The character is upper case.
455          */
456         l = 0;
457         r = _uccase_len[0] - 3;
458     } else {
459         /*
460          * The character is lower case.
461          */
462         l = _uccase_len[0];
463         r = (l + _uccase_len[1]) - 3;
464     }
465     return _uccase_lookup(code, l, r, field);
466 }
467
468 /**************************************************************************
469  *
470  * Support for decompositions.
471  *
472  **************************************************************************/
473
474 static unsigned long  _ucdcmp_size;
475 static unsigned long *_ucdcmp_nodes;
476 static unsigned long *_ucdcmp_decomp;
477
478 static void
479 _ucdcmp_load(char *paths, int reload)
480 {
481     FILE *in;
482     unsigned long size, i;
483     _ucheader_t hdr;
484
485     if (_ucdcmp_size > 0) {
486         if (!reload)
487           /*
488            * The decompositions have already been loaded.
489            */
490           return;
491
492         free((char *) _ucdcmp_nodes);
493         _ucdcmp_size = 0;
494     }
495
496     if ((in = _ucopenfile(paths, "decomp.dat", "rb")) == 0)
497       return;
498
499     /*
500      * Load the header.
501      */
502     fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
503
504     if (hdr.bom == 0xfffe) {
505         hdr.cnt = endian_short(hdr.cnt);
506         hdr.size.bytes = endian_long(hdr.size.bytes);
507     }
508
509     _ucdcmp_size = hdr.cnt << 1;
510     _ucdcmp_nodes = (unsigned long *) malloc(hdr.size.bytes);
511     _ucdcmp_decomp = _ucdcmp_nodes + (_ucdcmp_size + 1);
512
513     /*
514      * Read the decomposition data in.
515      */
516     size = hdr.size.bytes / sizeof(unsigned long);
517     fread((char *) _ucdcmp_nodes, sizeof(unsigned long), size, in);
518
519     /*
520      * Do an endian swap if necessary.
521      */
522     if (hdr.bom == 0xfffe) {
523         for (i = 0; i < size; i++)
524           _ucdcmp_nodes[i] = endian_long(_ucdcmp_nodes[i]);
525     }        
526 }
527
528 static void
529 _ucdcmp_unload(void)
530 {
531     if (_ucdcmp_size == 0)
532       return;
533
534     /*
535      * Only need to free the offsets because the memory is allocated as a
536      * single block.
537      */
538     free((char *) _ucdcmp_nodes);
539     _ucdcmp_size = 0;
540 }
541
542 int
543 ucdecomp(unsigned long code, unsigned long *num, unsigned long **decomp)
544 {
545     long l, r, m;
546
547     l = 0;
548     r = _ucdcmp_nodes[_ucdcmp_size] - 1;
549
550     while (l <= r) {
551         /*
552          * Determine a "mid" point and adjust to make sure the mid point is at
553          * the beginning of a code+offset pair.
554          */
555         m = (l + r) >> 1;
556         m -= (m & 1);
557         if (code > _ucdcmp_nodes[m])
558           l = m + 2;
559         else if (code < _ucdcmp_nodes[m])
560           r = m - 2;
561         else if (code == _ucdcmp_nodes[m]) {
562             *num = _ucdcmp_nodes[m + 3] - _ucdcmp_nodes[m + 1];
563             *decomp = &_ucdcmp_decomp[_ucdcmp_nodes[m + 1]];
564             return 1;
565         }
566     }
567     return 0;
568 }
569
570 int
571 ucdecomp_hangul(unsigned long code, unsigned long *num, unsigned long decomp[])
572 {
573     if (!ucishangul(code))
574       return 0;
575
576     code -= 0xac00;
577     decomp[0] = 0x1100 + (unsigned long) (code / 588);
578     decomp[1] = 0x1161 + (unsigned long) ((code % 588) / 28);
579     decomp[2] = 0x11a7 + (unsigned long) (code % 28);
580     *num = (decomp[2] != 0x11a7) ? 3 : 2;
581
582     return 1;
583 }
584
585 /**************************************************************************
586  *
587  * Support for combining classes.
588  *
589  **************************************************************************/
590
591 static unsigned long  _uccmcl_size;
592 static unsigned long *_uccmcl_nodes;
593
594 static void
595 _uccmcl_load(char *paths, int reload)
596 {
597     FILE *in;
598     unsigned long i;
599     _ucheader_t hdr;
600
601     if (_uccmcl_size > 0) {
602         if (!reload)
603           /*
604            * The combining classes have already been loaded.
605            */
606           return;
607
608         free((char *) _uccmcl_nodes);
609         _uccmcl_size = 0;
610     }
611
612     if ((in = _ucopenfile(paths, "cmbcl.dat", "rb")) == 0)
613       return;
614
615     /*
616      * Load the header.
617      */
618     fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
619
620     if (hdr.bom == 0xfffe) {
621         hdr.cnt = endian_short(hdr.cnt);
622         hdr.size.bytes = endian_long(hdr.size.bytes);
623     }
624
625     _uccmcl_size = hdr.cnt * 3;
626     _uccmcl_nodes = (unsigned long *) malloc(hdr.size.bytes);
627
628     /*
629      * Read the combining classes in.
630      */
631     fread((char *) _uccmcl_nodes, sizeof(unsigned long), _uccmcl_size, in);
632
633     /*
634      * Do an endian swap if necessary.
635      */
636     if (hdr.bom == 0xfffe) {
637         for (i = 0; i < _uccmcl_size; i++)
638           _uccmcl_nodes[i] = endian_long(_uccmcl_nodes[i]);
639     }        
640 }
641
642 static void
643 _uccmcl_unload(void)
644 {
645     if (_uccmcl_size == 0)
646       return;
647
648     free((char *) _uccmcl_nodes);
649     _uccmcl_size = 0;
650 }
651
652 unsigned long
653 uccombining_class(unsigned long code)
654 {
655     long l, r, m;
656
657     l = 0;
658     r = _uccmcl_size - 1;
659
660     while (l <= r) {
661         m = (l + r) >> 1;
662         m -= (m % 3);
663         if (code > _uccmcl_nodes[m + 1])
664           l = m + 3;
665         else if (code < _uccmcl_nodes[m])
666           r = m - 3;
667         else if (code >= _uccmcl_nodes[m] && code <= _uccmcl_nodes[m + 1])
668           return _uccmcl_nodes[m + 2];
669     }
670     return 0;
671 }
672
673 /**************************************************************************
674  *
675  * Support for numeric values.
676  *
677  **************************************************************************/
678
679 static unsigned long *_ucnum_nodes;
680 static unsigned long _ucnum_size;
681 static short *_ucnum_vals;
682
683 void
684 _ucnumb_load(char *paths, int reload)
685 {
686     FILE *in;
687     unsigned long size, i;
688     _ucheader_t hdr;
689
690     if (_ucnum_size > 0) {
691         if (!reload)
692           /*
693            * The numbers have already been loaded.
694            */
695           return;
696
697         free((char *) _ucnum_nodes);
698         _ucnum_size = 0;
699     }
700
701     if ((in = _ucopenfile(paths, "num.dat", "rb")) == 0)
702       return;
703
704     /*
705      * Load the header.
706      */
707     fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
708
709     if (hdr.bom == 0xfffe) {
710         hdr.cnt = endian_short(hdr.cnt);
711         hdr.size.bytes = endian_long(hdr.size.bytes);
712     }
713
714     _ucnum_size = hdr.cnt;
715     _ucnum_nodes = (unsigned long *) malloc(hdr.size.bytes);
716     _ucnum_vals = (short *) (_ucnum_nodes + _ucnum_size);
717
718     /*
719      * Read the combining classes in.
720      */
721     fread((char *) _ucnum_nodes, sizeof(unsigned char), hdr.size.bytes, in);
722
723     /*
724      * Do an endian swap if necessary.
725      */
726     if (hdr.bom == 0xfffe) {
727         for (i = 0; i < _ucnum_size; i++)
728           _ucnum_nodes[i] = endian_long(_ucnum_nodes[i]);
729
730         /*
731          * Determine the number of values that have to be adjusted.
732          */
733         size = (hdr.size.bytes -
734                 (_ucnum_size * (sizeof(unsigned long) << 1))) /
735             sizeof(short);
736
737         for (i = 0; i < size; i++)
738           _ucnum_vals[i] = endian_short(_ucnum_vals[i]);
739     }        
740 }
741
742 static void
743 _ucnumb_unload(void)
744 {
745     if (_ucnum_size == 0)
746       return;
747
748     free((char *) _ucnum_nodes);
749     _ucnum_size = 0;
750 }
751
752 int
753 ucnumber_lookup(unsigned long code, struct ucnumber *num)
754 {
755     long l, r, m;
756     short *vp;
757
758     l = 0;
759     r = _ucnum_size - 1;
760     while (l <= r) {
761         /*
762          * Determine a "mid" point and adjust to make sure the mid point is at
763          * the beginning of a code+offset pair.
764          */
765         m = (l + r) >> 1;
766         m -= (m & 1);
767         if (code > _ucnum_nodes[m])
768           l = m + 2;
769         else if (code < _ucnum_nodes[m])
770           r = m - 2;
771         else {
772             vp = _ucnum_vals + _ucnum_nodes[m + 1];
773             num->numerator = (int) *vp++;
774             num->denominator = (int) *vp;
775             return 1;
776         }
777     }
778     return 0;
779 }
780
781 int
782 ucdigit_lookup(unsigned long code, int *digit)
783 {
784     long l, r, m;
785     short *vp;
786
787     l = 0;
788     r = _ucnum_size - 1;
789     while (l <= r) {
790         /*
791          * Determine a "mid" point and adjust to make sure the mid point is at
792          * the beginning of a code+offset pair.
793          */
794         m = (l + r) >> 1;
795         m -= (m & 1);
796         if (code > _ucnum_nodes[m])
797           l = m + 2;
798         else if (code < _ucnum_nodes[m])
799           r = m - 2;
800         else {
801             vp = _ucnum_vals + _ucnum_nodes[m + 1];
802             if (*vp == *(vp + 1)) {
803               *digit = *vp;
804               return 1;
805             }
806             return 0;
807         }
808     }
809     return 0;
810 }
811
812 struct ucnumber
813 ucgetnumber(unsigned long code)
814 {
815     struct ucnumber num;
816
817     /*
818      * Initialize with some arbitrary value, because the caller simply cannot
819      * tell for sure if the code is a number without calling the ucisnumber()
820      * macro before calling this function.
821      */
822     num.numerator = num.denominator = -111;
823
824     (void) ucnumber_lookup(code, &num);
825
826     return num;
827 }
828
829 int
830 ucgetdigit(unsigned long code)
831 {
832     int dig;
833
834     /*
835      * Initialize with some arbitrary value, because the caller simply cannot
836      * tell for sure if the code is a number without calling the ucisdigit()
837      * macro before calling this function.
838      */
839     dig = -111;
840
841     (void) ucdigit_lookup(code, &dig);
842
843     return dig;
844 }
845
846 /**************************************************************************
847  *
848  * Setup and cleanup routines.
849  *
850  **************************************************************************/
851
852 void
853 ucdata_load(char *paths, int masks)
854 {
855     if (masks & UCDATA_CTYPE)
856       _ucprop_load(paths, 0);
857     if (masks & UCDATA_CASE)
858       _uccase_load(paths, 0);
859     if (masks & UCDATA_DECOMP)
860       _ucdcmp_load(paths, 0);
861     if (masks & UCDATA_CMBCL)
862       _uccmcl_load(paths, 0);
863     if (masks & UCDATA_NUM)
864       _ucnumb_load(paths, 0);
865 }
866
867 void
868 ucdata_unload(int masks)
869 {
870     if (masks & UCDATA_CTYPE)
871       _ucprop_unload();
872     if (masks & UCDATA_CASE)
873       _uccase_unload();
874     if (masks & UCDATA_DECOMP)
875       _ucdcmp_unload();
876     if (masks & UCDATA_CMBCL)
877       _uccmcl_unload();
878     if (masks & UCDATA_NUM)
879       _ucnumb_unload();
880 }
881
882 void
883 ucdata_reload(char *paths, int masks)
884 {
885     if (masks & UCDATA_CTYPE)
886       _ucprop_load(paths, 1);
887     if (masks & UCDATA_CASE)
888       _uccase_load(paths, 1);
889     if (masks & UCDATA_DECOMP)
890       _ucdcmp_load(paths, 1);
891     if (masks & UCDATA_CMBCL)
892       _uccmcl_load(paths, 1);
893     if (masks & UCDATA_NUM)
894       _ucnumb_load(paths, 1);
895 }
896
897 #ifdef TEST
898
899 void
900 main(void)
901 {
902     int dig;
903     unsigned long i, lo, *dec;
904     struct ucnumber num;
905
906     ucdata_setup(".");
907
908     if (ucisweak(0x30))
909       printf("WEAK\n");
910     else
911       printf("NOT WEAK\n");
912
913     printf("LOWER 0x%04lX\n", uctolower(0xff3a));
914     printf("UPPER 0x%04lX\n", uctoupper(0xff5a));
915
916     if (ucisalpha(0x1d5))
917       printf("ALPHA\n");
918     else
919       printf("NOT ALPHA\n");
920
921     if (ucisupper(0x1d5)) {
922         printf("UPPER\n");
923         lo = uctolower(0x1d5);
924         printf("0x%04lx\n", lo);
925         lo = uctotitle(0x1d5);
926         printf("0x%04lx\n", lo);
927     } else
928       printf("NOT UPPER\n");
929
930     if (ucistitle(0x1d5))
931       printf("TITLE\n");
932     else
933       printf("NOT TITLE\n");
934
935     if (uciscomposite(0x1d5))
936       printf("COMPOSITE\n");
937     else
938       printf("NOT COMPOSITE\n");
939
940     if (ucdecomp(0x1d5, &lo, &dec)) {
941         for (i = 0; i < lo; i++)
942           printf("0x%04lx ", dec[i]);
943         putchar('\n');
944     }
945
946     if ((lo = uccombining_class(0x41)) != 0)
947       printf("0x41 CCL %ld\n", lo);
948
949     if (ucisxdigit(0xfeff))
950       printf("0xFEFF HEX DIGIT\n");
951     else
952       printf("0xFEFF NOT HEX DIGIT\n");
953
954     if (ucisdefined(0x10000))
955       printf("0x10000 DEFINED\n");
956     else
957       printf("0x10000 NOT DEFINED\n");
958
959     if (ucnumber_lookup(0x30, &num)) {
960         if (num.numerator != num.denominator)
961           printf("UCNUMBER: 0x30 = %d/%d\n", num.numerator, num.denominator);
962         else
963           printf("UCNUMBER: 0x30 = %d\n", num.numerator);
964     } else
965       printf("UCNUMBER: 0x30 NOT A NUMBER\n");
966
967     if (ucnumber_lookup(0xbc, &num)) {
968         if (num.numerator != num.denominator)
969           printf("UCNUMBER: 0xbc = %d/%d\n", num.numerator, num.denominator);
970         else
971           printf("UCNUMBER: 0xbc = %d\n", num.numerator);
972     } else
973       printf("UCNUMBER: 0xbc NOT A NUMBER\n");
974
975
976     if (ucnumber_lookup(0xff19, &num)) {
977         if (num.numerator != num.denominator)
978           printf("UCNUMBER: 0xff19 = %d/%d\n", num.numerator, num.denominator);
979         else
980           printf("UCNUMBER: 0xff19 = %d\n", num.numerator);
981     } else
982       printf("UCNUMBER: 0xff19 NOT A NUMBER\n");
983
984     if (ucnumber_lookup(0x4e00, &num)) {
985         if (num.numerator != num.denominator)
986           printf("UCNUMBER: 0x4e00 = %d/%d\n", num.numerator, num.denominator);
987         else
988           printf("UCNUMBER: 0x4e00 = %d\n", num.numerator);
989     } else
990       printf("UCNUMBER: 0x4e00 NOT A NUMBER\n");
991
992     if (ucdigit_lookup(0x06f9, &dig))
993       printf("UCDIGIT: 0x6f9 = %d\n", dig);
994     else
995       printf("UCDIGIT: 0x6f9 NOT A NUMBER\n");
996
997     dig = ucgetdigit(0x0969);
998     printf("UCGETDIGIT: 0x969 = %d\n", dig);
999
1000     num = ucgetnumber(0x30);
1001     if (num.numerator != num.denominator)
1002       printf("UCGETNUMBER: 0x30 = %d/%d\n", num.numerator, num.denominator);
1003     else
1004       printf("UCGETNUMBER: 0x30 = %d\n", num.numerator);
1005
1006     num = ucgetnumber(0xbc);
1007     if (num.numerator != num.denominator)
1008       printf("UCGETNUMBER: 0xbc = %d/%d\n", num.numerator, num.denominator);
1009     else
1010       printf("UCGETNUMBER: 0xbc = %d\n", num.numerator);
1011
1012     num = ucgetnumber(0xff19);
1013     if (num.numerator != num.denominator)
1014       printf("UCGETNUMBER: 0xff19 = %d/%d\n", num.numerator, num.denominator);
1015     else
1016       printf("UCGETNUMBER: 0xff19 = %d\n", num.numerator);
1017
1018     ucdata_cleanup();
1019     exit(0);
1020 }
1021
1022 #endif /* TEST */