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