ucm.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2003-2013, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. * file name: ucm.h
  9. * encoding: UTF-8
  10. * tab size: 8 (not used)
  11. * indentation:4
  12. *
  13. * created on: 2003jun20
  14. * created by: Markus W. Scherer
  15. *
  16. * Definitions for the .ucm file parser and handler module ucm.c.
  17. */
  18. #ifndef __UCM_H__
  19. #define __UCM_H__
  20. #include "unicode/utypes.h"
  21. #include "ucnvmbcs.h"
  22. #include "ucnv_ext.h"
  23. #include "filestrm.h"
  24. #include <stdio.h>
  25. #if !UCONFIG_NO_CONVERSION
  26. U_CDECL_BEGIN
  27. /* constants for UCMapping.moveFlag */
  28. enum {
  29. UCM_MOVE_TO_EXT=1,
  30. UCM_REMOVE_MAPPING=2
  31. };
  32. /*
  33. * Per-mapping data structure
  34. *
  35. * u if uLen==1: Unicode code point
  36. * else index to uLen code points
  37. * b if bLen<=4: up to 4 bytes
  38. * else index to bLen bytes
  39. * uLen number of code points
  40. * bLen number of words containing left-justified bytes
  41. * bIsMultipleChars indicates that the bytes contain more than one sequence
  42. * according to the state table
  43. * f flag for roundtrip (0), fallback (1), sub mapping (2), reverse fallback (3)
  44. * or "good one-way" mapping (4).
  45. * Same values as in the source file after |
  46. */
  47. typedef struct UCMapping {
  48. UChar32 u;
  49. union {
  50. uint32_t idx;
  51. uint8_t bytes[4];
  52. } b;
  53. int8_t uLen, bLen, f, moveFlag;
  54. } UCMapping;
  55. /* constants for UCMTable.flagsType */
  56. enum {
  57. UCM_FLAGS_INITIAL, /* no mappings parsed yet */
  58. UCM_FLAGS_EXPLICIT, /* .ucm file has mappings with | fallback indicators */
  59. UCM_FLAGS_IMPLICIT, /* .ucm file has mappings without | fallback indicators, later wins */
  60. UCM_FLAGS_MIXED /* both implicit and explicit */
  61. };
  62. typedef struct UCMTable {
  63. UCMapping *mappings;
  64. int32_t mappingsCapacity, mappingsLength;
  65. UChar32 *codePoints;
  66. int32_t codePointsCapacity, codePointsLength;
  67. uint8_t *bytes;
  68. int32_t bytesCapacity, bytesLength;
  69. /* index map for mapping by bytes first */
  70. int32_t *reverseMap;
  71. uint8_t unicodeMask;
  72. int8_t flagsType; /* UCM_FLAGS_INITIAL etc. */
  73. UBool isSorted;
  74. } UCMTable;
  75. enum {
  76. MBCS_STATE_FLAG_DIRECT=1,
  77. MBCS_STATE_FLAG_SURROGATES,
  78. MBCS_STATE_FLAG_READY=16
  79. };
  80. typedef struct UCMStates {
  81. int32_t stateTable[MBCS_MAX_STATE_COUNT][256];
  82. uint32_t stateFlags[MBCS_MAX_STATE_COUNT],
  83. stateOffsetSum[MBCS_MAX_STATE_COUNT];
  84. int32_t countStates, minCharLength, maxCharLength, countToUCodeUnits;
  85. int8_t conversionType, outputType;
  86. } UCMStates;
  87. typedef struct UCMFile {
  88. UCMTable *base, *ext;
  89. UCMStates states;
  90. char baseName[UCNV_MAX_CONVERTER_NAME_LENGTH];
  91. } UCMFile;
  92. /* simple accesses ---------------------------------------------------------- */
  93. #define UCM_GET_CODE_POINTS(t, m) \
  94. (((m)->uLen==1) ? &(m)->u : (t)->codePoints+(m)->u)
  95. #define UCM_GET_BYTES(t, m) \
  96. (((m)->bLen<=4) ? (m)->b.bytes : (t)->bytes+(m)->b.idx)
  97. /* APIs --------------------------------------------------------------------- */
  98. U_CAPI UCMFile * U_EXPORT2
  99. ucm_open(void);
  100. U_CAPI void U_EXPORT2
  101. ucm_close(UCMFile *ucm);
  102. U_CAPI UBool U_EXPORT2
  103. ucm_parseHeaderLine(UCMFile *ucm,
  104. char *line, char **pKey, char **pValue);
  105. /* @return -1 illegal bytes 0 suitable for base table 1 needs to go into extension table */
  106. U_CAPI int32_t U_EXPORT2
  107. ucm_mappingType(UCMStates *baseStates,
  108. UCMapping *m,
  109. UChar32 codePoints[UCNV_EXT_MAX_UCHARS],
  110. uint8_t bytes[UCNV_EXT_MAX_BYTES]);
  111. /* add a mapping to the base or extension table as appropriate */
  112. U_CAPI UBool U_EXPORT2
  113. ucm_addMappingAuto(UCMFile *ucm, UBool forBase, UCMStates *baseStates,
  114. UCMapping *m,
  115. UChar32 codePoints[UCNV_EXT_MAX_UCHARS],
  116. uint8_t bytes[UCNV_EXT_MAX_BYTES]);
  117. U_CAPI UBool U_EXPORT2
  118. ucm_addMappingFromLine(UCMFile *ucm, const char *line, UBool forBase, UCMStates *baseStates);
  119. U_CAPI UCMTable * U_EXPORT2
  120. ucm_openTable(void);
  121. U_CAPI void U_EXPORT2
  122. ucm_closeTable(UCMTable *table);
  123. U_CAPI void U_EXPORT2
  124. ucm_resetTable(UCMTable *table);
  125. U_CAPI void U_EXPORT2
  126. ucm_sortTable(UCMTable *t);
  127. /*
  128. * Remove mappings with their move flag set from the base table
  129. * and move some of them (with UCM_MOVE_TO_EXT) to the extension table.
  130. */
  131. U_CAPI void U_EXPORT2
  132. ucm_moveMappings(UCMTable *base, UCMTable *ext);
  133. /**
  134. * Read a table from a .ucm file, from after the CHARMAP line to
  135. * including the END CHARMAP line.
  136. */
  137. U_CAPI void U_EXPORT2
  138. ucm_readTable(UCMFile *ucm, FileStream* convFile,
  139. UBool forBase, UCMStates *baseStates,
  140. UErrorCode *pErrorCode);
  141. /**
  142. * Check the validity of mappings against a base table's states;
  143. * necessary for extension-only tables that were read before their base tables.
  144. */
  145. U_CAPI UBool U_EXPORT2
  146. ucm_checkValidity(UCMTable *ext, UCMStates *baseStates);
  147. /**
  148. * Check a base table against an extension table.
  149. * Set the moveTarget!=NULL if it is possible to move mappings from the base.
  150. * This is the case where base and extension tables are parsed from a single file
  151. * (moveTarget==ext)
  152. * or when delta file mappings are subtracted from a base table.
  153. *
  154. * When a base table cannot be modified because a delta file is parsed in makeconv,
  155. * then set moveTarget=NULL.
  156. *
  157. * if(intersectBase) then mappings that exist in the base table but not in
  158. * the extension table are moved to moveTarget instead of showing an error.
  159. *
  160. * Special mode:
  161. * If intersectBase==2 for a DBCS extension table, then SBCS mappings are
  162. * not moved out of the base unless their Unicode input requires it.
  163. * This helps ucmkbase generate base tables for DBCS-only extension .cnv files.
  164. *
  165. * For both tables in the same file, the extension table is automatically
  166. * built.
  167. * For separate files, the extension file can use a complete mapping table (.ucm file),
  168. * so that common mappings need not be stripped out manually.
  169. *
  170. *
  171. * Sort both tables, and then for each mapping direction:
  172. *
  173. * If intersectBase is TRUE and the base table contains a mapping
  174. * that does not exist in the extension table, then this mapping is moved
  175. * to moveTarget.
  176. *
  177. * - otherwise -
  178. *
  179. * If the base table contains a mapping for which the input sequence is
  180. * the same as the extension input, then
  181. * - if the output is the same: remove the extension mapping
  182. * - else: error
  183. *
  184. * If the base table contains a mapping for which the input sequence is
  185. * a prefix of the extension input, then
  186. * - if moveTarget!=NULL: move the base mapping to the moveTarget table
  187. * - else: error
  188. *
  189. * @return FALSE in case of an irreparable error
  190. */
  191. U_CAPI UBool U_EXPORT2
  192. ucm_checkBaseExt(UCMStates *baseStates, UCMTable *base, UCMTable *ext,
  193. UCMTable *moveTarget, UBool intersectBase);
  194. U_CAPI void U_EXPORT2
  195. ucm_printTable(UCMTable *table, FILE *f, UBool byUnicode);
  196. U_CAPI void U_EXPORT2
  197. ucm_printMapping(UCMTable *table, UCMapping *m, FILE *f);
  198. U_CAPI void U_EXPORT2
  199. ucm_addState(UCMStates *states, const char *s);
  200. U_CAPI void U_EXPORT2
  201. ucm_processStates(UCMStates *states, UBool ignoreSISOCheck);
  202. U_CAPI int32_t U_EXPORT2
  203. ucm_countChars(UCMStates *states,
  204. const uint8_t *bytes, int32_t length);
  205. U_CAPI int8_t U_EXPORT2
  206. ucm_parseBytes(uint8_t bytes[UCNV_EXT_MAX_BYTES], const char *line, const char **ps);
  207. U_CAPI UBool U_EXPORT2
  208. ucm_parseMappingLine(UCMapping *m,
  209. UChar32 codePoints[UCNV_EXT_MAX_UCHARS],
  210. uint8_t bytes[UCNV_EXT_MAX_BYTES],
  211. const char *line);
  212. U_CAPI void U_EXPORT2
  213. ucm_addMapping(UCMTable *table,
  214. UCMapping *m,
  215. UChar32 codePoints[UCNV_EXT_MAX_UCHARS],
  216. uint8_t bytes[UCNV_EXT_MAX_BYTES]);
  217. /* very makeconv-specific functions ----------------------------------------- */
  218. /* finalize and optimize states after the toUnicode mappings are processed */
  219. U_CAPI void U_EXPORT2
  220. ucm_optimizeStates(UCMStates *states,
  221. uint16_t **pUnicodeCodeUnits,
  222. _MBCSToUFallback *toUFallbacks, int32_t countToUFallbacks,
  223. UBool verbose);
  224. /* moved here because it is used inside ucmstate.c */
  225. U_CAPI int32_t U_EXPORT2
  226. ucm_findFallback(_MBCSToUFallback *toUFallbacks, int32_t countToUFallbacks,
  227. uint32_t offset);
  228. /* very rptp2ucm-specific functions ----------------------------------------- */
  229. /*
  230. * Input: Separate tables with mappings from/to Unicode,
  231. * subchar and subchar1 (0 if none).
  232. * All mappings must have flag 0.
  233. *
  234. * Output: fromUTable will contain the union of mappings with the correct
  235. * precision flags, and be sorted.
  236. */
  237. U_CAPI void U_EXPORT2
  238. ucm_mergeTables(UCMTable *fromUTable, UCMTable *toUTable,
  239. const uint8_t *subchar, int32_t subcharLength,
  240. uint8_t subchar1);
  241. U_CAPI UBool U_EXPORT2
  242. ucm_separateMappings(UCMFile *ucm, UBool isSISO);
  243. U_CDECL_END
  244. #endif
  245. #endif