ucgendat.c 49 KB

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