1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2003-2013, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2003jun20
14 * created by: Markus W. Scherer
16 * Definitions for the .ucm file parser and handler module ucm.c.
22 #include "unicode/utypes.h"
28 #if !UCONFIG_NO_CONVERSION
32 /* constants for UCMapping.moveFlag */
39 * Per-mapping data structure
41 * u if uLen==1: Unicode code point
42 * else index to uLen code points
43 * b if bLen<=4: up to 4 bytes
44 * else index to bLen bytes
45 * uLen number of code points
46 * bLen number of words containing left-justified bytes
47 * bIsMultipleChars indicates that the bytes contain more than one sequence
48 * according to the state table
49 * f flag for roundtrip (0), fallback (1), sub mapping (2), reverse fallback (3)
50 * or "good one-way" mapping (4).
51 * Same values as in the source file after |
53 typedef struct UCMapping
{
59 int8_t uLen
, bLen
, f
, moveFlag
;
62 /* constants for UCMTable.flagsType */
64 UCM_FLAGS_INITIAL
, /* no mappings parsed yet */
65 UCM_FLAGS_EXPLICIT
, /* .ucm file has mappings with | fallback indicators */
66 UCM_FLAGS_IMPLICIT
, /* .ucm file has mappings without | fallback indicators, later wins */
67 UCM_FLAGS_MIXED
/* both implicit and explicit */
70 typedef struct UCMTable
{
72 int32_t mappingsCapacity
, mappingsLength
;
75 int32_t codePointsCapacity
, codePointsLength
;
78 int32_t bytesCapacity
, bytesLength
;
80 /* index map for mapping by bytes first */
84 int8_t flagsType
; /* UCM_FLAGS_INITIAL etc. */
89 MBCS_STATE_FLAG_DIRECT
=1,
90 MBCS_STATE_FLAG_SURROGATES
,
92 MBCS_STATE_FLAG_READY
=16
95 typedef struct UCMStates
{
96 int32_t stateTable
[MBCS_MAX_STATE_COUNT
][256];
97 uint32_t stateFlags
[MBCS_MAX_STATE_COUNT
],
98 stateOffsetSum
[MBCS_MAX_STATE_COUNT
];
100 int32_t countStates
, minCharLength
, maxCharLength
, countToUCodeUnits
;
101 int8_t conversionType
, outputType
;
104 typedef struct UCMFile
{
105 UCMTable
*base
, *ext
;
108 char baseName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
111 /* simple accesses ---------------------------------------------------------- */
113 #define UCM_GET_CODE_POINTS(t, m) \
114 (((m)->uLen==1) ? &(m)->u : (t)->codePoints+(m)->u)
116 #define UCM_GET_BYTES(t, m) \
117 (((m)->bLen<=4) ? (m)->b.bytes : (t)->bytes+(m)->b.idx)
119 /* APIs --------------------------------------------------------------------- */
121 U_CAPI UCMFile
* U_EXPORT2
124 U_CAPI
void U_EXPORT2
125 ucm_close(UCMFile
*ucm
);
127 U_CAPI UBool U_EXPORT2
128 ucm_parseHeaderLine(UCMFile
*ucm
,
129 char *line
, char **pKey
, char **pValue
);
131 /* @return -1 illegal bytes 0 suitable for base table 1 needs to go into extension table */
132 U_CAPI
int32_t U_EXPORT2
133 ucm_mappingType(UCMStates
*baseStates
,
135 UChar32 codePoints
[UCNV_EXT_MAX_UCHARS
],
136 uint8_t bytes
[UCNV_EXT_MAX_BYTES
]);
138 /* add a mapping to the base or extension table as appropriate */
139 U_CAPI UBool U_EXPORT2
140 ucm_addMappingAuto(UCMFile
*ucm
, UBool forBase
, UCMStates
*baseStates
,
142 UChar32 codePoints
[UCNV_EXT_MAX_UCHARS
],
143 uint8_t bytes
[UCNV_EXT_MAX_BYTES
]);
145 U_CAPI UBool U_EXPORT2
146 ucm_addMappingFromLine(UCMFile
*ucm
, const char *line
, UBool forBase
, UCMStates
*baseStates
);
149 U_CAPI UCMTable
* U_EXPORT2
152 U_CAPI
void U_EXPORT2
153 ucm_closeTable(UCMTable
*table
);
155 U_CAPI
void U_EXPORT2
156 ucm_resetTable(UCMTable
*table
);
158 U_CAPI
void U_EXPORT2
159 ucm_sortTable(UCMTable
*t
);
162 * Remove mappings with their move flag set from the base table
163 * and move some of them (with UCM_MOVE_TO_EXT) to the extension table.
165 U_CAPI
void U_EXPORT2
166 ucm_moveMappings(UCMTable
*base
, UCMTable
*ext
);
169 * Read a table from a .ucm file, from after the CHARMAP line to
170 * including the END CHARMAP line.
172 U_CAPI
void U_EXPORT2
173 ucm_readTable(UCMFile
*ucm
, FileStream
* convFile
,
174 UBool forBase
, UCMStates
*baseStates
,
175 UErrorCode
*pErrorCode
);
178 * Check the validity of mappings against a base table's states;
179 * necessary for extension-only tables that were read before their base tables.
181 U_CAPI UBool U_EXPORT2
182 ucm_checkValidity(UCMTable
*ext
, UCMStates
*baseStates
);
185 * Check a base table against an extension table.
186 * Set the moveTarget!=NULL if it is possible to move mappings from the base.
187 * This is the case where base and extension tables are parsed from a single file
189 * or when delta file mappings are subtracted from a base table.
191 * When a base table cannot be modified because a delta file is parsed in makeconv,
192 * then set moveTarget=NULL.
194 * if(intersectBase) then mappings that exist in the base table but not in
195 * the extension table are moved to moveTarget instead of showing an error.
198 * If intersectBase==2 for a DBCS extension table, then SBCS mappings are
199 * not moved out of the base unless their Unicode input requires it.
200 * This helps ucmkbase generate base tables for DBCS-only extension .cnv files.
202 * For both tables in the same file, the extension table is automatically
204 * For separate files, the extension file can use a complete mapping table (.ucm file),
205 * so that common mappings need not be stripped out manually.
208 * Sort both tables, and then for each mapping direction:
210 * If intersectBase is TRUE and the base table contains a mapping
211 * that does not exist in the extension table, then this mapping is moved
216 * If the base table contains a mapping for which the input sequence is
217 * the same as the extension input, then
218 * - if the output is the same: remove the extension mapping
221 * If the base table contains a mapping for which the input sequence is
222 * a prefix of the extension input, then
223 * - if moveTarget!=NULL: move the base mapping to the moveTarget table
226 * @return FALSE in case of an irreparable error
228 U_CAPI UBool U_EXPORT2
229 ucm_checkBaseExt(UCMStates
*baseStates
, UCMTable
*base
, UCMTable
*ext
,
230 UCMTable
*moveTarget
, UBool intersectBase
);
232 U_CAPI
void U_EXPORT2
233 ucm_printTable(UCMTable
*table
, FILE *f
, UBool byUnicode
);
235 U_CAPI
void U_EXPORT2
236 ucm_printMapping(UCMTable
*table
, UCMapping
*m
, FILE *f
);
239 U_CAPI
void U_EXPORT2
240 ucm_addState(UCMStates
*states
, const char *s
);
242 U_CAPI
void U_EXPORT2
243 ucm_processStates(UCMStates
*states
, UBool ignoreSISOCheck
);
245 U_CAPI
int32_t U_EXPORT2
246 ucm_countChars(UCMStates
*states
,
247 const uint8_t *bytes
, int32_t length
);
250 U_CAPI
int8_t U_EXPORT2
251 ucm_parseBytes(uint8_t bytes
[UCNV_EXT_MAX_BYTES
], const char *line
, const char **ps
);
253 U_CAPI UBool U_EXPORT2
254 ucm_parseMappingLine(UCMapping
*m
,
255 UChar32 codePoints
[UCNV_EXT_MAX_UCHARS
],
256 uint8_t bytes
[UCNV_EXT_MAX_BYTES
],
259 U_CAPI
void U_EXPORT2
260 ucm_addMapping(UCMTable
*table
,
262 UChar32 codePoints
[UCNV_EXT_MAX_UCHARS
],
263 uint8_t bytes
[UCNV_EXT_MAX_BYTES
]);
265 /* very makeconv-specific functions ----------------------------------------- */
267 /* finalize and optimize states after the toUnicode mappings are processed */
268 U_CAPI
void U_EXPORT2
269 ucm_optimizeStates(UCMStates
*states
,
270 uint16_t **pUnicodeCodeUnits
,
271 _MBCSToUFallback
*toUFallbacks
, int32_t countToUFallbacks
,
274 /* moved here because it is used inside ucmstate.c */
275 U_CAPI
int32_t U_EXPORT2
276 ucm_findFallback(_MBCSToUFallback
*toUFallbacks
, int32_t countToUFallbacks
,
279 /* very rptp2ucm-specific functions ----------------------------------------- */
282 * Input: Separate tables with mappings from/to Unicode,
283 * subchar and subchar1 (0 if none).
284 * All mappings must have flag 0.
286 * Output: fromUTable will contain the union of mappings with the correct
287 * precision flags, and be sorted.
289 U_CAPI
void U_EXPORT2
290 ucm_mergeTables(UCMTable
*fromUTable
, UCMTable
*toUTable
,
291 const uint8_t *subchar
, int32_t subcharLength
,
294 U_CAPI UBool U_EXPORT2
295 ucm_separateMappings(UCMFile
*ucm
, UBool isSISO
);