• 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 *   Copyright (C) 1999-2015 International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 **********************************************************************
8 *
9 *
10 *  ucnv_bld.h:
11 *  Contains internal data structure definitions
12 * Created by Bertrand A. Damiba
13 *
14 *   Change history:
15 *
16 *   06/29/2000  helena      Major rewrite of the callback APIs.
17 */
18 
19 #ifndef UCNV_BLD_H
20 #define UCNV_BLD_H
21 
22 #include "unicode/utypes.h"
23 
24 #if !UCONFIG_NO_CONVERSION
25 
26 #include "unicode/ucnv.h"
27 #include "unicode/ucnv_err.h"
28 #include "unicode/utf16.h"
29 #include "ucnv_cnv.h"
30 #include "ucnvmbcs.h"
31 #include "ucnv_ext.h"
32 #include "udataswp.h"
33 
34 /* size of the overflow buffers in UConverter, enough for escaping callbacks */
35 #define UCNV_ERROR_BUFFER_LENGTH 32
36 
37 /* at most 4 bytes per substitution character (part of .cnv file format! see UConverterStaticData) */
38 #define UCNV_MAX_SUBCHAR_LEN 4
39 
40 /* at most 8 bytes per character in toUBytes[] (UTF-8 uses up to 6) */
41 #define UCNV_MAX_CHAR_LEN 8
42 
43 /* converter options bits */
44 #define UCNV_OPTION_VERSION     0xf
45 #define UCNV_OPTION_SWAP_LFNL   0x10
46 
47 #define UCNV_GET_VERSION(cnv) ((cnv)->options&UCNV_OPTION_VERSION)
48 
49 U_CDECL_BEGIN /* We must declare the following as 'extern "C"' so that if ucnv
50                  itself is compiled under C++, the linkage of the funcptrs will
51                  work.
52               */
53 
54 union UConverterTable {
55     UConverterMBCSTable mbcs;
56 };
57 
58 typedef union UConverterTable UConverterTable;
59 
60 struct UConverterImpl;
61 typedef struct UConverterImpl UConverterImpl;
62 
63 /** values for the unicodeMask */
64 #define UCNV_HAS_SUPPLEMENTARY 1
65 #define UCNV_HAS_SURROGATES    2
66 
67 typedef struct UConverterStaticData {   /* +offset: size */
68     uint32_t structSize;                /* +0: 4 Size of this structure */
69 
70     char name
71       [UCNV_MAX_CONVERTER_NAME_LENGTH]; /* +4: 60  internal name of the converter- invariant chars */
72 
73     int32_t codepage;               /* +64: 4 codepage # (now IBM-$codepage) */
74 
75     int8_t platform;                /* +68: 1 platform of the converter (only IBM now) */
76     int8_t conversionType;          /* +69: 1 conversion type */
77 
78     int8_t minBytesPerChar;         /* +70: 1 Minimum # bytes per char in this codepage */
79     int8_t maxBytesPerChar;         /* +71: 1 Maximum # bytes output per UChar in this codepage */
80 
81     uint8_t subChar[UCNV_MAX_SUBCHAR_LEN]; /* +72: 4  [note:  4 and 8 byte boundary] */
82     int8_t subCharLen;              /* +76: 1 */
83 
84     uint8_t hasToUnicodeFallback;   /* +77: 1 UBool needs to be changed to UBool to be consistent across platform */
85     uint8_t hasFromUnicodeFallback; /* +78: 1 */
86     uint8_t unicodeMask;            /* +79: 1  bit 0: has supplementary  bit 1: has single surrogates */
87     uint8_t subChar1;               /* +80: 1  single-byte substitution character for IBM MBCS (0 if none) */
88     uint8_t reserved[19];           /* +81: 19 to round out the structure */
89                                     /* total size: 100 */
90 } UConverterStaticData;
91 
92 /*
93  * Defines the UConverterSharedData struct,
94  * the immutable, shared part of UConverter.
95  */
96 struct UConverterSharedData {
97     uint32_t structSize;            /* Size of this structure */
98     uint32_t referenceCounter;      /* used to count number of clients, unused for static/immutable SharedData */
99 
100     const void *dataMemory;         /* from udata_openChoice() - for cleanup */
101 
102     const UConverterStaticData *staticData; /* pointer to the static (non changing) data. */
103 
104     UBool                sharedDataCached;   /* TRUE:  shared data is in cache, don't destroy on ucnv_close() if 0 ref.  FALSE: shared data isn't in the cache, do attempt to clean it up if the ref is 0 */
105     /** If FALSE, then referenceCounter is not used. Must not change after initialization. */
106     UBool isReferenceCounted;
107 
108     const UConverterImpl *impl;     /* vtable-style struct of mostly function pointers */
109 
110     /*initial values of some members of the mutable part of object */
111     uint32_t toUnicodeStatus;
112 
113     /*
114      * Shared data structures currently come in two flavors:
115      * - readonly for built-in algorithmic converters
116      * - allocated for MBCS, with a pointer to an allocated UConverterTable
117      *   which always has a UConverterMBCSTable
118      *
119      * To eliminate one allocation, I am making the UConverterMBCSTable
120      * a member of the shared data.
121      *
122      * markus 2003-nov-07
123      */
124     UConverterMBCSTable mbcs;
125 };
126 
127 /** UConverterSharedData initializer for static, non-reference-counted converters. */
128 #define UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(pStaticData, pImpl) \
129     { \
130         sizeof(UConverterSharedData), ~((uint32_t)0), \
131         NULL, pStaticData, FALSE, FALSE, pImpl, \
132         0, UCNV_MBCS_TABLE_INITIALIZER \
133     }
134 
135 /* Defines a UConverter, the lightweight mutable part the user sees */
136 
137 struct UConverter {
138     /*
139      * Error function pointer called when conversion issues
140      * occur during a ucnv_fromUnicode call
141      */
142     void (U_EXPORT2 *fromUCharErrorBehaviour) (const void *context,
143                                      UConverterFromUnicodeArgs *args,
144                                      const UChar *codeUnits,
145                                      int32_t length,
146                                      UChar32 codePoint,
147                                      UConverterCallbackReason reason,
148                                      UErrorCode *);
149     /*
150      * Error function pointer called when conversion issues
151      * occur during a ucnv_toUnicode call
152      */
153     void (U_EXPORT2 *fromCharErrorBehaviour) (const void *context,
154                                     UConverterToUnicodeArgs *args,
155                                     const char *codeUnits,
156                                     int32_t length,
157                                     UConverterCallbackReason reason,
158                                     UErrorCode *);
159 
160     /*
161      * Pointer to additional data that depends on the converter type.
162      * Used by ISO 2022, SCSU, GB 18030 converters, possibly more.
163      */
164     void *extraInfo;
165 
166     const void *fromUContext;
167     const void *toUContext;
168 
169     /*
170      * Pointer to charset bytes for substitution string if subCharLen>0,
171      * or pointer to Unicode string (UChar *) if subCharLen<0.
172      * subCharLen==0 is equivalent to using a skip callback.
173      * If the pointer is !=subUChars then it is allocated with
174      * UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR bytes.
175      * The subUChars field is declared as UChar[] not uint8_t[] to
176      * guarantee alignment for UChars.
177      */
178     uint8_t *subChars;
179 
180     UConverterSharedData *sharedData;   /* Pointer to the shared immutable part of the converter object */
181 
182     uint32_t options; /* options flags from UConverterOpen, may contain additional bits */
183 
184     UBool sharedDataIsCached;  /* TRUE:  shared data is in cache, don't destroy on ucnv_close() if 0 ref.  FALSE: shared data isn't in the cache, do attempt to clean it up if the ref is 0 */
185     UBool isCopyLocal;  /* TRUE if UConverter is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */
186     UBool isExtraLocal; /* TRUE if extraInfo is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */
187 
188     UBool  useFallback;
189     int8_t toULength;                   /* number of bytes in toUBytes */
190     uint8_t toUBytes[UCNV_MAX_CHAR_LEN-1];/* more "toU status"; keeps the bytes of the current character */
191     uint32_t toUnicodeStatus;           /* Used to internalize stream status information */
192     int32_t mode;
193     uint32_t fromUnicodeStatus;
194 
195     /*
196      * More fromUnicode() status. Serves 3 purposes:
197      * - keeps a lead surrogate between buffers (similar to toUBytes[])
198      * - keeps a lead surrogate at the end of the stream,
199      *   which the framework handles as truncated input
200      * - if the fromUnicode() implementation returns to the framework
201      *   (ucnv.c ucnv_fromUnicode()), then the framework calls the callback
202      *   for this code point
203      */
204     UChar32 fromUChar32;
205 
206     /*
207      * value for ucnv_getMaxCharSize()
208      *
209      * usually simply copied from the static data, but ucnvmbcs.c modifies
210      * the value depending on the converter type and options
211      */
212     int8_t maxBytesPerUChar;
213 
214     int8_t subCharLen;                  /* length of the codepage specific character sequence */
215     int8_t invalidCharLength;
216     int8_t charErrorBufferLength;       /* number of valid bytes in charErrorBuffer */
217 
218     int8_t invalidUCharLength;
219     int8_t UCharErrorBufferLength;      /* number of valid UChars in charErrorBuffer */
220 
221     uint8_t subChar1;                                   /* single-byte substitution character if different from subChar */
222     UBool useSubChar1;
223     char invalidCharBuffer[UCNV_MAX_CHAR_LEN];          /* bytes from last error/callback situation */
224     uint8_t charErrorBuffer[UCNV_ERROR_BUFFER_LENGTH];  /* codepage output from Error functions */
225     UChar subUChars[UCNV_MAX_SUBCHAR_LEN/U_SIZEOF_UCHAR]; /* see subChars documentation */
226 
227     UChar invalidUCharBuffer[U16_MAX_LENGTH];           /* UChars from last error/callback situation */
228     UChar UCharErrorBuffer[UCNV_ERROR_BUFFER_LENGTH];   /* unicode output from Error functions */
229 
230     /* fields for conversion extension */
231 
232     /* store previous UChars/chars to continue partial matches */
233     UChar32 preFromUFirstCP;                /* >=0: partial match */
234     UChar preFromU[UCNV_EXT_MAX_UCHARS];
235     char preToU[UCNV_EXT_MAX_BYTES];
236     int8_t preFromULength, preToULength;    /* negative: replay */
237     int8_t preToUFirstLength;               /* length of first character */
238 
239     /* new fields for ICU 4.0 */
240     UConverterCallbackReason toUCallbackReason; /* (*fromCharErrorBehaviour) reason, set when error is detected */
241 };
242 
243 U_CDECL_END /* end of UConverter */
244 
245 #define CONVERTER_FILE_EXTENSION ".cnv"
246 
247 
248 /**
249  * Return the number of all converter names.
250  * @param pErrorCode The error code
251  * @return the number of all converter names
252  */
253 U_CFUNC uint16_t
254 ucnv_bld_countAvailableConverters(UErrorCode *pErrorCode);
255 
256 /**
257  * Return the (n)th converter name in mixed case, or NULL
258  * if there is none (typically, if the data cannot be loaded).
259  * 0<=index<ucnv_io_countAvailableConverters().
260  * @param n The number specifies which converter name to get
261  * @param pErrorCode The error code
262  * @return the (n)th converter name in mixed case, or NULL if there is none.
263  */
264 U_CFUNC const char *
265 ucnv_bld_getAvailableConverter(uint16_t n, UErrorCode *pErrorCode);
266 
267 /**
268  * Load a non-algorithmic converter.
269  * If pkg==NULL, then this function must be called inside umtx_lock(&cnvCacheMutex).
270  */
271 U_CAPI UConverterSharedData *
272 ucnv_load(UConverterLoadArgs *pArgs, UErrorCode *err);
273 
274 /**
275  * Unload a non-algorithmic converter.
276  * It must be sharedData->isReferenceCounted
277  * and this function must be called inside umtx_lock(&cnvCacheMutex).
278  */
279 U_CAPI void
280 ucnv_unload(UConverterSharedData *sharedData);
281 
282 /**
283  * Swap ICU .cnv conversion tables. See udataswp.h.
284  * @internal
285  */
286 U_CAPI int32_t U_EXPORT2
287 ucnv_swap(const UDataSwapper *ds,
288           const void *inData, int32_t length, void *outData,
289           UErrorCode *pErrorCode);
290 
291 U_CAPI void U_EXPORT2
292 ucnv_enableCleanup();
293 
294 #endif
295 
296 #endif /* _UCNV_BLD */
297