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