intl/icu/source/tools/makeconv/gencnvex.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 *******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 2003-2013, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 *******************************************************************************
michael@0 8 * file name: gencnvex.c
michael@0 9 * encoding: US-ASCII
michael@0 10 * tab size: 8 (not used)
michael@0 11 * indentation:4
michael@0 12 *
michael@0 13 * created on: 2003oct12
michael@0 14 * created by: Markus W. Scherer
michael@0 15 */
michael@0 16
michael@0 17 #include <stdio.h>
michael@0 18 #include "unicode/utypes.h"
michael@0 19 #include "unicode/ustring.h"
michael@0 20 #include "cstring.h"
michael@0 21 #include "cmemory.h"
michael@0 22 #include "ucnv_cnv.h"
michael@0 23 #include "ucnvmbcs.h"
michael@0 24 #include "toolutil.h"
michael@0 25 #include "unewdata.h"
michael@0 26 #include "ucm.h"
michael@0 27 #include "makeconv.h"
michael@0 28 #include "genmbcs.h"
michael@0 29
michael@0 30 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
michael@0 31
michael@0 32
michael@0 33 static void
michael@0 34 CnvExtClose(NewConverter *cnvData);
michael@0 35
michael@0 36 static UBool
michael@0 37 CnvExtIsValid(NewConverter *cnvData,
michael@0 38 const uint8_t *bytes, int32_t length);
michael@0 39
michael@0 40 static UBool
michael@0 41 CnvExtAddTable(NewConverter *cnvData, UCMTable *table, UConverterStaticData *staticData);
michael@0 42
michael@0 43 static uint32_t
michael@0 44 CnvExtWrite(NewConverter *cnvData, const UConverterStaticData *staticData,
michael@0 45 UNewDataMemory *pData, int32_t tableType);
michael@0 46
michael@0 47 typedef struct CnvExtData {
michael@0 48 NewConverter newConverter;
michael@0 49
michael@0 50 UCMFile *ucm;
michael@0 51
michael@0 52 /* toUnicode (state table in ucm->states) */
michael@0 53 UToolMemory *toUTable, *toUUChars;
michael@0 54
michael@0 55 /* fromUnicode */
michael@0 56 UToolMemory *fromUTableUChars, *fromUTableValues, *fromUBytes;
michael@0 57
michael@0 58 uint16_t stage1[MBCS_STAGE_1_SIZE];
michael@0 59 uint16_t stage2[MBCS_STAGE_2_SIZE];
michael@0 60 uint16_t stage3[0x10000<<UCNV_EXT_STAGE_2_LEFT_SHIFT]; /* 0x10000 because of 16-bit stage 2/3 indexes */
michael@0 61 uint32_t stage3b[0x10000];
michael@0 62
michael@0 63 int32_t stage1Top, stage2Top, stage3Top, stage3bTop;
michael@0 64
michael@0 65 /* for stage3 compaction of <subchar1> |2 mappings */
michael@0 66 uint16_t stage3Sub1Block;
michael@0 67
michael@0 68 /* statistics */
michael@0 69 int32_t
michael@0 70 maxInBytes, maxOutBytes, maxBytesPerUChar,
michael@0 71 maxInUChars, maxOutUChars, maxUCharsPerByte;
michael@0 72 } CnvExtData;
michael@0 73
michael@0 74 NewConverter *
michael@0 75 CnvExtOpen(UCMFile *ucm) {
michael@0 76 CnvExtData *extData;
michael@0 77
michael@0 78 extData=(CnvExtData *)uprv_malloc(sizeof(CnvExtData));
michael@0 79 if(extData==NULL) {
michael@0 80 printf("out of memory\n");
michael@0 81 exit(U_MEMORY_ALLOCATION_ERROR);
michael@0 82 }
michael@0 83 uprv_memset(extData, 0, sizeof(CnvExtData));
michael@0 84
michael@0 85 extData->ucm=ucm; /* aliased, not owned */
michael@0 86
michael@0 87 extData->newConverter.close=CnvExtClose;
michael@0 88 extData->newConverter.isValid=CnvExtIsValid;
michael@0 89 extData->newConverter.addTable=CnvExtAddTable;
michael@0 90 extData->newConverter.write=CnvExtWrite;
michael@0 91 return &extData->newConverter;
michael@0 92 }
michael@0 93
michael@0 94 static void
michael@0 95 CnvExtClose(NewConverter *cnvData) {
michael@0 96 CnvExtData *extData=(CnvExtData *)cnvData;
michael@0 97 if(extData!=NULL) {
michael@0 98 utm_close(extData->toUTable);
michael@0 99 utm_close(extData->toUUChars);
michael@0 100 utm_close(extData->fromUTableUChars);
michael@0 101 utm_close(extData->fromUTableValues);
michael@0 102 utm_close(extData->fromUBytes);
michael@0 103 uprv_free(extData);
michael@0 104 }
michael@0 105 }
michael@0 106
michael@0 107 /* we do not expect this to be called */
michael@0 108 static UBool
michael@0 109 CnvExtIsValid(NewConverter *cnvData,
michael@0 110 const uint8_t *bytes, int32_t length) {
michael@0 111 return FALSE;
michael@0 112 }
michael@0 113
michael@0 114 static uint32_t
michael@0 115 CnvExtWrite(NewConverter *cnvData, const UConverterStaticData *staticData,
michael@0 116 UNewDataMemory *pData, int32_t tableType) {
michael@0 117 CnvExtData *extData=(CnvExtData *)cnvData;
michael@0 118 int32_t length, top, headerSize;
michael@0 119
michael@0 120 int32_t indexes[UCNV_EXT_INDEXES_MIN_LENGTH]={ 0 };
michael@0 121
michael@0 122 if(tableType&TABLE_BASE) {
michael@0 123 headerSize=0;
michael@0 124 } else {
michael@0 125 _MBCSHeader header={ { 0, 0, 0, 0 }, 0, 0, 0, 0, 0, 0, 0 };
michael@0 126
michael@0 127 /* write the header and base table name for an extension-only table */
michael@0 128 length=(int32_t)uprv_strlen(extData->ucm->baseName)+1;
michael@0 129 while(length&3) {
michael@0 130 /* add padding */
michael@0 131 extData->ucm->baseName[length++]=0;
michael@0 132 }
michael@0 133
michael@0 134 headerSize=MBCS_HEADER_V4_LENGTH*4+length;
michael@0 135
michael@0 136 /* fill the header */
michael@0 137 header.version[0]=4;
michael@0 138 header.version[1]=2;
michael@0 139 header.flags=(uint32_t)((headerSize<<8)|MBCS_OUTPUT_EXT_ONLY);
michael@0 140
michael@0 141 /* write the header and the base table name */
michael@0 142 udata_writeBlock(pData, &header, MBCS_HEADER_V4_LENGTH*4);
michael@0 143 udata_writeBlock(pData, extData->ucm->baseName, length);
michael@0 144 }
michael@0 145
michael@0 146 /* fill indexes[] - offsets/indexes are in units of the target array */
michael@0 147 top=0;
michael@0 148
michael@0 149 indexes[UCNV_EXT_INDEXES_LENGTH]=length=UCNV_EXT_INDEXES_MIN_LENGTH;
michael@0 150 top+=length*4;
michael@0 151
michael@0 152 indexes[UCNV_EXT_TO_U_INDEX]=top;
michael@0 153 indexes[UCNV_EXT_TO_U_LENGTH]=length=utm_countItems(extData->toUTable);
michael@0 154 top+=length*4;
michael@0 155
michael@0 156 indexes[UCNV_EXT_TO_U_UCHARS_INDEX]=top;
michael@0 157 indexes[UCNV_EXT_TO_U_UCHARS_LENGTH]=length=utm_countItems(extData->toUUChars);
michael@0 158 top+=length*2;
michael@0 159
michael@0 160 indexes[UCNV_EXT_FROM_U_UCHARS_INDEX]=top;
michael@0 161 length=utm_countItems(extData->fromUTableUChars);
michael@0 162 top+=length*2;
michael@0 163
michael@0 164 if(top&3) {
michael@0 165 /* add padding */
michael@0 166 *((UChar *)utm_alloc(extData->fromUTableUChars))=0;
michael@0 167 *((uint32_t *)utm_alloc(extData->fromUTableValues))=0;
michael@0 168 ++length;
michael@0 169 top+=2;
michael@0 170 }
michael@0 171 indexes[UCNV_EXT_FROM_U_LENGTH]=length;
michael@0 172
michael@0 173 indexes[UCNV_EXT_FROM_U_VALUES_INDEX]=top;
michael@0 174 top+=length*4;
michael@0 175
michael@0 176 indexes[UCNV_EXT_FROM_U_BYTES_INDEX]=top;
michael@0 177 length=utm_countItems(extData->fromUBytes);
michael@0 178 top+=length;
michael@0 179
michael@0 180 if(top&1) {
michael@0 181 /* add padding */
michael@0 182 *((uint8_t *)utm_alloc(extData->fromUBytes))=0;
michael@0 183 ++length;
michael@0 184 ++top;
michael@0 185 }
michael@0 186 indexes[UCNV_EXT_FROM_U_BYTES_LENGTH]=length;
michael@0 187
michael@0 188 indexes[UCNV_EXT_FROM_U_STAGE_12_INDEX]=top;
michael@0 189 indexes[UCNV_EXT_FROM_U_STAGE_1_LENGTH]=length=extData->stage1Top;
michael@0 190 indexes[UCNV_EXT_FROM_U_STAGE_12_LENGTH]=length+=extData->stage2Top;
michael@0 191 top+=length*2;
michael@0 192
michael@0 193 indexes[UCNV_EXT_FROM_U_STAGE_3_INDEX]=top;
michael@0 194 length=extData->stage3Top;
michael@0 195 top+=length*2;
michael@0 196
michael@0 197 if(top&3) {
michael@0 198 /* add padding */
michael@0 199 extData->stage3[extData->stage3Top++]=0;
michael@0 200 ++length;
michael@0 201 top+=2;
michael@0 202 }
michael@0 203 indexes[UCNV_EXT_FROM_U_STAGE_3_LENGTH]=length;
michael@0 204
michael@0 205 indexes[UCNV_EXT_FROM_U_STAGE_3B_INDEX]=top;
michael@0 206 indexes[UCNV_EXT_FROM_U_STAGE_3B_LENGTH]=length=extData->stage3bTop;
michael@0 207 top+=length*4;
michael@0 208
michael@0 209 indexes[UCNV_EXT_SIZE]=top;
michael@0 210
michael@0 211 /* statistics */
michael@0 212 indexes[UCNV_EXT_COUNT_BYTES]=
michael@0 213 (extData->maxInBytes<<16)|
michael@0 214 (extData->maxOutBytes<<8)|
michael@0 215 extData->maxBytesPerUChar;
michael@0 216 indexes[UCNV_EXT_COUNT_UCHARS]=
michael@0 217 (extData->maxInUChars<<16)|
michael@0 218 (extData->maxOutUChars<<8)|
michael@0 219 extData->maxUCharsPerByte;
michael@0 220
michael@0 221 indexes[UCNV_EXT_FLAGS]=extData->ucm->ext->unicodeMask;
michael@0 222
michael@0 223 /* write the extension data */
michael@0 224 udata_writeBlock(pData, indexes, sizeof(indexes));
michael@0 225 udata_writeBlock(pData, utm_getStart(extData->toUTable), indexes[UCNV_EXT_TO_U_LENGTH]*4);
michael@0 226 udata_writeBlock(pData, utm_getStart(extData->toUUChars), indexes[UCNV_EXT_TO_U_UCHARS_LENGTH]*2);
michael@0 227
michael@0 228 udata_writeBlock(pData, utm_getStart(extData->fromUTableUChars), indexes[UCNV_EXT_FROM_U_LENGTH]*2);
michael@0 229 udata_writeBlock(pData, utm_getStart(extData->fromUTableValues), indexes[UCNV_EXT_FROM_U_LENGTH]*4);
michael@0 230 udata_writeBlock(pData, utm_getStart(extData->fromUBytes), indexes[UCNV_EXT_FROM_U_BYTES_LENGTH]);
michael@0 231
michael@0 232 udata_writeBlock(pData, extData->stage1, extData->stage1Top*2);
michael@0 233 udata_writeBlock(pData, extData->stage2, extData->stage2Top*2);
michael@0 234 udata_writeBlock(pData, extData->stage3, extData->stage3Top*2);
michael@0 235 udata_writeBlock(pData, extData->stage3b, extData->stage3bTop*4);
michael@0 236
michael@0 237 #if 0
michael@0 238 {
michael@0 239 int32_t i, j;
michael@0 240
michael@0 241 length=extData->stage1Top;
michael@0 242 printf("\nstage1[%x]:\n", length);
michael@0 243
michael@0 244 for(i=0; i<length; ++i) {
michael@0 245 if(extData->stage1[i]!=length) {
michael@0 246 printf("stage1[%04x]=%04x\n", i, extData->stage1[i]);
michael@0 247 }
michael@0 248 }
michael@0 249
michael@0 250 j=length;
michael@0 251 length=extData->stage2Top;
michael@0 252 printf("\nstage2[%x]:\n", length);
michael@0 253
michael@0 254 for(i=0; i<length; ++j, ++i) {
michael@0 255 if(extData->stage2[i]!=0) {
michael@0 256 printf("stage12[%04x]=%04x\n", j, extData->stage2[i]);
michael@0 257 }
michael@0 258 }
michael@0 259
michael@0 260 length=extData->stage3Top;
michael@0 261 printf("\nstage3[%x]:\n", length);
michael@0 262
michael@0 263 for(i=0; i<length; ++i) {
michael@0 264 if(extData->stage3[i]!=0) {
michael@0 265 printf("stage3[%04x]=%04x\n", i, extData->stage3[i]);
michael@0 266 }
michael@0 267 }
michael@0 268
michael@0 269 length=extData->stage3bTop;
michael@0 270 printf("\nstage3b[%x]:\n", length);
michael@0 271
michael@0 272 for(i=0; i<length; ++i) {
michael@0 273 if(extData->stage3b[i]!=0) {
michael@0 274 printf("stage3b[%04x]=%08x\n", i, extData->stage3b[i]);
michael@0 275 }
michael@0 276 }
michael@0 277 }
michael@0 278 #endif
michael@0 279
michael@0 280 if(VERBOSE) {
michael@0 281 printf("size of extension data: %ld\n", (long)top);
michael@0 282 }
michael@0 283
michael@0 284 /* return the number of bytes that should have been written */
michael@0 285 return (uint32_t)(headerSize+top);
michael@0 286 }
michael@0 287
michael@0 288 /* to Unicode --------------------------------------------------------------- */
michael@0 289
michael@0 290 /*
michael@0 291 * Remove fromUnicode fallbacks and SUB mappings which are irrelevant for
michael@0 292 * the toUnicode table.
michael@0 293 * This includes mappings with MBCS_FROM_U_EXT_FLAG which were suitable
michael@0 294 * for the base toUnicode table but not for the base fromUnicode table.
michael@0 295 * The table must be sorted.
michael@0 296 * Modifies previous data in the reverseMap.
michael@0 297 */
michael@0 298 static int32_t
michael@0 299 reduceToUMappings(UCMTable *table) {
michael@0 300 UCMapping *mappings;
michael@0 301 int32_t *map;
michael@0 302 int32_t i, j, count;
michael@0 303 int8_t flag;
michael@0 304
michael@0 305 mappings=table->mappings;
michael@0 306 map=table->reverseMap;
michael@0 307 count=table->mappingsLength;
michael@0 308
michael@0 309 /* leave the map alone for the initial mappings with desired flags */
michael@0 310 for(i=j=0; i<count; ++i) {
michael@0 311 flag=mappings[map[i]].f;
michael@0 312 if(flag!=0 && flag!=3) {
michael@0 313 break;
michael@0 314 }
michael@0 315 }
michael@0 316
michael@0 317 /* reduce from here to the rest */
michael@0 318 for(j=i; i<count; ++i) {
michael@0 319 flag=mappings[map[i]].f;
michael@0 320 if(flag==0 || flag==3) {
michael@0 321 map[j++]=map[i];
michael@0 322 }
michael@0 323 }
michael@0 324
michael@0 325 return j;
michael@0 326 }
michael@0 327
michael@0 328 static uint32_t
michael@0 329 getToUnicodeValue(CnvExtData *extData, UCMTable *table, UCMapping *m) {
michael@0 330 UChar32 *u32;
michael@0 331 UChar *u;
michael@0 332 uint32_t value;
michael@0 333 int32_t u16Length, ratio;
michael@0 334 UErrorCode errorCode;
michael@0 335
michael@0 336 /* write the Unicode result code point or string index */
michael@0 337 if(m->uLen==1) {
michael@0 338 u16Length=U16_LENGTH(m->u);
michael@0 339 value=(uint32_t)(UCNV_EXT_TO_U_MIN_CODE_POINT+m->u);
michael@0 340 } else {
michael@0 341 /* the parser enforces m->uLen<=UCNV_EXT_MAX_UCHARS */
michael@0 342
michael@0 343 /* get the result code point string and its 16-bit string length */
michael@0 344 u32=UCM_GET_CODE_POINTS(table, m);
michael@0 345 errorCode=U_ZERO_ERROR;
michael@0 346 u_strFromUTF32(NULL, 0, &u16Length, u32, m->uLen, &errorCode);
michael@0 347 if(U_FAILURE(errorCode) && errorCode!=U_BUFFER_OVERFLOW_ERROR) {
michael@0 348 exit(errorCode);
michael@0 349 }
michael@0 350
michael@0 351 /* allocate it and put its length and index into the value */
michael@0 352 value=
michael@0 353 (((uint32_t)u16Length+UCNV_EXT_TO_U_LENGTH_OFFSET)<<UCNV_EXT_TO_U_LENGTH_SHIFT)|
michael@0 354 ((uint32_t)utm_countItems(extData->toUUChars));
michael@0 355 u=utm_allocN(extData->toUUChars, u16Length);
michael@0 356
michael@0 357 /* write the result 16-bit string */
michael@0 358 errorCode=U_ZERO_ERROR;
michael@0 359 u_strFromUTF32(u, u16Length, NULL, u32, m->uLen, &errorCode);
michael@0 360 if(U_FAILURE(errorCode) && errorCode!=U_BUFFER_OVERFLOW_ERROR) {
michael@0 361 exit(errorCode);
michael@0 362 }
michael@0 363 }
michael@0 364 if(m->f==0) {
michael@0 365 value|=UCNV_EXT_TO_U_ROUNDTRIP_FLAG;
michael@0 366 }
michael@0 367
michael@0 368 /* update statistics */
michael@0 369 if(m->bLen>extData->maxInBytes) {
michael@0 370 extData->maxInBytes=m->bLen;
michael@0 371 }
michael@0 372 if(u16Length>extData->maxOutUChars) {
michael@0 373 extData->maxOutUChars=u16Length;
michael@0 374 }
michael@0 375
michael@0 376 ratio=(u16Length+(m->bLen-1))/m->bLen;
michael@0 377 if(ratio>extData->maxUCharsPerByte) {
michael@0 378 extData->maxUCharsPerByte=ratio;
michael@0 379 }
michael@0 380
michael@0 381 return value;
michael@0 382 }
michael@0 383
michael@0 384 /*
michael@0 385 * Recursive toUTable generator core function.
michael@0 386 * Preconditions:
michael@0 387 * - start<limit (There is at least one mapping.)
michael@0 388 * - The mappings are sorted lexically. (Access is through the reverseMap.)
michael@0 389 * - All mappings between start and limit have input sequences that share
michael@0 390 * the same prefix of unitIndex length, and therefore all of these sequences
michael@0 391 * are at least unitIndex+1 long.
michael@0 392 * - There are only relevant mappings available through the reverseMap,
michael@0 393 * see reduceToUMappings().
michael@0 394 *
michael@0 395 * One function invocation generates one section table.
michael@0 396 *
michael@0 397 * Steps:
michael@0 398 * 1. Count the number of unique unit values and get the low/high unit values
michael@0 399 * that occur at unitIndex.
michael@0 400 * 2. Allocate the section table with possible optimization for linear access.
michael@0 401 * 3. Write temporary version of the section table with start indexes of
michael@0 402 * subsections, each corresponding to one unit value at unitIndex.
michael@0 403 * 4. Iterate through the table once more, and depending on the subsection length:
michael@0 404 * 0: write 0 as a result value (unused byte in linear-access section table)
michael@0 405 * >0: if there is one mapping with an input unit sequence of unitIndex+1
michael@0 406 * then defaultValue=compute the mapping result for this whole sequence
michael@0 407 * else defaultValue=0
michael@0 408 *
michael@0 409 * recurse into the subsection
michael@0 410 */
michael@0 411 static UBool
michael@0 412 generateToUTable(CnvExtData *extData, UCMTable *table,
michael@0 413 int32_t start, int32_t limit, int32_t unitIndex,
michael@0 414 uint32_t defaultValue) {
michael@0 415 UCMapping *mappings, *m;
michael@0 416 int32_t *map;
michael@0 417 int32_t i, j, uniqueCount, count, subStart, subLimit;
michael@0 418
michael@0 419 uint8_t *bytes;
michael@0 420 int32_t low, high, prev;
michael@0 421
michael@0 422 uint32_t *section;
michael@0 423
michael@0 424 mappings=table->mappings;
michael@0 425 map=table->reverseMap;
michael@0 426
michael@0 427 /* step 1: examine the input units; set low, high, uniqueCount */
michael@0 428 m=mappings+map[start];
michael@0 429 bytes=UCM_GET_BYTES(table, m);
michael@0 430 low=bytes[unitIndex];
michael@0 431 uniqueCount=1;
michael@0 432
michael@0 433 prev=high=low;
michael@0 434 for(i=start+1; i<limit; ++i) {
michael@0 435 m=mappings+map[i];
michael@0 436 bytes=UCM_GET_BYTES(table, m);
michael@0 437 high=bytes[unitIndex];
michael@0 438
michael@0 439 if(high!=prev) {
michael@0 440 prev=high;
michael@0 441 ++uniqueCount;
michael@0 442 }
michael@0 443 }
michael@0 444
michael@0 445 /* step 2: allocate the section; set count, section */
michael@0 446 count=(high-low)+1;
michael@0 447 if(count<0x100 && (unitIndex==0 || uniqueCount>=(3*count)/4)) {
michael@0 448 /*
michael@0 449 * for the root table and for fairly full tables:
michael@0 450 * allocate for direct, linear array access
michael@0 451 * by keeping count, to write an entry for each unit value
michael@0 452 * from low to high
michael@0 453 * exception: use a compact table if count==0x100 because
michael@0 454 * that cannot be encoded in the length byte
michael@0 455 */
michael@0 456 } else {
michael@0 457 count=uniqueCount;
michael@0 458 }
michael@0 459
michael@0 460 if(count>=0x100) {
michael@0 461 fprintf(stderr, "error: toUnicode extension table section overflow: %ld section entries\n", (long)count);
michael@0 462 return FALSE;
michael@0 463 }
michael@0 464
michael@0 465 /* allocate the section: 1 entry for the header + count for the items */
michael@0 466 section=(uint32_t *)utm_allocN(extData->toUTable, 1+count);
michael@0 467
michael@0 468 /* write the section header */
michael@0 469 *section++=((uint32_t)count<<UCNV_EXT_TO_U_BYTE_SHIFT)|defaultValue;
michael@0 470
michael@0 471 /* step 3: write temporary section table with subsection starts */
michael@0 472 prev=low-1; /* just before low to prevent empty subsections before low */
michael@0 473 j=0; /* section table index */
michael@0 474 for(i=start; i<limit; ++i) {
michael@0 475 m=mappings+map[i];
michael@0 476 bytes=UCM_GET_BYTES(table, m);
michael@0 477 high=bytes[unitIndex];
michael@0 478
michael@0 479 if(high!=prev) {
michael@0 480 /* start of a new subsection for unit high */
michael@0 481 if(count>uniqueCount) {
michael@0 482 /* write empty subsections for unused units in a linear table */
michael@0 483 while(++prev<high) {
michael@0 484 section[j++]=((uint32_t)prev<<UCNV_EXT_TO_U_BYTE_SHIFT)|(uint32_t)i;
michael@0 485 }
michael@0 486 } else {
michael@0 487 prev=high;
michael@0 488 }
michael@0 489
michael@0 490 /* write the entry with the subsection start */
michael@0 491 section[j++]=((uint32_t)high<<UCNV_EXT_TO_U_BYTE_SHIFT)|(uint32_t)i;
michael@0 492 }
michael@0 493 }
michael@0 494 /* assert(j==count) */
michael@0 495
michael@0 496 /* step 4: recurse and write results */
michael@0 497 subLimit=UCNV_EXT_TO_U_GET_VALUE(section[0]);
michael@0 498 for(j=0; j<count; ++j) {
michael@0 499 subStart=subLimit;
michael@0 500 subLimit= (j+1)<count ? UCNV_EXT_TO_U_GET_VALUE(section[j+1]) : limit;
michael@0 501
michael@0 502 /* remove the subStart temporary value */
michael@0 503 section[j]&=~UCNV_EXT_TO_U_VALUE_MASK;
michael@0 504
michael@0 505 if(subStart==subLimit) {
michael@0 506 /* leave the value zero: empty subsection for unused unit in a linear table */
michael@0 507 continue;
michael@0 508 }
michael@0 509
michael@0 510 /* see if there is exactly one input unit sequence of length unitIndex+1 */
michael@0 511 defaultValue=0;
michael@0 512 m=mappings+map[subStart];
michael@0 513 if(m->bLen==unitIndex+1) {
michael@0 514 /* do not include this in generateToUTable() */
michael@0 515 ++subStart;
michael@0 516
michael@0 517 if(subStart<subLimit && mappings[map[subStart]].bLen==unitIndex+1) {
michael@0 518 /* print error for multiple same-input-sequence mappings */
michael@0 519 fprintf(stderr, "error: multiple mappings from same bytes\n");
michael@0 520 ucm_printMapping(table, m, stderr);
michael@0 521 ucm_printMapping(table, mappings+map[subStart], stderr);
michael@0 522 return FALSE;
michael@0 523 }
michael@0 524
michael@0 525 defaultValue=getToUnicodeValue(extData, table, m);
michael@0 526 }
michael@0 527
michael@0 528 if(subStart==subLimit) {
michael@0 529 /* write the result for the input sequence ending here */
michael@0 530 section[j]|=defaultValue;
michael@0 531 } else {
michael@0 532 /* write the index to the subsection table */
michael@0 533 section[j]|=(uint32_t)utm_countItems(extData->toUTable);
michael@0 534
michael@0 535 /* recurse */
michael@0 536 if(!generateToUTable(extData, table, subStart, subLimit, unitIndex+1, defaultValue)) {
michael@0 537 return FALSE;
michael@0 538 }
michael@0 539 }
michael@0 540 }
michael@0 541 return TRUE;
michael@0 542 }
michael@0 543
michael@0 544 /*
michael@0 545 * Generate the toUTable and toUUChars from the input table.
michael@0 546 * The input table must be sorted, and all precision flags must be 0..3.
michael@0 547 * This function will modify the table's reverseMap.
michael@0 548 */
michael@0 549 static UBool
michael@0 550 makeToUTable(CnvExtData *extData, UCMTable *table) {
michael@0 551 int32_t toUCount;
michael@0 552
michael@0 553 toUCount=reduceToUMappings(table);
michael@0 554
michael@0 555 extData->toUTable=utm_open("cnv extension toUTable", 0x10000, UCNV_EXT_TO_U_MIN_CODE_POINT, 4);
michael@0 556 extData->toUUChars=utm_open("cnv extension toUUChars", 0x10000, UCNV_EXT_TO_U_INDEX_MASK+1, 2);
michael@0 557
michael@0 558 return generateToUTable(extData, table, 0, toUCount, 0, 0);
michael@0 559 }
michael@0 560
michael@0 561 /* from Unicode ------------------------------------------------------------- */
michael@0 562
michael@0 563 /*
michael@0 564 * preprocessing:
michael@0 565 * rebuild reverseMap with mapping indexes for mappings relevant for from Unicode
michael@0 566 * change each Unicode string to encode all but the first code point in 16-bit form
michael@0 567 *
michael@0 568 * generation:
michael@0 569 * for each unique code point
michael@0 570 * write an entry in the 3-stage trie
michael@0 571 * check that there is only one single-code point sequence
michael@0 572 * start recursion for following 16-bit input units
michael@0 573 */
michael@0 574
michael@0 575 /*
michael@0 576 * Remove toUnicode fallbacks and non-<subchar1> SUB mappings
michael@0 577 * which are irrelevant for the fromUnicode extension table.
michael@0 578 * Remove MBCS_FROM_U_EXT_FLAG bits.
michael@0 579 * Overwrite the reverseMap with an index array to the relevant mappings.
michael@0 580 * Modify the code point sequences to a generator-friendly format where
michael@0 581 * the first code points remains unchanged but the following are recoded
michael@0 582 * into 16-bit Unicode string form.
michael@0 583 * The table must be sorted.
michael@0 584 * Destroys previous data in the reverseMap.
michael@0 585 */
michael@0 586 static int32_t
michael@0 587 prepareFromUMappings(UCMTable *table) {
michael@0 588 UCMapping *mappings, *m;
michael@0 589 int32_t *map;
michael@0 590 int32_t i, j, count;
michael@0 591 int8_t flag;
michael@0 592
michael@0 593 mappings=table->mappings;
michael@0 594 map=table->reverseMap;
michael@0 595 count=table->mappingsLength;
michael@0 596
michael@0 597 /*
michael@0 598 * we do not go through the map on input because the mappings are
michael@0 599 * sorted lexically
michael@0 600 */
michael@0 601 m=mappings;
michael@0 602
michael@0 603 for(i=j=0; i<count; ++m, ++i) {
michael@0 604 flag=m->f;
michael@0 605 if(flag>=0) {
michael@0 606 flag&=MBCS_FROM_U_EXT_MASK;
michael@0 607 m->f=flag;
michael@0 608 }
michael@0 609 if(flag==0 || flag==1 || (flag==2 && m->bLen==1) || flag==4) {
michael@0 610 map[j++]=i;
michael@0 611
michael@0 612 if(m->uLen>1) {
michael@0 613 /* recode all but the first code point to 16-bit Unicode */
michael@0 614 UChar32 *u32;
michael@0 615 UChar *u;
michael@0 616 UChar32 c;
michael@0 617 int32_t q, r;
michael@0 618
michael@0 619 u32=UCM_GET_CODE_POINTS(table, m);
michael@0 620 u=(UChar *)u32; /* destructive in-place recoding */
michael@0 621 for(r=2, q=1; q<m->uLen; ++q) {
michael@0 622 c=u32[q];
michael@0 623 U16_APPEND_UNSAFE(u, r, c);
michael@0 624 }
michael@0 625
michael@0 626 /* counts the first code point always at 2 - the first 16-bit unit is at 16-bit index 2 */
michael@0 627 m->uLen=(int8_t)r;
michael@0 628 }
michael@0 629 }
michael@0 630 }
michael@0 631
michael@0 632 return j;
michael@0 633 }
michael@0 634
michael@0 635 static uint32_t
michael@0 636 getFromUBytesValue(CnvExtData *extData, UCMTable *table, UCMapping *m) {
michael@0 637 uint8_t *bytes, *resultBytes;
michael@0 638 uint32_t value;
michael@0 639 int32_t u16Length, ratio;
michael@0 640
michael@0 641 if(m->f==2) {
michael@0 642 /*
michael@0 643 * no mapping, <subchar1> preferred
michael@0 644 *
michael@0 645 * no need to count in statistics because the subchars are already
michael@0 646 * counted for maxOutBytes and maxBytesPerUChar in UConverterStaticData,
michael@0 647 * and this non-mapping does not count for maxInUChars which are always
michael@0 648 * trivially at least two if counting unmappable supplementary code points
michael@0 649 */
michael@0 650 return UCNV_EXT_FROM_U_SUBCHAR1;
michael@0 651 }
michael@0 652
michael@0 653 bytes=UCM_GET_BYTES(table, m);
michael@0 654 value=0;
michael@0 655 switch(m->bLen) {
michael@0 656 /* 1..3: store the bytes in the value word */
michael@0 657 case 3:
michael@0 658 value=((uint32_t)*bytes++)<<16;
michael@0 659 case 2:
michael@0 660 value|=((uint32_t)*bytes++)<<8;
michael@0 661 case 1:
michael@0 662 value|=*bytes;
michael@0 663 break;
michael@0 664 default:
michael@0 665 /* the parser enforces m->bLen<=UCNV_EXT_MAX_BYTES */
michael@0 666 /* store the bytes in fromUBytes[] and the index in the value word */
michael@0 667 value=(uint32_t)utm_countItems(extData->fromUBytes);
michael@0 668 resultBytes=utm_allocN(extData->fromUBytes, m->bLen);
michael@0 669 uprv_memcpy(resultBytes, bytes, m->bLen);
michael@0 670 break;
michael@0 671 }
michael@0 672 value|=(uint32_t)m->bLen<<UCNV_EXT_FROM_U_LENGTH_SHIFT;
michael@0 673 if(m->f==0) {
michael@0 674 value|=UCNV_EXT_FROM_U_ROUNDTRIP_FLAG;
michael@0 675 } else if(m->f==4) {
michael@0 676 value|=UCNV_EXT_FROM_U_GOOD_ONE_WAY_FLAG;
michael@0 677 }
michael@0 678
michael@0 679 /* calculate the real UTF-16 length (see recoding in prepareFromUMappings()) */
michael@0 680 if(m->uLen==1) {
michael@0 681 u16Length=U16_LENGTH(m->u);
michael@0 682 } else {
michael@0 683 u16Length=U16_LENGTH(UCM_GET_CODE_POINTS(table, m)[0])+(m->uLen-2);
michael@0 684 }
michael@0 685
michael@0 686 /* update statistics */
michael@0 687 if(u16Length>extData->maxInUChars) {
michael@0 688 extData->maxInUChars=u16Length;
michael@0 689 }
michael@0 690 if(m->bLen>extData->maxOutBytes) {
michael@0 691 extData->maxOutBytes=m->bLen;
michael@0 692 }
michael@0 693
michael@0 694 ratio=(m->bLen+(u16Length-1))/u16Length;
michael@0 695 if(ratio>extData->maxBytesPerUChar) {
michael@0 696 extData->maxBytesPerUChar=ratio;
michael@0 697 }
michael@0 698
michael@0 699 return value;
michael@0 700 }
michael@0 701
michael@0 702 /*
michael@0 703 * works like generateToUTable(), except that the
michael@0 704 * output section consists of two arrays, one for input UChars and one
michael@0 705 * for result values
michael@0 706 *
michael@0 707 * also, fromUTable sections are always stored in a compact form for
michael@0 708 * access via binary search
michael@0 709 */
michael@0 710 static UBool
michael@0 711 generateFromUTable(CnvExtData *extData, UCMTable *table,
michael@0 712 int32_t start, int32_t limit, int32_t unitIndex,
michael@0 713 uint32_t defaultValue) {
michael@0 714 UCMapping *mappings, *m;
michael@0 715 int32_t *map;
michael@0 716 int32_t i, j, uniqueCount, count, subStart, subLimit;
michael@0 717
michael@0 718 UChar *uchars;
michael@0 719 UChar32 low, high, prev;
michael@0 720
michael@0 721 UChar *sectionUChars;
michael@0 722 uint32_t *sectionValues;
michael@0 723
michael@0 724 mappings=table->mappings;
michael@0 725 map=table->reverseMap;
michael@0 726
michael@0 727 /* step 1: examine the input units; set low, high, uniqueCount */
michael@0 728 m=mappings+map[start];
michael@0 729 uchars=(UChar *)UCM_GET_CODE_POINTS(table, m);
michael@0 730 low=uchars[unitIndex];
michael@0 731 uniqueCount=1;
michael@0 732
michael@0 733 prev=high=low;
michael@0 734 for(i=start+1; i<limit; ++i) {
michael@0 735 m=mappings+map[i];
michael@0 736 uchars=(UChar *)UCM_GET_CODE_POINTS(table, m);
michael@0 737 high=uchars[unitIndex];
michael@0 738
michael@0 739 if(high!=prev) {
michael@0 740 prev=high;
michael@0 741 ++uniqueCount;
michael@0 742 }
michael@0 743 }
michael@0 744
michael@0 745 /* step 2: allocate the section; set count, section */
michael@0 746 /* the fromUTable always stores for access via binary search */
michael@0 747 count=uniqueCount;
michael@0 748
michael@0 749 /* allocate the section: 1 entry for the header + count for the items */
michael@0 750 sectionUChars=(UChar *)utm_allocN(extData->fromUTableUChars, 1+count);
michael@0 751 sectionValues=(uint32_t *)utm_allocN(extData->fromUTableValues, 1+count);
michael@0 752
michael@0 753 /* write the section header */
michael@0 754 *sectionUChars++=(UChar)count;
michael@0 755 *sectionValues++=defaultValue;
michael@0 756
michael@0 757 /* step 3: write temporary section table with subsection starts */
michael@0 758 prev=low-1; /* just before low to prevent empty subsections before low */
michael@0 759 j=0; /* section table index */
michael@0 760 for(i=start; i<limit; ++i) {
michael@0 761 m=mappings+map[i];
michael@0 762 uchars=(UChar *)UCM_GET_CODE_POINTS(table, m);
michael@0 763 high=uchars[unitIndex];
michael@0 764
michael@0 765 if(high!=prev) {
michael@0 766 /* start of a new subsection for unit high */
michael@0 767 prev=high;
michael@0 768
michael@0 769 /* write the entry with the subsection start */
michael@0 770 sectionUChars[j]=(UChar)high;
michael@0 771 sectionValues[j]=(uint32_t)i;
michael@0 772 ++j;
michael@0 773 }
michael@0 774 }
michael@0 775 /* assert(j==count) */
michael@0 776
michael@0 777 /* step 4: recurse and write results */
michael@0 778 subLimit=(int32_t)(sectionValues[0]);
michael@0 779 for(j=0; j<count; ++j) {
michael@0 780 subStart=subLimit;
michael@0 781 subLimit= (j+1)<count ? (int32_t)(sectionValues[j+1]) : limit;
michael@0 782
michael@0 783 /* see if there is exactly one input unit sequence of length unitIndex+1 */
michael@0 784 defaultValue=0;
michael@0 785 m=mappings+map[subStart];
michael@0 786 if(m->uLen==unitIndex+1) {
michael@0 787 /* do not include this in generateToUTable() */
michael@0 788 ++subStart;
michael@0 789
michael@0 790 if(subStart<subLimit && mappings[map[subStart]].uLen==unitIndex+1) {
michael@0 791 /* print error for multiple same-input-sequence mappings */
michael@0 792 fprintf(stderr, "error: multiple mappings from same Unicode code points\n");
michael@0 793 ucm_printMapping(table, m, stderr);
michael@0 794 ucm_printMapping(table, mappings+map[subStart], stderr);
michael@0 795 return FALSE;
michael@0 796 }
michael@0 797
michael@0 798 defaultValue=getFromUBytesValue(extData, table, m);
michael@0 799 }
michael@0 800
michael@0 801 if(subStart==subLimit) {
michael@0 802 /* write the result for the input sequence ending here */
michael@0 803 sectionValues[j]=defaultValue;
michael@0 804 } else {
michael@0 805 /* write the index to the subsection table */
michael@0 806 sectionValues[j]=(uint32_t)utm_countItems(extData->fromUTableValues);
michael@0 807
michael@0 808 /* recurse */
michael@0 809 if(!generateFromUTable(extData, table, subStart, subLimit, unitIndex+1, defaultValue)) {
michael@0 810 return FALSE;
michael@0 811 }
michael@0 812 }
michael@0 813 }
michael@0 814 return TRUE;
michael@0 815 }
michael@0 816
michael@0 817 /*
michael@0 818 * add entries to the fromUnicode trie,
michael@0 819 * assume to be called with code points in ascending order
michael@0 820 * and use that to build the trie in precompacted form
michael@0 821 */
michael@0 822 static void
michael@0 823 addFromUTrieEntry(CnvExtData *extData, UChar32 c, uint32_t value) {
michael@0 824 int32_t i1, i2, i3, i3b, nextOffset, min, newBlock;
michael@0 825
michael@0 826 if(value==0) {
michael@0 827 return;
michael@0 828 }
michael@0 829
michael@0 830 /*
michael@0 831 * compute the index for each stage,
michael@0 832 * allocate a stage block if necessary,
michael@0 833 * and write the stage value
michael@0 834 */
michael@0 835 i1=c>>10;
michael@0 836 if(i1>=extData->stage1Top) {
michael@0 837 extData->stage1Top=i1+1;
michael@0 838 }
michael@0 839
michael@0 840 nextOffset=(c>>4)&0x3f;
michael@0 841
michael@0 842 if(extData->stage1[i1]==0) {
michael@0 843 /* allocate another block in stage 2; overlap with the previous block */
michael@0 844 newBlock=extData->stage2Top;
michael@0 845 min=newBlock-nextOffset; /* minimum block start with overlap */
michael@0 846 while(min<newBlock && extData->stage2[newBlock-1]==0) {
michael@0 847 --newBlock;
michael@0 848 }
michael@0 849
michael@0 850 extData->stage1[i1]=(uint16_t)newBlock;
michael@0 851 extData->stage2Top=newBlock+MBCS_STAGE_2_BLOCK_SIZE;
michael@0 852 if(extData->stage2Top>LENGTHOF(extData->stage2)) {
michael@0 853 fprintf(stderr, "error: too many stage 2 entries at U+%04x\n", (int)c);
michael@0 854 exit(U_MEMORY_ALLOCATION_ERROR);
michael@0 855 }
michael@0 856 }
michael@0 857
michael@0 858 i2=extData->stage1[i1]+nextOffset;
michael@0 859 nextOffset=c&0xf;
michael@0 860
michael@0 861 if(extData->stage2[i2]==0) {
michael@0 862 /* allocate another block in stage 3; overlap with the previous block */
michael@0 863 newBlock=extData->stage3Top;
michael@0 864 min=newBlock-nextOffset; /* minimum block start with overlap */
michael@0 865 while(min<newBlock && extData->stage3[newBlock-1]==0) {
michael@0 866 --newBlock;
michael@0 867 }
michael@0 868
michael@0 869 /* round up to a multiple of stage 3 granularity >1 (similar to utrie.c) */
michael@0 870 newBlock=(newBlock+(UCNV_EXT_STAGE_3_GRANULARITY-1))&~(UCNV_EXT_STAGE_3_GRANULARITY-1);
michael@0 871 extData->stage2[i2]=(uint16_t)(newBlock>>UCNV_EXT_STAGE_2_LEFT_SHIFT);
michael@0 872
michael@0 873 extData->stage3Top=newBlock+MBCS_STAGE_3_BLOCK_SIZE;
michael@0 874 if(extData->stage3Top>LENGTHOF(extData->stage3)) {
michael@0 875 fprintf(stderr, "error: too many stage 3 entries at U+%04x\n", (int)c);
michael@0 876 exit(U_MEMORY_ALLOCATION_ERROR);
michael@0 877 }
michael@0 878 }
michael@0 879
michael@0 880 i3=((int32_t)extData->stage2[i2]<<UCNV_EXT_STAGE_2_LEFT_SHIFT)+nextOffset;
michael@0 881 /*
michael@0 882 * assume extData->stage3[i3]==0 because we get
michael@0 883 * code points in strictly ascending order
michael@0 884 */
michael@0 885
michael@0 886 if(value==UCNV_EXT_FROM_U_SUBCHAR1) {
michael@0 887 /* <subchar1> SUB mapping, see getFromUBytesValue() and prepareFromUMappings() */
michael@0 888 extData->stage3[i3]=1;
michael@0 889
michael@0 890 /*
michael@0 891 * precompaction is not optimal for <subchar1> |2 mappings because
michael@0 892 * stage3 values for them are all the same, unlike for other mappings
michael@0 893 * which all have unique values;
michael@0 894 * use a simple compaction of reusing a whole block filled with these
michael@0 895 * mappings
michael@0 896 */
michael@0 897
michael@0 898 /* is the entire block filled with <subchar1> |2 mappings? */
michael@0 899 if(nextOffset==MBCS_STAGE_3_BLOCK_SIZE-1) {
michael@0 900 for(min=i3-nextOffset;
michael@0 901 min<i3 && extData->stage3[min]==1;
michael@0 902 ++min) {}
michael@0 903
michael@0 904 if(min==i3) {
michael@0 905 /* the entire block is filled with these mappings */
michael@0 906 if(extData->stage3Sub1Block!=0) {
michael@0 907 /* point to the previous such block and remove this block from stage3 */
michael@0 908 extData->stage2[i2]=extData->stage3Sub1Block;
michael@0 909 extData->stage3Top-=MBCS_STAGE_3_BLOCK_SIZE;
michael@0 910 uprv_memset(extData->stage3+extData->stage3Top, 0, MBCS_STAGE_3_BLOCK_SIZE*2);
michael@0 911 } else {
michael@0 912 /* remember this block's stage2 entry */
michael@0 913 extData->stage3Sub1Block=extData->stage2[i2];
michael@0 914 }
michael@0 915 }
michael@0 916 }
michael@0 917 } else {
michael@0 918 if((i3b=extData->stage3bTop++)>=LENGTHOF(extData->stage3b)) {
michael@0 919 fprintf(stderr, "error: too many stage 3b entries at U+%04x\n", (int)c);
michael@0 920 exit(U_MEMORY_ALLOCATION_ERROR);
michael@0 921 }
michael@0 922
michael@0 923 /* roundtrip or fallback mapping */
michael@0 924 extData->stage3[i3]=(uint16_t)i3b;
michael@0 925 extData->stage3b[i3b]=value;
michael@0 926 }
michael@0 927 }
michael@0 928
michael@0 929 static UBool
michael@0 930 generateFromUTrie(CnvExtData *extData, UCMTable *table, int32_t mapLength) {
michael@0 931 UCMapping *mappings, *m;
michael@0 932 int32_t *map;
michael@0 933 uint32_t value;
michael@0 934 int32_t subStart, subLimit;
michael@0 935
michael@0 936 UChar32 *codePoints;
michael@0 937 UChar32 c, next;
michael@0 938
michael@0 939 if(mapLength==0) {
michael@0 940 return TRUE;
michael@0 941 }
michael@0 942
michael@0 943 mappings=table->mappings;
michael@0 944 map=table->reverseMap;
michael@0 945
michael@0 946 /*
michael@0 947 * iterate over same-initial-code point mappings,
michael@0 948 * enter the initial code point into the trie,
michael@0 949 * and start a recursion on the corresponding mappings section
michael@0 950 * with generateFromUTable()
michael@0 951 */
michael@0 952 m=mappings+map[0];
michael@0 953 codePoints=UCM_GET_CODE_POINTS(table, m);
michael@0 954 next=codePoints[0];
michael@0 955 subLimit=0;
michael@0 956 while(subLimit<mapLength) {
michael@0 957 /* get a new subsection of mappings starting with the same code point */
michael@0 958 subStart=subLimit;
michael@0 959 c=next;
michael@0 960 while(next==c && ++subLimit<mapLength) {
michael@0 961 m=mappings+map[subLimit];
michael@0 962 codePoints=UCM_GET_CODE_POINTS(table, m);
michael@0 963 next=codePoints[0];
michael@0 964 }
michael@0 965
michael@0 966 /*
michael@0 967 * compute the value for this code point;
michael@0 968 * if there is a mapping for this code point alone, it is at subStart
michael@0 969 * because the table is sorted lexically
michael@0 970 */
michael@0 971 value=0;
michael@0 972 m=mappings+map[subStart];
michael@0 973 codePoints=UCM_GET_CODE_POINTS(table, m);
michael@0 974 if(m->uLen==1) {
michael@0 975 /* do not include this in generateFromUTable() */
michael@0 976 ++subStart;
michael@0 977
michael@0 978 if(subStart<subLimit && mappings[map[subStart]].uLen==1) {
michael@0 979 /* print error for multiple same-input-sequence mappings */
michael@0 980 fprintf(stderr, "error: multiple mappings from same Unicode code points\n");
michael@0 981 ucm_printMapping(table, m, stderr);
michael@0 982 ucm_printMapping(table, mappings+map[subStart], stderr);
michael@0 983 return FALSE;
michael@0 984 }
michael@0 985
michael@0 986 value=getFromUBytesValue(extData, table, m);
michael@0 987 }
michael@0 988
michael@0 989 if(subStart==subLimit) {
michael@0 990 /* write the result for this one code point */
michael@0 991 addFromUTrieEntry(extData, c, value);
michael@0 992 } else {
michael@0 993 /* write the index to the subsection table */
michael@0 994 addFromUTrieEntry(extData, c, (uint32_t)utm_countItems(extData->fromUTableValues));
michael@0 995
michael@0 996 /* recurse, starting from 16-bit-unit index 2, the first 16-bit unit after c */
michael@0 997 if(!generateFromUTable(extData, table, subStart, subLimit, 2, value)) {
michael@0 998 return FALSE;
michael@0 999 }
michael@0 1000 }
michael@0 1001 }
michael@0 1002 return TRUE;
michael@0 1003 }
michael@0 1004
michael@0 1005 /*
michael@0 1006 * Generate the fromU data structures from the input table.
michael@0 1007 * The input table must be sorted, and all precision flags must be 0..3.
michael@0 1008 * This function will modify the table's reverseMap.
michael@0 1009 */
michael@0 1010 static UBool
michael@0 1011 makeFromUTable(CnvExtData *extData, UCMTable *table) {
michael@0 1012 uint16_t *stage1;
michael@0 1013 int32_t i, stage1Top, fromUCount;
michael@0 1014
michael@0 1015 fromUCount=prepareFromUMappings(table);
michael@0 1016
michael@0 1017 extData->fromUTableUChars=utm_open("cnv extension fromUTableUChars", 0x10000, UCNV_EXT_FROM_U_DATA_MASK+1, 2);
michael@0 1018 extData->fromUTableValues=utm_open("cnv extension fromUTableValues", 0x10000, UCNV_EXT_FROM_U_DATA_MASK+1, 4);
michael@0 1019 extData->fromUBytes=utm_open("cnv extension fromUBytes", 0x10000, UCNV_EXT_FROM_U_DATA_MASK+1, 1);
michael@0 1020
michael@0 1021 /* allocate all-unassigned stage blocks */
michael@0 1022 extData->stage2Top=MBCS_STAGE_2_FIRST_ASSIGNED;
michael@0 1023 extData->stage3Top=MBCS_STAGE_3_FIRST_ASSIGNED;
michael@0 1024
michael@0 1025 /*
michael@0 1026 * stage 3b stores only unique values, and in
michael@0 1027 * index 0: 0 for "no mapping"
michael@0 1028 * index 1: "no mapping" with preference for <subchar1> rather than <subchar>
michael@0 1029 */
michael@0 1030 extData->stage3b[1]=UCNV_EXT_FROM_U_SUBCHAR1;
michael@0 1031 extData->stage3bTop=2;
michael@0 1032
michael@0 1033 /* allocate the first entry in the fromUTable because index 0 means "no result" */
michael@0 1034 utm_alloc(extData->fromUTableUChars);
michael@0 1035 utm_alloc(extData->fromUTableValues);
michael@0 1036
michael@0 1037 if(!generateFromUTrie(extData, table, fromUCount)) {
michael@0 1038 return FALSE;
michael@0 1039 }
michael@0 1040
michael@0 1041 /*
michael@0 1042 * offset the stage 1 trie entries by stage1Top because they will
michael@0 1043 * be stored in a single array
michael@0 1044 */
michael@0 1045 stage1=extData->stage1;
michael@0 1046 stage1Top=extData->stage1Top;
michael@0 1047 for(i=0; i<stage1Top; ++i) {
michael@0 1048 stage1[i]=(uint16_t)(stage1[i]+stage1Top);
michael@0 1049 }
michael@0 1050
michael@0 1051 return TRUE;
michael@0 1052 }
michael@0 1053
michael@0 1054 /* -------------------------------------------------------------------------- */
michael@0 1055
michael@0 1056 static UBool
michael@0 1057 CnvExtAddTable(NewConverter *cnvData, UCMTable *table, UConverterStaticData *staticData) {
michael@0 1058 CnvExtData *extData;
michael@0 1059
michael@0 1060 if(table->unicodeMask&UCNV_HAS_SURROGATES) {
michael@0 1061 fprintf(stderr, "error: contains mappings for surrogate code points\n");
michael@0 1062 return FALSE;
michael@0 1063 }
michael@0 1064
michael@0 1065 staticData->conversionType=UCNV_MBCS;
michael@0 1066
michael@0 1067 extData=(CnvExtData *)cnvData;
michael@0 1068
michael@0 1069 /*
michael@0 1070 * assume that the table is sorted
michael@0 1071 *
michael@0 1072 * call the functions in this order because
michael@0 1073 * makeToUTable() modifies the original reverseMap,
michael@0 1074 * makeFromUTable() writes a whole new mapping into reverseMap
michael@0 1075 */
michael@0 1076 return
michael@0 1077 makeToUTable(extData, table) &&
michael@0 1078 makeFromUTable(extData, table);
michael@0 1079 }

mercurial