michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2000-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: genmbcs.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2000jul06 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: #include michael@0: #include "unicode/utypes.h" michael@0: #include "cstring.h" michael@0: #include "cmemory.h" michael@0: #include "unewdata.h" michael@0: #include "ucnv_cnv.h" michael@0: #include "ucnvmbcs.h" michael@0: #include "ucm.h" michael@0: #include "makeconv.h" michael@0: #include "genmbcs.h" michael@0: michael@0: /* michael@0: * TODO: Split this file into toUnicode, SBCSFromUnicode and MBCSFromUnicode files. michael@0: * Reduce tests for maxCharLength. michael@0: */ michael@0: michael@0: struct MBCSData { michael@0: NewConverter newConverter; michael@0: michael@0: UCMFile *ucm; michael@0: michael@0: /* toUnicode (state table in ucm->states) */ michael@0: _MBCSToUFallback toUFallbacks[MBCS_MAX_FALLBACK_COUNT]; michael@0: int32_t countToUFallbacks; michael@0: uint16_t *unicodeCodeUnits; michael@0: michael@0: /* fromUnicode */ michael@0: uint16_t stage1[MBCS_STAGE_1_SIZE]; michael@0: uint16_t stage2Single[MBCS_STAGE_2_SIZE]; /* stage 2 for single-byte codepages */ michael@0: uint32_t stage2[MBCS_STAGE_2_SIZE]; /* stage 2 for MBCS */ michael@0: uint8_t *fromUBytes; michael@0: uint32_t stage2Top, stage3Top; michael@0: michael@0: /* fromUTF8 */ michael@0: uint16_t stageUTF8[0x10000>>MBCS_UTF8_STAGE_SHIFT]; /* allow for utf8Max=0xffff */ michael@0: michael@0: /* michael@0: * Maximum UTF-8-friendly code point. michael@0: * 0 if !utf8Friendly, otherwise 0x01ff..0xffff in steps of 0x100. michael@0: * If utf8Friendly, utf8Max is normally either MBCS_UTF8_MAX or 0xffff. michael@0: */ michael@0: uint16_t utf8Max; michael@0: michael@0: UBool utf8Friendly; michael@0: UBool omitFromU; michael@0: }; michael@0: michael@0: /* prototypes */ michael@0: static void michael@0: MBCSClose(NewConverter *cnvData); michael@0: michael@0: static UBool michael@0: MBCSStartMappings(MBCSData *mbcsData); michael@0: michael@0: static UBool michael@0: MBCSAddToUnicode(MBCSData *mbcsData, michael@0: const uint8_t *bytes, int32_t length, michael@0: UChar32 c, michael@0: int8_t flag); michael@0: michael@0: static UBool michael@0: MBCSIsValid(NewConverter *cnvData, michael@0: const uint8_t *bytes, int32_t length); michael@0: michael@0: static UBool michael@0: MBCSSingleAddFromUnicode(MBCSData *mbcsData, michael@0: const uint8_t *bytes, int32_t length, michael@0: UChar32 c, michael@0: int8_t flag); michael@0: michael@0: static UBool michael@0: MBCSAddFromUnicode(MBCSData *mbcsData, michael@0: const uint8_t *bytes, int32_t length, michael@0: UChar32 c, michael@0: int8_t flag); michael@0: michael@0: static void michael@0: MBCSPostprocess(MBCSData *mbcsData, const UConverterStaticData *staticData); michael@0: michael@0: static UBool michael@0: MBCSAddTable(NewConverter *cnvData, UCMTable *table, UConverterStaticData *staticData); michael@0: michael@0: static uint32_t michael@0: MBCSWrite(NewConverter *cnvData, const UConverterStaticData *staticData, michael@0: UNewDataMemory *pData, int32_t tableType); michael@0: michael@0: /* helper ------------------------------------------------------------------- */ michael@0: michael@0: static inline char michael@0: hexDigit(uint8_t digit) { michael@0: return digit<=9 ? (char)('0'+digit) : (char)('a'-10+digit); michael@0: } michael@0: michael@0: static inline char * michael@0: printBytes(char *buffer, const uint8_t *bytes, int32_t length) { michael@0: char *s=buffer; michael@0: while(length>0) { michael@0: *s++=hexDigit((uint8_t)(*bytes>>4)); michael@0: *s++=hexDigit((uint8_t)(*bytes&0xf)); michael@0: ++bytes; michael@0: --length; michael@0: } michael@0: michael@0: *s=0; michael@0: return buffer; michael@0: } michael@0: michael@0: /* implementation ----------------------------------------------------------- */ michael@0: michael@0: static MBCSData gDummy; michael@0: michael@0: U_CFUNC const MBCSData * michael@0: MBCSGetDummy() { michael@0: uprv_memset(&gDummy, 0, sizeof(MBCSData)); michael@0: michael@0: /* michael@0: * Set "pessimistic" values which may sometimes move too many michael@0: * mappings to the extension table (but never too few). michael@0: * These values cause MBCSOkForBaseFromUnicode() to return FALSE for the michael@0: * largest set of mappings. michael@0: * Assume maxCharLength>1. michael@0: */ michael@0: gDummy.utf8Friendly=TRUE; michael@0: if(SMALL) { michael@0: gDummy.utf8Max=0xffff; michael@0: gDummy.omitFromU=TRUE; michael@0: } else { michael@0: gDummy.utf8Max=MBCS_UTF8_MAX; michael@0: } michael@0: return &gDummy; michael@0: } michael@0: michael@0: static void michael@0: MBCSInit(MBCSData *mbcsData, UCMFile *ucm) { michael@0: uprv_memset(mbcsData, 0, sizeof(MBCSData)); michael@0: michael@0: mbcsData->ucm=ucm; /* aliased, not owned */ michael@0: michael@0: mbcsData->newConverter.close=MBCSClose; michael@0: mbcsData->newConverter.isValid=MBCSIsValid; michael@0: mbcsData->newConverter.addTable=MBCSAddTable; michael@0: mbcsData->newConverter.write=MBCSWrite; michael@0: } michael@0: michael@0: NewConverter * michael@0: MBCSOpen(UCMFile *ucm) { michael@0: MBCSData *mbcsData=(MBCSData *)uprv_malloc(sizeof(MBCSData)); michael@0: if(mbcsData==NULL) { michael@0: printf("out of memory\n"); michael@0: exit(U_MEMORY_ALLOCATION_ERROR); michael@0: } michael@0: michael@0: MBCSInit(mbcsData, ucm); michael@0: return &mbcsData->newConverter; michael@0: } michael@0: michael@0: static void michael@0: MBCSDestruct(MBCSData *mbcsData) { michael@0: uprv_free(mbcsData->unicodeCodeUnits); michael@0: uprv_free(mbcsData->fromUBytes); michael@0: } michael@0: michael@0: static void michael@0: MBCSClose(NewConverter *cnvData) { michael@0: MBCSData *mbcsData=(MBCSData *)cnvData; michael@0: if(mbcsData!=NULL) { michael@0: MBCSDestruct(mbcsData); michael@0: uprv_free(mbcsData); michael@0: } michael@0: } michael@0: michael@0: static UBool michael@0: MBCSStartMappings(MBCSData *mbcsData) { michael@0: int32_t i, sum, maxCharLength, michael@0: stage2NullLength, stage2AllocLength, michael@0: stage3NullLength, stage3AllocLength; michael@0: michael@0: /* toUnicode */ michael@0: michael@0: /* allocate the code unit array and prefill it with "unassigned" values */ michael@0: sum=mbcsData->ucm->states.countToUCodeUnits; michael@0: if(VERBOSE) { michael@0: printf("the total number of offsets is 0x%lx=%ld\n", (long)sum, (long)sum); michael@0: } michael@0: michael@0: if(sum>0) { michael@0: mbcsData->unicodeCodeUnits=(uint16_t *)uprv_malloc(sum*sizeof(uint16_t)); michael@0: if(mbcsData->unicodeCodeUnits==NULL) { michael@0: fprintf(stderr, "error: out of memory allocating %ld 16-bit code units\n", michael@0: (long)sum); michael@0: return FALSE; michael@0: } michael@0: for(i=0; iunicodeCodeUnits[i]=0xfffe; michael@0: } michael@0: } michael@0: michael@0: /* fromUnicode */ michael@0: maxCharLength=mbcsData->ucm->states.maxCharLength; michael@0: michael@0: /* allocate the codepage mappings and preset the first 16 characters to 0 */ michael@0: if(maxCharLength==1) { michael@0: /* allocate 64k 16-bit results for single-byte codepages */ michael@0: sum=0x20000; michael@0: } else { michael@0: /* allocate 1M * maxCharLength bytes for at most 1M mappings */ michael@0: sum=0x100000*maxCharLength; michael@0: } michael@0: mbcsData->fromUBytes=(uint8_t *)uprv_malloc(sum); michael@0: if(mbcsData->fromUBytes==NULL) { michael@0: fprintf(stderr, "error: out of memory allocating %ld B for target mappings\n", (long)sum); michael@0: return FALSE; michael@0: } michael@0: uprv_memset(mbcsData->fromUBytes, 0, sum); michael@0: michael@0: /* michael@0: * UTF-8-friendly fromUnicode tries: allocate multiple blocks at a time. michael@0: * See ucnvmbcs.h for details. michael@0: * michael@0: * There is code, for example in ucnv_MBCSGetUnicodeSetForUnicode(), which michael@0: * assumes that the initial stage 2/3 blocks are the all-unassigned ones. michael@0: * Therefore, we refine the data structure while maintaining this placement michael@0: * even though it would be convenient to allocate the ASCII block at the michael@0: * beginning of stage 3, for example. michael@0: * michael@0: * UTF-8-friendly fromUnicode tries work from sorted tables and are built michael@0: * pre-compacted, overlapping adjacent stage 2/3 blocks. michael@0: * This is necessary because the block allocation and compaction changes michael@0: * at SBCS_UTF8_MAX or MBCS_UTF8_MAX, and for MBCS tables the additional michael@0: * stage table uses direct indexes into stage 3, without a multiplier and michael@0: * thus with a smaller reach. michael@0: * michael@0: * Non-UTF-8-friendly fromUnicode tries work from unsorted tables michael@0: * (because implicit precision is used), and are compacted michael@0: * in post-processing. michael@0: * michael@0: * Preallocation for UTF-8-friendly fromUnicode tries: michael@0: * michael@0: * Stage 3: michael@0: * 64-entry all-unassigned first block followed by ASCII (128 entries). michael@0: * michael@0: * Stage 2: michael@0: * 64-entry all-unassigned first block followed by preallocated michael@0: * 64-block for ASCII. michael@0: */ michael@0: michael@0: /* Preallocate ASCII as a linear 128-entry stage 3 block. */ michael@0: stage2NullLength=MBCS_STAGE_2_BLOCK_SIZE; michael@0: stage2AllocLength=MBCS_STAGE_2_BLOCK_SIZE; michael@0: michael@0: stage3NullLength=MBCS_UTF8_STAGE_3_BLOCK_SIZE; michael@0: stage3AllocLength=128; /* ASCII U+0000..U+007f */ michael@0: michael@0: /* Initialize stage 1 for the preallocated blocks. */ michael@0: sum=stage2NullLength; michael@0: for(i=0; i<(stage2AllocLength>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT); ++i) { michael@0: mbcsData->stage1[i]=sum; michael@0: sum+=MBCS_STAGE_2_BLOCK_SIZE; michael@0: } michael@0: mbcsData->stage2Top=stage2NullLength+stage2AllocLength; /* ==sum */ michael@0: michael@0: /* michael@0: * Stage 2 indexes count 16-blocks in stage 3 as follows: michael@0: * SBCS: directly, indexes increment by 16 michael@0: * MBCS: indexes need to be multiplied by 16*maxCharLength, indexes increment by 1 michael@0: * MBCS UTF-8: directly, indexes increment by 16 michael@0: */ michael@0: if(maxCharLength==1) { michael@0: sum=stage3NullLength; michael@0: for(i=0; i<(stage3AllocLength/MBCS_STAGE_3_BLOCK_SIZE); ++i) { michael@0: mbcsData->stage2Single[mbcsData->stage1[0]+i]=sum; michael@0: sum+=MBCS_STAGE_3_BLOCK_SIZE; michael@0: } michael@0: } else { michael@0: sum=stage3NullLength/MBCS_STAGE_3_GRANULARITY; michael@0: for(i=0; i<(stage3AllocLength/MBCS_STAGE_3_BLOCK_SIZE); ++i) { michael@0: mbcsData->stage2[mbcsData->stage1[0]+i]=sum; michael@0: sum+=MBCS_STAGE_3_BLOCK_SIZE/MBCS_STAGE_3_GRANULARITY; michael@0: } michael@0: } michael@0: michael@0: sum=stage3NullLength; michael@0: for(i=0; i<(stage3AllocLength/MBCS_UTF8_STAGE_3_BLOCK_SIZE); ++i) { michael@0: mbcsData->stageUTF8[i]=sum; michael@0: sum+=MBCS_UTF8_STAGE_3_BLOCK_SIZE; michael@0: } michael@0: michael@0: /* michael@0: * Allocate a 64-entry all-unassigned first stage 3 block, michael@0: * for UTF-8-friendly lookup with a trail byte, michael@0: * plus 128 entries for ASCII. michael@0: */ michael@0: mbcsData->stage3Top=(stage3NullLength+stage3AllocLength)*maxCharLength; /* ==sum*maxCharLength */ michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: /* return TRUE for success */ michael@0: static UBool michael@0: setFallback(MBCSData *mbcsData, uint32_t offset, UChar32 c) { michael@0: int32_t i=ucm_findFallback(mbcsData->toUFallbacks, mbcsData->countToUFallbacks, offset); michael@0: if(i>=0) { michael@0: /* if there is already a fallback for this offset, then overwrite it */ michael@0: mbcsData->toUFallbacks[i].codePoint=c; michael@0: return TRUE; michael@0: } else { michael@0: /* if there is no fallback for this offset, then add one */ michael@0: i=mbcsData->countToUFallbacks; michael@0: if(i>=MBCS_MAX_FALLBACK_COUNT) { michael@0: fprintf(stderr, "error: too many toUnicode fallbacks, currently at: U+%x\n", (int)c); michael@0: return FALSE; michael@0: } else { michael@0: mbcsData->toUFallbacks[i].offset=offset; michael@0: mbcsData->toUFallbacks[i].codePoint=c; michael@0: mbcsData->countToUFallbacks=i+1; michael@0: return TRUE; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* remove fallback if there is one with this offset; return the code point if there was such a fallback, otherwise -1 */ michael@0: static int32_t michael@0: removeFallback(MBCSData *mbcsData, uint32_t offset) { michael@0: int32_t i=ucm_findFallback(mbcsData->toUFallbacks, mbcsData->countToUFallbacks, offset); michael@0: if(i>=0) { michael@0: _MBCSToUFallback *toUFallbacks; michael@0: int32_t limit, old; michael@0: michael@0: toUFallbacks=mbcsData->toUFallbacks; michael@0: limit=mbcsData->countToUFallbacks; michael@0: old=(int32_t)toUFallbacks[i].codePoint; michael@0: michael@0: /* copy the last fallback entry here to keep the list contiguous */ michael@0: toUFallbacks[i].offset=toUFallbacks[limit-1].offset; michael@0: toUFallbacks[i].codePoint=toUFallbacks[limit-1].codePoint; michael@0: mbcsData->countToUFallbacks=limit-1; michael@0: return old; michael@0: } else { michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * isFallback is almost a boolean: michael@0: * 1 (TRUE) this is a fallback mapping michael@0: * 0 (FALSE) this is a precise mapping michael@0: * -1 the precision of this mapping is not specified michael@0: */ michael@0: static UBool michael@0: MBCSAddToUnicode(MBCSData *mbcsData, michael@0: const uint8_t *bytes, int32_t length, michael@0: UChar32 c, michael@0: int8_t flag) { michael@0: char buffer[10]; michael@0: uint32_t offset=0; michael@0: int32_t i=0, entry, old; michael@0: uint8_t state=0; michael@0: michael@0: if(mbcsData->ucm->states.countStates==0) { michael@0: fprintf(stderr, "error: there is no state information!\n"); michael@0: return FALSE; michael@0: } michael@0: michael@0: /* for SI/SO (like EBCDIC-stateful), double-byte sequences start in state 1 */ michael@0: if(length==2 && mbcsData->ucm->states.outputType==MBCS_OUTPUT_2_SISO) { michael@0: state=1; michael@0: } michael@0: michael@0: /* michael@0: * Walk down the state table like in conversion, michael@0: * much like getNextUChar(). michael@0: * We assume that c<=0x10ffff. michael@0: */ michael@0: for(i=0;;) { michael@0: entry=mbcsData->ucm->states.stateTable[state][bytes[i++]]; michael@0: if(MBCS_ENTRY_IS_TRANSITION(entry)) { michael@0: if(i==length) { michael@0: fprintf(stderr, "error: byte sequence too short, ends in non-final state %hu: 0x%s (U+%x)\n", michael@0: (short)state, printBytes(buffer, bytes, length), (int)c); michael@0: return FALSE; michael@0: } michael@0: state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry); michael@0: offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry); michael@0: } else { michael@0: if(i0x%s\n", michael@0: (int)c, printBytes(buffer, bytes, length)); michael@0: return FALSE; michael@0: case MBCS_STATE_CHANGE_ONLY: michael@0: fprintf(stderr, "error: byte sequence ends in state-change-only at U+%04x<->0x%s\n", michael@0: (int)c, printBytes(buffer, bytes, length)); michael@0: return FALSE; michael@0: case MBCS_STATE_UNASSIGNED: michael@0: fprintf(stderr, "error: byte sequence ends in unassigned state at U+%04x<->0x%s\n", michael@0: (int)c, printBytes(buffer, bytes, length)); michael@0: return FALSE; michael@0: case MBCS_STATE_FALLBACK_DIRECT_16: michael@0: case MBCS_STATE_VALID_DIRECT_16: michael@0: case MBCS_STATE_FALLBACK_DIRECT_20: michael@0: case MBCS_STATE_VALID_DIRECT_20: michael@0: if(MBCS_ENTRY_SET_STATE(entry, 0)!=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, 0xfffe)) { michael@0: /* the "direct" action's value is not "valid-direct-16-unassigned" any more */ michael@0: if(MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_DIRECT_16 || MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_FALLBACK_DIRECT_16) { michael@0: old=MBCS_ENTRY_FINAL_VALUE(entry); michael@0: } else { michael@0: old=0x10000+MBCS_ENTRY_FINAL_VALUE(entry); michael@0: } michael@0: if(flag>=0) { michael@0: fprintf(stderr, "error: duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n", michael@0: (int)c, printBytes(buffer, bytes, length), (int)old); michael@0: return FALSE; michael@0: } else if(VERBOSE) { michael@0: fprintf(stderr, "duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n", michael@0: (int)c, printBytes(buffer, bytes, length), (int)old); michael@0: } michael@0: /* michael@0: * Continue after the above warning michael@0: * if the precision of the mapping is unspecified. michael@0: */ michael@0: } michael@0: /* reassign the correct action code */ michael@0: entry=MBCS_ENTRY_FINAL_SET_ACTION(entry, (MBCS_STATE_VALID_DIRECT_16+(flag==3 ? 2 : 0)+(c>=0x10000 ? 1 : 0))); michael@0: michael@0: /* put the code point into bits 22..7 for BMP, c-0x10000 into 26..7 for others */ michael@0: if(c<=0xffff) { michael@0: entry=MBCS_ENTRY_FINAL_SET_VALUE(entry, c); michael@0: } else { michael@0: entry=MBCS_ENTRY_FINAL_SET_VALUE(entry, c-0x10000); michael@0: } michael@0: mbcsData->ucm->states.stateTable[state][bytes[i-1]]=entry; michael@0: break; michael@0: case MBCS_STATE_VALID_16: michael@0: /* bits 26..16 are not used, 0 */ michael@0: /* bits 15..7 contain the final offset delta to one 16-bit code unit */ michael@0: offset+=MBCS_ENTRY_FINAL_VALUE_16(entry); michael@0: /* check that this byte sequence is still unassigned */ michael@0: if((old=mbcsData->unicodeCodeUnits[offset])!=0xfffe || (old=removeFallback(mbcsData, offset))!=-1) { michael@0: if(flag>=0) { michael@0: fprintf(stderr, "error: duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n", michael@0: (int)c, printBytes(buffer, bytes, length), (int)old); michael@0: return FALSE; michael@0: } else if(VERBOSE) { michael@0: fprintf(stderr, "duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n", michael@0: (int)c, printBytes(buffer, bytes, length), (int)old); michael@0: } michael@0: } michael@0: if(c>=0x10000) { michael@0: fprintf(stderr, "error: code point does not fit into valid-16-bit state at U+%04x<->0x%s\n", michael@0: (int)c, printBytes(buffer, bytes, length)); michael@0: return FALSE; michael@0: } michael@0: if(flag>0) { michael@0: /* assign only if there is no precise mapping */ michael@0: if(mbcsData->unicodeCodeUnits[offset]==0xfffe) { michael@0: return setFallback(mbcsData, offset, c); michael@0: } michael@0: } else { michael@0: mbcsData->unicodeCodeUnits[offset]=(uint16_t)c; michael@0: } michael@0: break; michael@0: case MBCS_STATE_VALID_16_PAIR: michael@0: /* bits 26..16 are not used, 0 */ michael@0: /* bits 15..7 contain the final offset delta to two 16-bit code units */ michael@0: offset+=MBCS_ENTRY_FINAL_VALUE_16(entry); michael@0: /* check that this byte sequence is still unassigned */ michael@0: old=mbcsData->unicodeCodeUnits[offset]; michael@0: if(old<0xfffe) { michael@0: int32_t real; michael@0: if(old<0xd800) { michael@0: real=old; michael@0: } else if(old<=0xdfff) { michael@0: real=0x10000+((old&0x3ff)<<10)+((mbcsData->unicodeCodeUnits[offset+1])&0x3ff); michael@0: } else /* old<=0xe001 */ { michael@0: real=mbcsData->unicodeCodeUnits[offset+1]; michael@0: } michael@0: if(flag>=0) { michael@0: fprintf(stderr, "error: duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n", michael@0: (int)c, printBytes(buffer, bytes, length), (int)real); michael@0: return FALSE; michael@0: } else if(VERBOSE) { michael@0: fprintf(stderr, "duplicate codepage byte sequence at U+%04x<->0x%s see U+%04x\n", michael@0: (int)c, printBytes(buffer, bytes, length), (int)real); michael@0: } michael@0: } michael@0: if(flag>0) { michael@0: /* assign only if there is no precise mapping */ michael@0: if(old<=0xdbff || old==0xe000) { michael@0: /* do nothing */ michael@0: } else if(c<=0xffff) { michael@0: /* set a BMP fallback code point as a pair with 0xe001 */ michael@0: mbcsData->unicodeCodeUnits[offset++]=0xe001; michael@0: mbcsData->unicodeCodeUnits[offset]=(uint16_t)c; michael@0: } else { michael@0: /* set a fallback surrogate pair with two second surrogates */ michael@0: mbcsData->unicodeCodeUnits[offset++]=(uint16_t)(0xdbc0+(c>>10)); michael@0: mbcsData->unicodeCodeUnits[offset]=(uint16_t)(0xdc00+(c&0x3ff)); michael@0: } michael@0: } else { michael@0: if(c<0xd800) { michael@0: /* set a BMP code point */ michael@0: mbcsData->unicodeCodeUnits[offset]=(uint16_t)c; michael@0: } else if(c<=0xffff) { michael@0: /* set a BMP code point above 0xd800 as a pair with 0xe000 */ michael@0: mbcsData->unicodeCodeUnits[offset++]=0xe000; michael@0: mbcsData->unicodeCodeUnits[offset]=(uint16_t)c; michael@0: } else { michael@0: /* set a surrogate pair */ michael@0: mbcsData->unicodeCodeUnits[offset++]=(uint16_t)(0xd7c0+(c>>10)); michael@0: mbcsData->unicodeCodeUnits[offset]=(uint16_t)(0xdc00+(c&0x3ff)); michael@0: } michael@0: } michael@0: break; michael@0: default: michael@0: /* reserved, must never occur */ michael@0: fprintf(stderr, "internal error: byte sequence reached reserved action code, entry 0x%02x: 0x%s (U+%x)\n", michael@0: (int)entry, printBytes(buffer, bytes, length), (int)c); michael@0: return FALSE; michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* is this byte sequence valid? (this is almost the same as MBCSAddToUnicode()) */ michael@0: static UBool michael@0: MBCSIsValid(NewConverter *cnvData, michael@0: const uint8_t *bytes, int32_t length) { michael@0: MBCSData *mbcsData=(MBCSData *)cnvData; michael@0: michael@0: return (UBool)(1==ucm_countChars(&mbcsData->ucm->states, bytes, length)); michael@0: } michael@0: michael@0: static UBool michael@0: MBCSSingleAddFromUnicode(MBCSData *mbcsData, michael@0: const uint8_t *bytes, int32_t /*length*/, michael@0: UChar32 c, michael@0: int8_t flag) { michael@0: uint16_t *stage3, *p; michael@0: uint32_t idx; michael@0: uint16_t old; michael@0: uint8_t b; michael@0: michael@0: uint32_t blockSize, newTop, i, nextOffset, newBlock, min; michael@0: michael@0: /* ignore |2 SUB mappings */ michael@0: if(flag==2) { michael@0: return TRUE; michael@0: } michael@0: michael@0: /* michael@0: * Walk down the triple-stage compact array ("trie") and michael@0: * allocate parts as necessary. michael@0: * Note that the first stage 2 and 3 blocks are reserved for all-unassigned mappings. michael@0: * We assume that length<=maxCharLength and that c<=0x10ffff. michael@0: */ michael@0: stage3=(uint16_t *)mbcsData->fromUBytes; michael@0: b=*bytes; michael@0: michael@0: /* inspect stage 1 */ michael@0: idx=c>>MBCS_STAGE_1_SHIFT; michael@0: if(mbcsData->utf8Friendly && c<=SBCS_UTF8_MAX) { michael@0: nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK&~(MBCS_UTF8_STAGE_3_BLOCKS-1); michael@0: } else { michael@0: nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK; michael@0: } michael@0: if(mbcsData->stage1[idx]==MBCS_STAGE_2_ALL_UNASSIGNED_INDEX) { michael@0: /* allocate another block in stage 2 */ michael@0: newBlock=mbcsData->stage2Top; michael@0: if(mbcsData->utf8Friendly) { michael@0: min=newBlock-nextOffset; /* minimum block start with overlap */ michael@0: while(minstage2Single[newBlock-1]==0) { michael@0: --newBlock; michael@0: } michael@0: } michael@0: newTop=newBlock+MBCS_STAGE_2_BLOCK_SIZE; michael@0: michael@0: if(newTop>MBCS_MAX_STAGE_2_TOP) { michael@0: fprintf(stderr, "error: too many stage 2 entries at U+%04x<->0x%02x\n", (int)c, b); michael@0: return FALSE; michael@0: } michael@0: michael@0: /* michael@0: * each stage 2 block contains 64 16-bit words: michael@0: * 6 code point bits 9..4 with 1 stage 3 index michael@0: */ michael@0: mbcsData->stage1[idx]=(uint16_t)newBlock; michael@0: mbcsData->stage2Top=newTop; michael@0: } michael@0: michael@0: /* inspect stage 2 */ michael@0: idx=mbcsData->stage1[idx]+nextOffset; michael@0: if(mbcsData->utf8Friendly && c<=SBCS_UTF8_MAX) { michael@0: /* allocate 64-entry blocks for UTF-8-friendly lookup */ michael@0: blockSize=MBCS_UTF8_STAGE_3_BLOCK_SIZE; michael@0: nextOffset=c&MBCS_UTF8_STAGE_3_BLOCK_MASK; michael@0: } else { michael@0: blockSize=MBCS_STAGE_3_BLOCK_SIZE; michael@0: nextOffset=c&MBCS_STAGE_3_BLOCK_MASK; michael@0: } michael@0: if(mbcsData->stage2Single[idx]==0) { michael@0: /* allocate another block in stage 3 */ michael@0: newBlock=mbcsData->stage3Top; michael@0: if(mbcsData->utf8Friendly) { michael@0: min=newBlock-nextOffset; /* minimum block start with overlap */ michael@0: while(minMBCS_STAGE_3_SBCS_SIZE) { michael@0: fprintf(stderr, "error: too many code points at U+%04x<->0x%02x\n", (int)c, b); michael@0: return FALSE; michael@0: } michael@0: /* each block has 16 uint16_t entries */ michael@0: i=idx; michael@0: while(newBlockstage2Single[i++]=(uint16_t)newBlock; michael@0: newBlock+=MBCS_STAGE_3_BLOCK_SIZE; michael@0: } michael@0: mbcsData->stage3Top=newTop; /* ==newBlock */ michael@0: } michael@0: michael@0: /* write the codepage entry into stage 3 and get the previous entry */ michael@0: p=stage3+mbcsData->stage2Single[idx]+nextOffset; michael@0: old=*p; michael@0: if(flag<=0) { michael@0: *p=(uint16_t)(0xf00|b); michael@0: } else if(IS_PRIVATE_USE(c)) { michael@0: *p=(uint16_t)(0xc00|b); michael@0: } else { michael@0: *p=(uint16_t)(0x800|b); michael@0: } michael@0: michael@0: /* check that this Unicode code point was still unassigned */ michael@0: if(old>=0x100) { michael@0: if(flag>=0) { michael@0: fprintf(stderr, "error: duplicate Unicode code point at U+%04x<->0x%02x see 0x%02x\n", michael@0: (int)c, b, old&0xff); michael@0: return FALSE; michael@0: } else if(VERBOSE) { michael@0: fprintf(stderr, "duplicate Unicode code point at U+%04x<->0x%02x see 0x%02x\n", michael@0: (int)c, b, old&0xff); michael@0: } michael@0: /* continue after the above warning if the precision of the mapping is unspecified */ michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: static UBool michael@0: MBCSAddFromUnicode(MBCSData *mbcsData, michael@0: const uint8_t *bytes, int32_t length, michael@0: UChar32 c, michael@0: int8_t flag) { michael@0: char buffer[10]; michael@0: const uint8_t *pb; michael@0: uint8_t *stage3, *p; michael@0: uint32_t idx, b, old, stage3Index; michael@0: int32_t maxCharLength; michael@0: michael@0: uint32_t blockSize, newTop, i, nextOffset, newBlock, min, overlap, maxOverlap; michael@0: michael@0: maxCharLength=mbcsData->ucm->states.maxCharLength; michael@0: michael@0: if( mbcsData->ucm->states.outputType==MBCS_OUTPUT_2_SISO && michael@0: (!IGNORE_SISO_CHECK && (*bytes==0xe || *bytes==0xf)) michael@0: ) { michael@0: fprintf(stderr, "error: illegal mapping to SI or SO for SI/SO codepage: U+%04x<->0x%s\n", michael@0: (int)c, printBytes(buffer, bytes, length)); michael@0: return FALSE; michael@0: } michael@0: michael@0: if(flag==1 && length==1 && *bytes==0) { michael@0: fprintf(stderr, "error: unable to encode a |1 fallback from U+%04x to 0x%02x\n", michael@0: (int)c, *bytes); michael@0: return FALSE; michael@0: } michael@0: michael@0: /* michael@0: * Walk down the triple-stage compact array ("trie") and michael@0: * allocate parts as necessary. michael@0: * Note that the first stage 2 and 3 blocks are reserved for michael@0: * all-unassigned mappings. michael@0: * We assume that length<=maxCharLength and that c<=0x10ffff. michael@0: */ michael@0: stage3=mbcsData->fromUBytes; michael@0: michael@0: /* inspect stage 1 */ michael@0: idx=c>>MBCS_STAGE_1_SHIFT; michael@0: if(mbcsData->utf8Friendly && c<=mbcsData->utf8Max) { michael@0: nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK&~(MBCS_UTF8_STAGE_3_BLOCKS-1); michael@0: } else { michael@0: nextOffset=(c>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK; michael@0: } michael@0: if(mbcsData->stage1[idx]==MBCS_STAGE_2_ALL_UNASSIGNED_INDEX) { michael@0: /* allocate another block in stage 2 */ michael@0: newBlock=mbcsData->stage2Top; michael@0: if(mbcsData->utf8Friendly) { michael@0: min=newBlock-nextOffset; /* minimum block start with overlap */ michael@0: while(minstage2[newBlock-1]==0) { michael@0: --newBlock; michael@0: } michael@0: } michael@0: newTop=newBlock+MBCS_STAGE_2_BLOCK_SIZE; michael@0: michael@0: if(newTop>MBCS_MAX_STAGE_2_TOP) { michael@0: fprintf(stderr, "error: too many stage 2 entries at U+%04x<->0x%s\n", michael@0: (int)c, printBytes(buffer, bytes, length)); michael@0: return FALSE; michael@0: } michael@0: michael@0: /* michael@0: * each stage 2 block contains 64 32-bit words: michael@0: * 6 code point bits 9..4 with value with bits 31..16 "assigned" flags and bits 15..0 stage 3 index michael@0: */ michael@0: i=idx; michael@0: while(newBlockstage1[i++]=(uint16_t)newBlock; michael@0: newBlock+=MBCS_STAGE_2_BLOCK_SIZE; michael@0: } michael@0: mbcsData->stage2Top=newTop; /* ==newBlock */ michael@0: } michael@0: michael@0: /* inspect stage 2 */ michael@0: idx=mbcsData->stage1[idx]+nextOffset; michael@0: if(mbcsData->utf8Friendly && c<=mbcsData->utf8Max) { michael@0: /* allocate 64-entry blocks for UTF-8-friendly lookup */ michael@0: blockSize=MBCS_UTF8_STAGE_3_BLOCK_SIZE*maxCharLength; michael@0: nextOffset=c&MBCS_UTF8_STAGE_3_BLOCK_MASK; michael@0: } else { michael@0: blockSize=MBCS_STAGE_3_BLOCK_SIZE*maxCharLength; michael@0: nextOffset=c&MBCS_STAGE_3_BLOCK_MASK; michael@0: } michael@0: if(mbcsData->stage2[idx]==0) { michael@0: /* allocate another block in stage 3 */ michael@0: newBlock=mbcsData->stage3Top; michael@0: if(mbcsData->utf8Friendly && nextOffset>=MBCS_STAGE_3_GRANULARITY) { michael@0: /* michael@0: * Overlap stage 3 blocks only in multiples of 16-entry blocks michael@0: * because of the indexing granularity in stage 2. michael@0: */ michael@0: maxOverlap=(nextOffset&~(MBCS_STAGE_3_GRANULARITY-1))*maxCharLength; michael@0: for(overlap=0; michael@0: overlapMBCS_STAGE_3_MBCS_SIZE*(uint32_t)maxCharLength) { michael@0: fprintf(stderr, "error: too many code points at U+%04x<->0x%s\n", michael@0: (int)c, printBytes(buffer, bytes, length)); michael@0: return FALSE; michael@0: } michael@0: /* each block has 16*maxCharLength bytes */ michael@0: i=idx; michael@0: while(newBlockstage2[i++]=(newBlock/MBCS_STAGE_3_GRANULARITY)/maxCharLength; michael@0: newBlock+=MBCS_STAGE_3_BLOCK_SIZE*maxCharLength; michael@0: } michael@0: mbcsData->stage3Top=newTop; /* ==newBlock */ michael@0: } michael@0: michael@0: stage3Index=MBCS_STAGE_3_GRANULARITY*(uint32_t)(uint16_t)mbcsData->stage2[idx]; michael@0: michael@0: /* Build an alternate, UTF-8-friendly stage table as well. */ michael@0: if(mbcsData->utf8Friendly && c<=mbcsData->utf8Max) { michael@0: /* Overflow for uint16_t entries in stageUTF8? */ michael@0: if(stage3Index>0xffff) { michael@0: /* michael@0: * This can occur only if the mapping table is nearly perfectly filled and if michael@0: * utf8Max==0xffff. michael@0: * (There is no known charset like this. GB 18030 does not map michael@0: * surrogate code points and LMBCS does not map 256 PUA code points.) michael@0: * michael@0: * Otherwise, stage3Index<=MBCS_UTF8_LIMIT<0xffff michael@0: * (stage3Index can at most reach exactly MBCS_UTF8_LIMIT) michael@0: * because we have a sorted table and there are at most MBCS_UTF8_LIMIT michael@0: * mappings with 0<=cutf8Max=0xfeff; michael@0: } else { michael@0: /* michael@0: * The stage 3 block has been assigned for the regular trie. michael@0: * Just copy its index into stageUTF8[], without the granularity. michael@0: */ michael@0: mbcsData->stageUTF8[c>>MBCS_UTF8_STAGE_SHIFT]=(uint16_t)stage3Index; michael@0: } michael@0: } michael@0: michael@0: /* write the codepage bytes into stage 3 and get the previous bytes */ michael@0: michael@0: /* assemble the bytes into a single integer */ michael@0: pb=bytes; michael@0: b=0; michael@0: switch(length) { michael@0: case 4: michael@0: b=*pb++; michael@0: case 3: michael@0: b=(b<<8)|*pb++; michael@0: case 2: michael@0: b=(b<<8)|*pb++; michael@0: case 1: michael@0: default: michael@0: b=(b<<8)|*pb++; michael@0: break; michael@0: } michael@0: michael@0: old=0; michael@0: p=stage3+(stage3Index+nextOffset)*maxCharLength; michael@0: switch(maxCharLength) { michael@0: case 2: michael@0: old=*(uint16_t *)p; michael@0: *(uint16_t *)p=(uint16_t)b; michael@0: break; michael@0: case 3: michael@0: old=(uint32_t)*p<<16; michael@0: *p++=(uint8_t)(b>>16); michael@0: old|=(uint32_t)*p<<8; michael@0: *p++=(uint8_t)(b>>8); michael@0: old|=*p; michael@0: *p=(uint8_t)b; michael@0: break; michael@0: case 4: michael@0: old=*(uint32_t *)p; michael@0: *(uint32_t *)p=b; michael@0: break; michael@0: default: michael@0: /* will never occur */ michael@0: break; michael@0: } michael@0: michael@0: /* check that this Unicode code point was still unassigned */ michael@0: if((mbcsData->stage2[idx+(nextOffset>>MBCS_STAGE_2_SHIFT)]&(1UL<<(16+(c&0xf))))!=0 || old!=0) { michael@0: if(flag>=0) { michael@0: fprintf(stderr, "error: duplicate Unicode code point at U+%04x<->0x%s see 0x%02x\n", michael@0: (int)c, printBytes(buffer, bytes, length), (int)old); michael@0: return FALSE; michael@0: } else if(VERBOSE) { michael@0: fprintf(stderr, "duplicate Unicode code point at U+%04x<->0x%s see 0x%02x\n", michael@0: (int)c, printBytes(buffer, bytes, length), (int)old); michael@0: } michael@0: /* continue after the above warning if the precision of the mapping is michael@0: unspecified */ michael@0: } michael@0: if(flag<=0) { michael@0: /* set the roundtrip flag */ michael@0: mbcsData->stage2[idx+(nextOffset>>4)]|=(1UL<<(16+(c&0xf))); michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: U_CFUNC UBool michael@0: MBCSOkForBaseFromUnicode(const MBCSData *mbcsData, michael@0: const uint8_t *bytes, int32_t length, michael@0: UChar32 c, int8_t flag) { michael@0: /* michael@0: * A 1:1 mapping does not fit into the MBCS base table's fromUnicode table under michael@0: * the following conditions: michael@0: * michael@0: * - a |2 SUB mapping for (no base table data structure for them) michael@0: * - a |1 fallback to 0x00 (result value 0, indistinguishable from unmappable entry) michael@0: * - a multi-byte mapping with leading 0x00 bytes (no explicit length field) michael@0: * michael@0: * Some of these tests are redundant with ucm_mappingType(). michael@0: */ michael@0: if( (flag==2 && length==1) || michael@0: (flag==1 && bytes[0]==0) || /* testing length==1 would be redundant with the next test */ michael@0: (flag<=1 && length>1 && bytes[0]==0) michael@0: ) { michael@0: return FALSE; michael@0: } michael@0: michael@0: /* michael@0: * Additional restrictions for UTF-8-friendly fromUnicode tables, michael@0: * for code points up to the maximum optimized one: michael@0: * michael@0: * - any mapping to 0x00 (result value 0, indistinguishable from unmappable entry) michael@0: * - any |1 fallback (no roundtrip flags in the optimized table) michael@0: */ michael@0: if(mbcsData->utf8Friendly && flag<=1 && c<=mbcsData->utf8Max && (bytes[0]==0 || flag==1)) { michael@0: return FALSE; michael@0: } michael@0: michael@0: /* michael@0: * If we omit the fromUnicode data, we can only store roundtrips there michael@0: * because only they are recoverable from the toUnicode data. michael@0: * Fallbacks must go into the extension table. michael@0: */ michael@0: if(mbcsData->omitFromU && flag!=0) { michael@0: return FALSE; michael@0: } michael@0: michael@0: /* All other mappings do fit into the base table. */ michael@0: return TRUE; michael@0: } michael@0: michael@0: /* we can assume that the table only contains 1:1 mappings with <=4 bytes each */ michael@0: static UBool michael@0: MBCSAddTable(NewConverter *cnvData, UCMTable *table, UConverterStaticData *staticData) { michael@0: MBCSData *mbcsData; michael@0: UCMapping *m; michael@0: UChar32 c; michael@0: int32_t i, maxCharLength; michael@0: int8_t f; michael@0: UBool isOK, utf8Friendly; michael@0: michael@0: staticData->unicodeMask=table->unicodeMask; michael@0: if(staticData->unicodeMask==3) { michael@0: fprintf(stderr, "error: contains mappings for both supplementary and surrogate code points\n"); michael@0: return FALSE; michael@0: } michael@0: michael@0: staticData->conversionType=UCNV_MBCS; michael@0: michael@0: mbcsData=(MBCSData *)cnvData; michael@0: maxCharLength=mbcsData->ucm->states.maxCharLength; michael@0: michael@0: /* michael@0: * Generation of UTF-8-friendly data requires michael@0: * a sorted table, which makeconv generates when explicit precision michael@0: * indicators are used. michael@0: */ michael@0: mbcsData->utf8Friendly=utf8Friendly=(UBool)((table->flagsType&UCM_FLAGS_EXPLICIT)!=0); michael@0: if(utf8Friendly) { michael@0: mbcsData->utf8Max=MBCS_UTF8_MAX; michael@0: if(SMALL && maxCharLength>1) { michael@0: mbcsData->omitFromU=TRUE; michael@0: } michael@0: } else { michael@0: mbcsData->utf8Max=0; michael@0: if(SMALL && maxCharLength>1) { michael@0: fprintf(stderr, michael@0: "makeconv warning: --small not available for .ucm files without |0 etc.\n"); michael@0: } michael@0: } michael@0: michael@0: if(!MBCSStartMappings(mbcsData)) { michael@0: return FALSE; michael@0: } michael@0: michael@0: staticData->hasFromUnicodeFallback=FALSE; michael@0: staticData->hasToUnicodeFallback=FALSE; michael@0: michael@0: isOK=TRUE; michael@0: michael@0: m=table->mappings; michael@0: for(i=0; imappingsLength; ++m, ++i) { michael@0: c=m->u; michael@0: f=m->f; michael@0: michael@0: /* michael@0: * Small optimization for --small .cnv files: michael@0: * michael@0: * If there are fromUnicode mappings above MBCS_UTF8_MAX, michael@0: * then the file size will be smaller if we make utf8Max larger michael@0: * because the size increase in stageUTF8 will be more than balanced by michael@0: * how much less of stage2 needs to be stored. michael@0: * michael@0: * There is no point in doing this incrementally because stageUTF8 michael@0: * uses so much less space per block than stage2, michael@0: * so we immediately increase utf8Max to 0xffff. michael@0: * michael@0: * Do not increase utf8Max if it is already at 0xfeff because MBCSAddFromUnicode() michael@0: * sets it to that value when stageUTF8 overflows. michael@0: */ michael@0: if( mbcsData->omitFromU && f<=1 && michael@0: mbcsData->utf8Maxutf8Max<0xfeff michael@0: ) { michael@0: mbcsData->utf8Max=0xffff; michael@0: } michael@0: michael@0: switch(f) { michael@0: case -1: michael@0: /* there was no precision/fallback indicator */ michael@0: /* fall through to set the mappings */ michael@0: case 0: michael@0: /* set roundtrip mappings */ michael@0: isOK&=MBCSAddToUnicode(mbcsData, m->b.bytes, m->bLen, c, f); michael@0: michael@0: if(maxCharLength==1) { michael@0: isOK&=MBCSSingleAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f); michael@0: } else if(MBCSOkForBaseFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f)) { michael@0: isOK&=MBCSAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f); michael@0: } else { michael@0: m->f|=MBCS_FROM_U_EXT_FLAG; michael@0: m->moveFlag=UCM_MOVE_TO_EXT; michael@0: } michael@0: break; michael@0: case 1: michael@0: /* set only a fallback mapping from Unicode to codepage */ michael@0: if(maxCharLength==1) { michael@0: staticData->hasFromUnicodeFallback=TRUE; michael@0: isOK&=MBCSSingleAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f); michael@0: } else if(MBCSOkForBaseFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f)) { michael@0: staticData->hasFromUnicodeFallback=TRUE; michael@0: isOK&=MBCSAddFromUnicode(mbcsData, m->b.bytes, m->bLen, c, f); michael@0: } else { michael@0: m->f|=MBCS_FROM_U_EXT_FLAG; michael@0: m->moveFlag=UCM_MOVE_TO_EXT; michael@0: } michael@0: break; michael@0: case 2: michael@0: /* ignore |2 SUB mappings, except to move mappings to the extension table */ michael@0: if(maxCharLength>1 && m->bLen==1) { michael@0: m->f|=MBCS_FROM_U_EXT_FLAG; michael@0: m->moveFlag=UCM_MOVE_TO_EXT; michael@0: } michael@0: break; michael@0: case 3: michael@0: /* set only a fallback mapping from codepage to Unicode */ michael@0: staticData->hasToUnicodeFallback=TRUE; michael@0: isOK&=MBCSAddToUnicode(mbcsData, m->b.bytes, m->bLen, c, f); michael@0: break; michael@0: case 4: michael@0: /* move "good one-way" mappings to the extension table */ michael@0: m->f|=MBCS_FROM_U_EXT_FLAG; michael@0: m->moveFlag=UCM_MOVE_TO_EXT; michael@0: break; michael@0: default: michael@0: /* will not occur because the parser checked it already */ michael@0: fprintf(stderr, "error: illegal fallback indicator %d\n", f); michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: MBCSPostprocess(mbcsData, staticData); michael@0: michael@0: return isOK; michael@0: } michael@0: michael@0: static UBool michael@0: transformEUC(MBCSData *mbcsData) { michael@0: uint8_t *p8; michael@0: uint32_t i, value, oldLength, old3Top; michael@0: uint8_t b; michael@0: michael@0: oldLength=mbcsData->ucm->states.maxCharLength; michael@0: if(oldLength<3) { michael@0: return FALSE; michael@0: } michael@0: michael@0: old3Top=mbcsData->stage3Top; michael@0: michael@0: /* careful: 2-byte and 4-byte codes are stored in platform endianness! */ michael@0: michael@0: /* test if all first bytes are in {0, 0x8e, 0x8f} */ michael@0: p8=mbcsData->fromUBytes; michael@0: michael@0: #if !U_IS_BIG_ENDIAN michael@0: if(oldLength==4) { michael@0: p8+=3; michael@0: } michael@0: #endif michael@0: michael@0: for(i=0; ifromUBytes; michael@0: michael@0: /* modify outputType and adjust stage3Top */ michael@0: mbcsData->ucm->states.outputType=(int8_t)(MBCS_OUTPUT_3_EUC+oldLength-3); michael@0: mbcsData->stage3Top=(old3Top*(oldLength-1))/oldLength; michael@0: michael@0: /* michael@0: * EUC-encode all byte sequences; michael@0: * see "CJKV Information Processing" (1st ed. 1999) from Ken Lunde, O'Reilly, michael@0: * p. 161 in chapter 4 "Encoding Methods" michael@0: * michael@0: * This also must reverse the byte order if the platform is little-endian! michael@0: */ michael@0: if(oldLength==3) { michael@0: uint16_t *q=(uint16_t *)p8; michael@0: for(i=0; i>16); michael@0: (*q++)=(uint8_t)(value>>8); michael@0: (*q++)=(uint8_t)value; michael@0: } else if(value<=0x8effffff) { michael@0: /* code set 2 */ michael@0: (*q++)=(uint8_t)((value>>16)&0x7f); michael@0: (*q++)=(uint8_t)(value>>8); michael@0: (*q++)=(uint8_t)value; michael@0: } else /* first byte is 0x8f */ { michael@0: /* code set 3 */ michael@0: (*q++)=(uint8_t)(value>>16); michael@0: (*q++)=(uint8_t)((value>>8)&0x7f); michael@0: (*q++)=(uint8_t)value; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: /* michael@0: * Compact stage 2 for SBCS by overlapping adjacent stage 2 blocks as far michael@0: * as possible. Overlapping is done on unassigned head and tail michael@0: * parts of blocks in steps of MBCS_STAGE_2_MULTIPLIER. michael@0: * Stage 1 indexes need to be adjusted accordingly. michael@0: * This function is very similar to genprops/store.c/compactStage(). michael@0: */ michael@0: static void michael@0: singleCompactStage2(MBCSData *mbcsData) { michael@0: /* this array maps the ordinal number of a stage 2 block to its new stage 1 index */ michael@0: uint16_t map[MBCS_STAGE_2_MAX_BLOCKS]; michael@0: uint16_t i, start, prevEnd, newStart; michael@0: michael@0: /* enter the all-unassigned first stage 2 block into the map */ michael@0: map[0]=MBCS_STAGE_2_ALL_UNASSIGNED_INDEX; michael@0: michael@0: /* begin with the first block after the all-unassigned one */ michael@0: start=newStart=MBCS_STAGE_2_FIRST_ASSIGNED; michael@0: while(startstage2Top) { michael@0: prevEnd=(uint16_t)(newStart-1); michael@0: michael@0: /* find the size of the overlap */ michael@0: for(i=0; istage2Single[start+i]==0 && mbcsData->stage2Single[prevEnd-i]==0; ++i) {} michael@0: michael@0: if(i>0) { michael@0: map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=(uint16_t)(newStart-i); michael@0: michael@0: /* move the non-overlapping indexes to their new positions */ michael@0: start+=i; michael@0: for(i=(uint16_t)(MBCS_STAGE_2_BLOCK_SIZE-i); i>0; --i) { michael@0: mbcsData->stage2Single[newStart++]=mbcsData->stage2Single[start++]; michael@0: } michael@0: } else if(newStart>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=newStart; michael@0: for(i=MBCS_STAGE_2_BLOCK_SIZE; i>0; --i) { michael@0: mbcsData->stage2Single[newStart++]=mbcsData->stage2Single[start++]; michael@0: } michael@0: } else /* no overlap && newStart==start */ { michael@0: map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=start; michael@0: start=newStart+=MBCS_STAGE_2_BLOCK_SIZE; michael@0: } michael@0: } michael@0: michael@0: /* adjust stage2Top */ michael@0: if(VERBOSE && newStartstage2Top) { michael@0: printf("compacting stage 2 from stage2Top=0x%lx to 0x%lx, saving %ld bytes\n", michael@0: (unsigned long)mbcsData->stage2Top, (unsigned long)newStart, michael@0: (long)(mbcsData->stage2Top-newStart)*2); michael@0: } michael@0: mbcsData->stage2Top=newStart; michael@0: michael@0: /* now adjust stage 1 */ michael@0: for(i=0; istage1[i]=map[mbcsData->stage1[i]>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]; michael@0: } michael@0: } michael@0: michael@0: /* Compact stage 3 for SBCS - same algorithm as above. */ michael@0: static void michael@0: singleCompactStage3(MBCSData *mbcsData) { michael@0: uint16_t *stage3=(uint16_t *)mbcsData->fromUBytes; michael@0: michael@0: /* this array maps the ordinal number of a stage 3 block to its new stage 2 index */ michael@0: uint16_t map[0x1000]; michael@0: uint16_t i, start, prevEnd, newStart; michael@0: michael@0: /* enter the all-unassigned first stage 3 block into the map */ michael@0: map[0]=0; michael@0: michael@0: /* begin with the first block after the all-unassigned one */ michael@0: start=newStart=16; michael@0: while(startstage3Top) { michael@0: prevEnd=(uint16_t)(newStart-1); michael@0: michael@0: /* find the size of the overlap */ michael@0: for(i=0; i<16 && stage3[start+i]==0 && stage3[prevEnd-i]==0; ++i) {} michael@0: michael@0: if(i>0) { michael@0: map[start>>4]=(uint16_t)(newStart-i); michael@0: michael@0: /* move the non-overlapping indexes to their new positions */ michael@0: start+=i; michael@0: for(i=(uint16_t)(16-i); i>0; --i) { michael@0: stage3[newStart++]=stage3[start++]; michael@0: } michael@0: } else if(newStart>4]=newStart; michael@0: for(i=16; i>0; --i) { michael@0: stage3[newStart++]=stage3[start++]; michael@0: } michael@0: } else /* no overlap && newStart==start */ { michael@0: map[start>>4]=start; michael@0: start=newStart+=16; michael@0: } michael@0: } michael@0: michael@0: /* adjust stage3Top */ michael@0: if(VERBOSE && newStartstage3Top) { michael@0: printf("compacting stage 3 from stage3Top=0x%lx to 0x%lx, saving %ld bytes\n", michael@0: (unsigned long)mbcsData->stage3Top, (unsigned long)newStart, michael@0: (long)(mbcsData->stage3Top-newStart)*2); michael@0: } michael@0: mbcsData->stage3Top=newStart; michael@0: michael@0: /* now adjust stage 2 */ michael@0: for(i=0; istage2Top; ++i) { michael@0: mbcsData->stage2Single[i]=map[mbcsData->stage2Single[i]>>4]; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Compact stage 2 by overlapping adjacent stage 2 blocks as far michael@0: * as possible. Overlapping is done on unassigned head and tail michael@0: * parts of blocks in steps of MBCS_STAGE_2_MULTIPLIER. michael@0: * Stage 1 indexes need to be adjusted accordingly. michael@0: * This function is very similar to genprops/store.c/compactStage(). michael@0: */ michael@0: static void michael@0: compactStage2(MBCSData *mbcsData) { michael@0: /* this array maps the ordinal number of a stage 2 block to its new stage 1 index */ michael@0: uint16_t map[MBCS_STAGE_2_MAX_BLOCKS]; michael@0: uint16_t i, start, prevEnd, newStart; michael@0: michael@0: /* enter the all-unassigned first stage 2 block into the map */ michael@0: map[0]=MBCS_STAGE_2_ALL_UNASSIGNED_INDEX; michael@0: michael@0: /* begin with the first block after the all-unassigned one */ michael@0: start=newStart=MBCS_STAGE_2_FIRST_ASSIGNED; michael@0: while(startstage2Top) { michael@0: prevEnd=(uint16_t)(newStart-1); michael@0: michael@0: /* find the size of the overlap */ michael@0: for(i=0; istage2[start+i]==0 && mbcsData->stage2[prevEnd-i]==0; ++i) {} michael@0: michael@0: if(i>0) { michael@0: map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=(uint16_t)(newStart-i); michael@0: michael@0: /* move the non-overlapping indexes to their new positions */ michael@0: start+=i; michael@0: for(i=(uint16_t)(MBCS_STAGE_2_BLOCK_SIZE-i); i>0; --i) { michael@0: mbcsData->stage2[newStart++]=mbcsData->stage2[start++]; michael@0: } michael@0: } else if(newStart>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=newStart; michael@0: for(i=MBCS_STAGE_2_BLOCK_SIZE; i>0; --i) { michael@0: mbcsData->stage2[newStart++]=mbcsData->stage2[start++]; michael@0: } michael@0: } else /* no overlap && newStart==start */ { michael@0: map[start>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]=start; michael@0: start=newStart+=MBCS_STAGE_2_BLOCK_SIZE; michael@0: } michael@0: } michael@0: michael@0: /* adjust stage2Top */ michael@0: if(VERBOSE && newStartstage2Top) { michael@0: printf("compacting stage 2 from stage2Top=0x%lx to 0x%lx, saving %ld bytes\n", michael@0: (unsigned long)mbcsData->stage2Top, (unsigned long)newStart, michael@0: (long)(mbcsData->stage2Top-newStart)*4); michael@0: } michael@0: mbcsData->stage2Top=newStart; michael@0: michael@0: /* now adjust stage 1 */ michael@0: for(i=0; istage1[i]=map[mbcsData->stage1[i]>>MBCS_STAGE_2_BLOCK_SIZE_SHIFT]; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: MBCSPostprocess(MBCSData *mbcsData, const UConverterStaticData * /*staticData*/) { michael@0: UCMStates *states; michael@0: int32_t maxCharLength, stage3Width; michael@0: michael@0: states=&mbcsData->ucm->states; michael@0: stage3Width=maxCharLength=states->maxCharLength; michael@0: michael@0: ucm_optimizeStates(states, michael@0: &mbcsData->unicodeCodeUnits, michael@0: mbcsData->toUFallbacks, mbcsData->countToUFallbacks, michael@0: VERBOSE); michael@0: michael@0: /* try to compact the fromUnicode tables */ michael@0: if(transformEUC(mbcsData)) { michael@0: --stage3Width; michael@0: } michael@0: michael@0: /* michael@0: * UTF-8-friendly tries are built precompacted, to cope with variable michael@0: * stage 3 allocation block sizes. michael@0: * michael@0: * Tables without precision indicators cannot be built that way, michael@0: * because if a block was overlapped with a previous one, then a smaller michael@0: * code point for the same block would not fit. michael@0: * Therefore, such tables are not marked UTF-8-friendly and must be michael@0: * compacted after all mappings are entered. michael@0: */ michael@0: if(!mbcsData->utf8Friendly) { michael@0: if(maxCharLength==1) { michael@0: singleCompactStage3(mbcsData); michael@0: singleCompactStage2(mbcsData); michael@0: } else { michael@0: compactStage2(mbcsData); michael@0: } michael@0: } michael@0: michael@0: if(VERBOSE) { michael@0: /*uint32_t c, i1, i2, i2Limit, i3;*/ michael@0: michael@0: printf("fromUnicode number of uint%s_t in stage 2: 0x%lx=%lu\n", michael@0: maxCharLength==1 ? "16" : "32", michael@0: (unsigned long)mbcsData->stage2Top, michael@0: (unsigned long)mbcsData->stage2Top); michael@0: printf("fromUnicode number of %d-byte stage 3 mapping entries: 0x%lx=%lu\n", michael@0: (int)stage3Width, michael@0: (unsigned long)mbcsData->stage3Top/stage3Width, michael@0: (unsigned long)mbcsData->stage3Top/stage3Width); michael@0: #if 0 michael@0: c=0; michael@0: for(i1=0; i1stage1[i1]; michael@0: if(i2==0) { michael@0: c+=MBCS_STAGE_2_BLOCK_SIZE*MBCS_STAGE_3_BLOCK_SIZE; michael@0: continue; michael@0: } michael@0: for(i2Limit=i2+MBCS_STAGE_2_BLOCK_SIZE; i2stage2Single[i2]; michael@0: } else { michael@0: i3=(uint16_t)mbcsData->stage2[i2]; michael@0: } michael@0: if(i3==0) { michael@0: c+=MBCS_STAGE_3_BLOCK_SIZE; michael@0: continue; michael@0: } michael@0: printf("U+%04lx i1=0x%02lx i2=0x%04lx i3=0x%04lx\n", michael@0: (unsigned long)c, michael@0: (unsigned long)i1, michael@0: (unsigned long)i2, michael@0: (unsigned long)i3); michael@0: c+=MBCS_STAGE_3_BLOCK_SIZE; michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: static uint32_t michael@0: MBCSWrite(NewConverter *cnvData, const UConverterStaticData *staticData, michael@0: UNewDataMemory *pData, int32_t tableType) { michael@0: MBCSData *mbcsData=(MBCSData *)cnvData; michael@0: uint32_t stage2Start, stage2Length; michael@0: uint32_t top, stageUTF8Length=0; michael@0: int32_t i, stage1Top; michael@0: uint32_t headerLength; michael@0: michael@0: _MBCSHeader header=UCNV_MBCS_HEADER_INITIALIZER; michael@0: michael@0: stage2Length=mbcsData->stage2Top; michael@0: if(mbcsData->omitFromU) { michael@0: /* find how much of stage2 can be omitted */ michael@0: int32_t utf8Limit=(int32_t)mbcsData->utf8Max+1; michael@0: uint32_t st2=0; /*initialized it to avoid compiler warnings */ michael@0: michael@0: i=utf8Limit>>MBCS_STAGE_1_SHIFT; michael@0: if((utf8Limit&((1<stage1[i])!=0) { michael@0: /* utf8Limit is in the middle of an existing stage 2 block */ michael@0: stage2Start=st2+((utf8Limit>>MBCS_STAGE_2_SHIFT)&MBCS_STAGE_2_BLOCK_MASK); michael@0: } else { michael@0: /* find the last stage2 block with mappings before utf8Limit */ michael@0: while(i>0 && (st2=mbcsData->stage1[--i])==0) {} michael@0: /* stage2 up to the end of this block corresponds to stageUTF8 */ michael@0: stage2Start=st2+MBCS_STAGE_2_BLOCK_SIZE; michael@0: } michael@0: header.options|=MBCS_OPT_NO_FROM_U; michael@0: header.fullStage2Length=stage2Length; michael@0: stage2Length-=stage2Start; michael@0: if(VERBOSE) { michael@0: printf("+ omitting %lu out of %lu stage2 entries and %lu fromUBytes\n", michael@0: (unsigned long)stage2Start, michael@0: (unsigned long)mbcsData->stage2Top, michael@0: (unsigned long)mbcsData->stage3Top); michael@0: printf("+ total size savings: %lu bytes\n", (unsigned long)stage2Start*4+mbcsData->stage3Top); michael@0: } michael@0: } else { michael@0: stage2Start=0; michael@0: } michael@0: michael@0: if(staticData->unicodeMask&UCNV_HAS_SUPPLEMENTARY) { michael@0: stage1Top=MBCS_STAGE_1_SIZE; /* 0x440==1088 */ michael@0: } else { michael@0: stage1Top=0x40; /* 0x40==64 */ michael@0: } michael@0: michael@0: /* adjust stage 1 entries to include the size of stage 1 in the offsets to stage 2 */ michael@0: if(mbcsData->ucm->states.maxCharLength==1) { michael@0: for(i=0; istage1[i]+=(uint16_t)stage1Top; michael@0: } michael@0: michael@0: /* stage2Top/Length have counted 16-bit results, now we need to count bytes */ michael@0: /* also round up to a multiple of 4 bytes */ michael@0: stage2Length=(stage2Length*2+1)&~1; michael@0: michael@0: /* stage3Top has counted 16-bit results, now we need to count bytes */ michael@0: mbcsData->stage3Top*=2; michael@0: michael@0: if(mbcsData->utf8Friendly) { michael@0: header.version[2]=(uint8_t)(SBCS_UTF8_MAX>>8); /* store 0x1f for max==0x1fff */ michael@0: } michael@0: } else { michael@0: for(i=0; istage1[i]+=(uint16_t)stage1Top/2; /* stage 2 contains 32-bit entries, stage 1 16-bit entries */ michael@0: } michael@0: michael@0: /* stage2Top/Length have counted 32-bit results, now we need to count bytes */ michael@0: stage2Length*=4; michael@0: /* leave stage2Start counting 32-bit units */ michael@0: michael@0: if(mbcsData->utf8Friendly) { michael@0: stageUTF8Length=(mbcsData->utf8Max+1)>>MBCS_UTF8_STAGE_SHIFT; michael@0: header.version[2]=(uint8_t)(mbcsData->utf8Max>>8); /* store 0xd7 for max==0xd7ff */ michael@0: } michael@0: michael@0: /* stage3Top has already counted bytes */ michael@0: } michael@0: michael@0: /* round up stage3Top so that the sizes of all data blocks are multiples of 4 */ michael@0: mbcsData->stage3Top=(mbcsData->stage3Top+3)&~3; michael@0: michael@0: /* fill the header */ michael@0: if(header.options&MBCS_OPT_INCOMPATIBLE_MASK) { michael@0: header.version[0]=5; michael@0: if(header.options&MBCS_OPT_NO_FROM_U) { michael@0: headerLength=10; /* include fullStage2Length */ michael@0: } else { michael@0: headerLength=MBCS_HEADER_V5_MIN_LENGTH; /* 9 */ michael@0: } michael@0: } else { michael@0: header.version[0]=4; michael@0: headerLength=MBCS_HEADER_V4_LENGTH; /* 8 */ michael@0: } michael@0: header.version[1]=4; michael@0: /* header.version[2] set above for utf8Friendly data */ michael@0: michael@0: header.options|=(uint32_t)headerLength; michael@0: michael@0: header.countStates=mbcsData->ucm->states.countStates; michael@0: header.countToUFallbacks=mbcsData->countToUFallbacks; michael@0: michael@0: header.offsetToUCodeUnits= michael@0: headerLength*4+ michael@0: mbcsData->ucm->states.countStates*1024+ michael@0: mbcsData->countToUFallbacks*sizeof(_MBCSToUFallback); michael@0: header.offsetFromUTable= michael@0: header.offsetToUCodeUnits+ michael@0: mbcsData->ucm->states.countToUCodeUnits*2; michael@0: header.offsetFromUBytes= michael@0: header.offsetFromUTable+ michael@0: stage1Top*2+ michael@0: stage2Length; michael@0: header.fromUBytesLength=mbcsData->stage3Top; michael@0: michael@0: top=header.offsetFromUBytes+stageUTF8Length*2; michael@0: if(!(header.options&MBCS_OPT_NO_FROM_U)) { michael@0: top+=header.fromUBytesLength; michael@0: } michael@0: michael@0: header.flags=(uint8_t)(mbcsData->ucm->states.outputType); michael@0: michael@0: if(tableType&TABLE_EXT) { michael@0: if(top>0xffffff) { michael@0: fprintf(stderr, "error: offset 0x%lx to extension table exceeds 0xffffff\n", (long)top); michael@0: return 0; michael@0: } michael@0: michael@0: header.flags|=top<<8; michael@0: } michael@0: michael@0: /* write the MBCS data */ michael@0: udata_writeBlock(pData, &header, headerLength*4); michael@0: udata_writeBlock(pData, mbcsData->ucm->states.stateTable, header.countStates*1024); michael@0: udata_writeBlock(pData, mbcsData->toUFallbacks, mbcsData->countToUFallbacks*sizeof(_MBCSToUFallback)); michael@0: udata_writeBlock(pData, mbcsData->unicodeCodeUnits, mbcsData->ucm->states.countToUCodeUnits*2); michael@0: udata_writeBlock(pData, mbcsData->stage1, stage1Top*2); michael@0: if(mbcsData->ucm->states.maxCharLength==1) { michael@0: udata_writeBlock(pData, mbcsData->stage2Single+stage2Start, stage2Length); michael@0: } else { michael@0: udata_writeBlock(pData, mbcsData->stage2+stage2Start, stage2Length); michael@0: } michael@0: if(!(header.options&MBCS_OPT_NO_FROM_U)) { michael@0: udata_writeBlock(pData, mbcsData->fromUBytes, mbcsData->stage3Top); michael@0: } michael@0: michael@0: if(stageUTF8Length>0) { michael@0: udata_writeBlock(pData, mbcsData->stageUTF8, stageUTF8Length*2); michael@0: } michael@0: michael@0: /* return the number of bytes that should have been written */ michael@0: return top; michael@0: }