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