2 *******************************************************************************
4 * Copyright (C) 2003-2014, 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"
31 CnvExtClose(NewConverter
*cnvData
);
34 CnvExtIsValid(NewConverter
*cnvData
,
35 const uint8_t *bytes
, int32_t length
);
38 CnvExtAddTable(NewConverter
*cnvData
, UCMTable
*table
, UConverterStaticData
*staticData
);
41 CnvExtWrite(NewConverter
*cnvData
, const UConverterStaticData
*staticData
,
42 UNewDataMemory
*pData
, int32_t tableType
);
44 typedef struct CnvExtData
{
45 NewConverter newConverter
;
49 /* toUnicode (state table in ucm->states) */
50 UToolMemory
*toUTable
, *toUUChars
;
53 UToolMemory
*fromUTableUChars
, *fromUTableValues
, *fromUBytes
;
55 uint16_t stage1
[MBCS_STAGE_1_SIZE
];
56 uint16_t stage2
[MBCS_STAGE_2_SIZE
];
57 uint16_t stage3
[0x10000<<UCNV_EXT_STAGE_2_LEFT_SHIFT
]; /* 0x10000 because of 16-bit stage 2/3 indexes */
58 uint32_t stage3b
[0x10000];
60 int32_t stage1Top
, stage2Top
, stage3Top
, stage3bTop
;
62 /* for stage3 compaction of <subchar1> |2 mappings */
63 uint16_t stage3Sub1Block
;
67 maxInBytes
, maxOutBytes
, maxBytesPerUChar
,
68 maxInUChars
, maxOutUChars
, maxUCharsPerByte
;
72 CnvExtOpen(UCMFile
*ucm
) {
75 extData
=(CnvExtData
*)uprv_malloc(sizeof(CnvExtData
));
77 printf("out of memory\n");
78 exit(U_MEMORY_ALLOCATION_ERROR
);
80 uprv_memset(extData
, 0, sizeof(CnvExtData
));
82 extData
->ucm
=ucm
; /* aliased, not owned */
84 extData
->newConverter
.close
=CnvExtClose
;
85 extData
->newConverter
.isValid
=CnvExtIsValid
;
86 extData
->newConverter
.addTable
=CnvExtAddTable
;
87 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
);
104 /* we do not expect this to be called */
106 CnvExtIsValid(NewConverter
*cnvData
,
107 const uint8_t *bytes
, int32_t length
) {
112 CnvExtWrite(NewConverter
*cnvData
, const UConverterStaticData
*staticData
,
113 UNewDataMemory
*pData
, int32_t tableType
) {
114 CnvExtData
*extData
=(CnvExtData
*)cnvData
;
115 int32_t length
, top
, headerSize
;
117 int32_t indexes
[UCNV_EXT_INDEXES_MIN_LENGTH
]={ 0 };
119 if(tableType
&TABLE_BASE
) {
122 _MBCSHeader header
={ { 0, 0, 0, 0 }, 0, 0, 0, 0, 0, 0, 0 };
124 /* write the header and base table name for an extension-only table */
125 length
=(int32_t)uprv_strlen(extData
->ucm
->baseName
)+1;
128 extData
->ucm
->baseName
[length
++]=0;
131 headerSize
=MBCS_HEADER_V4_LENGTH
*4+length
;
133 /* fill the header */
136 header
.flags
=(uint32_t)((headerSize
<<8)|MBCS_OUTPUT_EXT_ONLY
);
138 /* write the header and the base table name */
139 udata_writeBlock(pData
, &header
, MBCS_HEADER_V4_LENGTH
*4);
140 udata_writeBlock(pData
, extData
->ucm
->baseName
, length
);
143 /* fill indexes[] - offsets/indexes are in units of the target array */
146 indexes
[UCNV_EXT_INDEXES_LENGTH
]=length
=UCNV_EXT_INDEXES_MIN_LENGTH
;
149 indexes
[UCNV_EXT_TO_U_INDEX
]=top
;
150 indexes
[UCNV_EXT_TO_U_LENGTH
]=length
=utm_countItems(extData
->toUTable
);
153 indexes
[UCNV_EXT_TO_U_UCHARS_INDEX
]=top
;
154 indexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]=length
=utm_countItems(extData
->toUUChars
);
157 indexes
[UCNV_EXT_FROM_U_UCHARS_INDEX
]=top
;
158 length
=utm_countItems(extData
->fromUTableUChars
);
163 *((UChar
*)utm_alloc(extData
->fromUTableUChars
))=0;
164 *((uint32_t *)utm_alloc(extData
->fromUTableValues
))=0;
168 indexes
[UCNV_EXT_FROM_U_LENGTH
]=length
;
170 indexes
[UCNV_EXT_FROM_U_VALUES_INDEX
]=top
;
173 indexes
[UCNV_EXT_FROM_U_BYTES_INDEX
]=top
;
174 length
=utm_countItems(extData
->fromUBytes
);
179 *((uint8_t *)utm_alloc(extData
->fromUBytes
))=0;
183 indexes
[UCNV_EXT_FROM_U_BYTES_LENGTH
]=length
;
185 indexes
[UCNV_EXT_FROM_U_STAGE_12_INDEX
]=top
;
186 indexes
[UCNV_EXT_FROM_U_STAGE_1_LENGTH
]=length
=extData
->stage1Top
;
187 indexes
[UCNV_EXT_FROM_U_STAGE_12_LENGTH
]=length
+=extData
->stage2Top
;
190 indexes
[UCNV_EXT_FROM_U_STAGE_3_INDEX
]=top
;
191 length
=extData
->stage3Top
;
196 extData
->stage3
[extData
->stage3Top
++]=0;
200 indexes
[UCNV_EXT_FROM_U_STAGE_3_LENGTH
]=length
;
202 indexes
[UCNV_EXT_FROM_U_STAGE_3B_INDEX
]=top
;
203 indexes
[UCNV_EXT_FROM_U_STAGE_3B_LENGTH
]=length
=extData
->stage3bTop
;
206 indexes
[UCNV_EXT_SIZE
]=top
;
209 indexes
[UCNV_EXT_COUNT_BYTES
]=
210 (extData
->maxInBytes
<<16)|
211 (extData
->maxOutBytes
<<8)|
212 extData
->maxBytesPerUChar
;
213 indexes
[UCNV_EXT_COUNT_UCHARS
]=
214 (extData
->maxInUChars
<<16)|
215 (extData
->maxOutUChars
<<8)|
216 extData
->maxUCharsPerByte
;
218 indexes
[UCNV_EXT_FLAGS
]=extData
->ucm
->ext
->unicodeMask
;
220 /* write the extension data */
221 udata_writeBlock(pData
, indexes
, sizeof(indexes
));
222 udata_writeBlock(pData
, utm_getStart(extData
->toUTable
), indexes
[UCNV_EXT_TO_U_LENGTH
]*4);
223 udata_writeBlock(pData
, utm_getStart(extData
->toUUChars
), indexes
[UCNV_EXT_TO_U_UCHARS_LENGTH
]*2);
225 udata_writeBlock(pData
, utm_getStart(extData
->fromUTableUChars
), indexes
[UCNV_EXT_FROM_U_LENGTH
]*2);
226 udata_writeBlock(pData
, utm_getStart(extData
->fromUTableValues
), indexes
[UCNV_EXT_FROM_U_LENGTH
]*4);
227 udata_writeBlock(pData
, utm_getStart(extData
->fromUBytes
), indexes
[UCNV_EXT_FROM_U_BYTES_LENGTH
]);
229 udata_writeBlock(pData
, extData
->stage1
, extData
->stage1Top
*2);
230 udata_writeBlock(pData
, extData
->stage2
, extData
->stage2Top
*2);
231 udata_writeBlock(pData
, extData
->stage3
, extData
->stage3Top
*2);
232 udata_writeBlock(pData
, extData
->stage3b
, extData
->stage3bTop
*4);
238 length
=extData
->stage1Top
;
239 printf("\nstage1[%x]:\n", length
);
241 for(i
=0; i
<length
; ++i
) {
242 if(extData
->stage1
[i
]!=length
) {
243 printf("stage1[%04x]=%04x\n", i
, extData
->stage1
[i
]);
248 length
=extData
->stage2Top
;
249 printf("\nstage2[%x]:\n", length
);
251 for(i
=0; i
<length
; ++j
, ++i
) {
252 if(extData
->stage2
[i
]!=0) {
253 printf("stage12[%04x]=%04x\n", j
, extData
->stage2
[i
]);
257 length
=extData
->stage3Top
;
258 printf("\nstage3[%x]:\n", length
);
260 for(i
=0; i
<length
; ++i
) {
261 if(extData
->stage3
[i
]!=0) {
262 printf("stage3[%04x]=%04x\n", i
, extData
->stage3
[i
]);
266 length
=extData
->stage3bTop
;
267 printf("\nstage3b[%x]:\n", length
);
269 for(i
=0; i
<length
; ++i
) {
270 if(extData
->stage3b
[i
]!=0) {
271 printf("stage3b[%04x]=%08x\n", i
, extData
->stage3b
[i
]);
278 printf("size of extension data: %ld\n", (long)top
);
281 /* return the number of bytes that should have been written */
282 return (uint32_t)(headerSize
+top
);
285 /* to Unicode --------------------------------------------------------------- */
288 * Remove fromUnicode fallbacks and SUB mappings which are irrelevant for
289 * the toUnicode table.
290 * This includes mappings with MBCS_FROM_U_EXT_FLAG which were suitable
291 * for the base toUnicode table but not for the base fromUnicode table.
292 * The table must be sorted.
293 * Modifies previous data in the reverseMap.
296 reduceToUMappings(UCMTable
*table
) {
302 mappings
=table
->mappings
;
303 map
=table
->reverseMap
;
304 count
=table
->mappingsLength
;
306 /* leave the map alone for the initial mappings with desired flags */
307 for(i
=j
=0; i
<count
; ++i
) {
308 flag
=mappings
[map
[i
]].f
;
309 if(flag
!=0 && flag
!=3) {
314 /* reduce from here to the rest */
315 for(j
=i
; i
<count
; ++i
) {
316 flag
=mappings
[map
[i
]].f
;
317 if(flag
==0 || flag
==3) {
326 getToUnicodeValue(CnvExtData
*extData
, UCMTable
*table
, UCMapping
*m
) {
330 int32_t u16Length
, ratio
;
331 UErrorCode errorCode
;
333 /* write the Unicode result code point or string index */
335 u16Length
=U16_LENGTH(m
->u
);
336 value
=(uint32_t)(UCNV_EXT_TO_U_MIN_CODE_POINT
+m
->u
);
338 /* the parser enforces m->uLen<=UCNV_EXT_MAX_UCHARS */
340 /* get the result code point string and its 16-bit string length */
341 u32
=UCM_GET_CODE_POINTS(table
, m
);
342 errorCode
=U_ZERO_ERROR
;
343 u_strFromUTF32(NULL
, 0, &u16Length
, u32
, m
->uLen
, &errorCode
);
344 if(U_FAILURE(errorCode
) && errorCode
!=U_BUFFER_OVERFLOW_ERROR
) {
348 /* allocate it and put its length and index into the value */
350 (((uint32_t)u16Length
+UCNV_EXT_TO_U_LENGTH_OFFSET
)<<UCNV_EXT_TO_U_LENGTH_SHIFT
)|
351 ((uint32_t)utm_countItems(extData
->toUUChars
));
352 u
=utm_allocN(extData
->toUUChars
, u16Length
);
354 /* write the result 16-bit string */
355 errorCode
=U_ZERO_ERROR
;
356 u_strFromUTF32(u
, u16Length
, NULL
, u32
, m
->uLen
, &errorCode
);
357 if(U_FAILURE(errorCode
) && errorCode
!=U_BUFFER_OVERFLOW_ERROR
) {
362 value
|=UCNV_EXT_TO_U_ROUNDTRIP_FLAG
;
365 /* update statistics */
366 if(m
->bLen
>extData
->maxInBytes
) {
367 extData
->maxInBytes
=m
->bLen
;
369 if(u16Length
>extData
->maxOutUChars
) {
370 extData
->maxOutUChars
=u16Length
;
373 ratio
=(u16Length
+(m
->bLen
-1))/m
->bLen
;
374 if(ratio
>extData
->maxUCharsPerByte
) {
375 extData
->maxUCharsPerByte
=ratio
;
382 * Recursive toUTable generator core function.
384 * - start<limit (There is at least one mapping.)
385 * - The mappings are sorted lexically. (Access is through the reverseMap.)
386 * - All mappings between start and limit have input sequences that share
387 * the same prefix of unitIndex length, and therefore all of these sequences
388 * are at least unitIndex+1 long.
389 * - There are only relevant mappings available through the reverseMap,
390 * see reduceToUMappings().
392 * One function invocation generates one section table.
395 * 1. Count the number of unique unit values and get the low/high unit values
396 * that occur at unitIndex.
397 * 2. Allocate the section table with possible optimization for linear access.
398 * 3. Write temporary version of the section table with start indexes of
399 * subsections, each corresponding to one unit value at unitIndex.
400 * 4. Iterate through the table once more, and depending on the subsection length:
401 * 0: write 0 as a result value (unused byte in linear-access section table)
402 * >0: if there is one mapping with an input unit sequence of unitIndex+1
403 * then defaultValue=compute the mapping result for this whole sequence
404 * else defaultValue=0
406 * recurse into the subsection
409 generateToUTable(CnvExtData
*extData
, UCMTable
*table
,
410 int32_t start
, int32_t limit
, int32_t unitIndex
,
411 uint32_t defaultValue
) {
412 UCMapping
*mappings
, *m
;
414 int32_t i
, j
, uniqueCount
, count
, subStart
, subLimit
;
417 int32_t low
, high
, prev
;
421 mappings
=table
->mappings
;
422 map
=table
->reverseMap
;
424 /* step 1: examine the input units; set low, high, uniqueCount */
425 m
=mappings
+map
[start
];
426 bytes
=UCM_GET_BYTES(table
, m
);
427 low
=bytes
[unitIndex
];
431 for(i
=start
+1; i
<limit
; ++i
) {
433 bytes
=UCM_GET_BYTES(table
, m
);
434 high
=bytes
[unitIndex
];
442 /* step 2: allocate the section; set count, section */
444 if(count
<0x100 && (unitIndex
==0 || uniqueCount
>=(3*count
)/4)) {
446 * for the root table and for fairly full tables:
447 * allocate for direct, linear array access
448 * by keeping count, to write an entry for each unit value
450 * exception: use a compact table if count==0x100 because
451 * that cannot be encoded in the length byte
458 fprintf(stderr
, "error: toUnicode extension table section overflow: %ld section entries\n", (long)count
);
462 /* allocate the section: 1 entry for the header + count for the items */
463 section
=(uint32_t *)utm_allocN(extData
->toUTable
, 1+count
);
465 /* write the section header */
466 *section
++=((uint32_t)count
<<UCNV_EXT_TO_U_BYTE_SHIFT
)|defaultValue
;
468 /* step 3: write temporary section table with subsection starts */
469 prev
=low
-1; /* just before low to prevent empty subsections before low */
470 j
=0; /* section table index */
471 for(i
=start
; i
<limit
; ++i
) {
473 bytes
=UCM_GET_BYTES(table
, m
);
474 high
=bytes
[unitIndex
];
477 /* start of a new subsection for unit high */
478 if(count
>uniqueCount
) {
479 /* write empty subsections for unused units in a linear table */
481 section
[j
++]=((uint32_t)prev
<<UCNV_EXT_TO_U_BYTE_SHIFT
)|(uint32_t)i
;
487 /* write the entry with the subsection start */
488 section
[j
++]=((uint32_t)high
<<UCNV_EXT_TO_U_BYTE_SHIFT
)|(uint32_t)i
;
491 /* assert(j==count) */
493 /* step 4: recurse and write results */
494 subLimit
=UCNV_EXT_TO_U_GET_VALUE(section
[0]);
495 for(j
=0; j
<count
; ++j
) {
497 subLimit
= (j
+1)<count
? UCNV_EXT_TO_U_GET_VALUE(section
[j
+1]) : limit
;
499 /* remove the subStart temporary value */
500 section
[j
]&=~UCNV_EXT_TO_U_VALUE_MASK
;
502 if(subStart
==subLimit
) {
503 /* leave the value zero: empty subsection for unused unit in a linear table */
507 /* see if there is exactly one input unit sequence of length unitIndex+1 */
509 m
=mappings
+map
[subStart
];
510 if(m
->bLen
==unitIndex
+1) {
511 /* do not include this in generateToUTable() */
514 if(subStart
<subLimit
&& mappings
[map
[subStart
]].bLen
==unitIndex
+1) {
515 /* print error for multiple same-input-sequence mappings */
516 fprintf(stderr
, "error: multiple mappings from same bytes\n");
517 ucm_printMapping(table
, m
, stderr
);
518 ucm_printMapping(table
, mappings
+map
[subStart
], stderr
);
522 defaultValue
=getToUnicodeValue(extData
, table
, m
);
525 if(subStart
==subLimit
) {
526 /* write the result for the input sequence ending here */
527 section
[j
]|=defaultValue
;
529 /* write the index to the subsection table */
530 section
[j
]|=(uint32_t)utm_countItems(extData
->toUTable
);
533 if(!generateToUTable(extData
, table
, subStart
, subLimit
, unitIndex
+1, defaultValue
)) {
542 * Generate the toUTable and toUUChars from the input table.
543 * The input table must be sorted, and all precision flags must be 0..3.
544 * This function will modify the table's reverseMap.
547 makeToUTable(CnvExtData
*extData
, UCMTable
*table
) {
550 toUCount
=reduceToUMappings(table
);
552 extData
->toUTable
=utm_open("cnv extension toUTable", 0x10000, UCNV_EXT_TO_U_MIN_CODE_POINT
, 4);
553 extData
->toUUChars
=utm_open("cnv extension toUUChars", 0x10000, UCNV_EXT_TO_U_INDEX_MASK
+1, 2);
555 return generateToUTable(extData
, table
, 0, toUCount
, 0, 0);
558 /* from Unicode ------------------------------------------------------------- */
562 * rebuild reverseMap with mapping indexes for mappings relevant for from Unicode
563 * change each Unicode string to encode all but the first code point in 16-bit form
566 * for each unique code point
567 * write an entry in the 3-stage trie
568 * check that there is only one single-code point sequence
569 * start recursion for following 16-bit input units
573 * Remove toUnicode fallbacks and non-<subchar1> SUB mappings
574 * which are irrelevant for the fromUnicode extension table.
575 * Remove MBCS_FROM_U_EXT_FLAG bits.
576 * Overwrite the reverseMap with an index array to the relevant mappings.
577 * Modify the code point sequences to a generator-friendly format where
578 * the first code points remains unchanged but the following are recoded
579 * into 16-bit Unicode string form.
580 * The table must be sorted.
581 * Destroys previous data in the reverseMap.
584 prepareFromUMappings(UCMTable
*table
) {
585 UCMapping
*mappings
, *m
;
590 mappings
=table
->mappings
;
591 map
=table
->reverseMap
;
592 count
=table
->mappingsLength
;
595 * we do not go through the map on input because the mappings are
600 for(i
=j
=0; i
<count
; ++m
, ++i
) {
603 flag
&=MBCS_FROM_U_EXT_MASK
;
606 if(flag
==0 || flag
==1 || (flag
==2 && m
->bLen
==1) || flag
==4) {
610 /* recode all but the first code point to 16-bit Unicode */
616 u32
=UCM_GET_CODE_POINTS(table
, m
);
617 u
=(UChar
*)u32
; /* destructive in-place recoding */
618 for(r
=2, q
=1; q
<m
->uLen
; ++q
) {
620 U16_APPEND_UNSAFE(u
, r
, c
);
623 /* counts the first code point always at 2 - the first 16-bit unit is at 16-bit index 2 */
633 getFromUBytesValue(CnvExtData
*extData
, UCMTable
*table
, UCMapping
*m
) {
634 uint8_t *bytes
, *resultBytes
;
636 int32_t u16Length
, ratio
;
640 * no mapping, <subchar1> preferred
642 * no need to count in statistics because the subchars are already
643 * counted for maxOutBytes and maxBytesPerUChar in UConverterStaticData,
644 * and this non-mapping does not count for maxInUChars which are always
645 * trivially at least two if counting unmappable supplementary code points
647 return UCNV_EXT_FROM_U_SUBCHAR1
;
650 bytes
=UCM_GET_BYTES(table
, m
);
653 /* 1..3: store the bytes in the value word */
655 value
=((uint32_t)*bytes
++)<<16;
657 value
|=((uint32_t)*bytes
++)<<8;
662 /* the parser enforces m->bLen<=UCNV_EXT_MAX_BYTES */
663 /* store the bytes in fromUBytes[] and the index in the value word */
664 value
=(uint32_t)utm_countItems(extData
->fromUBytes
);
665 resultBytes
=utm_allocN(extData
->fromUBytes
, m
->bLen
);
666 uprv_memcpy(resultBytes
, bytes
, m
->bLen
);
669 value
|=(uint32_t)m
->bLen
<<UCNV_EXT_FROM_U_LENGTH_SHIFT
;
671 value
|=UCNV_EXT_FROM_U_ROUNDTRIP_FLAG
;
673 value
|=UCNV_EXT_FROM_U_GOOD_ONE_WAY_FLAG
;
676 /* calculate the real UTF-16 length (see recoding in prepareFromUMappings()) */
678 u16Length
=U16_LENGTH(m
->u
);
680 u16Length
=U16_LENGTH(UCM_GET_CODE_POINTS(table
, m
)[0])+(m
->uLen
-2);
683 /* update statistics */
684 if(u16Length
>extData
->maxInUChars
) {
685 extData
->maxInUChars
=u16Length
;
687 if(m
->bLen
>extData
->maxOutBytes
) {
688 extData
->maxOutBytes
=m
->bLen
;
691 ratio
=(m
->bLen
+(u16Length
-1))/u16Length
;
692 if(ratio
>extData
->maxBytesPerUChar
) {
693 extData
->maxBytesPerUChar
=ratio
;
700 * works like generateToUTable(), except that the
701 * output section consists of two arrays, one for input UChars and one
704 * also, fromUTable sections are always stored in a compact form for
705 * access via binary search
708 generateFromUTable(CnvExtData
*extData
, UCMTable
*table
,
709 int32_t start
, int32_t limit
, int32_t unitIndex
,
710 uint32_t defaultValue
) {
711 UCMapping
*mappings
, *m
;
713 int32_t i
, j
, uniqueCount
, count
, subStart
, subLimit
;
716 UChar32 low
, high
, prev
;
718 UChar
*sectionUChars
;
719 uint32_t *sectionValues
;
721 mappings
=table
->mappings
;
722 map
=table
->reverseMap
;
724 /* step 1: examine the input units; set low, high, uniqueCount */
725 m
=mappings
+map
[start
];
726 uchars
=(UChar
*)UCM_GET_CODE_POINTS(table
, m
);
727 low
=uchars
[unitIndex
];
731 for(i
=start
+1; i
<limit
; ++i
) {
733 uchars
=(UChar
*)UCM_GET_CODE_POINTS(table
, m
);
734 high
=uchars
[unitIndex
];
742 /* step 2: allocate the section; set count, section */
743 /* the fromUTable always stores for access via binary search */
746 /* allocate the section: 1 entry for the header + count for the items */
747 sectionUChars
=(UChar
*)utm_allocN(extData
->fromUTableUChars
, 1+count
);
748 sectionValues
=(uint32_t *)utm_allocN(extData
->fromUTableValues
, 1+count
);
750 /* write the section header */
751 *sectionUChars
++=(UChar
)count
;
752 *sectionValues
++=defaultValue
;
754 /* step 3: write temporary section table with subsection starts */
755 prev
=low
-1; /* just before low to prevent empty subsections before low */
756 j
=0; /* section table index */
757 for(i
=start
; i
<limit
; ++i
) {
759 uchars
=(UChar
*)UCM_GET_CODE_POINTS(table
, m
);
760 high
=uchars
[unitIndex
];
763 /* start of a new subsection for unit high */
766 /* write the entry with the subsection start */
767 sectionUChars
[j
]=(UChar
)high
;
768 sectionValues
[j
]=(uint32_t)i
;
772 /* assert(j==count) */
774 /* step 4: recurse and write results */
775 subLimit
=(int32_t)(sectionValues
[0]);
776 for(j
=0; j
<count
; ++j
) {
778 subLimit
= (j
+1)<count
? (int32_t)(sectionValues
[j
+1]) : limit
;
780 /* see if there is exactly one input unit sequence of length unitIndex+1 */
782 m
=mappings
+map
[subStart
];
783 if(m
->uLen
==unitIndex
+1) {
784 /* do not include this in generateToUTable() */
787 if(subStart
<subLimit
&& mappings
[map
[subStart
]].uLen
==unitIndex
+1) {
788 /* print error for multiple same-input-sequence mappings */
789 fprintf(stderr
, "error: multiple mappings from same Unicode code points\n");
790 ucm_printMapping(table
, m
, stderr
);
791 ucm_printMapping(table
, mappings
+map
[subStart
], stderr
);
795 defaultValue
=getFromUBytesValue(extData
, table
, m
);
798 if(subStart
==subLimit
) {
799 /* write the result for the input sequence ending here */
800 sectionValues
[j
]=defaultValue
;
802 /* write the index to the subsection table */
803 sectionValues
[j
]=(uint32_t)utm_countItems(extData
->fromUTableValues
);
806 if(!generateFromUTable(extData
, table
, subStart
, subLimit
, unitIndex
+1, defaultValue
)) {
815 * add entries to the fromUnicode trie,
816 * assume to be called with code points in ascending order
817 * and use that to build the trie in precompacted form
820 addFromUTrieEntry(CnvExtData
*extData
, UChar32 c
, uint32_t value
) {
821 int32_t i1
, i2
, i3
, i3b
, nextOffset
, min
, newBlock
;
828 * compute the index for each stage,
829 * allocate a stage block if necessary,
830 * and write the stage value
833 if(i1
>=extData
->stage1Top
) {
834 extData
->stage1Top
=i1
+1;
837 nextOffset
=(c
>>4)&0x3f;
839 if(extData
->stage1
[i1
]==0) {
840 /* allocate another block in stage 2; overlap with the previous block */
841 newBlock
=extData
->stage2Top
;
842 min
=newBlock
-nextOffset
; /* minimum block start with overlap */
843 while(min
<newBlock
&& extData
->stage2
[newBlock
-1]==0) {
847 extData
->stage1
[i1
]=(uint16_t)newBlock
;
848 extData
->stage2Top
=newBlock
+MBCS_STAGE_2_BLOCK_SIZE
;
849 if(extData
->stage2Top
>UPRV_LENGTHOF(extData
->stage2
)) {
850 fprintf(stderr
, "error: too many stage 2 entries at U+%04x\n", (int)c
);
851 exit(U_MEMORY_ALLOCATION_ERROR
);
855 i2
=extData
->stage1
[i1
]+nextOffset
;
858 if(extData
->stage2
[i2
]==0) {
859 /* allocate another block in stage 3; overlap with the previous block */
860 newBlock
=extData
->stage3Top
;
861 min
=newBlock
-nextOffset
; /* minimum block start with overlap */
862 while(min
<newBlock
&& extData
->stage3
[newBlock
-1]==0) {
866 /* round up to a multiple of stage 3 granularity >1 (similar to utrie.c) */
867 newBlock
=(newBlock
+(UCNV_EXT_STAGE_3_GRANULARITY
-1))&~(UCNV_EXT_STAGE_3_GRANULARITY
-1);
868 extData
->stage2
[i2
]=(uint16_t)(newBlock
>>UCNV_EXT_STAGE_2_LEFT_SHIFT
);
870 extData
->stage3Top
=newBlock
+MBCS_STAGE_3_BLOCK_SIZE
;
871 if(extData
->stage3Top
>UPRV_LENGTHOF(extData
->stage3
)) {
872 fprintf(stderr
, "error: too many stage 3 entries at U+%04x\n", (int)c
);
873 exit(U_MEMORY_ALLOCATION_ERROR
);
877 i3
=((int32_t)extData
->stage2
[i2
]<<UCNV_EXT_STAGE_2_LEFT_SHIFT
)+nextOffset
;
879 * assume extData->stage3[i3]==0 because we get
880 * code points in strictly ascending order
883 if(value
==UCNV_EXT_FROM_U_SUBCHAR1
) {
884 /* <subchar1> SUB mapping, see getFromUBytesValue() and prepareFromUMappings() */
885 extData
->stage3
[i3
]=1;
888 * precompaction is not optimal for <subchar1> |2 mappings because
889 * stage3 values for them are all the same, unlike for other mappings
890 * which all have unique values;
891 * use a simple compaction of reusing a whole block filled with these
895 /* is the entire block filled with <subchar1> |2 mappings? */
896 if(nextOffset
==MBCS_STAGE_3_BLOCK_SIZE
-1) {
897 for(min
=i3
-nextOffset
;
898 min
<i3
&& extData
->stage3
[min
]==1;
902 /* the entire block is filled with these mappings */
903 if(extData
->stage3Sub1Block
!=0) {
904 /* point to the previous such block and remove this block from stage3 */
905 extData
->stage2
[i2
]=extData
->stage3Sub1Block
;
906 extData
->stage3Top
-=MBCS_STAGE_3_BLOCK_SIZE
;
907 uprv_memset(extData
->stage3
+extData
->stage3Top
, 0, MBCS_STAGE_3_BLOCK_SIZE
*2);
909 /* remember this block's stage2 entry */
910 extData
->stage3Sub1Block
=extData
->stage2
[i2
];
915 if((i3b
=extData
->stage3bTop
++)>=UPRV_LENGTHOF(extData
->stage3b
)) {
916 fprintf(stderr
, "error: too many stage 3b entries at U+%04x\n", (int)c
);
917 exit(U_MEMORY_ALLOCATION_ERROR
);
920 /* roundtrip or fallback mapping */
921 extData
->stage3
[i3
]=(uint16_t)i3b
;
922 extData
->stage3b
[i3b
]=value
;
927 generateFromUTrie(CnvExtData
*extData
, UCMTable
*table
, int32_t mapLength
) {
928 UCMapping
*mappings
, *m
;
931 int32_t subStart
, subLimit
;
940 mappings
=table
->mappings
;
941 map
=table
->reverseMap
;
944 * iterate over same-initial-code point mappings,
945 * enter the initial code point into the trie,
946 * and start a recursion on the corresponding mappings section
947 * with generateFromUTable()
950 codePoints
=UCM_GET_CODE_POINTS(table
, m
);
953 while(subLimit
<mapLength
) {
954 /* get a new subsection of mappings starting with the same code point */
957 while(next
==c
&& ++subLimit
<mapLength
) {
958 m
=mappings
+map
[subLimit
];
959 codePoints
=UCM_GET_CODE_POINTS(table
, m
);
964 * compute the value for this code point;
965 * if there is a mapping for this code point alone, it is at subStart
966 * because the table is sorted lexically
969 m
=mappings
+map
[subStart
];
970 codePoints
=UCM_GET_CODE_POINTS(table
, m
);
972 /* do not include this in generateFromUTable() */
975 if(subStart
<subLimit
&& mappings
[map
[subStart
]].uLen
==1) {
976 /* print error for multiple same-input-sequence mappings */
977 fprintf(stderr
, "error: multiple mappings from same Unicode code points\n");
978 ucm_printMapping(table
, m
, stderr
);
979 ucm_printMapping(table
, mappings
+map
[subStart
], stderr
);
983 value
=getFromUBytesValue(extData
, table
, m
);
986 if(subStart
==subLimit
) {
987 /* write the result for this one code point */
988 addFromUTrieEntry(extData
, c
, value
);
990 /* write the index to the subsection table */
991 addFromUTrieEntry(extData
, c
, (uint32_t)utm_countItems(extData
->fromUTableValues
));
993 /* recurse, starting from 16-bit-unit index 2, the first 16-bit unit after c */
994 if(!generateFromUTable(extData
, table
, subStart
, subLimit
, 2, value
)) {
1003 * Generate the fromU data structures from the input table.
1004 * The input table must be sorted, and all precision flags must be 0..3.
1005 * This function will modify the table's reverseMap.
1008 makeFromUTable(CnvExtData
*extData
, UCMTable
*table
) {
1010 int32_t i
, stage1Top
, fromUCount
;
1012 fromUCount
=prepareFromUMappings(table
);
1014 extData
->fromUTableUChars
=utm_open("cnv extension fromUTableUChars", 0x10000, UCNV_EXT_FROM_U_DATA_MASK
+1, 2);
1015 extData
->fromUTableValues
=utm_open("cnv extension fromUTableValues", 0x10000, UCNV_EXT_FROM_U_DATA_MASK
+1, 4);
1016 extData
->fromUBytes
=utm_open("cnv extension fromUBytes", 0x10000, UCNV_EXT_FROM_U_DATA_MASK
+1, 1);
1018 /* allocate all-unassigned stage blocks */
1019 extData
->stage2Top
=MBCS_STAGE_2_FIRST_ASSIGNED
;
1020 extData
->stage3Top
=MBCS_STAGE_3_FIRST_ASSIGNED
;
1023 * stage 3b stores only unique values, and in
1024 * index 0: 0 for "no mapping"
1025 * index 1: "no mapping" with preference for <subchar1> rather than <subchar>
1027 extData
->stage3b
[1]=UCNV_EXT_FROM_U_SUBCHAR1
;
1028 extData
->stage3bTop
=2;
1030 /* allocate the first entry in the fromUTable because index 0 means "no result" */
1031 utm_alloc(extData
->fromUTableUChars
);
1032 utm_alloc(extData
->fromUTableValues
);
1034 if(!generateFromUTrie(extData
, table
, fromUCount
)) {
1039 * offset the stage 1 trie entries by stage1Top because they will
1040 * be stored in a single array
1042 stage1
=extData
->stage1
;
1043 stage1Top
=extData
->stage1Top
;
1044 for(i
=0; i
<stage1Top
; ++i
) {
1045 stage1
[i
]=(uint16_t)(stage1
[i
]+stage1Top
);
1051 /* -------------------------------------------------------------------------- */
1054 CnvExtAddTable(NewConverter
*cnvData
, UCMTable
*table
, UConverterStaticData
*staticData
) {
1055 CnvExtData
*extData
;
1057 if(table
->unicodeMask
&UCNV_HAS_SURROGATES
) {
1058 fprintf(stderr
, "error: contains mappings for surrogate code points\n");
1062 staticData
->conversionType
=UCNV_MBCS
;
1064 extData
=(CnvExtData
*)cnvData
;
1067 * assume that the table is sorted
1069 * call the functions in this order because
1070 * makeToUTable() modifies the original reverseMap,
1071 * makeFromUTable() writes a whole new mapping into reverseMap
1074 makeToUTable(extData
, table
) &&
1075 makeFromUTable(extData
, table
);