Home | History | Annotate | Line # | Download | only in ucdata
ucgendat.c revision 1.1.1.1.8.2
      1  1.1.1.1.8.2  lukem /* $OpenLDAP: pkg/ldap/libraries/liblunicode/ucdata/ucgendat.c,v 1.39.2.3 2008/02/11 23:26:42 kurt Exp $ */
      2  1.1.1.1.8.2  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      3  1.1.1.1.8.2  lukem  *
      4  1.1.1.1.8.2  lukem  * Copyright 1998-2008 The OpenLDAP Foundation.
      5  1.1.1.1.8.2  lukem  * All rights reserved.
      6  1.1.1.1.8.2  lukem  *
      7  1.1.1.1.8.2  lukem  * Redistribution and use in source and binary forms, with or without
      8  1.1.1.1.8.2  lukem  * modification, are permitted only as authorized by the OpenLDAP
      9  1.1.1.1.8.2  lukem  * Public License.
     10  1.1.1.1.8.2  lukem  *
     11  1.1.1.1.8.2  lukem  * A copy of this license is available in file LICENSE in the
     12  1.1.1.1.8.2  lukem  * top-level directory of the distribution or, alternatively, at
     13  1.1.1.1.8.2  lukem  * <http://www.OpenLDAP.org/license.html>.
     14  1.1.1.1.8.2  lukem  */
     15  1.1.1.1.8.2  lukem /* Copyright 2001 Computing Research Labs, New Mexico State University
     16  1.1.1.1.8.2  lukem  *
     17  1.1.1.1.8.2  lukem  * Permission is hereby granted, free of charge, to any person obtaining a
     18  1.1.1.1.8.2  lukem  * copy of this software and associated documentation files (the "Software"),
     19  1.1.1.1.8.2  lukem  * to deal in the Software without restriction, including without limitation
     20  1.1.1.1.8.2  lukem  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     21  1.1.1.1.8.2  lukem  * and/or sell copies of the Software, and to permit persons to whom the
     22  1.1.1.1.8.2  lukem  * Software is furnished to do so, subject to the following conditions:
     23  1.1.1.1.8.2  lukem  *
     24  1.1.1.1.8.2  lukem  * The above copyright notice and this permission notice shall be included in
     25  1.1.1.1.8.2  lukem  * all copies or substantial portions of the Software.
     26  1.1.1.1.8.2  lukem  *
     27  1.1.1.1.8.2  lukem  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     28  1.1.1.1.8.2  lukem  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     29  1.1.1.1.8.2  lukem  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     30  1.1.1.1.8.2  lukem  * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
     31  1.1.1.1.8.2  lukem  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
     32  1.1.1.1.8.2  lukem  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
     33  1.1.1.1.8.2  lukem  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     34  1.1.1.1.8.2  lukem  */
     35  1.1.1.1.8.2  lukem /* $Id: ucgendat.c,v 1.1.1.1.8.2 2008/05/22 14:20:37 lukem Exp $" */
     36  1.1.1.1.8.2  lukem 
     37  1.1.1.1.8.2  lukem #include "portable.h"
     38  1.1.1.1.8.2  lukem #include "ldap_config.h"
     39  1.1.1.1.8.2  lukem 
     40  1.1.1.1.8.2  lukem #include <stdio.h>
     41  1.1.1.1.8.2  lukem #include <ac/ctype.h>
     42  1.1.1.1.8.2  lukem #include <ac/stdlib.h>
     43  1.1.1.1.8.2  lukem #include <ac/string.h>
     44  1.1.1.1.8.2  lukem #include <ac/unistd.h>
     45  1.1.1.1.8.2  lukem 
     46  1.1.1.1.8.2  lukem #include <ac/bytes.h>
     47  1.1.1.1.8.2  lukem 
     48  1.1.1.1.8.2  lukem #include <lutil.h>
     49  1.1.1.1.8.2  lukem 
     50  1.1.1.1.8.2  lukem #ifndef HARDCODE_DATA
     51  1.1.1.1.8.2  lukem #define	HARDCODE_DATA	1
     52  1.1.1.1.8.2  lukem #endif
     53  1.1.1.1.8.2  lukem 
     54  1.1.1.1.8.2  lukem #undef ishdigit
     55  1.1.1.1.8.2  lukem #define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\
     56  1.1.1.1.8.2  lukem                       ((cc) >= 'A' && (cc) <= 'F') ||\
     57  1.1.1.1.8.2  lukem                       ((cc) >= 'a' && (cc) <= 'f'))
     58  1.1.1.1.8.2  lukem 
     59  1.1.1.1.8.2  lukem /*
     60  1.1.1.1.8.2  lukem  * A header written to the output file with the byte-order-mark and the number
     61  1.1.1.1.8.2  lukem  * of property nodes.
     62  1.1.1.1.8.2  lukem  */
     63  1.1.1.1.8.2  lukem static ac_uint2 hdr[2] = {0xfeff, 0};
     64  1.1.1.1.8.2  lukem 
     65  1.1.1.1.8.2  lukem #define NUMPROPS 50
     66  1.1.1.1.8.2  lukem #define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3)))
     67  1.1.1.1.8.2  lukem 
     68  1.1.1.1.8.2  lukem typedef struct {
     69  1.1.1.1.8.2  lukem     char *name;
     70  1.1.1.1.8.2  lukem     int len;
     71  1.1.1.1.8.2  lukem } _prop_t;
     72  1.1.1.1.8.2  lukem 
     73  1.1.1.1.8.2  lukem /*
     74  1.1.1.1.8.2  lukem  * List of properties expected to be found in the Unicode Character Database
     75  1.1.1.1.8.2  lukem  * including some implementation specific properties.
     76  1.1.1.1.8.2  lukem  *
     77  1.1.1.1.8.2  lukem  * The implementation specific properties are:
     78  1.1.1.1.8.2  lukem  * Cm = Composed (can be decomposed)
     79  1.1.1.1.8.2  lukem  * Nb = Non-breaking
     80  1.1.1.1.8.2  lukem  * Sy = Symmetric (has left and right forms)
     81  1.1.1.1.8.2  lukem  * Hd = Hex digit
     82  1.1.1.1.8.2  lukem  * Qm = Quote marks
     83  1.1.1.1.8.2  lukem  * Mr = Mirroring
     84  1.1.1.1.8.2  lukem  * Ss = Space, other
     85  1.1.1.1.8.2  lukem  * Cp = Defined character
     86  1.1.1.1.8.2  lukem  */
     87  1.1.1.1.8.2  lukem static _prop_t props[NUMPROPS] = {
     88  1.1.1.1.8.2  lukem     {"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2},
     89  1.1.1.1.8.2  lukem     {"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2},
     90  1.1.1.1.8.2  lukem     {"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2},
     91  1.1.1.1.8.2  lukem     {"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2},
     92  1.1.1.1.8.2  lukem     {"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L",  1}, {"R",  1},
     93  1.1.1.1.8.2  lukem     {"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B",  1},
     94  1.1.1.1.8.2  lukem     {"S",  1}, {"WS", 2}, {"ON", 2},
     95  1.1.1.1.8.2  lukem     {"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2},
     96  1.1.1.1.8.2  lukem     {"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2}, {"AL", 2}
     97  1.1.1.1.8.2  lukem };
     98  1.1.1.1.8.2  lukem 
     99  1.1.1.1.8.2  lukem typedef struct {
    100  1.1.1.1.8.2  lukem     ac_uint4 *ranges;
    101  1.1.1.1.8.2  lukem     ac_uint2 used;
    102  1.1.1.1.8.2  lukem     ac_uint2 size;
    103  1.1.1.1.8.2  lukem } _ranges_t;
    104  1.1.1.1.8.2  lukem 
    105  1.1.1.1.8.2  lukem static _ranges_t proptbl[NUMPROPS];
    106  1.1.1.1.8.2  lukem 
    107  1.1.1.1.8.2  lukem /*
    108  1.1.1.1.8.2  lukem  * Make sure this array is sized to be on a 4-byte boundary at compile time.
    109  1.1.1.1.8.2  lukem  */
    110  1.1.1.1.8.2  lukem static ac_uint2 propcnt[NEEDPROPS];
    111  1.1.1.1.8.2  lukem 
    112  1.1.1.1.8.2  lukem /*
    113  1.1.1.1.8.2  lukem  * Array used to collect a decomposition before adding it to the decomposition
    114  1.1.1.1.8.2  lukem  * table.
    115  1.1.1.1.8.2  lukem  */
    116  1.1.1.1.8.2  lukem static ac_uint4 dectmp[64];
    117  1.1.1.1.8.2  lukem static ac_uint4 dectmp_size;
    118  1.1.1.1.8.2  lukem 
    119  1.1.1.1.8.2  lukem typedef struct {
    120  1.1.1.1.8.2  lukem     ac_uint4 code;
    121  1.1.1.1.8.2  lukem     ac_uint2 size;
    122  1.1.1.1.8.2  lukem     ac_uint2 used;
    123  1.1.1.1.8.2  lukem     ac_uint4 *decomp;
    124  1.1.1.1.8.2  lukem } _decomp_t;
    125  1.1.1.1.8.2  lukem 
    126  1.1.1.1.8.2  lukem /*
    127  1.1.1.1.8.2  lukem  * List of decomposition.  Created and expanded in order as the characters are
    128  1.1.1.1.8.2  lukem  * encountered. First list contains canonical mappings, second also includes
    129  1.1.1.1.8.2  lukem  * compatibility mappings.
    130  1.1.1.1.8.2  lukem  */
    131  1.1.1.1.8.2  lukem static _decomp_t *decomps;
    132  1.1.1.1.8.2  lukem static ac_uint4 decomps_used;
    133  1.1.1.1.8.2  lukem static ac_uint4 decomps_size;
    134  1.1.1.1.8.2  lukem 
    135  1.1.1.1.8.2  lukem static _decomp_t *kdecomps;
    136  1.1.1.1.8.2  lukem static ac_uint4 kdecomps_used;
    137  1.1.1.1.8.2  lukem static ac_uint4 kdecomps_size;
    138  1.1.1.1.8.2  lukem 
    139  1.1.1.1.8.2  lukem /*
    140  1.1.1.1.8.2  lukem  * Composition exclusion table stuff.
    141  1.1.1.1.8.2  lukem  */
    142  1.1.1.1.8.2  lukem #define COMPEX_SET(c) (compexs[(c) >> 5] |= (1 << ((c) & 31)))
    143  1.1.1.1.8.2  lukem #define COMPEX_TEST(c) (compexs[(c) >> 5] & (1 << ((c) & 31)))
    144  1.1.1.1.8.2  lukem static ac_uint4 compexs[8192];
    145  1.1.1.1.8.2  lukem 
    146  1.1.1.1.8.2  lukem /*
    147  1.1.1.1.8.2  lukem  * Struct for holding a composition pair, and array of composition pairs
    148  1.1.1.1.8.2  lukem  */
    149  1.1.1.1.8.2  lukem typedef struct {
    150  1.1.1.1.8.2  lukem     ac_uint4 comp;
    151  1.1.1.1.8.2  lukem     ac_uint4 count;
    152  1.1.1.1.8.2  lukem     ac_uint4 code1;
    153  1.1.1.1.8.2  lukem     ac_uint4 code2;
    154  1.1.1.1.8.2  lukem } _comp_t;
    155  1.1.1.1.8.2  lukem 
    156  1.1.1.1.8.2  lukem static _comp_t *comps;
    157  1.1.1.1.8.2  lukem static ac_uint4 comps_used;
    158  1.1.1.1.8.2  lukem 
    159  1.1.1.1.8.2  lukem /*
    160  1.1.1.1.8.2  lukem  * Types and lists for handling lists of case mappings.
    161  1.1.1.1.8.2  lukem  */
    162  1.1.1.1.8.2  lukem typedef struct {
    163  1.1.1.1.8.2  lukem     ac_uint4 key;
    164  1.1.1.1.8.2  lukem     ac_uint4 other1;
    165  1.1.1.1.8.2  lukem     ac_uint4 other2;
    166  1.1.1.1.8.2  lukem } _case_t;
    167  1.1.1.1.8.2  lukem 
    168  1.1.1.1.8.2  lukem static _case_t *upper;
    169  1.1.1.1.8.2  lukem static _case_t *lower;
    170  1.1.1.1.8.2  lukem static _case_t *title;
    171  1.1.1.1.8.2  lukem static ac_uint4 upper_used;
    172  1.1.1.1.8.2  lukem static ac_uint4 upper_size;
    173  1.1.1.1.8.2  lukem static ac_uint4 lower_used;
    174  1.1.1.1.8.2  lukem static ac_uint4 lower_size;
    175  1.1.1.1.8.2  lukem static ac_uint4 title_used;
    176  1.1.1.1.8.2  lukem static ac_uint4 title_size;
    177  1.1.1.1.8.2  lukem 
    178  1.1.1.1.8.2  lukem /*
    179  1.1.1.1.8.2  lukem  * Array used to collect case mappings before adding them to a list.
    180  1.1.1.1.8.2  lukem  */
    181  1.1.1.1.8.2  lukem static ac_uint4 cases[3];
    182  1.1.1.1.8.2  lukem 
    183  1.1.1.1.8.2  lukem /*
    184  1.1.1.1.8.2  lukem  * An array to hold ranges for combining classes.
    185  1.1.1.1.8.2  lukem  */
    186  1.1.1.1.8.2  lukem static ac_uint4 *ccl;
    187  1.1.1.1.8.2  lukem static ac_uint4 ccl_used;
    188  1.1.1.1.8.2  lukem static ac_uint4 ccl_size;
    189  1.1.1.1.8.2  lukem 
    190  1.1.1.1.8.2  lukem /*
    191  1.1.1.1.8.2  lukem  * Structures for handling numbers.
    192  1.1.1.1.8.2  lukem  */
    193  1.1.1.1.8.2  lukem typedef struct {
    194  1.1.1.1.8.2  lukem     ac_uint4 code;
    195  1.1.1.1.8.2  lukem     ac_uint4 idx;
    196  1.1.1.1.8.2  lukem } _codeidx_t;
    197  1.1.1.1.8.2  lukem 
    198  1.1.1.1.8.2  lukem typedef struct {
    199  1.1.1.1.8.2  lukem     short numerator;
    200  1.1.1.1.8.2  lukem     short denominator;
    201  1.1.1.1.8.2  lukem } _num_t;
    202  1.1.1.1.8.2  lukem 
    203  1.1.1.1.8.2  lukem /*
    204  1.1.1.1.8.2  lukem  * Arrays to hold the mapping of codes to numbers.
    205  1.1.1.1.8.2  lukem  */
    206  1.1.1.1.8.2  lukem static _codeidx_t *ncodes;
    207  1.1.1.1.8.2  lukem static ac_uint4 ncodes_used;
    208  1.1.1.1.8.2  lukem static ac_uint4 ncodes_size;
    209  1.1.1.1.8.2  lukem 
    210  1.1.1.1.8.2  lukem static _num_t *nums;
    211  1.1.1.1.8.2  lukem static ac_uint4 nums_used;
    212  1.1.1.1.8.2  lukem static ac_uint4 nums_size;
    213  1.1.1.1.8.2  lukem 
    214  1.1.1.1.8.2  lukem /*
    215  1.1.1.1.8.2  lukem  * Array for holding numbers.
    216  1.1.1.1.8.2  lukem  */
    217  1.1.1.1.8.2  lukem static _num_t *nums;
    218  1.1.1.1.8.2  lukem static ac_uint4 nums_used;
    219  1.1.1.1.8.2  lukem static ac_uint4 nums_size;
    220  1.1.1.1.8.2  lukem 
    221  1.1.1.1.8.2  lukem static void
    222  1.1.1.1.8.2  lukem add_range(ac_uint4 start, ac_uint4 end, char *p1, char *p2)
    223  1.1.1.1.8.2  lukem {
    224  1.1.1.1.8.2  lukem     int i, j, k, len;
    225  1.1.1.1.8.2  lukem     _ranges_t *rlp;
    226  1.1.1.1.8.2  lukem     char *name;
    227  1.1.1.1.8.2  lukem 
    228  1.1.1.1.8.2  lukem     for (k = 0; k < 2; k++) {
    229  1.1.1.1.8.2  lukem         if (k == 0) {
    230  1.1.1.1.8.2  lukem             name = p1;
    231  1.1.1.1.8.2  lukem             len = 2;
    232  1.1.1.1.8.2  lukem         } else {
    233  1.1.1.1.8.2  lukem             if (p2 == 0)
    234  1.1.1.1.8.2  lukem               break;
    235  1.1.1.1.8.2  lukem 
    236  1.1.1.1.8.2  lukem             name = p2;
    237  1.1.1.1.8.2  lukem             len = 1;
    238  1.1.1.1.8.2  lukem         }
    239  1.1.1.1.8.2  lukem 
    240  1.1.1.1.8.2  lukem         for (i = 0; i < NUMPROPS; i++) {
    241  1.1.1.1.8.2  lukem             if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
    242  1.1.1.1.8.2  lukem               break;
    243  1.1.1.1.8.2  lukem         }
    244  1.1.1.1.8.2  lukem 
    245  1.1.1.1.8.2  lukem         if (i == NUMPROPS)
    246  1.1.1.1.8.2  lukem           continue;
    247  1.1.1.1.8.2  lukem 
    248  1.1.1.1.8.2  lukem         rlp = &proptbl[i];
    249  1.1.1.1.8.2  lukem 
    250  1.1.1.1.8.2  lukem         /*
    251  1.1.1.1.8.2  lukem          * Resize the range list if necessary.
    252  1.1.1.1.8.2  lukem          */
    253  1.1.1.1.8.2  lukem         if (rlp->used == rlp->size) {
    254  1.1.1.1.8.2  lukem             if (rlp->size == 0)
    255  1.1.1.1.8.2  lukem               rlp->ranges = (ac_uint4 *)
    256  1.1.1.1.8.2  lukem                   malloc(sizeof(ac_uint4) << 3);
    257  1.1.1.1.8.2  lukem             else
    258  1.1.1.1.8.2  lukem               rlp->ranges = (ac_uint4 *)
    259  1.1.1.1.8.2  lukem                   realloc((char *) rlp->ranges,
    260  1.1.1.1.8.2  lukem                           sizeof(ac_uint4) * (rlp->size + 8));
    261  1.1.1.1.8.2  lukem             rlp->size += 8;
    262  1.1.1.1.8.2  lukem         }
    263  1.1.1.1.8.2  lukem 
    264  1.1.1.1.8.2  lukem         /*
    265  1.1.1.1.8.2  lukem          * If this is the first code for this property list, just add it
    266  1.1.1.1.8.2  lukem          * and return.
    267  1.1.1.1.8.2  lukem          */
    268  1.1.1.1.8.2  lukem         if (rlp->used == 0) {
    269  1.1.1.1.8.2  lukem             rlp->ranges[0] = start;
    270  1.1.1.1.8.2  lukem             rlp->ranges[1] = end;
    271  1.1.1.1.8.2  lukem             rlp->used += 2;
    272  1.1.1.1.8.2  lukem             continue;
    273  1.1.1.1.8.2  lukem         }
    274  1.1.1.1.8.2  lukem 
    275  1.1.1.1.8.2  lukem         /*
    276  1.1.1.1.8.2  lukem          * Optimize the case of adding the range to the end.
    277  1.1.1.1.8.2  lukem          */
    278  1.1.1.1.8.2  lukem         j = rlp->used - 1;
    279  1.1.1.1.8.2  lukem         if (start > rlp->ranges[j]) {
    280  1.1.1.1.8.2  lukem             j = rlp->used;
    281  1.1.1.1.8.2  lukem             rlp->ranges[j++] = start;
    282  1.1.1.1.8.2  lukem             rlp->ranges[j++] = end;
    283  1.1.1.1.8.2  lukem             rlp->used = j;
    284  1.1.1.1.8.2  lukem             continue;
    285  1.1.1.1.8.2  lukem         }
    286  1.1.1.1.8.2  lukem 
    287  1.1.1.1.8.2  lukem         /*
    288  1.1.1.1.8.2  lukem          * Need to locate the insertion point.
    289  1.1.1.1.8.2  lukem          */
    290  1.1.1.1.8.2  lukem         for (i = 0;
    291  1.1.1.1.8.2  lukem              i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
    292  1.1.1.1.8.2  lukem 
    293  1.1.1.1.8.2  lukem         /*
    294  1.1.1.1.8.2  lukem          * If the start value lies in the current range, then simply set the
    295  1.1.1.1.8.2  lukem          * new end point of the range to the end value passed as a parameter.
    296  1.1.1.1.8.2  lukem          */
    297  1.1.1.1.8.2  lukem         if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
    298  1.1.1.1.8.2  lukem             rlp->ranges[i + 1] = end;
    299  1.1.1.1.8.2  lukem             return;
    300  1.1.1.1.8.2  lukem         }
    301  1.1.1.1.8.2  lukem 
    302  1.1.1.1.8.2  lukem         /*
    303  1.1.1.1.8.2  lukem          * Shift following values up by two.
    304  1.1.1.1.8.2  lukem          */
    305  1.1.1.1.8.2  lukem         for (j = rlp->used; j > i; j -= 2) {
    306  1.1.1.1.8.2  lukem             rlp->ranges[j] = rlp->ranges[j - 2];
    307  1.1.1.1.8.2  lukem             rlp->ranges[j + 1] = rlp->ranges[j - 1];
    308  1.1.1.1.8.2  lukem         }
    309  1.1.1.1.8.2  lukem 
    310  1.1.1.1.8.2  lukem         /*
    311  1.1.1.1.8.2  lukem          * Add the new range at the insertion point.
    312  1.1.1.1.8.2  lukem          */
    313  1.1.1.1.8.2  lukem         rlp->ranges[i] = start;
    314  1.1.1.1.8.2  lukem         rlp->ranges[i + 1] = end;
    315  1.1.1.1.8.2  lukem         rlp->used += 2;
    316  1.1.1.1.8.2  lukem     }
    317  1.1.1.1.8.2  lukem }
    318  1.1.1.1.8.2  lukem 
    319  1.1.1.1.8.2  lukem static void
    320  1.1.1.1.8.2  lukem ordered_range_insert(ac_uint4 c, char *name, int len)
    321  1.1.1.1.8.2  lukem {
    322  1.1.1.1.8.2  lukem     int i, j;
    323  1.1.1.1.8.2  lukem     ac_uint4 s, e;
    324  1.1.1.1.8.2  lukem     _ranges_t *rlp;
    325  1.1.1.1.8.2  lukem 
    326  1.1.1.1.8.2  lukem     if (len == 0)
    327  1.1.1.1.8.2  lukem       return;
    328  1.1.1.1.8.2  lukem 
    329  1.1.1.1.8.2  lukem     /*
    330  1.1.1.1.8.2  lukem      * Deal with directionality codes introduced in Unicode 3.0.
    331  1.1.1.1.8.2  lukem      */
    332  1.1.1.1.8.2  lukem     if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
    333  1.1.1.1.8.2  lukem         (len == 3 &&
    334  1.1.1.1.8.2  lukem          (memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
    335  1.1.1.1.8.2  lukem           memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
    336  1.1.1.1.8.2  lukem           memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
    337  1.1.1.1.8.2  lukem         /*
    338  1.1.1.1.8.2  lukem          * Mark all of these as Other Neutral to preserve compatibility with
    339  1.1.1.1.8.2  lukem          * older versions.
    340  1.1.1.1.8.2  lukem          */
    341  1.1.1.1.8.2  lukem         len = 2;
    342  1.1.1.1.8.2  lukem         name = "ON";
    343  1.1.1.1.8.2  lukem     }
    344  1.1.1.1.8.2  lukem 
    345  1.1.1.1.8.2  lukem     for (i = 0; i < NUMPROPS; i++) {
    346  1.1.1.1.8.2  lukem         if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
    347  1.1.1.1.8.2  lukem           break;
    348  1.1.1.1.8.2  lukem     }
    349  1.1.1.1.8.2  lukem 
    350  1.1.1.1.8.2  lukem     if (i == NUMPROPS)
    351  1.1.1.1.8.2  lukem       return;
    352  1.1.1.1.8.2  lukem 
    353  1.1.1.1.8.2  lukem     /*
    354  1.1.1.1.8.2  lukem      * Have a match, so insert the code in order.
    355  1.1.1.1.8.2  lukem      */
    356  1.1.1.1.8.2  lukem     rlp = &proptbl[i];
    357  1.1.1.1.8.2  lukem 
    358  1.1.1.1.8.2  lukem     /*
    359  1.1.1.1.8.2  lukem      * Resize the range list if necessary.
    360  1.1.1.1.8.2  lukem      */
    361  1.1.1.1.8.2  lukem     if (rlp->used == rlp->size) {
    362  1.1.1.1.8.2  lukem         if (rlp->size == 0)
    363  1.1.1.1.8.2  lukem           rlp->ranges = (ac_uint4 *)
    364  1.1.1.1.8.2  lukem               malloc(sizeof(ac_uint4) << 3);
    365  1.1.1.1.8.2  lukem         else
    366  1.1.1.1.8.2  lukem           rlp->ranges = (ac_uint4 *)
    367  1.1.1.1.8.2  lukem               realloc((char *) rlp->ranges,
    368  1.1.1.1.8.2  lukem                       sizeof(ac_uint4) * (rlp->size + 8));
    369  1.1.1.1.8.2  lukem         rlp->size += 8;
    370  1.1.1.1.8.2  lukem     }
    371  1.1.1.1.8.2  lukem 
    372  1.1.1.1.8.2  lukem     /*
    373  1.1.1.1.8.2  lukem      * If this is the first code for this property list, just add it
    374  1.1.1.1.8.2  lukem      * and return.
    375  1.1.1.1.8.2  lukem      */
    376  1.1.1.1.8.2  lukem     if (rlp->used == 0) {
    377  1.1.1.1.8.2  lukem         rlp->ranges[0] = rlp->ranges[1] = c;
    378  1.1.1.1.8.2  lukem         rlp->used += 2;
    379  1.1.1.1.8.2  lukem         return;
    380  1.1.1.1.8.2  lukem     }
    381  1.1.1.1.8.2  lukem 
    382  1.1.1.1.8.2  lukem     /*
    383  1.1.1.1.8.2  lukem      * Optimize the cases of extending the last range and adding new ranges to
    384  1.1.1.1.8.2  lukem      * the end.
    385  1.1.1.1.8.2  lukem      */
    386  1.1.1.1.8.2  lukem     j = rlp->used - 1;
    387  1.1.1.1.8.2  lukem     e = rlp->ranges[j];
    388  1.1.1.1.8.2  lukem     s = rlp->ranges[j - 1];
    389  1.1.1.1.8.2  lukem 
    390  1.1.1.1.8.2  lukem     if (c == e + 1) {
    391  1.1.1.1.8.2  lukem         /*
    392  1.1.1.1.8.2  lukem          * Extend the last range.
    393  1.1.1.1.8.2  lukem          */
    394  1.1.1.1.8.2  lukem         rlp->ranges[j] = c;
    395  1.1.1.1.8.2  lukem         return;
    396  1.1.1.1.8.2  lukem     }
    397  1.1.1.1.8.2  lukem 
    398  1.1.1.1.8.2  lukem     if (c > e + 1) {
    399  1.1.1.1.8.2  lukem         /*
    400  1.1.1.1.8.2  lukem          * Start another range on the end.
    401  1.1.1.1.8.2  lukem          */
    402  1.1.1.1.8.2  lukem         j = rlp->used;
    403  1.1.1.1.8.2  lukem         rlp->ranges[j] = rlp->ranges[j + 1] = c;
    404  1.1.1.1.8.2  lukem         rlp->used += 2;
    405  1.1.1.1.8.2  lukem         return;
    406  1.1.1.1.8.2  lukem     }
    407  1.1.1.1.8.2  lukem 
    408  1.1.1.1.8.2  lukem     if (c >= s)
    409  1.1.1.1.8.2  lukem       /*
    410  1.1.1.1.8.2  lukem        * The code is a duplicate of a code in the last range, so just return.
    411  1.1.1.1.8.2  lukem        */
    412  1.1.1.1.8.2  lukem       return;
    413  1.1.1.1.8.2  lukem 
    414  1.1.1.1.8.2  lukem     /*
    415  1.1.1.1.8.2  lukem      * The code should be inserted somewhere before the last range in the
    416  1.1.1.1.8.2  lukem      * list.  Locate the insertion point.
    417  1.1.1.1.8.2  lukem      */
    418  1.1.1.1.8.2  lukem     for (i = 0;
    419  1.1.1.1.8.2  lukem          i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
    420  1.1.1.1.8.2  lukem 
    421  1.1.1.1.8.2  lukem     s = rlp->ranges[i];
    422  1.1.1.1.8.2  lukem     e = rlp->ranges[i + 1];
    423  1.1.1.1.8.2  lukem 
    424  1.1.1.1.8.2  lukem     if (c == e + 1)
    425  1.1.1.1.8.2  lukem       /*
    426  1.1.1.1.8.2  lukem        * Simply extend the current range.
    427  1.1.1.1.8.2  lukem        */
    428  1.1.1.1.8.2  lukem       rlp->ranges[i + 1] = c;
    429  1.1.1.1.8.2  lukem     else if (c < s) {
    430  1.1.1.1.8.2  lukem         /*
    431  1.1.1.1.8.2  lukem          * Add a new entry before the current location.  Shift all entries
    432  1.1.1.1.8.2  lukem          * before the current one up by one to make room.
    433  1.1.1.1.8.2  lukem          */
    434  1.1.1.1.8.2  lukem         for (j = rlp->used; j > i; j -= 2) {
    435  1.1.1.1.8.2  lukem             rlp->ranges[j] = rlp->ranges[j - 2];
    436  1.1.1.1.8.2  lukem             rlp->ranges[j + 1] = rlp->ranges[j - 1];
    437  1.1.1.1.8.2  lukem         }
    438  1.1.1.1.8.2  lukem         rlp->ranges[i] = rlp->ranges[i + 1] = c;
    439  1.1.1.1.8.2  lukem 
    440  1.1.1.1.8.2  lukem         rlp->used += 2;
    441  1.1.1.1.8.2  lukem     }
    442  1.1.1.1.8.2  lukem }
    443  1.1.1.1.8.2  lukem 
    444  1.1.1.1.8.2  lukem static void
    445  1.1.1.1.8.2  lukem add_decomp(ac_uint4 code, short compat)
    446  1.1.1.1.8.2  lukem {
    447  1.1.1.1.8.2  lukem     ac_uint4 i, j, size;
    448  1.1.1.1.8.2  lukem     _decomp_t **pdecomps;
    449  1.1.1.1.8.2  lukem     ac_uint4 *pdecomps_used;
    450  1.1.1.1.8.2  lukem     ac_uint4 *pdecomps_size;
    451  1.1.1.1.8.2  lukem 
    452  1.1.1.1.8.2  lukem     if (compat) {
    453  1.1.1.1.8.2  lukem 	pdecomps = &kdecomps;
    454  1.1.1.1.8.2  lukem 	pdecomps_used = &kdecomps_used;
    455  1.1.1.1.8.2  lukem 	pdecomps_size = &kdecomps_size;
    456  1.1.1.1.8.2  lukem     } else {
    457  1.1.1.1.8.2  lukem 	pdecomps = &decomps;
    458  1.1.1.1.8.2  lukem 	pdecomps_used = &decomps_used;
    459  1.1.1.1.8.2  lukem 	pdecomps_size = &decomps_size;
    460  1.1.1.1.8.2  lukem     }
    461  1.1.1.1.8.2  lukem 
    462  1.1.1.1.8.2  lukem     /*
    463  1.1.1.1.8.2  lukem      * Add the code to the composite property.
    464  1.1.1.1.8.2  lukem      */
    465  1.1.1.1.8.2  lukem     if (!compat) {
    466  1.1.1.1.8.2  lukem 	ordered_range_insert(code, "Cm", 2);
    467  1.1.1.1.8.2  lukem     }
    468  1.1.1.1.8.2  lukem 
    469  1.1.1.1.8.2  lukem     /*
    470  1.1.1.1.8.2  lukem      * Locate the insertion point for the code.
    471  1.1.1.1.8.2  lukem      */
    472  1.1.1.1.8.2  lukem     for (i = 0; i < *pdecomps_used && code > (*pdecomps)[i].code; i++) ;
    473  1.1.1.1.8.2  lukem 
    474  1.1.1.1.8.2  lukem     /*
    475  1.1.1.1.8.2  lukem      * Allocate space for a new decomposition.
    476  1.1.1.1.8.2  lukem      */
    477  1.1.1.1.8.2  lukem     if (*pdecomps_used == *pdecomps_size) {
    478  1.1.1.1.8.2  lukem         if (*pdecomps_size == 0)
    479  1.1.1.1.8.2  lukem           *pdecomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
    480  1.1.1.1.8.2  lukem         else
    481  1.1.1.1.8.2  lukem           *pdecomps = (_decomp_t *)
    482  1.1.1.1.8.2  lukem               realloc((char *) *pdecomps,
    483  1.1.1.1.8.2  lukem                       sizeof(_decomp_t) * (*pdecomps_size + 8));
    484  1.1.1.1.8.2  lukem         (void) memset((char *) (*pdecomps + *pdecomps_size), '\0',
    485  1.1.1.1.8.2  lukem                       sizeof(_decomp_t) << 3);
    486  1.1.1.1.8.2  lukem         *pdecomps_size += 8;
    487  1.1.1.1.8.2  lukem     }
    488  1.1.1.1.8.2  lukem 
    489  1.1.1.1.8.2  lukem     if (i < *pdecomps_used && code != (*pdecomps)[i].code) {
    490  1.1.1.1.8.2  lukem         /*
    491  1.1.1.1.8.2  lukem          * Shift the decomps up by one if the codes don't match.
    492  1.1.1.1.8.2  lukem          */
    493  1.1.1.1.8.2  lukem         for (j = *pdecomps_used; j > i; j--)
    494  1.1.1.1.8.2  lukem           (void) AC_MEMCPY((char *) &(*pdecomps)[j], (char *) &(*pdecomps)[j - 1],
    495  1.1.1.1.8.2  lukem                         sizeof(_decomp_t));
    496  1.1.1.1.8.2  lukem     }
    497  1.1.1.1.8.2  lukem 
    498  1.1.1.1.8.2  lukem     /*
    499  1.1.1.1.8.2  lukem      * Insert or replace a decomposition.
    500  1.1.1.1.8.2  lukem      */
    501  1.1.1.1.8.2  lukem     size = dectmp_size + (4 - (dectmp_size & 3));
    502  1.1.1.1.8.2  lukem     if ((*pdecomps)[i].size < size) {
    503  1.1.1.1.8.2  lukem         if ((*pdecomps)[i].size == 0)
    504  1.1.1.1.8.2  lukem           (*pdecomps)[i].decomp = (ac_uint4 *)
    505  1.1.1.1.8.2  lukem               malloc(sizeof(ac_uint4) * size);
    506  1.1.1.1.8.2  lukem         else
    507  1.1.1.1.8.2  lukem           (*pdecomps)[i].decomp = (ac_uint4 *)
    508  1.1.1.1.8.2  lukem               realloc((char *) (*pdecomps)[i].decomp,
    509  1.1.1.1.8.2  lukem                       sizeof(ac_uint4) * size);
    510  1.1.1.1.8.2  lukem         (*pdecomps)[i].size = size;
    511  1.1.1.1.8.2  lukem     }
    512  1.1.1.1.8.2  lukem 
    513  1.1.1.1.8.2  lukem     if ((*pdecomps)[i].code != code)
    514  1.1.1.1.8.2  lukem       (*pdecomps_used)++;
    515  1.1.1.1.8.2  lukem 
    516  1.1.1.1.8.2  lukem     (*pdecomps)[i].code = code;
    517  1.1.1.1.8.2  lukem     (*pdecomps)[i].used = dectmp_size;
    518  1.1.1.1.8.2  lukem     (void) AC_MEMCPY((char *) (*pdecomps)[i].decomp, (char *) dectmp,
    519  1.1.1.1.8.2  lukem                   sizeof(ac_uint4) * dectmp_size);
    520  1.1.1.1.8.2  lukem 
    521  1.1.1.1.8.2  lukem     /*
    522  1.1.1.1.8.2  lukem      * NOTICE: This needs changing later so it is more general than simply
    523  1.1.1.1.8.2  lukem      * pairs.  This calculation is done here to simplify allocation elsewhere.
    524  1.1.1.1.8.2  lukem      */
    525  1.1.1.1.8.2  lukem     if (!compat && dectmp_size == 2)
    526  1.1.1.1.8.2  lukem       comps_used++;
    527  1.1.1.1.8.2  lukem }
    528  1.1.1.1.8.2  lukem 
    529  1.1.1.1.8.2  lukem static void
    530  1.1.1.1.8.2  lukem add_title(ac_uint4 code)
    531  1.1.1.1.8.2  lukem {
    532  1.1.1.1.8.2  lukem     ac_uint4 i, j;
    533  1.1.1.1.8.2  lukem 
    534  1.1.1.1.8.2  lukem     /*
    535  1.1.1.1.8.2  lukem      * Always map the code to itself.
    536  1.1.1.1.8.2  lukem      */
    537  1.1.1.1.8.2  lukem     cases[2] = code;
    538  1.1.1.1.8.2  lukem 
    539  1.1.1.1.8.2  lukem     if (title_used == title_size) {
    540  1.1.1.1.8.2  lukem         if (title_size == 0)
    541  1.1.1.1.8.2  lukem           title = (_case_t *) malloc(sizeof(_case_t) << 3);
    542  1.1.1.1.8.2  lukem         else
    543  1.1.1.1.8.2  lukem           title = (_case_t *) realloc((char *) title,
    544  1.1.1.1.8.2  lukem                                       sizeof(_case_t) * (title_size + 8));
    545  1.1.1.1.8.2  lukem         title_size += 8;
    546  1.1.1.1.8.2  lukem     }
    547  1.1.1.1.8.2  lukem 
    548  1.1.1.1.8.2  lukem     /*
    549  1.1.1.1.8.2  lukem      * Locate the insertion point.
    550  1.1.1.1.8.2  lukem      */
    551  1.1.1.1.8.2  lukem     for (i = 0; i < title_used && code > title[i].key; i++) ;
    552  1.1.1.1.8.2  lukem 
    553  1.1.1.1.8.2  lukem     if (i < title_used) {
    554  1.1.1.1.8.2  lukem         /*
    555  1.1.1.1.8.2  lukem          * Shift the array up by one.
    556  1.1.1.1.8.2  lukem          */
    557  1.1.1.1.8.2  lukem         for (j = title_used; j > i; j--)
    558  1.1.1.1.8.2  lukem           (void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
    559  1.1.1.1.8.2  lukem                         sizeof(_case_t));
    560  1.1.1.1.8.2  lukem     }
    561  1.1.1.1.8.2  lukem 
    562  1.1.1.1.8.2  lukem     title[i].key = cases[2];    /* Title */
    563  1.1.1.1.8.2  lukem     title[i].other1 = cases[0]; /* Upper */
    564  1.1.1.1.8.2  lukem     title[i].other2 = cases[1]; /* Lower */
    565  1.1.1.1.8.2  lukem 
    566  1.1.1.1.8.2  lukem     title_used++;
    567  1.1.1.1.8.2  lukem }
    568  1.1.1.1.8.2  lukem 
    569  1.1.1.1.8.2  lukem static void
    570  1.1.1.1.8.2  lukem add_upper(ac_uint4 code)
    571  1.1.1.1.8.2  lukem {
    572  1.1.1.1.8.2  lukem     ac_uint4 i, j;
    573  1.1.1.1.8.2  lukem 
    574  1.1.1.1.8.2  lukem     /*
    575  1.1.1.1.8.2  lukem      * Always map the code to itself.
    576  1.1.1.1.8.2  lukem      */
    577  1.1.1.1.8.2  lukem     cases[0] = code;
    578  1.1.1.1.8.2  lukem 
    579  1.1.1.1.8.2  lukem     /*
    580  1.1.1.1.8.2  lukem      * If the title case character is not present, then make it the same as
    581  1.1.1.1.8.2  lukem      * the upper case.
    582  1.1.1.1.8.2  lukem      */
    583  1.1.1.1.8.2  lukem     if (cases[2] == 0)
    584  1.1.1.1.8.2  lukem       cases[2] = code;
    585  1.1.1.1.8.2  lukem 
    586  1.1.1.1.8.2  lukem     if (upper_used == upper_size) {
    587  1.1.1.1.8.2  lukem         if (upper_size == 0)
    588  1.1.1.1.8.2  lukem           upper = (_case_t *) malloc(sizeof(_case_t) << 3);
    589  1.1.1.1.8.2  lukem         else
    590  1.1.1.1.8.2  lukem           upper = (_case_t *) realloc((char *) upper,
    591  1.1.1.1.8.2  lukem                                       sizeof(_case_t) * (upper_size + 8));
    592  1.1.1.1.8.2  lukem         upper_size += 8;
    593  1.1.1.1.8.2  lukem     }
    594  1.1.1.1.8.2  lukem 
    595  1.1.1.1.8.2  lukem     /*
    596  1.1.1.1.8.2  lukem      * Locate the insertion point.
    597  1.1.1.1.8.2  lukem      */
    598  1.1.1.1.8.2  lukem     for (i = 0; i < upper_used && code > upper[i].key; i++) ;
    599  1.1.1.1.8.2  lukem 
    600  1.1.1.1.8.2  lukem     if (i < upper_used) {
    601  1.1.1.1.8.2  lukem         /*
    602  1.1.1.1.8.2  lukem          * Shift the array up by one.
    603  1.1.1.1.8.2  lukem          */
    604  1.1.1.1.8.2  lukem         for (j = upper_used; j > i; j--)
    605  1.1.1.1.8.2  lukem           (void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
    606  1.1.1.1.8.2  lukem                         sizeof(_case_t));
    607  1.1.1.1.8.2  lukem     }
    608  1.1.1.1.8.2  lukem 
    609  1.1.1.1.8.2  lukem     upper[i].key = cases[0];    /* Upper */
    610  1.1.1.1.8.2  lukem     upper[i].other1 = cases[1]; /* Lower */
    611  1.1.1.1.8.2  lukem     upper[i].other2 = cases[2]; /* Title */
    612  1.1.1.1.8.2  lukem 
    613  1.1.1.1.8.2  lukem     upper_used++;
    614  1.1.1.1.8.2  lukem }
    615  1.1.1.1.8.2  lukem 
    616  1.1.1.1.8.2  lukem static void
    617  1.1.1.1.8.2  lukem add_lower(ac_uint4 code)
    618  1.1.1.1.8.2  lukem {
    619  1.1.1.1.8.2  lukem     ac_uint4 i, j;
    620  1.1.1.1.8.2  lukem 
    621  1.1.1.1.8.2  lukem     /*
    622  1.1.1.1.8.2  lukem      * Always map the code to itself.
    623  1.1.1.1.8.2  lukem      */
    624  1.1.1.1.8.2  lukem     cases[1] = code;
    625  1.1.1.1.8.2  lukem 
    626  1.1.1.1.8.2  lukem     /*
    627  1.1.1.1.8.2  lukem      * If the title case character is empty, then make it the same as the
    628  1.1.1.1.8.2  lukem      * upper case.
    629  1.1.1.1.8.2  lukem      */
    630  1.1.1.1.8.2  lukem     if (cases[2] == 0)
    631  1.1.1.1.8.2  lukem       cases[2] = cases[0];
    632  1.1.1.1.8.2  lukem 
    633  1.1.1.1.8.2  lukem     if (lower_used == lower_size) {
    634  1.1.1.1.8.2  lukem         if (lower_size == 0)
    635  1.1.1.1.8.2  lukem           lower = (_case_t *) malloc(sizeof(_case_t) << 3);
    636  1.1.1.1.8.2  lukem         else
    637  1.1.1.1.8.2  lukem           lower = (_case_t *) realloc((char *) lower,
    638  1.1.1.1.8.2  lukem                                       sizeof(_case_t) * (lower_size + 8));
    639  1.1.1.1.8.2  lukem         lower_size += 8;
    640  1.1.1.1.8.2  lukem     }
    641  1.1.1.1.8.2  lukem 
    642  1.1.1.1.8.2  lukem     /*
    643  1.1.1.1.8.2  lukem      * Locate the insertion point.
    644  1.1.1.1.8.2  lukem      */
    645  1.1.1.1.8.2  lukem     for (i = 0; i < lower_used && code > lower[i].key; i++) ;
    646  1.1.1.1.8.2  lukem 
    647  1.1.1.1.8.2  lukem     if (i < lower_used) {
    648  1.1.1.1.8.2  lukem         /*
    649  1.1.1.1.8.2  lukem          * Shift the array up by one.
    650  1.1.1.1.8.2  lukem          */
    651  1.1.1.1.8.2  lukem         for (j = lower_used; j > i; j--)
    652  1.1.1.1.8.2  lukem           (void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
    653  1.1.1.1.8.2  lukem                         sizeof(_case_t));
    654  1.1.1.1.8.2  lukem     }
    655  1.1.1.1.8.2  lukem 
    656  1.1.1.1.8.2  lukem     lower[i].key = cases[1];    /* Lower */
    657  1.1.1.1.8.2  lukem     lower[i].other1 = cases[0]; /* Upper */
    658  1.1.1.1.8.2  lukem     lower[i].other2 = cases[2]; /* Title */
    659  1.1.1.1.8.2  lukem 
    660  1.1.1.1.8.2  lukem     lower_used++;
    661  1.1.1.1.8.2  lukem }
    662  1.1.1.1.8.2  lukem 
    663  1.1.1.1.8.2  lukem static void
    664  1.1.1.1.8.2  lukem ordered_ccl_insert(ac_uint4 c, ac_uint4 ccl_code)
    665  1.1.1.1.8.2  lukem {
    666  1.1.1.1.8.2  lukem     ac_uint4 i, j;
    667  1.1.1.1.8.2  lukem 
    668  1.1.1.1.8.2  lukem     if (ccl_used == ccl_size) {
    669  1.1.1.1.8.2  lukem         if (ccl_size == 0)
    670  1.1.1.1.8.2  lukem           ccl = (ac_uint4 *) malloc(sizeof(ac_uint4) * 24);
    671  1.1.1.1.8.2  lukem         else
    672  1.1.1.1.8.2  lukem           ccl = (ac_uint4 *)
    673  1.1.1.1.8.2  lukem               realloc((char *) ccl, sizeof(ac_uint4) * (ccl_size + 24));
    674  1.1.1.1.8.2  lukem         ccl_size += 24;
    675  1.1.1.1.8.2  lukem     }
    676  1.1.1.1.8.2  lukem 
    677  1.1.1.1.8.2  lukem     /*
    678  1.1.1.1.8.2  lukem      * Optimize adding the first item.
    679  1.1.1.1.8.2  lukem      */
    680  1.1.1.1.8.2  lukem     if (ccl_used == 0) {
    681  1.1.1.1.8.2  lukem         ccl[0] = ccl[1] = c;
    682  1.1.1.1.8.2  lukem         ccl[2] = ccl_code;
    683  1.1.1.1.8.2  lukem         ccl_used += 3;
    684  1.1.1.1.8.2  lukem         return;
    685  1.1.1.1.8.2  lukem     }
    686  1.1.1.1.8.2  lukem 
    687  1.1.1.1.8.2  lukem     /*
    688  1.1.1.1.8.2  lukem      * Handle the special case of extending the range on the end.  This
    689  1.1.1.1.8.2  lukem      * requires that the combining class codes are the same.
    690  1.1.1.1.8.2  lukem      */
    691  1.1.1.1.8.2  lukem     if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
    692  1.1.1.1.8.2  lukem         ccl[ccl_used - 2] = c;
    693  1.1.1.1.8.2  lukem         return;
    694  1.1.1.1.8.2  lukem     }
    695  1.1.1.1.8.2  lukem 
    696  1.1.1.1.8.2  lukem     /*
    697  1.1.1.1.8.2  lukem      * Handle the special case of adding another range on the end.
    698  1.1.1.1.8.2  lukem      */
    699  1.1.1.1.8.2  lukem     if (c > ccl[ccl_used - 2] + 1 ||
    700  1.1.1.1.8.2  lukem         (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
    701  1.1.1.1.8.2  lukem         ccl[ccl_used++] = c;
    702  1.1.1.1.8.2  lukem         ccl[ccl_used++] = c;
    703  1.1.1.1.8.2  lukem         ccl[ccl_used++] = ccl_code;
    704  1.1.1.1.8.2  lukem         return;
    705  1.1.1.1.8.2  lukem     }
    706  1.1.1.1.8.2  lukem 
    707  1.1.1.1.8.2  lukem     /*
    708  1.1.1.1.8.2  lukem      * Locate either the insertion point or range for the code.
    709  1.1.1.1.8.2  lukem      */
    710  1.1.1.1.8.2  lukem     for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
    711  1.1.1.1.8.2  lukem 
    712  1.1.1.1.8.2  lukem     if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
    713  1.1.1.1.8.2  lukem         /*
    714  1.1.1.1.8.2  lukem          * Extend an existing range.
    715  1.1.1.1.8.2  lukem          */
    716  1.1.1.1.8.2  lukem         ccl[i + 1] = c;
    717  1.1.1.1.8.2  lukem         return;
    718  1.1.1.1.8.2  lukem     } else if (c < ccl[i]) {
    719  1.1.1.1.8.2  lukem         /*
    720  1.1.1.1.8.2  lukem          * Start a new range before the current location.
    721  1.1.1.1.8.2  lukem          */
    722  1.1.1.1.8.2  lukem         for (j = ccl_used; j > i; j -= 3) {
    723  1.1.1.1.8.2  lukem             ccl[j] = ccl[j - 3];
    724  1.1.1.1.8.2  lukem             ccl[j - 1] = ccl[j - 4];
    725  1.1.1.1.8.2  lukem             ccl[j - 2] = ccl[j - 5];
    726  1.1.1.1.8.2  lukem         }
    727  1.1.1.1.8.2  lukem         ccl[i] = ccl[i + 1] = c;
    728  1.1.1.1.8.2  lukem         ccl[i + 2] = ccl_code;
    729  1.1.1.1.8.2  lukem     }
    730  1.1.1.1.8.2  lukem }
    731  1.1.1.1.8.2  lukem 
    732  1.1.1.1.8.2  lukem /*
    733  1.1.1.1.8.2  lukem  * Adds a number if it does not already exist and returns an index value
    734  1.1.1.1.8.2  lukem  * multiplied by 2.
    735  1.1.1.1.8.2  lukem  */
    736  1.1.1.1.8.2  lukem static ac_uint4
    737  1.1.1.1.8.2  lukem make_number(short num, short denom)
    738  1.1.1.1.8.2  lukem {
    739  1.1.1.1.8.2  lukem     ac_uint4 n;
    740  1.1.1.1.8.2  lukem 
    741  1.1.1.1.8.2  lukem     /*
    742  1.1.1.1.8.2  lukem      * Determine if the number already exists.
    743  1.1.1.1.8.2  lukem      */
    744  1.1.1.1.8.2  lukem     for (n = 0; n < nums_used; n++) {
    745  1.1.1.1.8.2  lukem         if (nums[n].numerator == num && nums[n].denominator == denom)
    746  1.1.1.1.8.2  lukem           return n << 1;
    747  1.1.1.1.8.2  lukem     }
    748  1.1.1.1.8.2  lukem 
    749  1.1.1.1.8.2  lukem     if (nums_used == nums_size) {
    750  1.1.1.1.8.2  lukem         if (nums_size == 0)
    751  1.1.1.1.8.2  lukem           nums = (_num_t *) malloc(sizeof(_num_t) << 3);
    752  1.1.1.1.8.2  lukem         else
    753  1.1.1.1.8.2  lukem           nums = (_num_t *) realloc((char *) nums,
    754  1.1.1.1.8.2  lukem                                     sizeof(_num_t) * (nums_size + 8));
    755  1.1.1.1.8.2  lukem         nums_size += 8;
    756  1.1.1.1.8.2  lukem     }
    757  1.1.1.1.8.2  lukem 
    758  1.1.1.1.8.2  lukem     n = nums_used++;
    759  1.1.1.1.8.2  lukem     nums[n].numerator = num;
    760  1.1.1.1.8.2  lukem     nums[n].denominator = denom;
    761  1.1.1.1.8.2  lukem 
    762  1.1.1.1.8.2  lukem     return n << 1;
    763  1.1.1.1.8.2  lukem }
    764  1.1.1.1.8.2  lukem 
    765  1.1.1.1.8.2  lukem static void
    766  1.1.1.1.8.2  lukem add_number(ac_uint4 code, short num, short denom)
    767  1.1.1.1.8.2  lukem {
    768  1.1.1.1.8.2  lukem     ac_uint4 i, j;
    769  1.1.1.1.8.2  lukem 
    770  1.1.1.1.8.2  lukem     /*
    771  1.1.1.1.8.2  lukem      * Insert the code in order.
    772  1.1.1.1.8.2  lukem      */
    773  1.1.1.1.8.2  lukem     for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
    774  1.1.1.1.8.2  lukem 
    775  1.1.1.1.8.2  lukem     /*
    776  1.1.1.1.8.2  lukem      * Handle the case of the codes matching and simply replace the number
    777  1.1.1.1.8.2  lukem      * that was there before.
    778  1.1.1.1.8.2  lukem      */
    779  1.1.1.1.8.2  lukem     if (i < ncodes_used && code == ncodes[i].code) {
    780  1.1.1.1.8.2  lukem         ncodes[i].idx = make_number(num, denom);
    781  1.1.1.1.8.2  lukem         return;
    782  1.1.1.1.8.2  lukem     }
    783  1.1.1.1.8.2  lukem 
    784  1.1.1.1.8.2  lukem     /*
    785  1.1.1.1.8.2  lukem      * Resize the array if necessary.
    786  1.1.1.1.8.2  lukem      */
    787  1.1.1.1.8.2  lukem     if (ncodes_used == ncodes_size) {
    788  1.1.1.1.8.2  lukem         if (ncodes_size == 0)
    789  1.1.1.1.8.2  lukem           ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
    790  1.1.1.1.8.2  lukem         else
    791  1.1.1.1.8.2  lukem           ncodes = (_codeidx_t *)
    792  1.1.1.1.8.2  lukem               realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
    793  1.1.1.1.8.2  lukem 
    794  1.1.1.1.8.2  lukem         ncodes_size += 8;
    795  1.1.1.1.8.2  lukem     }
    796  1.1.1.1.8.2  lukem 
    797  1.1.1.1.8.2  lukem     /*
    798  1.1.1.1.8.2  lukem      * Shift things around to insert the code if necessary.
    799  1.1.1.1.8.2  lukem      */
    800  1.1.1.1.8.2  lukem     if (i < ncodes_used) {
    801  1.1.1.1.8.2  lukem         for (j = ncodes_used; j > i; j--) {
    802  1.1.1.1.8.2  lukem             ncodes[j].code = ncodes[j - 1].code;
    803  1.1.1.1.8.2  lukem             ncodes[j].idx = ncodes[j - 1].idx;
    804  1.1.1.1.8.2  lukem         }
    805  1.1.1.1.8.2  lukem     }
    806  1.1.1.1.8.2  lukem     ncodes[i].code = code;
    807  1.1.1.1.8.2  lukem     ncodes[i].idx = make_number(num, denom);
    808  1.1.1.1.8.2  lukem 
    809  1.1.1.1.8.2  lukem     ncodes_used++;
    810  1.1.1.1.8.2  lukem }
    811  1.1.1.1.8.2  lukem 
    812  1.1.1.1.8.2  lukem /*
    813  1.1.1.1.8.2  lukem  * This routine assumes that the line is a valid Unicode Character Database
    814  1.1.1.1.8.2  lukem  * entry.
    815  1.1.1.1.8.2  lukem  */
    816  1.1.1.1.8.2  lukem static void
    817  1.1.1.1.8.2  lukem read_cdata(FILE *in)
    818  1.1.1.1.8.2  lukem {
    819  1.1.1.1.8.2  lukem     ac_uint4 i, lineno, skip, code, ccl_code;
    820  1.1.1.1.8.2  lukem     short wnum, neg, number[2], compat;
    821  1.1.1.1.8.2  lukem     char line[512], *s, *e;
    822  1.1.1.1.8.2  lukem 
    823  1.1.1.1.8.2  lukem     lineno = skip = 0;
    824  1.1.1.1.8.2  lukem     while (fgets(line, sizeof(line), in)) {
    825  1.1.1.1.8.2  lukem 	if( (s=strchr(line, '\n')) ) *s = '\0';
    826  1.1.1.1.8.2  lukem         lineno++;
    827  1.1.1.1.8.2  lukem 
    828  1.1.1.1.8.2  lukem         /*
    829  1.1.1.1.8.2  lukem          * Skip blank lines and lines that start with a '#'.
    830  1.1.1.1.8.2  lukem          */
    831  1.1.1.1.8.2  lukem         if (line[0] == 0 || line[0] == '#')
    832  1.1.1.1.8.2  lukem           continue;
    833  1.1.1.1.8.2  lukem 
    834  1.1.1.1.8.2  lukem         /*
    835  1.1.1.1.8.2  lukem          * If lines need to be skipped, do it here.
    836  1.1.1.1.8.2  lukem          */
    837  1.1.1.1.8.2  lukem         if (skip) {
    838  1.1.1.1.8.2  lukem             skip--;
    839  1.1.1.1.8.2  lukem             continue;
    840  1.1.1.1.8.2  lukem         }
    841  1.1.1.1.8.2  lukem 
    842  1.1.1.1.8.2  lukem         /*
    843  1.1.1.1.8.2  lukem          * Collect the code.  The code can be up to 6 hex digits in length to
    844  1.1.1.1.8.2  lukem          * allow surrogates to be specified.
    845  1.1.1.1.8.2  lukem          */
    846  1.1.1.1.8.2  lukem         for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
    847  1.1.1.1.8.2  lukem             code <<= 4;
    848  1.1.1.1.8.2  lukem             if (*s >= '0' && *s <= '9')
    849  1.1.1.1.8.2  lukem               code += *s - '0';
    850  1.1.1.1.8.2  lukem             else if (*s >= 'A' && *s <= 'F')
    851  1.1.1.1.8.2  lukem               code += (*s - 'A') + 10;
    852  1.1.1.1.8.2  lukem             else if (*s >= 'a' && *s <= 'f')
    853  1.1.1.1.8.2  lukem               code += (*s - 'a') + 10;
    854  1.1.1.1.8.2  lukem         }
    855  1.1.1.1.8.2  lukem 
    856  1.1.1.1.8.2  lukem         /*
    857  1.1.1.1.8.2  lukem          * Handle the following special cases:
    858  1.1.1.1.8.2  lukem          * 1. 4E00-9FA5 CJK Ideographs.
    859  1.1.1.1.8.2  lukem          * 2. AC00-D7A3 Hangul Syllables.
    860  1.1.1.1.8.2  lukem          * 3. D800-DFFF Surrogates.
    861  1.1.1.1.8.2  lukem          * 4. E000-F8FF Private Use Area.
    862  1.1.1.1.8.2  lukem          * 5. F900-FA2D Han compatibility.
    863  1.1.1.1.8.2  lukem 	 * ...Plus additional ranges in newer Unicode versions...
    864  1.1.1.1.8.2  lukem          */
    865  1.1.1.1.8.2  lukem         switch (code) {
    866  1.1.1.1.8.2  lukem 	  case 0x3400:
    867  1.1.1.1.8.2  lukem 	    /* CJK Ideograph Extension A */
    868  1.1.1.1.8.2  lukem             add_range(0x3400, 0x4db5, "Lo", "L");
    869  1.1.1.1.8.2  lukem 
    870  1.1.1.1.8.2  lukem             add_range(0x3400, 0x4db5, "Cp", 0);
    871  1.1.1.1.8.2  lukem 
    872  1.1.1.1.8.2  lukem 	    skip = 1;
    873  1.1.1.1.8.2  lukem 	    break;
    874  1.1.1.1.8.2  lukem           case 0x4e00:
    875  1.1.1.1.8.2  lukem             /*
    876  1.1.1.1.8.2  lukem              * The Han ideographs.
    877  1.1.1.1.8.2  lukem              */
    878  1.1.1.1.8.2  lukem             add_range(0x4e00, 0x9fff, "Lo", "L");
    879  1.1.1.1.8.2  lukem 
    880  1.1.1.1.8.2  lukem             /*
    881  1.1.1.1.8.2  lukem              * Add the characters to the defined category.
    882  1.1.1.1.8.2  lukem              */
    883  1.1.1.1.8.2  lukem             add_range(0x4e00, 0x9fa5, "Cp", 0);
    884  1.1.1.1.8.2  lukem 
    885  1.1.1.1.8.2  lukem             skip = 1;
    886  1.1.1.1.8.2  lukem             break;
    887  1.1.1.1.8.2  lukem           case 0xac00:
    888  1.1.1.1.8.2  lukem             /*
    889  1.1.1.1.8.2  lukem              * The Hangul syllables.
    890  1.1.1.1.8.2  lukem              */
    891  1.1.1.1.8.2  lukem             add_range(0xac00, 0xd7a3, "Lo", "L");
    892  1.1.1.1.8.2  lukem 
    893  1.1.1.1.8.2  lukem             /*
    894  1.1.1.1.8.2  lukem              * Add the characters to the defined category.
    895  1.1.1.1.8.2  lukem              */
    896  1.1.1.1.8.2  lukem             add_range(0xac00, 0xd7a3, "Cp", 0);
    897  1.1.1.1.8.2  lukem 
    898  1.1.1.1.8.2  lukem             skip = 1;
    899  1.1.1.1.8.2  lukem             break;
    900  1.1.1.1.8.2  lukem           case 0xd800:
    901  1.1.1.1.8.2  lukem             /*
    902  1.1.1.1.8.2  lukem              * Make a range of all surrogates and assume some default
    903  1.1.1.1.8.2  lukem              * properties.
    904  1.1.1.1.8.2  lukem              */
    905  1.1.1.1.8.2  lukem             add_range(0x010000, 0x10ffff, "Cs", "L");
    906  1.1.1.1.8.2  lukem             skip = 5;
    907  1.1.1.1.8.2  lukem             break;
    908  1.1.1.1.8.2  lukem           case 0xe000:
    909  1.1.1.1.8.2  lukem             /*
    910  1.1.1.1.8.2  lukem              * The Private Use area.  Add with a default set of properties.
    911  1.1.1.1.8.2  lukem              */
    912  1.1.1.1.8.2  lukem             add_range(0xe000, 0xf8ff, "Co", "L");
    913  1.1.1.1.8.2  lukem             skip = 1;
    914  1.1.1.1.8.2  lukem             break;
    915  1.1.1.1.8.2  lukem           case 0xf900:
    916  1.1.1.1.8.2  lukem             /*
    917  1.1.1.1.8.2  lukem              * The CJK compatibility area.
    918  1.1.1.1.8.2  lukem              */
    919  1.1.1.1.8.2  lukem             add_range(0xf900, 0xfaff, "Lo", "L");
    920  1.1.1.1.8.2  lukem 
    921  1.1.1.1.8.2  lukem             /*
    922  1.1.1.1.8.2  lukem              * Add the characters to the defined category.
    923  1.1.1.1.8.2  lukem              */
    924  1.1.1.1.8.2  lukem             add_range(0xf900, 0xfaff, "Cp", 0);
    925  1.1.1.1.8.2  lukem 
    926  1.1.1.1.8.2  lukem             skip = 1;
    927  1.1.1.1.8.2  lukem 	    break;
    928  1.1.1.1.8.2  lukem 	  case 0x20000:
    929  1.1.1.1.8.2  lukem 	    /* CJK Ideograph Extension B */
    930  1.1.1.1.8.2  lukem             add_range(0x20000, 0x2a6d6, "Lo", "L");
    931  1.1.1.1.8.2  lukem 
    932  1.1.1.1.8.2  lukem             add_range(0x20000, 0x2a6d6, "Cp", 0);
    933  1.1.1.1.8.2  lukem 
    934  1.1.1.1.8.2  lukem 	    skip = 1;
    935  1.1.1.1.8.2  lukem 	    break;
    936  1.1.1.1.8.2  lukem 	  case 0xf0000:
    937  1.1.1.1.8.2  lukem 	    /* Plane 15 private use */
    938  1.1.1.1.8.2  lukem 	    add_range(0xf0000, 0xffffd, "Co", "L");
    939  1.1.1.1.8.2  lukem 	    skip = 1;
    940  1.1.1.1.8.2  lukem 	    break;
    941  1.1.1.1.8.2  lukem 
    942  1.1.1.1.8.2  lukem 	  case 0x100000:
    943  1.1.1.1.8.2  lukem 	    /* Plane 16 private use */
    944  1.1.1.1.8.2  lukem 	    add_range(0x100000, 0x10fffd, "Co", "L");
    945  1.1.1.1.8.2  lukem 	    skip = 1;
    946  1.1.1.1.8.2  lukem 	    break;
    947  1.1.1.1.8.2  lukem         }
    948  1.1.1.1.8.2  lukem 
    949  1.1.1.1.8.2  lukem         if (skip)
    950  1.1.1.1.8.2  lukem           continue;
    951  1.1.1.1.8.2  lukem 
    952  1.1.1.1.8.2  lukem         /*
    953  1.1.1.1.8.2  lukem          * Add the code to the defined category.
    954  1.1.1.1.8.2  lukem          */
    955  1.1.1.1.8.2  lukem         ordered_range_insert(code, "Cp", 2);
    956  1.1.1.1.8.2  lukem 
    957  1.1.1.1.8.2  lukem         /*
    958  1.1.1.1.8.2  lukem          * Locate the first character property field.
    959  1.1.1.1.8.2  lukem          */
    960  1.1.1.1.8.2  lukem         for (i = 0; *s != 0 && i < 2; s++) {
    961  1.1.1.1.8.2  lukem             if (*s == ';')
    962  1.1.1.1.8.2  lukem               i++;
    963  1.1.1.1.8.2  lukem         }
    964  1.1.1.1.8.2  lukem         for (e = s; *e && *e != ';'; e++) ;
    965  1.1.1.1.8.2  lukem 
    966  1.1.1.1.8.2  lukem         ordered_range_insert(code, s, e - s);
    967  1.1.1.1.8.2  lukem 
    968  1.1.1.1.8.2  lukem         /*
    969  1.1.1.1.8.2  lukem          * Locate the combining class code.
    970  1.1.1.1.8.2  lukem          */
    971  1.1.1.1.8.2  lukem         for (s = e; *s != 0 && i < 3; s++) {
    972  1.1.1.1.8.2  lukem             if (*s == ';')
    973  1.1.1.1.8.2  lukem               i++;
    974  1.1.1.1.8.2  lukem         }
    975  1.1.1.1.8.2  lukem 
    976  1.1.1.1.8.2  lukem         /*
    977  1.1.1.1.8.2  lukem          * Convert the combining class code from decimal.
    978  1.1.1.1.8.2  lukem          */
    979  1.1.1.1.8.2  lukem         for (ccl_code = 0, e = s; *e && *e != ';'; e++)
    980  1.1.1.1.8.2  lukem           ccl_code = (ccl_code * 10) + (*e - '0');
    981  1.1.1.1.8.2  lukem 
    982  1.1.1.1.8.2  lukem         /*
    983  1.1.1.1.8.2  lukem          * Add the code if it not 0.
    984  1.1.1.1.8.2  lukem          */
    985  1.1.1.1.8.2  lukem         if (ccl_code != 0)
    986  1.1.1.1.8.2  lukem           ordered_ccl_insert(code, ccl_code);
    987  1.1.1.1.8.2  lukem 
    988  1.1.1.1.8.2  lukem         /*
    989  1.1.1.1.8.2  lukem          * Locate the second character property field.
    990  1.1.1.1.8.2  lukem          */
    991  1.1.1.1.8.2  lukem         for (s = e; *s != 0 && i < 4; s++) {
    992  1.1.1.1.8.2  lukem             if (*s == ';')
    993  1.1.1.1.8.2  lukem               i++;
    994  1.1.1.1.8.2  lukem         }
    995  1.1.1.1.8.2  lukem         for (e = s; *e && *e != ';'; e++) ;
    996  1.1.1.1.8.2  lukem 
    997  1.1.1.1.8.2  lukem         ordered_range_insert(code, s, e - s);
    998  1.1.1.1.8.2  lukem 
    999  1.1.1.1.8.2  lukem         /*
   1000  1.1.1.1.8.2  lukem          * Check for a decomposition.
   1001  1.1.1.1.8.2  lukem          */
   1002  1.1.1.1.8.2  lukem         s = ++e;
   1003  1.1.1.1.8.2  lukem         if (*s != ';') {
   1004  1.1.1.1.8.2  lukem 	    compat = *s == '<';
   1005  1.1.1.1.8.2  lukem 	    if (compat) {
   1006  1.1.1.1.8.2  lukem 		/*
   1007  1.1.1.1.8.2  lukem 		 * Skip compatibility formatting tag.
   1008  1.1.1.1.8.2  lukem 		 */
   1009  1.1.1.1.8.2  lukem 		while (*s++ != '>');
   1010  1.1.1.1.8.2  lukem 	    }
   1011  1.1.1.1.8.2  lukem             /*
   1012  1.1.1.1.8.2  lukem              * Collect the codes of the decomposition.
   1013  1.1.1.1.8.2  lukem              */
   1014  1.1.1.1.8.2  lukem             for (dectmp_size = 0; *s != ';'; ) {
   1015  1.1.1.1.8.2  lukem                 /*
   1016  1.1.1.1.8.2  lukem                  * Skip all leading non-hex digits.
   1017  1.1.1.1.8.2  lukem                  */
   1018  1.1.1.1.8.2  lukem                 while (!ishdigit(*s))
   1019  1.1.1.1.8.2  lukem  		  s++;
   1020  1.1.1.1.8.2  lukem 
   1021  1.1.1.1.8.2  lukem                 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
   1022  1.1.1.1.8.2  lukem                     dectmp[dectmp_size] <<= 4;
   1023  1.1.1.1.8.2  lukem                     if (*s >= '0' && *s <= '9')
   1024  1.1.1.1.8.2  lukem                       dectmp[dectmp_size] += *s - '0';
   1025  1.1.1.1.8.2  lukem                     else if (*s >= 'A' && *s <= 'F')
   1026  1.1.1.1.8.2  lukem                       dectmp[dectmp_size] += (*s - 'A') + 10;
   1027  1.1.1.1.8.2  lukem                     else if (*s >= 'a' && *s <= 'f')
   1028  1.1.1.1.8.2  lukem                       dectmp[dectmp_size] += (*s - 'a') + 10;
   1029  1.1.1.1.8.2  lukem                 }
   1030  1.1.1.1.8.2  lukem                 dectmp_size++;
   1031  1.1.1.1.8.2  lukem             }
   1032  1.1.1.1.8.2  lukem 
   1033  1.1.1.1.8.2  lukem             /*
   1034  1.1.1.1.8.2  lukem              * If there are any codes in the temporary decomposition array,
   1035  1.1.1.1.8.2  lukem              * then add the character with its decomposition.
   1036  1.1.1.1.8.2  lukem              */
   1037  1.1.1.1.8.2  lukem             if (dectmp_size > 0) {
   1038  1.1.1.1.8.2  lukem 		if (!compat) {
   1039  1.1.1.1.8.2  lukem 		    add_decomp(code, 0);
   1040  1.1.1.1.8.2  lukem 		}
   1041  1.1.1.1.8.2  lukem 		add_decomp(code, 1);
   1042  1.1.1.1.8.2  lukem 	    }
   1043  1.1.1.1.8.2  lukem         }
   1044  1.1.1.1.8.2  lukem 
   1045  1.1.1.1.8.2  lukem         /*
   1046  1.1.1.1.8.2  lukem          * Skip to the number field.
   1047  1.1.1.1.8.2  lukem          */
   1048  1.1.1.1.8.2  lukem         for (i = 0; i < 3 && *s; s++) {
   1049  1.1.1.1.8.2  lukem             if (*s == ';')
   1050  1.1.1.1.8.2  lukem               i++;
   1051  1.1.1.1.8.2  lukem         }
   1052  1.1.1.1.8.2  lukem 
   1053  1.1.1.1.8.2  lukem         /*
   1054  1.1.1.1.8.2  lukem          * Scan the number in.
   1055  1.1.1.1.8.2  lukem          */
   1056  1.1.1.1.8.2  lukem         number[0] = number[1] = 0;
   1057  1.1.1.1.8.2  lukem         for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
   1058  1.1.1.1.8.2  lukem             if (*e == '-') {
   1059  1.1.1.1.8.2  lukem                 neg = 1;
   1060  1.1.1.1.8.2  lukem                 continue;
   1061  1.1.1.1.8.2  lukem             }
   1062  1.1.1.1.8.2  lukem 
   1063  1.1.1.1.8.2  lukem             if (*e == '/') {
   1064  1.1.1.1.8.2  lukem                 /*
   1065  1.1.1.1.8.2  lukem                  * Move the the denominator of the fraction.
   1066  1.1.1.1.8.2  lukem                  */
   1067  1.1.1.1.8.2  lukem                 if (neg)
   1068  1.1.1.1.8.2  lukem                   number[wnum] *= -1;
   1069  1.1.1.1.8.2  lukem                 neg = 0;
   1070  1.1.1.1.8.2  lukem                 e++;
   1071  1.1.1.1.8.2  lukem                 wnum++;
   1072  1.1.1.1.8.2  lukem             }
   1073  1.1.1.1.8.2  lukem             number[wnum] = (number[wnum] * 10) + (*e - '0');
   1074  1.1.1.1.8.2  lukem         }
   1075  1.1.1.1.8.2  lukem 
   1076  1.1.1.1.8.2  lukem         if (e > s) {
   1077  1.1.1.1.8.2  lukem             /*
   1078  1.1.1.1.8.2  lukem              * Adjust the denominator in case of integers and add the number.
   1079  1.1.1.1.8.2  lukem              */
   1080  1.1.1.1.8.2  lukem             if (wnum == 0)
   1081  1.1.1.1.8.2  lukem               number[1] = 1;
   1082  1.1.1.1.8.2  lukem 
   1083  1.1.1.1.8.2  lukem             add_number(code, number[0], number[1]);
   1084  1.1.1.1.8.2  lukem         }
   1085  1.1.1.1.8.2  lukem 
   1086  1.1.1.1.8.2  lukem         /*
   1087  1.1.1.1.8.2  lukem          * Skip to the start of the possible case mappings.
   1088  1.1.1.1.8.2  lukem          */
   1089  1.1.1.1.8.2  lukem         for (s = e, i = 0; i < 4 && *s; s++) {
   1090  1.1.1.1.8.2  lukem             if (*s == ';')
   1091  1.1.1.1.8.2  lukem               i++;
   1092  1.1.1.1.8.2  lukem         }
   1093  1.1.1.1.8.2  lukem 
   1094  1.1.1.1.8.2  lukem         /*
   1095  1.1.1.1.8.2  lukem          * Collect the case mappings.
   1096  1.1.1.1.8.2  lukem          */
   1097  1.1.1.1.8.2  lukem         cases[0] = cases[1] = cases[2] = 0;
   1098  1.1.1.1.8.2  lukem         for (i = 0; i < 3; i++) {
   1099  1.1.1.1.8.2  lukem             while (ishdigit(*s)) {
   1100  1.1.1.1.8.2  lukem                 cases[i] <<= 4;
   1101  1.1.1.1.8.2  lukem                 if (*s >= '0' && *s <= '9')
   1102  1.1.1.1.8.2  lukem                   cases[i] += *s - '0';
   1103  1.1.1.1.8.2  lukem                 else if (*s >= 'A' && *s <= 'F')
   1104  1.1.1.1.8.2  lukem                   cases[i] += (*s - 'A') + 10;
   1105  1.1.1.1.8.2  lukem                 else if (*s >= 'a' && *s <= 'f')
   1106  1.1.1.1.8.2  lukem                   cases[i] += (*s - 'a') + 10;
   1107  1.1.1.1.8.2  lukem                 s++;
   1108  1.1.1.1.8.2  lukem             }
   1109  1.1.1.1.8.2  lukem             if (*s == ';')
   1110  1.1.1.1.8.2  lukem               s++;
   1111  1.1.1.1.8.2  lukem         }
   1112  1.1.1.1.8.2  lukem         if (cases[0] && cases[1])
   1113  1.1.1.1.8.2  lukem           /*
   1114  1.1.1.1.8.2  lukem            * Add the upper and lower mappings for a title case character.
   1115  1.1.1.1.8.2  lukem            */
   1116  1.1.1.1.8.2  lukem           add_title(code);
   1117  1.1.1.1.8.2  lukem         else if (cases[1])
   1118  1.1.1.1.8.2  lukem           /*
   1119  1.1.1.1.8.2  lukem            * Add the lower and title case mappings for the upper case
   1120  1.1.1.1.8.2  lukem            * character.
   1121  1.1.1.1.8.2  lukem            */
   1122  1.1.1.1.8.2  lukem           add_upper(code);
   1123  1.1.1.1.8.2  lukem         else if (cases[0])
   1124  1.1.1.1.8.2  lukem           /*
   1125  1.1.1.1.8.2  lukem            * Add the upper and title case mappings for the lower case
   1126  1.1.1.1.8.2  lukem            * character.
   1127  1.1.1.1.8.2  lukem            */
   1128  1.1.1.1.8.2  lukem           add_lower(code);
   1129  1.1.1.1.8.2  lukem     }
   1130  1.1.1.1.8.2  lukem }
   1131  1.1.1.1.8.2  lukem 
   1132  1.1.1.1.8.2  lukem static _decomp_t *
   1133  1.1.1.1.8.2  lukem find_decomp(ac_uint4 code, short compat)
   1134  1.1.1.1.8.2  lukem {
   1135  1.1.1.1.8.2  lukem     long l, r, m;
   1136  1.1.1.1.8.2  lukem     _decomp_t *decs;
   1137  1.1.1.1.8.2  lukem 
   1138  1.1.1.1.8.2  lukem     l = 0;
   1139  1.1.1.1.8.2  lukem     r = (compat ? kdecomps_used : decomps_used) - 1;
   1140  1.1.1.1.8.2  lukem     decs = compat ? kdecomps : decomps;
   1141  1.1.1.1.8.2  lukem     while (l <= r) {
   1142  1.1.1.1.8.2  lukem         m = (l + r) >> 1;
   1143  1.1.1.1.8.2  lukem         if (code > decs[m].code)
   1144  1.1.1.1.8.2  lukem           l = m + 1;
   1145  1.1.1.1.8.2  lukem         else if (code < decs[m].code)
   1146  1.1.1.1.8.2  lukem           r = m - 1;
   1147  1.1.1.1.8.2  lukem         else
   1148  1.1.1.1.8.2  lukem           return &decs[m];
   1149  1.1.1.1.8.2  lukem     }
   1150  1.1.1.1.8.2  lukem     return 0;
   1151  1.1.1.1.8.2  lukem }
   1152  1.1.1.1.8.2  lukem 
   1153  1.1.1.1.8.2  lukem static void
   1154  1.1.1.1.8.2  lukem decomp_it(_decomp_t *d, short compat)
   1155  1.1.1.1.8.2  lukem {
   1156  1.1.1.1.8.2  lukem     ac_uint4 i;
   1157  1.1.1.1.8.2  lukem     _decomp_t *dp;
   1158  1.1.1.1.8.2  lukem 
   1159  1.1.1.1.8.2  lukem     for (i = 0; i < d->used; i++) {
   1160  1.1.1.1.8.2  lukem         if ((dp = find_decomp(d->decomp[i], compat)) != 0)
   1161  1.1.1.1.8.2  lukem           decomp_it(dp, compat);
   1162  1.1.1.1.8.2  lukem         else
   1163  1.1.1.1.8.2  lukem           dectmp[dectmp_size++] = d->decomp[i];
   1164  1.1.1.1.8.2  lukem     }
   1165  1.1.1.1.8.2  lukem }
   1166  1.1.1.1.8.2  lukem 
   1167  1.1.1.1.8.2  lukem /*
   1168  1.1.1.1.8.2  lukem  * Expand all decompositions by recursively decomposing each character
   1169  1.1.1.1.8.2  lukem  * in the decomposition.
   1170  1.1.1.1.8.2  lukem  */
   1171  1.1.1.1.8.2  lukem static void
   1172  1.1.1.1.8.2  lukem expand_decomp(void)
   1173  1.1.1.1.8.2  lukem {
   1174  1.1.1.1.8.2  lukem     ac_uint4 i;
   1175  1.1.1.1.8.2  lukem 
   1176  1.1.1.1.8.2  lukem     for (i = 0; i < decomps_used; i++) {
   1177  1.1.1.1.8.2  lukem         dectmp_size = 0;
   1178  1.1.1.1.8.2  lukem         decomp_it(&decomps[i], 0);
   1179  1.1.1.1.8.2  lukem         if (dectmp_size > 0)
   1180  1.1.1.1.8.2  lukem           add_decomp(decomps[i].code, 0);
   1181  1.1.1.1.8.2  lukem     }
   1182  1.1.1.1.8.2  lukem 
   1183  1.1.1.1.8.2  lukem     for (i = 0; i < kdecomps_used; i++) {
   1184  1.1.1.1.8.2  lukem         dectmp_size = 0;
   1185  1.1.1.1.8.2  lukem         decomp_it(&kdecomps[i], 1);
   1186  1.1.1.1.8.2  lukem         if (dectmp_size > 0)
   1187  1.1.1.1.8.2  lukem           add_decomp(kdecomps[i].code, 1);
   1188  1.1.1.1.8.2  lukem     }
   1189  1.1.1.1.8.2  lukem }
   1190  1.1.1.1.8.2  lukem 
   1191  1.1.1.1.8.2  lukem static int
   1192  1.1.1.1.8.2  lukem cmpcomps(const void *v_comp1, const void *v_comp2)
   1193  1.1.1.1.8.2  lukem {
   1194  1.1.1.1.8.2  lukem 	const _comp_t *comp1 = v_comp1, *comp2 = v_comp2;
   1195  1.1.1.1.8.2  lukem     long diff = comp1->code1 - comp2->code1;
   1196  1.1.1.1.8.2  lukem 
   1197  1.1.1.1.8.2  lukem     if (!diff)
   1198  1.1.1.1.8.2  lukem 	diff = comp1->code2 - comp2->code2;
   1199  1.1.1.1.8.2  lukem     return (int) diff;
   1200  1.1.1.1.8.2  lukem }
   1201  1.1.1.1.8.2  lukem 
   1202  1.1.1.1.8.2  lukem /*
   1203  1.1.1.1.8.2  lukem  * Load composition exclusion data
   1204  1.1.1.1.8.2  lukem  */
   1205  1.1.1.1.8.2  lukem static void
   1206  1.1.1.1.8.2  lukem read_compexdata(FILE *in)
   1207  1.1.1.1.8.2  lukem {
   1208  1.1.1.1.8.2  lukem     ac_uint2 i;
   1209  1.1.1.1.8.2  lukem     ac_uint4 code;
   1210  1.1.1.1.8.2  lukem     char line[512], *s;
   1211  1.1.1.1.8.2  lukem 
   1212  1.1.1.1.8.2  lukem     (void) memset((char *) compexs, 0, sizeof(compexs));
   1213  1.1.1.1.8.2  lukem 
   1214  1.1.1.1.8.2  lukem     while (fgets(line, sizeof(line), in)) {
   1215  1.1.1.1.8.2  lukem 	if( (s=strchr(line, '\n')) ) *s = '\0';
   1216  1.1.1.1.8.2  lukem         /*
   1217  1.1.1.1.8.2  lukem          * Skip blank lines and lines that start with a '#'.
   1218  1.1.1.1.8.2  lukem          */
   1219  1.1.1.1.8.2  lukem         if (line[0] == 0 || line[0] == '#')
   1220  1.1.1.1.8.2  lukem 	    continue;
   1221  1.1.1.1.8.2  lukem 
   1222  1.1.1.1.8.2  lukem 	/*
   1223  1.1.1.1.8.2  lukem          * Collect the code.  Assume max 6 digits
   1224  1.1.1.1.8.2  lukem          */
   1225  1.1.1.1.8.2  lukem 
   1226  1.1.1.1.8.2  lukem 	for (s = line, i = code = 0; *s != '#' && i < 6; i++, s++) {
   1227  1.1.1.1.8.2  lukem 	    if (isspace((unsigned char)*s)) break;
   1228  1.1.1.1.8.2  lukem             code <<= 4;
   1229  1.1.1.1.8.2  lukem             if (*s >= '0' && *s <= '9')
   1230  1.1.1.1.8.2  lukem 		code += *s - '0';
   1231  1.1.1.1.8.2  lukem             else if (*s >= 'A' && *s <= 'F')
   1232  1.1.1.1.8.2  lukem 		code += (*s - 'A') + 10;
   1233  1.1.1.1.8.2  lukem             else if (*s >= 'a' && *s <= 'f')
   1234  1.1.1.1.8.2  lukem 		code += (*s - 'a') + 10;
   1235  1.1.1.1.8.2  lukem         }
   1236  1.1.1.1.8.2  lukem         COMPEX_SET(code);
   1237  1.1.1.1.8.2  lukem     }
   1238  1.1.1.1.8.2  lukem }
   1239  1.1.1.1.8.2  lukem 
   1240  1.1.1.1.8.2  lukem /*
   1241  1.1.1.1.8.2  lukem  * Creates array of compositions from decomposition array
   1242  1.1.1.1.8.2  lukem  */
   1243  1.1.1.1.8.2  lukem static void
   1244  1.1.1.1.8.2  lukem create_comps(void)
   1245  1.1.1.1.8.2  lukem {
   1246  1.1.1.1.8.2  lukem     ac_uint4 i, cu;
   1247  1.1.1.1.8.2  lukem 
   1248  1.1.1.1.8.2  lukem     comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
   1249  1.1.1.1.8.2  lukem 
   1250  1.1.1.1.8.2  lukem     for (i = cu = 0; i < decomps_used; i++) {
   1251  1.1.1.1.8.2  lukem 	if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
   1252  1.1.1.1.8.2  lukem 	    continue;
   1253  1.1.1.1.8.2  lukem 	comps[cu].comp = decomps[i].code;
   1254  1.1.1.1.8.2  lukem 	comps[cu].count = 2;
   1255  1.1.1.1.8.2  lukem 	comps[cu].code1 = decomps[i].decomp[0];
   1256  1.1.1.1.8.2  lukem 	comps[cu].code2 = decomps[i].decomp[1];
   1257  1.1.1.1.8.2  lukem 	cu++;
   1258  1.1.1.1.8.2  lukem     }
   1259  1.1.1.1.8.2  lukem     comps_used = cu;
   1260  1.1.1.1.8.2  lukem     qsort(comps, comps_used, sizeof(_comp_t), cmpcomps);
   1261  1.1.1.1.8.2  lukem }
   1262  1.1.1.1.8.2  lukem 
   1263  1.1.1.1.8.2  lukem #if HARDCODE_DATA
   1264  1.1.1.1.8.2  lukem static void
   1265  1.1.1.1.8.2  lukem write_case(FILE *out, _case_t *tab, int num, int first)
   1266  1.1.1.1.8.2  lukem {
   1267  1.1.1.1.8.2  lukem     int i;
   1268  1.1.1.1.8.2  lukem 
   1269  1.1.1.1.8.2  lukem     for (i=0; i<num; i++) {
   1270  1.1.1.1.8.2  lukem 	if (first) first = 0;
   1271  1.1.1.1.8.2  lukem 	else fprintf(out, ",");
   1272  1.1.1.1.8.2  lukem 	fprintf(out, "\n\t0x%08lx, 0x%08lx, 0x%08lx",
   1273  1.1.1.1.8.2  lukem 		(unsigned long) tab[i].key, (unsigned long) tab[i].other1,
   1274  1.1.1.1.8.2  lukem 		(unsigned long) tab[i].other2);
   1275  1.1.1.1.8.2  lukem     }
   1276  1.1.1.1.8.2  lukem }
   1277  1.1.1.1.8.2  lukem 
   1278  1.1.1.1.8.2  lukem #define PREF "static const "
   1279  1.1.1.1.8.2  lukem 
   1280  1.1.1.1.8.2  lukem #endif
   1281  1.1.1.1.8.2  lukem 
   1282  1.1.1.1.8.2  lukem static void
   1283  1.1.1.1.8.2  lukem write_cdata(char *opath)
   1284  1.1.1.1.8.2  lukem {
   1285  1.1.1.1.8.2  lukem     FILE *out;
   1286  1.1.1.1.8.2  lukem 	ac_uint4 bytes;
   1287  1.1.1.1.8.2  lukem     ac_uint4 i, idx, nprops;
   1288  1.1.1.1.8.2  lukem #if !(HARDCODE_DATA)
   1289  1.1.1.1.8.2  lukem     ac_uint2 casecnt[2];
   1290  1.1.1.1.8.2  lukem #endif
   1291  1.1.1.1.8.2  lukem     char path[BUFSIZ];
   1292  1.1.1.1.8.2  lukem #if HARDCODE_DATA
   1293  1.1.1.1.8.2  lukem     int j, k;
   1294  1.1.1.1.8.2  lukem 
   1295  1.1.1.1.8.2  lukem     /*****************************************************************
   1296  1.1.1.1.8.2  lukem      *
   1297  1.1.1.1.8.2  lukem      * Generate the ctype data.
   1298  1.1.1.1.8.2  lukem      *
   1299  1.1.1.1.8.2  lukem      *****************************************************************/
   1300  1.1.1.1.8.2  lukem 
   1301  1.1.1.1.8.2  lukem     /*
   1302  1.1.1.1.8.2  lukem      * Open the output file.
   1303  1.1.1.1.8.2  lukem      */
   1304  1.1.1.1.8.2  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "uctable.h", opath);
   1305  1.1.1.1.8.2  lukem     if ((out = fopen(path, "w")) == 0)
   1306  1.1.1.1.8.2  lukem       return;
   1307  1.1.1.1.8.2  lukem #else
   1308  1.1.1.1.8.2  lukem     /*
   1309  1.1.1.1.8.2  lukem      * Open the ctype.dat file.
   1310  1.1.1.1.8.2  lukem      */
   1311  1.1.1.1.8.2  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "ctype.dat", opath);
   1312  1.1.1.1.8.2  lukem     if ((out = fopen(path, "wb")) == 0)
   1313  1.1.1.1.8.2  lukem       return;
   1314  1.1.1.1.8.2  lukem #endif
   1315  1.1.1.1.8.2  lukem 
   1316  1.1.1.1.8.2  lukem     /*
   1317  1.1.1.1.8.2  lukem      * Collect the offsets for the properties.  The offsets array is
   1318  1.1.1.1.8.2  lukem      * on a 4-byte boundary to keep things efficient for architectures
   1319  1.1.1.1.8.2  lukem      * that need such a thing.
   1320  1.1.1.1.8.2  lukem      */
   1321  1.1.1.1.8.2  lukem     for (i = idx = 0; i < NUMPROPS; i++) {
   1322  1.1.1.1.8.2  lukem         propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
   1323  1.1.1.1.8.2  lukem         idx += proptbl[i].used;
   1324  1.1.1.1.8.2  lukem     }
   1325  1.1.1.1.8.2  lukem 
   1326  1.1.1.1.8.2  lukem     /*
   1327  1.1.1.1.8.2  lukem      * Add the sentinel index which is used by the binary search as the upper
   1328  1.1.1.1.8.2  lukem      * bound for a search.
   1329  1.1.1.1.8.2  lukem      */
   1330  1.1.1.1.8.2  lukem     propcnt[i] = idx;
   1331  1.1.1.1.8.2  lukem 
   1332  1.1.1.1.8.2  lukem     /*
   1333  1.1.1.1.8.2  lukem      * Record the actual number of property lists.  This may be different than
   1334  1.1.1.1.8.2  lukem      * the number of offsets actually written because of aligning on a 4-byte
   1335  1.1.1.1.8.2  lukem      * boundary.
   1336  1.1.1.1.8.2  lukem      */
   1337  1.1.1.1.8.2  lukem     hdr[1] = NUMPROPS;
   1338  1.1.1.1.8.2  lukem 
   1339  1.1.1.1.8.2  lukem     /*
   1340  1.1.1.1.8.2  lukem      * Calculate the byte count needed and pad the property counts array to a
   1341  1.1.1.1.8.2  lukem      * 4-byte boundary.
   1342  1.1.1.1.8.2  lukem      */
   1343  1.1.1.1.8.2  lukem     if ((bytes = sizeof(ac_uint2) * (NUMPROPS + 1)) & 3)
   1344  1.1.1.1.8.2  lukem       bytes += 4 - (bytes & 3);
   1345  1.1.1.1.8.2  lukem     nprops = bytes / sizeof(ac_uint2);
   1346  1.1.1.1.8.2  lukem     bytes += sizeof(ac_uint4) * idx;
   1347  1.1.1.1.8.2  lukem 
   1348  1.1.1.1.8.2  lukem #if HARDCODE_DATA
   1349  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _ucprop_size = %d;\n\n", NUMPROPS);
   1350  1.1.1.1.8.2  lukem 
   1351  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint2 _ucprop_offsets[] = {");
   1352  1.1.1.1.8.2  lukem 
   1353  1.1.1.1.8.2  lukem     for (i = 0; i<nprops; i++) {
   1354  1.1.1.1.8.2  lukem        if (i) fprintf(out, ",");
   1355  1.1.1.1.8.2  lukem        if (!(i&7)) fprintf(out, "\n\t");
   1356  1.1.1.1.8.2  lukem        else fprintf(out, " ");
   1357  1.1.1.1.8.2  lukem        fprintf(out, "0x%04x", propcnt[i]);
   1358  1.1.1.1.8.2  lukem     }
   1359  1.1.1.1.8.2  lukem     fprintf(out, "\n};\n\n");
   1360  1.1.1.1.8.2  lukem 
   1361  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _ucprop_ranges[] = {");
   1362  1.1.1.1.8.2  lukem 
   1363  1.1.1.1.8.2  lukem     k = 0;
   1364  1.1.1.1.8.2  lukem     for (i = 0; i < NUMPROPS; i++) {
   1365  1.1.1.1.8.2  lukem 	if (proptbl[i].used > 0) {
   1366  1.1.1.1.8.2  lukem 	  for (j=0; j<proptbl[i].used; j++) {
   1367  1.1.1.1.8.2  lukem 	    if (k) fprintf(out, ",");
   1368  1.1.1.1.8.2  lukem 	    if (!(k&3)) fprintf(out,"\n\t");
   1369  1.1.1.1.8.2  lukem 	    else fprintf(out, " ");
   1370  1.1.1.1.8.2  lukem 	    k++;
   1371  1.1.1.1.8.2  lukem 	    fprintf(out, "0x%08lx", (unsigned long) proptbl[i].ranges[j]);
   1372  1.1.1.1.8.2  lukem 	  }
   1373  1.1.1.1.8.2  lukem 	}
   1374  1.1.1.1.8.2  lukem     }
   1375  1.1.1.1.8.2  lukem     fprintf(out, "\n};\n\n");
   1376  1.1.1.1.8.2  lukem #else
   1377  1.1.1.1.8.2  lukem     /*
   1378  1.1.1.1.8.2  lukem      * Write the header.
   1379  1.1.1.1.8.2  lukem      */
   1380  1.1.1.1.8.2  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1381  1.1.1.1.8.2  lukem 
   1382  1.1.1.1.8.2  lukem     /*
   1383  1.1.1.1.8.2  lukem      * Write the byte count.
   1384  1.1.1.1.8.2  lukem      */
   1385  1.1.1.1.8.2  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1386  1.1.1.1.8.2  lukem 
   1387  1.1.1.1.8.2  lukem     /*
   1388  1.1.1.1.8.2  lukem      * Write the property list counts.
   1389  1.1.1.1.8.2  lukem      */
   1390  1.1.1.1.8.2  lukem     fwrite((char *) propcnt, sizeof(ac_uint2), nprops, out);
   1391  1.1.1.1.8.2  lukem 
   1392  1.1.1.1.8.2  lukem     /*
   1393  1.1.1.1.8.2  lukem      * Write the property lists.
   1394  1.1.1.1.8.2  lukem      */
   1395  1.1.1.1.8.2  lukem     for (i = 0; i < NUMPROPS; i++) {
   1396  1.1.1.1.8.2  lukem         if (proptbl[i].used > 0)
   1397  1.1.1.1.8.2  lukem           fwrite((char *) proptbl[i].ranges, sizeof(ac_uint4),
   1398  1.1.1.1.8.2  lukem                  proptbl[i].used, out);
   1399  1.1.1.1.8.2  lukem     }
   1400  1.1.1.1.8.2  lukem 
   1401  1.1.1.1.8.2  lukem     fclose(out);
   1402  1.1.1.1.8.2  lukem #endif
   1403  1.1.1.1.8.2  lukem 
   1404  1.1.1.1.8.2  lukem     /*****************************************************************
   1405  1.1.1.1.8.2  lukem      *
   1406  1.1.1.1.8.2  lukem      * Generate the case mapping data.
   1407  1.1.1.1.8.2  lukem      *
   1408  1.1.1.1.8.2  lukem      *****************************************************************/
   1409  1.1.1.1.8.2  lukem 
   1410  1.1.1.1.8.2  lukem #if HARDCODE_DATA
   1411  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _uccase_size = %ld;\n\n",
   1412  1.1.1.1.8.2  lukem         (long) (upper_used + lower_used + title_used));
   1413  1.1.1.1.8.2  lukem 
   1414  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint2 _uccase_len[2] = {%ld, %ld};\n\n",
   1415  1.1.1.1.8.2  lukem         (long) upper_used, (long) lower_used);
   1416  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _uccase_map[] = {");
   1417  1.1.1.1.8.2  lukem 
   1418  1.1.1.1.8.2  lukem     if (upper_used > 0)
   1419  1.1.1.1.8.2  lukem       /*
   1420  1.1.1.1.8.2  lukem        * Write the upper case table.
   1421  1.1.1.1.8.2  lukem        */
   1422  1.1.1.1.8.2  lukem       write_case(out, upper, upper_used, 1);
   1423  1.1.1.1.8.2  lukem 
   1424  1.1.1.1.8.2  lukem     if (lower_used > 0)
   1425  1.1.1.1.8.2  lukem       /*
   1426  1.1.1.1.8.2  lukem        * Write the lower case table.
   1427  1.1.1.1.8.2  lukem        */
   1428  1.1.1.1.8.2  lukem       write_case(out, lower, lower_used, !upper_used);
   1429  1.1.1.1.8.2  lukem 
   1430  1.1.1.1.8.2  lukem     if (title_used > 0)
   1431  1.1.1.1.8.2  lukem       /*
   1432  1.1.1.1.8.2  lukem        * Write the title case table.
   1433  1.1.1.1.8.2  lukem        */
   1434  1.1.1.1.8.2  lukem       write_case(out, title, title_used, !(upper_used||lower_used));
   1435  1.1.1.1.8.2  lukem 
   1436  1.1.1.1.8.2  lukem     if (!(upper_used || lower_used || title_used))
   1437  1.1.1.1.8.2  lukem 	fprintf(out, "\t0");
   1438  1.1.1.1.8.2  lukem 
   1439  1.1.1.1.8.2  lukem     fprintf(out, "\n};\n\n");
   1440  1.1.1.1.8.2  lukem #else
   1441  1.1.1.1.8.2  lukem     /*
   1442  1.1.1.1.8.2  lukem      * Open the case.dat file.
   1443  1.1.1.1.8.2  lukem      */
   1444  1.1.1.1.8.2  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "case.dat", opath);
   1445  1.1.1.1.8.2  lukem     if ((out = fopen(path, "wb")) == 0)
   1446  1.1.1.1.8.2  lukem       return;
   1447  1.1.1.1.8.2  lukem 
   1448  1.1.1.1.8.2  lukem     /*
   1449  1.1.1.1.8.2  lukem      * Write the case mapping tables.
   1450  1.1.1.1.8.2  lukem      */
   1451  1.1.1.1.8.2  lukem     hdr[1] = upper_used + lower_used + title_used;
   1452  1.1.1.1.8.2  lukem     casecnt[0] = upper_used;
   1453  1.1.1.1.8.2  lukem     casecnt[1] = lower_used;
   1454  1.1.1.1.8.2  lukem 
   1455  1.1.1.1.8.2  lukem     /*
   1456  1.1.1.1.8.2  lukem      * Write the header.
   1457  1.1.1.1.8.2  lukem      */
   1458  1.1.1.1.8.2  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1459  1.1.1.1.8.2  lukem 
   1460  1.1.1.1.8.2  lukem     /*
   1461  1.1.1.1.8.2  lukem      * Write the upper and lower case table sizes.
   1462  1.1.1.1.8.2  lukem      */
   1463  1.1.1.1.8.2  lukem     fwrite((char *) casecnt, sizeof(ac_uint2), 2, out);
   1464  1.1.1.1.8.2  lukem 
   1465  1.1.1.1.8.2  lukem     if (upper_used > 0)
   1466  1.1.1.1.8.2  lukem       /*
   1467  1.1.1.1.8.2  lukem        * Write the upper case table.
   1468  1.1.1.1.8.2  lukem        */
   1469  1.1.1.1.8.2  lukem       fwrite((char *) upper, sizeof(_case_t), upper_used, out);
   1470  1.1.1.1.8.2  lukem 
   1471  1.1.1.1.8.2  lukem     if (lower_used > 0)
   1472  1.1.1.1.8.2  lukem       /*
   1473  1.1.1.1.8.2  lukem        * Write the lower case table.
   1474  1.1.1.1.8.2  lukem        */
   1475  1.1.1.1.8.2  lukem       fwrite((char *) lower, sizeof(_case_t), lower_used, out);
   1476  1.1.1.1.8.2  lukem 
   1477  1.1.1.1.8.2  lukem     if (title_used > 0)
   1478  1.1.1.1.8.2  lukem       /*
   1479  1.1.1.1.8.2  lukem        * Write the title case table.
   1480  1.1.1.1.8.2  lukem        */
   1481  1.1.1.1.8.2  lukem       fwrite((char *) title, sizeof(_case_t), title_used, out);
   1482  1.1.1.1.8.2  lukem 
   1483  1.1.1.1.8.2  lukem     fclose(out);
   1484  1.1.1.1.8.2  lukem #endif
   1485  1.1.1.1.8.2  lukem 
   1486  1.1.1.1.8.2  lukem     /*****************************************************************
   1487  1.1.1.1.8.2  lukem      *
   1488  1.1.1.1.8.2  lukem      * Generate the composition data.
   1489  1.1.1.1.8.2  lukem      *
   1490  1.1.1.1.8.2  lukem      *****************************************************************/
   1491  1.1.1.1.8.2  lukem 
   1492  1.1.1.1.8.2  lukem     /*
   1493  1.1.1.1.8.2  lukem      * Create compositions from decomposition data
   1494  1.1.1.1.8.2  lukem      */
   1495  1.1.1.1.8.2  lukem     create_comps();
   1496  1.1.1.1.8.2  lukem 
   1497  1.1.1.1.8.2  lukem #if HARDCODE_DATA
   1498  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _uccomp_size = %ld;\n\n",
   1499  1.1.1.1.8.2  lukem         comps_used * 4L);
   1500  1.1.1.1.8.2  lukem 
   1501  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _uccomp_data[] = {");
   1502  1.1.1.1.8.2  lukem 
   1503  1.1.1.1.8.2  lukem      /*
   1504  1.1.1.1.8.2  lukem       * Now, if comps exist, write them out.
   1505  1.1.1.1.8.2  lukem       */
   1506  1.1.1.1.8.2  lukem     if (comps_used > 0) {
   1507  1.1.1.1.8.2  lukem 	for (i=0; i<comps_used; i++) {
   1508  1.1.1.1.8.2  lukem 	    if (i) fprintf(out, ",");
   1509  1.1.1.1.8.2  lukem 	    fprintf(out, "\n\t0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx",
   1510  1.1.1.1.8.2  lukem 	        (unsigned long) comps[i].comp, (unsigned long) comps[i].count,
   1511  1.1.1.1.8.2  lukem 	        (unsigned long) comps[i].code1, (unsigned long) comps[i].code2);
   1512  1.1.1.1.8.2  lukem 	}
   1513  1.1.1.1.8.2  lukem     } else {
   1514  1.1.1.1.8.2  lukem 	fprintf(out, "\t0");
   1515  1.1.1.1.8.2  lukem     }
   1516  1.1.1.1.8.2  lukem     fprintf(out, "\n};\n\n");
   1517  1.1.1.1.8.2  lukem #else
   1518  1.1.1.1.8.2  lukem     /*
   1519  1.1.1.1.8.2  lukem      * Open the comp.dat file.
   1520  1.1.1.1.8.2  lukem      */
   1521  1.1.1.1.8.2  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "comp.dat", opath);
   1522  1.1.1.1.8.2  lukem     if ((out = fopen(path, "wb")) == 0)
   1523  1.1.1.1.8.2  lukem 	return;
   1524  1.1.1.1.8.2  lukem 
   1525  1.1.1.1.8.2  lukem     /*
   1526  1.1.1.1.8.2  lukem      * Write the header.
   1527  1.1.1.1.8.2  lukem      */
   1528  1.1.1.1.8.2  lukem     hdr[1] = (ac_uint2) comps_used * 4;
   1529  1.1.1.1.8.2  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1530  1.1.1.1.8.2  lukem 
   1531  1.1.1.1.8.2  lukem     /*
   1532  1.1.1.1.8.2  lukem      * Write out the byte count to maintain header size.
   1533  1.1.1.1.8.2  lukem      */
   1534  1.1.1.1.8.2  lukem     bytes = comps_used * sizeof(_comp_t);
   1535  1.1.1.1.8.2  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1536  1.1.1.1.8.2  lukem 
   1537  1.1.1.1.8.2  lukem     /*
   1538  1.1.1.1.8.2  lukem      * Now, if comps exist, write them out.
   1539  1.1.1.1.8.2  lukem      */
   1540  1.1.1.1.8.2  lukem     if (comps_used > 0)
   1541  1.1.1.1.8.2  lukem         fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
   1542  1.1.1.1.8.2  lukem 
   1543  1.1.1.1.8.2  lukem     fclose(out);
   1544  1.1.1.1.8.2  lukem #endif
   1545  1.1.1.1.8.2  lukem 
   1546  1.1.1.1.8.2  lukem     /*****************************************************************
   1547  1.1.1.1.8.2  lukem      *
   1548  1.1.1.1.8.2  lukem      * Generate the decomposition data.
   1549  1.1.1.1.8.2  lukem      *
   1550  1.1.1.1.8.2  lukem      *****************************************************************/
   1551  1.1.1.1.8.2  lukem 
   1552  1.1.1.1.8.2  lukem     /*
   1553  1.1.1.1.8.2  lukem      * Fully expand all decompositions before generating the output file.
   1554  1.1.1.1.8.2  lukem      */
   1555  1.1.1.1.8.2  lukem     expand_decomp();
   1556  1.1.1.1.8.2  lukem 
   1557  1.1.1.1.8.2  lukem #if HARDCODE_DATA
   1558  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _ucdcmp_size = %ld;\n\n",
   1559  1.1.1.1.8.2  lukem         decomps_used * 2L);
   1560  1.1.1.1.8.2  lukem 
   1561  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _ucdcmp_nodes[] = {");
   1562  1.1.1.1.8.2  lukem 
   1563  1.1.1.1.8.2  lukem     if (decomps_used) {
   1564  1.1.1.1.8.2  lukem 	/*
   1565  1.1.1.1.8.2  lukem 	 * Write the list of decomp nodes.
   1566  1.1.1.1.8.2  lukem 	 */
   1567  1.1.1.1.8.2  lukem 	for (i = idx = 0; i < decomps_used; i++) {
   1568  1.1.1.1.8.2  lukem 	    fprintf(out, "\n\t0x%08lx, 0x%08lx,",
   1569  1.1.1.1.8.2  lukem 	        (unsigned long) decomps[i].code, (unsigned long) idx);
   1570  1.1.1.1.8.2  lukem 	    idx += decomps[i].used;
   1571  1.1.1.1.8.2  lukem 	}
   1572  1.1.1.1.8.2  lukem 
   1573  1.1.1.1.8.2  lukem 	/*
   1574  1.1.1.1.8.2  lukem 	 * Write the sentinel index as the last decomp node.
   1575  1.1.1.1.8.2  lukem 	 */
   1576  1.1.1.1.8.2  lukem 	fprintf(out, "\n\t0x%08lx\n};\n\n", (unsigned long) idx);
   1577  1.1.1.1.8.2  lukem 
   1578  1.1.1.1.8.2  lukem 	fprintf(out, PREF "ac_uint4 _ucdcmp_decomp[] = {");
   1579  1.1.1.1.8.2  lukem 	/*
   1580  1.1.1.1.8.2  lukem 	 * Write the decompositions themselves.
   1581  1.1.1.1.8.2  lukem 	 */
   1582  1.1.1.1.8.2  lukem 	k = 0;
   1583  1.1.1.1.8.2  lukem 	for (i = 0; i < decomps_used; i++)
   1584  1.1.1.1.8.2  lukem 	  for (j=0; j<decomps[i].used; j++) {
   1585  1.1.1.1.8.2  lukem 	    if (k) fprintf(out, ",");
   1586  1.1.1.1.8.2  lukem 	    if (!(k&3)) fprintf(out,"\n\t");
   1587  1.1.1.1.8.2  lukem 	    else fprintf(out, " ");
   1588  1.1.1.1.8.2  lukem 	    k++;
   1589  1.1.1.1.8.2  lukem 	    fprintf(out, "0x%08lx", (unsigned long) decomps[i].decomp[j]);
   1590  1.1.1.1.8.2  lukem 	  }
   1591  1.1.1.1.8.2  lukem 	fprintf(out, "\n};\n\n");
   1592  1.1.1.1.8.2  lukem     }
   1593  1.1.1.1.8.2  lukem #else
   1594  1.1.1.1.8.2  lukem     /*
   1595  1.1.1.1.8.2  lukem      * Open the decomp.dat file.
   1596  1.1.1.1.8.2  lukem      */
   1597  1.1.1.1.8.2  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "decomp.dat", opath);
   1598  1.1.1.1.8.2  lukem     if ((out = fopen(path, "wb")) == 0)
   1599  1.1.1.1.8.2  lukem       return;
   1600  1.1.1.1.8.2  lukem 
   1601  1.1.1.1.8.2  lukem     hdr[1] = decomps_used;
   1602  1.1.1.1.8.2  lukem 
   1603  1.1.1.1.8.2  lukem     /*
   1604  1.1.1.1.8.2  lukem      * Write the header.
   1605  1.1.1.1.8.2  lukem      */
   1606  1.1.1.1.8.2  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1607  1.1.1.1.8.2  lukem 
   1608  1.1.1.1.8.2  lukem     /*
   1609  1.1.1.1.8.2  lukem      * Write a temporary byte count which will be calculated as the
   1610  1.1.1.1.8.2  lukem      * decompositions are written out.
   1611  1.1.1.1.8.2  lukem      */
   1612  1.1.1.1.8.2  lukem     bytes = 0;
   1613  1.1.1.1.8.2  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1614  1.1.1.1.8.2  lukem 
   1615  1.1.1.1.8.2  lukem     if (decomps_used) {
   1616  1.1.1.1.8.2  lukem         /*
   1617  1.1.1.1.8.2  lukem          * Write the list of decomp nodes.
   1618  1.1.1.1.8.2  lukem          */
   1619  1.1.1.1.8.2  lukem         for (i = idx = 0; i < decomps_used; i++) {
   1620  1.1.1.1.8.2  lukem             fwrite((char *) &decomps[i].code, sizeof(ac_uint4), 1, out);
   1621  1.1.1.1.8.2  lukem             fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
   1622  1.1.1.1.8.2  lukem             idx += decomps[i].used;
   1623  1.1.1.1.8.2  lukem         }
   1624  1.1.1.1.8.2  lukem 
   1625  1.1.1.1.8.2  lukem         /*
   1626  1.1.1.1.8.2  lukem          * Write the sentinel index as the last decomp node.
   1627  1.1.1.1.8.2  lukem          */
   1628  1.1.1.1.8.2  lukem         fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
   1629  1.1.1.1.8.2  lukem 
   1630  1.1.1.1.8.2  lukem         /*
   1631  1.1.1.1.8.2  lukem          * Write the decompositions themselves.
   1632  1.1.1.1.8.2  lukem          */
   1633  1.1.1.1.8.2  lukem         for (i = 0; i < decomps_used; i++)
   1634  1.1.1.1.8.2  lukem           fwrite((char *) decomps[i].decomp, sizeof(ac_uint4),
   1635  1.1.1.1.8.2  lukem                  decomps[i].used, out);
   1636  1.1.1.1.8.2  lukem 
   1637  1.1.1.1.8.2  lukem         /*
   1638  1.1.1.1.8.2  lukem          * Seek back to the beginning and write the byte count.
   1639  1.1.1.1.8.2  lukem          */
   1640  1.1.1.1.8.2  lukem         bytes = (sizeof(ac_uint4) * idx) +
   1641  1.1.1.1.8.2  lukem             (sizeof(ac_uint4) * ((hdr[1] << 1) + 1));
   1642  1.1.1.1.8.2  lukem         fseek(out, sizeof(ac_uint2) << 1, 0L);
   1643  1.1.1.1.8.2  lukem         fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1644  1.1.1.1.8.2  lukem 
   1645  1.1.1.1.8.2  lukem         fclose(out);
   1646  1.1.1.1.8.2  lukem     }
   1647  1.1.1.1.8.2  lukem #endif
   1648  1.1.1.1.8.2  lukem 
   1649  1.1.1.1.8.2  lukem #ifdef HARDCODE_DATA
   1650  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _uckdcmp_size = %ld;\n\n",
   1651  1.1.1.1.8.2  lukem         kdecomps_used * 2L);
   1652  1.1.1.1.8.2  lukem 
   1653  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _uckdcmp_nodes[] = {");
   1654  1.1.1.1.8.2  lukem 
   1655  1.1.1.1.8.2  lukem     if (kdecomps_used) {
   1656  1.1.1.1.8.2  lukem 	/*
   1657  1.1.1.1.8.2  lukem 	 * Write the list of kdecomp nodes.
   1658  1.1.1.1.8.2  lukem 	 */
   1659  1.1.1.1.8.2  lukem 	for (i = idx = 0; i < kdecomps_used; i++) {
   1660  1.1.1.1.8.2  lukem 	    fprintf(out, "\n\t0x%08lx, 0x%08lx,",
   1661  1.1.1.1.8.2  lukem 	        (unsigned long) kdecomps[i].code, (unsigned long) idx);
   1662  1.1.1.1.8.2  lukem 	    idx += kdecomps[i].used;
   1663  1.1.1.1.8.2  lukem 	}
   1664  1.1.1.1.8.2  lukem 
   1665  1.1.1.1.8.2  lukem 	/*
   1666  1.1.1.1.8.2  lukem 	 * Write the sentinel index as the last decomp node.
   1667  1.1.1.1.8.2  lukem 	 */
   1668  1.1.1.1.8.2  lukem 	fprintf(out, "\n\t0x%08lx\n};\n\n", (unsigned long) idx);
   1669  1.1.1.1.8.2  lukem 
   1670  1.1.1.1.8.2  lukem 	fprintf(out, PREF "ac_uint4 _uckdcmp_decomp[] = {");
   1671  1.1.1.1.8.2  lukem 
   1672  1.1.1.1.8.2  lukem 	/*
   1673  1.1.1.1.8.2  lukem 	 * Write the decompositions themselves.
   1674  1.1.1.1.8.2  lukem 	 */
   1675  1.1.1.1.8.2  lukem 	k = 0;
   1676  1.1.1.1.8.2  lukem 	for (i = 0; i < kdecomps_used; i++)
   1677  1.1.1.1.8.2  lukem 	  for (j=0; j<kdecomps[i].used; j++) {
   1678  1.1.1.1.8.2  lukem 	    if (k) fprintf(out, ",");
   1679  1.1.1.1.8.2  lukem 	    if (!(k&3)) fprintf(out,"\n\t");
   1680  1.1.1.1.8.2  lukem 	    else fprintf(out, " ");
   1681  1.1.1.1.8.2  lukem 	    k++;
   1682  1.1.1.1.8.2  lukem 	    fprintf(out, "0x%08lx", (unsigned long) kdecomps[i].decomp[j]);
   1683  1.1.1.1.8.2  lukem 	  }
   1684  1.1.1.1.8.2  lukem 	fprintf(out, "\n};\n\n");
   1685  1.1.1.1.8.2  lukem     }
   1686  1.1.1.1.8.2  lukem #else
   1687  1.1.1.1.8.2  lukem     /*
   1688  1.1.1.1.8.2  lukem      * Open the kdecomp.dat file.
   1689  1.1.1.1.8.2  lukem      */
   1690  1.1.1.1.8.2  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "kdecomp.dat", opath);
   1691  1.1.1.1.8.2  lukem     if ((out = fopen(path, "wb")) == 0)
   1692  1.1.1.1.8.2  lukem       return;
   1693  1.1.1.1.8.2  lukem 
   1694  1.1.1.1.8.2  lukem     hdr[1] = kdecomps_used;
   1695  1.1.1.1.8.2  lukem 
   1696  1.1.1.1.8.2  lukem     /*
   1697  1.1.1.1.8.2  lukem      * Write the header.
   1698  1.1.1.1.8.2  lukem      */
   1699  1.1.1.1.8.2  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1700  1.1.1.1.8.2  lukem 
   1701  1.1.1.1.8.2  lukem     /*
   1702  1.1.1.1.8.2  lukem      * Write a temporary byte count which will be calculated as the
   1703  1.1.1.1.8.2  lukem      * decompositions are written out.
   1704  1.1.1.1.8.2  lukem      */
   1705  1.1.1.1.8.2  lukem     bytes = 0;
   1706  1.1.1.1.8.2  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1707  1.1.1.1.8.2  lukem 
   1708  1.1.1.1.8.2  lukem     if (kdecomps_used) {
   1709  1.1.1.1.8.2  lukem         /*
   1710  1.1.1.1.8.2  lukem          * Write the list of kdecomp nodes.
   1711  1.1.1.1.8.2  lukem          */
   1712  1.1.1.1.8.2  lukem         for (i = idx = 0; i < kdecomps_used; i++) {
   1713  1.1.1.1.8.2  lukem             fwrite((char *) &kdecomps[i].code, sizeof(ac_uint4), 1, out);
   1714  1.1.1.1.8.2  lukem             fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
   1715  1.1.1.1.8.2  lukem             idx += kdecomps[i].used;
   1716  1.1.1.1.8.2  lukem         }
   1717  1.1.1.1.8.2  lukem 
   1718  1.1.1.1.8.2  lukem         /*
   1719  1.1.1.1.8.2  lukem          * Write the sentinel index as the last decomp node.
   1720  1.1.1.1.8.2  lukem          */
   1721  1.1.1.1.8.2  lukem         fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
   1722  1.1.1.1.8.2  lukem 
   1723  1.1.1.1.8.2  lukem         /*
   1724  1.1.1.1.8.2  lukem          * Write the decompositions themselves.
   1725  1.1.1.1.8.2  lukem          */
   1726  1.1.1.1.8.2  lukem         for (i = 0; i < kdecomps_used; i++)
   1727  1.1.1.1.8.2  lukem           fwrite((char *) kdecomps[i].decomp, sizeof(ac_uint4),
   1728  1.1.1.1.8.2  lukem                  kdecomps[i].used, out);
   1729  1.1.1.1.8.2  lukem 
   1730  1.1.1.1.8.2  lukem         /*
   1731  1.1.1.1.8.2  lukem          * Seek back to the beginning and write the byte count.
   1732  1.1.1.1.8.2  lukem          */
   1733  1.1.1.1.8.2  lukem         bytes = (sizeof(ac_uint4) * idx) +
   1734  1.1.1.1.8.2  lukem             (sizeof(ac_uint4) * ((hdr[1] << 1) + 1));
   1735  1.1.1.1.8.2  lukem         fseek(out, sizeof(ac_uint2) << 1, 0L);
   1736  1.1.1.1.8.2  lukem         fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1737  1.1.1.1.8.2  lukem 
   1738  1.1.1.1.8.2  lukem         fclose(out);
   1739  1.1.1.1.8.2  lukem     }
   1740  1.1.1.1.8.2  lukem #endif
   1741  1.1.1.1.8.2  lukem 
   1742  1.1.1.1.8.2  lukem     /*****************************************************************
   1743  1.1.1.1.8.2  lukem      *
   1744  1.1.1.1.8.2  lukem      * Generate the combining class data.
   1745  1.1.1.1.8.2  lukem      *
   1746  1.1.1.1.8.2  lukem      *****************************************************************/
   1747  1.1.1.1.8.2  lukem #ifdef HARDCODE_DATA
   1748  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _uccmcl_size = %ld;\n\n", (long) ccl_used);
   1749  1.1.1.1.8.2  lukem 
   1750  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _uccmcl_nodes[] = {");
   1751  1.1.1.1.8.2  lukem 
   1752  1.1.1.1.8.2  lukem     if (ccl_used > 0) {
   1753  1.1.1.1.8.2  lukem 	/*
   1754  1.1.1.1.8.2  lukem 	 * Write the combining class ranges out.
   1755  1.1.1.1.8.2  lukem 	 */
   1756  1.1.1.1.8.2  lukem 	for (i = 0; i<ccl_used; i++) {
   1757  1.1.1.1.8.2  lukem 	    if (i) fprintf(out, ",");
   1758  1.1.1.1.8.2  lukem 	    if (!(i&3)) fprintf(out, "\n\t");
   1759  1.1.1.1.8.2  lukem 	    else fprintf(out, " ");
   1760  1.1.1.1.8.2  lukem 	    fprintf(out, "0x%08lx", (unsigned long) ccl[i]);
   1761  1.1.1.1.8.2  lukem 	}
   1762  1.1.1.1.8.2  lukem     } else {
   1763  1.1.1.1.8.2  lukem 	fprintf(out, "\t0");
   1764  1.1.1.1.8.2  lukem     }
   1765  1.1.1.1.8.2  lukem     fprintf(out, "\n};\n\n");
   1766  1.1.1.1.8.2  lukem #else
   1767  1.1.1.1.8.2  lukem     /*
   1768  1.1.1.1.8.2  lukem      * Open the cmbcl.dat file.
   1769  1.1.1.1.8.2  lukem      */
   1770  1.1.1.1.8.2  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "cmbcl.dat", opath);
   1771  1.1.1.1.8.2  lukem     if ((out = fopen(path, "wb")) == 0)
   1772  1.1.1.1.8.2  lukem       return;
   1773  1.1.1.1.8.2  lukem 
   1774  1.1.1.1.8.2  lukem     /*
   1775  1.1.1.1.8.2  lukem      * Set the number of ranges used.  Each range has a combining class which
   1776  1.1.1.1.8.2  lukem      * means each entry is a 3-tuple.
   1777  1.1.1.1.8.2  lukem      */
   1778  1.1.1.1.8.2  lukem     hdr[1] = ccl_used / 3;
   1779  1.1.1.1.8.2  lukem 
   1780  1.1.1.1.8.2  lukem     /*
   1781  1.1.1.1.8.2  lukem      * Write the header.
   1782  1.1.1.1.8.2  lukem      */
   1783  1.1.1.1.8.2  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1784  1.1.1.1.8.2  lukem 
   1785  1.1.1.1.8.2  lukem     /*
   1786  1.1.1.1.8.2  lukem      * Write out the byte count to maintain header size.
   1787  1.1.1.1.8.2  lukem      */
   1788  1.1.1.1.8.2  lukem     bytes = ccl_used * sizeof(ac_uint4);
   1789  1.1.1.1.8.2  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1790  1.1.1.1.8.2  lukem 
   1791  1.1.1.1.8.2  lukem     if (ccl_used > 0)
   1792  1.1.1.1.8.2  lukem       /*
   1793  1.1.1.1.8.2  lukem        * Write the combining class ranges out.
   1794  1.1.1.1.8.2  lukem        */
   1795  1.1.1.1.8.2  lukem       fwrite((char *) ccl, sizeof(ac_uint4), ccl_used, out);
   1796  1.1.1.1.8.2  lukem 
   1797  1.1.1.1.8.2  lukem     fclose(out);
   1798  1.1.1.1.8.2  lukem #endif
   1799  1.1.1.1.8.2  lukem 
   1800  1.1.1.1.8.2  lukem     /*****************************************************************
   1801  1.1.1.1.8.2  lukem      *
   1802  1.1.1.1.8.2  lukem      * Generate the number data.
   1803  1.1.1.1.8.2  lukem      *
   1804  1.1.1.1.8.2  lukem      *****************************************************************/
   1805  1.1.1.1.8.2  lukem 
   1806  1.1.1.1.8.2  lukem #if HARDCODE_DATA
   1807  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _ucnum_size = %lu;\n\n",
   1808  1.1.1.1.8.2  lukem         (unsigned long)ncodes_used<<1);
   1809  1.1.1.1.8.2  lukem 
   1810  1.1.1.1.8.2  lukem     fprintf(out, PREF "ac_uint4 _ucnum_nodes[] = {");
   1811  1.1.1.1.8.2  lukem 
   1812  1.1.1.1.8.2  lukem     /*
   1813  1.1.1.1.8.2  lukem      * Now, if number mappings exist, write them out.
   1814  1.1.1.1.8.2  lukem      */
   1815  1.1.1.1.8.2  lukem     if (ncodes_used > 0) {
   1816  1.1.1.1.8.2  lukem 	for (i = 0; i<ncodes_used; i++) {
   1817  1.1.1.1.8.2  lukem 	    if (i) fprintf(out, ",");
   1818  1.1.1.1.8.2  lukem 	    if (!(i&1)) fprintf(out, "\n\t");
   1819  1.1.1.1.8.2  lukem 	    else fprintf(out, " ");
   1820  1.1.1.1.8.2  lukem 	    fprintf(out, "0x%08lx, 0x%08lx",
   1821  1.1.1.1.8.2  lukem 	        (unsigned long) ncodes[i].code, (unsigned long) ncodes[i].idx);
   1822  1.1.1.1.8.2  lukem 	}
   1823  1.1.1.1.8.2  lukem 	fprintf(out, "\n};\n\n");
   1824  1.1.1.1.8.2  lukem 
   1825  1.1.1.1.8.2  lukem 	fprintf(out, PREF "short _ucnum_vals[] = {");
   1826  1.1.1.1.8.2  lukem 	for (i = 0; i<nums_used; i++) {
   1827  1.1.1.1.8.2  lukem 	    if (i) fprintf(out, ",");
   1828  1.1.1.1.8.2  lukem 	    if (!(i&3)) fprintf(out, "\n\t");
   1829  1.1.1.1.8.2  lukem 	    else fprintf(out, " ");
   1830  1.1.1.1.8.2  lukem 	    if (nums[i].numerator < 0) {
   1831  1.1.1.1.8.2  lukem 		fprintf(out, "%6d, 0x%04x",
   1832  1.1.1.1.8.2  lukem 		  nums[i].numerator, nums[i].denominator);
   1833  1.1.1.1.8.2  lukem 	    } else {
   1834  1.1.1.1.8.2  lukem 		fprintf(out, "0x%04x, 0x%04x",
   1835  1.1.1.1.8.2  lukem 		  nums[i].numerator, nums[i].denominator);
   1836  1.1.1.1.8.2  lukem 	    }
   1837  1.1.1.1.8.2  lukem 	}
   1838  1.1.1.1.8.2  lukem 	fprintf(out, "\n};\n\n");
   1839  1.1.1.1.8.2  lukem     }
   1840  1.1.1.1.8.2  lukem #else
   1841  1.1.1.1.8.2  lukem     /*
   1842  1.1.1.1.8.2  lukem      * Open the num.dat file.
   1843  1.1.1.1.8.2  lukem      */
   1844  1.1.1.1.8.2  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "num.dat", opath);
   1845  1.1.1.1.8.2  lukem     if ((out = fopen(path, "wb")) == 0)
   1846  1.1.1.1.8.2  lukem       return;
   1847  1.1.1.1.8.2  lukem 
   1848  1.1.1.1.8.2  lukem     /*
   1849  1.1.1.1.8.2  lukem      * The count part of the header will be the total number of codes that
   1850  1.1.1.1.8.2  lukem      * have numbers.
   1851  1.1.1.1.8.2  lukem      */
   1852  1.1.1.1.8.2  lukem     hdr[1] = (ac_uint2) (ncodes_used << 1);
   1853  1.1.1.1.8.2  lukem     bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
   1854  1.1.1.1.8.2  lukem 
   1855  1.1.1.1.8.2  lukem     /*
   1856  1.1.1.1.8.2  lukem      * Write the header.
   1857  1.1.1.1.8.2  lukem      */
   1858  1.1.1.1.8.2  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1859  1.1.1.1.8.2  lukem 
   1860  1.1.1.1.8.2  lukem     /*
   1861  1.1.1.1.8.2  lukem      * Write out the byte count to maintain header size.
   1862  1.1.1.1.8.2  lukem      */
   1863  1.1.1.1.8.2  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1864  1.1.1.1.8.2  lukem 
   1865  1.1.1.1.8.2  lukem     /*
   1866  1.1.1.1.8.2  lukem      * Now, if number mappings exist, write them out.
   1867  1.1.1.1.8.2  lukem      */
   1868  1.1.1.1.8.2  lukem     if (ncodes_used > 0) {
   1869  1.1.1.1.8.2  lukem         fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
   1870  1.1.1.1.8.2  lukem         fwrite((char *) nums, sizeof(_num_t), nums_used, out);
   1871  1.1.1.1.8.2  lukem     }
   1872  1.1.1.1.8.2  lukem #endif
   1873  1.1.1.1.8.2  lukem 
   1874  1.1.1.1.8.2  lukem     fclose(out);
   1875  1.1.1.1.8.2  lukem }
   1876  1.1.1.1.8.2  lukem 
   1877  1.1.1.1.8.2  lukem static void
   1878  1.1.1.1.8.2  lukem usage(char *prog)
   1879  1.1.1.1.8.2  lukem {
   1880  1.1.1.1.8.2  lukem     fprintf(stderr,
   1881  1.1.1.1.8.2  lukem             "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
   1882  1.1.1.1.8.2  lukem     fprintf(stderr, " datafile1 datafile2 ...\n\n");
   1883  1.1.1.1.8.2  lukem     fprintf(stderr,
   1884  1.1.1.1.8.2  lukem             "-o output-directory\n\t\tWrite the output files to a different");
   1885  1.1.1.1.8.2  lukem     fprintf(stderr, " directory (default: .).\n");
   1886  1.1.1.1.8.2  lukem     fprintf(stderr,
   1887  1.1.1.1.8.2  lukem             "-x composition-exclusion\n\t\tFile of composition codes");
   1888  1.1.1.1.8.2  lukem     fprintf(stderr, " that should be excluded.\n");
   1889  1.1.1.1.8.2  lukem     exit(1);
   1890  1.1.1.1.8.2  lukem }
   1891  1.1.1.1.8.2  lukem 
   1892  1.1.1.1.8.2  lukem int
   1893  1.1.1.1.8.2  lukem main(int argc, char *argv[])
   1894  1.1.1.1.8.2  lukem {
   1895  1.1.1.1.8.2  lukem     FILE *in;
   1896  1.1.1.1.8.2  lukem     char *prog, *opath;
   1897  1.1.1.1.8.2  lukem 
   1898  1.1.1.1.8.2  lukem     prog = lutil_progname( "ucgendat", argc, argv );
   1899  1.1.1.1.8.2  lukem 
   1900  1.1.1.1.8.2  lukem     opath = 0;
   1901  1.1.1.1.8.2  lukem     in = stdin;
   1902  1.1.1.1.8.2  lukem 
   1903  1.1.1.1.8.2  lukem     argc--;
   1904  1.1.1.1.8.2  lukem     argv++;
   1905  1.1.1.1.8.2  lukem 
   1906  1.1.1.1.8.2  lukem     while (argc > 0) {
   1907  1.1.1.1.8.2  lukem         if (argv[0][0] == '-') {
   1908  1.1.1.1.8.2  lukem             switch (argv[0][1]) {
   1909  1.1.1.1.8.2  lukem               case 'o':
   1910  1.1.1.1.8.2  lukem                 argc--;
   1911  1.1.1.1.8.2  lukem                 argv++;
   1912  1.1.1.1.8.2  lukem                 opath = argv[0];
   1913  1.1.1.1.8.2  lukem                 break;
   1914  1.1.1.1.8.2  lukem               case 'x':
   1915  1.1.1.1.8.2  lukem                 argc--;
   1916  1.1.1.1.8.2  lukem                 argv++;
   1917  1.1.1.1.8.2  lukem                 if ((in = fopen(argv[0], "r")) == 0)
   1918  1.1.1.1.8.2  lukem                   fprintf(stderr,
   1919  1.1.1.1.8.2  lukem                           "%s: unable to open composition exclusion file %s\n",
   1920  1.1.1.1.8.2  lukem                           prog, argv[0]);
   1921  1.1.1.1.8.2  lukem                 else {
   1922  1.1.1.1.8.2  lukem                     read_compexdata(in);
   1923  1.1.1.1.8.2  lukem                     fclose(in);
   1924  1.1.1.1.8.2  lukem                     in = 0;
   1925  1.1.1.1.8.2  lukem                 }
   1926  1.1.1.1.8.2  lukem                 break;
   1927  1.1.1.1.8.2  lukem               default:
   1928  1.1.1.1.8.2  lukem                 usage(prog);
   1929  1.1.1.1.8.2  lukem             }
   1930  1.1.1.1.8.2  lukem         } else {
   1931  1.1.1.1.8.2  lukem             if (in != stdin && in != NULL)
   1932  1.1.1.1.8.2  lukem               fclose(in);
   1933  1.1.1.1.8.2  lukem             if ((in = fopen(argv[0], "r")) == 0)
   1934  1.1.1.1.8.2  lukem               fprintf(stderr, "%s: unable to open ctype file %s\n",
   1935  1.1.1.1.8.2  lukem                       prog, argv[0]);
   1936  1.1.1.1.8.2  lukem             else {
   1937  1.1.1.1.8.2  lukem                 read_cdata(in);
   1938  1.1.1.1.8.2  lukem                 fclose(in);
   1939  1.1.1.1.8.2  lukem                 in = 0;
   1940  1.1.1.1.8.2  lukem 	    }
   1941  1.1.1.1.8.2  lukem         }
   1942  1.1.1.1.8.2  lukem         argc--;
   1943  1.1.1.1.8.2  lukem         argv++;
   1944  1.1.1.1.8.2  lukem     }
   1945  1.1.1.1.8.2  lukem 
   1946  1.1.1.1.8.2  lukem     if (opath == 0)
   1947  1.1.1.1.8.2  lukem       opath = ".";
   1948  1.1.1.1.8.2  lukem     write_cdata(opath);
   1949  1.1.1.1.8.2  lukem 
   1950  1.1.1.1.8.2  lukem     return 0;
   1951  1.1.1.1.8.2  lukem }
   1952