1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2003-2013, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
12 * tab size: 8 (not used)
15 * created on: 2003jun20
16 * created by: Markus W. Scherer
18 * This file reads a .ucm file, stores its mappings and sorts them.
19 * It implements handling of Unicode conversion mappings from .ucm files
20 * for makeconv, canonucm, rptp2ucm, etc.
22 * Unicode code point sequences with a length of more than 1,
23 * as well as byte sequences with more than 4 bytes or more than one complete
24 * character sequence are handled to support m:n mappings.
27 #include "unicode/utypes.h"
28 #include "unicode/ustring.h"
40 #if !UCONFIG_NO_CONVERSION
42 /* -------------------------------------------------------------------------- */
45 printMapping(UCMapping *m, UChar32 *codePoints, uint8_t *bytes, FILE *f) {
48 for(j=0; j<m->uLen; ++j) {
49 fprintf(f, "<U%04lX>", (long)codePoints[j]);
54 for(j=0; j<m->bLen; ++j) {
55 fprintf(f, "\\x%02X", bytes[j]);
59 fprintf(f, " |%u\n", m->f);
66 ucm_printMapping(UCMTable *table, UCMapping *m, FILE *f) {
67 printMapping(m, UCM_GET_CODE_POINTS(table, m), UCM_GET_BYTES(table, m), f);
71 ucm_printTable(UCMTable *table, FILE *f, UBool byUnicode) {
76 length=table->mappingsLength;
78 for(i=0; i<length; ++m, ++i) {
79 ucm_printMapping(table, m, f);
82 const int32_t *map=table->reverseMap;
83 for(i=0; i<length; ++i) {
84 ucm_printMapping(table, m+map[i], f);
89 /* mapping comparisons ------------------------------------------------------ */
92 compareUnicode(UCMTable *lTable, const UCMapping *l,
93 UCMTable *rTable, const UCMapping *r) {
94 const UChar32 *lu, *ru;
95 int32_t result, i, length;
97 if(l->uLen==1 && r->uLen==1) {
98 /* compare two single code points */
102 /* get pointers to the code point sequences */
103 lu=UCM_GET_CODE_POINTS(lTable, l);
104 ru=UCM_GET_CODE_POINTS(rTable, r);
106 /* get the minimum length */
107 if(l->uLen<=r->uLen) {
113 /* compare the code points */
114 for(i=0; i<length; ++i) {
121 /* compare the lengths */
122 return l->uLen-r->uLen;
126 compareBytes(UCMTable *lTable, const UCMapping *l,
127 UCMTable *rTable, const UCMapping *r,
129 const uint8_t *lb, *rb;
130 int32_t result, i, length;
133 * A lexical comparison is used for sorting in the builder, to allow
134 * an efficient search for a byte sequence that could be a prefix
135 * of a previously entered byte sequence.
137 * Comparing by lengths first is for compatibility with old .ucm tools
138 * like canonucm and rptp2ucm.
141 /* get the minimum length and continue */
142 if(l->bLen<=r->bLen) {
148 /* compare lengths first */
149 result=l->bLen-r->bLen;
157 /* get pointers to the byte sequences */
158 lb=UCM_GET_BYTES(lTable, l);
159 rb=UCM_GET_BYTES(rTable, r);
161 /* compare the bytes */
162 for(i=0; i<length; ++i) {
169 /* compare the lengths */
170 return l->bLen-r->bLen;
173 /* compare UCMappings for sorting */
175 compareMappings(UCMTable *lTable, const UCMapping *l,
176 UCMTable *rTable, const UCMapping *r,
180 /* choose which side to compare first */
182 /* Unicode then bytes */
183 result=compareUnicode(lTable, l, rTable, r);
185 result=compareBytes(lTable, l, rTable, r, FALSE); /* not lexically, like canonucm */
188 /* bytes then Unicode */
189 result=compareBytes(lTable, l, rTable, r, TRUE); /* lexically, for builder */
191 result=compareUnicode(lTable, l, rTable, r);
199 /* compare the flags */
203 /* sorting by Unicode first sorts mappings directly */
205 compareMappingsUnicodeFirst(const void *context, const void *left, const void *right) {
206 return compareMappings(
207 (UCMTable *)context, (const UCMapping *)left,
208 (UCMTable *)context, (const UCMapping *)right, TRUE);
211 /* sorting by bytes first sorts the reverseMap; use indirection to mappings */
213 compareMappingsBytesFirst(const void *context, const void *left, const void *right) {
214 UCMTable *table=(UCMTable *)context;
215 int32_t l=*(const int32_t *)left, r=*(const int32_t *)right;
216 return compareMappings(
217 table, table->mappings+l,
218 table, table->mappings+r, FALSE);
221 U_CAPI void U_EXPORT2
222 ucm_sortTable(UCMTable *t) {
223 UErrorCode errorCode;
230 errorCode=U_ZERO_ERROR;
232 /* 1. sort by Unicode first */
233 uprv_sortArray(t->mappings, t->mappingsLength, sizeof(UCMapping),
234 compareMappingsUnicodeFirst, t,
237 /* build the reverseMap */
238 if(t->reverseMap==NULL) {
240 * allocate mappingsCapacity instead of mappingsLength so that
241 * if mappings are added, the reverseMap need not be
242 * reallocated each time
243 * (see ucm_moveMappings() and ucm_addMapping())
245 t->reverseMap=(int32_t *)uprv_malloc(t->mappingsCapacity*sizeof(int32_t));
246 if(t->reverseMap==NULL) {
247 fprintf(stderr, "ucm error: unable to allocate reverseMap\n");
248 exit(U_MEMORY_ALLOCATION_ERROR);
251 for(i=0; i<t->mappingsLength; ++i) {
255 /* 2. sort reverseMap by mappings bytes first */
256 uprv_sortArray(t->reverseMap, t->mappingsLength, sizeof(int32_t),
257 compareMappingsBytesFirst, t,
260 if(U_FAILURE(errorCode)) {
261 fprintf(stderr, "ucm error: sortTable()/uprv_sortArray() fails - %s\n",
262 u_errorName(errorCode));
270 * remove mappings with their move flag set from the base table
271 * and move some of them (with UCM_MOVE_TO_EXT) to the extension table
273 U_CAPI void U_EXPORT2
274 ucm_moveMappings(UCMTable *base, UCMTable *ext) {
275 UCMapping *mb, *mbLimit;
279 mbLimit=mb+base->mappingsLength;
284 /* reset the move flag */
287 if(ext!=NULL && (flag&UCM_MOVE_TO_EXT)) {
288 /* add the mapping to the extension table */
289 ucm_addMapping(ext, mb, UCM_GET_CODE_POINTS(base, mb), UCM_GET_BYTES(base, mb));
292 /* remove this mapping: move the last base mapping down and overwrite the current one */
294 uprv_memcpy(mb, mbLimit-1, sizeof(UCMapping));
297 --base->mappingsLength;
298 base->isSorted=FALSE;
311 checkBaseExtUnicode(UCMStates *baseStates, UCMTable *base, UCMTable *ext,
312 UBool moveToExt, UBool intersectBase) {
313 UCMapping *mb, *me, *mbLimit, *meLimit;
318 mbLimit=mb+base->mappingsLength;
321 meLimit=me+ext->mappingsLength;
326 /* skip irrelevant mappings on both sides */
332 if((0<=mb->f && mb->f<=2) || mb->f==4) {
344 if((0<=me->f && me->f<=2) || me->f==4) {
351 /* compare the base and extension mappings */
352 cmp=compareUnicode(base, mb, ext, me);
354 if(intersectBase && (intersectBase!=2 || mb->bLen>1)) {
356 * mapping in base but not in ext, move it
358 * if ext is DBCS, move DBCS mappings here
359 * and check SBCS ones for Unicode prefix below
361 mb->moveFlag|=UCM_MOVE_TO_EXT;
364 /* does mb map from an input sequence that is a prefix of me's? */
365 } else if( mb->uLen<me->uLen &&
366 0==uprv_memcmp(UCM_GET_CODE_POINTS(base, mb), UCM_GET_CODE_POINTS(ext, me), 4*mb->uLen)
369 /* mark this mapping to be moved to the extension table */
370 mb->moveFlag|=UCM_MOVE_TO_EXT;
374 "ucm error: the base table contains a mapping whose input sequence\n"
375 " is a prefix of the input sequence of an extension mapping\n");
376 ucm_printMapping(base, mb, stderr);
377 ucm_printMapping(ext, me, stderr);
385 * same output: remove the extension mapping,
386 * otherwise treat as an error
388 if( mb->f==me->f && mb->bLen==me->bLen &&
389 0==uprv_memcmp(UCM_GET_BYTES(base, mb), UCM_GET_BYTES(ext, me), mb->bLen)
391 me->moveFlag|=UCM_REMOVE_MAPPING;
393 } else if(intersectBase) {
394 /* mapping in base but not in ext, move it */
395 mb->moveFlag|=UCM_MOVE_TO_EXT;
399 "ucm error: the base table contains a mapping whose input sequence\n"
400 " is the same as the input sequence of an extension mapping\n"
401 " but it maps differently\n");
402 ucm_printMapping(base, mb, stderr);
403 ucm_printMapping(ext, me, stderr);
415 checkBaseExtBytes(UCMStates *baseStates, UCMTable *base, UCMTable *ext,
416 UBool moveToExt, UBool intersectBase) {
418 int32_t *baseMap, *extMap;
419 int32_t b, e, bLimit, eLimit, cmp;
423 baseMap=base->reverseMap;
424 extMap=ext->reverseMap;
427 bLimit=base->mappingsLength;
428 eLimit=ext->mappingsLength;
432 isSISO=(UBool)(baseStates->outputType==MBCS_OUTPUT_2_SISO);
435 /* skip irrelevant mappings on both sides */
440 mb=base->mappings+baseMap[b];
442 if(intersectBase==2 && mb->bLen==1) {
444 * comparing a base against a DBCS extension:
445 * leave SBCS base mappings alone
450 if(mb->f==0 || mb->f==3) {
459 me=ext->mappings+extMap[e];
461 if(me->f==0 || me->f==3) {
468 /* compare the base and extension mappings */
469 cmp=compareBytes(base, mb, ext, me, TRUE);
472 /* mapping in base but not in ext, move it */
473 mb->moveFlag|=UCM_MOVE_TO_EXT;
477 * does mb map from an input sequence that is a prefix of me's?
478 * for SI/SO tables, a single byte is never a prefix because it
479 * occurs in a separate single-byte state
481 } else if( mb->bLen<me->bLen &&
482 (!isSISO || mb->bLen>1) &&
483 0==uprv_memcmp(UCM_GET_BYTES(base, mb), UCM_GET_BYTES(ext, me), mb->bLen)
486 /* mark this mapping to be moved to the extension table */
487 mb->moveFlag|=UCM_MOVE_TO_EXT;
491 "ucm error: the base table contains a mapping whose input sequence\n"
492 " is a prefix of the input sequence of an extension mapping\n");
493 ucm_printMapping(base, mb, stderr);
494 ucm_printMapping(ext, me, stderr);
502 * same output: remove the extension mapping,
503 * otherwise treat as an error
505 if( mb->f==me->f && mb->uLen==me->uLen &&
506 0==uprv_memcmp(UCM_GET_CODE_POINTS(base, mb), UCM_GET_CODE_POINTS(ext, me), 4*mb->uLen)
508 me->moveFlag|=UCM_REMOVE_MAPPING;
510 } else if(intersectBase) {
511 /* mapping in base but not in ext, move it */
512 mb->moveFlag|=UCM_MOVE_TO_EXT;
516 "ucm error: the base table contains a mapping whose input sequence\n"
517 " is the same as the input sequence of an extension mapping\n"
518 " but it maps differently\n");
519 ucm_printMapping(base, mb, stderr);
520 ucm_printMapping(ext, me, stderr);
531 U_CAPI UBool U_EXPORT2
532 ucm_checkValidity(UCMTable *table, UCMStates *baseStates) {
533 UCMapping *m, *mLimit;
538 mLimit=m+table->mappingsLength;
542 count=ucm_countChars(baseStates, UCM_GET_BYTES(table, m), m->bLen);
544 ucm_printMapping(table, m, stderr);
553 U_CAPI UBool U_EXPORT2
554 ucm_checkBaseExt(UCMStates *baseStates,
555 UCMTable *base, UCMTable *ext, UCMTable *moveTarget,
556 UBool intersectBase) {
559 /* if we have an extension table, we must always use precision flags */
560 if(base->flagsType&UCM_FLAGS_IMPLICIT) {
561 fprintf(stderr, "ucm error: the base table contains mappings without precision flags\n");
564 if(ext->flagsType&UCM_FLAGS_IMPLICIT) {
565 fprintf(stderr, "ucm error: extension table contains mappings without precision flags\n");
569 /* checking requires both tables to be sorted */
575 checkBaseExtUnicode(baseStates, base, ext, (UBool)(moveTarget!=NULL), intersectBase)|
576 checkBaseExtBytes(baseStates, base, ext, (UBool)(moveTarget!=NULL), intersectBase);
578 if(result&HAS_ERRORS) {
582 if(result&NEEDS_MOVE) {
583 ucm_moveMappings(ext, NULL);
584 ucm_moveMappings(base, moveTarget);
587 if(moveTarget!=NULL) {
588 ucm_sortTable(moveTarget);
595 /* merge tables for rptp2ucm ------------------------------------------------ */
597 U_CAPI void U_EXPORT2
598 ucm_mergeTables(UCMTable *fromUTable, UCMTable *toUTable,
599 const uint8_t *subchar, int32_t subcharLength,
601 UCMapping *fromUMapping, *toUMapping;
602 int32_t fromUIndex, toUIndex, fromUTop, toUTop, cmp;
604 ucm_sortTable(fromUTable);
605 ucm_sortTable(toUTable);
607 fromUMapping=fromUTable->mappings;
608 toUMapping=toUTable->mappings;
610 fromUTop=fromUTable->mappingsLength;
611 toUTop=toUTable->mappingsLength;
613 fromUIndex=toUIndex=0;
615 while(fromUIndex<fromUTop && toUIndex<toUTop) {
616 cmp=compareMappings(fromUTable, fromUMapping, toUTable, toUMapping, TRUE);
618 /* equal: roundtrip, nothing to do (flags are initially 0) */
626 * the fromU mapping does not have a toU counterpart:
627 * fallback Unicode->codepage
629 if( (fromUMapping->bLen==subcharLength &&
630 0==uprv_memcmp(UCM_GET_BYTES(fromUTable, fromUMapping), subchar, subcharLength)) ||
631 (subchar1!=0 && fromUMapping->bLen==1 && fromUMapping->b.bytes[0]==subchar1)
633 fromUMapping->f=2; /* SUB mapping */
635 fromUMapping->f=1; /* normal fallback */
642 * the toU mapping does not have a fromU counterpart:
643 * (reverse) fallback codepage->Unicode, copy it to the fromU table
646 /* ignore reverse fallbacks to Unicode SUB */
647 if(!(toUMapping->uLen==1 && (toUMapping->u==0xfffd || toUMapping->u==0x1a))) {
648 toUMapping->f=3; /* reverse fallback */
649 ucm_addMapping(fromUTable, toUMapping, UCM_GET_CODE_POINTS(toUTable, toUMapping), UCM_GET_BYTES(toUTable, toUMapping));
651 /* the table may have been reallocated */
652 fromUMapping=fromUTable->mappings+fromUIndex;
660 /* either one or both tables are exhausted */
661 while(fromUIndex<fromUTop) {
662 /* leftover fromU mappings are fallbacks */
663 if( (fromUMapping->bLen==subcharLength &&
664 0==uprv_memcmp(UCM_GET_BYTES(fromUTable, fromUMapping), subchar, subcharLength)) ||
665 (subchar1!=0 && fromUMapping->bLen==1 && fromUMapping->b.bytes[0]==subchar1)
667 fromUMapping->f=2; /* SUB mapping */
669 fromUMapping->f=1; /* normal fallback */
676 while(toUIndex<toUTop) {
677 /* leftover toU mappings are reverse fallbacks */
679 /* ignore reverse fallbacks to Unicode SUB */
680 if(!(toUMapping->uLen==1 && (toUMapping->u==0xfffd || toUMapping->u==0x1a))) {
681 toUMapping->f=3; /* reverse fallback */
682 ucm_addMapping(fromUTable, toUMapping, UCM_GET_CODE_POINTS(toUTable, toUMapping), UCM_GET_BYTES(toUTable, toUMapping));
689 fromUTable->isSorted=FALSE;
692 /* separate extension mappings out of base table for rptp2ucm --------------- */
694 U_CAPI UBool U_EXPORT2
695 ucm_separateMappings(UCMFile *ucm, UBool isSISO) {
697 UCMapping *m, *mLimit;
699 UBool needsMove, isOK;
703 mLimit=m+table->mappingsLength;
708 for(; m<mLimit; ++m) {
709 if(isSISO && m->bLen==1 && (m->b.bytes[0]==0xe || m->b.bytes[0]==0xf)) {
710 fprintf(stderr, "warning: removing illegal mapping from an SI/SO-stateful table\n");
711 ucm_printMapping(table, m, stderr);
712 m->moveFlag|=UCM_REMOVE_MAPPING;
717 type=ucm_mappingType(
719 UCM_GET_CODE_POINTS(table, m), UCM_GET_BYTES(table, m));
721 /* illegal byte sequence */
722 printMapping(m, UCM_GET_CODE_POINTS(table, m), UCM_GET_BYTES(table, m), stderr);
725 m->moveFlag|=UCM_MOVE_TO_EXT;
734 ucm_moveMappings(ucm->base, ucm->ext);
735 return ucm_checkBaseExt(&ucm->states, ucm->base, ucm->ext, ucm->ext, FALSE);
737 ucm_sortTable(ucm->base);
742 /* ucm parser --------------------------------------------------------------- */
744 U_CAPI int8_t U_EXPORT2
745 ucm_parseBytes(uint8_t bytes[UCNV_EXT_MAX_BYTES], const char *line, const char **ps) {
753 /* skip an optional plus sign */
754 if(bLen>0 && *s=='+') {
762 (byte=(uint8_t)uprv_strtoul(s+2, &end, 16), end)!=s+4
764 fprintf(stderr, "ucm error: byte must be formatted as \\xXX (2 hex digits) - \"%s\"\n", line);
768 if(bLen==UCNV_EXT_MAX_BYTES) {
769 fprintf(stderr, "ucm error: too many bytes on \"%s\"\n", line);
780 /* parse a mapping line; must not be empty */
781 U_CAPI UBool U_EXPORT2
782 ucm_parseMappingLine(UCMapping *m,
783 UChar32 codePoints[UCNV_EXT_MAX_UCHARS],
784 uint8_t bytes[UCNV_EXT_MAX_BYTES],
790 int8_t uLen, bLen, f;
795 /* parse code points */
797 /* skip an optional plus sign */
798 if(uLen>0 && *s=='+') {
806 (cp=(UChar32)uprv_strtoul(s+2, &end, 16), end)==s+2 ||
809 fprintf(stderr, "ucm error: Unicode code point must be formatted as <UXXXX> (1..6 hex digits) - \"%s\"\n", line);
812 if((uint32_t)cp>0x10ffff || U_IS_SURROGATE(cp)) {
813 fprintf(stderr, "ucm error: Unicode code point must be 0..d7ff or e000..10ffff - \"%s\"\n", line);
817 if(uLen==UCNV_EXT_MAX_UCHARS) {
818 fprintf(stderr, "ucm error: too many code points on \"%s\"\n", line);
821 codePoints[uLen++]=cp;
826 fprintf(stderr, "ucm error: no Unicode code points on \"%s\"\n", line);
831 UErrorCode errorCode=U_ZERO_ERROR;
832 u_strFromUTF32(NULL, 0, &u16Length, codePoints, uLen, &errorCode);
833 if( (U_FAILURE(errorCode) && errorCode!=U_BUFFER_OVERFLOW_ERROR) ||
834 u16Length>UCNV_EXT_MAX_UCHARS
836 fprintf(stderr, "ucm error: too many UChars on \"%s\"\n", line);
841 s=u_skipWhitespace(s);
844 bLen=ucm_parseBytes(bytes, line, &s);
849 fprintf(stderr, "ucm error: no bytes on \"%s\"\n", line);
852 uprv_memcpy(m->b.bytes, bytes, bLen);
855 /* skip everything until the fallback indicator, even the start of a comment */
858 f=-1; /* no fallback indicator */
861 f=(int8_t)(s[1]-'0');
863 fprintf(stderr, "ucm error: fallback indicator must be |0..|4 - \"%s\"\n", line);
877 /* general APIs ------------------------------------------------------------- */
879 U_CAPI UCMTable * U_EXPORT2
881 UCMTable *table=(UCMTable *)uprv_malloc(sizeof(UCMTable));
883 fprintf(stderr, "ucm error: unable to allocate a UCMTable\n");
884 exit(U_MEMORY_ALLOCATION_ERROR);
887 memset(table, 0, sizeof(UCMTable));
891 U_CAPI void U_EXPORT2
892 ucm_closeTable(UCMTable *table) {
894 uprv_free(table->mappings);
895 uprv_free(table->codePoints);
896 uprv_free(table->bytes);
897 uprv_free(table->reverseMap);
902 U_CAPI void U_EXPORT2
903 ucm_resetTable(UCMTable *table) {
905 table->mappingsLength=0;
907 table->unicodeMask=0;
908 table->bytesLength=table->codePointsLength=0;
909 table->isSorted=FALSE;
913 U_CAPI void U_EXPORT2
914 ucm_addMapping(UCMTable *table,
916 UChar32 codePoints[UCNV_EXT_MAX_UCHARS],
917 uint8_t bytes[UCNV_EXT_MAX_BYTES]) {
922 if(table->mappingsLength>=table->mappingsCapacity) {
923 /* make the mappings array larger */
924 if(table->mappingsCapacity==0) {
925 table->mappingsCapacity=1000;
927 table->mappingsCapacity*=10;
929 table->mappings=(UCMapping *)uprv_realloc(table->mappings,
930 table->mappingsCapacity*sizeof(UCMapping));
931 if(table->mappings==NULL) {
932 fprintf(stderr, "ucm error: unable to allocate %d UCMappings\n",
933 (int)table->mappingsCapacity);
934 exit(U_MEMORY_ALLOCATION_ERROR);
937 if(table->reverseMap!=NULL) {
938 /* the reverseMap must be reallocated in a new sort */
939 uprv_free(table->reverseMap);
940 table->reverseMap=NULL;
944 if(m->uLen>1 && table->codePointsCapacity==0) {
945 table->codePointsCapacity=10000;
946 table->codePoints=(UChar32 *)uprv_malloc(table->codePointsCapacity*4);
947 if(table->codePoints==NULL) {
948 fprintf(stderr, "ucm error: unable to allocate %d UChar32s\n",
949 (int)table->codePointsCapacity);
950 exit(U_MEMORY_ALLOCATION_ERROR);
954 if(m->bLen>4 && table->bytesCapacity==0) {
955 table->bytesCapacity=10000;
956 table->bytes=(uint8_t *)uprv_malloc(table->bytesCapacity);
957 if(table->bytes==NULL) {
958 fprintf(stderr, "ucm error: unable to allocate %d bytes\n",
959 (int)table->bytesCapacity);
960 exit(U_MEMORY_ALLOCATION_ERROR);
965 idx=table->codePointsLength;
966 table->codePointsLength+=m->uLen;
967 if(table->codePointsLength>table->codePointsCapacity) {
968 fprintf(stderr, "ucm error: too many code points in multiple-code point mappings\n");
969 exit(U_MEMORY_ALLOCATION_ERROR);
972 uprv_memcpy(table->codePoints+idx, codePoints, (size_t)m->uLen*4);
977 idx=table->bytesLength;
978 table->bytesLength+=m->bLen;
979 if(table->bytesLength>table->bytesCapacity) {
980 fprintf(stderr, "ucm error: too many bytes in mappings with >4 charset bytes\n");
981 exit(U_MEMORY_ALLOCATION_ERROR);
984 uprv_memcpy(table->bytes+idx, bytes, m->bLen);
988 /* set unicodeMask */
989 for(idx=0; idx<m->uLen; ++idx) {
992 table->unicodeMask|=UCNV_HAS_SUPPLEMENTARY; /* there are supplementary code points */
993 } else if(U_IS_SURROGATE(c)) {
994 table->unicodeMask|=UCNV_HAS_SURROGATES; /* there are surrogate code points */
1000 table->flagsType|=UCM_FLAGS_IMPLICIT;
1002 table->flagsType|=UCM_FLAGS_EXPLICIT;
1005 tm=table->mappings+table->mappingsLength++;
1006 uprv_memcpy(tm, m, sizeof(UCMapping));
1008 table->isSorted=FALSE;
1011 U_CAPI UCMFile * U_EXPORT2
1013 UCMFile *ucm=(UCMFile *)uprv_malloc(sizeof(UCMFile));
1015 fprintf(stderr, "ucm error: unable to allocate a UCMFile\n");
1016 exit(U_MEMORY_ALLOCATION_ERROR);
1019 memset(ucm, 0, sizeof(UCMFile));
1021 ucm->base=ucm_openTable();
1022 ucm->ext=ucm_openTable();
1024 ucm->states.stateFlags[0]=MBCS_STATE_FLAG_DIRECT;
1025 ucm->states.conversionType=UCNV_UNSUPPORTED_CONVERTER;
1026 ucm->states.outputType=-1;
1027 ucm->states.minCharLength=ucm->states.maxCharLength=1;
1032 U_CAPI void U_EXPORT2
1033 ucm_close(UCMFile *ucm) {
1035 ucm_closeTable(ucm->base);
1036 ucm_closeTable(ucm->ext);
1041 U_CAPI int32_t U_EXPORT2
1042 ucm_mappingType(UCMStates *baseStates,
1044 UChar32 codePoints[UCNV_EXT_MAX_UCHARS],
1045 uint8_t bytes[UCNV_EXT_MAX_BYTES]) {
1046 /* check validity of the bytes and count the characters in them */
1047 int32_t count=ucm_countChars(baseStates, bytes, m->bLen);
1049 /* illegal byte sequence */
1054 * Suitable for an ICU conversion base table means:
1055 * - a 1:1 mapping (1 Unicode code point : 1 byte sequence)
1056 * - precision flag 0..3
1057 * - SBCS: any 1:1 mapping
1058 * (the table stores additional bits to distinguish mapping types)
1059 * - MBCS: not a |2 SUB mapping for <subchar1>
1060 * - MBCS: not a |1 fallback to 0x00
1061 * - MBCS: not a multi-byte mapping with leading 0x00 bytes
1063 * Further restrictions for fromUnicode tables
1064 * are enforced in makeconv (MBCSOkForBaseFromUnicode()).
1066 * All of the MBCS fromUnicode specific tests could be removed from here,
1067 * but the ones above are for unusual mappings, and removing the tests
1068 * from here would change canonucm output which seems gratuitous.
1069 * (Markus Scherer 2006-nov-28)
1071 * Exception: All implicit mappings (f<0) that need to be moved
1072 * because of fromUnicode restrictions _must_ be moved here because
1073 * makeconv uses a hack for moving mappings only for the fromUnicode table
1074 * that only works with non-negative values of f.
1076 if( m->uLen==1 && count==1 && m->f<=3 &&
1077 (baseStates->maxCharLength==1 ||
1078 !((m->f==2 && m->bLen==1) ||
1079 (m->f==1 && bytes[0]==0) ||
1080 (m->f<=1 && m->bLen>1 && bytes[0]==0)))
1082 return 0; /* suitable for a base table */
1084 return 1; /* needs to go into an extension table */
1088 U_CAPI UBool U_EXPORT2
1089 ucm_addMappingAuto(UCMFile *ucm, UBool forBase, UCMStates *baseStates,
1091 UChar32 codePoints[UCNV_EXT_MAX_UCHARS],
1092 uint8_t bytes[UCNV_EXT_MAX_BYTES]) {
1095 if(m->f==2 && m->uLen>1) {
1096 fprintf(stderr, "ucm error: illegal <subchar1> |2 mapping from multiple code points\n");
1097 printMapping(m, codePoints, bytes, stderr);
1101 if(baseStates!=NULL) {
1102 /* check validity of the bytes and count the characters in them */
1103 type=ucm_mappingType(baseStates, m, codePoints, bytes);
1105 /* illegal byte sequence */
1106 printMapping(m, codePoints, bytes, stderr);
1110 /* not used - adding a mapping for an extension-only table before its base table is read */
1115 * Add the mapping to the base table if this is requested and suitable.
1116 * Otherwise, add it to the extension table.
1118 if(forBase && type==0) {
1119 ucm_addMapping(ucm->base, m, codePoints, bytes);
1121 ucm_addMapping(ucm->ext, m, codePoints, bytes);
1127 U_CAPI UBool U_EXPORT2
1128 ucm_addMappingFromLine(UCMFile *ucm, const char *line, UBool forBase, UCMStates *baseStates) {
1129 UCMapping m={ 0, {0}, 0, 0, 0, 0 };
1130 UChar32 codePoints[UCNV_EXT_MAX_UCHARS];
1131 uint8_t bytes[UCNV_EXT_MAX_BYTES];
1135 /* ignore empty and comment lines */
1136 if(line[0]=='#' || *(s=u_skipWhitespace(line))==0 || *s=='\n' || *s=='\r') {
1141 ucm_parseMappingLine(&m, codePoints, bytes, line) &&
1142 ucm_addMappingAuto(ucm, forBase, baseStates, &m, codePoints, bytes);
1145 U_CAPI void U_EXPORT2
1146 ucm_readTable(UCMFile *ucm, FileStream* convFile,
1147 UBool forBase, UCMStates *baseStates,
1148 UErrorCode *pErrorCode) {
1153 if(U_FAILURE(*pErrorCode)) {
1160 /* read the next line */
1161 if(!T_FileStream_readLine(convFile, line, sizeof(line))) {
1162 fprintf(stderr, "incomplete charmap section\n");
1168 end=uprv_strchr(line, 0);
1169 while(line<end && (*(end-1)=='\r' || *(end-1)=='\n')) {
1174 /* ignore empty and comment lines */
1175 if(line[0]==0 || line[0]=='#') {
1179 /* stop at the end of the mapping table */
1180 if(0==uprv_strcmp(line, "END CHARMAP")) {
1184 isOK&=ucm_addMappingFromLine(ucm, line, forBase, baseStates);
1188 *pErrorCode=U_INVALID_TABLE_FORMAT;