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