• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 *   Copyright (C) 2003-2014, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 *******************************************************************************
10 *   file name:  gencnvex.c
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   created on: 2003oct12
16 *   created by: Markus W. Scherer
17 */
18 
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include "unicode/utypes.h"
22 #include "unicode/ustring.h"
23 #include "cstring.h"
24 #include "cmemory.h"
25 #include "ucnv_cnv.h"
26 #include "ucnvmbcs.h"
27 #include "toolutil.h"
28 #include "unewdata.h"
29 #include "ucm.h"
30 #include "makeconv.h"
31 #include "genmbcs.h"
32 
33 static void
34 CnvExtClose(NewConverter *cnvData);
35 
36 static UBool
37 CnvExtIsValid(NewConverter *cnvData,
38               const uint8_t *bytes, int32_t length);
39 
40 static UBool
41 CnvExtAddTable(NewConverter *cnvData, UCMTable *table, UConverterStaticData *staticData);
42 
43 static uint32_t
44 CnvExtWrite(NewConverter *cnvData, const UConverterStaticData *staticData,
45             UNewDataMemory *pData, int32_t tableType);
46 
47 typedef struct CnvExtData {
48     NewConverter newConverter;
49 
50     UCMFile *ucm;
51 
52     /* toUnicode (state table in ucm->states) */
53     UToolMemory *toUTable, *toUUChars;
54 
55     /* fromUnicode */
56     UToolMemory *fromUTableUChars, *fromUTableValues, *fromUBytes;
57 
58     uint16_t stage1[MBCS_STAGE_1_SIZE];
59     uint16_t stage2[MBCS_STAGE_2_SIZE];
60     uint16_t stage3[0x10000<<UCNV_EXT_STAGE_2_LEFT_SHIFT]; /* 0x10000 because of 16-bit stage 2/3 indexes */
61     uint32_t stage3b[0x10000];
62 
63     int32_t stage1Top, stage2Top, stage3Top, stage3bTop;
64 
65     /* for stage3 compaction of <subchar1> |2 mappings */
66     uint16_t stage3Sub1Block;
67 
68     /* statistics */
69     int32_t
70         maxInBytes, maxOutBytes, maxBytesPerUChar,
71         maxInUChars, maxOutUChars, maxUCharsPerByte;
72 } CnvExtData;
73 
74 NewConverter *
CnvExtOpen(UCMFile * ucm)75 CnvExtOpen(UCMFile *ucm) {
76     CnvExtData *extData;
77 
78     extData=(CnvExtData *)uprv_malloc(sizeof(CnvExtData));
79     if(extData==NULL) {
80         printf("out of memory\n");
81         exit(U_MEMORY_ALLOCATION_ERROR);
82     }
83     uprv_memset(extData, 0, sizeof(CnvExtData));
84 
85     extData->ucm=ucm; /* aliased, not owned */
86 
87     extData->newConverter.close=CnvExtClose;
88     extData->newConverter.isValid=CnvExtIsValid;
89     extData->newConverter.addTable=CnvExtAddTable;
90     extData->newConverter.write=CnvExtWrite;
91     return &extData->newConverter;
92 }
93 
94 static void
CnvExtClose(NewConverter * cnvData)95 CnvExtClose(NewConverter *cnvData) {
96     CnvExtData *extData=(CnvExtData *)cnvData;
97     if(extData!=NULL) {
98         utm_close(extData->toUTable);
99         utm_close(extData->toUUChars);
100         utm_close(extData->fromUTableUChars);
101         utm_close(extData->fromUTableValues);
102         utm_close(extData->fromUBytes);
103         uprv_free(extData);
104     }
105 }
106 
107 /* we do not expect this to be called */
108 static UBool
CnvExtIsValid(NewConverter * cnvData,const uint8_t * bytes,int32_t length)109 CnvExtIsValid(NewConverter *cnvData,
110         const uint8_t *bytes, int32_t length) {
111     // suppress compiler warnings about unused variables
112     (void)cnvData;
113     (void)bytes;
114     (void)length;
115     return false;
116 }
117 
118 static uint32_t
CnvExtWrite(NewConverter * cnvData,const UConverterStaticData * staticData,UNewDataMemory * pData,int32_t tableType)119 CnvExtWrite(NewConverter *cnvData, const UConverterStaticData *staticData,
120             UNewDataMemory *pData, int32_t tableType) {
121     (void) staticData; // suppress compiler warnings about unused variable
122     CnvExtData *extData=(CnvExtData *)cnvData;
123     int32_t length, top, headerSize;
124 
125     int32_t indexes[UCNV_EXT_INDEXES_MIN_LENGTH]={ 0 };
126 
127     if(tableType&TABLE_BASE) {
128         headerSize=0;
129     } else {
130         _MBCSHeader header={ { 0, 0, 0, 0 }, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
131 
132         /* write the header and base table name for an extension-only table */
133         length=(int32_t)uprv_strlen(extData->ucm->baseName)+1;
134         while(length&3) {
135             /* add padding */
136             extData->ucm->baseName[length++]=0;
137         }
138 
139         headerSize=MBCS_HEADER_V4_LENGTH*4+length;
140 
141         /* fill the header */
142         header.version[0]=4;
143         header.version[1]=2;
144         header.flags=(uint32_t)((headerSize<<8)|MBCS_OUTPUT_EXT_ONLY);
145 
146         /* write the header and the base table name */
147         udata_writeBlock(pData, &header, MBCS_HEADER_V4_LENGTH*4);
148         udata_writeBlock(pData, extData->ucm->baseName, length);
149     }
150 
151     /* fill indexes[] - offsets/indexes are in units of the target array */
152     top=0;
153 
154     indexes[UCNV_EXT_INDEXES_LENGTH]=length=UCNV_EXT_INDEXES_MIN_LENGTH;
155     top+=length*4;
156 
157     indexes[UCNV_EXT_TO_U_INDEX]=top;
158     indexes[UCNV_EXT_TO_U_LENGTH]=length=utm_countItems(extData->toUTable);
159     top+=length*4;
160 
161     indexes[UCNV_EXT_TO_U_UCHARS_INDEX]=top;
162     indexes[UCNV_EXT_TO_U_UCHARS_LENGTH]=length=utm_countItems(extData->toUUChars);
163     top+=length*2;
164 
165     indexes[UCNV_EXT_FROM_U_UCHARS_INDEX]=top;
166     length=utm_countItems(extData->fromUTableUChars);
167     top+=length*2;
168 
169     if(top&3) {
170         /* add padding */
171         *((UChar *)utm_alloc(extData->fromUTableUChars))=0;
172         *((uint32_t *)utm_alloc(extData->fromUTableValues))=0;
173         ++length;
174         top+=2;
175     }
176     indexes[UCNV_EXT_FROM_U_LENGTH]=length;
177 
178     indexes[UCNV_EXT_FROM_U_VALUES_INDEX]=top;
179     top+=length*4;
180 
181     indexes[UCNV_EXT_FROM_U_BYTES_INDEX]=top;
182     length=utm_countItems(extData->fromUBytes);
183     top+=length;
184 
185     if(top&1) {
186         /* add padding */
187         *((uint8_t *)utm_alloc(extData->fromUBytes))=0;
188         ++length;
189         ++top;
190     }
191     indexes[UCNV_EXT_FROM_U_BYTES_LENGTH]=length;
192 
193     indexes[UCNV_EXT_FROM_U_STAGE_12_INDEX]=top;
194     indexes[UCNV_EXT_FROM_U_STAGE_1_LENGTH]=length=extData->stage1Top;
195     indexes[UCNV_EXT_FROM_U_STAGE_12_LENGTH]=length+=extData->stage2Top;
196     top+=length*2;
197 
198     indexes[UCNV_EXT_FROM_U_STAGE_3_INDEX]=top;
199     length=extData->stage3Top;
200     top+=length*2;
201 
202     if(top&3) {
203         /* add padding */
204         extData->stage3[extData->stage3Top++]=0;
205         ++length;
206         top+=2;
207     }
208     indexes[UCNV_EXT_FROM_U_STAGE_3_LENGTH]=length;
209 
210     indexes[UCNV_EXT_FROM_U_STAGE_3B_INDEX]=top;
211     indexes[UCNV_EXT_FROM_U_STAGE_3B_LENGTH]=length=extData->stage3bTop;
212     top+=length*4;
213 
214     indexes[UCNV_EXT_SIZE]=top;
215 
216     /* statistics */
217     indexes[UCNV_EXT_COUNT_BYTES]=
218         (extData->maxInBytes<<16)|
219         (extData->maxOutBytes<<8)|
220         extData->maxBytesPerUChar;
221     indexes[UCNV_EXT_COUNT_UCHARS]=
222         (extData->maxInUChars<<16)|
223         (extData->maxOutUChars<<8)|
224         extData->maxUCharsPerByte;
225 
226     indexes[UCNV_EXT_FLAGS]=extData->ucm->ext->unicodeMask;
227 
228     /* write the extension data */
229     udata_writeBlock(pData, indexes, sizeof(indexes));
230     udata_writeBlock(pData, utm_getStart(extData->toUTable), indexes[UCNV_EXT_TO_U_LENGTH]*4);
231     udata_writeBlock(pData, utm_getStart(extData->toUUChars), indexes[UCNV_EXT_TO_U_UCHARS_LENGTH]*2);
232 
233     udata_writeBlock(pData, utm_getStart(extData->fromUTableUChars), indexes[UCNV_EXT_FROM_U_LENGTH]*2);
234     udata_writeBlock(pData, utm_getStart(extData->fromUTableValues), indexes[UCNV_EXT_FROM_U_LENGTH]*4);
235     udata_writeBlock(pData, utm_getStart(extData->fromUBytes), indexes[UCNV_EXT_FROM_U_BYTES_LENGTH]);
236 
237     udata_writeBlock(pData, extData->stage1, extData->stage1Top*2);
238     udata_writeBlock(pData, extData->stage2, extData->stage2Top*2);
239     udata_writeBlock(pData, extData->stage3, extData->stage3Top*2);
240     udata_writeBlock(pData, extData->stage3b, extData->stage3bTop*4);
241 
242 #if 0
243     {
244         int32_t i, j;
245 
246         length=extData->stage1Top;
247         printf("\nstage1[%x]:\n", length);
248 
249         for(i=0; i<length; ++i) {
250             if(extData->stage1[i]!=length) {
251                 printf("stage1[%04x]=%04x\n", i, extData->stage1[i]);
252             }
253         }
254 
255         j=length;
256         length=extData->stage2Top;
257         printf("\nstage2[%x]:\n", length);
258 
259         for(i=0; i<length; ++j, ++i) {
260             if(extData->stage2[i]!=0) {
261                 printf("stage12[%04x]=%04x\n", j, extData->stage2[i]);
262             }
263         }
264 
265         length=extData->stage3Top;
266         printf("\nstage3[%x]:\n", length);
267 
268         for(i=0; i<length; ++i) {
269             if(extData->stage3[i]!=0) {
270                 printf("stage3[%04x]=%04x\n", i, extData->stage3[i]);
271             }
272         }
273 
274         length=extData->stage3bTop;
275         printf("\nstage3b[%x]:\n", length);
276 
277         for(i=0; i<length; ++i) {
278             if(extData->stage3b[i]!=0) {
279                 printf("stage3b[%04x]=%08x\n", i, extData->stage3b[i]);
280             }
281         }
282     }
283 #endif
284 
285     if(VERBOSE) {
286         printf("size of extension data: %ld\n", (long)top);
287     }
288 
289     /* return the number of bytes that should have been written */
290     return (uint32_t)(headerSize+top);
291 }
292 
293 /* to Unicode --------------------------------------------------------------- */
294 
295 /*
296  * Remove fromUnicode fallbacks and SUB mappings which are irrelevant for
297  * the toUnicode table.
298  * This includes mappings with MBCS_FROM_U_EXT_FLAG which were suitable
299  * for the base toUnicode table but not for the base fromUnicode table.
300  * The table must be sorted.
301  * Modifies previous data in the reverseMap.
302  */
303 static int32_t
reduceToUMappings(UCMTable * table)304 reduceToUMappings(UCMTable *table) {
305     UCMapping *mappings;
306     int32_t *map;
307     int32_t i, j, count;
308     int8_t flag;
309 
310     mappings=table->mappings;
311     map=table->reverseMap;
312     count=table->mappingsLength;
313 
314     /* leave the map alone for the initial mappings with desired flags */
315     for(i=j=0; i<count; ++i) {
316         flag=mappings[map[i]].f;
317         if(flag!=0 && flag!=3) {
318             break;
319         }
320     }
321 
322     /* reduce from here to the rest */
323     for(j=i; i<count; ++i) {
324         flag=mappings[map[i]].f;
325         if(flag==0 || flag==3) {
326             map[j++]=map[i];
327         }
328     }
329 
330     return j;
331 }
332 
333 static uint32_t
getToUnicodeValue(CnvExtData * extData,UCMTable * table,UCMapping * m)334 getToUnicodeValue(CnvExtData *extData, UCMTable *table, UCMapping *m) {
335     UChar32 *u32;
336     UChar *u;
337     uint32_t value;
338     int32_t u16Length, ratio;
339     UErrorCode errorCode;
340 
341     /* write the Unicode result code point or string index */
342     if(m->uLen==1) {
343         u16Length=U16_LENGTH(m->u);
344         value=(uint32_t)(UCNV_EXT_TO_U_MIN_CODE_POINT+m->u);
345     } else {
346         /* the parser enforces m->uLen<=UCNV_EXT_MAX_UCHARS */
347 
348         /* get the result code point string and its 16-bit string length */
349         u32=UCM_GET_CODE_POINTS(table, m);
350         errorCode=U_ZERO_ERROR;
351         u_strFromUTF32(NULL, 0, &u16Length, u32, m->uLen, &errorCode);
352         if(U_FAILURE(errorCode) && errorCode!=U_BUFFER_OVERFLOW_ERROR) {
353             exit(errorCode);
354         }
355 
356         /* allocate it and put its length and index into the value */
357         value=
358             (((uint32_t)u16Length+UCNV_EXT_TO_U_LENGTH_OFFSET)<<UCNV_EXT_TO_U_LENGTH_SHIFT)|
359             ((uint32_t)utm_countItems(extData->toUUChars));
360         u=utm_allocN(extData->toUUChars, u16Length);
361 
362         /* write the result 16-bit string */
363         errorCode=U_ZERO_ERROR;
364         u_strFromUTF32(u, u16Length, NULL, u32, m->uLen, &errorCode);
365         if(U_FAILURE(errorCode) && errorCode!=U_BUFFER_OVERFLOW_ERROR) {
366             exit(errorCode);
367         }
368     }
369     if(m->f==0) {
370         value|=UCNV_EXT_TO_U_ROUNDTRIP_FLAG;
371     }
372 
373     /* update statistics */
374     if(m->bLen>extData->maxInBytes) {
375         extData->maxInBytes=m->bLen;
376     }
377     if(u16Length>extData->maxOutUChars) {
378         extData->maxOutUChars=u16Length;
379     }
380 
381     ratio=(u16Length+(m->bLen-1))/m->bLen;
382     if(ratio>extData->maxUCharsPerByte) {
383         extData->maxUCharsPerByte=ratio;
384     }
385 
386     return value;
387 }
388 
389 /*
390  * Recursive toUTable generator core function.
391  * Preconditions:
392  * - start<limit (There is at least one mapping.)
393  * - The mappings are sorted lexically. (Access is through the reverseMap.)
394  * - All mappings between start and limit have input sequences that share
395  *   the same prefix of unitIndex length, and therefore all of these sequences
396  *   are at least unitIndex+1 long.
397  * - There are only relevant mappings available through the reverseMap,
398  *   see reduceToUMappings().
399  *
400  * One function invocation generates one section table.
401  *
402  * Steps:
403  * 1. Count the number of unique unit values and get the low/high unit values
404  *    that occur at unitIndex.
405  * 2. Allocate the section table with possible optimization for linear access.
406  * 3. Write temporary version of the section table with start indexes of
407  *    subsections, each corresponding to one unit value at unitIndex.
408  * 4. Iterate through the table once more, and depending on the subsection length:
409  *    0: write 0 as a result value (unused byte in linear-access section table)
410  *   >0: if there is one mapping with an input unit sequence of unitIndex+1
411  *       then defaultValue=compute the mapping result for this whole sequence
412  *       else defaultValue=0
413  *
414  *       recurse into the subsection
415  */
416 static UBool
generateToUTable(CnvExtData * extData,UCMTable * table,int32_t start,int32_t limit,int32_t unitIndex,uint32_t defaultValue)417 generateToUTable(CnvExtData *extData, UCMTable *table,
418                  int32_t start, int32_t limit, int32_t unitIndex,
419                  uint32_t defaultValue) {
420     UCMapping *mappings, *m;
421     int32_t *map;
422     int32_t i, j, uniqueCount, count, subStart, subLimit;
423 
424     uint8_t *bytes;
425     int32_t low, high, prev;
426 
427     uint32_t *section;
428 
429     mappings=table->mappings;
430     map=table->reverseMap;
431 
432     /* step 1: examine the input units; set low, high, uniqueCount */
433     m=mappings+map[start];
434     bytes=UCM_GET_BYTES(table, m);
435     low=bytes[unitIndex];
436     uniqueCount=1;
437 
438     prev=high=low;
439     for(i=start+1; i<limit; ++i) {
440         m=mappings+map[i];
441         bytes=UCM_GET_BYTES(table, m);
442         high=bytes[unitIndex];
443 
444         if(high!=prev) {
445             prev=high;
446             ++uniqueCount;
447         }
448     }
449 
450     /* step 2: allocate the section; set count, section */
451     count=(high-low)+1;
452     if(count<0x100 && (unitIndex==0 || uniqueCount>=(3*count)/4)) {
453         /*
454          * for the root table and for fairly full tables:
455          * allocate for direct, linear array access
456          * by keeping count, to write an entry for each unit value
457          * from low to high
458          * exception: use a compact table if count==0x100 because
459          * that cannot be encoded in the length byte
460          */
461     } else {
462         count=uniqueCount;
463     }
464 
465     if(count>=0x100) {
466         fprintf(stderr, "error: toUnicode extension table section overflow: %ld section entries\n", (long)count);
467         return false;
468     }
469 
470     /* allocate the section: 1 entry for the header + count for the items */
471     section=(uint32_t *)utm_allocN(extData->toUTable, 1+count);
472 
473     /* write the section header */
474     *section++=((uint32_t)count<<UCNV_EXT_TO_U_BYTE_SHIFT)|defaultValue;
475 
476     /* step 3: write temporary section table with subsection starts */
477     prev=low-1; /* just before low to prevent empty subsections before low */
478     j=0; /* section table index */
479     for(i=start; i<limit; ++i) {
480         m=mappings+map[i];
481         bytes=UCM_GET_BYTES(table, m);
482         high=bytes[unitIndex];
483 
484         if(high!=prev) {
485             /* start of a new subsection for unit high */
486             if(count>uniqueCount) {
487                 /* write empty subsections for unused units in a linear table */
488                 while(++prev<high) {
489                     section[j++]=((uint32_t)prev<<UCNV_EXT_TO_U_BYTE_SHIFT)|(uint32_t)i;
490                 }
491             } else {
492                 prev=high;
493             }
494 
495             /* write the entry with the subsection start */
496             section[j++]=((uint32_t)high<<UCNV_EXT_TO_U_BYTE_SHIFT)|(uint32_t)i;
497         }
498     }
499     /* assert(j==count) */
500 
501     /* step 4: recurse and write results */
502     subLimit=UCNV_EXT_TO_U_GET_VALUE(section[0]);
503     for(j=0; j<count; ++j) {
504         subStart=subLimit;
505         subLimit= (j+1)<count ? UCNV_EXT_TO_U_GET_VALUE(section[j+1]) : limit;
506 
507         /* remove the subStart temporary value */
508         section[j]&=~UCNV_EXT_TO_U_VALUE_MASK;
509 
510         if(subStart==subLimit) {
511             /* leave the value zero: empty subsection for unused unit in a linear table */
512             continue;
513         }
514 
515         /* see if there is exactly one input unit sequence of length unitIndex+1 */
516         defaultValue=0;
517         m=mappings+map[subStart];
518         if(m->bLen==unitIndex+1) {
519             /* do not include this in generateToUTable() */
520             ++subStart;
521 
522             if(subStart<subLimit && mappings[map[subStart]].bLen==unitIndex+1) {
523                 /* print error for multiple same-input-sequence mappings */
524                 fprintf(stderr, "error: multiple mappings from same bytes\n");
525                 ucm_printMapping(table, m, stderr);
526                 ucm_printMapping(table, mappings+map[subStart], stderr);
527                 return false;
528             }
529 
530             defaultValue=getToUnicodeValue(extData, table, m);
531         }
532 
533         if(subStart==subLimit) {
534             /* write the result for the input sequence ending here */
535             section[j]|=defaultValue;
536         } else {
537             /* write the index to the subsection table */
538             section[j]|=(uint32_t)utm_countItems(extData->toUTable);
539 
540             /* recurse */
541             if(!generateToUTable(extData, table, subStart, subLimit, unitIndex+1, defaultValue)) {
542                 return false;
543             }
544         }
545     }
546     return true;
547 }
548 
549 /*
550  * Generate the toUTable and toUUChars from the input table.
551  * The input table must be sorted, and all precision flags must be 0..3.
552  * This function will modify the table's reverseMap.
553  */
554 static UBool
makeToUTable(CnvExtData * extData,UCMTable * table)555 makeToUTable(CnvExtData *extData, UCMTable *table) {
556     int32_t toUCount;
557 
558     toUCount=reduceToUMappings(table);
559 
560     extData->toUTable=utm_open("cnv extension toUTable", 0x10000, UCNV_EXT_TO_U_MIN_CODE_POINT, 4);
561     extData->toUUChars=utm_open("cnv extension toUUChars", 0x10000, UCNV_EXT_TO_U_INDEX_MASK+1, 2);
562 
563     return generateToUTable(extData, table, 0, toUCount, 0, 0);
564 }
565 
566 /* from Unicode ------------------------------------------------------------- */
567 
568 /*
569  * preprocessing:
570  * rebuild reverseMap with mapping indexes for mappings relevant for from Unicode
571  * change each Unicode string to encode all but the first code point in 16-bit form
572  *
573  * generation:
574  * for each unique code point
575  *   write an entry in the 3-stage trie
576  *   check that there is only one single-code point sequence
577  *   start recursion for following 16-bit input units
578  */
579 
580 /*
581  * Remove toUnicode fallbacks and non-<subchar1> SUB mappings
582  * which are irrelevant for the fromUnicode extension table.
583  * Remove MBCS_FROM_U_EXT_FLAG bits.
584  * Overwrite the reverseMap with an index array to the relevant mappings.
585  * Modify the code point sequences to a generator-friendly format where
586  * the first code points remains unchanged but the following are recoded
587  * into 16-bit Unicode string form.
588  * The table must be sorted.
589  * Destroys previous data in the reverseMap.
590  */
591 static int32_t
prepareFromUMappings(UCMTable * table)592 prepareFromUMappings(UCMTable *table) {
593     UCMapping *mappings, *m;
594     int32_t *map;
595     int32_t i, j, count;
596     int8_t flag;
597 
598     mappings=table->mappings;
599     map=table->reverseMap;
600     count=table->mappingsLength;
601 
602     /*
603      * we do not go through the map on input because the mappings are
604      * sorted lexically
605      */
606     m=mappings;
607 
608     for(i=j=0; i<count; ++m, ++i) {
609         flag=m->f;
610         if(flag>=0) {
611             flag&=MBCS_FROM_U_EXT_MASK;
612             m->f=flag;
613         }
614         if(flag==0 || flag==1 || (flag==2 && m->bLen==1) || flag==4) {
615             map[j++]=i;
616 
617             if(m->uLen>1) {
618                 /* recode all but the first code point to 16-bit Unicode */
619                 UChar32 *u32;
620                 UChar *u;
621                 UChar32 c;
622                 int32_t q, r;
623 
624                 u32=UCM_GET_CODE_POINTS(table, m);
625                 u=(UChar *)u32; /* destructive in-place recoding */
626                 for(r=2, q=1; q<m->uLen; ++q) {
627                     c=u32[q];
628                     U16_APPEND_UNSAFE(u, r, c);
629                 }
630 
631                 /* counts the first code point always at 2 - the first 16-bit unit is at 16-bit index 2 */
632                 m->uLen=(int8_t)r;
633             }
634         }
635     }
636 
637     return j;
638 }
639 
640 static uint32_t
getFromUBytesValue(CnvExtData * extData,UCMTable * table,UCMapping * m)641 getFromUBytesValue(CnvExtData *extData, UCMTable *table, UCMapping *m) {
642     uint8_t *bytes, *resultBytes;
643     uint32_t value;
644     int32_t u16Length, ratio;
645 
646     if(m->f==2) {
647         /*
648          * no mapping, <subchar1> preferred
649          *
650          * no need to count in statistics because the subchars are already
651          * counted for maxOutBytes and maxBytesPerUChar in UConverterStaticData,
652          * and this non-mapping does not count for maxInUChars which are always
653          * trivially at least two if counting unmappable supplementary code points
654          */
655         return UCNV_EXT_FROM_U_SUBCHAR1;
656     }
657 
658     bytes=UCM_GET_BYTES(table, m);
659     value=0;
660     switch(m->bLen) {
661         /* 1..3: store the bytes in the value word */
662     case 3:
663         value=((uint32_t)*bytes++)<<16;
664     case 2:
665         value|=((uint32_t)*bytes++)<<8;
666     case 1:
667         value|=*bytes;
668         break;
669     default:
670         /* the parser enforces m->bLen<=UCNV_EXT_MAX_BYTES */
671         /* store the bytes in fromUBytes[] and the index in the value word */
672         value=(uint32_t)utm_countItems(extData->fromUBytes);
673         resultBytes=utm_allocN(extData->fromUBytes, m->bLen);
674         uprv_memcpy(resultBytes, bytes, m->bLen);
675         break;
676     }
677     value|=(uint32_t)m->bLen<<UCNV_EXT_FROM_U_LENGTH_SHIFT;
678     if(m->f==0) {
679         value|=UCNV_EXT_FROM_U_ROUNDTRIP_FLAG;
680     } else if(m->f==4) {
681         value|=UCNV_EXT_FROM_U_GOOD_ONE_WAY_FLAG;
682     }
683 
684     /* calculate the real UTF-16 length (see recoding in prepareFromUMappings()) */
685     if(m->uLen==1) {
686         u16Length=U16_LENGTH(m->u);
687     } else {
688         u16Length=U16_LENGTH(UCM_GET_CODE_POINTS(table, m)[0])+(m->uLen-2);
689     }
690 
691     /* update statistics */
692     if(u16Length>extData->maxInUChars) {
693         extData->maxInUChars=u16Length;
694     }
695     if(m->bLen>extData->maxOutBytes) {
696         extData->maxOutBytes=m->bLen;
697     }
698 
699     ratio=(m->bLen+(u16Length-1))/u16Length;
700     if(ratio>extData->maxBytesPerUChar) {
701         extData->maxBytesPerUChar=ratio;
702     }
703 
704     return value;
705 }
706 
707 /*
708  * works like generateToUTable(), except that the
709  * output section consists of two arrays, one for input UChars and one
710  * for result values
711  *
712  * also, fromUTable sections are always stored in a compact form for
713  * access via binary search
714  */
715 static UBool
generateFromUTable(CnvExtData * extData,UCMTable * table,int32_t start,int32_t limit,int32_t unitIndex,uint32_t defaultValue)716 generateFromUTable(CnvExtData *extData, UCMTable *table,
717                    int32_t start, int32_t limit, int32_t unitIndex,
718                    uint32_t defaultValue) {
719     UCMapping *mappings, *m;
720     int32_t *map;
721     int32_t i, j, uniqueCount, count, subStart, subLimit;
722 
723     UChar *uchars;
724     UChar32 low, high, prev;
725 
726     UChar *sectionUChars;
727     uint32_t *sectionValues;
728 
729     mappings=table->mappings;
730     map=table->reverseMap;
731 
732     /* step 1: examine the input units; set low, high, uniqueCount */
733     m=mappings+map[start];
734     uchars=(UChar *)UCM_GET_CODE_POINTS(table, m);
735     low=uchars[unitIndex];
736     uniqueCount=1;
737 
738     prev=high=low;
739     for(i=start+1; i<limit; ++i) {
740         m=mappings+map[i];
741         uchars=(UChar *)UCM_GET_CODE_POINTS(table, m);
742         high=uchars[unitIndex];
743 
744         if(high!=prev) {
745             prev=high;
746             ++uniqueCount;
747         }
748     }
749 
750     /* step 2: allocate the section; set count, section */
751     /* the fromUTable always stores for access via binary search */
752     count=uniqueCount;
753 
754     /* allocate the section: 1 entry for the header + count for the items */
755     sectionUChars=(UChar *)utm_allocN(extData->fromUTableUChars, 1+count);
756     sectionValues=(uint32_t *)utm_allocN(extData->fromUTableValues, 1+count);
757 
758     /* write the section header */
759     *sectionUChars++=(UChar)count;
760     *sectionValues++=defaultValue;
761 
762     /* step 3: write temporary section table with subsection starts */
763     prev=low-1; /* just before low to prevent empty subsections before low */
764     j=0; /* section table index */
765     for(i=start; i<limit; ++i) {
766         m=mappings+map[i];
767         uchars=(UChar *)UCM_GET_CODE_POINTS(table, m);
768         high=uchars[unitIndex];
769 
770         if(high!=prev) {
771             /* start of a new subsection for unit high */
772             prev=high;
773 
774             /* write the entry with the subsection start */
775             sectionUChars[j]=(UChar)high;
776             sectionValues[j]=(uint32_t)i;
777             ++j;
778         }
779     }
780     /* assert(j==count) */
781 
782     /* step 4: recurse and write results */
783     subLimit=(int32_t)(sectionValues[0]);
784     for(j=0; j<count; ++j) {
785         subStart=subLimit;
786         subLimit= (j+1)<count ? (int32_t)(sectionValues[j+1]) : limit;
787 
788         /* see if there is exactly one input unit sequence of length unitIndex+1 */
789         defaultValue=0;
790         m=mappings+map[subStart];
791         if(m->uLen==unitIndex+1) {
792             /* do not include this in generateToUTable() */
793             ++subStart;
794 
795             if(subStart<subLimit && mappings[map[subStart]].uLen==unitIndex+1) {
796                 /* print error for multiple same-input-sequence mappings */
797                 fprintf(stderr, "error: multiple mappings from same Unicode code points\n");
798                 ucm_printMapping(table, m, stderr);
799                 ucm_printMapping(table, mappings+map[subStart], stderr);
800                 return false;
801             }
802 
803             defaultValue=getFromUBytesValue(extData, table, m);
804         }
805 
806         if(subStart==subLimit) {
807             /* write the result for the input sequence ending here */
808             sectionValues[j]=defaultValue;
809         } else {
810             /* write the index to the subsection table */
811             sectionValues[j]=(uint32_t)utm_countItems(extData->fromUTableValues);
812 
813             /* recurse */
814             if(!generateFromUTable(extData, table, subStart, subLimit, unitIndex+1, defaultValue)) {
815                 return false;
816             }
817         }
818     }
819     return true;
820 }
821 
822 /*
823  * add entries to the fromUnicode trie,
824  * assume to be called with code points in ascending order
825  * and use that to build the trie in precompacted form
826  */
827 static void
addFromUTrieEntry(CnvExtData * extData,UChar32 c,uint32_t value)828 addFromUTrieEntry(CnvExtData *extData, UChar32 c, uint32_t value) {
829     int32_t i1, i2, i3, i3b, nextOffset, min, newBlock;
830 
831     if(value==0) {
832         return;
833     }
834 
835     /*
836      * compute the index for each stage,
837      * allocate a stage block if necessary,
838      * and write the stage value
839      */
840     i1=c>>10;
841     if(i1>=extData->stage1Top) {
842         extData->stage1Top=i1+1;
843     }
844 
845     nextOffset=(c>>4)&0x3f;
846 
847     if(extData->stage1[i1]==0) {
848         /* allocate another block in stage 2; overlap with the previous block */
849         newBlock=extData->stage2Top;
850         min=newBlock-nextOffset; /* minimum block start with overlap */
851         while(min<newBlock && extData->stage2[newBlock-1]==0) {
852             --newBlock;
853         }
854 
855         extData->stage1[i1]=(uint16_t)newBlock;
856         extData->stage2Top=newBlock+MBCS_STAGE_2_BLOCK_SIZE;
857         if(extData->stage2Top>UPRV_LENGTHOF(extData->stage2)) {
858             fprintf(stderr, "error: too many stage 2 entries at U+%04x\n", (int)c);
859             exit(U_MEMORY_ALLOCATION_ERROR);
860         }
861     }
862 
863     i2=extData->stage1[i1]+nextOffset;
864     nextOffset=c&0xf;
865 
866     if(extData->stage2[i2]==0) {
867         /* allocate another block in stage 3; overlap with the previous block */
868         newBlock=extData->stage3Top;
869         min=newBlock-nextOffset; /* minimum block start with overlap */
870         while(min<newBlock && extData->stage3[newBlock-1]==0) {
871             --newBlock;
872         }
873 
874         /* round up to a multiple of stage 3 granularity >1 (similar to utrie.c) */
875         newBlock=(newBlock+(UCNV_EXT_STAGE_3_GRANULARITY-1))&~(UCNV_EXT_STAGE_3_GRANULARITY-1);
876         extData->stage2[i2]=(uint16_t)(newBlock>>UCNV_EXT_STAGE_2_LEFT_SHIFT);
877 
878         extData->stage3Top=newBlock+MBCS_STAGE_3_BLOCK_SIZE;
879         if(extData->stage3Top>UPRV_LENGTHOF(extData->stage3)) {
880             fprintf(stderr, "error: too many stage 3 entries at U+%04x\n", (int)c);
881             exit(U_MEMORY_ALLOCATION_ERROR);
882         }
883     }
884 
885     i3=((int32_t)extData->stage2[i2]<<UCNV_EXT_STAGE_2_LEFT_SHIFT)+nextOffset;
886     /*
887      * assume extData->stage3[i3]==0 because we get
888      * code points in strictly ascending order
889      */
890 
891     if(value==UCNV_EXT_FROM_U_SUBCHAR1) {
892         /* <subchar1> SUB mapping, see getFromUBytesValue() and prepareFromUMappings() */
893         extData->stage3[i3]=1;
894 
895         /*
896          * precompaction is not optimal for <subchar1> |2 mappings because
897          * stage3 values for them are all the same, unlike for other mappings
898          * which all have unique values;
899          * use a simple compaction of reusing a whole block filled with these
900          * mappings
901          */
902 
903         /* is the entire block filled with <subchar1> |2 mappings? */
904         if(nextOffset==MBCS_STAGE_3_BLOCK_SIZE-1) {
905             for(min=i3-nextOffset;
906                 min<i3 && extData->stage3[min]==1;
907                 ++min) {}
908 
909             if(min==i3) {
910                 /* the entire block is filled with these mappings */
911                 if(extData->stage3Sub1Block!=0) {
912                     /* point to the previous such block and remove this block from stage3 */
913                     extData->stage2[i2]=extData->stage3Sub1Block;
914                     extData->stage3Top-=MBCS_STAGE_3_BLOCK_SIZE;
915                     uprv_memset(extData->stage3+extData->stage3Top, 0, MBCS_STAGE_3_BLOCK_SIZE*2);
916                 } else {
917                     /* remember this block's stage2 entry */
918                     extData->stage3Sub1Block=extData->stage2[i2];
919                 }
920             }
921         }
922     } else {
923         if((i3b=extData->stage3bTop++)>=UPRV_LENGTHOF(extData->stage3b)) {
924             fprintf(stderr, "error: too many stage 3b entries at U+%04x\n", (int)c);
925             exit(U_MEMORY_ALLOCATION_ERROR);
926         }
927 
928         /* roundtrip or fallback mapping */
929         extData->stage3[i3]=(uint16_t)i3b;
930         extData->stage3b[i3b]=value;
931     }
932 }
933 
934 static UBool
generateFromUTrie(CnvExtData * extData,UCMTable * table,int32_t mapLength)935 generateFromUTrie(CnvExtData *extData, UCMTable *table, int32_t mapLength) {
936     UCMapping *mappings, *m;
937     int32_t *map;
938     uint32_t value;
939     int32_t subStart, subLimit;
940 
941     UChar32 *codePoints;
942     UChar32 c, next;
943 
944     if(mapLength==0) {
945         return true;
946     }
947 
948     mappings=table->mappings;
949     map=table->reverseMap;
950 
951     /*
952      * iterate over same-initial-code point mappings,
953      * enter the initial code point into the trie,
954      * and start a recursion on the corresponding mappings section
955      * with generateFromUTable()
956      */
957     m=mappings+map[0];
958     codePoints=UCM_GET_CODE_POINTS(table, m);
959     next=codePoints[0];
960     subLimit=0;
961     while(subLimit<mapLength) {
962         /* get a new subsection of mappings starting with the same code point */
963         subStart=subLimit;
964         c=next;
965         while(next==c && ++subLimit<mapLength) {
966             m=mappings+map[subLimit];
967             codePoints=UCM_GET_CODE_POINTS(table, m);
968             next=codePoints[0];
969         }
970 
971         /*
972          * compute the value for this code point;
973          * if there is a mapping for this code point alone, it is at subStart
974          * because the table is sorted lexically
975          */
976         value=0;
977         m=mappings+map[subStart];
978         codePoints=UCM_GET_CODE_POINTS(table, m);
979         if(m->uLen==1) {
980             /* do not include this in generateFromUTable() */
981             ++subStart;
982 
983             if(subStart<subLimit && mappings[map[subStart]].uLen==1) {
984                 /* print error for multiple same-input-sequence mappings */
985                 fprintf(stderr, "error: multiple mappings from same Unicode code points\n");
986                 ucm_printMapping(table, m, stderr);
987                 ucm_printMapping(table, mappings+map[subStart], stderr);
988                 return false;
989             }
990 
991             value=getFromUBytesValue(extData, table, m);
992         }
993 
994         if(subStart==subLimit) {
995             /* write the result for this one code point */
996             addFromUTrieEntry(extData, c, value);
997         } else {
998             /* write the index to the subsection table */
999             addFromUTrieEntry(extData, c, (uint32_t)utm_countItems(extData->fromUTableValues));
1000 
1001             /* recurse, starting from 16-bit-unit index 2, the first 16-bit unit after c */
1002             if(!generateFromUTable(extData, table, subStart, subLimit, 2, value)) {
1003                 return false;
1004             }
1005         }
1006     }
1007     return true;
1008 }
1009 
1010 /*
1011  * Generate the fromU data structures from the input table.
1012  * The input table must be sorted, and all precision flags must be 0..3.
1013  * This function will modify the table's reverseMap.
1014  */
1015 static UBool
makeFromUTable(CnvExtData * extData,UCMTable * table)1016 makeFromUTable(CnvExtData *extData, UCMTable *table) {
1017     uint16_t *stage1;
1018     int32_t i, stage1Top, fromUCount;
1019 
1020     fromUCount=prepareFromUMappings(table);
1021 
1022     extData->fromUTableUChars=utm_open("cnv extension fromUTableUChars", 0x10000, UCNV_EXT_FROM_U_DATA_MASK+1, 2);
1023     extData->fromUTableValues=utm_open("cnv extension fromUTableValues", 0x10000, UCNV_EXT_FROM_U_DATA_MASK+1, 4);
1024     extData->fromUBytes=utm_open("cnv extension fromUBytes", 0x10000, UCNV_EXT_FROM_U_DATA_MASK+1, 1);
1025 
1026     /* allocate all-unassigned stage blocks */
1027     extData->stage2Top=MBCS_STAGE_2_FIRST_ASSIGNED;
1028     extData->stage3Top=MBCS_STAGE_3_FIRST_ASSIGNED;
1029 
1030     /*
1031      * stage 3b stores only unique values, and in
1032      * index 0: 0 for "no mapping"
1033      * index 1: "no mapping" with preference for <subchar1> rather than <subchar>
1034      */
1035     extData->stage3b[1]=UCNV_EXT_FROM_U_SUBCHAR1;
1036     extData->stage3bTop=2;
1037 
1038     /* allocate the first entry in the fromUTable because index 0 means "no result" */
1039     utm_alloc(extData->fromUTableUChars);
1040     utm_alloc(extData->fromUTableValues);
1041 
1042     if(!generateFromUTrie(extData, table, fromUCount)) {
1043         return false;
1044     }
1045 
1046     /*
1047      * offset the stage 1 trie entries by stage1Top because they will
1048      * be stored in a single array
1049      */
1050     stage1=extData->stage1;
1051     stage1Top=extData->stage1Top;
1052     for(i=0; i<stage1Top; ++i) {
1053         stage1[i]=(uint16_t)(stage1[i]+stage1Top);
1054     }
1055 
1056     return true;
1057 }
1058 
1059 /* -------------------------------------------------------------------------- */
1060 
1061 static UBool
CnvExtAddTable(NewConverter * cnvData,UCMTable * table,UConverterStaticData * staticData)1062 CnvExtAddTable(NewConverter *cnvData, UCMTable *table, UConverterStaticData *staticData) {
1063     CnvExtData *extData;
1064 
1065     if(table->unicodeMask&UCNV_HAS_SURROGATES) {
1066         fprintf(stderr, "error: contains mappings for surrogate code points\n");
1067         return false;
1068     }
1069 
1070     staticData->conversionType=UCNV_MBCS;
1071 
1072     extData=(CnvExtData *)cnvData;
1073 
1074     /*
1075      * assume that the table is sorted
1076      *
1077      * call the functions in this order because
1078      * makeToUTable() modifies the original reverseMap,
1079      * makeFromUTable() writes a whole new mapping into reverseMap
1080      */
1081     return
1082         makeToUTable(extData, table) &&
1083         makeFromUTable(extData, table);
1084 }
1085