2 *******************************************************************************
4 * Copyright (C) 2003-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: gencnvex.c
10 * tab size: 8 (not used)
13 * created on: 2003oct12
14 * created by: Markus W. Scherer
18 #include "unicode/utypes.h"
19 #include "unicode/ustring.h"
30 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
33 CnvExtClose(NewConverter
*cnvData
);
36 CnvExtIsValid(NewConverter
*cnvData
,
37 const uint8_t *bytes
, int32_t length
);
40 CnvExtAddTable(NewConverter
*cnvData
, UCMTable
*table
, UConverterStaticData
*staticData
);
43 CnvExtWrite(NewConverter
*cnvData
, const UConverterStaticData
*staticData
,
44 UNewDataMemory
*pData
, int32_t tableType
);
46 typedef struct CnvExtData
{
47 NewConverter newConverter
;
51 /* toUnicode (state table in ucm->states) */
52 UToolMemory
*toUTable
, *toUUChars
;
55 UToolMemory
*fromUTableUChars
, *fromUTableValues
, *fromUBytes
;
57 uint16_t stage1
[MBCS_STAGE_1_SIZE
];
58 uint16_t stage2
[MBCS_STAGE_2_SIZE
];
59 uint16_t stage3
[0x10000<<UCNV_EXT_STAGE_2_LEFT_SHIFT
]; /* 0x10000 because of 16-bit stage 2/3 indexes */
60 uint32_t stage3b
[0x10000];
62 int32_t stage1Top
, stage2Top
, stage3Top
, stage3bTop
;
64 /* for stage3 compaction of <subchar1> |2 mappings */
65 uint16_t stage3Sub1Block
;
69 maxInBytes
, maxOutBytes
, maxBytesPerUChar
,
70 maxInUChars
, maxOutUChars
, maxUCharsPerByte
;
74 CnvExtOpen(UCMFile
*ucm
) {
77 extData
=(CnvExtData
*)uprv_malloc(sizeof(CnvExtData
));
79 uprv_memset(extData
, 0, sizeof(CnvExtData
));
81 extData
->ucm
=ucm
; /* aliased, not owned */
83 extData
->newConverter
.close
=CnvExtClose
;
84 extData
->newConverter
.isValid
=CnvExtIsValid
;
85 extData
->newConverter
.addTable
=CnvExtAddTable
;
86 extData
->newConverter
.write
=CnvExtWrite
;
88 return &extData
->newConverter
;
92 CnvExtClose(NewConverter
*cnvData
) {
93 CnvExtData
*extData
=(CnvExtData
*)cnvData
;
95 utm_close(extData
->toUTable
);
96 utm_close(extData
->toUUChars
);
97 utm_close(extData
->fromUTableUChars
);
98 utm_close(extData
->fromUTableValues
);
99 utm_close(extData
->fromUBytes
);
103 /* we do not expect this to be called */
105 CnvExtIsValid(NewConverter
*cnvData
,
106 const uint8_t *bytes
, int32_t length
) {
111 CnvExtWrite(NewConverter
*cnvData
, const UConverterStaticData
*staticData
,
112 UNewDataMemory
*pData
, int32_t tableType
) {
113 CnvExtData
*extData
=(CnvExtData
*)cnvData
;
114 int32_t length
, top
, headerSize
;
116 int32_t indexes
[UCNV_EXT_INDEXES_MIN_LENGTH
]={ 0 };
118 if(tableType
&TABLE_BASE
) {
121 _MBCSHeader header
={ { 0, 0, 0, 0 }, 0, 0, 0, 0, 0, 0, 0 };
123 /* write the header and base table name for an extension-only table */
124 length
=(int32_t)uprv_strlen(extData
->ucm
->baseName
)+1;
127 extData
->ucm
->baseName
[length
++]=0;
130 headerSize
=sizeof(header
)+length
;
132 /* fill the header */
135 header
.flags
=(uint32_t)((headerSize
<<8)|MBCS_OUTPUT_EXT_ONLY
);
137 /* write the header and the base table name */
138 udata_writeBlock(pData
, &header
, sizeof(header
));
139 udata_writeBlock(pData
, extData
->ucm
->baseName
, length
);
142 /* fill indexes[] - offsets/indexes are in units of the target array */
145 indexes
[UCNV_EXT_INDEXES_LENGTH
]=length
=UCNV_EXT_INDEXES_MIN_LENGTH
;
148 indexes
[UCNV_EXT_TO_U_INDEX
]=top
;
149 indexes
[UCNV_EXT_TO_U_LENGTH
]=length
=utm_countItems(extData
->toUTable
);
152 indexes
[UCNV_EXT_TO_U_UCHARS_INDEX
]=top
;
153 indexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]=length
=utm_countItems(extData
->toUUChars
);
156 indexes
[UCNV_EXT_FROM_U_UCHARS_INDEX
]=top
;
157 length
=utm_countItems(extData
->fromUTableUChars
);
162 *((UChar
*)utm_alloc(extData
->fromUTableUChars
))=0;
163 *((uint32_t *)utm_alloc(extData
->fromUTableValues
))=0;
167 indexes
[UCNV_EXT_FROM_U_LENGTH
]=length
;
169 indexes
[UCNV_EXT_FROM_U_VALUES_INDEX
]=top
;
172 indexes
[UCNV_EXT_FROM_U_BYTES_INDEX
]=top
;
173 length
=utm_countItems(extData
->fromUBytes
);
178 *((uint8_t *)utm_alloc(extData
->fromUBytes
))=0;
182 indexes
[UCNV_EXT_FROM_U_BYTES_LENGTH
]=length
;
184 indexes
[UCNV_EXT_FROM_U_STAGE_12_INDEX
]=top
;
185 indexes
[UCNV_EXT_FROM_U_STAGE_1_LENGTH
]=length
=extData
->stage1Top
;
186 indexes
[UCNV_EXT_FROM_U_STAGE_12_LENGTH
]=length
+=extData
->stage2Top
;
189 indexes
[UCNV_EXT_FROM_U_STAGE_3_INDEX
]=top
;
190 length
=extData
->stage3Top
;
195 extData
->stage3
[extData
->stage3Top
++]=0;
199 indexes
[UCNV_EXT_FROM_U_STAGE_3_LENGTH
]=length
;
201 indexes
[UCNV_EXT_FROM_U_STAGE_3B_INDEX
]=top
;
202 indexes
[UCNV_EXT_FROM_U_STAGE_3B_LENGTH
]=length
=extData
->stage3bTop
;
205 indexes
[UCNV_EXT_SIZE
]=top
;
208 indexes
[UCNV_EXT_COUNT_BYTES
]=
209 (extData
->maxInBytes
<<16)|
210 (extData
->maxOutBytes
<<8)|
211 extData
->maxBytesPerUChar
;
212 indexes
[UCNV_EXT_COUNT_UCHARS
]=
213 (extData
->maxInUChars
<<16)|
214 (extData
->maxOutUChars
<<8)|
215 extData
->maxUCharsPerByte
;
217 indexes
[UCNV_EXT_FLAGS
]=extData
->ucm
->ext
->unicodeMask
;
219 /* write the extension data */
220 udata_writeBlock(pData
, indexes
, sizeof(indexes
));
221 udata_writeBlock(pData
, utm_getStart(extData
->toUTable
), indexes
[UCNV_EXT_TO_U_LENGTH
]*4);
222 udata_writeBlock(pData
, utm_getStart(extData
->toUUChars
), indexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]*2);
224 udata_writeBlock(pData
, utm_getStart(extData
->fromUTableUChars
), indexes
[UCNV_EXT_FROM_U_LENGTH
]*2);
225 udata_writeBlock(pData
, utm_getStart(extData
->fromUTableValues
), indexes
[UCNV_EXT_FROM_U_LENGTH
]*4);
226 udata_writeBlock(pData
, utm_getStart(extData
->fromUBytes
), indexes
[UCNV_EXT_FROM_U_BYTES_LENGTH
]);
228 udata_writeBlock(pData
, extData
->stage1
, extData
->stage1Top
*2);
229 udata_writeBlock(pData
, extData
->stage2
, extData
->stage2Top
*2);
230 udata_writeBlock(pData
, extData
->stage3
, extData
->stage3Top
*2);
231 udata_writeBlock(pData
, extData
->stage3b
, extData
->stage3bTop
*4);
237 length
=extData
->stage1Top
;
238 printf("\nstage1[%x]:\n", length
);
240 for(i
=0; i
<length
; ++i
) {
241 if(extData
->stage1
[i
]!=length
) {
242 printf("stage1[%04x]=%04x\n", i
, extData
->stage1
[i
]);
247 length
=extData
->stage2Top
;
248 printf("\nstage2[%x]:\n", length
);
250 for(i
=0; i
<length
; ++j
, ++i
) {
251 if(extData
->stage2
[i
]!=0) {
252 printf("stage12[%04x]=%04x\n", j
, extData
->stage2
[i
]);
256 length
=extData
->stage3Top
;
257 printf("\nstage3[%x]:\n", length
);
259 for(i
=0; i
<length
; ++i
) {
260 if(extData
->stage3
[i
]!=0) {
261 printf("stage3[%04x]=%04x\n", i
, extData
->stage3
[i
]);
265 length
=extData
->stage3bTop
;
266 printf("\nstage3b[%x]:\n", length
);
268 for(i
=0; i
<length
; ++i
) {
269 if(extData
->stage3b
[i
]!=0) {
270 printf("stage3b[%04x]=%08x\n", i
, extData
->stage3b
[i
]);
277 printf("size of extension data: %ld\n", (long)top
);
280 /* return the number of bytes that should have been written */
281 return (uint32_t)(headerSize
+top
);
284 /* to Unicode --------------------------------------------------------------- */
287 * Remove fromUnicode fallbacks and SUB mappings which are irrelevant for
288 * the toUnicode table.
289 * The table must be sorted.
290 * Destroys previous data in the reverseMap.
293 reduceToUMappings(UCMTable
*table
) {
299 mappings
=table
->mappings
;
300 map
=table
->reverseMap
;
301 count
=table
->mappingsLength
;
303 /* leave the map alone for the initial mappings with desired flags */
304 for(i
=j
=0; i
<count
; ++i
) {
305 flag
=mappings
[map
[i
]].f
;
306 if(flag
!=0 && flag
!=3) {
311 /* reduce from here to the rest */
312 for(j
=i
; i
<count
; ++i
) {
313 flag
=mappings
[map
[i
]].f
;
314 if(flag
==0 || flag
==3) {
323 getToUnicodeValue(CnvExtData
*extData
, UCMTable
*table
, UCMapping
*m
) {
327 int32_t u16Length
, ratio
;
328 UErrorCode errorCode
;
330 /* write the Unicode result code point or string index */
332 u16Length
=U16_LENGTH(m
->u
);
333 value
=(uint32_t)(UCNV_EXT_TO_U_MIN_CODE_POINT
+m
->u
);
335 /* the parser enforces m->uLen<=UCNV_EXT_MAX_UCHARS */
337 /* get the result code point string and its 16-bit string length */
338 u32
=UCM_GET_CODE_POINTS(table
, m
);
339 errorCode
=U_ZERO_ERROR
;
340 u_strFromUTF32(NULL
, 0, &u16Length
, u32
, m
->uLen
, &errorCode
);
341 if(U_FAILURE(errorCode
) && errorCode
!=U_BUFFER_OVERFLOW_ERROR
) {
345 /* allocate it and put its length and index into the value */
347 (((uint32_t)m
->uLen
+UCNV_EXT_TO_U_LENGTH_OFFSET
)<<UCNV_EXT_TO_U_LENGTH_SHIFT
)|
348 ((uint32_t)utm_countItems(extData
->toUUChars
));
349 u
=utm_allocN(extData
->toUUChars
, u16Length
);
351 /* write the result 16-bit string */
352 errorCode
=U_ZERO_ERROR
;
353 u_strFromUTF32(u
, u16Length
, NULL
, u32
, m
->uLen
, &errorCode
);
354 if(U_FAILURE(errorCode
) && errorCode
!=U_BUFFER_OVERFLOW_ERROR
) {
359 value
|=UCNV_EXT_TO_U_ROUNDTRIP_FLAG
;
362 /* update statistics */
363 if(m
->bLen
>extData
->maxInBytes
) {
364 extData
->maxInBytes
=m
->bLen
;
366 if(u16Length
>extData
->maxOutUChars
) {
367 extData
->maxOutUChars
=u16Length
;
370 ratio
=(u16Length
+(m
->bLen
-1))/m
->bLen
;
371 if(ratio
>extData
->maxUCharsPerByte
) {
372 extData
->maxUCharsPerByte
=ratio
;
379 * Recursive toUTable generator core function.
381 * - start<limit (There is at least one mapping.)
382 * - The mappings are sorted lexically. (Access is through the reverseMap.)
383 * - All mappings between start and limit have input sequences that share
384 * the same prefix of unitIndex length, and therefore all of these sequences
385 * are at least unitIndex+1 long.
386 * - There are only relevant mappings available through the reverseMap,
387 * see reduceToUMappings().
389 * One function invocation generates one section table.
392 * 1. Count the number of unique unit values and get the low/high unit values
393 * that occur at unitIndex.
394 * 2. Allocate the section table with possible optimization for linear access.
395 * 3. Write temporary version of the section table with start indexes of
396 * subsections, each corresponding to one unit value at unitIndex.
397 * 4. Iterate through the table once more, and depending on the subsection length:
398 * 0: write 0 as a result value (unused byte in linear-access section table)
399 * >0: if there is one mapping with an input unit sequence of unitIndex+1
400 * then defaultValue=compute the mapping result for this whole sequence
401 * else defaultValue=0
403 * recurse into the subsection
406 generateToUTable(CnvExtData
*extData
, UCMTable
*table
,
407 int32_t start
, int32_t limit
, int32_t unitIndex
,
408 uint32_t defaultValue
) {
409 UCMapping
*mappings
, *m
;
411 int32_t i
, j
, uniqueCount
, count
, subStart
, subLimit
;
414 int32_t low
, high
, prev
;
418 mappings
=table
->mappings
;
419 map
=table
->reverseMap
;
421 /* step 1: examine the input units; set low, high, uniqueCount */
422 m
=mappings
+map
[start
];
423 bytes
=UCM_GET_BYTES(table
, m
);
424 low
=bytes
[unitIndex
];
428 for(i
=start
+1; i
<limit
; ++i
) {
430 bytes
=UCM_GET_BYTES(table
, m
);
431 high
=bytes
[unitIndex
];
439 /* step 2: allocate the section; set count, section */
441 if(unitIndex
==0 || uniqueCount
>=(3*count
)/4) {
443 * for the root table and for fairly full tables:
444 * allocate for direct, linear array access
445 * by keeping count, to write an entry for each unit value
452 /* allocate the section: 1 entry for the header + count for the items */
453 section
=(uint32_t *)utm_allocN(extData
->toUTable
, 1+count
);
455 /* write the section header */
456 *section
++=((uint32_t)count
<<UCNV_EXT_TO_U_BYTE_SHIFT
)|defaultValue
;
458 /* step 3: write temporary section table with subsection starts */
459 prev
=low
-1; /* just before low to prevent empty subsections before low */
460 j
=0; /* section table index */
461 for(i
=start
; i
<limit
; ++i
) {
463 bytes
=UCM_GET_BYTES(table
, m
);
464 high
=bytes
[unitIndex
];
467 /* start of a new subsection for unit high */
468 if(count
>uniqueCount
) {
469 /* write empty subsections for unused units in a linear table */
471 section
[j
++]=((uint32_t)prev
<<UCNV_EXT_TO_U_BYTE_SHIFT
)|(uint32_t)i
;
477 /* write the entry with the subsection start */
478 section
[j
++]=((uint32_t)high
<<UCNV_EXT_TO_U_BYTE_SHIFT
)|(uint32_t)i
;
481 /* assert(j==count) */
483 /* step 4: recurse and write results */
484 subLimit
=UCNV_EXT_TO_U_GET_VALUE(section
[0]);
485 for(j
=0; j
<count
; ++j
) {
487 subLimit
= (j
+1)<count
? UCNV_EXT_TO_U_GET_VALUE(section
[j
+1]) : limit
;
489 /* remove the subStart temporary value */
490 section
[j
]&=~UCNV_EXT_TO_U_VALUE_MASK
;
492 if(subStart
==subLimit
) {
493 /* leave the value zero: empty subsection for unused unit in a linear table */
497 /* see if there is exactly one input unit sequence of length unitIndex+1 */
499 m
=mappings
+map
[subStart
];
500 if(m
->bLen
==unitIndex
+1) {
501 /* do not include this in generateToUTable() */
504 if(subStart
<subLimit
&& mappings
[map
[subStart
]].bLen
==unitIndex
+1) {
505 /* print error for multiple same-input-sequence mappings */
506 fprintf(stderr
, "error: multiple mappings from same bytes\n");
507 ucm_printMapping(table
, m
, stderr
);
508 ucm_printMapping(table
, mappings
+map
[subStart
], stderr
);
512 defaultValue
=getToUnicodeValue(extData
, table
, m
);
515 if(subStart
==subLimit
) {
516 /* write the result for the input sequence ending here */
517 section
[j
]|=defaultValue
;
519 /* write the index to the subsection table */
520 section
[j
]|=(uint32_t)utm_countItems(extData
->toUTable
);
523 if(!generateToUTable(extData
, table
, subStart
, subLimit
, unitIndex
+1, defaultValue
)) {
532 * Generate the toUTable and toUUChars from the input table.
533 * The input table must be sorted, and all precision flags must be 0..3.
534 * This function will modify the table's reverseMap.
537 makeToUTable(CnvExtData
*extData
, UCMTable
*table
) {
540 toUCount
=reduceToUMappings(table
);
542 extData
->toUTable
=utm_open("cnv extension toUTable", 0x10000, UCNV_EXT_TO_U_MIN_CODE_POINT
, 4);
543 extData
->toUUChars
=utm_open("cnv extension toUUChars", 0x10000, UCNV_EXT_TO_U_INDEX_MASK
+1, 2);
545 return generateToUTable(extData
, table
, 0, toUCount
, 0, 0);
548 /* from Unicode ------------------------------------------------------------- */
552 * rebuild reverseMap with mapping indexes for mappings relevant for from Unicode
553 * change each Unicode string to encode all but the first code point in 16-bit form
556 * for each unique code point
557 * write an entry in the 3-stage trie
558 * check that there is only one single-code point sequence
559 * start recursion for following 16-bit input units
563 * Remove toUnicode fallbacks and non-<subchar1> SUB mappings
564 * which are irrelevant for the fromUnicode extension table.
565 * Overwrite the reverseMap with an index array to the relevant mappings.
566 * Modify the code point sequences to a generator-friendly format where
567 * the first code points remains unchanged but the following are recoded
568 * into 16-bit Unicode string form.
569 * The table must be sorted.
570 * Destroys previous data in the reverseMap.
573 prepareFromUMappings(UCMTable
*table
) {
574 UCMapping
*mappings
, *m
;
579 mappings
=table
->mappings
;
580 map
=table
->reverseMap
;
581 count
=table
->mappingsLength
;
584 * we do not go through the map on input because the mappings are
589 for(i
=j
=0; i
<count
; ++m
, ++i
) {
591 if(flag
==0 || flag
==1 || (flag
==2 && m
->bLen
==1)) {
595 /* recode all but the first code point to 16-bit Unicode */
601 u32
=UCM_GET_CODE_POINTS(table
, m
);
602 u
=(UChar
*)u32
; /* destructive in-place recoding */
603 for(r
=2, q
=1; q
<m
->uLen
; ++q
) {
605 U16_APPEND_UNSAFE(u
, r
, c
);
608 /* counts the first code point always at 2 - the first 16-bit unit is at 16-bit index 2 */
618 getFromUBytesValue(CnvExtData
*extData
, UCMTable
*table
, UCMapping
*m
) {
619 uint8_t *bytes
, *resultBytes
;
621 int32_t u16Length
, ratio
;
625 * no mapping, <subchar1> preferred
627 * no need to count in statistics because the subchars are already
628 * counted for maxOutBytes and maxBytesPerUChar in UConverterStaticData,
629 * and this non-mapping does not count for maxInUChars which are always
630 * trivially at least two if counting unmappable supplementary code points
632 return UCNV_EXT_FROM_U_SUBCHAR1
;
635 bytes
=UCM_GET_BYTES(table
, m
);
638 /* 1..3: store the bytes in the value word */
640 value
=((uint32_t)*bytes
++)<<16;
642 value
|=((uint32_t)*bytes
++)<<8;
647 /* the parser enforces m->bLen<=UCNV_EXT_MAX_BYTES */
648 /* store the bytes in fromUBytes[] and the index in the value word */
649 value
=(uint32_t)utm_countItems(extData
->fromUBytes
);
650 resultBytes
=utm_allocN(extData
->fromUBytes
, m
->bLen
);
651 uprv_memcpy(resultBytes
, bytes
, m
->bLen
);
654 value
|=(uint32_t)m
->bLen
<<UCNV_EXT_FROM_U_LENGTH_SHIFT
;
656 value
|=UCNV_EXT_FROM_U_ROUNDTRIP_FLAG
;
659 /* calculate the real UTF-16 length (see recoding in prepareFromUMappings()) */
661 u16Length
=U16_LENGTH(m
->u
);
663 u16Length
=U16_LENGTH(UCM_GET_CODE_POINTS(table
, m
)[0])+(m
->uLen
-2);
666 /* update statistics */
667 if(u16Length
>extData
->maxInUChars
) {
668 extData
->maxInUChars
=u16Length
;
670 if(m
->bLen
>extData
->maxOutBytes
) {
671 extData
->maxOutBytes
=m
->bLen
;
674 ratio
=(m
->bLen
+(u16Length
-1))/u16Length
;
675 if(ratio
>extData
->maxBytesPerUChar
) {
676 extData
->maxBytesPerUChar
=ratio
;
683 * works like generateToUTable(), except that the
684 * output section consists of two arrays, one for input UChars and one
687 * also, fromUTable sections are always stored in a compact form for
688 * access via binary search
691 generateFromUTable(CnvExtData
*extData
, UCMTable
*table
,
692 int32_t start
, int32_t limit
, int32_t unitIndex
,
693 uint32_t defaultValue
) {
694 UCMapping
*mappings
, *m
;
696 int32_t i
, j
, uniqueCount
, count
, subStart
, subLimit
;
699 UChar32 low
, high
, prev
;
701 UChar
*sectionUChars
;
702 uint32_t *sectionValues
;
704 mappings
=table
->mappings
;
705 map
=table
->reverseMap
;
707 /* step 1: examine the input units; set low, high, uniqueCount */
708 m
=mappings
+map
[start
];
709 uchars
=(UChar
*)UCM_GET_CODE_POINTS(table
, m
);
710 low
=uchars
[unitIndex
];
714 for(i
=start
+1; i
<limit
; ++i
) {
716 uchars
=(UChar
*)UCM_GET_CODE_POINTS(table
, m
);
717 high
=uchars
[unitIndex
];
725 /* step 2: allocate the section; set count, section */
726 /* the fromUTable always stores for access via binary search */
729 /* allocate the section: 1 entry for the header + count for the items */
730 sectionUChars
=(UChar
*)utm_allocN(extData
->fromUTableUChars
, 1+count
);
731 sectionValues
=(uint32_t *)utm_allocN(extData
->fromUTableValues
, 1+count
);
733 /* write the section header */
734 *sectionUChars
++=(UChar
)count
;
735 *sectionValues
++=defaultValue
;
737 /* step 3: write temporary section table with subsection starts */
738 prev
=low
-1; /* just before low to prevent empty subsections before low */
739 j
=0; /* section table index */
740 for(i
=start
; i
<limit
; ++i
) {
742 uchars
=(UChar
*)UCM_GET_CODE_POINTS(table
, m
);
743 high
=uchars
[unitIndex
];
746 /* start of a new subsection for unit high */
749 /* write the entry with the subsection start */
750 sectionUChars
[j
]=(UChar
)high
;
751 sectionValues
[j
]=(uint32_t)i
;
755 /* assert(j==count) */
757 /* step 4: recurse and write results */
758 subLimit
=(int32_t)(sectionValues
[0]);
759 for(j
=0; j
<count
; ++j
) {
761 subLimit
= (j
+1)<count
? (int32_t)(sectionValues
[j
+1]) : limit
;
763 /* see if there is exactly one input unit sequence of length unitIndex+1 */
765 m
=mappings
+map
[subStart
];
766 if(m
->uLen
==unitIndex
+1) {
767 /* do not include this in generateToUTable() */
770 if(subStart
<subLimit
&& mappings
[map
[subStart
]].uLen
==unitIndex
+1) {
771 /* print error for multiple same-input-sequence mappings */
772 fprintf(stderr
, "error: multiple mappings from same Unicode code points\n");
773 ucm_printMapping(table
, m
, stderr
);
774 ucm_printMapping(table
, mappings
+map
[subStart
], stderr
);
778 defaultValue
=getFromUBytesValue(extData
, table
, m
);
781 if(subStart
==subLimit
) {
782 /* write the result for the input sequence ending here */
783 sectionValues
[j
]=defaultValue
;
785 /* write the index to the subsection table */
786 sectionValues
[j
]=(uint32_t)utm_countItems(extData
->fromUTableValues
);
789 if(!generateFromUTable(extData
, table
, subStart
, subLimit
, unitIndex
+1, defaultValue
)) {
798 * add entries to the fromUnicode trie,
799 * assume to be called with code points in ascending order
800 * and use that to build the trie in precompacted form
803 addFromUTrieEntry(CnvExtData
*extData
, UChar32 c
, uint32_t value
) {
804 int32_t i1
, i2
, i3
, i3b
, nextOffset
, min
, newBlock
;
811 * compute the index for each stage,
812 * allocate a stage block if necessary,
813 * and write the stage value
816 if(i1
>=extData
->stage1Top
) {
817 extData
->stage1Top
=i1
+1;
820 nextOffset
=(c
>>4)&0x3f;
822 if(extData
->stage1
[i1
]==0) {
823 /* allocate another block in stage 2; overlap with the previous block */
824 newBlock
=extData
->stage2Top
;
825 min
=newBlock
-nextOffset
; /* minimum block start with overlap */
826 while(min
<newBlock
&& extData
->stage2
[newBlock
-1]==0) {
830 extData
->stage1
[i1
]=(uint16_t)newBlock
;
831 extData
->stage2Top
=newBlock
+MBCS_STAGE_2_BLOCK_SIZE
;
832 if(extData
->stage2Top
>LENGTHOF(extData
->stage2
)) {
833 fprintf(stderr
, "error: too many stage 2 entries at U+%04x\n", (int)c
);
834 exit(U_MEMORY_ALLOCATION_ERROR
);
838 i2
=extData
->stage1
[i1
]+nextOffset
;
841 if(extData
->stage2
[i2
]==0) {
842 /* allocate another block in stage 3; overlap with the previous block */
843 newBlock
=extData
->stage3Top
;
844 min
=newBlock
-nextOffset
; /* minimum block start with overlap */
845 while(min
<newBlock
&& extData
->stage3
[newBlock
-1]==0) {
849 /* round up to a multiple of stage 3 granularity >1 (similar to utrie.c) */
850 newBlock
=(newBlock
+(UCNV_EXT_STAGE_3_GRANULARITY
-1))&~(UCNV_EXT_STAGE_3_GRANULARITY
-1);
851 extData
->stage2
[i2
]=(uint16_t)(newBlock
>>UCNV_EXT_STAGE_2_LEFT_SHIFT
);
853 extData
->stage3Top
=newBlock
+MBCS_STAGE_3_BLOCK_SIZE
;
854 if(extData
->stage3Top
>LENGTHOF(extData
->stage3
)) {
855 fprintf(stderr
, "error: too many stage 3 entries at U+%04x\n", (int)c
);
856 exit(U_MEMORY_ALLOCATION_ERROR
);
860 i3
=((int32_t)extData
->stage2
[i2
]<<UCNV_EXT_STAGE_2_LEFT_SHIFT
)+nextOffset
;
862 * assume extData->stage3[i3]==0 because we get
863 * code points in strictly ascending order
866 if(value
==UCNV_EXT_FROM_U_SUBCHAR1
) {
867 /* <subchar1> SUB mapping, see getFromUBytesValue() and prepareFromUMappings() */
868 extData
->stage3
[i3
]=1;
871 * precompaction is not optimal for <subchar1> |2 mappings because
872 * stage3 values for them are all the same, unlike for other mappings
873 * which all have unique values;
874 * use a simple compaction of reusing a whole block filled with these
878 /* is the entire block filled with <subchar1> |2 mappings? */
879 if(nextOffset
==MBCS_STAGE_3_BLOCK_SIZE
-1) {
880 for(min
=i3
-nextOffset
;
881 min
<i3
&& extData
->stage3
[min
]==1;
885 /* the entire block is filled with these mappings */
886 if(extData
->stage3Sub1Block
!=0) {
887 /* point to the previous such block and remove this block from stage3 */
888 extData
->stage2
[i2
]=extData
->stage3Sub1Block
;
889 extData
->stage3Top
-=MBCS_STAGE_3_BLOCK_SIZE
;
890 uprv_memset(extData
->stage3
+extData
->stage3Top
, 0, MBCS_STAGE_3_BLOCK_SIZE
*2);
892 /* remember this block's stage2 entry */
893 extData
->stage3Sub1Block
=extData
->stage2
[i2
];
898 if((i3b
=extData
->stage3bTop
++)>=LENGTHOF(extData
->stage3b
)) {
899 fprintf(stderr
, "error: too many stage 3b entries at U+%04x\n", (int)c
);
900 exit(U_MEMORY_ALLOCATION_ERROR
);
903 /* roundtrip or fallback mapping */
904 extData
->stage3
[i3
]=(uint16_t)i3b
;
905 extData
->stage3b
[i3b
]=value
;
910 generateFromUTrie(CnvExtData
*extData
, UCMTable
*table
, int32_t mapLength
) {
911 UCMapping
*mappings
, *m
;
914 int32_t subStart
, subLimit
;
923 mappings
=table
->mappings
;
924 map
=table
->reverseMap
;
927 * iterate over same-initial-code point mappings,
928 * enter the initial code point into the trie,
929 * and start a recursion on the corresponding mappings section
930 * with generateFromUTable()
933 codePoints
=UCM_GET_CODE_POINTS(table
, m
);
936 while(subLimit
<mapLength
) {
937 /* get a new subsection of mappings starting with the same code point */
940 while(next
==c
&& ++subLimit
<mapLength
) {
941 m
=mappings
+map
[subLimit
];
942 codePoints
=UCM_GET_CODE_POINTS(table
, m
);
947 * compute the value for this code point;
948 * if there is a mapping for this code point alone, it is at subStart
949 * because the table is sorted lexically
952 m
=mappings
+map
[subStart
];
953 codePoints
=UCM_GET_CODE_POINTS(table
, m
);
955 /* do not include this in generateFromUTable() */
958 if(subStart
<subLimit
&& mappings
[map
[subStart
]].uLen
==1) {
959 /* print error for multiple same-input-sequence mappings */
960 fprintf(stderr
, "error: multiple mappings from same Unicode code points\n");
961 ucm_printMapping(table
, m
, stderr
);
962 ucm_printMapping(table
, mappings
+map
[subStart
], stderr
);
966 value
=getFromUBytesValue(extData
, table
, m
);
969 if(subStart
==subLimit
) {
970 /* write the result for this one code point */
971 addFromUTrieEntry(extData
, c
, value
);
973 /* write the index to the subsection table */
974 addFromUTrieEntry(extData
, c
, (uint32_t)utm_countItems(extData
->fromUTableValues
));
976 /* recurse, starting from 16-bit-unit index 2, the first 16-bit unit after c */
977 if(!generateFromUTable(extData
, table
, subStart
, subLimit
, 2, value
)) {
986 * Generate the fromU data structures from the input table.
987 * The input table must be sorted, and all precision flags must be 0..3.
988 * This function will modify the table's reverseMap.
991 makeFromUTable(CnvExtData
*extData
, UCMTable
*table
) {
993 int32_t i
, stage1Top
, fromUCount
;
995 fromUCount
=prepareFromUMappings(table
);
997 extData
->fromUTableUChars
=utm_open("cnv extension fromUTableUChars", 0x10000, UCNV_EXT_FROM_U_DATA_MASK
+1, 2);
998 extData
->fromUTableValues
=utm_open("cnv extension fromUTableValues", 0x10000, UCNV_EXT_FROM_U_DATA_MASK
+1, 4);
999 extData
->fromUBytes
=utm_open("cnv extension fromUBytes", 0x10000, UCNV_EXT_FROM_U_DATA_MASK
+1, 1);
1001 /* allocate all-unassigned stage blocks */
1002 extData
->stage2Top
=MBCS_STAGE_2_FIRST_ASSIGNED
;
1003 extData
->stage3Top
=MBCS_STAGE_3_FIRST_ASSIGNED
;
1006 * stage 3b stores only unique values, and in
1007 * index 0: 0 for "no mapping"
1008 * index 1: "no mapping" with preference for <subchar1> rather than <subchar>
1010 extData
->stage3b
[1]=UCNV_EXT_FROM_U_SUBCHAR1
;
1011 extData
->stage3bTop
=2;
1013 /* allocate the first entry in the fromUTable because index 0 means "no result" */
1014 utm_alloc(extData
->fromUTableUChars
);
1015 utm_alloc(extData
->fromUTableValues
);
1017 if(!generateFromUTrie(extData
, table
, fromUCount
)) {
1022 * offset the stage 1 trie entries by stage1Top because they will
1023 * be stored in a single array
1025 stage1
=extData
->stage1
;
1026 stage1Top
=extData
->stage1Top
;
1027 for(i
=0; i
<stage1Top
; ++i
) {
1028 stage1
[i
]=(uint16_t)(stage1
[i
]+stage1Top
);
1034 /* -------------------------------------------------------------------------- */
1037 CnvExtAddTable(NewConverter
*cnvData
, UCMTable
*table
, UConverterStaticData
*staticData
) {
1038 CnvExtData
*extData
;
1040 if(table
->unicodeMask
&UCNV_HAS_SURROGATES
) {
1041 fprintf(stderr
, "error: contains mappings for surrogate code points\n");
1045 staticData
->conversionType
=UCNV_MBCS
;
1047 extData
=(CnvExtData
*)cnvData
;
1050 * assume that the table is sorted
1052 * call the functions in this order because
1053 * makeToUTable() modifies the original reverseMap,
1054 * makeFromUTable() writes a whole new mapping into reverseMap
1057 makeToUTable(extData
, table
) &&
1058 makeFromUTable(extData
, table
);