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