Home | History | Annotate | Download | only in common
      1 /*
      2 **********************************************************************
      3 *   Copyright (C) 1999-2015 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, unused for static/immutable SharedData */
     97 
     98     const void *dataMemory;         /* from udata_openChoice() - for cleanup */
     99 
    100     const UConverterStaticData *staticData; /* pointer to the static (non changing) data. */
    101 
    102     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 */
    103     /** If FALSE, then referenceCounter is not used. Must not change after initialization. */
    104     UBool isReferenceCounted;
    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.
    119      *
    120      * markus 2003-nov-07
    121      */
    122     UConverterMBCSTable mbcs;
    123 };
    124 
    125 /** UConverterSharedData initializer for static, non-reference-counted converters. */
    126 #define UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(pStaticData, pImpl) \
    127     { \
    128         sizeof(UConverterSharedData), ~((uint32_t)0), \
    129         NULL, pStaticData, FALSE, FALSE, pImpl, \
    130         0, UCNV_MBCS_TABLE_INITIALIZER \
    131     }
    132 
    133 /* Defines a UConverter, the lightweight mutable part the user sees */
    134 
    135 struct UConverter {
    136     /*
    137      * Error function pointer called when conversion issues
    138      * occur during a ucnv_fromUnicode call
    139      */
    140     void (U_EXPORT2 *fromUCharErrorBehaviour) (const void *context,
    141                                      UConverterFromUnicodeArgs *args,
    142                                      const UChar *codeUnits,
    143                                      int32_t length,
    144                                      UChar32 codePoint,
    145                                      UConverterCallbackReason reason,
    146                                      UErrorCode *);
    147     /*
    148      * Error function pointer called when conversion issues
    149      * occur during a ucnv_toUnicode call
    150      */
    151     void (U_EXPORT2 *fromCharErrorBehaviour) (const void *context,
    152                                     UConverterToUnicodeArgs *args,
    153                                     const char *codeUnits,
    154                                     int32_t length,
    155                                     UConverterCallbackReason reason,
    156                                     UErrorCode *);
    157 
    158     /*
    159      * Pointer to additional data that depends on the converter type.
    160      * Used by ISO 2022, SCSU, GB 18030 converters, possibly more.
    161      */
    162     void *extraInfo;
    163 
    164     const void *fromUContext;
    165     const void *toUContext;
    166 
    167     /*
    168      * Pointer to charset bytes for substitution string if subCharLen>0,
    169      * or pointer to Unicode string (UChar *) if subCharLen<0.
    170      * subCharLen==0 is equivalent to using a skip callback.
    171      * If the pointer is !=subUChars then it is allocated with
    172      * UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR bytes.
    173      * The subUChars field is declared as UChar[] not uint8_t[] to
    174      * guarantee alignment for UChars.
    175      */
    176     uint8_t *subChars;
    177 
    178     UConverterSharedData *sharedData;   /* Pointer to the shared immutable part of the converter object */
    179 
    180     uint32_t options; /* options flags from UConverterOpen, may contain additional bits */
    181 
    182     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 */
    183     UBool isCopyLocal;  /* TRUE if UConverter is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */
    184     UBool isExtraLocal; /* TRUE if extraInfo is not owned and not released in ucnv_close() (stack-allocated, safeClone(), etc.) */
    185 
    186     UBool  useFallback;
    187     int8_t toULength;                   /* number of bytes in toUBytes */
    188     uint8_t toUBytes[UCNV_MAX_CHAR_LEN-1];/* more "toU status"; keeps the bytes of the current character */
    189     uint32_t toUnicodeStatus;           /* Used to internalize stream status information */
    190     int32_t mode;
    191     uint32_t fromUnicodeStatus;
    192 
    193     /*
    194      * More fromUnicode() status. Serves 3 purposes:
    195      * - keeps a lead surrogate between buffers (similar to toUBytes[])
    196      * - keeps a lead surrogate at the end of the stream,
    197      *   which the framework handles as truncated input
    198      * - if the fromUnicode() implementation returns to the framework
    199      *   (ucnv.c ucnv_fromUnicode()), then the framework calls the callback
    200      *   for this code point
    201      */
    202     UChar32 fromUChar32;
    203 
    204     /*
    205      * value for ucnv_getMaxCharSize()
    206      *
    207      * usually simply copied from the static data, but ucnvmbcs.c modifies
    208      * the value depending on the converter type and options
    209      */
    210     int8_t maxBytesPerUChar;
    211 
    212     int8_t subCharLen;                  /* length of the codepage specific character sequence */
    213     int8_t invalidCharLength;
    214     int8_t charErrorBufferLength;       /* number of valid bytes in charErrorBuffer */
    215 
    216     int8_t invalidUCharLength;
    217     int8_t UCharErrorBufferLength;      /* number of valid UChars in charErrorBuffer */
    218 
    219     uint8_t subChar1;                                   /* single-byte substitution character if different from subChar */
    220     UBool useSubChar1;
    221     char invalidCharBuffer[UCNV_MAX_CHAR_LEN];          /* bytes from last error/callback situation */
    222     uint8_t charErrorBuffer[UCNV_ERROR_BUFFER_LENGTH];  /* codepage output from Error functions */
    223     UChar subUChars[UCNV_MAX_SUBCHAR_LEN/U_SIZEOF_UCHAR]; /* see subChars documentation */
    224 
    225     UChar invalidUCharBuffer[U16_MAX_LENGTH];           /* UChars from last error/callback situation */
    226     UChar UCharErrorBuffer[UCNV_ERROR_BUFFER_LENGTH];   /* unicode output from Error functions */
    227 
    228     /* fields for conversion extension */
    229 
    230     /* store previous UChars/chars to continue partial matches */
    231     UChar32 preFromUFirstCP;                /* >=0: partial match */
    232     UChar preFromU[UCNV_EXT_MAX_UCHARS];
    233     char preToU[UCNV_EXT_MAX_BYTES];
    234     int8_t preFromULength, preToULength;    /* negative: replay */
    235     int8_t preToUFirstLength;               /* length of first character */
    236 
    237     /* new fields for ICU 4.0 */
    238     UConverterCallbackReason toUCallbackReason; /* (*fromCharErrorBehaviour) reason, set when error is detected */
    239 };
    240 
    241 U_CDECL_END /* end of UConverter */
    242 
    243 #define CONVERTER_FILE_EXTENSION ".cnv"
    244 
    245 
    246 /**
    247  * Return the number of all converter names.
    248  * @param pErrorCode The error code
    249  * @return the number of all converter names
    250  */
    251 U_CFUNC uint16_t
    252 ucnv_bld_countAvailableConverters(UErrorCode *pErrorCode);
    253 
    254 /**
    255  * Return the (n)th converter name in mixed case, or NULL
    256  * if there is none (typically, if the data cannot be loaded).
    257  * 0<=index<ucnv_io_countAvailableConverters().
    258  * @param n The number specifies which converter name to get
    259  * @param pErrorCode The error code
    260  * @return the (n)th converter name in mixed case, or NULL if there is none.
    261  */
    262 U_CFUNC const char *
    263 ucnv_bld_getAvailableConverter(uint16_t n, UErrorCode *pErrorCode);
    264 
    265 /**
    266  * Load a non-algorithmic converter.
    267  * If pkg==NULL, then this function must be called inside umtx_lock(&cnvCacheMutex).
    268  */
    269 U_CAPI UConverterSharedData *
    270 ucnv_load(UConverterLoadArgs *pArgs, UErrorCode *err);
    271 
    272 /**
    273  * Unload a non-algorithmic converter.
    274  * It must be sharedData->isReferenceCounted
    275  * and this function must be called inside umtx_lock(&cnvCacheMutex).
    276  */
    277 U_CAPI void
    278 ucnv_unload(UConverterSharedData *sharedData);
    279 
    280 /**
    281  * Swap ICU .cnv conversion tables. See udataswp.h.
    282  * @internal
    283  */
    284 U_CAPI int32_t U_EXPORT2
    285 ucnv_swap(const UDataSwapper *ds,
    286           const void *inData, int32_t length, void *outData,
    287           UErrorCode *pErrorCode);
    288 
    289 #endif
    290 
    291 #endif /* _UCNV_BLD */
    292