1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2000-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: genmbcs.cpp
12 * tab size: 8 (not used)
15 * created on: 2000jul06
16 * created by: Markus W. Scherer
20 #include "unicode/utypes.h"
31 * TODO: Split this file into toUnicode, SBCSFromUnicode and MBCSFromUnicode files.
32 * Reduce tests for maxCharLength.
36 NewConverter newConverter;
40 /* toUnicode (state table in ucm->states) */
41 _MBCSToUFallback toUFallbacks[MBCS_MAX_FALLBACK_COUNT];
42 int32_t countToUFallbacks;
43 uint16_t *unicodeCodeUnits;
46 uint16_t stage1[MBCS_STAGE_1_SIZE];
47 uint16_t stage2Single[MBCS_STAGE_2_SIZE]; /* stage 2 for single-byte codepages */
48 uint32_t stage2[MBCS_STAGE_2_SIZE]; /* stage 2 for MBCS */
50 uint32_t stage2Top, stage3Top;
53 uint16_t stageUTF8[0x10000>>MBCS_UTF8_STAGE_SHIFT]; /* allow for utf8Max=0xffff */
56 * Maximum UTF-8-friendly code point.
57 * 0 if !utf8Friendly, otherwise 0x01ff..0xffff in steps of 0x100.
58 * If utf8Friendly, utf8Max is normally either MBCS_UTF8_MAX or 0xffff.
68 MBCSClose(NewConverter *cnvData);
71 MBCSStartMappings(MBCSData *mbcsData);
74 MBCSAddToUnicode(MBCSData *mbcsData,
75 const uint8_t *bytes, int32_t length,
80 MBCSIsValid(NewConverter *cnvData,
81 const uint8_t *bytes, int32_t length);
84 MBCSSingleAddFromUnicode(MBCSData *mbcsData,
85 const uint8_t *bytes, int32_t length,
90 MBCSAddFromUnicode(MBCSData *mbcsData,
91 const uint8_t *bytes, int32_t length,
96 MBCSPostprocess(MBCSData *mbcsData, const UConverterStaticData *staticData);
99 MBCSAddTable(NewConverter *cnvData, UCMTable *table, UConverterStaticData *staticData);
102 MBCSWrite(NewConverter *cnvData, const UConverterStaticData *staticData,
103 UNewDataMemory *pData, int32_t tableType);
105 /* helper ------------------------------------------------------------------- */
108 hexDigit(uint8_t digit) {
109 return digit<=9 ? (char)('0'+digit) : (char)('a'-10+digit);
113 printBytes(char *buffer, const uint8_t *bytes, int32_t length) {
116 *s++=hexDigit((uint8_t)(*bytes>>4));
117 *s++=hexDigit((uint8_t)(*bytes&0xf));
126 /* implementation ----------------------------------------------------------- */
128 static MBCSData gDummy;
130 U_CFUNC const MBCSData *
132 uprv_memset(&gDummy, 0, sizeof(MBCSData));
135 * Set "pessimistic" values which may sometimes move too many
136 * mappings to the extension table (but never too few).
137 * These values cause MBCSOkForBaseFromUnicode() to return FALSE for the
138 * largest set of mappings.
139 * Assume maxCharLength>1.
141 gDummy.utf8Friendly=TRUE;
143 gDummy.utf8Max=0xffff;
144 gDummy.omitFromU=TRUE;
146 gDummy.utf8Max=MBCS_UTF8_MAX;
152 MBCSInit(MBCSData *mbcsData, UCMFile *ucm) {
153 uprv_memset(mbcsData, 0, sizeof(MBCSData));
155 mbcsData->ucm=ucm; /* aliased, not owned */
157 mbcsData->newConverter.close=MBCSClose;
158 mbcsData->newConverter.isValid=MBCSIsValid;
159 mbcsData->newConverter.addTable=MBCSAddTable;
160 mbcsData->newConverter.write=MBCSWrite;
164 MBCSOpen(UCMFile *ucm) {
165 MBCSData *mbcsData=(MBCSData *)uprv_malloc(sizeof(MBCSData));
167 printf("out of memory\n");
168 exit(U_MEMORY_ALLOCATION_ERROR);
171 MBCSInit(mbcsData, ucm);
172 return &mbcsData->newConverter;
176 MBCSDestruct(MBCSData *mbcsData) {
177 uprv_free(mbcsData->unicodeCodeUnits);
178 uprv_free(mbcsData->fromUBytes);
182 MBCSClose(NewConverter *cnvData) {
183 MBCSData *mbcsData=(MBCSData *)cnvData;
185 MBCSDestruct(mbcsData);
191 MBCSStartMappings(MBCSData *mbcsData) {
192 int32_t i, sum, maxCharLength,
193 stage2NullLength, stage2AllocLength,
194 stage3NullLength, stage3AllocLength;
198 /* allocate the code unit array and prefill it with "unassigned" values */
199 sum=mbcsData->ucm->states.countToUCodeUnits;
201 printf("the total number of offsets is 0x%lx=%ld\n", (long)sum, (long)sum);
205 mbcsData->unicodeCodeUnits=(uint16_t *)uprv_malloc(sum*sizeof(uint16_t));
206 if(mbcsData->unicodeCodeUnits==NULL) {
207 fprintf(stderr, "error: out of memory allocating %ld 16-bit code units\n",
211 for(i=0; i<sum; ++i) {
212 mbcsData->unicodeCodeUnits[i]=0xfffe;
217 maxCharLength=mbcsData->ucm->states.maxCharLength;
219 /* allocate the codepage mappings and preset the first 16 characters to 0 */
220 if(maxCharLength==1) {
221 /* allocate 64k 16-bit results for single-byte codepages */
224 /* allocate 1M * maxCharLength bytes for at most 1M mappings */
225 sum=0x100000*maxCharLength;
227 mbcsData->fromUBytes=(uint8_t *)uprv_malloc(sum);
228 if(mbcsData->fromUBytes==NULL) {
229 fprintf(stderr, "error: out of memory allocating %ld B for target mappings\n", (long)sum);
232 uprv_memset(mbcsData->fromUBytes, 0, sum);
235 * UTF-8-friendly fromUnicode tries: allocate multiple blocks at a time.
236 * See ucnvmbcs.h for details.
238 * There is code, for example in ucnv_MBCSGetUnicodeSetForUnicode(), which
239 * assumes that the initial stage 2/3 blocks are the all-unassigned ones.
240 * Therefore, we refine the data structure while maintaining this placement
241 * even though it would be convenient to allocate the ASCII block at the
242 * beginning of stage 3, for example.
244 * UTF-8-friendly fromUnicode tries work from sorted tables and are built
245 * pre-compacted, overlapping adjacent stage 2/3 blocks.
246 * This is necessary because the block allocation and compaction changes
247 * at SBCS_UTF8_MAX or MBCS_UTF8_MAX, and for MBCS tables the additional
248 * stage table uses direct indexes into stage 3, without a multiplier and
249 * thus with a smaller reach.
251 * Non-UTF-8-friendly fromUnicode tries work from unsorted tables
252 * (because implicit precision is used), and are compacted
253 * in post-processing.
255 * Preallocation for UTF-8-friendly fromUnicode tries:
258 * 64-entry all-unassigned first block followed by ASCII (128 entries).
261 * 64-entry all-unassigned first block followed by preallocated
262 * 64-block for ASCII.
265 /* Preallocate ASCII as a linear 128-entry stage 3 block. */
266 stage2NullLength=MBCS_STAGE_2_BLOCK_SIZE;
267 stage2AllocLength=MBCS_STAGE_2_BLOCK_SIZE;
269 stage3NullLength=MBCS_UTF8_STAGE_3_BLOCK_SIZE;
270 stage3AllocLength=128; /* ASCII U+0000..U+007f */
272 /* Initialize stage 1 for the preallocated blocks. */
273 sum=stage2NullLength;
274 for(i=0; i<(stage2AllocLength>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT); ++i) {
275 mbcsData->stage1[i]=sum;
276 sum+=MBCS_STAGE_2_BLOCK_SIZE;
278 mbcsData->stage2Top=stage2NullLength+stage2AllocLength; /* ==sum */
281 * Stage 2 indexes count 16-blocks in stage 3 as follows:
282 * SBCS: directly, indexes increment by 16
283 * MBCS: indexes need to be multiplied by 16*maxCharLength, indexes increment by 1
284 * MBCS UTF-8: directly, indexes increment by 16
286 if(maxCharLength==1) {
287 sum=stage3NullLength;
288 for(i=0; i<(stage3AllocLength/MBCS_STAGE_3_BLOCK_SIZE); ++i) {
289 mbcsData->stage2Single[mbcsData->stage1[0]+i]=sum;
290 sum+=MBCS_STAGE_3_BLOCK_SIZE;
293 sum=stage3NullLength/MBCS_STAGE_3_GRANULARITY;
294 for(i=0; i<(stage3AllocLength/MBCS_STAGE_3_BLOCK_SIZE); ++i) {
295 mbcsData->stage2[mbcsData->stage1[0]+i]=sum;
296 sum+=MBCS_STAGE_3_BLOCK_SIZE/MBCS_STAGE_3_GRANULARITY;
300 sum=stage3NullLength;
301 for(i=0; i<(stage3AllocLength/MBCS_UTF8_STAGE_3_BLOCK_SIZE); ++i) {
302 mbcsData->stageUTF8[i]=sum;
303 sum+=MBCS_UTF8_STAGE_3_BLOCK_SIZE;
307 * Allocate a 64-entry all-unassigned first stage 3 block,
308 * for UTF-8-friendly lookup with a trail byte,
309 * plus 128 entries for ASCII.
311 mbcsData->stage3Top=(stage3NullLength+stage3AllocLength)*maxCharLength; /* ==sum*maxCharLength */
316 /* return TRUE for success */
318 setFallback(MBCSData *mbcsData, uint32_t offset, UChar32 c) {
319 int32_t i=ucm_findFallback(mbcsData->toUFallbacks, mbcsData->countToUFallbacks, offset);
321 /* if there is already a fallback for this offset, then overwrite it */
322 mbcsData->toUFallbacks[i].codePoint=c;
325 /* if there is no fallback for this offset, then add one */
326 i=mbcsData->countToUFallbacks;
327 if(i>=MBCS_MAX_FALLBACK_COUNT) {
328 fprintf(stderr, "error: too many toUnicode fallbacks, currently at: U+%x\n", (int)c);
331 mbcsData->toUFallbacks[i].offset=offset;
332 mbcsData->toUFallbacks[i].codePoint=c;
333 mbcsData->countToUFallbacks=i+1;
339 /* remove fallback if there is one with this offset; return the code point if there was such a fallback, otherwise -1 */
341 removeFallback(MBCSData *mbcsData, uint32_t offset) {
342 int32_t i=ucm_findFallback(mbcsData->toUFallbacks, mbcsData->countToUFallbacks, offset);
344 _MBCSToUFallback *toUFallbacks;
347 toUFallbacks=mbcsData->toUFallbacks;
348 limit=mbcsData->countToUFallbacks;
349 old=(int32_t)toUFallbacks[i].codePoint;
351 /* copy the last fallback entry here to keep the list contiguous */
352 toUFallbacks[i].offset=toUFallbacks[limit-1].offset;
353 toUFallbacks[i].codePoint=toUFallbacks[limit-1].codePoint;
354 mbcsData->countToUFallbacks=limit-1;
362 * isFallback is almost a boolean:
363 * 1 (TRUE) this is a fallback mapping
364 * 0 (FALSE) this is a precise mapping
365 * -1 the precision of this mapping is not specified
368 MBCSAddToUnicode(MBCSData *mbcsData,
369 const uint8_t *bytes, int32_t length,
374 int32_t i=0, entry, old;
377 if(mbcsData->ucm->states.countStates==0) {
378 fprintf(stderr, "error: there is no state information!\n");
382 /* for SI/SO (like EBCDIC-stateful), double-byte sequences start in state 1 */
383 if(length==2 && mbcsData->ucm->states.outputType==MBCS_OUTPUT_2_SISO) {
388 * Walk down the state table like in conversion,
389 * much like getNextUChar().
390 * We assume that c<=0x10ffff.
393 entry=mbcsData->ucm->states.stateTable[state][bytes[i++]];
394 if(MBCS_ENTRY_IS_TRANSITION(entry)) {
396 fprintf(stderr, "error: byte sequence too short, ends in non-final state %hu: 0x%s (U+%x)\n",
397 (short)state, printBytes(buffer, bytes, length), (int)c);
400 state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
401 offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
404 fprintf(stderr, "error: byte sequence too long by %d bytes, final state %u: 0x%s (U+%x)\n",
405 (int)(length-i), state, printBytes(buffer, bytes, length), (int)c);
408 switch(MBCS_ENTRY_FINAL_ACTION(entry)) {
409 case MBCS_STATE_ILLEGAL:
410 fprintf(stderr, "error: byte sequence ends in illegal state at U+%04x<->0x%s\n",
411 (int)c, printBytes(buffer, bytes, length));
413 case MBCS_STATE_CHANGE_ONLY:
414 fprintf(stderr, "error: byte sequence ends in state-change-only at U+%04x<->0x%s\n",
415 (int)c, printBytes(buffer, bytes, length));
417 case MBCS_STATE_UNASSIGNED:
418 fprintf(stderr, "error: byte sequence ends in unassigned state at U+%04x<->0x%s\n",
419 (int)c, printBytes(buffer, bytes, length));
421 case MBCS_STATE_FALLBACK_DIRECT_16:
422 case MBCS_STATE_VALID_DIRECT_16:
423 case MBCS_STATE_FALLBACK_DIRECT_20:
424 case MBCS_STATE_VALID_DIRECT_20:
425 if(MBCS_ENTRY_SET_STATE(entry, 0)!=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, 0xfffe)) {
426 /* the "direct" action's value is not "valid-direct-16-unassigned" any more */
427 if(MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_DIRECT_16 || MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_FALLBACK_DIRECT_16) {
428 old=MBCS_ENTRY_FINAL_VALUE(entry);
430 old=0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
433 fprintf(stderr, "error: duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
434 (int)c, printBytes(buffer, bytes, length), (int)old);
437 fprintf(stderr, "duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
438 (int)c, printBytes(buffer, bytes, length), (int)old);
441 * Continue after the above warning
442 * if the precision of the mapping is unspecified.
445 /* reassign the correct action code */
446 entry=MBCS_ENTRY_FINAL_SET_ACTION(entry, (MBCS_STATE_VALID_DIRECT_16+(flag==3 ? 2 : 0)+(c>=0x10000 ? 1 : 0)));
448 /* put the code point into bits 22..7 for BMP, c-0x10000 into 26..7 for others */
450 entry=MBCS_ENTRY_FINAL_SET_VALUE(entry, c);
452 entry=MBCS_ENTRY_FINAL_SET_VALUE(entry, c-0x10000);
454 mbcsData->ucm->states.stateTable[state][bytes[i-1]]=entry;
456 case MBCS_STATE_VALID_16:
457 /* bits 26..16 are not used, 0 */
458 /* bits 15..7 contain the final offset delta to one 16-bit code unit */
459 offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
460 /* check that this byte sequence is still unassigned */
461 if((old=mbcsData->unicodeCodeUnits[offset])!=0xfffe || (old=removeFallback(mbcsData, offset))!=-1) {
463 fprintf(stderr, "error: duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
464 (int)c, printBytes(buffer, bytes, length), (int)old);
467 fprintf(stderr, "duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
468 (int)c, printBytes(buffer, bytes, length), (int)old);
472 fprintf(stderr, "error: code point does not fit into valid-16-bit state at U+%04x<->0x%s\n",
473 (int)c, printBytes(buffer, bytes, length));
477 /* assign only if there is no precise mapping */
478 if(mbcsData->unicodeCodeUnits[offset]==0xfffe) {
479 return setFallback(mbcsData, offset, c);
482 mbcsData->unicodeCodeUnits[offset]=(uint16_t)c;
485 case MBCS_STATE_VALID_16_PAIR:
486 /* bits 26..16 are not used, 0 */
487 /* bits 15..7 contain the final offset delta to two 16-bit code units */
488 offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
489 /* check that this byte sequence is still unassigned */
490 old=mbcsData->unicodeCodeUnits[offset];
495 } else if(old<=0xdfff) {
496 real=0x10000+((old&0x3ff)<<10)+((mbcsData->unicodeCodeUnits[offset+1])&0x3ff);
497 } else /* old<=0xe001 */ {
498 real=mbcsData->unicodeCodeUnits[offset+1];
501 fprintf(stderr, "error: duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
502 (int)c, printBytes(buffer, bytes, length), (int)real);
505 fprintf(stderr, "duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n",
506 (int)c, printBytes(buffer, bytes, length), (int)real);
510 /* assign only if there is no precise mapping */
511 if(old<=0xdbff || old==0xe000) {
513 } else if(c<=0xffff) {
514 /* set a BMP fallback code point as a pair with 0xe001 */
515 mbcsData->unicodeCodeUnits[offset++]=0xe001;
516 mbcsData->unicodeCodeUnits[offset]=(uint16_t)c;
518 /* set a fallback surrogate pair with two second surrogates */
519 mbcsData->unicodeCodeUnits[offset++]=(uint16_t)(0xdbc0+(c>>10));
520 mbcsData->unicodeCodeUnits[offset]=(uint16_t)(0xdc00+(c&0x3ff));
524 /* set a BMP code point */
525 mbcsData->unicodeCodeUnits[offset]=(uint16_t)c;
526 } else if(c<=0xffff) {
527 /* set a BMP code point above 0xd800 as a pair with 0xe000 */
528 mbcsData->unicodeCodeUnits[offset++]=0xe000;
529 mbcsData->unicodeCodeUnits[offset]=(uint16_t)c;
531 /* set a surrogate pair */
532 mbcsData->unicodeCodeUnits[offset++]=(uint16_t)(0xd7c0+(c>>10));
533 mbcsData->unicodeCodeUnits[offset]=(uint16_t)(0xdc00+(c&0x3ff));
538 /* reserved, must never occur */
539 fprintf(stderr, "internal error: byte sequence reached reserved action code, entry 0x%02x: 0x%s (U+%x)\n",
540 (int)entry, printBytes(buffer, bytes, length), (int)c);
549 /* is this byte sequence valid? (this is almost the same as MBCSAddToUnicode()) */
551 MBCSIsValid(NewConverter *cnvData,
552 const uint8_t *bytes, int32_t length) {
553 MBCSData *mbcsData=(MBCSData *)cnvData;
555 return (UBool)(1==ucm_countChars(&mbcsData->ucm->states, bytes, length));
559 MBCSSingleAddFromUnicode(MBCSData *mbcsData,
560 const uint8_t *bytes, int32_t /*length*/,
563 uint16_t *stage3, *p;
568 uint32_t blockSize, newTop, i, nextOffset, newBlock, min;
570 /* ignore |2 SUB mappings */
576 * Walk down the triple-stage compact array ("trie") and
577 * allocate parts as necessary.
578 * Note that the first stage 2 and 3 blocks are reserved for all-unassigned mappings.
579 * We assume that length<=maxCharLength and that c<=0x10ffff.
581 stage3=(uint16_t *)mbcsData->fromUBytes;
584 /* inspect stage 1 */
585 idx=c>>MBCS_STAGE_1_SHIFT;
586 if(mbcsData->utf8Friendly && c<=SBCS_UTF8_MAX) {
587 nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK&~(MBCS_UTF8_STAGE_3_BLOCKS-1);
589 nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK;
591 if(mbcsData->stage1[idx]==MBCS_STAGE_2_ALL_UNASSIGNED_INDEX) {
592 /* allocate another block in stage 2 */
593 newBlock=mbcsData->stage2Top;
594 if(mbcsData->utf8Friendly) {
595 min=newBlock-nextOffset; /* minimum block start with overlap */
596 while(min<newBlock && mbcsData->stage2Single[newBlock-1]==0) {
600 newTop=newBlock+MBCS_STAGE_2_BLOCK_SIZE;
602 if(newTop>MBCS_MAX_STAGE_2_TOP) {
603 fprintf(stderr, "error: too many stage 2 entries at U+%04x<->0x%02x\n", (int)c, b);
608 * each stage 2 block contains 64 16-bit words:
609 * 6 code point bits 9..4 with 1 stage 3 index
611 mbcsData->stage1[idx]=(uint16_t)newBlock;
612 mbcsData->stage2Top=newTop;
615 /* inspect stage 2 */
616 idx=mbcsData->stage1[idx]+nextOffset;
617 if(mbcsData->utf8Friendly && c<=SBCS_UTF8_MAX) {
618 /* allocate 64-entry blocks for UTF-8-friendly lookup */
619 blockSize=MBCS_UTF8_STAGE_3_BLOCK_SIZE;
620 nextOffset=c&MBCS_UTF8_STAGE_3_BLOCK_MASK;
622 blockSize=MBCS_STAGE_3_BLOCK_SIZE;
623 nextOffset=c&MBCS_STAGE_3_BLOCK_MASK;
625 if(mbcsData->stage2Single[idx]==0) {
626 /* allocate another block in stage 3 */
627 newBlock=mbcsData->stage3Top;
628 if(mbcsData->utf8Friendly) {
629 min=newBlock-nextOffset; /* minimum block start with overlap */
630 while(min<newBlock && stage3[newBlock-1]==0) {
634 newTop=newBlock+blockSize;
636 if(newTop>MBCS_STAGE_3_SBCS_SIZE) {
637 fprintf(stderr, "error: too many code points at U+%04x<->0x%02x\n", (int)c, b);
640 /* each block has 16 uint16_t entries */
642 while(newBlock<newTop) {
643 mbcsData->stage2Single[i++]=(uint16_t)newBlock;
644 newBlock+=MBCS_STAGE_3_BLOCK_SIZE;
646 mbcsData->stage3Top=newTop; /* ==newBlock */
649 /* write the codepage entry into stage 3 and get the previous entry */
650 p=stage3+mbcsData->stage2Single[idx]+nextOffset;
653 *p=(uint16_t)(0xf00|b);
654 } else if(IS_PRIVATE_USE(c)) {
655 *p=(uint16_t)(0xc00|b);
657 *p=(uint16_t)(0x800|b);
660 /* check that this Unicode code point was still unassigned */
663 fprintf(stderr, "error: duplicate Unicode code point at U+%04x<->0x%02x see 0x%02x\n",
664 (int)c, b, old&0xff);
667 fprintf(stderr, "duplicate Unicode code point at U+%04x<->0x%02x see 0x%02x\n",
668 (int)c, b, old&0xff);
670 /* continue after the above warning if the precision of the mapping is unspecified */
677 MBCSAddFromUnicode(MBCSData *mbcsData,
678 const uint8_t *bytes, int32_t length,
684 uint32_t idx, b, old, stage3Index;
685 int32_t maxCharLength;
687 uint32_t blockSize, newTop, i, nextOffset, newBlock, min, overlap, maxOverlap;
689 maxCharLength=mbcsData->ucm->states.maxCharLength;
691 if( mbcsData->ucm->states.outputType==MBCS_OUTPUT_2_SISO &&
692 (!IGNORE_SISO_CHECK && (*bytes==0xe || *bytes==0xf))
694 fprintf(stderr, "error: illegal mapping to SI or SO for SI/SO codepage: U+%04x<->0x%s\n",
695 (int)c, printBytes(buffer, bytes, length));
699 if(flag==1 && length==1 && *bytes==0) {
700 fprintf(stderr, "error: unable to encode a |1 fallback from U+%04x to 0x%02x\n",
706 * Walk down the triple-stage compact array ("trie") and
707 * allocate parts as necessary.
708 * Note that the first stage 2 and 3 blocks are reserved for
709 * all-unassigned mappings.
710 * We assume that length<=maxCharLength and that c<=0x10ffff.
712 stage3=mbcsData->fromUBytes;
714 /* inspect stage 1 */
715 idx=c>>MBCS_STAGE_1_SHIFT;
716 if(mbcsData->utf8Friendly && c<=mbcsData->utf8Max) {
717 nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK&~(MBCS_UTF8_STAGE_3_BLOCKS-1);
719 nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK;
721 if(mbcsData->stage1[idx]==MBCS_STAGE_2_ALL_UNASSIGNED_INDEX) {
722 /* allocate another block in stage 2 */
723 newBlock=mbcsData->stage2Top;
724 if(mbcsData->utf8Friendly) {
725 min=newBlock-nextOffset; /* minimum block start with overlap */
726 while(min<newBlock && mbcsData->stage2[newBlock-1]==0) {
730 newTop=newBlock+MBCS_STAGE_2_BLOCK_SIZE;
732 if(newTop>MBCS_MAX_STAGE_2_TOP) {
733 fprintf(stderr, "error: too many stage 2 entries at U+%04x<->0x%s\n",
734 (int)c, printBytes(buffer, bytes, length));
739 * each stage 2 block contains 64 32-bit words:
740 * 6 code point bits 9..4 with value with bits 31..16 "assigned" flags and bits 15..0 stage 3 index
743 while(newBlock<newTop) {
744 mbcsData->stage1[i++]=(uint16_t)newBlock;
745 newBlock+=MBCS_STAGE_2_BLOCK_SIZE;
747 mbcsData->stage2Top=newTop; /* ==newBlock */
750 /* inspect stage 2 */
751 idx=mbcsData->stage1[idx]+nextOffset;
752 if(mbcsData->utf8Friendly && c<=mbcsData->utf8Max) {
753 /* allocate 64-entry blocks for UTF-8-friendly lookup */
754 blockSize=MBCS_UTF8_STAGE_3_BLOCK_SIZE*maxCharLength;
755 nextOffset=c&MBCS_UTF8_STAGE_3_BLOCK_MASK;
757 blockSize=MBCS_STAGE_3_BLOCK_SIZE*maxCharLength;
758 nextOffset=c&MBCS_STAGE_3_BLOCK_MASK;
760 if(mbcsData->stage2[idx]==0) {
761 /* allocate another block in stage 3 */
762 newBlock=mbcsData->stage3Top;
763 if(mbcsData->utf8Friendly && nextOffset>=MBCS_STAGE_3_GRANULARITY) {
765 * Overlap stage 3 blocks only in multiples of 16-entry blocks
766 * because of the indexing granularity in stage 2.
768 maxOverlap=(nextOffset&~(MBCS_STAGE_3_GRANULARITY-1))*maxCharLength;
770 overlap<maxOverlap && stage3[newBlock-overlap-1]==0;
773 overlap=(overlap/MBCS_STAGE_3_GRANULARITY)/maxCharLength;
774 overlap=(overlap*MBCS_STAGE_3_GRANULARITY)*maxCharLength;
778 newTop=newBlock+blockSize;
780 if(newTop>MBCS_STAGE_3_MBCS_SIZE*(uint32_t)maxCharLength) {
781 fprintf(stderr, "error: too many code points at U+%04x<->0x%s\n",
782 (int)c, printBytes(buffer, bytes, length));
785 /* each block has 16*maxCharLength bytes */
787 while(newBlock<newTop) {
788 mbcsData->stage2[i++]=(newBlock/MBCS_STAGE_3_GRANULARITY)/maxCharLength;
789 newBlock+=MBCS_STAGE_3_BLOCK_SIZE*maxCharLength;
791 mbcsData->stage3Top=newTop; /* ==newBlock */
794 stage3Index=MBCS_STAGE_3_GRANULARITY*(uint32_t)(uint16_t)mbcsData->stage2[idx];
796 /* Build an alternate, UTF-8-friendly stage table as well. */
797 if(mbcsData->utf8Friendly && c<=mbcsData->utf8Max) {
798 /* Overflow for uint16_t entries in stageUTF8? */
799 if(stage3Index>0xffff) {
801 * This can occur only if the mapping table is nearly perfectly filled and if
803 * (There is no known charset like this. GB 18030 does not map
804 * surrogate code points and LMBCS does not map 256 PUA code points.)
806 * Otherwise, stage3Index<=MBCS_UTF8_LIMIT<0xffff
807 * (stage3Index can at most reach exactly MBCS_UTF8_LIMIT)
808 * because we have a sorted table and there are at most MBCS_UTF8_LIMIT
809 * mappings with 0<=c<MBCS_UTF8_LIMIT, and there is only also
810 * the initial all-unassigned block in stage3.
812 * Solution for the overflow: Reduce utf8Max to the next lower value, 0xfeff.
814 * (See svn revision 20866 of the markus/ucnvutf8 feature branch for
815 * code that causes MBCSAddTable() to rebuild the table not utf8Friendly
816 * in case of overflow. That code was not tested.)
818 mbcsData->utf8Max=0xfeff;
821 * The stage 3 block has been assigned for the regular trie.
822 * Just copy its index into stageUTF8[], without the granularity.
824 mbcsData->stageUTF8[c>>MBCS_UTF8_STAGE_SHIFT]=(uint16_t)stage3Index;
828 /* write the codepage bytes into stage 3 and get the previous bytes */
830 /* assemble the bytes into a single integer */
850 p=stage3+(stage3Index+nextOffset)*maxCharLength;
851 switch(maxCharLength) {
854 *(uint16_t *)p=(uint16_t)b;
857 old=(uint32_t)*p<<16;
858 *p++=(uint8_t)(b>>16);
859 old|=(uint32_t)*p<<8;
860 *p++=(uint8_t)(b>>8);
869 /* will never occur */
873 /* check that this Unicode code point was still unassigned */
874 if((mbcsData->stage2[idx+(nextOffset>>MBCS_STAGE_2_SHIFT)]&(1UL<<(16+(c&0xf))))!=0 || old!=0) {
876 fprintf(stderr, "error: duplicate Unicode code point at U+%04x<->0x%s see 0x%02x\n",
877 (int)c, printBytes(buffer, bytes, length), (int)old);
880 fprintf(stderr, "duplicate Unicode code point at U+%04x<->0x%s see 0x%02x\n",
881 (int)c, printBytes(buffer, bytes, length), (int)old);
883 /* continue after the above warning if the precision of the mapping is
887 /* set the roundtrip flag */
888 mbcsData->stage2[idx+(nextOffset>>4)]|=(1UL<<(16+(c&0xf)));
895 MBCSOkForBaseFromUnicode(const MBCSData *mbcsData,
896 const uint8_t *bytes, int32_t length,
897 UChar32 c, int8_t flag) {
899 * A 1:1 mapping does not fit into the MBCS base table's fromUnicode table under
900 * the following conditions:
902 * - a |2 SUB mapping for <subchar1> (no base table data structure for them)
903 * - a |1 fallback to 0x00 (result value 0, indistinguishable from unmappable entry)
904 * - a multi-byte mapping with leading 0x00 bytes (no explicit length field)
906 * Some of these tests are redundant with ucm_mappingType().
908 if( (flag==2 && length==1) ||
909 (flag==1 && bytes[0]==0) || /* testing length==1 would be redundant with the next test */
910 (flag<=1 && length>1 && bytes[0]==0)
916 * Additional restrictions for UTF-8-friendly fromUnicode tables,
917 * for code points up to the maximum optimized one:
919 * - any mapping to 0x00 (result value 0, indistinguishable from unmappable entry)
920 * - any |1 fallback (no roundtrip flags in the optimized table)
922 if(mbcsData->utf8Friendly && flag<=1 && c<=mbcsData->utf8Max && (bytes[0]==0 || flag==1)) {
927 * If we omit the fromUnicode data, we can only store roundtrips there
928 * because only they are recoverable from the toUnicode data.
929 * Fallbacks must go into the extension table.
931 if(mbcsData->omitFromU && flag!=0) {
935 /* All other mappings do fit into the base table. */
939 /* we can assume that the table only contains 1:1 mappings with <=4 bytes each */
941 MBCSAddTable(NewConverter *cnvData, UCMTable *table, UConverterStaticData *staticData) {
945 int32_t i, maxCharLength;
947 UBool isOK, utf8Friendly;
949 staticData->unicodeMask=table->unicodeMask;
950 if(staticData->unicodeMask==3) {
951 fprintf(stderr, "error: contains mappings for both supplementary and surrogate code points\n");
955 staticData->conversionType=UCNV_MBCS;
957 mbcsData=(MBCSData *)cnvData;
958 maxCharLength=mbcsData->ucm->states.maxCharLength;
961 * Generation of UTF-8-friendly data requires
962 * a sorted table, which makeconv generates when explicit precision
963 * indicators are used.
965 mbcsData->utf8Friendly=utf8Friendly=(UBool)((table->flagsType&UCM_FLAGS_EXPLICIT)!=0);
967 mbcsData->utf8Max=MBCS_UTF8_MAX;
968 if(SMALL && maxCharLength>1) {
969 mbcsData->omitFromU=TRUE;
973 if(SMALL && maxCharLength>1) {
975 "makeconv warning: --small not available for .ucm files without |0 etc.\n");
979 if(!MBCSStartMappings(mbcsData)) {
983 staticData->hasFromUnicodeFallback=FALSE;
984 staticData->hasToUnicodeFallback=FALSE;
989 for(i=0; i<table->mappingsLength; ++m, ++i) {
994 * Small optimization for --small .cnv files:
996 * If there are fromUnicode mappings above MBCS_UTF8_MAX,
997 * then the file size will be smaller if we make utf8Max larger
998 * because the size increase in stageUTF8 will be more than balanced by
999 * how much less of stage2 needs to be stored.
1001 * There is no point in doing this incrementally because stageUTF8
1002 * uses so much less space per block than stage2,
1003 * so we immediately increase utf8Max to 0xffff.
1005 * Do not increase utf8Max if it is already at 0xfeff because MBCSAddFromUnicode()
1006 * sets it to that value when stageUTF8 overflows.
1008 if( mbcsData->omitFromU && f<=1 &&
1009 mbcsData->utf8Max<c && c<=0xffff &&
1010 mbcsData->utf8Max<0xfeff
1012 mbcsData->utf8Max=0xffff;
1017 /* there was no precision/fallback indicator */
1018 /* fall through to set the mappings */
1021 /* set roundtrip mappings */
1022 isOK&=MBCSAddToUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
1024 if(maxCharLength==1) {
1025 isOK&=MBCSSingleAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
1026 } else if(MBCSOkForBaseFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f)) {
1027 isOK&=MBCSAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
1029 m->f|=MBCS_FROM_U_EXT_FLAG;
1030 m->moveFlag=UCM_MOVE_TO_EXT;
1034 /* set only a fallback mapping from Unicode to codepage */
1035 if(maxCharLength==1) {
1036 staticData->hasFromUnicodeFallback=TRUE;
1037 isOK&=MBCSSingleAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
1038 } else if(MBCSOkForBaseFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f)) {
1039 staticData->hasFromUnicodeFallback=TRUE;
1040 isOK&=MBCSAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
1042 m->f|=MBCS_FROM_U_EXT_FLAG;
1043 m->moveFlag=UCM_MOVE_TO_EXT;
1047 /* ignore |2 SUB mappings, except to move <subchar1> mappings to the extension table */
1048 if(maxCharLength>1 && m->bLen==1) {
1049 m->f|=MBCS_FROM_U_EXT_FLAG;
1050 m->moveFlag=UCM_MOVE_TO_EXT;
1054 /* set only a fallback mapping from codepage to Unicode */
1055 staticData->hasToUnicodeFallback=TRUE;
1056 isOK&=MBCSAddToUnicode(mbcsData, m->b.bytes, m->bLen, c, f);
1059 /* move "good one-way" mappings to the extension table */
1060 m->f|=MBCS_FROM_U_EXT_FLAG;
1061 m->moveFlag=UCM_MOVE_TO_EXT;
1064 /* will not occur because the parser checked it already */
1065 fprintf(stderr, "error: illegal fallback indicator %d\n", f);
1070 MBCSPostprocess(mbcsData, staticData);
1076 transformEUC(MBCSData *mbcsData) {
1078 uint32_t i, value, oldLength, old3Top;
1081 oldLength=mbcsData->ucm->states.maxCharLength;
1086 old3Top=mbcsData->stage3Top;
1088 /* careful: 2-byte and 4-byte codes are stored in platform endianness! */
1090 /* test if all first bytes are in {0, 0x8e, 0x8f} */
1091 p8=mbcsData->fromUBytes;
1093 #if !U_IS_BIG_ENDIAN
1099 for(i=0; i<old3Top; i+=oldLength) {
1101 if(b!=0 && b!=0x8e && b!=0x8f) {
1102 /* some first byte does not fit the EUC pattern, nothing to be done */
1106 /* restore p if it was modified above */
1107 p8=mbcsData->fromUBytes;
1109 /* modify outputType and adjust stage3Top */
1110 mbcsData->ucm->states.outputType=(int8_t)(MBCS_OUTPUT_3_EUC+oldLength-3);
1111 mbcsData->stage3Top=(old3Top*(oldLength-1))/oldLength;
1114 * EUC-encode all byte sequences;
1115 * see "CJKV Information Processing" (1st ed. 1999) from Ken Lunde, O'Reilly,
1116 * p. 161 in chapter 4 "Encoding Methods"
1118 * This also must reverse the byte order if the platform is little-endian!
1121 uint16_t *q=(uint16_t *)p8;
1122 for(i=0; i<old3Top; i+=oldLength) {
1125 /* short sequences are stored directly */
1126 /* code set 0 or 1 */
1127 (*q++)=(uint16_t)((p8[1]<<8)|p8[2]);
1128 } else if(b==0x8e) {
1130 (*q++)=(uint16_t)(((p8[1]&0x7f)<<8)|p8[2]);
1131 } else /* b==0x8f */ {
1133 (*q++)=(uint16_t)((p8[1]<<8)|(p8[2]&0x7f));
1137 } else /* oldLength==4 */ {
1139 uint32_t *p32=(uint32_t *)p8;
1140 for(i=0; i<old3Top; i+=4) {
1142 if(value<=0xffffff) {
1143 /* short sequences are stored directly */
1144 /* code set 0 or 1 */
1145 (*q++)=(uint8_t)(value>>16);
1146 (*q++)=(uint8_t)(value>>8);
1147 (*q++)=(uint8_t)value;
1148 } else if(value<=0x8effffff) {
1150 (*q++)=(uint8_t)((value>>16)&0x7f);
1151 (*q++)=(uint8_t)(value>>8);
1152 (*q++)=(uint8_t)value;
1153 } else /* first byte is 0x8f */ {
1155 (*q++)=(uint8_t)(value>>16);
1156 (*q++)=(uint8_t)((value>>8)&0x7f);
1157 (*q++)=(uint8_t)value;
1166 * Compact stage 2 for SBCS by overlapping adjacent stage 2 blocks as far
1167 * as possible. Overlapping is done on unassigned head and tail
1168 * parts of blocks in steps of MBCS_STAGE_2_MULTIPLIER.
1169 * Stage 1 indexes need to be adjusted accordingly.
1170 * This function is very similar to genprops/store.c/compactStage().
1173 singleCompactStage2(MBCSData *mbcsData) {
1174 /* this array maps the ordinal number of a stage 2 block to its new stage 1 index */
1175 uint16_t map[MBCS_STAGE_2_MAX_BLOCKS];
1176 uint16_t i, start, prevEnd, newStart;
1178 /* enter the all-unassigned first stage 2 block into the map */
1179 map[0]=MBCS_STAGE_2_ALL_UNASSIGNED_INDEX;
1181 /* begin with the first block after the all-unassigned one */
1182 start=newStart=MBCS_STAGE_2_FIRST_ASSIGNED;
1183 while(start<mbcsData->stage2Top) {
1184 prevEnd=(uint16_t)(newStart-1);
1186 /* find the size of the overlap */
1187 for(i=0; i<MBCS_STAGE_2_BLOCK_SIZE && mbcsData->stage2Single[start+i]==0 && mbcsData->stage2Single[prevEnd-i]==0; ++i) {}
1190 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=(uint16_t)(newStart-i);
1192 /* move the non-overlapping indexes to their new positions */
1194 for(i=(uint16_t)(MBCS_STAGE_2_BLOCK_SIZE-i); i>0; --i) {
1195 mbcsData->stage2Single[newStart++]=mbcsData->stage2Single[start++];
1197 } else if(newStart<start) {
1198 /* move the indexes to their new positions */
1199 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=newStart;
1200 for(i=MBCS_STAGE_2_BLOCK_SIZE; i>0; --i) {
1201 mbcsData->stage2Single[newStart++]=mbcsData->stage2Single[start++];
1203 } else /* no overlap && newStart==start */ {
1204 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=start;
1205 start=newStart+=MBCS_STAGE_2_BLOCK_SIZE;
1209 /* adjust stage2Top */
1210 if(VERBOSE && newStart<mbcsData->stage2Top) {
1211 printf("compacting stage 2 from stage2Top=0x%lx to 0x%lx, saving %ld bytes\n",
1212 (unsigned long)mbcsData->stage2Top, (unsigned long)newStart,
1213 (long)(mbcsData->stage2Top-newStart)*2);
1215 mbcsData->stage2Top=newStart;
1217 /* now adjust stage 1 */
1218 for(i=0; i<MBCS_STAGE_1_SIZE; ++i) {
1219 mbcsData->stage1[i]=map[mbcsData->stage1[i]>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT];
1223 /* Compact stage 3 for SBCS - same algorithm as above. */
1225 singleCompactStage3(MBCSData *mbcsData) {
1226 uint16_t *stage3=(uint16_t *)mbcsData->fromUBytes;
1228 /* this array maps the ordinal number of a stage 3 block to its new stage 2 index */
1229 uint16_t map[0x1000];
1230 uint16_t i, start, prevEnd, newStart;
1232 /* enter the all-unassigned first stage 3 block into the map */
1235 /* begin with the first block after the all-unassigned one */
1237 while(start<mbcsData->stage3Top) {
1238 prevEnd=(uint16_t)(newStart-1);
1240 /* find the size of the overlap */
1241 for(i=0; i<16 && stage3[start+i]==0 && stage3[prevEnd-i]==0; ++i) {}
1244 map[start>>4]=(uint16_t)(newStart-i);
1246 /* move the non-overlapping indexes to their new positions */
1248 for(i=(uint16_t)(16-i); i>0; --i) {
1249 stage3[newStart++]=stage3[start++];
1251 } else if(newStart<start) {
1252 /* move the indexes to their new positions */
1253 map[start>>4]=newStart;
1254 for(i=16; i>0; --i) {
1255 stage3[newStart++]=stage3[start++];
1257 } else /* no overlap && newStart==start */ {
1258 map[start>>4]=start;
1263 /* adjust stage3Top */
1264 if(VERBOSE && newStart<mbcsData->stage3Top) {
1265 printf("compacting stage 3 from stage3Top=0x%lx to 0x%lx, saving %ld bytes\n",
1266 (unsigned long)mbcsData->stage3Top, (unsigned long)newStart,
1267 (long)(mbcsData->stage3Top-newStart)*2);
1269 mbcsData->stage3Top=newStart;
1271 /* now adjust stage 2 */
1272 for(i=0; i<mbcsData->stage2Top; ++i) {
1273 mbcsData->stage2Single[i]=map[mbcsData->stage2Single[i]>>4];
1278 * Compact stage 2 by overlapping adjacent stage 2 blocks as far
1279 * as possible. Overlapping is done on unassigned head and tail
1280 * parts of blocks in steps of MBCS_STAGE_2_MULTIPLIER.
1281 * Stage 1 indexes need to be adjusted accordingly.
1282 * This function is very similar to genprops/store.c/compactStage().
1285 compactStage2(MBCSData *mbcsData) {
1286 /* this array maps the ordinal number of a stage 2 block to its new stage 1 index */
1287 uint16_t map[MBCS_STAGE_2_MAX_BLOCKS];
1288 uint16_t i, start, prevEnd, newStart;
1290 /* enter the all-unassigned first stage 2 block into the map */
1291 map[0]=MBCS_STAGE_2_ALL_UNASSIGNED_INDEX;
1293 /* begin with the first block after the all-unassigned one */
1294 start=newStart=MBCS_STAGE_2_FIRST_ASSIGNED;
1295 while(start<mbcsData->stage2Top) {
1296 prevEnd=(uint16_t)(newStart-1);
1298 /* find the size of the overlap */
1299 for(i=0; i<MBCS_STAGE_2_BLOCK_SIZE && mbcsData->stage2[start+i]==0 && mbcsData->stage2[prevEnd-i]==0; ++i) {}
1302 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=(uint16_t)(newStart-i);
1304 /* move the non-overlapping indexes to their new positions */
1306 for(i=(uint16_t)(MBCS_STAGE_2_BLOCK_SIZE-i); i>0; --i) {
1307 mbcsData->stage2[newStart++]=mbcsData->stage2[start++];
1309 } else if(newStart<start) {
1310 /* move the indexes to their new positions */
1311 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=newStart;
1312 for(i=MBCS_STAGE_2_BLOCK_SIZE; i>0; --i) {
1313 mbcsData->stage2[newStart++]=mbcsData->stage2[start++];
1315 } else /* no overlap && newStart==start */ {
1316 map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=start;
1317 start=newStart+=MBCS_STAGE_2_BLOCK_SIZE;
1321 /* adjust stage2Top */
1322 if(VERBOSE && newStart<mbcsData->stage2Top) {
1323 printf("compacting stage 2 from stage2Top=0x%lx to 0x%lx, saving %ld bytes\n",
1324 (unsigned long)mbcsData->stage2Top, (unsigned long)newStart,
1325 (long)(mbcsData->stage2Top-newStart)*4);
1327 mbcsData->stage2Top=newStart;
1329 /* now adjust stage 1 */
1330 for(i=0; i<MBCS_STAGE_1_SIZE; ++i) {
1331 mbcsData->stage1[i]=map[mbcsData->stage1[i]>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT];
1336 MBCSPostprocess(MBCSData *mbcsData, const UConverterStaticData * /*staticData*/) {
1338 int32_t maxCharLength, stage3Width;
1340 states=&mbcsData->ucm->states;
1341 stage3Width=maxCharLength=states->maxCharLength;
1343 ucm_optimizeStates(states,
1344 &mbcsData->unicodeCodeUnits,
1345 mbcsData->toUFallbacks, mbcsData->countToUFallbacks,
1348 /* try to compact the fromUnicode tables */
1349 if(transformEUC(mbcsData)) {
1354 * UTF-8-friendly tries are built precompacted, to cope with variable
1355 * stage 3 allocation block sizes.
1357 * Tables without precision indicators cannot be built that way,
1358 * because if a block was overlapped with a previous one, then a smaller
1359 * code point for the same block would not fit.
1360 * Therefore, such tables are not marked UTF-8-friendly and must be
1361 * compacted after all mappings are entered.
1363 if(!mbcsData->utf8Friendly) {
1364 if(maxCharLength==1) {
1365 singleCompactStage3(mbcsData);
1366 singleCompactStage2(mbcsData);
1368 compactStage2(mbcsData);
1373 /*uint32_t c, i1, i2, i2Limit, i3;*/
1375 printf("fromUnicode number of uint%s_t in stage 2: 0x%lx=%lu\n",
1376 maxCharLength==1 ? "16" : "32",
1377 (unsigned long)mbcsData->stage2Top,
1378 (unsigned long)mbcsData->stage2Top);
1379 printf("fromUnicode number of %d-byte stage 3 mapping entries: 0x%lx=%lu\n",
1381 (unsigned long)mbcsData->stage3Top/stage3Width,
1382 (unsigned long)mbcsData->stage3Top/stage3Width);
1385 for(i1=0; i1<MBCS_STAGE_1_SIZE; ++i1) {
1386 i2=mbcsData->stage1[i1];
1388 c+=MBCS_STAGE_2_BLOCK_SIZE*MBCS_STAGE_3_BLOCK_SIZE;
1391 for(i2Limit=i2+MBCS_STAGE_2_BLOCK_SIZE; i2<i2Limit; ++i2) {
1392 if(maxCharLength==1) {
1393 i3=mbcsData->stage2Single[i2];
1395 i3=(uint16_t)mbcsData->stage2[i2];
1398 c+=MBCS_STAGE_3_BLOCK_SIZE;
1401 printf("U+%04lx i1=0x%02lx i2=0x%04lx i3=0x%04lx\n",
1406 c+=MBCS_STAGE_3_BLOCK_SIZE;
1414 MBCSWrite(NewConverter *cnvData, const UConverterStaticData *staticData,
1415 UNewDataMemory *pData, int32_t tableType) {
1416 MBCSData *mbcsData=(MBCSData *)cnvData;
1417 uint32_t stage2Start, stage2Length;
1418 uint32_t top, stageUTF8Length=0;
1419 int32_t i, stage1Top;
1420 uint32_t headerLength;
1422 _MBCSHeader header=UCNV_MBCS_HEADER_INITIALIZER;
1424 stage2Length=mbcsData->stage2Top;
1425 if(mbcsData->omitFromU) {
1426 /* find how much of stage2 can be omitted */
1427 int32_t utf8Limit=(int32_t)mbcsData->utf8Max+1;
1428 uint32_t st2=0; /*initialized it to avoid compiler warnings */
1430 i=utf8Limit>>MBCS_STAGE_1_SHIFT;
1431 if((utf8Limit&((1<<MBCS_STAGE_1_SHIFT)-1))!=0 && (st2=mbcsData->stage1[i])!=0) {
1432 /* utf8Limit is in the middle of an existing stage 2 block */
1433 stage2Start=st2+((utf8Limit>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK);
1435 /* find the last stage2 block with mappings before utf8Limit */
1436 while(i>0 && (st2=mbcsData->stage1[--i])==0) {}
1437 /* stage2 up to the end of this block corresponds to stageUTF8 */
1438 stage2Start=st2+MBCS_STAGE_2_BLOCK_SIZE;
1440 header.options|=MBCS_OPT_NO_FROM_U;
1441 header.fullStage2Length=stage2Length;
1442 stage2Length-=stage2Start;
1444 printf("+ omitting %lu out of %lu stage2 entries and %lu fromUBytes\n",
1445 (unsigned long)stage2Start,
1446 (unsigned long)mbcsData->stage2Top,
1447 (unsigned long)mbcsData->stage3Top);
1448 printf("+ total size savings: %lu bytes\n", (unsigned long)stage2Start*4+mbcsData->stage3Top);
1454 if(staticData->unicodeMask&UCNV_HAS_SUPPLEMENTARY) {
1455 stage1Top=MBCS_STAGE_1_SIZE; /* 0x440==1088 */
1457 stage1Top=0x40; /* 0x40==64 */
1460 /* adjust stage 1 entries to include the size of stage 1 in the offsets to stage 2 */
1461 if(mbcsData->ucm->states.maxCharLength==1) {
1462 for(i=0; i<stage1Top; ++i) {
1463 mbcsData->stage1[i]+=(uint16_t)stage1Top;
1466 /* stage2Top/Length have counted 16-bit results, now we need to count bytes */
1467 /* also round up to a multiple of 4 bytes */
1468 stage2Length=(stage2Length*2+1)&~1;
1470 /* stage3Top has counted 16-bit results, now we need to count bytes */
1471 mbcsData->stage3Top*=2;
1473 if(mbcsData->utf8Friendly) {
1474 header.version[2]=(uint8_t)(SBCS_UTF8_MAX>>8); /* store 0x1f for max==0x1fff */
1477 for(i=0; i<stage1Top; ++i) {
1478 mbcsData->stage1[i]+=(uint16_t)stage1Top/2; /* stage 2 contains 32-bit entries, stage 1 16-bit entries */
1481 /* stage2Top/Length have counted 32-bit results, now we need to count bytes */
1483 /* leave stage2Start counting 32-bit units */
1485 if(mbcsData->utf8Friendly) {
1486 stageUTF8Length=(mbcsData->utf8Max+1)>>MBCS_UTF8_STAGE_SHIFT;
1487 header.version[2]=(uint8_t)(mbcsData->utf8Max>>8); /* store 0xd7 for max==0xd7ff */
1490 /* stage3Top has already counted bytes */
1493 /* round up stage3Top so that the sizes of all data blocks are multiples of 4 */
1494 mbcsData->stage3Top=(mbcsData->stage3Top+3)&~3;
1496 /* fill the header */
1497 if(header.options&MBCS_OPT_INCOMPATIBLE_MASK) {
1498 header.version[0]=5;
1499 if(header.options&MBCS_OPT_NO_FROM_U) {
1500 headerLength=10; /* include fullStage2Length */
1502 headerLength=MBCS_HEADER_V5_MIN_LENGTH; /* 9 */
1505 header.version[0]=4;
1506 headerLength=MBCS_HEADER_V4_LENGTH; /* 8 */
1508 header.version[1]=4;
1509 /* header.version[2] set above for utf8Friendly data */
1511 header.options|=(uint32_t)headerLength;
1513 header.countStates=mbcsData->ucm->states.countStates;
1514 header.countToUFallbacks=mbcsData->countToUFallbacks;
1516 header.offsetToUCodeUnits=
1518 mbcsData->ucm->states.countStates*1024+
1519 mbcsData->countToUFallbacks*sizeof(_MBCSToUFallback);
1520 header.offsetFromUTable=
1521 header.offsetToUCodeUnits+
1522 mbcsData->ucm->states.countToUCodeUnits*2;
1523 header.offsetFromUBytes=
1524 header.offsetFromUTable+
1527 header.fromUBytesLength=mbcsData->stage3Top;
1529 top=header.offsetFromUBytes+stageUTF8Length*2;
1530 if(!(header.options&MBCS_OPT_NO_FROM_U)) {
1531 top+=header.fromUBytesLength;
1534 header.flags=(uint8_t)(mbcsData->ucm->states.outputType);
1536 if(tableType&TABLE_EXT) {
1538 fprintf(stderr, "error: offset 0x%lx to extension table exceeds 0xffffff\n", (long)top);
1542 header.flags|=top<<8;
1545 /* write the MBCS data */
1546 udata_writeBlock(pData, &header, headerLength*4);
1547 udata_writeBlock(pData, mbcsData->ucm->states.stateTable, header.countStates*1024);
1548 udata_writeBlock(pData, mbcsData->toUFallbacks, mbcsData->countToUFallbacks*sizeof(_MBCSToUFallback));
1549 udata_writeBlock(pData, mbcsData->unicodeCodeUnits, mbcsData->ucm->states.countToUCodeUnits*2);
1550 udata_writeBlock(pData, mbcsData->stage1, stage1Top*2);
1551 if(mbcsData->ucm->states.maxCharLength==1) {
1552 udata_writeBlock(pData, mbcsData->stage2Single+stage2Start, stage2Length);
1554 udata_writeBlock(pData, mbcsData->stage2+stage2Start, stage2Length);
1556 if(!(header.options&MBCS_OPT_NO_FROM_U)) {
1557 udata_writeBlock(pData, mbcsData->fromUBytes, mbcsData->stage3Top);
1560 if(stageUTF8Length>0) {
1561 udata_writeBlock(pData, mbcsData->stageUTF8, stageUTF8Length*2);
1564 /* return the number of bytes that should have been written */