Back to index

php5  5.3.10
ucgendat.c
Go to the documentation of this file.
00001 /* Further modified for PHP */
00002 /* $Id: ucgendat.c 304057 2010-10-05 02:34:35Z cataphract $ */
00003 
00004 /* $OpenLDAP: pkg/ldap/libraries/liblunicode/ucdata/ucgendat.c,v 1.36.2.4 2007/01/02 21:43:51 kurt Exp $ */
00005 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00006  *
00007  * Copyright 1998-2007 The OpenLDAP Foundation.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted only as authorized by the OpenLDAP
00012  * Public License.
00013  *
00014  * A copy of this license is available at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 
00018 /* Copyright 2001 Computing Research Labs, New Mexico State University
00019  *
00020  * Permission is hereby granted, free of charge, to any person obtaining a
00021  * copy of this software and associated documentation files (the "Software"),
00022  * to deal in the Software without restriction, including without limitation
00023  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00024  * and/or sell copies of the Software, and to permit persons to whom the
00025  * Software is furnished to do so, subject to the following conditions:
00026  *
00027  * The above copyright notice and this permission notice shall be included in
00028  * all copies or substantial portions of the Software.
00029  *
00030  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00031  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00032  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00033  * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
00034  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
00035  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
00036  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  */
00038 /* orig Id: ucgendat.c,v 1.4 2001/01/02 18:46:20 mleisher Exp $" */
00039 
00040 #include <stdio.h>
00041 #include <ctype.h>
00042 #include <stdlib.h>
00043 #include <string.h>
00044 #include <unistd.h>
00045 
00046 #define ac_uint2 unsigned short
00047 #define ac_uint4 unsigned int
00048 #define LDAP_DIRSEP "/"
00049 #define AC_MEMCPY memcpy
00050 
00051 #ifndef HARDCODE_DATA
00052 #define       HARDCODE_DATA 1
00053 #endif
00054 
00055 #undef ishdigit
00056 #define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\
00057                       ((cc) >= 'A' && (cc) <= 'F') ||\
00058                       ((cc) >= 'a' && (cc) <= 'f'))
00059 
00060 /*
00061  * A header written to the output file with the byte-order-mark and the number
00062  * of property nodes.
00063  */
00064 static ac_uint2 hdr[2] = {0xfeff, 0};
00065 
00066 #define NUMPROPS 50
00067 #define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3)))
00068 
00069 typedef struct {
00070     char *name;
00071     int len;
00072 } _prop_t;
00073 
00074 /*
00075  * List of properties expected to be found in the Unicode Character Database
00076  * including some implementation specific properties.
00077  *
00078  * The implementation specific properties are:
00079  * Cm = Composed (can be decomposed)
00080  * Nb = Non-breaking
00081  * Sy = Symmetric (has left and right forms)
00082  * Hd = Hex digit
00083  * Qm = Quote marks
00084  * Mr = Mirroring
00085  * Ss = Space, other
00086  * Cp = Defined character
00087  */
00088 static _prop_t props[NUMPROPS] = {
00089     {"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2},
00090     {"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2},
00091     {"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2},
00092     {"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2},
00093     {"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L",  1}, {"R",  1},
00094     {"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B",  1},
00095     {"S",  1}, {"WS", 2}, {"ON", 2},
00096     {"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2},
00097     {"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2}, {"AL", 2}
00098 };
00099 
00100 typedef struct {
00101     ac_uint4 *ranges;
00102     ac_uint2 used;
00103     ac_uint2 size;
00104 } _ranges_t;
00105 
00106 static _ranges_t proptbl[NUMPROPS];
00107 
00108 /*
00109  * Make sure this array is sized to be on a 4-byte boundary at compile time.
00110  */
00111 static ac_uint2 propcnt[NEEDPROPS];
00112 
00113 /*
00114  * Array used to collect a decomposition before adding it to the decomposition
00115  * table.
00116  */
00117 static ac_uint4 dectmp[64];
00118 static ac_uint4 dectmp_size;
00119 
00120 typedef struct {
00121     ac_uint4 code;
00122     ac_uint2 size;
00123     ac_uint2 used;
00124     ac_uint4 *decomp;
00125 } _decomp_t;
00126 
00127 /*
00128  * List of decomposition.  Created and expanded in order as the characters are
00129  * encountered. First list contains canonical mappings, second also includes
00130  * compatibility mappings.
00131  */
00132 static _decomp_t *decomps;
00133 static ac_uint4 decomps_used;
00134 static ac_uint4 decomps_size;
00135 
00136 static _decomp_t *kdecomps;
00137 static ac_uint4 kdecomps_used;
00138 static ac_uint4 kdecomps_size;
00139 
00140 /*
00141  * Composition exclusion table stuff.
00142  */
00143 #define COMPEX_SET(c) (compexs[(c) >> 5] |= (1 << ((c) & 31)))
00144 #define COMPEX_TEST(c) (compexs[(c) >> 5] & (1 << ((c) & 31)))
00145 static ac_uint4 compexs[8192];
00146 
00147 /*
00148  * Struct for holding a composition pair, and array of composition pairs
00149  */
00150 typedef struct {
00151     ac_uint4 comp;
00152     ac_uint4 count;
00153     ac_uint4 code1;
00154     ac_uint4 code2;
00155 } _comp_t;
00156 
00157 #if 0
00158 static _comp_t *comps;
00159 #endif
00160 static ac_uint4 comps_used;
00161 
00162 /*
00163  * Types and lists for handling lists of case mappings.
00164  */
00165 typedef struct {
00166     ac_uint4 key;
00167     ac_uint4 other1;
00168     ac_uint4 other2;
00169 } _case_t;
00170 
00171 static _case_t *upper;
00172 static _case_t *lower;
00173 static _case_t *title;
00174 static ac_uint4 upper_used;
00175 static ac_uint4 upper_size;
00176 static ac_uint4 lower_used;
00177 static ac_uint4 lower_size;
00178 static ac_uint4 title_used;
00179 static ac_uint4 title_size;
00180 
00181 /*
00182  * Array used to collect case mappings before adding them to a list.
00183  */
00184 static ac_uint4 cases[3];
00185 
00186 /*
00187  * An array to hold ranges for combining classes.
00188  */
00189 static ac_uint4 *ccl;
00190 static ac_uint4 ccl_used;
00191 static ac_uint4 ccl_size;
00192 
00193 /*
00194  * Structures for handling numbers.
00195  */
00196 typedef struct {
00197     ac_uint4 code;
00198     ac_uint4 idx;
00199 } _codeidx_t;
00200 
00201 typedef struct {
00202     short numerator;
00203     short denominator;
00204 } _num_t;
00205 
00206 /*
00207  * Arrays to hold the mapping of codes to numbers.
00208  */
00209 static _codeidx_t *ncodes;
00210 static ac_uint4 ncodes_used;
00211 static ac_uint4 ncodes_size;
00212 
00213 static _num_t *nums;
00214 static ac_uint4 nums_used;
00215 static ac_uint4 nums_size;
00216 
00217 /*
00218  * Array for holding numbers.
00219  */
00220 static _num_t *nums;
00221 static ac_uint4 nums_used;
00222 static ac_uint4 nums_size;
00223 
00224 static void
00225 add_range(ac_uint4 start, ac_uint4 end, char *p1, char *p2)
00226 {
00227     int i, j, k, len;
00228     _ranges_t *rlp;
00229     char *name;
00230 
00231     for (k = 0; k < 2; k++) {
00232         if (k == 0) {
00233             name = p1;
00234             len = 2;
00235         } else {
00236             if (p2 == 0)
00237               break;
00238 
00239             name = p2;
00240             len = 1;
00241         }
00242 
00243         for (i = 0; i < NUMPROPS; i++) {
00244             if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
00245               break;
00246         }
00247 
00248         if (i == NUMPROPS)
00249           continue;
00250 
00251         rlp = &proptbl[i];
00252 
00253         /*
00254          * Resize the range list if necessary.
00255          */
00256         if (rlp->used == rlp->size) {
00257             if (rlp->size == 0)
00258               rlp->ranges = (ac_uint4 *)
00259                   malloc(sizeof(ac_uint4) << 3);
00260             else
00261               rlp->ranges = (ac_uint4 *)
00262                   realloc((char *) rlp->ranges,
00263                           sizeof(ac_uint4) * (rlp->size + 8));
00264             rlp->size += 8;
00265         }
00266 
00267         /*
00268          * If this is the first code for this property list, just add it
00269          * and return.
00270          */
00271         if (rlp->used == 0) {
00272             rlp->ranges[0] = start;
00273             rlp->ranges[1] = end;
00274             rlp->used += 2;
00275             continue;
00276         }
00277 
00278         /*
00279          * Optimize the case of adding the range to the end.
00280          */
00281         j = rlp->used - 1;
00282         if (start > rlp->ranges[j]) {
00283             j = rlp->used;
00284             rlp->ranges[j++] = start;
00285             rlp->ranges[j++] = end;
00286             rlp->used = j;
00287             continue;
00288         }
00289 
00290         /*
00291          * Need to locate the insertion point.
00292          */
00293         for (i = 0;
00294              i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
00295 
00296         /*
00297          * If the start value lies in the current range, then simply set the
00298          * new end point of the range to the end value passed as a parameter.
00299          */
00300         if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
00301             rlp->ranges[i + 1] = end;
00302             return;
00303         }
00304 
00305         /*
00306          * Shift following values up by two.
00307          */
00308         for (j = rlp->used; j > i; j -= 2) {
00309             rlp->ranges[j] = rlp->ranges[j - 2];
00310             rlp->ranges[j + 1] = rlp->ranges[j - 1];
00311         }
00312 
00313         /*
00314          * Add the new range at the insertion point.
00315          */
00316         rlp->ranges[i] = start;
00317         rlp->ranges[i + 1] = end;
00318         rlp->used += 2;
00319     }
00320 }
00321 
00322 static void
00323 ordered_range_insert(ac_uint4 c, char *name, int len)
00324 {
00325     int i, j;
00326     ac_uint4 s, e;
00327     _ranges_t *rlp;
00328 
00329     if (len == 0)
00330       return;
00331 
00332     /*
00333      * Deal with directionality codes introduced in Unicode 3.0.
00334      */
00335     if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
00336         (len == 3 &&
00337          (memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
00338           memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
00339           memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
00340         /*
00341          * Mark all of these as Other Neutral to preserve compatibility with
00342          * older versions.
00343          */
00344         len = 2;
00345         name = "ON";
00346     }
00347 
00348     for (i = 0; i < NUMPROPS; i++) {
00349         if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
00350           break;
00351     }
00352 
00353     if (i == NUMPROPS)
00354       return;
00355 
00356     /*
00357      * Have a match, so insert the code in order.
00358      */
00359     rlp = &proptbl[i];
00360 
00361     /*
00362      * Resize the range list if necessary.
00363      */
00364     if (rlp->used == rlp->size) {
00365         if (rlp->size == 0)
00366           rlp->ranges = (ac_uint4 *)
00367               malloc(sizeof(ac_uint4) << 3);
00368         else
00369           rlp->ranges = (ac_uint4 *)
00370               realloc((char *) rlp->ranges,
00371                       sizeof(ac_uint4) * (rlp->size + 8));
00372         rlp->size += 8;
00373     }
00374 
00375     /*
00376      * If this is the first code for this property list, just add it
00377      * and return.
00378      */
00379     if (rlp->used == 0) {
00380         rlp->ranges[0] = rlp->ranges[1] = c;
00381         rlp->used += 2;
00382         return;
00383     }
00384 
00385     /*
00386      * Optimize the cases of extending the last range and adding new ranges to
00387      * the end.
00388      */
00389     j = rlp->used - 1;
00390     e = rlp->ranges[j];
00391     s = rlp->ranges[j - 1];
00392 
00393     if (c == e + 1) {
00394         /*
00395          * Extend the last range.
00396          */
00397         rlp->ranges[j] = c;
00398         return;
00399     }
00400 
00401     if (c > e + 1) {
00402         /*
00403          * Start another range on the end.
00404          */
00405         j = rlp->used;
00406         rlp->ranges[j] = rlp->ranges[j + 1] = c;
00407         rlp->used += 2;
00408         return;
00409     }
00410 
00411     if (c >= s)
00412       /*
00413        * The code is a duplicate of a code in the last range, so just return.
00414        */
00415       return;
00416 
00417     /*
00418      * The code should be inserted somewhere before the last range in the
00419      * list.  Locate the insertion point.
00420      */
00421     for (i = 0;
00422          i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
00423 
00424     s = rlp->ranges[i];
00425     e = rlp->ranges[i + 1];
00426 
00427     if (c == e + 1)
00428       /*
00429        * Simply extend the current range.
00430        */
00431       rlp->ranges[i + 1] = c;
00432     else if (c < s) {
00433         /*
00434          * Add a new entry before the current location.  Shift all entries
00435          * before the current one up by one to make room.
00436          */
00437         for (j = rlp->used; j > i; j -= 2) {
00438             rlp->ranges[j] = rlp->ranges[j - 2];
00439             rlp->ranges[j + 1] = rlp->ranges[j - 1];
00440         }
00441         rlp->ranges[i] = rlp->ranges[i + 1] = c;
00442 
00443         rlp->used += 2;
00444     }
00445 }
00446 
00447 static void
00448 add_decomp(ac_uint4 code, short compat)
00449 {
00450     ac_uint4 i, j, size;
00451     _decomp_t **pdecomps;
00452     ac_uint4 *pdecomps_used;
00453     ac_uint4 *pdecomps_size;
00454 
00455     if (compat) {
00456        pdecomps = &kdecomps;
00457        pdecomps_used = &kdecomps_used;
00458        pdecomps_size = &kdecomps_size;
00459     } else {
00460        pdecomps = &decomps;
00461        pdecomps_used = &decomps_used;
00462        pdecomps_size = &decomps_size;
00463     }
00464     
00465     /*
00466      * Add the code to the composite property.
00467      */
00468     if (!compat) {
00469        ordered_range_insert(code, "Cm", 2);
00470     }
00471 
00472     /*
00473      * Locate the insertion point for the code.
00474      */
00475     for (i = 0; i < *pdecomps_used && code > (*pdecomps)[i].code; i++) ;
00476 
00477     /*
00478      * Allocate space for a new decomposition.
00479      */
00480     if (*pdecomps_used == *pdecomps_size) {
00481         if (*pdecomps_size == 0)
00482           *pdecomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
00483         else
00484           *pdecomps = (_decomp_t *)
00485               realloc((char *) *pdecomps,
00486                       sizeof(_decomp_t) * (*pdecomps_size + 8));
00487         (void) memset((char *) (*pdecomps + *pdecomps_size), '\0',
00488                       sizeof(_decomp_t) << 3);
00489         *pdecomps_size += 8;
00490     }
00491 
00492     if (i < *pdecomps_used && code != (*pdecomps)[i].code) {
00493         /*
00494          * Shift the decomps up by one if the codes don't match.
00495          */
00496         for (j = *pdecomps_used; j > i; j--)
00497           (void) AC_MEMCPY((char *) &(*pdecomps)[j], (char *) &(*pdecomps)[j - 1],
00498                         sizeof(_decomp_t));
00499     }
00500 
00501     /*
00502      * Insert or replace a decomposition.
00503      */
00504     size = dectmp_size + (4 - (dectmp_size & 3));
00505     if ((*pdecomps)[i].size < size) {
00506         if ((*pdecomps)[i].size == 0)
00507           (*pdecomps)[i].decomp = (ac_uint4 *)
00508               malloc(sizeof(ac_uint4) * size);
00509         else
00510           (*pdecomps)[i].decomp = (ac_uint4 *)
00511               realloc((char *) (*pdecomps)[i].decomp,
00512                       sizeof(ac_uint4) * size);
00513         (*pdecomps)[i].size = size;
00514     }
00515 
00516     if ((*pdecomps)[i].code != code)
00517       (*pdecomps_used)++;
00518 
00519     (*pdecomps)[i].code = code;
00520     (*pdecomps)[i].used = dectmp_size;
00521     (void) AC_MEMCPY((char *) (*pdecomps)[i].decomp, (char *) dectmp,
00522                   sizeof(ac_uint4) * dectmp_size);
00523 
00524     /*
00525      * NOTICE: This needs changing later so it is more general than simply
00526      * pairs.  This calculation is done here to simplify allocation elsewhere.
00527      */
00528     if (!compat && dectmp_size == 2)
00529       comps_used++;
00530 }
00531 
00532 static void
00533 add_title(ac_uint4 code)
00534 {
00535     ac_uint4 i, j;
00536 
00537     /*
00538      * Always map the code to itself.
00539      */
00540     cases[2] = code;
00541 
00542     if (title_used == title_size) {
00543         if (title_size == 0)
00544           title = (_case_t *) malloc(sizeof(_case_t) << 3);
00545         else
00546           title = (_case_t *) realloc((char *) title,
00547                                       sizeof(_case_t) * (title_size + 8));
00548         title_size += 8;
00549     }
00550 
00551     /*
00552      * Locate the insertion point.
00553      */
00554     for (i = 0; i < title_used && code > title[i].key; i++) ;
00555 
00556     if (i < title_used) {
00557         /*
00558          * Shift the array up by one.
00559          */
00560         for (j = title_used; j > i; j--)
00561           (void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
00562                         sizeof(_case_t));
00563     }
00564 
00565     title[i].key = cases[2];    /* Title */
00566     title[i].other1 = cases[0]; /* Upper */
00567     title[i].other2 = cases[1]; /* Lower */
00568 
00569     title_used++;
00570 }
00571 
00572 static void
00573 add_upper(ac_uint4 code)
00574 {
00575     ac_uint4 i, j;
00576 
00577     /*
00578      * Always map the code to itself.
00579      */
00580     cases[0] = code;
00581 
00582     /*
00583      * If the title case character is not present, then make it the same as
00584      * the upper case.
00585      */
00586     if (cases[2] == 0)
00587       cases[2] = code;
00588 
00589     if (upper_used == upper_size) {
00590         if (upper_size == 0)
00591           upper = (_case_t *) malloc(sizeof(_case_t) << 3);
00592         else
00593           upper = (_case_t *) realloc((char *) upper,
00594                                       sizeof(_case_t) * (upper_size + 8));
00595         upper_size += 8;
00596     }
00597 
00598     /*
00599      * Locate the insertion point.
00600      */
00601     for (i = 0; i < upper_used && code > upper[i].key; i++) ;
00602 
00603     if (i < upper_used) {
00604         /*
00605          * Shift the array up by one.
00606          */
00607         for (j = upper_used; j > i; j--)
00608           (void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
00609                         sizeof(_case_t));
00610     }
00611 
00612     upper[i].key = cases[0];    /* Upper */
00613     upper[i].other1 = cases[1]; /* Lower */
00614     upper[i].other2 = cases[2]; /* Title */
00615 
00616     upper_used++;
00617 }
00618 
00619 static void
00620 add_lower(ac_uint4 code)
00621 {
00622     ac_uint4 i, j;
00623 
00624     /*
00625      * Always map the code to itself.
00626      */
00627     cases[1] = code;
00628 
00629     /*
00630      * If the title case character is empty, then make it the same as the
00631      * upper case.
00632      */
00633     if (cases[2] == 0)
00634       cases[2] = cases[0];
00635 
00636     if (lower_used == lower_size) {
00637         if (lower_size == 0)
00638           lower = (_case_t *) malloc(sizeof(_case_t) << 3);
00639         else
00640           lower = (_case_t *) realloc((char *) lower,
00641                                       sizeof(_case_t) * (lower_size + 8));
00642         lower_size += 8;
00643     }
00644 
00645     /*
00646      * Locate the insertion point.
00647      */
00648     for (i = 0; i < lower_used && code > lower[i].key; i++) ;
00649 
00650     if (i < lower_used) {
00651         /*
00652          * Shift the array up by one.
00653          */
00654         for (j = lower_used; j > i; j--)
00655           (void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
00656                         sizeof(_case_t));
00657     }
00658 
00659     lower[i].key = cases[1];    /* Lower */
00660     lower[i].other1 = cases[0]; /* Upper */
00661     lower[i].other2 = cases[2]; /* Title */
00662 
00663     lower_used++;
00664 }
00665 
00666 static void
00667 ordered_ccl_insert(ac_uint4 c, ac_uint4 ccl_code)
00668 {
00669     ac_uint4 i, j;
00670 
00671     if (ccl_used == ccl_size) {
00672         if (ccl_size == 0)
00673           ccl = (ac_uint4 *) malloc(sizeof(ac_uint4) * 24);
00674         else
00675           ccl = (ac_uint4 *)
00676               realloc((char *) ccl, sizeof(ac_uint4) * (ccl_size + 24));
00677         ccl_size += 24;
00678     }
00679 
00680     /*
00681      * Optimize adding the first item.
00682      */
00683     if (ccl_used == 0) {
00684         ccl[0] = ccl[1] = c;
00685         ccl[2] = ccl_code;
00686         ccl_used += 3;
00687         return;
00688     }
00689 
00690     /*
00691      * Handle the special case of extending the range on the end.  This
00692      * requires that the combining class codes are the same.
00693      */
00694     if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
00695         ccl[ccl_used - 2] = c;
00696         return;
00697     }
00698 
00699     /*
00700      * Handle the special case of adding another range on the end.
00701      */
00702     if (c > ccl[ccl_used - 2] + 1 ||
00703         (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
00704         ccl[ccl_used++] = c;
00705         ccl[ccl_used++] = c;
00706         ccl[ccl_used++] = ccl_code;
00707         return;
00708     }
00709 
00710     /*
00711      * Locate either the insertion point or range for the code.
00712      */
00713     for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
00714 
00715     if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
00716         /*
00717          * Extend an existing range.
00718          */
00719         ccl[i + 1] = c;
00720         return;
00721     } else if (c < ccl[i]) {
00722         /*
00723          * Start a new range before the current location.
00724          */
00725         for (j = ccl_used; j > i; j -= 3) {
00726             ccl[j] = ccl[j - 3];
00727             ccl[j - 1] = ccl[j - 4];
00728             ccl[j - 2] = ccl[j - 5];
00729         }
00730         ccl[i] = ccl[i + 1] = c;
00731         ccl[i + 2] = ccl_code;
00732     }
00733 }
00734 
00735 /*
00736  * Adds a number if it does not already exist and returns an index value
00737  * multiplied by 2.
00738  */
00739 static ac_uint4
00740 make_number(short num, short denom)
00741 {
00742     ac_uint4 n;
00743 
00744     /*
00745      * Determine if the number already exists.
00746      */
00747     for (n = 0; n < nums_used; n++) {
00748         if (nums[n].numerator == num && nums[n].denominator == denom)
00749           return n << 1;
00750     }
00751 
00752     if (nums_used == nums_size) {
00753         if (nums_size == 0)
00754           nums = (_num_t *) malloc(sizeof(_num_t) << 3);
00755         else
00756           nums = (_num_t *) realloc((char *) nums,
00757                                     sizeof(_num_t) * (nums_size + 8));
00758         nums_size += 8;
00759     }
00760 
00761     n = nums_used++;
00762     nums[n].numerator = num;
00763     nums[n].denominator = denom;
00764 
00765     return n << 1;
00766 }
00767 
00768 static void
00769 add_number(ac_uint4 code, short num, short denom)
00770 {
00771     ac_uint4 i, j;
00772 
00773     /*
00774      * Insert the code in order.
00775      */
00776     for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
00777 
00778     /*
00779      * Handle the case of the codes matching and simply replace the number
00780      * that was there before.
00781      */
00782     if (i < ncodes_used && code == ncodes[i].code) {
00783         ncodes[i].idx = make_number(num, denom);
00784         return;
00785     }
00786 
00787     /*
00788      * Resize the array if necessary.
00789      */
00790     if (ncodes_used == ncodes_size) {
00791         if (ncodes_size == 0)
00792           ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
00793         else
00794           ncodes = (_codeidx_t *)
00795               realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
00796 
00797         ncodes_size += 8;
00798     }
00799 
00800     /*
00801      * Shift things around to insert the code if necessary.
00802      */
00803     if (i < ncodes_used) {
00804         for (j = ncodes_used; j > i; j--) {
00805             ncodes[j].code = ncodes[j - 1].code;
00806             ncodes[j].idx = ncodes[j - 1].idx;
00807         }
00808     }
00809     ncodes[i].code = code;
00810     ncodes[i].idx = make_number(num, denom);
00811 
00812     ncodes_used++;
00813 }
00814 
00815 /*
00816  * This routine assumes that the line is a valid Unicode Character Database
00817  * entry.
00818  */
00819 static void
00820 read_cdata(FILE *in)
00821 {
00822     ac_uint4 i, lineno, skip, code, ccl_code;
00823     short wnum, neg, number[2], compat;
00824     char line[512], *s, *e;
00825 
00826     lineno = skip = 0;
00827     while (fgets(line, sizeof(line), in)) {
00828        if( (s=strchr(line, '\n')) ) *s = '\0';
00829         lineno++;
00830 
00831         /*
00832          * Skip blank lines and lines that start with a '#'.
00833          */
00834         if (line[0] == 0 || line[0] == '#')
00835           continue;
00836 
00837         /*
00838          * If lines need to be skipped, do it here.
00839          */
00840         if (skip) {
00841             skip--;
00842             continue;
00843         }
00844 
00845         /*
00846          * Collect the code.  The code can be up to 6 hex digits in length to
00847          * allow surrogates to be specified.
00848          */
00849         for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
00850             code <<= 4;
00851             if (*s >= '0' && *s <= '9')
00852               code += *s - '0';
00853             else if (*s >= 'A' && *s <= 'F')
00854               code += (*s - 'A') + 10;
00855             else if (*s >= 'a' && *s <= 'f')
00856               code += (*s - 'a') + 10;
00857         }
00858 
00859         /*
00860          * Handle the following special cases:
00861          * 1. 4E00-9FA5 CJK Ideographs.
00862          * 2. AC00-D7A3 Hangul Syllables.
00863          * 3. D800-DFFF Surrogates.
00864          * 4. E000-F8FF Private Use Area.
00865          * 5. F900-FA2D Han compatibility.
00866         * ...Plus additional ranges in newer Unicode versions...
00867          */
00868         switch (code) {
00869          case 0x3400:
00870            /* CJK Ideograph Extension A */
00871             add_range(0x3400, 0x4db5, "Lo", "L");
00872 
00873             add_range(0x3400, 0x4db5, "Cp", 0);
00874 
00875            skip = 1;
00876            break;
00877           case 0x4e00:
00878             /*
00879              * The Han ideographs.
00880              */
00881             add_range(0x4e00, 0x9fff, "Lo", "L");
00882 
00883             /*
00884              * Add the characters to the defined category.
00885              */
00886             add_range(0x4e00, 0x9fa5, "Cp", 0);
00887 
00888             skip = 1;
00889             break;
00890           case 0xac00:
00891             /*
00892              * The Hangul syllables.
00893              */
00894             add_range(0xac00, 0xd7a3, "Lo", "L");
00895 
00896             /*
00897              * Add the characters to the defined category.
00898              */
00899             add_range(0xac00, 0xd7a3, "Cp", 0);
00900 
00901             skip = 1;
00902             break;
00903           case 0xd800:
00904             /*
00905              * Make a range of all surrogates and assume some default
00906              * properties.
00907              */
00908             add_range(0x010000, 0x10ffff, "Cs", "L");
00909             skip = 5;
00910             break;
00911           case 0xe000:
00912             /*
00913              * The Private Use area.  Add with a default set of properties.
00914              */
00915             add_range(0xe000, 0xf8ff, "Co", "L");
00916             skip = 1;
00917             break;
00918           case 0xf900:
00919             /*
00920              * The CJK compatibility area.
00921              */
00922             add_range(0xf900, 0xfaff, "Lo", "L");
00923 
00924             /*
00925              * Add the characters to the defined category.
00926              */
00927             add_range(0xf900, 0xfaff, "Cp", 0);
00928 
00929             skip = 1;
00930            break;
00931          case 0x20000:
00932            /* CJK Ideograph Extension B */
00933             add_range(0x20000, 0x2a6d6, "Lo", "L");
00934 
00935             add_range(0x20000, 0x2a6d6, "Cp", 0);
00936 
00937            skip = 1;
00938            break;
00939          case 0xf0000:
00940            /* Plane 15 private use */
00941            add_range(0xf0000, 0xffffd, "Co", "L");
00942            skip = 1;
00943            break;
00944 
00945          case 0x100000:
00946            /* Plane 16 private use */
00947            add_range(0x100000, 0x10fffd, "Co", "L");
00948            skip = 1;
00949            break;
00950         }
00951 
00952         if (skip)
00953           continue;
00954 
00955         /*
00956          * Add the code to the defined category.
00957          */
00958         ordered_range_insert(code, "Cp", 2);
00959 
00960         /*
00961          * Locate the first character property field.
00962          */
00963         for (i = 0; *s != 0 && i < 2; s++) {
00964             if (*s == ';')
00965               i++;
00966         }
00967         for (e = s; *e && *e != ';'; e++) ;
00968     
00969         ordered_range_insert(code, s, e - s);
00970 
00971         /*
00972          * Locate the combining class code.
00973          */
00974         for (s = e; *s != 0 && i < 3; s++) {
00975             if (*s == ';')
00976               i++;
00977         }
00978 
00979         /*
00980          * Convert the combining class code from decimal.
00981          */
00982         for (ccl_code = 0, e = s; *e && *e != ';'; e++)
00983           ccl_code = (ccl_code * 10) + (*e - '0');
00984 
00985         /*
00986          * Add the code if it not 0.
00987          */
00988         if (ccl_code != 0)
00989           ordered_ccl_insert(code, ccl_code);
00990 
00991         /*
00992          * Locate the second character property field.
00993          */
00994         for (s = e; *s != 0 && i < 4; s++) {
00995             if (*s == ';')
00996               i++;
00997         }
00998         for (e = s; *e && *e != ';'; e++) ;
00999 
01000         ordered_range_insert(code, s, e - s);
01001 
01002         /*
01003          * Check for a decomposition.
01004          */
01005         s = ++e;
01006         if (*s != ';') {
01007            compat = *s == '<';
01008            if (compat) {
01009               /*
01010                * Skip compatibility formatting tag.
01011                */
01012               while (*s++ != '>');
01013            }
01014             /*
01015              * Collect the codes of the decomposition.
01016              */
01017             for (dectmp_size = 0; *s != ';'; ) {
01018                 /*
01019                  * Skip all leading non-hex digits.
01020                  */
01021                 while (!ishdigit(*s))
01022                 s++;
01023 
01024                 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
01025                     dectmp[dectmp_size] <<= 4;
01026                     if (*s >= '0' && *s <= '9')
01027                       dectmp[dectmp_size] += *s - '0';
01028                     else if (*s >= 'A' && *s <= 'F')
01029                       dectmp[dectmp_size] += (*s - 'A') + 10;
01030                     else if (*s >= 'a' && *s <= 'f')
01031                       dectmp[dectmp_size] += (*s - 'a') + 10;
01032                 }
01033                 dectmp_size++;
01034             }
01035 
01036             /*
01037              * If there are any codes in the temporary decomposition array,
01038              * then add the character with its decomposition.
01039              */
01040             if (dectmp_size > 0) {
01041               if (!compat) {
01042                   add_decomp(code, 0);
01043               }
01044               add_decomp(code, 1);
01045            }
01046         }
01047 
01048         /*
01049          * Skip to the number field.
01050          */
01051         for (i = 0; i < 3 && *s; s++) {
01052             if (*s == ';')
01053               i++;
01054         }
01055 
01056         /*
01057          * Scan the number in.
01058          */
01059         number[0] = number[1] = 0;
01060         for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
01061             if (*e == '-') {
01062                 neg = 1;
01063                 continue;
01064             }
01065 
01066             if (*e == '/') {
01067                 /*
01068                  * Move the the denominator of the fraction.
01069                  */
01070                 if (neg)
01071                   number[wnum] *= -1;
01072                 neg = 0;
01073                 e++;
01074                 wnum++;
01075             }
01076             number[wnum] = (number[wnum] * 10) + (*e - '0');
01077         }
01078 
01079         if (e > s) {
01080             /*
01081              * Adjust the denominator in case of integers and add the number.
01082              */
01083             if (wnum == 0)
01084               number[1] = 1;
01085 
01086             add_number(code, number[0], number[1]);
01087         }
01088 
01089         /*
01090          * Skip to the start of the possible case mappings.
01091          */
01092         for (s = e, i = 0; i < 4 && *s; s++) {
01093             if (*s == ';')
01094               i++;
01095         }
01096 
01097         /*
01098          * Collect the case mappings.
01099          */
01100         cases[0] = cases[1] = cases[2] = 0;
01101         for (i = 0; i < 3; i++) {
01102             while (ishdigit(*s)) {
01103                 cases[i] <<= 4;
01104                 if (*s >= '0' && *s <= '9')
01105                   cases[i] += *s - '0';
01106                 else if (*s >= 'A' && *s <= 'F')
01107                   cases[i] += (*s - 'A') + 10;
01108                 else if (*s >= 'a' && *s <= 'f')
01109                   cases[i] += (*s - 'a') + 10;
01110                 s++;
01111             }
01112             if (*s == ';')
01113               s++;
01114         }
01115         if (cases[0] && cases[1])
01116           /*
01117            * Add the upper and lower mappings for a title case character.
01118            */
01119           add_title(code);
01120         else if (cases[1])
01121           /*
01122            * Add the lower and title case mappings for the upper case
01123            * character.
01124            */
01125           add_upper(code);
01126         else if (cases[0])
01127           /*
01128            * Add the upper and title case mappings for the lower case
01129            * character.
01130            */
01131           add_lower(code);
01132     }
01133 }
01134 
01135 #if 0
01136 
01137 static _decomp_t *
01138 find_decomp(ac_uint4 code, short compat)
01139 {
01140     long l, r, m;
01141     _decomp_t *decs;
01142     
01143     l = 0;
01144     r = (compat ? kdecomps_used : decomps_used) - 1;
01145     decs = compat ? kdecomps : decomps;
01146     while (l <= r) {
01147         m = (l + r) >> 1;
01148         if (code > decs[m].code)
01149           l = m + 1;
01150         else if (code < decs[m].code)
01151           r = m - 1;
01152         else
01153           return &decs[m];
01154     }
01155     return 0;
01156 }
01157 
01158 static void
01159 decomp_it(_decomp_t *d, short compat)
01160 {
01161     ac_uint4 i;
01162     _decomp_t *dp;
01163 
01164     for (i = 0; i < d->used; i++) {
01165         if ((dp = find_decomp(d->decomp[i], compat)) != 0)
01166           decomp_it(dp, compat);
01167         else
01168           dectmp[dectmp_size++] = d->decomp[i];
01169     }
01170 }
01171 
01172 
01173 /*
01174  * Expand all decompositions by recursively decomposing each character
01175  * in the decomposition.
01176  */
01177 static void
01178 expand_decomp(void)
01179 {
01180     ac_uint4 i;
01181 
01182     for (i = 0; i < decomps_used; i++) {
01183         dectmp_size = 0;
01184         decomp_it(&decomps[i], 0);
01185         if (dectmp_size > 0)
01186           add_decomp(decomps[i].code, 0);
01187     }
01188 
01189     for (i = 0; i < kdecomps_used; i++) {
01190         dectmp_size = 0;
01191         decomp_it(&kdecomps[i], 1);
01192         if (dectmp_size > 0)
01193           add_decomp(kdecomps[i].code, 1);
01194     }
01195 }
01196 
01197 static int
01198 cmpcomps(const void *v_comp1, const void *v_comp2)
01199 {
01200        const _comp_t *comp1 = v_comp1, *comp2 = v_comp2;
01201     long diff = comp1->code1 - comp2->code1;
01202 
01203     if (!diff)
01204        diff = comp1->code2 - comp2->code2;
01205     return (int) diff;
01206 }
01207 
01208 #endif
01209 
01210 /*
01211  * Load composition exclusion data
01212  */
01213 static void
01214 read_compexdata(FILE *in)
01215 {
01216     ac_uint2 i;
01217     ac_uint4 code;
01218     char line[512], *s;
01219 
01220     (void) memset((char *) compexs, 0, sizeof(compexs));
01221 
01222     while (fgets(line, sizeof(line), in)) {
01223        if( (s=strchr(line, '\n')) ) *s = '\0';
01224         /*
01225          * Skip blank lines and lines that start with a '#'.
01226          */
01227         if (line[0] == 0 || line[0] == '#')
01228            continue;
01229 
01230        /*
01231          * Collect the code.  Assume max 6 digits
01232          */
01233 
01234        for (s = line, i = code = 0; *s != '#' && i < 6; i++, s++) {
01235            if (isspace((unsigned char)*s)) break;
01236             code <<= 4;
01237             if (*s >= '0' && *s <= '9')
01238               code += *s - '0';
01239             else if (*s >= 'A' && *s <= 'F')
01240               code += (*s - 'A') + 10;
01241             else if (*s >= 'a' && *s <= 'f')
01242               code += (*s - 'a') + 10;
01243         }
01244         COMPEX_SET(code);
01245     }
01246 }
01247 
01248 #if 0
01249 
01250 /*
01251  * Creates array of compositions from decomposition array
01252  */
01253 static void
01254 create_comps(void)
01255 {
01256     ac_uint4 i, cu;
01257 
01258     comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
01259 
01260     for (i = cu = 0; i < decomps_used; i++) {
01261        if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
01262            continue;
01263        comps[cu].comp = decomps[i].code;
01264        comps[cu].count = 2;
01265        comps[cu].code1 = decomps[i].decomp[0];
01266        comps[cu].code2 = decomps[i].decomp[1];
01267        cu++;
01268     }
01269     comps_used = cu;
01270     qsort(comps, comps_used, sizeof(_comp_t), cmpcomps);
01271 }
01272 
01273 #endif
01274 
01275 #if HARDCODE_DATA
01276 static void
01277 write_case(FILE *out, _case_t *tab, int num, int first)
01278 {
01279     int i;
01280 
01281     for (i=0; i<num; i++) {
01282        if (first) first = 0;
01283        else fprintf(out, ",");
01284        fprintf(out, "\n\t0x%08lx, 0x%08lx, 0x%08lx",
01285               (unsigned long) tab[i].key, (unsigned long) tab[i].other1,
01286               (unsigned long) tab[i].other2);
01287     }
01288 }
01289 
01290 #define PREF "static const "
01291 
01292 #endif
01293 
01294 static void
01295 write_cdata(char *opath)
01296 {
01297     FILE *out;
01298        ac_uint4 bytes;
01299     ac_uint4 i, idx, nprops;
01300 #if !(HARDCODE_DATA)
01301     ac_uint2 casecnt[2];
01302 #endif
01303     char path[BUFSIZ];
01304 #if HARDCODE_DATA
01305     int j, k;
01306 
01307     /*****************************************************************
01308      *
01309      * Generate the ctype data.
01310      *
01311      *****************************************************************/
01312 
01313     /*
01314      * Open the output file.
01315      */
01316     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "uctable.h", opath);
01317     if ((out = fopen(path, "w")) == 0)
01318       return;
01319 #else
01320     /*
01321      * Open the ctype.dat file.
01322      */
01323     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "ctype.dat", opath);
01324     if ((out = fopen(path, "wb")) == 0)
01325       return;
01326 #endif
01327 
01328     /*
01329      * Collect the offsets for the properties.  The offsets array is
01330      * on a 4-byte boundary to keep things efficient for architectures
01331      * that need such a thing.
01332      */
01333     for (i = idx = 0; i < NUMPROPS; i++) {
01334         propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
01335         idx += proptbl[i].used;
01336     }
01337 
01338     /*
01339      * Add the sentinel index which is used by the binary search as the upper
01340      * bound for a search.
01341      */
01342     propcnt[i] = idx;
01343 
01344     /*
01345      * Record the actual number of property lists.  This may be different than
01346      * the number of offsets actually written because of aligning on a 4-byte
01347      * boundary.
01348      */
01349     hdr[1] = NUMPROPS;
01350 
01351     /*
01352      * Calculate the byte count needed and pad the property counts array to a
01353      * 4-byte boundary.
01354      */
01355     if ((bytes = sizeof(ac_uint2) * (NUMPROPS + 1)) & 3)
01356       bytes += 4 - (bytes & 3);
01357     nprops = bytes / sizeof(ac_uint2);
01358     bytes += sizeof(ac_uint4) * idx;
01359 
01360 #if HARDCODE_DATA
01361     fprintf(out,
01362         "/* This file was generated from a modified version UCData's ucgendat.\n"
01363         " *\n"
01364         " *                     DO NOT EDIT THIS FILE!\n"
01365         " * \n"
01366         " * Instead, compile ucgendat.c (bundled with PHP in ext/mbstring), download\n"
01367         " * the appropriate UnicodeData-x.x.x.txt and CompositionExclusions-x.x.x.txt\n"
01368         " * files from  http://www.unicode.org/Public/ and run this program.\n"
01369         " *\n"
01370         " * More information can be found in the UCData package. Unfortunately,\n"
01371         " * the project's page doesn't seem to be live anymore, so you can use\n"
01372         " * OpenLDAPs modified copy (look in libraries/liblunicode/ucdata) */\n\n");
01373 
01374     fprintf(out, PREF "unsigned short _ucprop_size = %d;\n\n", NUMPROPS);
01375 
01376     fprintf(out, PREF "unsigned short  _ucprop_offsets[] = {");
01377 
01378     for (i = 0; i<nprops; i++) {
01379        if (i) fprintf(out, ",");
01380        if (!(i&7)) fprintf(out, "\n\t");
01381        else fprintf(out, " ");
01382        fprintf(out, "0x%04x", propcnt[i]);
01383     }
01384     fprintf(out, "\n};\n\n");
01385 
01386     fprintf(out, PREF "unsigned int _ucprop_ranges[] = {");
01387 
01388     k = 0;
01389     for (i = 0; i < NUMPROPS; i++) {
01390        if (proptbl[i].used > 0) {
01391          for (j=0; j<proptbl[i].used; j++) {
01392            if (k) fprintf(out, ",");
01393            if (!(k&3)) fprintf(out,"\n\t");
01394            else fprintf(out, " ");
01395            k++;
01396            fprintf(out, "0x%08lx", (unsigned long) proptbl[i].ranges[j]);
01397          }
01398        }
01399     }
01400     fprintf(out, "\n};\n\n");
01401 #else
01402     /*
01403      * Write the header.
01404      */
01405     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
01406 
01407     /*
01408      * Write the byte count.
01409      */
01410     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
01411 
01412     /*
01413      * Write the property list counts.
01414      */
01415     fwrite((char *) propcnt, sizeof(ac_uint2), nprops, out);
01416 
01417     /*
01418      * Write the property lists.
01419      */
01420     for (i = 0; i < NUMPROPS; i++) {
01421         if (proptbl[i].used > 0)
01422           fwrite((char *) proptbl[i].ranges, sizeof(ac_uint4),
01423                  proptbl[i].used, out);
01424     }
01425 
01426     fclose(out);
01427 #endif
01428 
01429     /*****************************************************************
01430      *
01431      * Generate the case mapping data.
01432      *
01433      *****************************************************************/
01434 
01435 #if HARDCODE_DATA
01436     fprintf(out, PREF "unsigned int _uccase_size = %ld;\n\n",
01437         (long) (upper_used + lower_used + title_used));
01438 
01439     fprintf(out,
01440         "/* Starting indexes of the case tables\n"
01441         " * UpperIndex = 0\n"
01442         " * LowerIndex = _uccase_len[0]\n"
01443         " * TitleIndex = LowerIndex + _uccase_len[1] */\n\n");
01444     fprintf(out, PREF "unsigned short _uccase_len[2] = {%ld, %ld};\n\n",
01445         (long) upper_used * 3, (long) lower_used * 3);
01446     fprintf(out, PREF "unsigned int _uccase_map[] = {");
01447 
01448     if (upper_used > 0)
01449       /*
01450        * Write the upper case table.
01451        */
01452       write_case(out, upper, upper_used, 1);
01453 
01454     if (lower_used > 0)
01455       /*
01456        * Write the lower case table.
01457        */
01458       write_case(out, lower, lower_used, !upper_used);
01459 
01460     if (title_used > 0)
01461       /*
01462        * Write the title case table.
01463        */
01464       write_case(out, title, title_used, !(upper_used||lower_used));
01465 
01466     if (!(upper_used || lower_used || title_used))
01467        fprintf(out, "\t0");
01468 
01469     fprintf(out, "\n};\n\n");
01470 #else
01471     /*
01472      * Open the case.dat file.
01473      */
01474     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "case.dat", opath);
01475     if ((out = fopen(path, "wb")) == 0)
01476       return;
01477 
01478     /*
01479      * Write the case mapping tables.
01480      */
01481     hdr[1] = upper_used + lower_used + title_used;
01482     casecnt[0] = upper_used;
01483     casecnt[1] = lower_used;
01484 
01485     /*
01486      * Write the header.
01487      */
01488     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
01489 
01490     /*
01491      * Write the upper and lower case table sizes.
01492      */
01493     fwrite((char *) casecnt, sizeof(ac_uint2), 2, out);
01494 
01495     if (upper_used > 0)
01496       /*
01497        * Write the upper case table.
01498        */
01499       fwrite((char *) upper, sizeof(_case_t), upper_used, out);
01500 
01501     if (lower_used > 0)
01502       /*
01503        * Write the lower case table.
01504        */
01505       fwrite((char *) lower, sizeof(_case_t), lower_used, out);
01506 
01507     if (title_used > 0)
01508       /*
01509        * Write the title case table.
01510        */
01511       fwrite((char *) title, sizeof(_case_t), title_used, out);
01512 
01513     fclose(out);
01514 #endif
01515 
01516 #if 0
01517 
01518     /*****************************************************************
01519      *
01520      * Generate the composition data.
01521      *
01522      *****************************************************************/
01523     
01524     /*
01525      * Create compositions from decomposition data
01526      */
01527     create_comps();
01528     
01529 #if HARDCODE_DATA
01530     fprintf(out, PREF "ac_uint4 _uccomp_size = %ld;\n\n",
01531         comps_used * 4L);
01532 
01533     fprintf(out, PREF "ac_uint4 _uccomp_data[] = {");
01534 
01535      /*
01536       * Now, if comps exist, write them out.
01537       */
01538     if (comps_used > 0) {
01539        for (i=0; i<comps_used; i++) {
01540            if (i) fprintf(out, ",");
01541            fprintf(out, "\n\t0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx",
01542                (unsigned long) comps[i].comp, (unsigned long) comps[i].count,
01543                (unsigned long) comps[i].code1, (unsigned long) comps[i].code2);
01544        }
01545     } else {
01546        fprintf(out, "\t0");
01547     }
01548     fprintf(out, "\n};\n\n");
01549 #else
01550     /*
01551      * Open the comp.dat file.
01552      */
01553     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "comp.dat", opath);
01554     if ((out = fopen(path, "wb")) == 0)
01555        return;
01556     
01557     /*
01558      * Write the header.
01559      */
01560     hdr[1] = (ac_uint2) comps_used * 4;
01561     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
01562     
01563     /*
01564      * Write out the byte count to maintain header size.
01565      */
01566     bytes = comps_used * sizeof(_comp_t);
01567     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
01568     
01569     /*
01570      * Now, if comps exist, write them out.
01571      */
01572     if (comps_used > 0)
01573         fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
01574     
01575     fclose(out);
01576 #endif
01577     
01578     /*****************************************************************
01579      *
01580      * Generate the decomposition data.
01581      *
01582      *****************************************************************/
01583 
01584     /*
01585      * Fully expand all decompositions before generating the output file.
01586      */
01587     expand_decomp();
01588 
01589 #if HARDCODE_DATA
01590     fprintf(out, PREF "ac_uint4 _ucdcmp_size = %ld;\n\n",
01591         decomps_used * 2L);
01592 
01593     fprintf(out, PREF "ac_uint4 _ucdcmp_nodes[] = {");
01594 
01595     if (decomps_used) {
01596        /*
01597         * Write the list of decomp nodes.
01598         */
01599        for (i = idx = 0; i < decomps_used; i++) {
01600            fprintf(out, "\n\t0x%08lx, 0x%08lx,",
01601                (unsigned long) decomps[i].code, (unsigned long) idx);
01602            idx += decomps[i].used;
01603        }
01604 
01605        /*
01606         * Write the sentinel index as the last decomp node.
01607         */
01608        fprintf(out, "\n\t0x%08lx\n};\n\n", (unsigned long) idx);
01609 
01610        fprintf(out, PREF "ac_uint4 _ucdcmp_decomp[] = {");
01611        /*
01612         * Write the decompositions themselves.
01613         */
01614        k = 0;
01615        for (i = 0; i < decomps_used; i++)
01616          for (j=0; j<decomps[i].used; j++) {
01617            if (k) fprintf(out, ",");
01618            if (!(k&3)) fprintf(out,"\n\t");
01619            else fprintf(out, " ");
01620            k++;
01621            fprintf(out, "0x%08lx", (unsigned long) decomps[i].decomp[j]);
01622          }
01623        fprintf(out, "\n};\n\n");
01624     }
01625 #else
01626     /*
01627      * Open the decomp.dat file.
01628      */
01629     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "decomp.dat", opath);
01630     if ((out = fopen(path, "wb")) == 0)
01631       return;
01632 
01633     hdr[1] = decomps_used;
01634 
01635     /*
01636      * Write the header.
01637      */
01638     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
01639 
01640     /*
01641      * Write a temporary byte count which will be calculated as the
01642      * decompositions are written out.
01643      */
01644     bytes = 0;
01645     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
01646 
01647     if (decomps_used) {
01648         /*
01649          * Write the list of decomp nodes.
01650          */
01651         for (i = idx = 0; i < decomps_used; i++) {
01652             fwrite((char *) &decomps[i].code, sizeof(ac_uint4), 1, out);
01653             fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
01654             idx += decomps[i].used;
01655         }
01656 
01657         /*
01658          * Write the sentinel index as the last decomp node.
01659          */
01660         fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
01661 
01662         /*
01663          * Write the decompositions themselves.
01664          */
01665         for (i = 0; i < decomps_used; i++)
01666           fwrite((char *) decomps[i].decomp, sizeof(ac_uint4),
01667                  decomps[i].used, out);
01668 
01669         /*
01670          * Seek back to the beginning and write the byte count.
01671          */
01672         bytes = (sizeof(ac_uint4) * idx) +
01673             (sizeof(ac_uint4) * ((hdr[1] << 1) + 1));
01674         fseek(out, sizeof(ac_uint2) << 1, 0L);
01675         fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
01676 
01677         fclose(out);
01678     }
01679 #endif
01680 
01681 #ifdef HARDCODE_DATA
01682     fprintf(out, PREF "ac_uint4 _uckdcmp_size = %ld;\n\n",
01683         kdecomps_used * 2L);
01684 
01685     fprintf(out, PREF "ac_uint4 _uckdcmp_nodes[] = {");
01686 
01687     if (kdecomps_used) {
01688        /*
01689         * Write the list of kdecomp nodes.
01690         */
01691        for (i = idx = 0; i < kdecomps_used; i++) {
01692            fprintf(out, "\n\t0x%08lx, 0x%08lx,",
01693                (unsigned long) kdecomps[i].code, (unsigned long) idx);
01694            idx += kdecomps[i].used;
01695        }
01696 
01697        /*
01698         * Write the sentinel index as the last decomp node.
01699         */
01700        fprintf(out, "\n\t0x%08lx\n};\n\n", (unsigned long) idx);
01701 
01702        fprintf(out, PREF "ac_uint4 _uckdcmp_decomp[] = {");
01703 
01704        /*
01705         * Write the decompositions themselves.
01706         */
01707        k = 0;
01708        for (i = 0; i < kdecomps_used; i++)
01709          for (j=0; j<kdecomps[i].used; j++) {
01710            if (k) fprintf(out, ",");
01711            if (!(k&3)) fprintf(out,"\n\t");
01712            else fprintf(out, " ");
01713            k++;
01714            fprintf(out, "0x%08lx", (unsigned long) kdecomps[i].decomp[j]);
01715          }
01716        fprintf(out, "\n};\n\n");
01717     }
01718 #else
01719     /*
01720      * Open the kdecomp.dat file.
01721      */
01722     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "kdecomp.dat", opath);
01723     if ((out = fopen(path, "wb")) == 0)
01724       return;
01725 
01726     hdr[1] = kdecomps_used;
01727 
01728     /*
01729      * Write the header.
01730      */
01731     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
01732 
01733     /*
01734      * Write a temporary byte count which will be calculated as the
01735      * decompositions are written out.
01736      */
01737     bytes = 0;
01738     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
01739 
01740     if (kdecomps_used) {
01741         /*
01742          * Write the list of kdecomp nodes.
01743          */
01744         for (i = idx = 0; i < kdecomps_used; i++) {
01745             fwrite((char *) &kdecomps[i].code, sizeof(ac_uint4), 1, out);
01746             fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
01747             idx += kdecomps[i].used;
01748         }
01749 
01750         /*
01751          * Write the sentinel index as the last decomp node.
01752          */
01753         fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
01754 
01755         /*
01756          * Write the decompositions themselves.
01757          */
01758         for (i = 0; i < kdecomps_used; i++)
01759           fwrite((char *) kdecomps[i].decomp, sizeof(ac_uint4),
01760                  kdecomps[i].used, out);
01761 
01762         /*
01763          * Seek back to the beginning and write the byte count.
01764          */
01765         bytes = (sizeof(ac_uint4) * idx) +
01766             (sizeof(ac_uint4) * ((hdr[1] << 1) + 1));
01767         fseek(out, sizeof(ac_uint2) << 1, 0L);
01768         fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
01769 
01770         fclose(out);
01771     }
01772 #endif
01773 
01774     /*****************************************************************
01775      *
01776      * Generate the combining class data.
01777      *
01778      *****************************************************************/
01779 #ifdef HARDCODE_DATA
01780     fprintf(out, PREF "ac_uint4 _uccmcl_size = %ld;\n\n", (long) ccl_used);
01781 
01782     fprintf(out, PREF "ac_uint4 _uccmcl_nodes[] = {");
01783 
01784     if (ccl_used > 0) {
01785        /*
01786         * Write the combining class ranges out.
01787         */
01788        for (i = 0; i<ccl_used; i++) {
01789            if (i) fprintf(out, ",");
01790            if (!(i&3)) fprintf(out, "\n\t");
01791            else fprintf(out, " ");
01792            fprintf(out, "0x%08lx", (unsigned long) ccl[i]);
01793        }
01794     } else {
01795        fprintf(out, "\t0");
01796     }
01797     fprintf(out, "\n};\n\n");
01798 #else
01799     /*
01800      * Open the cmbcl.dat file.
01801      */
01802     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "cmbcl.dat", opath);
01803     if ((out = fopen(path, "wb")) == 0)
01804       return;
01805 
01806     /*
01807      * Set the number of ranges used.  Each range has a combining class which
01808      * means each entry is a 3-tuple.
01809      */
01810     hdr[1] = ccl_used / 3;
01811 
01812     /*
01813      * Write the header.
01814      */
01815     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
01816 
01817     /*
01818      * Write out the byte count to maintain header size.
01819      */
01820     bytes = ccl_used * sizeof(ac_uint4);
01821     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
01822 
01823     if (ccl_used > 0)
01824       /*
01825        * Write the combining class ranges out.
01826        */
01827       fwrite((char *) ccl, sizeof(ac_uint4), ccl_used, out);
01828 
01829     fclose(out);
01830 #endif
01831 
01832     /*****************************************************************
01833      *
01834      * Generate the number data.
01835      *
01836      *****************************************************************/
01837 
01838 #if HARDCODE_DATA
01839     fprintf(out, PREF "ac_uint4 _ucnum_size = %lu;\n\n",
01840         (unsigned long)ncodes_used<<1);
01841 
01842     fprintf(out, PREF "ac_uint4 _ucnum_nodes[] = {");
01843 
01844     /*
01845      * Now, if number mappings exist, write them out.
01846      */
01847     if (ncodes_used > 0) {
01848        for (i = 0; i<ncodes_used; i++) {
01849            if (i) fprintf(out, ",");
01850            if (!(i&1)) fprintf(out, "\n\t");
01851            else fprintf(out, " ");
01852            fprintf(out, "0x%08lx, 0x%08lx",
01853                (unsigned long) ncodes[i].code, (unsigned long) ncodes[i].idx);
01854        }
01855        fprintf(out, "\n};\n\n");
01856 
01857        fprintf(out, PREF "short _ucnum_vals[] = {");
01858        for (i = 0; i<nums_used; i++) {
01859            if (i) fprintf(out, ",");
01860            if (!(i&3)) fprintf(out, "\n\t");
01861            else fprintf(out, " ");
01862            if (nums[i].numerator < 0) {
01863               fprintf(out, "%6d, 0x%04x",
01864                 nums[i].numerator, nums[i].denominator);
01865            } else {
01866               fprintf(out, "0x%04x, 0x%04x",
01867                 nums[i].numerator, nums[i].denominator);
01868            }
01869        }
01870        fprintf(out, "\n};\n\n");
01871     }
01872 #else
01873     /*
01874      * Open the num.dat file.
01875      */
01876     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "num.dat", opath);
01877     if ((out = fopen(path, "wb")) == 0)
01878       return;
01879 
01880     /*
01881      * The count part of the header will be the total number of codes that
01882      * have numbers.
01883      */
01884     hdr[1] = (ac_uint2) (ncodes_used << 1);
01885     bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
01886 
01887     /*
01888      * Write the header.
01889      */
01890     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
01891 
01892     /*
01893      * Write out the byte count to maintain header size.
01894      */
01895     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
01896 
01897     /*
01898      * Now, if number mappings exist, write them out.
01899      */
01900     if (ncodes_used > 0) {
01901         fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
01902         fwrite((char *) nums, sizeof(_num_t), nums_used, out);
01903     }
01904 #endif
01905 
01906 #endif
01907 
01908     fclose(out);
01909 }
01910 
01911 static void
01912 usage(char *prog)
01913 {
01914     fprintf(stderr,
01915             "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
01916     fprintf(stderr, " datafile1 datafile2 ...\n\n");
01917     fprintf(stderr,
01918             "-o output-directory\n\t\tWrite the output files to a different");
01919     fprintf(stderr, " directory (default: .).\n");
01920     fprintf(stderr,
01921             "-x composition-exclusion\n\t\tFile of composition codes");
01922     fprintf(stderr, " that should be excluded.\n");
01923     exit(1);
01924 }
01925 
01926 int
01927 main(int argc, char *argv[])
01928 {
01929     FILE *in;
01930     char *prog, *opath;
01931 
01932     prog = argv[1];
01933 
01934     opath = 0;
01935     in = stdin;
01936 
01937     argc--;
01938     argv++;
01939 
01940     while (argc > 0) {
01941         if (argv[0][0] == '-') {
01942             switch (argv[0][1]) {
01943               case 'o':
01944                 argc--;
01945                 argv++;
01946                 opath = argv[0];
01947                 break;
01948               case 'x':
01949                 argc--;
01950                 argv++;
01951                 if ((in = fopen(argv[0], "r")) == 0)
01952                   fprintf(stderr,
01953                           "%s: unable to open composition exclusion file %s\n",
01954                           prog, argv[0]);
01955                 else {
01956                     read_compexdata(in);
01957                     fclose(in);
01958                     in = 0;
01959                 }
01960                 break;
01961               default:
01962                 usage(prog);
01963             }
01964         } else {
01965             if (in != stdin && in != NULL)
01966               fclose(in);
01967             if ((in = fopen(argv[0], "r")) == 0)
01968               fprintf(stderr, "%s: unable to open ctype file %s\n",
01969                       prog, argv[0]);
01970             else {
01971                 read_cdata(in);
01972                 fclose(in);
01973                 in = 0;
01974            }
01975         }
01976         argc--;
01977         argv++;
01978     }
01979 
01980     if (opath == 0)
01981       opath = ".";
01982     write_cdata(opath);
01983 
01984     return 0;
01985 }