intl/icu/source/tools/makeconv/genmbcs.cpp

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) 2000-2013, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 *******************************************************************************
michael@0 8 * file name: genmbcs.cpp
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: 2000jul06
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 "cstring.h"
michael@0 20 #include "cmemory.h"
michael@0 21 #include "unewdata.h"
michael@0 22 #include "ucnv_cnv.h"
michael@0 23 #include "ucnvmbcs.h"
michael@0 24 #include "ucm.h"
michael@0 25 #include "makeconv.h"
michael@0 26 #include "genmbcs.h"
michael@0 27
michael@0 28 /*
michael@0 29 * TODO: Split this file into toUnicode, SBCSFromUnicode and MBCSFromUnicode files.
michael@0 30 * Reduce tests for maxCharLength.
michael@0 31 */
michael@0 32
michael@0 33 struct MBCSData {
michael@0 34 NewConverter newConverter;
michael@0 35
michael@0 36 UCMFile *ucm;
michael@0 37
michael@0 38 /* toUnicode (state table in ucm->states) */
michael@0 39 _MBCSToUFallback toUFallbacks[MBCS_MAX_FALLBACK_COUNT];
michael@0 40 int32_t countToUFallbacks;
michael@0 41 uint16_t *unicodeCodeUnits;
michael@0 42
michael@0 43 /* fromUnicode */
michael@0 44 uint16_t stage1[MBCS_STAGE_1_SIZE];
michael@0 45 uint16_t stage2Single[MBCS_STAGE_2_SIZE]; /* stage 2 for single-byte codepages */
michael@0 46 uint32_t stage2[MBCS_STAGE_2_SIZE]; /* stage 2 for MBCS */
michael@0 47 uint8_t *fromUBytes;
michael@0 48 uint32_t stage2Top, stage3Top;
michael@0 49
michael@0 50 /* fromUTF8 */
michael@0 51 uint16_t stageUTF8[0x10000>>MBCS_UTF8_STAGE_SHIFT]; /* allow for utf8Max=0xffff */
michael@0 52
michael@0 53 /*
michael@0 54 * Maximum UTF-8-friendly code point.
michael@0 55 * 0 if !utf8Friendly, otherwise 0x01ff..0xffff in steps of 0x100.
michael@0 56 * If utf8Friendly, utf8Max is normally either MBCS_UTF8_MAX or 0xffff.
michael@0 57 */
michael@0 58 uint16_t utf8Max;
michael@0 59
michael@0 60 UBool utf8Friendly;
michael@0 61 UBool omitFromU;
michael@0 62 };
michael@0 63
michael@0 64 /* prototypes */
michael@0 65 static void
michael@0 66 MBCSClose(NewConverter *cnvData);
michael@0 67
michael@0 68 static UBool
michael@0 69 MBCSStartMappings(MBCSData *mbcsData);
michael@0 70
michael@0 71 static UBool
michael@0 72 MBCSAddToUnicode(MBCSData *mbcsData,
michael@0 73 const uint8_t *bytes, int32_t length,
michael@0 74 UChar32 c,
michael@0 75 int8_t flag);
michael@0 76
michael@0 77 static UBool
michael@0 78 MBCSIsValid(NewConverter *cnvData,
michael@0 79 const uint8_t *bytes, int32_t length);
michael@0 80
michael@0 81 static UBool
michael@0 82 MBCSSingleAddFromUnicode(MBCSData *mbcsData,
michael@0 83 const uint8_t *bytes, int32_t length,
michael@0 84 UChar32 c,
michael@0 85 int8_t flag);
michael@0 86
michael@0 87 static UBool
michael@0 88 MBCSAddFromUnicode(MBCSData *mbcsData,
michael@0 89 const uint8_t *bytes, int32_t length,
michael@0 90 UChar32 c,
michael@0 91 int8_t flag);
michael@0 92
michael@0 93 static void
michael@0 94 MBCSPostprocess(MBCSData *mbcsData, const UConverterStaticData *staticData);
michael@0 95
michael@0 96 static UBool
michael@0 97 MBCSAddTable(NewConverter *cnvData, UCMTable *table, UConverterStaticData *staticData);
michael@0 98
michael@0 99 static uint32_t
michael@0 100 MBCSWrite(NewConverter *cnvData, const UConverterStaticData *staticData,
michael@0 101 UNewDataMemory *pData, int32_t tableType);
michael@0 102
michael@0 103 /* helper ------------------------------------------------------------------- */
michael@0 104
michael@0 105 static inline char
michael@0 106 hexDigit(uint8_t digit) {
michael@0 107 return digit<=9 ? (char)('0'+digit) : (char)('a'-10+digit);
michael@0 108 }
michael@0 109
michael@0 110 static inline char *
michael@0 111 printBytes(char *buffer, const uint8_t *bytes, int32_t length) {
michael@0 112 char *s=buffer;
michael@0 113 while(length>0) {
michael@0 114 *s++=hexDigit((uint8_t)(*bytes>>4));
michael@0 115 *s++=hexDigit((uint8_t)(*bytes&0xf));
michael@0 116 ++bytes;
michael@0 117 --length;
michael@0 118 }
michael@0 119
michael@0 120 *s=0;
michael@0 121 return buffer;
michael@0 122 }
michael@0 123
michael@0 124 /* implementation ----------------------------------------------------------- */
michael@0 125
michael@0 126 static MBCSData gDummy;
michael@0 127
michael@0 128 U_CFUNC const MBCSData *
michael@0 129 MBCSGetDummy() {
michael@0 130 uprv_memset(&gDummy, 0, sizeof(MBCSData));
michael@0 131
michael@0 132 /*
michael@0 133 * Set "pessimistic" values which may sometimes move too many
michael@0 134 * mappings to the extension table (but never too few).
michael@0 135 * These values cause MBCSOkForBaseFromUnicode() to return FALSE for the
michael@0 136 * largest set of mappings.
michael@0 137 * Assume maxCharLength>1.
michael@0 138 */
michael@0 139 gDummy.utf8Friendly=TRUE;
michael@0 140 if(SMALL) {
michael@0 141 gDummy.utf8Max=0xffff;
michael@0 142 gDummy.omitFromU=TRUE;
michael@0 143 } else {
michael@0 144 gDummy.utf8Max=MBCS_UTF8_MAX;
michael@0 145 }
michael@0 146 return &gDummy;
michael@0 147 }
michael@0 148
michael@0 149 static void
michael@0 150 MBCSInit(MBCSData *mbcsData, UCMFile *ucm) {
michael@0 151 uprv_memset(mbcsData, 0, sizeof(MBCSData));
michael@0 152
michael@0 153 mbcsData->ucm=ucm; /* aliased, not owned */
michael@0 154
michael@0 155 mbcsData->newConverter.close=MBCSClose;
michael@0 156 mbcsData->newConverter.isValid=MBCSIsValid;
michael@0 157 mbcsData->newConverter.addTable=MBCSAddTable;
michael@0 158 mbcsData->newConverter.write=MBCSWrite;
michael@0 159 }
michael@0 160
michael@0 161 NewConverter *
michael@0 162 MBCSOpen(UCMFile *ucm) {
michael@0 163 MBCSData *mbcsData=(MBCSData *)uprv_malloc(sizeof(MBCSData));
michael@0 164 if(mbcsData==NULL) {
michael@0 165 printf("out of memory\n");
michael@0 166 exit(U_MEMORY_ALLOCATION_ERROR);
michael@0 167 }
michael@0 168
michael@0 169 MBCSInit(mbcsData, ucm);
michael@0 170 return &mbcsData->newConverter;
michael@0 171 }
michael@0 172
michael@0 173 static void
michael@0 174 MBCSDestruct(MBCSData *mbcsData) {
michael@0 175 uprv_free(mbcsData->unicodeCodeUnits);
michael@0 176 uprv_free(mbcsData->fromUBytes);
michael@0 177 }
michael@0 178
michael@0 179 static void
michael@0 180 MBCSClose(NewConverter *cnvData) {
michael@0 181 MBCSData *mbcsData=(MBCSData *)cnvData;
michael@0 182 if(mbcsData!=NULL) {
michael@0 183 MBCSDestruct(mbcsData);
michael@0 184 uprv_free(mbcsData);
michael@0 185 }
michael@0 186 }
michael@0 187
michael@0 188 static UBool
michael@0 189 MBCSStartMappings(MBCSData *mbcsData) {
michael@0 190 int32_t i, sum, maxCharLength,
michael@0 191 stage2NullLength, stage2AllocLength,
michael@0 192 stage3NullLength, stage3AllocLength;
michael@0 193
michael@0 194 /* toUnicode */
michael@0 195
michael@0 196 /* allocate the code unit array and prefill it with "unassigned" values */
michael@0 197 sum=mbcsData->ucm->states.countToUCodeUnits;
michael@0 198 if(VERBOSE) {
michael@0 199 printf("the total number of offsets is 0x%lx=%ld\n", (long)sum, (long)sum);
michael@0 200 }
michael@0 201
michael@0 202 if(sum>0) {
michael@0 203 mbcsData->unicodeCodeUnits=(uint16_t *)uprv_malloc(sum*sizeof(uint16_t));
michael@0 204 if(mbcsData->unicodeCodeUnits==NULL) {
michael@0 205 fprintf(stderr, "error: out of memory allocating %ld 16-bit code units\n",
michael@0 206 (long)sum);
michael@0 207 return FALSE;
michael@0 208 }
michael@0 209 for(i=0; i<sum; ++i) {
michael@0 210 mbcsData->unicodeCodeUnits[i]=0xfffe;
michael@0 211 }
michael@0 212 }
michael@0 213
michael@0 214 /* fromUnicode */
michael@0 215 maxCharLength=mbcsData->ucm->states.maxCharLength;
michael@0 216
michael@0 217 /* allocate the codepage mappings and preset the first 16 characters to 0 */
michael@0 218 if(maxCharLength==1) {
michael@0 219 /* allocate 64k 16-bit results for single-byte codepages */
michael@0 220 sum=0x20000;
michael@0 221 } else {
michael@0 222 /* allocate 1M * maxCharLength bytes for at most 1M mappings */
michael@0 223 sum=0x100000*maxCharLength;
michael@0 224 }
michael@0 225 mbcsData->fromUBytes=(uint8_t *)uprv_malloc(sum);
michael@0 226 if(mbcsData->fromUBytes==NULL) {
michael@0 227 fprintf(stderr, "error: out of memory allocating %ld B for target mappings\n", (long)sum);
michael@0 228 return FALSE;
michael@0 229 }
michael@0 230 uprv_memset(mbcsData->fromUBytes, 0, sum);
michael@0 231
michael@0 232 /*
michael@0 233 * UTF-8-friendly fromUnicode tries: allocate multiple blocks at a time.
michael@0 234 * See ucnvmbcs.h for details.
michael@0 235 *
michael@0 236 * There is code, for example in ucnv_MBCSGetUnicodeSetForUnicode(), which
michael@0 237 * assumes that the initial stage 2/3 blocks are the all-unassigned ones.
michael@0 238 * Therefore, we refine the data structure while maintaining this placement
michael@0 239 * even though it would be convenient to allocate the ASCII block at the
michael@0 240 * beginning of stage 3, for example.
michael@0 241 *
michael@0 242 * UTF-8-friendly fromUnicode tries work from sorted tables and are built
michael@0 243 * pre-compacted, overlapping adjacent stage 2/3 blocks.
michael@0 244 * This is necessary because the block allocation and compaction changes
michael@0 245 * at SBCS_UTF8_MAX or MBCS_UTF8_MAX, and for MBCS tables the additional
michael@0 246 * stage table uses direct indexes into stage 3, without a multiplier and
michael@0 247 * thus with a smaller reach.
michael@0 248 *
michael@0 249 * Non-UTF-8-friendly fromUnicode tries work from unsorted tables
michael@0 250 * (because implicit precision is used), and are compacted
michael@0 251 * in post-processing.
michael@0 252 *
michael@0 253 * Preallocation for UTF-8-friendly fromUnicode tries:
michael@0 254 *
michael@0 255 * Stage 3:
michael@0 256 * 64-entry all-unassigned first block followed by ASCII (128 entries).
michael@0 257 *
michael@0 258 * Stage 2:
michael@0 259 * 64-entry all-unassigned first block followed by preallocated
michael@0 260 * 64-block for ASCII.
michael@0 261 */
michael@0 262
michael@0 263 /* Preallocate ASCII as a linear 128-entry stage 3 block. */
michael@0 264 stage2NullLength=MBCS_STAGE_2_BLOCK_SIZE;
michael@0 265 stage2AllocLength=MBCS_STAGE_2_BLOCK_SIZE;
michael@0 266
michael@0 267 stage3NullLength=MBCS_UTF8_STAGE_3_BLOCK_SIZE;
michael@0 268 stage3AllocLength=128; /* ASCII U+0000..U+007f */
michael@0 269
michael@0 270 /* Initialize stage 1 for the preallocated blocks. */
michael@0 271 sum=stage2NullLength;
michael@0 272 for(i=0; i<(stage2AllocLength>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT); ++i) {
michael@0 273 mbcsData->stage1[i]=sum;
michael@0 274 sum+=MBCS_STAGE_2_BLOCK_SIZE;
michael@0 275 }
michael@0 276 mbcsData->stage2Top=stage2NullLength+stage2AllocLength; /* ==sum */
michael@0 277
michael@0 278 /*
michael@0 279 * Stage 2 indexes count 16-blocks in stage 3 as follows:
michael@0 280 * SBCS: directly, indexes increment by 16
michael@0 281 * MBCS: indexes need to be multiplied by 16*maxCharLength, indexes increment by 1
michael@0 282 * MBCS UTF-8: directly, indexes increment by 16
michael@0 283 */
michael@0 284 if(maxCharLength==1) {
michael@0 285 sum=stage3NullLength;
michael@0 286 for(i=0; i<(stage3AllocLength/MBCS_STAGE_3_BLOCK_SIZE); ++i) {
michael@0 287 mbcsData->stage2Single[mbcsData->stage1[0]+i]=sum;
michael@0 288 sum+=MBCS_STAGE_3_BLOCK_SIZE;
michael@0 289 }
michael@0 290 } else {
michael@0 291 sum=stage3NullLength/MBCS_STAGE_3_GRANULARITY;
michael@0 292 for(i=0; i<(stage3AllocLength/MBCS_STAGE_3_BLOCK_SIZE); ++i) {
michael@0 293 mbcsData->stage2[mbcsData->stage1[0]+i]=sum;
michael@0 294 sum+=MBCS_STAGE_3_BLOCK_SIZE/MBCS_STAGE_3_GRANULARITY;
michael@0 295 }
michael@0 296 }
michael@0 297
michael@0 298 sum=stage3NullLength;
michael@0 299 for(i=0; i<(stage3AllocLength/MBCS_UTF8_STAGE_3_BLOCK_SIZE); ++i) {
michael@0 300 mbcsData->stageUTF8[i]=sum;
michael@0 301 sum+=MBCS_UTF8_STAGE_3_BLOCK_SIZE;
michael@0 302 }
michael@0 303
michael@0 304 /*
michael@0 305 * Allocate a 64-entry all-unassigned first stage 3 block,
michael@0 306 * for UTF-8-friendly lookup with a trail byte,
michael@0 307 * plus 128 entries for ASCII.
michael@0 308 */
michael@0 309 mbcsData->stage3Top=(stage3NullLength+stage3AllocLength)*maxCharLength; /* ==sum*maxCharLength */
michael@0 310
michael@0 311 return TRUE;
michael@0 312 }
michael@0 313
michael@0 314 /* return TRUE for success */
michael@0 315 static UBool
michael@0 316 setFallback(MBCSData *mbcsData, uint32_t offset, UChar32 c) {
michael@0 317 int32_t i=ucm_findFallback(mbcsData->toUFallbacks, mbcsData->countToUFallbacks, offset);
michael@0 318 if(i>=0) {
michael@0 319 /* if there is already a fallback for this offset, then overwrite it */
michael@0 320 mbcsData->toUFallbacks[i].codePoint=c;
michael@0 321 return TRUE;
michael@0 322 } else {
michael@0 323 /* if there is no fallback for this offset, then add one */
michael@0 324 i=mbcsData->countToUFallbacks;
michael@0 325 if(i>=MBCS_MAX_FALLBACK_COUNT) {
michael@0 326 fprintf(stderr, "error: too many toUnicode fallbacks, currently at: U+%x\n", (int)c);
michael@0 327 return FALSE;
michael@0 328 } else {
michael@0 329 mbcsData->toUFallbacks[i].offset=offset;
michael@0 330 mbcsData->toUFallbacks[i].codePoint=c;
michael@0 331 mbcsData->countToUFallbacks=i+1;
michael@0 332 return TRUE;
michael@0 333 }
michael@0 334 }
michael@0 335 }
michael@0 336
michael@0 337 /* remove fallback if there is one with this offset; return the code point if there was such a fallback, otherwise -1 */
michael@0 338 static int32_t
michael@0 339 removeFallback(MBCSData *mbcsData, uint32_t offset) {
michael@0 340 int32_t i=ucm_findFallback(mbcsData->toUFallbacks, mbcsData->countToUFallbacks, offset);
michael@0 341 if(i>=0) {
michael@0 342 _MBCSToUFallback *toUFallbacks;
michael@0 343 int32_t limit, old;
michael@0 344
michael@0 345 toUFallbacks=mbcsData->toUFallbacks;
michael@0 346 limit=mbcsData->countToUFallbacks;
michael@0 347 old=(int32_t)toUFallbacks[i].codePoint;
michael@0 348
michael@0 349 /* copy the last fallback entry here to keep the list contiguous */
michael@0 350 toUFallbacks[i].offset=toUFallbacks[limit-1].offset;
michael@0 351 toUFallbacks[i].codePoint=toUFallbacks[limit-1].codePoint;
michael@0 352 mbcsData->countToUFallbacks=limit-1;
michael@0 353 return old;
michael@0 354 } else {
michael@0 355 return -1;
michael@0 356 }
michael@0 357 }
michael@0 358
michael@0 359 /*
michael@0 360 * isFallback is almost a boolean:
michael@0 361 * 1 (TRUE) this is a fallback mapping
michael@0 362 * 0 (FALSE) this is a precise mapping
michael@0 363 * -1 the precision of this mapping is not specified
michael@0 364 */
michael@0 365 static UBool
michael@0 366 MBCSAddToUnicode(MBCSData *mbcsData,
michael@0 367 const uint8_t *bytes, int32_t length,
michael@0 368 UChar32 c,
michael@0 369 int8_t flag) {
michael@0 370 char buffer[10];
michael@0 371 uint32_t offset=0;
michael@0 372 int32_t i=0, entry, old;
michael@0 373 uint8_t state=0;
michael@0 374
michael@0 375 if(mbcsData->ucm->states.countStates==0) {
michael@0 376 fprintf(stderr, "error: there is no state information!\n");
michael@0 377 return FALSE;
michael@0 378 }
michael@0 379
michael@0 380 /* for SI/SO (like EBCDIC-stateful), double-byte sequences start in state 1 */
michael@0 381 if(length==2 && mbcsData->ucm->states.outputType==MBCS_OUTPUT_2_SISO) {
michael@0 382 state=1;
michael@0 383 }
michael@0 384
michael@0 385 /*
michael@0 386 * Walk down the state table like in conversion,
michael@0 387 * much like getNextUChar().
michael@0 388 * We assume that c<=0x10ffff.
michael@0 389 */
michael@0 390 for(i=0;;) {
michael@0 391 entry=mbcsData->ucm->states.stateTable[state][bytes[i++]];
michael@0 392 if(MBCS_ENTRY_IS_TRANSITION(entry)) {
michael@0 393 if(i==length) {
michael@0 394 fprintf(stderr, "error: byte sequence too short, ends in non-final state %hu: 0x%s (U+%x)\n",
michael@0 395 (short)state, printBytes(buffer, bytes, length), (int)c);
michael@0 396 return FALSE;
michael@0 397 }
michael@0 398 state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
michael@0 399 offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
michael@0 400 } else {
michael@0 401 if(i<length) {
michael@0 402 fprintf(stderr, "error: byte sequence too long by %d bytes, final state %u: 0x%s (U+%x)\n",
michael@0 403 (int)(length-i), state, printBytes(buffer, bytes, length), (int)c);
michael@0 404 return FALSE;
michael@0 405 }
michael@0 406 switch(MBCS_ENTRY_FINAL_ACTION(entry)) {
michael@0 407 case MBCS_STATE_ILLEGAL:
michael@0 408 fprintf(stderr, "error: byte sequence ends in illegal state at U+%04x<->0x%s\n",
michael@0 409 (int)c, printBytes(buffer, bytes, length));
michael@0 410 return FALSE;
michael@0 411 case MBCS_STATE_CHANGE_ONLY:
michael@0 412 fprintf(stderr, "error: byte sequence ends in state-change-only at U+%04x<->0x%s\n",
michael@0 413 (int)c, printBytes(buffer, bytes, length));
michael@0 414 return FALSE;
michael@0 415 case MBCS_STATE_UNASSIGNED:
michael@0 416 fprintf(stderr, "error: byte sequence ends in unassigned state at U+%04x<->0x%s\n",
michael@0 417 (int)c, printBytes(buffer, bytes, length));
michael@0 418 return FALSE;
michael@0 419 case MBCS_STATE_FALLBACK_DIRECT_16:
michael@0 420 case MBCS_STATE_VALID_DIRECT_16:
michael@0 421 case MBCS_STATE_FALLBACK_DIRECT_20:
michael@0 422 case MBCS_STATE_VALID_DIRECT_20:
michael@0 423 if(MBCS_ENTRY_SET_STATE(entry, 0)!=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, 0xfffe)) {
michael@0 424 /* the "direct" action's value is not "valid-direct-16-unassigned" any more */
michael@0 425 if(MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_DIRECT_16 || MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_FALLBACK_DIRECT_16) {
michael@0 426 old=MBCS_ENTRY_FINAL_VALUE(entry);
michael@0 427 } else {
michael@0 428 old=0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
michael@0 429 }
michael@0 430 if(flag>=0) {
michael@0 431 fprintf(stderr, "error: duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
michael@0 432 (int)c, printBytes(buffer, bytes, length), (int)old);
michael@0 433 return FALSE;
michael@0 434 } else if(VERBOSE) {
michael@0 435 fprintf(stderr, "duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
michael@0 436 (int)c, printBytes(buffer, bytes, length), (int)old);
michael@0 437 }
michael@0 438 /*
michael@0 439 * Continue after the above warning
michael@0 440 * if the precision of the mapping is unspecified.
michael@0 441 */
michael@0 442 }
michael@0 443 /* reassign the correct action code */
michael@0 444 entry=MBCS_ENTRY_FINAL_SET_ACTION(entry, (MBCS_STATE_VALID_DIRECT_16+(flag==3 ? 2 : 0)+(c>=0x10000 ? 1 : 0)));
michael@0 445
michael@0 446 /* put the code point into bits 22..7 for BMP, c-0x10000 into 26..7 for others */
michael@0 447 if(c<=0xffff) {
michael@0 448 entry=MBCS_ENTRY_FINAL_SET_VALUE(entry, c);
michael@0 449 } else {
michael@0 450 entry=MBCS_ENTRY_FINAL_SET_VALUE(entry, c-0x10000);
michael@0 451 }
michael@0 452 mbcsData->ucm->states.stateTable[state][bytes[i-1]]=entry;
michael@0 453 break;
michael@0 454 case MBCS_STATE_VALID_16:
michael@0 455 /* bits 26..16 are not used, 0 */
michael@0 456 /* bits 15..7 contain the final offset delta to one 16-bit code unit */
michael@0 457 offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
michael@0 458 /* check that this byte sequence is still unassigned */
michael@0 459 if((old=mbcsData->unicodeCodeUnits[offset])!=0xfffe || (old=removeFallback(mbcsData, offset))!=-1) {
michael@0 460 if(flag>=0) {
michael@0 461 fprintf(stderr, "error: duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
michael@0 462 (int)c, printBytes(buffer, bytes, length), (int)old);
michael@0 463 return FALSE;
michael@0 464 } else if(VERBOSE) {
michael@0 465 fprintf(stderr, "duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
michael@0 466 (int)c, printBytes(buffer, bytes, length), (int)old);
michael@0 467 }
michael@0 468 }
michael@0 469 if(c>=0x10000) {
michael@0 470 fprintf(stderr, "error: code point does not fit into valid-16-bit state at U+%04x<->0x%s\n",
michael@0 471 (int)c, printBytes(buffer, bytes, length));
michael@0 472 return FALSE;
michael@0 473 }
michael@0 474 if(flag>0) {
michael@0 475 /* assign only if there is no precise mapping */
michael@0 476 if(mbcsData->unicodeCodeUnits[offset]==0xfffe) {
michael@0 477 return setFallback(mbcsData, offset, c);
michael@0 478 }
michael@0 479 } else {
michael@0 480 mbcsData->unicodeCodeUnits[offset]=(uint16_t)c;
michael@0 481 }
michael@0 482 break;
michael@0 483 case MBCS_STATE_VALID_16_PAIR:
michael@0 484 /* bits 26..16 are not used, 0 */
michael@0 485 /* bits 15..7 contain the final offset delta to two 16-bit code units */
michael@0 486 offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
michael@0 487 /* check that this byte sequence is still unassigned */
michael@0 488 old=mbcsData->unicodeCodeUnits[offset];
michael@0 489 if(old<0xfffe) {
michael@0 490 int32_t real;
michael@0 491 if(old<0xd800) {
michael@0 492 real=old;
michael@0 493 } else if(old<=0xdfff) {
michael@0 494 real=0x10000+((old&0x3ff)<<10)+((mbcsData->unicodeCodeUnits[offset+1])&0x3ff);
michael@0 495 } else /* old<=0xe001 */ {
michael@0 496 real=mbcsData->unicodeCodeUnits[offset+1];
michael@0 497 }
michael@0 498 if(flag>=0) {
michael@0 499 fprintf(stderr, "error: duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
michael@0 500 (int)c, printBytes(buffer, bytes, length), (int)real);
michael@0 501 return FALSE;
michael@0 502 } else if(VERBOSE) {
michael@0 503 fprintf(stderr, "duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
michael@0 504 (int)c, printBytes(buffer, bytes, length), (int)real);
michael@0 505 }
michael@0 506 }
michael@0 507 if(flag>0) {
michael@0 508 /* assign only if there is no precise mapping */
michael@0 509 if(old<=0xdbff || old==0xe000) {
michael@0 510 /* do nothing */
michael@0 511 } else if(c<=0xffff) {
michael@0 512 /* set a BMP fallback code point as a pair with 0xe001 */
michael@0 513 mbcsData->unicodeCodeUnits[offset++]=0xe001;
michael@0 514 mbcsData->unicodeCodeUnits[offset]=(uint16_t)c;
michael@0 515 } else {
michael@0 516 /* set a fallback surrogate pair with two second surrogates */
michael@0 517 mbcsData->unicodeCodeUnits[offset++]=(uint16_t)(0xdbc0+(c>>10));
michael@0 518 mbcsData->unicodeCodeUnits[offset]=(uint16_t)(0xdc00+(c&0x3ff));
michael@0 519 }
michael@0 520 } else {
michael@0 521 if(c<0xd800) {
michael@0 522 /* set a BMP code point */
michael@0 523 mbcsData->unicodeCodeUnits[offset]=(uint16_t)c;
michael@0 524 } else if(c<=0xffff) {
michael@0 525 /* set a BMP code point above 0xd800 as a pair with 0xe000 */
michael@0 526 mbcsData->unicodeCodeUnits[offset++]=0xe000;
michael@0 527 mbcsData->unicodeCodeUnits[offset]=(uint16_t)c;
michael@0 528 } else {
michael@0 529 /* set a surrogate pair */
michael@0 530 mbcsData->unicodeCodeUnits[offset++]=(uint16_t)(0xd7c0+(c>>10));
michael@0 531 mbcsData->unicodeCodeUnits[offset]=(uint16_t)(0xdc00+(c&0x3ff));
michael@0 532 }
michael@0 533 }
michael@0 534 break;
michael@0 535 default:
michael@0 536 /* reserved, must never occur */
michael@0 537 fprintf(stderr, "internal error: byte sequence reached reserved action code, entry 0x%02x: 0x%s (U+%x)\n",
michael@0 538 (int)entry, printBytes(buffer, bytes, length), (int)c);
michael@0 539 return FALSE;
michael@0 540 }
michael@0 541
michael@0 542 return TRUE;
michael@0 543 }
michael@0 544 }
michael@0 545 }
michael@0 546
michael@0 547 /* is this byte sequence valid? (this is almost the same as MBCSAddToUnicode()) */
michael@0 548 static UBool
michael@0 549 MBCSIsValid(NewConverter *cnvData,
michael@0 550 const uint8_t *bytes, int32_t length) {
michael@0 551 MBCSData *mbcsData=(MBCSData *)cnvData;
michael@0 552
michael@0 553 return (UBool)(1==ucm_countChars(&mbcsData->ucm->states, bytes, length));
michael@0 554 }
michael@0 555
michael@0 556 static UBool
michael@0 557 MBCSSingleAddFromUnicode(MBCSData *mbcsData,
michael@0 558 const uint8_t *bytes, int32_t /*length*/,
michael@0 559 UChar32 c,
michael@0 560 int8_t flag) {
michael@0 561 uint16_t *stage3, *p;
michael@0 562 uint32_t idx;
michael@0 563 uint16_t old;
michael@0 564 uint8_t b;
michael@0 565
michael@0 566 uint32_t blockSize, newTop, i, nextOffset, newBlock, min;
michael@0 567
michael@0 568 /* ignore |2 SUB mappings */
michael@0 569 if(flag==2) {
michael@0 570 return TRUE;
michael@0 571 }
michael@0 572
michael@0 573 /*
michael@0 574 * Walk down the triple-stage compact array ("trie") and
michael@0 575 * allocate parts as necessary.
michael@0 576 * Note that the first stage 2 and 3 blocks are reserved for all-unassigned mappings.
michael@0 577 * We assume that length<=maxCharLength and that c<=0x10ffff.
michael@0 578 */
michael@0 579 stage3=(uint16_t *)mbcsData->fromUBytes;
michael@0 580 b=*bytes;
michael@0 581
michael@0 582 /* inspect stage 1 */
michael@0 583 idx=c>>MBCS_STAGE_1_SHIFT;
michael@0 584 if(mbcsData->utf8Friendly && c<=SBCS_UTF8_MAX) {
michael@0 585 nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK&~(MBCS_UTF8_STAGE_3_BLOCKS-1);
michael@0 586 } else {
michael@0 587 nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK;
michael@0 588 }
michael@0 589 if(mbcsData->stage1[idx]==MBCS_STAGE_2_ALL_UNASSIGNED_INDEX) {
michael@0 590 /* allocate another block in stage 2 */
michael@0 591 newBlock=mbcsData->stage2Top;
michael@0 592 if(mbcsData->utf8Friendly) {
michael@0 593 min=newBlock-nextOffset; /* minimum block start with overlap */
michael@0 594 while(min<newBlock && mbcsData->stage2Single[newBlock-1]==0) {
michael@0 595 --newBlock;
michael@0 596 }
michael@0 597 }
michael@0 598 newTop=newBlock+MBCS_STAGE_2_BLOCK_SIZE;
michael@0 599
michael@0 600 if(newTop>MBCS_MAX_STAGE_2_TOP) {
michael@0 601 fprintf(stderr, "error: too many stage 2 entries at U+%04x<->0x%02x\n", (int)c, b);
michael@0 602 return FALSE;
michael@0 603 }
michael@0 604
michael@0 605 /*
michael@0 606 * each stage 2 block contains 64 16-bit words:
michael@0 607 * 6 code point bits 9..4 with 1 stage 3 index
michael@0 608 */
michael@0 609 mbcsData->stage1[idx]=(uint16_t)newBlock;
michael@0 610 mbcsData->stage2Top=newTop;
michael@0 611 }
michael@0 612
michael@0 613 /* inspect stage 2 */
michael@0 614 idx=mbcsData->stage1[idx]+nextOffset;
michael@0 615 if(mbcsData->utf8Friendly && c<=SBCS_UTF8_MAX) {
michael@0 616 /* allocate 64-entry blocks for UTF-8-friendly lookup */
michael@0 617 blockSize=MBCS_UTF8_STAGE_3_BLOCK_SIZE;
michael@0 618 nextOffset=c&MBCS_UTF8_STAGE_3_BLOCK_MASK;
michael@0 619 } else {
michael@0 620 blockSize=MBCS_STAGE_3_BLOCK_SIZE;
michael@0 621 nextOffset=c&MBCS_STAGE_3_BLOCK_MASK;
michael@0 622 }
michael@0 623 if(mbcsData->stage2Single[idx]==0) {
michael@0 624 /* allocate another block in stage 3 */
michael@0 625 newBlock=mbcsData->stage3Top;
michael@0 626 if(mbcsData->utf8Friendly) {
michael@0 627 min=newBlock-nextOffset; /* minimum block start with overlap */
michael@0 628 while(min<newBlock && stage3[newBlock-1]==0) {
michael@0 629 --newBlock;
michael@0 630 }
michael@0 631 }
michael@0 632 newTop=newBlock+blockSize;
michael@0 633
michael@0 634 if(newTop>MBCS_STAGE_3_SBCS_SIZE) {
michael@0 635 fprintf(stderr, "error: too many code points at U+%04x<->0x%02x\n", (int)c, b);
michael@0 636 return FALSE;
michael@0 637 }
michael@0 638 /* each block has 16 uint16_t entries */
michael@0 639 i=idx;
michael@0 640 while(newBlock<newTop) {
michael@0 641 mbcsData->stage2Single[i++]=(uint16_t)newBlock;
michael@0 642 newBlock+=MBCS_STAGE_3_BLOCK_SIZE;
michael@0 643 }
michael@0 644 mbcsData->stage3Top=newTop; /* ==newBlock */
michael@0 645 }
michael@0 646
michael@0 647 /* write the codepage entry into stage 3 and get the previous entry */
michael@0 648 p=stage3+mbcsData->stage2Single[idx]+nextOffset;
michael@0 649 old=*p;
michael@0 650 if(flag<=0) {
michael@0 651 *p=(uint16_t)(0xf00|b);
michael@0 652 } else if(IS_PRIVATE_USE(c)) {
michael@0 653 *p=(uint16_t)(0xc00|b);
michael@0 654 } else {
michael@0 655 *p=(uint16_t)(0x800|b);
michael@0 656 }
michael@0 657
michael@0 658 /* check that this Unicode code point was still unassigned */
michael@0 659 if(old>=0x100) {
michael@0 660 if(flag>=0) {
michael@0 661 fprintf(stderr, "error: duplicate Unicode code point at U+%04x<->0x%02x see 0x%02x\n",
michael@0 662 (int)c, b, old&0xff);
michael@0 663 return FALSE;
michael@0 664 } else if(VERBOSE) {
michael@0 665 fprintf(stderr, "duplicate Unicode code point at U+%04x<->0x%02x see 0x%02x\n",
michael@0 666 (int)c, b, old&0xff);
michael@0 667 }
michael@0 668 /* continue after the above warning if the precision of the mapping is unspecified */
michael@0 669 }
michael@0 670
michael@0 671 return TRUE;
michael@0 672 }
michael@0 673
michael@0 674 static UBool
michael@0 675 MBCSAddFromUnicode(MBCSData *mbcsData,
michael@0 676 const uint8_t *bytes, int32_t length,
michael@0 677 UChar32 c,
michael@0 678 int8_t flag) {
michael@0 679 char buffer[10];
michael@0 680 const uint8_t *pb;
michael@0 681 uint8_t *stage3, *p;
michael@0 682 uint32_t idx, b, old, stage3Index;
michael@0 683 int32_t maxCharLength;
michael@0 684
michael@0 685 uint32_t blockSize, newTop, i, nextOffset, newBlock, min, overlap, maxOverlap;
michael@0 686
michael@0 687 maxCharLength=mbcsData->ucm->states.maxCharLength;
michael@0 688
michael@0 689 if( mbcsData->ucm->states.outputType==MBCS_OUTPUT_2_SISO &&
michael@0 690 (!IGNORE_SISO_CHECK && (*bytes==0xe || *bytes==0xf))
michael@0 691 ) {
michael@0 692 fprintf(stderr, "error: illegal mapping to SI or SO for SI/SO codepage: U+%04x<->0x%s\n",
michael@0 693 (int)c, printBytes(buffer, bytes, length));
michael@0 694 return FALSE;
michael@0 695 }
michael@0 696
michael@0 697 if(flag==1 && length==1 && *bytes==0) {
michael@0 698 fprintf(stderr, "error: unable to encode a |1 fallback from U+%04x to 0x%02x\n",
michael@0 699 (int)c, *bytes);
michael@0 700 return FALSE;
michael@0 701 }
michael@0 702
michael@0 703 /*
michael@0 704 * Walk down the triple-stage compact array ("trie") and
michael@0 705 * allocate parts as necessary.
michael@0 706 * Note that the first stage 2 and 3 blocks are reserved for
michael@0 707 * all-unassigned mappings.
michael@0 708 * We assume that length<=maxCharLength and that c<=0x10ffff.
michael@0 709 */
michael@0 710 stage3=mbcsData->fromUBytes;
michael@0 711
michael@0 712 /* inspect stage 1 */
michael@0 713 idx=c>>MBCS_STAGE_1_SHIFT;
michael@0 714 if(mbcsData->utf8Friendly && c<=mbcsData->utf8Max) {
michael@0 715 nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK&~(MBCS_UTF8_STAGE_3_BLOCKS-1);
michael@0 716 } else {
michael@0 717 nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK;
michael@0 718 }
michael@0 719 if(mbcsData->stage1[idx]==MBCS_STAGE_2_ALL_UNASSIGNED_INDEX) {
michael@0 720 /* allocate another block in stage 2 */
michael@0 721 newBlock=mbcsData->stage2Top;
michael@0 722 if(mbcsData->utf8Friendly) {
michael@0 723 min=newBlock-nextOffset; /* minimum block start with overlap */
michael@0 724 while(min<newBlock && mbcsData->stage2[newBlock-1]==0) {
michael@0 725 --newBlock;
michael@0 726 }
michael@0 727 }
michael@0 728 newTop=newBlock+MBCS_STAGE_2_BLOCK_SIZE;
michael@0 729
michael@0 730 if(newTop>MBCS_MAX_STAGE_2_TOP) {
michael@0 731 fprintf(stderr, "error: too many stage 2 entries at U+%04x<->0x%s\n",
michael@0 732 (int)c, printBytes(buffer, bytes, length));
michael@0 733 return FALSE;
michael@0 734 }
michael@0 735
michael@0 736 /*
michael@0 737 * each stage 2 block contains 64 32-bit words:
michael@0 738 * 6 code point bits 9..4 with value with bits 31..16 "assigned" flags and bits 15..0 stage 3 index
michael@0 739 */
michael@0 740 i=idx;
michael@0 741 while(newBlock<newTop) {
michael@0 742 mbcsData->stage1[i++]=(uint16_t)newBlock;
michael@0 743 newBlock+=MBCS_STAGE_2_BLOCK_SIZE;
michael@0 744 }
michael@0 745 mbcsData->stage2Top=newTop; /* ==newBlock */
michael@0 746 }
michael@0 747
michael@0 748 /* inspect stage 2 */
michael@0 749 idx=mbcsData->stage1[idx]+nextOffset;
michael@0 750 if(mbcsData->utf8Friendly && c<=mbcsData->utf8Max) {
michael@0 751 /* allocate 64-entry blocks for UTF-8-friendly lookup */
michael@0 752 blockSize=MBCS_UTF8_STAGE_3_BLOCK_SIZE*maxCharLength;
michael@0 753 nextOffset=c&MBCS_UTF8_STAGE_3_BLOCK_MASK;
michael@0 754 } else {
michael@0 755 blockSize=MBCS_STAGE_3_BLOCK_SIZE*maxCharLength;
michael@0 756 nextOffset=c&MBCS_STAGE_3_BLOCK_MASK;
michael@0 757 }
michael@0 758 if(mbcsData->stage2[idx]==0) {
michael@0 759 /* allocate another block in stage 3 */
michael@0 760 newBlock=mbcsData->stage3Top;
michael@0 761 if(mbcsData->utf8Friendly && nextOffset>=MBCS_STAGE_3_GRANULARITY) {
michael@0 762 /*
michael@0 763 * Overlap stage 3 blocks only in multiples of 16-entry blocks
michael@0 764 * because of the indexing granularity in stage 2.
michael@0 765 */
michael@0 766 maxOverlap=(nextOffset&~(MBCS_STAGE_3_GRANULARITY-1))*maxCharLength;
michael@0 767 for(overlap=0;
michael@0 768 overlap<maxOverlap && stage3[newBlock-overlap-1]==0;
michael@0 769 ++overlap) {}
michael@0 770
michael@0 771 overlap=(overlap/MBCS_STAGE_3_GRANULARITY)/maxCharLength;
michael@0 772 overlap=(overlap*MBCS_STAGE_3_GRANULARITY)*maxCharLength;
michael@0 773
michael@0 774 newBlock-=overlap;
michael@0 775 }
michael@0 776 newTop=newBlock+blockSize;
michael@0 777
michael@0 778 if(newTop>MBCS_STAGE_3_MBCS_SIZE*(uint32_t)maxCharLength) {
michael@0 779 fprintf(stderr, "error: too many code points at U+%04x<->0x%s\n",
michael@0 780 (int)c, printBytes(buffer, bytes, length));
michael@0 781 return FALSE;
michael@0 782 }
michael@0 783 /* each block has 16*maxCharLength bytes */
michael@0 784 i=idx;
michael@0 785 while(newBlock<newTop) {
michael@0 786 mbcsData->stage2[i++]=(newBlock/MBCS_STAGE_3_GRANULARITY)/maxCharLength;
michael@0 787 newBlock+=MBCS_STAGE_3_BLOCK_SIZE*maxCharLength;
michael@0 788 }
michael@0 789 mbcsData->stage3Top=newTop; /* ==newBlock */
michael@0 790 }
michael@0 791
michael@0 792 stage3Index=MBCS_STAGE_3_GRANULARITY*(uint32_t)(uint16_t)mbcsData->stage2[idx];
michael@0 793
michael@0 794 /* Build an alternate, UTF-8-friendly stage table as well. */
michael@0 795 if(mbcsData->utf8Friendly && c<=mbcsData->utf8Max) {
michael@0 796 /* Overflow for uint16_t entries in stageUTF8? */
michael@0 797 if(stage3Index>0xffff) {
michael@0 798 /*
michael@0 799 * This can occur only if the mapping table is nearly perfectly filled and if
michael@0 800 * utf8Max==0xffff.
michael@0 801 * (There is no known charset like this. GB 18030 does not map
michael@0 802 * surrogate code points and LMBCS does not map 256 PUA code points.)
michael@0 803 *
michael@0 804 * Otherwise, stage3Index<=MBCS_UTF8_LIMIT<0xffff
michael@0 805 * (stage3Index can at most reach exactly MBCS_UTF8_LIMIT)
michael@0 806 * because we have a sorted table and there are at most MBCS_UTF8_LIMIT
michael@0 807 * mappings with 0<=c<MBCS_UTF8_LIMIT, and there is only also
michael@0 808 * the initial all-unassigned block in stage3.
michael@0 809 *
michael@0 810 * Solution for the overflow: Reduce utf8Max to the next lower value, 0xfeff.
michael@0 811 *
michael@0 812 * (See svn revision 20866 of the markus/ucnvutf8 feature branch for
michael@0 813 * code that causes MBCSAddTable() to rebuild the table not utf8Friendly
michael@0 814 * in case of overflow. That code was not tested.)
michael@0 815 */
michael@0 816 mbcsData->utf8Max=0xfeff;
michael@0 817 } else {
michael@0 818 /*
michael@0 819 * The stage 3 block has been assigned for the regular trie.
michael@0 820 * Just copy its index into stageUTF8[], without the granularity.
michael@0 821 */
michael@0 822 mbcsData->stageUTF8[c>>MBCS_UTF8_STAGE_SHIFT]=(uint16_t)stage3Index;
michael@0 823 }
michael@0 824 }
michael@0 825
michael@0 826 /* write the codepage bytes into stage 3 and get the previous bytes */
michael@0 827
michael@0 828 /* assemble the bytes into a single integer */
michael@0 829 pb=bytes;
michael@0 830 b=0;
michael@0 831 switch(length) {
michael@0 832 case 4:
michael@0 833 b=*pb++;
michael@0 834 case 3:
michael@0 835 b=(b<<8)|*pb++;
michael@0 836 case 2:
michael@0 837 b=(b<<8)|*pb++;
michael@0 838 case 1:
michael@0 839 default:
michael@0 840 b=(b<<8)|*pb++;
michael@0 841 break;
michael@0 842 }
michael@0 843
michael@0 844 old=0;
michael@0 845 p=stage3+(stage3Index+nextOffset)*maxCharLength;
michael@0 846 switch(maxCharLength) {
michael@0 847 case 2:
michael@0 848 old=*(uint16_t *)p;
michael@0 849 *(uint16_t *)p=(uint16_t)b;
michael@0 850 break;
michael@0 851 case 3:
michael@0 852 old=(uint32_t)*p<<16;
michael@0 853 *p++=(uint8_t)(b>>16);
michael@0 854 old|=(uint32_t)*p<<8;
michael@0 855 *p++=(uint8_t)(b>>8);
michael@0 856 old|=*p;
michael@0 857 *p=(uint8_t)b;
michael@0 858 break;
michael@0 859 case 4:
michael@0 860 old=*(uint32_t *)p;
michael@0 861 *(uint32_t *)p=b;
michael@0 862 break;
michael@0 863 default:
michael@0 864 /* will never occur */
michael@0 865 break;
michael@0 866 }
michael@0 867
michael@0 868 /* check that this Unicode code point was still unassigned */
michael@0 869 if((mbcsData->stage2[idx+(nextOffset>>MBCS_STAGE_2_SHIFT)]&(1UL<<(16+(c&0xf))))!=0 || old!=0) {
michael@0 870 if(flag>=0) {
michael@0 871 fprintf(stderr, "error: duplicate Unicode code point at U+%04x<->0x%s see 0x%02x\n",
michael@0 872 (int)c, printBytes(buffer, bytes, length), (int)old);
michael@0 873 return FALSE;
michael@0 874 } else if(VERBOSE) {
michael@0 875 fprintf(stderr, "duplicate Unicode code point at U+%04x<->0x%s see 0x%02x\n",
michael@0 876 (int)c, printBytes(buffer, bytes, length), (int)old);
michael@0 877 }
michael@0 878 /* continue after the above warning if the precision of the mapping is
michael@0 879 unspecified */
michael@0 880 }
michael@0 881 if(flag<=0) {
michael@0 882 /* set the roundtrip flag */
michael@0 883 mbcsData->stage2[idx+(nextOffset>>4)]|=(1UL<<(16+(c&0xf)));
michael@0 884 }
michael@0 885
michael@0 886 return TRUE;
michael@0 887 }
michael@0 888
michael@0 889 U_CFUNC UBool
michael@0 890 MBCSOkForBaseFromUnicode(const MBCSData *mbcsData,
michael@0 891 const uint8_t *bytes, int32_t length,
michael@0 892 UChar32 c, int8_t flag) {
michael@0 893 /*
michael@0 894 * A 1:1 mapping does not fit into the MBCS base table's fromUnicode table under
michael@0 895 * the following conditions:
michael@0 896 *
michael@0 897 * - a |2 SUB mapping for <subchar1> (no base table data structure for them)
michael@0 898 * - a |1 fallback to 0x00 (result value 0, indistinguishable from unmappable entry)
michael@0 899 * - a multi-byte mapping with leading 0x00 bytes (no explicit length field)
michael@0 900 *
michael@0 901 * Some of these tests are redundant with ucm_mappingType().
michael@0 902 */
michael@0 903 if( (flag==2 && length==1) ||
michael@0 904 (flag==1 && bytes[0]==0) || /* testing length==1 would be redundant with the next test */
michael@0 905 (flag<=1 && length>1 && bytes[0]==0)
michael@0 906 ) {
michael@0 907 return FALSE;
michael@0 908 }
michael@0 909
michael@0 910 /*
michael@0 911 * Additional restrictions for UTF-8-friendly fromUnicode tables,
michael@0 912 * for code points up to the maximum optimized one:
michael@0 913 *
michael@0 914 * - any mapping to 0x00 (result value 0, indistinguishable from unmappable entry)
michael@0 915 * - any |1 fallback (no roundtrip flags in the optimized table)
michael@0 916 */
michael@0 917 if(mbcsData->utf8Friendly && flag<=1 && c<=mbcsData->utf8Max && (bytes[0]==0 || flag==1)) {
michael@0 918 return FALSE;
michael@0 919 }
michael@0 920
michael@0 921 /*
michael@0 922 * If we omit the fromUnicode data, we can only store roundtrips there
michael@0 923 * because only they are recoverable from the toUnicode data.
michael@0 924 * Fallbacks must go into the extension table.
michael@0 925 */
michael@0 926 if(mbcsData->omitFromU && flag!=0) {
michael@0 927 return FALSE;
michael@0 928 }
michael@0 929
michael@0 930 /* All other mappings do fit into the base table. */
michael@0 931 return TRUE;
michael@0 932 }
michael@0 933
michael@0 934 /* we can assume that the table only contains 1:1 mappings with <=4 bytes each */
michael@0 935 static UBool
michael@0 936 MBCSAddTable(NewConverter *cnvData, UCMTable *table, UConverterStaticData *staticData) {
michael@0 937 MBCSData *mbcsData;
michael@0 938 UCMapping *m;
michael@0 939 UChar32 c;
michael@0 940 int32_t i, maxCharLength;
michael@0 941 int8_t f;
michael@0 942 UBool isOK, utf8Friendly;
michael@0 943
michael@0 944 staticData->unicodeMask=table->unicodeMask;
michael@0 945 if(staticData->unicodeMask==3) {
michael@0 946 fprintf(stderr, "error: contains mappings for both supplementary and surrogate code points\n");
michael@0 947 return FALSE;
michael@0 948 }
michael@0 949
michael@0 950 staticData->conversionType=UCNV_MBCS;
michael@0 951
michael@0 952 mbcsData=(MBCSData *)cnvData;
michael@0 953 maxCharLength=mbcsData->ucm->states.maxCharLength;
michael@0 954
michael@0 955 /*
michael@0 956 * Generation of UTF-8-friendly data requires
michael@0 957 * a sorted table, which makeconv generates when explicit precision
michael@0 958 * indicators are used.
michael@0 959 */
michael@0 960 mbcsData->utf8Friendly=utf8Friendly=(UBool)((table->flagsType&UCM_FLAGS_EXPLICIT)!=0);
michael@0 961 if(utf8Friendly) {
michael@0 962 mbcsData->utf8Max=MBCS_UTF8_MAX;
michael@0 963 if(SMALL && maxCharLength>1) {
michael@0 964 mbcsData->omitFromU=TRUE;
michael@0 965 }
michael@0 966 } else {
michael@0 967 mbcsData->utf8Max=0;
michael@0 968 if(SMALL && maxCharLength>1) {
michael@0 969 fprintf(stderr,
michael@0 970 "makeconv warning: --small not available for .ucm files without |0 etc.\n");
michael@0 971 }
michael@0 972 }
michael@0 973
michael@0 974 if(!MBCSStartMappings(mbcsData)) {
michael@0 975 return FALSE;
michael@0 976 }
michael@0 977
michael@0 978 staticData->hasFromUnicodeFallback=FALSE;
michael@0 979 staticData->hasToUnicodeFallback=FALSE;
michael@0 980
michael@0 981 isOK=TRUE;
michael@0 982
michael@0 983 m=table->mappings;
michael@0 984 for(i=0; i<table->mappingsLength; ++m, ++i) {
michael@0 985 c=m->u;
michael@0 986 f=m->f;
michael@0 987
michael@0 988 /*
michael@0 989 * Small optimization for --small .cnv files:
michael@0 990 *
michael@0 991 * If there are fromUnicode mappings above MBCS_UTF8_MAX,
michael@0 992 * then the file size will be smaller if we make utf8Max larger
michael@0 993 * because the size increase in stageUTF8 will be more than balanced by
michael@0 994 * how much less of stage2 needs to be stored.
michael@0 995 *
michael@0 996 * There is no point in doing this incrementally because stageUTF8
michael@0 997 * uses so much less space per block than stage2,
michael@0 998 * so we immediately increase utf8Max to 0xffff.
michael@0 999 *
michael@0 1000 * Do not increase utf8Max if it is already at 0xfeff because MBCSAddFromUnicode()
michael@0 1001 * sets it to that value when stageUTF8 overflows.
michael@0 1002 */
michael@0 1003 if( mbcsData->omitFromU && f<=1 &&
michael@0 1004 mbcsData->utf8Max<c && c<=0xffff &&
michael@0 1005 mbcsData->utf8Max<0xfeff
michael@0 1006 ) {
michael@0 1007 mbcsData->utf8Max=0xffff;
michael@0 1008 }
michael@0 1009
michael@0 1010 switch(f) {
michael@0 1011 case -1:
michael@0 1012 /* there was no precision/fallback indicator */
michael@0 1013 /* fall through to set the mappings */
michael@0 1014 case 0:
michael@0 1015 /* set roundtrip mappings */
michael@0 1016 isOK&=MBCSAddToUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
michael@0 1017
michael@0 1018 if(maxCharLength==1) {
michael@0 1019 isOK&=MBCSSingleAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
michael@0 1020 } else if(MBCSOkForBaseFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f)) {
michael@0 1021 isOK&=MBCSAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
michael@0 1022 } else {
michael@0 1023 m->f|=MBCS_FROM_U_EXT_FLAG;
michael@0 1024 m->moveFlag=UCM_MOVE_TO_EXT;
michael@0 1025 }
michael@0 1026 break;
michael@0 1027 case 1:
michael@0 1028 /* set only a fallback mapping from Unicode to codepage */
michael@0 1029 if(maxCharLength==1) {
michael@0 1030 staticData->hasFromUnicodeFallback=TRUE;
michael@0 1031 isOK&=MBCSSingleAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
michael@0 1032 } else if(MBCSOkForBaseFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f)) {
michael@0 1033 staticData->hasFromUnicodeFallback=TRUE;
michael@0 1034 isOK&=MBCSAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
michael@0 1035 } else {
michael@0 1036 m->f|=MBCS_FROM_U_EXT_FLAG;
michael@0 1037 m->moveFlag=UCM_MOVE_TO_EXT;
michael@0 1038 }
michael@0 1039 break;
michael@0 1040 case 2:
michael@0 1041 /* ignore |2 SUB mappings, except to move <subchar1> mappings to the extension table */
michael@0 1042 if(maxCharLength>1 && m->bLen==1) {
michael@0 1043 m->f|=MBCS_FROM_U_EXT_FLAG;
michael@0 1044 m->moveFlag=UCM_MOVE_TO_EXT;
michael@0 1045 }
michael@0 1046 break;
michael@0 1047 case 3:
michael@0 1048 /* set only a fallback mapping from codepage to Unicode */
michael@0 1049 staticData->hasToUnicodeFallback=TRUE;
michael@0 1050 isOK&=MBCSAddToUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
michael@0 1051 break;
michael@0 1052 case 4:
michael@0 1053 /* move "good one-way" mappings to the extension table */
michael@0 1054 m->f|=MBCS_FROM_U_EXT_FLAG;
michael@0 1055 m->moveFlag=UCM_MOVE_TO_EXT;
michael@0 1056 break;
michael@0 1057 default:
michael@0 1058 /* will not occur because the parser checked it already */
michael@0 1059 fprintf(stderr, "error: illegal fallback indicator %d\n", f);
michael@0 1060 return FALSE;
michael@0 1061 }
michael@0 1062 }
michael@0 1063
michael@0 1064 MBCSPostprocess(mbcsData, staticData);
michael@0 1065
michael@0 1066 return isOK;
michael@0 1067 }
michael@0 1068
michael@0 1069 static UBool
michael@0 1070 transformEUC(MBCSData *mbcsData) {
michael@0 1071 uint8_t *p8;
michael@0 1072 uint32_t i, value, oldLength, old3Top;
michael@0 1073 uint8_t b;
michael@0 1074
michael@0 1075 oldLength=mbcsData->ucm->states.maxCharLength;
michael@0 1076 if(oldLength<3) {
michael@0 1077 return FALSE;
michael@0 1078 }
michael@0 1079
michael@0 1080 old3Top=mbcsData->stage3Top;
michael@0 1081
michael@0 1082 /* careful: 2-byte and 4-byte codes are stored in platform endianness! */
michael@0 1083
michael@0 1084 /* test if all first bytes are in {0, 0x8e, 0x8f} */
michael@0 1085 p8=mbcsData->fromUBytes;
michael@0 1086
michael@0 1087 #if !U_IS_BIG_ENDIAN
michael@0 1088 if(oldLength==4) {
michael@0 1089 p8+=3;
michael@0 1090 }
michael@0 1091 #endif
michael@0 1092
michael@0 1093 for(i=0; i<old3Top; i+=oldLength) {
michael@0 1094 b=p8[i];
michael@0 1095 if(b!=0 && b!=0x8e && b!=0x8f) {
michael@0 1096 /* some first byte does not fit the EUC pattern, nothing to be done */
michael@0 1097 return FALSE;
michael@0 1098 }
michael@0 1099 }
michael@0 1100 /* restore p if it was modified above */
michael@0 1101 p8=mbcsData->fromUBytes;
michael@0 1102
michael@0 1103 /* modify outputType and adjust stage3Top */
michael@0 1104 mbcsData->ucm->states.outputType=(int8_t)(MBCS_OUTPUT_3_EUC+oldLength-3);
michael@0 1105 mbcsData->stage3Top=(old3Top*(oldLength-1))/oldLength;
michael@0 1106
michael@0 1107 /*
michael@0 1108 * EUC-encode all byte sequences;
michael@0 1109 * see "CJKV Information Processing" (1st ed. 1999) from Ken Lunde, O'Reilly,
michael@0 1110 * p. 161 in chapter 4 "Encoding Methods"
michael@0 1111 *
michael@0 1112 * This also must reverse the byte order if the platform is little-endian!
michael@0 1113 */
michael@0 1114 if(oldLength==3) {
michael@0 1115 uint16_t *q=(uint16_t *)p8;
michael@0 1116 for(i=0; i<old3Top; i+=oldLength) {
michael@0 1117 b=*p8;
michael@0 1118 if(b==0) {
michael@0 1119 /* short sequences are stored directly */
michael@0 1120 /* code set 0 or 1 */
michael@0 1121 (*q++)=(uint16_t)((p8[1]<<8)|p8[2]);
michael@0 1122 } else if(b==0x8e) {
michael@0 1123 /* code set 2 */
michael@0 1124 (*q++)=(uint16_t)(((p8[1]&0x7f)<<8)|p8[2]);
michael@0 1125 } else /* b==0x8f */ {
michael@0 1126 /* code set 3 */
michael@0 1127 (*q++)=(uint16_t)((p8[1]<<8)|(p8[2]&0x7f));
michael@0 1128 }
michael@0 1129 p8+=3;
michael@0 1130 }
michael@0 1131 } else /* oldLength==4 */ {
michael@0 1132 uint8_t *q=p8;
michael@0 1133 uint32_t *p32=(uint32_t *)p8;
michael@0 1134 for(i=0; i<old3Top; i+=4) {
michael@0 1135 value=(*p32++);
michael@0 1136 if(value<=0xffffff) {
michael@0 1137 /* short sequences are stored directly */
michael@0 1138 /* code set 0 or 1 */
michael@0 1139 (*q++)=(uint8_t)(value>>16);
michael@0 1140 (*q++)=(uint8_t)(value>>8);
michael@0 1141 (*q++)=(uint8_t)value;
michael@0 1142 } else if(value<=0x8effffff) {
michael@0 1143 /* code set 2 */
michael@0 1144 (*q++)=(uint8_t)((value>>16)&0x7f);
michael@0 1145 (*q++)=(uint8_t)(value>>8);
michael@0 1146 (*q++)=(uint8_t)value;
michael@0 1147 } else /* first byte is 0x8f */ {
michael@0 1148 /* code set 3 */
michael@0 1149 (*q++)=(uint8_t)(value>>16);
michael@0 1150 (*q++)=(uint8_t)((value>>8)&0x7f);
michael@0 1151 (*q++)=(uint8_t)value;
michael@0 1152 }
michael@0 1153 }
michael@0 1154 }
michael@0 1155
michael@0 1156 return TRUE;
michael@0 1157 }
michael@0 1158
michael@0 1159 /*
michael@0 1160 * Compact stage 2 for SBCS by overlapping adjacent stage 2 blocks as far
michael@0 1161 * as possible. Overlapping is done on unassigned head and tail
michael@0 1162 * parts of blocks in steps of MBCS_STAGE_2_MULTIPLIER.
michael@0 1163 * Stage 1 indexes need to be adjusted accordingly.
michael@0 1164 * This function is very similar to genprops/store.c/compactStage().
michael@0 1165 */
michael@0 1166 static void
michael@0 1167 singleCompactStage2(MBCSData *mbcsData) {
michael@0 1168 /* this array maps the ordinal number of a stage 2 block to its new stage 1 index */
michael@0 1169 uint16_t map[MBCS_STAGE_2_MAX_BLOCKS];
michael@0 1170 uint16_t i, start, prevEnd, newStart;
michael@0 1171
michael@0 1172 /* enter the all-unassigned first stage 2 block into the map */
michael@0 1173 map[0]=MBCS_STAGE_2_ALL_UNASSIGNED_INDEX;
michael@0 1174
michael@0 1175 /* begin with the first block after the all-unassigned one */
michael@0 1176 start=newStart=MBCS_STAGE_2_FIRST_ASSIGNED;
michael@0 1177 while(start<mbcsData->stage2Top) {
michael@0 1178 prevEnd=(uint16_t)(newStart-1);
michael@0 1179
michael@0 1180 /* find the size of the overlap */
michael@0 1181 for(i=0; i<MBCS_STAGE_2_BLOCK_SIZE && mbcsData->stage2Single[start+i]==0 && mbcsData->stage2Single[prevEnd-i]==0; ++i) {}
michael@0 1182
michael@0 1183 if(i>0) {
michael@0 1184 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=(uint16_t)(newStart-i);
michael@0 1185
michael@0 1186 /* move the non-overlapping indexes to their new positions */
michael@0 1187 start+=i;
michael@0 1188 for(i=(uint16_t)(MBCS_STAGE_2_BLOCK_SIZE-i); i>0; --i) {
michael@0 1189 mbcsData->stage2Single[newStart++]=mbcsData->stage2Single[start++];
michael@0 1190 }
michael@0 1191 } else if(newStart<start) {
michael@0 1192 /* move the indexes to their new positions */
michael@0 1193 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=newStart;
michael@0 1194 for(i=MBCS_STAGE_2_BLOCK_SIZE; i>0; --i) {
michael@0 1195 mbcsData->stage2Single[newStart++]=mbcsData->stage2Single[start++];
michael@0 1196 }
michael@0 1197 } else /* no overlap && newStart==start */ {
michael@0 1198 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=start;
michael@0 1199 start=newStart+=MBCS_STAGE_2_BLOCK_SIZE;
michael@0 1200 }
michael@0 1201 }
michael@0 1202
michael@0 1203 /* adjust stage2Top */
michael@0 1204 if(VERBOSE && newStart<mbcsData->stage2Top) {
michael@0 1205 printf("compacting stage 2 from stage2Top=0x%lx to 0x%lx, saving %ld bytes\n",
michael@0 1206 (unsigned long)mbcsData->stage2Top, (unsigned long)newStart,
michael@0 1207 (long)(mbcsData->stage2Top-newStart)*2);
michael@0 1208 }
michael@0 1209 mbcsData->stage2Top=newStart;
michael@0 1210
michael@0 1211 /* now adjust stage 1 */
michael@0 1212 for(i=0; i<MBCS_STAGE_1_SIZE; ++i) {
michael@0 1213 mbcsData->stage1[i]=map[mbcsData->stage1[i]>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT];
michael@0 1214 }
michael@0 1215 }
michael@0 1216
michael@0 1217 /* Compact stage 3 for SBCS - same algorithm as above. */
michael@0 1218 static void
michael@0 1219 singleCompactStage3(MBCSData *mbcsData) {
michael@0 1220 uint16_t *stage3=(uint16_t *)mbcsData->fromUBytes;
michael@0 1221
michael@0 1222 /* this array maps the ordinal number of a stage 3 block to its new stage 2 index */
michael@0 1223 uint16_t map[0x1000];
michael@0 1224 uint16_t i, start, prevEnd, newStart;
michael@0 1225
michael@0 1226 /* enter the all-unassigned first stage 3 block into the map */
michael@0 1227 map[0]=0;
michael@0 1228
michael@0 1229 /* begin with the first block after the all-unassigned one */
michael@0 1230 start=newStart=16;
michael@0 1231 while(start<mbcsData->stage3Top) {
michael@0 1232 prevEnd=(uint16_t)(newStart-1);
michael@0 1233
michael@0 1234 /* find the size of the overlap */
michael@0 1235 for(i=0; i<16 && stage3[start+i]==0 && stage3[prevEnd-i]==0; ++i) {}
michael@0 1236
michael@0 1237 if(i>0) {
michael@0 1238 map[start>>4]=(uint16_t)(newStart-i);
michael@0 1239
michael@0 1240 /* move the non-overlapping indexes to their new positions */
michael@0 1241 start+=i;
michael@0 1242 for(i=(uint16_t)(16-i); i>0; --i) {
michael@0 1243 stage3[newStart++]=stage3[start++];
michael@0 1244 }
michael@0 1245 } else if(newStart<start) {
michael@0 1246 /* move the indexes to their new positions */
michael@0 1247 map[start>>4]=newStart;
michael@0 1248 for(i=16; i>0; --i) {
michael@0 1249 stage3[newStart++]=stage3[start++];
michael@0 1250 }
michael@0 1251 } else /* no overlap && newStart==start */ {
michael@0 1252 map[start>>4]=start;
michael@0 1253 start=newStart+=16;
michael@0 1254 }
michael@0 1255 }
michael@0 1256
michael@0 1257 /* adjust stage3Top */
michael@0 1258 if(VERBOSE && newStart<mbcsData->stage3Top) {
michael@0 1259 printf("compacting stage 3 from stage3Top=0x%lx to 0x%lx, saving %ld bytes\n",
michael@0 1260 (unsigned long)mbcsData->stage3Top, (unsigned long)newStart,
michael@0 1261 (long)(mbcsData->stage3Top-newStart)*2);
michael@0 1262 }
michael@0 1263 mbcsData->stage3Top=newStart;
michael@0 1264
michael@0 1265 /* now adjust stage 2 */
michael@0 1266 for(i=0; i<mbcsData->stage2Top; ++i) {
michael@0 1267 mbcsData->stage2Single[i]=map[mbcsData->stage2Single[i]>>4];
michael@0 1268 }
michael@0 1269 }
michael@0 1270
michael@0 1271 /*
michael@0 1272 * Compact stage 2 by overlapping adjacent stage 2 blocks as far
michael@0 1273 * as possible. Overlapping is done on unassigned head and tail
michael@0 1274 * parts of blocks in steps of MBCS_STAGE_2_MULTIPLIER.
michael@0 1275 * Stage 1 indexes need to be adjusted accordingly.
michael@0 1276 * This function is very similar to genprops/store.c/compactStage().
michael@0 1277 */
michael@0 1278 static void
michael@0 1279 compactStage2(MBCSData *mbcsData) {
michael@0 1280 /* this array maps the ordinal number of a stage 2 block to its new stage 1 index */
michael@0 1281 uint16_t map[MBCS_STAGE_2_MAX_BLOCKS];
michael@0 1282 uint16_t i, start, prevEnd, newStart;
michael@0 1283
michael@0 1284 /* enter the all-unassigned first stage 2 block into the map */
michael@0 1285 map[0]=MBCS_STAGE_2_ALL_UNASSIGNED_INDEX;
michael@0 1286
michael@0 1287 /* begin with the first block after the all-unassigned one */
michael@0 1288 start=newStart=MBCS_STAGE_2_FIRST_ASSIGNED;
michael@0 1289 while(start<mbcsData->stage2Top) {
michael@0 1290 prevEnd=(uint16_t)(newStart-1);
michael@0 1291
michael@0 1292 /* find the size of the overlap */
michael@0 1293 for(i=0; i<MBCS_STAGE_2_BLOCK_SIZE && mbcsData->stage2[start+i]==0 && mbcsData->stage2[prevEnd-i]==0; ++i) {}
michael@0 1294
michael@0 1295 if(i>0) {
michael@0 1296 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=(uint16_t)(newStart-i);
michael@0 1297
michael@0 1298 /* move the non-overlapping indexes to their new positions */
michael@0 1299 start+=i;
michael@0 1300 for(i=(uint16_t)(MBCS_STAGE_2_BLOCK_SIZE-i); i>0; --i) {
michael@0 1301 mbcsData->stage2[newStart++]=mbcsData->stage2[start++];
michael@0 1302 }
michael@0 1303 } else if(newStart<start) {
michael@0 1304 /* move the indexes to their new positions */
michael@0 1305 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=newStart;
michael@0 1306 for(i=MBCS_STAGE_2_BLOCK_SIZE; i>0; --i) {
michael@0 1307 mbcsData->stage2[newStart++]=mbcsData->stage2[start++];
michael@0 1308 }
michael@0 1309 } else /* no overlap && newStart==start */ {
michael@0 1310 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=start;
michael@0 1311 start=newStart+=MBCS_STAGE_2_BLOCK_SIZE;
michael@0 1312 }
michael@0 1313 }
michael@0 1314
michael@0 1315 /* adjust stage2Top */
michael@0 1316 if(VERBOSE && newStart<mbcsData->stage2Top) {
michael@0 1317 printf("compacting stage 2 from stage2Top=0x%lx to 0x%lx, saving %ld bytes\n",
michael@0 1318 (unsigned long)mbcsData->stage2Top, (unsigned long)newStart,
michael@0 1319 (long)(mbcsData->stage2Top-newStart)*4);
michael@0 1320 }
michael@0 1321 mbcsData->stage2Top=newStart;
michael@0 1322
michael@0 1323 /* now adjust stage 1 */
michael@0 1324 for(i=0; i<MBCS_STAGE_1_SIZE; ++i) {
michael@0 1325 mbcsData->stage1[i]=map[mbcsData->stage1[i]>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT];
michael@0 1326 }
michael@0 1327 }
michael@0 1328
michael@0 1329 static void
michael@0 1330 MBCSPostprocess(MBCSData *mbcsData, const UConverterStaticData * /*staticData*/) {
michael@0 1331 UCMStates *states;
michael@0 1332 int32_t maxCharLength, stage3Width;
michael@0 1333
michael@0 1334 states=&mbcsData->ucm->states;
michael@0 1335 stage3Width=maxCharLength=states->maxCharLength;
michael@0 1336
michael@0 1337 ucm_optimizeStates(states,
michael@0 1338 &mbcsData->unicodeCodeUnits,
michael@0 1339 mbcsData->toUFallbacks, mbcsData->countToUFallbacks,
michael@0 1340 VERBOSE);
michael@0 1341
michael@0 1342 /* try to compact the fromUnicode tables */
michael@0 1343 if(transformEUC(mbcsData)) {
michael@0 1344 --stage3Width;
michael@0 1345 }
michael@0 1346
michael@0 1347 /*
michael@0 1348 * UTF-8-friendly tries are built precompacted, to cope with variable
michael@0 1349 * stage 3 allocation block sizes.
michael@0 1350 *
michael@0 1351 * Tables without precision indicators cannot be built that way,
michael@0 1352 * because if a block was overlapped with a previous one, then a smaller
michael@0 1353 * code point for the same block would not fit.
michael@0 1354 * Therefore, such tables are not marked UTF-8-friendly and must be
michael@0 1355 * compacted after all mappings are entered.
michael@0 1356 */
michael@0 1357 if(!mbcsData->utf8Friendly) {
michael@0 1358 if(maxCharLength==1) {
michael@0 1359 singleCompactStage3(mbcsData);
michael@0 1360 singleCompactStage2(mbcsData);
michael@0 1361 } else {
michael@0 1362 compactStage2(mbcsData);
michael@0 1363 }
michael@0 1364 }
michael@0 1365
michael@0 1366 if(VERBOSE) {
michael@0 1367 /*uint32_t c, i1, i2, i2Limit, i3;*/
michael@0 1368
michael@0 1369 printf("fromUnicode number of uint%s_t in stage 2: 0x%lx=%lu\n",
michael@0 1370 maxCharLength==1 ? "16" : "32",
michael@0 1371 (unsigned long)mbcsData->stage2Top,
michael@0 1372 (unsigned long)mbcsData->stage2Top);
michael@0 1373 printf("fromUnicode number of %d-byte stage 3 mapping entries: 0x%lx=%lu\n",
michael@0 1374 (int)stage3Width,
michael@0 1375 (unsigned long)mbcsData->stage3Top/stage3Width,
michael@0 1376 (unsigned long)mbcsData->stage3Top/stage3Width);
michael@0 1377 #if 0
michael@0 1378 c=0;
michael@0 1379 for(i1=0; i1<MBCS_STAGE_1_SIZE; ++i1) {
michael@0 1380 i2=mbcsData->stage1[i1];
michael@0 1381 if(i2==0) {
michael@0 1382 c+=MBCS_STAGE_2_BLOCK_SIZE*MBCS_STAGE_3_BLOCK_SIZE;
michael@0 1383 continue;
michael@0 1384 }
michael@0 1385 for(i2Limit=i2+MBCS_STAGE_2_BLOCK_SIZE; i2<i2Limit; ++i2) {
michael@0 1386 if(maxCharLength==1) {
michael@0 1387 i3=mbcsData->stage2Single[i2];
michael@0 1388 } else {
michael@0 1389 i3=(uint16_t)mbcsData->stage2[i2];
michael@0 1390 }
michael@0 1391 if(i3==0) {
michael@0 1392 c+=MBCS_STAGE_3_BLOCK_SIZE;
michael@0 1393 continue;
michael@0 1394 }
michael@0 1395 printf("U+%04lx i1=0x%02lx i2=0x%04lx i3=0x%04lx\n",
michael@0 1396 (unsigned long)c,
michael@0 1397 (unsigned long)i1,
michael@0 1398 (unsigned long)i2,
michael@0 1399 (unsigned long)i3);
michael@0 1400 c+=MBCS_STAGE_3_BLOCK_SIZE;
michael@0 1401 }
michael@0 1402 }
michael@0 1403 #endif
michael@0 1404 }
michael@0 1405 }
michael@0 1406
michael@0 1407 static uint32_t
michael@0 1408 MBCSWrite(NewConverter *cnvData, const UConverterStaticData *staticData,
michael@0 1409 UNewDataMemory *pData, int32_t tableType) {
michael@0 1410 MBCSData *mbcsData=(MBCSData *)cnvData;
michael@0 1411 uint32_t stage2Start, stage2Length;
michael@0 1412 uint32_t top, stageUTF8Length=0;
michael@0 1413 int32_t i, stage1Top;
michael@0 1414 uint32_t headerLength;
michael@0 1415
michael@0 1416 _MBCSHeader header=UCNV_MBCS_HEADER_INITIALIZER;
michael@0 1417
michael@0 1418 stage2Length=mbcsData->stage2Top;
michael@0 1419 if(mbcsData->omitFromU) {
michael@0 1420 /* find how much of stage2 can be omitted */
michael@0 1421 int32_t utf8Limit=(int32_t)mbcsData->utf8Max+1;
michael@0 1422 uint32_t st2=0; /*initialized it to avoid compiler warnings */
michael@0 1423
michael@0 1424 i=utf8Limit>>MBCS_STAGE_1_SHIFT;
michael@0 1425 if((utf8Limit&((1<<MBCS_STAGE_1_SHIFT)-1))!=0 && (st2=mbcsData->stage1[i])!=0) {
michael@0 1426 /* utf8Limit is in the middle of an existing stage 2 block */
michael@0 1427 stage2Start=st2+((utf8Limit>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK);
michael@0 1428 } else {
michael@0 1429 /* find the last stage2 block with mappings before utf8Limit */
michael@0 1430 while(i>0 && (st2=mbcsData->stage1[--i])==0) {}
michael@0 1431 /* stage2 up to the end of this block corresponds to stageUTF8 */
michael@0 1432 stage2Start=st2+MBCS_STAGE_2_BLOCK_SIZE;
michael@0 1433 }
michael@0 1434 header.options|=MBCS_OPT_NO_FROM_U;
michael@0 1435 header.fullStage2Length=stage2Length;
michael@0 1436 stage2Length-=stage2Start;
michael@0 1437 if(VERBOSE) {
michael@0 1438 printf("+ omitting %lu out of %lu stage2 entries and %lu fromUBytes\n",
michael@0 1439 (unsigned long)stage2Start,
michael@0 1440 (unsigned long)mbcsData->stage2Top,
michael@0 1441 (unsigned long)mbcsData->stage3Top);
michael@0 1442 printf("+ total size savings: %lu bytes\n", (unsigned long)stage2Start*4+mbcsData->stage3Top);
michael@0 1443 }
michael@0 1444 } else {
michael@0 1445 stage2Start=0;
michael@0 1446 }
michael@0 1447
michael@0 1448 if(staticData->unicodeMask&UCNV_HAS_SUPPLEMENTARY) {
michael@0 1449 stage1Top=MBCS_STAGE_1_SIZE; /* 0x440==1088 */
michael@0 1450 } else {
michael@0 1451 stage1Top=0x40; /* 0x40==64 */
michael@0 1452 }
michael@0 1453
michael@0 1454 /* adjust stage 1 entries to include the size of stage 1 in the offsets to stage 2 */
michael@0 1455 if(mbcsData->ucm->states.maxCharLength==1) {
michael@0 1456 for(i=0; i<stage1Top; ++i) {
michael@0 1457 mbcsData->stage1[i]+=(uint16_t)stage1Top;
michael@0 1458 }
michael@0 1459
michael@0 1460 /* stage2Top/Length have counted 16-bit results, now we need to count bytes */
michael@0 1461 /* also round up to a multiple of 4 bytes */
michael@0 1462 stage2Length=(stage2Length*2+1)&~1;
michael@0 1463
michael@0 1464 /* stage3Top has counted 16-bit results, now we need to count bytes */
michael@0 1465 mbcsData->stage3Top*=2;
michael@0 1466
michael@0 1467 if(mbcsData->utf8Friendly) {
michael@0 1468 header.version[2]=(uint8_t)(SBCS_UTF8_MAX>>8); /* store 0x1f for max==0x1fff */
michael@0 1469 }
michael@0 1470 } else {
michael@0 1471 for(i=0; i<stage1Top; ++i) {
michael@0 1472 mbcsData->stage1[i]+=(uint16_t)stage1Top/2; /* stage 2 contains 32-bit entries, stage 1 16-bit entries */
michael@0 1473 }
michael@0 1474
michael@0 1475 /* stage2Top/Length have counted 32-bit results, now we need to count bytes */
michael@0 1476 stage2Length*=4;
michael@0 1477 /* leave stage2Start counting 32-bit units */
michael@0 1478
michael@0 1479 if(mbcsData->utf8Friendly) {
michael@0 1480 stageUTF8Length=(mbcsData->utf8Max+1)>>MBCS_UTF8_STAGE_SHIFT;
michael@0 1481 header.version[2]=(uint8_t)(mbcsData->utf8Max>>8); /* store 0xd7 for max==0xd7ff */
michael@0 1482 }
michael@0 1483
michael@0 1484 /* stage3Top has already counted bytes */
michael@0 1485 }
michael@0 1486
michael@0 1487 /* round up stage3Top so that the sizes of all data blocks are multiples of 4 */
michael@0 1488 mbcsData->stage3Top=(mbcsData->stage3Top+3)&~3;
michael@0 1489
michael@0 1490 /* fill the header */
michael@0 1491 if(header.options&MBCS_OPT_INCOMPATIBLE_MASK) {
michael@0 1492 header.version[0]=5;
michael@0 1493 if(header.options&MBCS_OPT_NO_FROM_U) {
michael@0 1494 headerLength=10; /* include fullStage2Length */
michael@0 1495 } else {
michael@0 1496 headerLength=MBCS_HEADER_V5_MIN_LENGTH; /* 9 */
michael@0 1497 }
michael@0 1498 } else {
michael@0 1499 header.version[0]=4;
michael@0 1500 headerLength=MBCS_HEADER_V4_LENGTH; /* 8 */
michael@0 1501 }
michael@0 1502 header.version[1]=4;
michael@0 1503 /* header.version[2] set above for utf8Friendly data */
michael@0 1504
michael@0 1505 header.options|=(uint32_t)headerLength;
michael@0 1506
michael@0 1507 header.countStates=mbcsData->ucm->states.countStates;
michael@0 1508 header.countToUFallbacks=mbcsData->countToUFallbacks;
michael@0 1509
michael@0 1510 header.offsetToUCodeUnits=
michael@0 1511 headerLength*4+
michael@0 1512 mbcsData->ucm->states.countStates*1024+
michael@0 1513 mbcsData->countToUFallbacks*sizeof(_MBCSToUFallback);
michael@0 1514 header.offsetFromUTable=
michael@0 1515 header.offsetToUCodeUnits+
michael@0 1516 mbcsData->ucm->states.countToUCodeUnits*2;
michael@0 1517 header.offsetFromUBytes=
michael@0 1518 header.offsetFromUTable+
michael@0 1519 stage1Top*2+
michael@0 1520 stage2Length;
michael@0 1521 header.fromUBytesLength=mbcsData->stage3Top;
michael@0 1522
michael@0 1523 top=header.offsetFromUBytes+stageUTF8Length*2;
michael@0 1524 if(!(header.options&MBCS_OPT_NO_FROM_U)) {
michael@0 1525 top+=header.fromUBytesLength;
michael@0 1526 }
michael@0 1527
michael@0 1528 header.flags=(uint8_t)(mbcsData->ucm->states.outputType);
michael@0 1529
michael@0 1530 if(tableType&TABLE_EXT) {
michael@0 1531 if(top>0xffffff) {
michael@0 1532 fprintf(stderr, "error: offset 0x%lx to extension table exceeds 0xffffff\n", (long)top);
michael@0 1533 return 0;
michael@0 1534 }
michael@0 1535
michael@0 1536 header.flags|=top<<8;
michael@0 1537 }
michael@0 1538
michael@0 1539 /* write the MBCS data */
michael@0 1540 udata_writeBlock(pData, &header, headerLength*4);
michael@0 1541 udata_writeBlock(pData, mbcsData->ucm->states.stateTable, header.countStates*1024);
michael@0 1542 udata_writeBlock(pData, mbcsData->toUFallbacks, mbcsData->countToUFallbacks*sizeof(_MBCSToUFallback));
michael@0 1543 udata_writeBlock(pData, mbcsData->unicodeCodeUnits, mbcsData->ucm->states.countToUCodeUnits*2);
michael@0 1544 udata_writeBlock(pData, mbcsData->stage1, stage1Top*2);
michael@0 1545 if(mbcsData->ucm->states.maxCharLength==1) {
michael@0 1546 udata_writeBlock(pData, mbcsData->stage2Single+stage2Start, stage2Length);
michael@0 1547 } else {
michael@0 1548 udata_writeBlock(pData, mbcsData->stage2+stage2Start, stage2Length);
michael@0 1549 }
michael@0 1550 if(!(header.options&MBCS_OPT_NO_FROM_U)) {
michael@0 1551 udata_writeBlock(pData, mbcsData->fromUBytes, mbcsData->stage3Top);
michael@0 1552 }
michael@0 1553
michael@0 1554 if(stageUTF8Length>0) {
michael@0 1555 udata_writeBlock(pData, mbcsData->stageUTF8, stageUTF8Length*2);
michael@0 1556 }
michael@0 1557
michael@0 1558 /* return the number of bytes that should have been written */
michael@0 1559 return top;
michael@0 1560 }

mercurial