forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathucgendat.c
1985 lines (1715 loc) · 49.4 KB
/
ucgendat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Further modified for PHP */
/* $Id$ */
/* $OpenLDAP: pkg/ldap/libraries/liblunicode/ucdata/ucgendat.c,v 1.36.2.4 2007/01/02 21:43:51 kurt Exp $ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 1998-2007 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available at
* <http://www.OpenLDAP.org/license.html>.
*/
/* Copyright 2001 Computing Research Labs, New Mexico State University
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* orig Id: ucgendat.c,v 1.4 2001/01/02 18:46:20 mleisher Exp $" */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ac_uint2 unsigned short
#define ac_uint4 unsigned int
#define LDAP_DIRSEP "/"
#define AC_MEMCPY memcpy
#ifndef HARDCODE_DATA
#define HARDCODE_DATA 1
#endif
#undef ishdigit
#define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\
((cc) >= 'A' && (cc) <= 'F') ||\
((cc) >= 'a' && (cc) <= 'f'))
/*
* A header written to the output file with the byte-order-mark and the number
* of property nodes.
*/
static ac_uint2 hdr[2] = {0xfeff, 0};
#define NUMPROPS 50
#define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3)))
typedef struct {
char *name;
int len;
} _prop_t;
/*
* List of properties expected to be found in the Unicode Character Database
* including some implementation specific properties.
*
* The implementation specific properties are:
* Cm = Composed (can be decomposed)
* Nb = Non-breaking
* Sy = Symmetric (has left and right forms)
* Hd = Hex digit
* Qm = Quote marks
* Mr = Mirroring
* Ss = Space, other
* Cp = Defined character
*/
static _prop_t props[NUMPROPS] = {
{"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2},
{"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2},
{"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2},
{"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2},
{"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L", 1}, {"R", 1},
{"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B", 1},
{"S", 1}, {"WS", 2}, {"ON", 2},
{"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2},
{"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2}, {"AL", 2}
};
typedef struct {
ac_uint4 *ranges;
ac_uint2 used;
ac_uint2 size;
} _ranges_t;
static _ranges_t proptbl[NUMPROPS];
/*
* Make sure this array is sized to be on a 4-byte boundary at compile time.
*/
static ac_uint2 propcnt[NEEDPROPS];
/*
* Array used to collect a decomposition before adding it to the decomposition
* table.
*/
static ac_uint4 dectmp[64];
static ac_uint4 dectmp_size;
typedef struct {
ac_uint4 code;
ac_uint2 size;
ac_uint2 used;
ac_uint4 *decomp;
} _decomp_t;
/*
* List of decomposition. Created and expanded in order as the characters are
* encountered. First list contains canonical mappings, second also includes
* compatibility mappings.
*/
static _decomp_t *decomps;
static ac_uint4 decomps_used;
static ac_uint4 decomps_size;
static _decomp_t *kdecomps;
static ac_uint4 kdecomps_used;
static ac_uint4 kdecomps_size;
/*
* Composition exclusion table stuff.
*/
#define COMPEX_SET(c) (compexs[(c) >> 5] |= (1 << ((c) & 31)))
#define COMPEX_TEST(c) (compexs[(c) >> 5] & (1 << ((c) & 31)))
static ac_uint4 compexs[8192];
/*
* Struct for holding a composition pair, and array of composition pairs
*/
typedef struct {
ac_uint4 comp;
ac_uint4 count;
ac_uint4 code1;
ac_uint4 code2;
} _comp_t;
#if 0
static _comp_t *comps;
#endif
static ac_uint4 comps_used;
/*
* Types and lists for handling lists of case mappings.
*/
typedef struct {
ac_uint4 key;
ac_uint4 other1;
ac_uint4 other2;
} _case_t;
static _case_t *upper;
static _case_t *lower;
static _case_t *title;
static ac_uint4 upper_used;
static ac_uint4 upper_size;
static ac_uint4 lower_used;
static ac_uint4 lower_size;
static ac_uint4 title_used;
static ac_uint4 title_size;
/*
* Array used to collect case mappings before adding them to a list.
*/
static ac_uint4 cases[3];
/*
* An array to hold ranges for combining classes.
*/
static ac_uint4 *ccl;
static ac_uint4 ccl_used;
static ac_uint4 ccl_size;
/*
* Structures for handling numbers.
*/
typedef struct {
ac_uint4 code;
ac_uint4 idx;
} _codeidx_t;
typedef struct {
short numerator;
short denominator;
} _num_t;
/*
* Arrays to hold the mapping of codes to numbers.
*/
static _codeidx_t *ncodes;
static ac_uint4 ncodes_used;
static ac_uint4 ncodes_size;
static _num_t *nums;
static ac_uint4 nums_used;
static ac_uint4 nums_size;
/*
* Array for holding numbers.
*/
static _num_t *nums;
static ac_uint4 nums_used;
static ac_uint4 nums_size;
static void
add_range(ac_uint4 start, ac_uint4 end, char *p1, char *p2)
{
int i, j, k, len;
_ranges_t *rlp;
char *name;
for (k = 0; k < 2; k++) {
if (k == 0) {
name = p1;
len = 2;
} else {
if (p2 == 0)
break;
name = p2;
len = 1;
}
for (i = 0; i < NUMPROPS; i++) {
if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
break;
}
if (i == NUMPROPS)
continue;
rlp = &proptbl[i];
/*
* Resize the range list if necessary.
*/
if (rlp->used == rlp->size) {
if (rlp->size == 0)
rlp->ranges = (ac_uint4 *)
malloc(sizeof(ac_uint4) << 3);
else
rlp->ranges = (ac_uint4 *)
realloc((char *) rlp->ranges,
sizeof(ac_uint4) * (rlp->size + 8));
rlp->size += 8;
}
/*
* If this is the first code for this property list, just add it
* and return.
*/
if (rlp->used == 0) {
rlp->ranges[0] = start;
rlp->ranges[1] = end;
rlp->used += 2;
continue;
}
/*
* Optimize the case of adding the range to the end.
*/
j = rlp->used - 1;
if (start > rlp->ranges[j]) {
j = rlp->used;
rlp->ranges[j++] = start;
rlp->ranges[j++] = end;
rlp->used = j;
continue;
}
/*
* Need to locate the insertion point.
*/
for (i = 0;
i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
/*
* If the start value lies in the current range, then simply set the
* new end point of the range to the end value passed as a parameter.
*/
if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
rlp->ranges[i + 1] = end;
return;
}
/*
* Shift following values up by two.
*/
for (j = rlp->used; j > i; j -= 2) {
rlp->ranges[j] = rlp->ranges[j - 2];
rlp->ranges[j + 1] = rlp->ranges[j - 1];
}
/*
* Add the new range at the insertion point.
*/
rlp->ranges[i] = start;
rlp->ranges[i + 1] = end;
rlp->used += 2;
}
}
static void
ordered_range_insert(ac_uint4 c, char *name, int len)
{
int i, j;
ac_uint4 s, e;
_ranges_t *rlp;
if (len == 0)
return;
/*
* Deal with directionality codes introduced in Unicode 3.0.
*/
if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
(len == 3 &&
(memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
/*
* Mark all of these as Other Neutral to preserve compatibility with
* older versions.
*/
len = 2;
name = "ON";
}
for (i = 0; i < NUMPROPS; i++) {
if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
break;
}
if (i == NUMPROPS)
return;
/*
* Have a match, so insert the code in order.
*/
rlp = &proptbl[i];
/*
* Resize the range list if necessary.
*/
if (rlp->used == rlp->size) {
if (rlp->size == 0)
rlp->ranges = (ac_uint4 *)
malloc(sizeof(ac_uint4) << 3);
else
rlp->ranges = (ac_uint4 *)
realloc((char *) rlp->ranges,
sizeof(ac_uint4) * (rlp->size + 8));
rlp->size += 8;
}
/*
* If this is the first code for this property list, just add it
* and return.
*/
if (rlp->used == 0) {
rlp->ranges[0] = rlp->ranges[1] = c;
rlp->used += 2;
return;
}
/*
* Optimize the cases of extending the last range and adding new ranges to
* the end.
*/
j = rlp->used - 1;
e = rlp->ranges[j];
s = rlp->ranges[j - 1];
if (c == e + 1) {
/*
* Extend the last range.
*/
rlp->ranges[j] = c;
return;
}
if (c > e + 1) {
/*
* Start another range on the end.
*/
j = rlp->used;
rlp->ranges[j] = rlp->ranges[j + 1] = c;
rlp->used += 2;
return;
}
if (c >= s)
/*
* The code is a duplicate of a code in the last range, so just return.
*/
return;
/*
* The code should be inserted somewhere before the last range in the
* list. Locate the insertion point.
*/
for (i = 0;
i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
s = rlp->ranges[i];
e = rlp->ranges[i + 1];
if (c == e + 1)
/*
* Simply extend the current range.
*/
rlp->ranges[i + 1] = c;
else if (c < s) {
/*
* Add a new entry before the current location. Shift all entries
* before the current one up by one to make room.
*/
for (j = rlp->used; j > i; j -= 2) {
rlp->ranges[j] = rlp->ranges[j - 2];
rlp->ranges[j + 1] = rlp->ranges[j - 1];
}
rlp->ranges[i] = rlp->ranges[i + 1] = c;
rlp->used += 2;
}
}
static void
add_decomp(ac_uint4 code, short compat)
{
ac_uint4 i, j, size;
_decomp_t **pdecomps;
ac_uint4 *pdecomps_used;
ac_uint4 *pdecomps_size;
if (compat) {
pdecomps = &kdecomps;
pdecomps_used = &kdecomps_used;
pdecomps_size = &kdecomps_size;
} else {
pdecomps = &decomps;
pdecomps_used = &decomps_used;
pdecomps_size = &decomps_size;
}
/*
* Add the code to the composite property.
*/
if (!compat) {
ordered_range_insert(code, "Cm", 2);
}
/*
* Locate the insertion point for the code.
*/
for (i = 0; i < *pdecomps_used && code > (*pdecomps)[i].code; i++) ;
/*
* Allocate space for a new decomposition.
*/
if (*pdecomps_used == *pdecomps_size) {
if (*pdecomps_size == 0)
*pdecomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
else
*pdecomps = (_decomp_t *)
realloc((char *) *pdecomps,
sizeof(_decomp_t) * (*pdecomps_size + 8));
(void) memset((char *) (*pdecomps + *pdecomps_size), '\0',
sizeof(_decomp_t) << 3);
*pdecomps_size += 8;
}
if (i < *pdecomps_used && code != (*pdecomps)[i].code) {
/*
* Shift the decomps up by one if the codes don't match.
*/
for (j = *pdecomps_used; j > i; j--)
(void) AC_MEMCPY((char *) &(*pdecomps)[j], (char *) &(*pdecomps)[j - 1],
sizeof(_decomp_t));
}
/*
* Insert or replace a decomposition.
*/
size = dectmp_size + (4 - (dectmp_size & 3));
if ((*pdecomps)[i].size < size) {
if ((*pdecomps)[i].size == 0)
(*pdecomps)[i].decomp = (ac_uint4 *)
malloc(sizeof(ac_uint4) * size);
else
(*pdecomps)[i].decomp = (ac_uint4 *)
realloc((char *) (*pdecomps)[i].decomp,
sizeof(ac_uint4) * size);
(*pdecomps)[i].size = size;
}
if ((*pdecomps)[i].code != code)
(*pdecomps_used)++;
(*pdecomps)[i].code = code;
(*pdecomps)[i].used = dectmp_size;
(void) AC_MEMCPY((char *) (*pdecomps)[i].decomp, (char *) dectmp,
sizeof(ac_uint4) * dectmp_size);
/*
* NOTICE: This needs changing later so it is more general than simply
* pairs. This calculation is done here to simplify allocation elsewhere.
*/
if (!compat && dectmp_size == 2)
comps_used++;
}
static void
add_title(ac_uint4 code)
{
ac_uint4 i, j;
/*
* Always map the code to itself.
*/
cases[2] = code;
if (title_used == title_size) {
if (title_size == 0)
title = (_case_t *) malloc(sizeof(_case_t) << 3);
else
title = (_case_t *) realloc((char *) title,
sizeof(_case_t) * (title_size + 8));
title_size += 8;
}
/*
* Locate the insertion point.
*/
for (i = 0; i < title_used && code > title[i].key; i++) ;
if (i < title_used) {
/*
* Shift the array up by one.
*/
for (j = title_used; j > i; j--)
(void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
sizeof(_case_t));
}
title[i].key = cases[2]; /* Title */
title[i].other1 = cases[0]; /* Upper */
title[i].other2 = cases[1]; /* Lower */
title_used++;
}
static void
add_upper(ac_uint4 code)
{
ac_uint4 i, j;
/*
* Always map the code to itself.
*/
cases[0] = code;
/*
* If the title case character is not present, then make it the same as
* the upper case.
*/
if (cases[2] == 0)
cases[2] = code;
if (upper_used == upper_size) {
if (upper_size == 0)
upper = (_case_t *) malloc(sizeof(_case_t) << 3);
else
upper = (_case_t *) realloc((char *) upper,
sizeof(_case_t) * (upper_size + 8));
upper_size += 8;
}
/*
* Locate the insertion point.
*/
for (i = 0; i < upper_used && code > upper[i].key; i++) ;
if (i < upper_used) {
/*
* Shift the array up by one.
*/
for (j = upper_used; j > i; j--)
(void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
sizeof(_case_t));
}
upper[i].key = cases[0]; /* Upper */
upper[i].other1 = cases[1]; /* Lower */
upper[i].other2 = cases[2]; /* Title */
upper_used++;
}
static void
add_lower(ac_uint4 code)
{
ac_uint4 i, j;
/*
* Always map the code to itself.
*/
cases[1] = code;
/*
* If the title case character is empty, then make it the same as the
* upper case.
*/
if (cases[2] == 0)
cases[2] = cases[0];
if (lower_used == lower_size) {
if (lower_size == 0)
lower = (_case_t *) malloc(sizeof(_case_t) << 3);
else
lower = (_case_t *) realloc((char *) lower,
sizeof(_case_t) * (lower_size + 8));
lower_size += 8;
}
/*
* Locate the insertion point.
*/
for (i = 0; i < lower_used && code > lower[i].key; i++) ;
if (i < lower_used) {
/*
* Shift the array up by one.
*/
for (j = lower_used; j > i; j--)
(void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
sizeof(_case_t));
}
lower[i].key = cases[1]; /* Lower */
lower[i].other1 = cases[0]; /* Upper */
lower[i].other2 = cases[2]; /* Title */
lower_used++;
}
static void
ordered_ccl_insert(ac_uint4 c, ac_uint4 ccl_code)
{
ac_uint4 i, j;
if (ccl_used == ccl_size) {
if (ccl_size == 0)
ccl = (ac_uint4 *) malloc(sizeof(ac_uint4) * 24);
else
ccl = (ac_uint4 *)
realloc((char *) ccl, sizeof(ac_uint4) * (ccl_size + 24));
ccl_size += 24;
}
/*
* Optimize adding the first item.
*/
if (ccl_used == 0) {
ccl[0] = ccl[1] = c;
ccl[2] = ccl_code;
ccl_used += 3;
return;
}
/*
* Handle the special case of extending the range on the end. This
* requires that the combining class codes are the same.
*/
if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
ccl[ccl_used - 2] = c;
return;
}
/*
* Handle the special case of adding another range on the end.
*/
if (c > ccl[ccl_used - 2] + 1 ||
(c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
ccl[ccl_used++] = c;
ccl[ccl_used++] = c;
ccl[ccl_used++] = ccl_code;
return;
}
/*
* Locate either the insertion point or range for the code.
*/
for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
/*
* Extend an existing range.
*/
ccl[i + 1] = c;
return;
} else if (c < ccl[i]) {
/*
* Start a new range before the current location.
*/
for (j = ccl_used; j > i; j -= 3) {
ccl[j] = ccl[j - 3];
ccl[j - 1] = ccl[j - 4];
ccl[j - 2] = ccl[j - 5];
}
ccl[i] = ccl[i + 1] = c;
ccl[i + 2] = ccl_code;
}
}
/*
* Adds a number if it does not already exist and returns an index value
* multiplied by 2.
*/
static ac_uint4
make_number(short num, short denom)
{
ac_uint4 n;
/*
* Determine if the number already exists.
*/
for (n = 0; n < nums_used; n++) {
if (nums[n].numerator == num && nums[n].denominator == denom)
return n << 1;
}
if (nums_used == nums_size) {
if (nums_size == 0)
nums = (_num_t *) malloc(sizeof(_num_t) << 3);
else
nums = (_num_t *) realloc((char *) nums,
sizeof(_num_t) * (nums_size + 8));
nums_size += 8;
}
n = nums_used++;
nums[n].numerator = num;
nums[n].denominator = denom;
return n << 1;
}
static void
add_number(ac_uint4 code, short num, short denom)
{
ac_uint4 i, j;
/*
* Insert the code in order.
*/
for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
/*
* Handle the case of the codes matching and simply replace the number
* that was there before.
*/
if (i < ncodes_used && code == ncodes[i].code) {
ncodes[i].idx = make_number(num, denom);
return;
}
/*
* Resize the array if necessary.
*/
if (ncodes_used == ncodes_size) {
if (ncodes_size == 0)
ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
else
ncodes = (_codeidx_t *)
realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
ncodes_size += 8;
}
/*
* Shift things around to insert the code if necessary.
*/
if (i < ncodes_used) {
for (j = ncodes_used; j > i; j--) {
ncodes[j].code = ncodes[j - 1].code;
ncodes[j].idx = ncodes[j - 1].idx;
}
}
ncodes[i].code = code;
ncodes[i].idx = make_number(num, denom);
ncodes_used++;
}
/*
* This routine assumes that the line is a valid Unicode Character Database
* entry.
*/
static void
read_cdata(FILE *in)
{
ac_uint4 i, lineno, skip, code, ccl_code;
short wnum, neg, number[2], compat;
char line[512], *s, *e;
lineno = skip = 0;
while (fgets(line, sizeof(line), in)) {
if( (s=strchr(line, '\n')) ) *s = '\0';
lineno++;
/*
* Skip blank lines and lines that start with a '#'.
*/
if (line[0] == 0 || line[0] == '#')
continue;
/*
* If lines need to be skipped, do it here.
*/
if (skip) {
skip--;
continue;
}
/*
* Collect the code. The code can be up to 6 hex digits in length to
* allow surrogates to be specified.
*/
for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
code <<= 4;
if (*s >= '0' && *s <= '9')
code += *s - '0';
else if (*s >= 'A' && *s <= 'F')
code += (*s - 'A') + 10;
else if (*s >= 'a' && *s <= 'f')
code += (*s - 'a') + 10;
}
/*
* Handle the following special cases:
* 1. 4E00-9FA5 CJK Ideographs.
* 2. AC00-D7A3 Hangul Syllables.
* 3. D800-DFFF Surrogates.
* 4. E000-F8FF Private Use Area.
* 5. F900-FA2D Han compatibility.
* ...Plus additional ranges in newer Unicode versions...
*/
switch (code) {
case 0x3400:
/* CJK Ideograph Extension A */
add_range(0x3400, 0x4db5, "Lo", "L");
add_range(0x3400, 0x4db5, "Cp", 0);
skip = 1;
break;
case 0x4e00:
/*
* The Han ideographs.
*/
add_range(0x4e00, 0x9fff, "Lo", "L");
/*
* Add the characters to the defined category.
*/
add_range(0x4e00, 0x9fa5, "Cp", 0);
skip = 1;
break;
case 0xac00:
/*
* The Hangul syllables.
*/
add_range(0xac00, 0xd7a3, "Lo", "L");
/*
* Add the characters to the defined category.
*/
add_range(0xac00, 0xd7a3, "Cp", 0);
skip = 1;
break;
case 0xd800:
/*
* Make a range of all surrogates and assume some default
* properties.
*/
add_range(0x010000, 0x10ffff, "Cs", "L");
skip = 5;
break;
case 0xe000:
/*
* The Private Use area. Add with a default set of properties.
*/
add_range(0xe000, 0xf8ff, "Co", "L");
skip = 1;
break;
case 0xf900:
/*
* The CJK compatibility area.
*/
add_range(0xf900, 0xfaff, "Lo", "L");
/*
* Add the characters to the defined category.
*/
add_range(0xf900, 0xfaff, "Cp", 0);
skip = 1;
break;
case 0x20000:
/* CJK Ideograph Extension B */
add_range(0x20000, 0x2a6d6, "Lo", "L");
add_range(0x20000, 0x2a6d6, "Cp", 0);
skip = 1;
break;
case 0xf0000:
/* Plane 15 private use */
add_range(0xf0000, 0xffffd, "Co", "L");
skip = 1;
break;
case 0x100000:
/* Plane 16 private use */
add_range(0x100000, 0x10fffd, "Co", "L");
skip = 1;
break;
}
if (skip)
continue;
/*
* Add the code to the defined category.
*/
ordered_range_insert(code, "Cp", 2);
/*
* Locate the first character property field.
*/
for (i = 0; *s != 0 && i < 2; s++) {
if (*s == ';')
i++;
}
for (e = s; *e && *e != ';'; e++) ;
ordered_range_insert(code, s, e - s);
/*
* Locate the combining class code.
*/
for (s = e; *s != 0 && i < 3; s++) {
if (*s == ';')
i++;
}
/*
* Convert the combining class code from decimal.
*/
for (ccl_code = 0, e = s; *e && *e != ';'; e++)
ccl_code = (ccl_code * 10) + (*e - '0');
/*
* Add the code if it not 0.
*/
if (ccl_code != 0)
ordered_ccl_insert(code, ccl_code);
/*
* Locate the second character property field.
*/
for (s = e; *s != 0 && i < 4; s++) {
if (*s == ';')
i++;
}
for (e = s; *e && *e != ';'; e++) ;
ordered_range_insert(code, s, e - s);