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