// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ********************************************************************** * Copyright (C) 1999-2014, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * ucnv.h: * External APIs for the ICU's codeset conversion library * Bertrand A. Damiba * * Modification History: * * Date Name Description * 04/04/99 helena Fixed internal header inclusion. * 05/11/00 helena Added setFallback and usesFallback APIs. * 06/29/2000 helena Major rewrite of the callback APIs. * 12/07/2000 srl Update of documentation */ /** * \file * \brief C API: Character conversion * *
This API is used to convert codepage or character encoded data to and * from UTF-16. You can open a converter with {@link ucnv_open() }. With that * converter, you can get its properties, set options, convert your data and * close the converter.
* *Since many software programs recognize different converter names for * different types of converters, there are other functions in this API to * iterate over the converter aliases. The functions {@link ucnv_getAvailableName() }, * {@link ucnv_getAlias() } and {@link ucnv_getStandardName() } are some of the * more frequently used alias functions to get this information.
* *When a converter encounters an illegal, irregular, invalid or unmappable character * its default behavior is to use a substitution character to replace the * bad byte sequence. This behavior can be changed by using {@link ucnv_setFromUCallBack() } * or {@link ucnv_setToUCallBack() } on the converter. The header ucnv_err.h defines * many other callback actions that can be used instead of a character substitution.
* *More information about this API can be found in our * User Guide.
*/ #ifndef UCNV_H #define UCNV_H #include "unicode/ucnv_err.h" #include "unicode/uenum.h" #if U_SHOW_CPLUSPLUS_API #include "unicode/localpointer.h" #endif // U_SHOW_CPLUSPLUS_API #if !defined(USET_DEFINED) && !defined(U_IN_DOXYGEN) #define USET_DEFINED /** * USet is the C API type corresponding to C++ class UnicodeSet. * It is forward-declared here to avoid including unicode/uset.h file if related * conversion APIs are not used. * * @see ucnv_getUnicodeSet * @stable ICU 2.4 */ typedef struct USet USet; #endif #if !UCONFIG_NO_CONVERSION U_CDECL_BEGIN /** Maximum length of a converter name including the terminating NULL @stable ICU 2.0 */ #define UCNV_MAX_CONVERTER_NAME_LENGTH 60 /** Maximum length of a converter name including path and terminating NULL @stable ICU 2.0 */ #define UCNV_MAX_FULL_FILE_NAME_LENGTH (600+UCNV_MAX_CONVERTER_NAME_LENGTH) /** Shift in for EBDCDIC_STATEFUL and iso2022 states @stable ICU 2.0 */ #define UCNV_SI 0x0F /** Shift out for EBDCDIC_STATEFUL and iso2022 states @stable ICU 2.0 */ #define UCNV_SO 0x0E /** * Enum for specifying basic types of converters * @see ucnv_getType * @stable ICU 2.0 */ typedef enum { /** @stable ICU 2.0 */ UCNV_UNSUPPORTED_CONVERTER = -1, /** @stable ICU 2.0 */ UCNV_SBCS = 0, /** @stable ICU 2.0 */ UCNV_DBCS = 1, /** @stable ICU 2.0 */ UCNV_MBCS = 2, /** @stable ICU 2.0 */ UCNV_LATIN_1 = 3, /** @stable ICU 2.0 */ UCNV_UTF8 = 4, /** @stable ICU 2.0 */ UCNV_UTF16_BigEndian = 5, /** @stable ICU 2.0 */ UCNV_UTF16_LittleEndian = 6, /** @stable ICU 2.0 */ UCNV_UTF32_BigEndian = 7, /** @stable ICU 2.0 */ UCNV_UTF32_LittleEndian = 8, /** @stable ICU 2.0 */ UCNV_EBCDIC_STATEFUL = 9, /** @stable ICU 2.0 */ UCNV_ISO_2022 = 10, /** @stable ICU 2.0 */ UCNV_LMBCS_1 = 11, /** @stable ICU 2.0 */ UCNV_LMBCS_2, /** @stable ICU 2.0 */ UCNV_LMBCS_3, /** @stable ICU 2.0 */ UCNV_LMBCS_4, /** @stable ICU 2.0 */ UCNV_LMBCS_5, /** @stable ICU 2.0 */ UCNV_LMBCS_6, /** @stable ICU 2.0 */ UCNV_LMBCS_8, /** @stable ICU 2.0 */ UCNV_LMBCS_11, /** @stable ICU 2.0 */ UCNV_LMBCS_16, /** @stable ICU 2.0 */ UCNV_LMBCS_17, /** @stable ICU 2.0 */ UCNV_LMBCS_18, /** @stable ICU 2.0 */ UCNV_LMBCS_19, /** @stable ICU 2.0 */ UCNV_LMBCS_LAST = UCNV_LMBCS_19, /** @stable ICU 2.0 */ UCNV_HZ, /** @stable ICU 2.0 */ UCNV_SCSU, /** @stable ICU 2.0 */ UCNV_ISCII, /** @stable ICU 2.0 */ UCNV_US_ASCII, /** @stable ICU 2.0 */ UCNV_UTF7, /** @stable ICU 2.2 */ UCNV_BOCU1, /** @stable ICU 2.2 */ UCNV_UTF16, /** @stable ICU 2.2 */ UCNV_UTF32, /** @stable ICU 2.2 */ UCNV_CESU8, /** @stable ICU 2.4 */ UCNV_IMAP_MAILBOX, /** @stable ICU 4.8 */ UCNV_COMPOUND_TEXT, /* Number of converter types for which we have conversion routines. */ UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES } UConverterType; /** * Enum for specifying which platform a converter ID refers to. * The use of platform/CCSID is not recommended. See ucnv_openCCSID(). * * @see ucnv_getPlatform * @see ucnv_openCCSID * @see ucnv_getCCSID * @stable ICU 2.0 */ typedef enum { UCNV_UNKNOWN = -1, UCNV_IBM = 0 } UConverterPlatform; /** * Function pointer for error callback in the codepage to unicode direction. * Called when an error has occurred in conversion to unicode, or on open/close of the callback (see reason). * @param context Pointer to the callback's private data * @param args Information about the conversion in progress * @param codeUnits Points to 'length' bytes of the concerned codepage sequence * @param length Size (in bytes) of the concerned codepage sequence * @param reason Defines the reason the callback was invoked * @param pErrorCode ICU error code in/out parameter. * For converter callback functions, set to a conversion error * before the call, and the callback may reset it to U_ZERO_ERROR. * @see ucnv_setToUCallBack * @see UConverterToUnicodeArgs * @stable ICU 2.0 */ typedef void (U_EXPORT2 *UConverterToUCallback) ( const void* context, UConverterToUnicodeArgs *args, const char *codeUnits, int32_t length, UConverterCallbackReason reason, UErrorCode *pErrorCode); /** * Function pointer for error callback in the unicode to codepage direction. * Called when an error has occurred in conversion from unicode, or on open/close of the callback (see reason). * @param context Pointer to the callback's private data * @param args Information about the conversion in progress * @param codeUnits Points to 'length' UChars of the concerned Unicode sequence * @param length Size (in bytes) of the concerned codepage sequence * @param codePoint Single UChar32 (UTF-32) containing the concerend Unicode codepoint. * @param reason Defines the reason the callback was invoked * @param pErrorCode ICU error code in/out parameter. * For converter callback functions, set to a conversion error * before the call, and the callback may reset it to U_ZERO_ERROR. * @see ucnv_setFromUCallBack * @stable ICU 2.0 */ typedef void (U_EXPORT2 *UConverterFromUCallback) ( const void* context, UConverterFromUnicodeArgs *args, const UChar* codeUnits, int32_t length, UChar32 codePoint, UConverterCallbackReason reason, UErrorCode *pErrorCode); U_CDECL_END /** * Character that separates converter names from options and options from each other. * @see ucnv_open * @stable ICU 2.0 */ #define UCNV_OPTION_SEP_CHAR ',' /** * String version of UCNV_OPTION_SEP_CHAR. * @see ucnv_open * @stable ICU 2.0 */ #define UCNV_OPTION_SEP_STRING "," /** * Character that separates a converter option from its value. * @see ucnv_open * @stable ICU 2.0 */ #define UCNV_VALUE_SEP_CHAR '=' /** * String version of UCNV_VALUE_SEP_CHAR. * @see ucnv_open * @stable ICU 2.0 */ #define UCNV_VALUE_SEP_STRING "=" /** * Converter option for specifying a locale. * For example, ucnv_open("SCSU,locale=ja", &errorCode); * See convrtrs.txt. * * @see ucnv_open * @stable ICU 2.0 */ #define UCNV_LOCALE_OPTION_STRING ",locale=" /** * Converter option for specifying a version selector (0..9) for some converters. * For example, * \code * ucnv_open("UTF-7,version=1", &errorCode); * \endcode * See convrtrs.txt. * * @see ucnv_open * @stable ICU 2.4 */ #define UCNV_VERSION_OPTION_STRING ",version=" /** * Converter option for EBCDIC SBCS or mixed-SBCS/DBCS (stateful) codepages. * Swaps Unicode mappings for EBCDIC LF and NL codes, as used on * S/390 (z/OS) Unix System Services (Open Edition). * For example, ucnv_open("ibm-1047,swaplfnl", &errorCode); * See convrtrs.txt. * * @see ucnv_open * @stable ICU 2.4 */ #define UCNV_SWAP_LFNL_OPTION_STRING ",swaplfnl" /** * Do a fuzzy compare of two converter/alias names. * The comparison is case-insensitive, ignores leading zeroes if they are not * followed by further digits, and ignores all but letters and digits. * Thus the strings "UTF-8", "utf_8", "u*T@f08" and "Utf 8" are exactly equivalent. * See section 1.4, Charset Alias Matching in Unicode Technical Standard #22 * at http://www.unicode.org/reports/tr22/ * * @param name1 a converter name or alias, zero-terminated * @param name2 a converter name or alias, zero-terminated * @return 0 if the names match, or a negative value if the name1 * lexically precedes name2, or a positive value if the name1 * lexically follows name2. * @stable ICU 2.0 */ U_CAPI int U_EXPORT2 ucnv_compareNames(const char *name1, const char *name2); /** * Creates a UConverter object with the name of a coded character set specified as a C string. * The actual name will be resolved with the alias file * using a case-insensitive string comparison that ignores * leading zeroes and all non-alphanumeric characters. * E.g., the names "UTF8", "utf-8", "u*T@f08" and "Utf 8" are all equivalent. * (See also ucnv_compareNames().) * IfNULL
is passed for the converter name, it will create one with the
* getDefaultName return value.
*
* A converter name for ICU 1.5 and above may contain options * like a locale specification to control the specific behavior of * the newly instantiated converter. * The meaning of the options depends on the particular converter. * If an option is not defined for or recognized by a given converter, then it is ignored.
* *Options are appended to the converter name string, with a
* UCNV_OPTION_SEP_CHAR
between the name and the first option and
* also between adjacent options.
If the alias is ambiguous, then the preferred converter is used * and the status is set to U_AMBIGUOUS_ALIAS_WARNING.
* *The conversion behavior and names can vary between platforms. ICU may * convert some characters differently from other platforms. Details on this topic * are in the User * Guide. Aliases starting with a "cp" prefix have no specific meaning * other than its an alias starting with the letters "cp". Please do not * associate any meaning to these aliases.
* * @param converterName Name of the coded character set table. * This may have options appended to the string. * IANA alias character set names, IBM CCSIDs starting with "ibm-", * Windows codepage numbers starting with "windows-" are frequently * used for this parameter. See ucnv_getAvailableName and * ucnv_getAlias for a complete list that is available. * If this parameter is NULL, the default converter will be used. * @param err outgoing error status U_MEMORY_ALLOCATION_ERROR, U_FILE_ACCESS_ERROR * @return the created Unicode converter object, or NULL if an error occurred * @see ucnv_openU * @see ucnv_openCCSID * @see ucnv_getAvailableName * @see ucnv_getAlias * @see ucnv_getDefaultName * @see ucnv_close * @see ucnv_compareNames * @stable ICU 2.0 */ U_CAPI UConverter* U_EXPORT2 ucnv_open(const char *converterName, UErrorCode *err); /** * Creates a Unicode converter with the names specified as unicode string. * The name should be limited to the ASCII-7 alphanumerics range. * The actual name will be resolved with the alias file * using a case-insensitive string comparison that ignores * leading zeroes and all non-alphanumeric characters. * E.g., the names "UTF8", "utf-8", "u*T@f08" and "Utf 8" are all equivalent. * (See also ucnv_compareNames().) * If NULL is passed for the converter name, it will create * one with the ucnv_getDefaultName() return value. * If the alias is ambiguous, then the preferred converter is used * and the status is set to U_AMBIGUOUS_ALIAS_WARNING. * *See ucnv_open for the complete details
* @param name Name of the UConverter table in a zero terminated * Unicode string * @param err outgoing error status U_MEMORY_ALLOCATION_ERROR, * U_FILE_ACCESS_ERROR * @return the created Unicode converter object, or NULL if an * error occurred * @see ucnv_open * @see ucnv_openCCSID * @see ucnv_close * @see ucnv_compareNames * @stable ICU 2.0 */ U_CAPI UConverter* U_EXPORT2 ucnv_openU(const UChar *name, UErrorCode *err); /** * Creates a UConverter object from a CCSID number and platform pair. * Note that the usefulness of this function is limited to platforms with numeric * encoding IDs. Only IBM and Microsoft platforms use numeric (16-bit) identifiers for * encodings. * * In addition, IBM CCSIDs and Unicode conversion tables are not 1:1 related. * For many IBM CCSIDs there are multiple (up to six) Unicode conversion tables, and * for some Unicode conversion tables there are multiple CCSIDs. * Some "alternate" Unicode conversion tables are provided by the * IBM CDRA conversion table registry. * The most prominent example of a systematic modification of conversion tables that is * not provided in the form of conversion table files in the repository is * that S/390 Unix System Services swaps the codes for Line Feed and New Line in all * EBCDIC codepages, which requires such a swap in the Unicode conversion tables as well. * * Only IBM default conversion tables are accessible with ucnv_openCCSID(). * ucnv_getCCSID() will return the same CCSID for all conversion tables that are associated * with that CCSID. * * Currently, the only "platform" supported in the ICU converter API is UCNV_IBM. * * In summary, the use of CCSIDs and the associated API functions is not recommended. * * In order to open a converter with the default IBM CDRA Unicode conversion table, * you can use this function or use the prefix "ibm-": * \code * char name[20]; * sprintf(name, "ibm-%hu", ccsid); * cnv=ucnv_open(name, &errorCode); * \endcode * * In order to open a converter with the IBM S/390 Unix System Services variant * of a Unicode/EBCDIC conversion table, * you can use the prefix "ibm-" together with the option string UCNV_SWAP_LFNL_OPTION_STRING: * \code * char name[20]; * sprintf(name, "ibm-%hu" UCNV_SWAP_LFNL_OPTION_STRING, ccsid); * cnv=ucnv_open(name, &errorCode); * \endcode * * In order to open a converter from a Microsoft codepage number, use the prefix "cp": * \code * char name[20]; * sprintf(name, "cp%hu", codepageID); * cnv=ucnv_open(name, &errorCode); * \endcode * * If the alias is ambiguous, then the preferred converter is used * and the status is set to U_AMBIGUOUS_ALIAS_WARNING. * * @param codepage codepage number to create * @param platform the platform in which the codepage number exists * @param err error status U_MEMORY_ALLOCATION_ERROR, U_FILE_ACCESS_ERROR * @return the created Unicode converter object, or NULL if an error * occurred. * @see ucnv_open * @see ucnv_openU * @see ucnv_close * @see ucnv_getCCSID * @see ucnv_getPlatform * @see UConverterPlatform * @stable ICU 2.0 */ U_CAPI UConverter* U_EXPORT2 ucnv_openCCSID(int32_t codepage, UConverterPlatform platform, UErrorCode * err); /** *Creates a UConverter object specified from a packageName and a converterName.
* *The packageName and converterName must point to an ICU udata object, as defined by
* udata_open( packageName, "cnv", converterName, err)
or equivalent.
* Typically, packageName will refer to a (.dat) file, or to a package registered with
* udata_setAppData(). Using a full file or directory pathname for packageName is deprecated.
The name will NOT be looked up in the alias mechanism, nor will the converter be * stored in the converter cache or the alias table. The only way to open further converters * is call this function multiple times, or use the ucnv_clone() function to clone a * 'primary' converter.
* *A future version of ICU may add alias table lookups and/or caching * to this function.
* *Example Use:
* cnv = ucnv_openPackage("myapp", "myconverter", &err);
*
U_BUFFER_OVERFLOW_ERROR
will be set if the target is full and there is
* still data to be written to the target.
* @see ucnv_fromUChars
* @see ucnv_convert
* @see ucnv_getMinCharSize
* @see ucnv_setToUCallBack
* @stable ICU 2.0
*/
U_CAPI void U_EXPORT2
ucnv_fromUnicode (UConverter * converter,
char **target,
const char *targetLimit,
const UChar ** source,
const UChar * sourceLimit,
int32_t* offsets,
UBool flush,
UErrorCode * err);
/**
* Converts a buffer of codepage bytes into an array of unicode UChars
* characters. This function is optimized for converting a continuous
* stream of data in buffer-sized chunks, where the entire source and
* target does not fit in available buffers.
*
* The source pointer is an in/out parameter. It starts out pointing where the
* conversion is to begin, and ends up pointing after the last byte of source consumed.
*
* Target similarly starts out pointer at the first available UChar in the output
* buffer, and ends up pointing after the last UChar written to the output.
* It does NOT necessarily keep UChar sequences together.
*
* The converter always attempts to consume the entire source buffer, unless
* (1.) the target buffer is full, or (2.) a failing error is returned from the
* current callback function. When a successful error status has been
* returned, it means that all of the source buffer has been
* consumed. At that point, the caller should reset the source and
* sourceLimit pointers to point to the next chunk.
*
* At the end of the stream (flush==true), the input is completely consumed
* when *source==sourceLimit and no error code is set
* The converter object is then automatically reset by this function.
* (This means that a converter need not be reset explicitly between data
* streams if it finishes the previous stream without errors.)
*
* This is a stateful conversion. Additionally, even when all source data has
* been consumed, some data may be in the converters' internal state.
* Call this function repeatedly, updating the target pointers with
* the next empty chunk of target in case of a
* U_BUFFER_OVERFLOW_ERROR, and updating the source pointers
* with the next chunk of source when a successful error status is
* returned, until there are no more chunks of source data.
* @param converter the Unicode converter
* @param target I/O parameter. Input : Points to the beginning of the buffer to copy
* UChars into. Output : points to after the last UChar copied.
* @param targetLimit the pointer just after the end of the target buffer
* @param source I/O parameter, pointer to pointer to the source codepage buffer.
* @param sourceLimit the pointer to the byte after the end of the source buffer
* @param offsets if NULL is passed, nothing will happen to it, otherwise it needs to have the same number
* of allocated cells as target. Will fill in offsets from target to source pointer
* e.g: offsets[3] is equal to 6, it means that the target[3] was a result of transcoding source[6]
* For output data carried across calls, and other data without a specific source character
* (such as from escape sequences or callbacks) -1 will be placed for offsets.
* @param flush set to true if the current source buffer is the last available
* chunk of the source, false otherwise. Note that if a failing status is returned,
* this function may have to be called multiple times with flush set to true until
* the source buffer is consumed.
* @param err the error status. U_ILLEGAL_ARGUMENT_ERROR will be set if the
* converter is NULL.
* U_BUFFER_OVERFLOW_ERROR
will be set if the target is full and there is
* still data to be written to the target.
* @see ucnv_fromUChars
* @see ucnv_convert
* @see ucnv_getMinCharSize
* @see ucnv_setFromUCallBack
* @see ucnv_getNextUChar
* @stable ICU 2.0
*/
U_CAPI void U_EXPORT2
ucnv_toUnicode(UConverter *converter,
UChar **target,
const UChar *targetLimit,
const char **source,
const char *sourceLimit,
int32_t *offsets,
UBool flush,
UErrorCode *err);
/**
* Convert the Unicode string into a codepage string using an existing UConverter.
* The output string is NUL-terminated if possible.
*
* This function is a more convenient but less powerful version of ucnv_fromUnicode().
* It is only useful for whole strings, not for streaming conversion.
*
* The maximum output buffer capacity required (barring output from callbacks) will be
* UCNV_GET_MAX_BYTES_FOR_STRING(srcLength, ucnv_getMaxCharSize(cnv)).
*
* @param cnv the converter object to be used (ucnv_resetFromUnicode() will be called)
* @param src the input Unicode string
* @param srcLength the input string length, or -1 if NUL-terminated
* @param dest destination string buffer, can be NULL if destCapacity==0
* @param destCapacity the number of chars available at dest
* @param pErrorCode normal ICU error code;
* common error codes that may be set by this function include
* U_BUFFER_OVERFLOW_ERROR, U_STRING_NOT_TERMINATED_WARNING,
* U_ILLEGAL_ARGUMENT_ERROR, and conversion errors
* @return the length of the output string, not counting the terminating NUL;
* if the length is greater than destCapacity, then the string will not fit
* and a buffer of the indicated length would need to be passed in
* @see ucnv_fromUnicode
* @see ucnv_convert
* @see UCNV_GET_MAX_BYTES_FOR_STRING
* @stable ICU 2.0
*/
U_CAPI int32_t U_EXPORT2
ucnv_fromUChars(UConverter *cnv,
char *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
UErrorCode *pErrorCode);
/**
* Convert the codepage string into a Unicode string using an existing UConverter.
* The output string is NUL-terminated if possible.
*
* This function is a more convenient but less powerful version of ucnv_toUnicode().
* It is only useful for whole strings, not for streaming conversion.
*
* The maximum output buffer capacity required (barring output from callbacks) will be
* 2*srcLength (each char may be converted into a surrogate pair).
*
* @param cnv the converter object to be used (ucnv_resetToUnicode() will be called)
* @param src the input codepage string
* @param srcLength the input string length, or -1 if NUL-terminated
* @param dest destination string buffer, can be NULL if destCapacity==0
* @param destCapacity the number of UChars available at dest
* @param pErrorCode normal ICU error code;
* common error codes that may be set by this function include
* U_BUFFER_OVERFLOW_ERROR, U_STRING_NOT_TERMINATED_WARNING,
* U_ILLEGAL_ARGUMENT_ERROR, and conversion errors
* @return the length of the output string, not counting the terminating NUL;
* if the length is greater than destCapacity, then the string will not fit
* and a buffer of the indicated length would need to be passed in
* @see ucnv_toUnicode
* @see ucnv_convert
* @stable ICU 2.0
*/
U_CAPI int32_t U_EXPORT2
ucnv_toUChars(UConverter *cnv,
UChar *dest, int32_t destCapacity,
const char *src, int32_t srcLength,
UErrorCode *pErrorCode);
/**
* Convert a codepage buffer into Unicode one character at a time.
* The input is completely consumed when the U_INDEX_OUTOFBOUNDS_ERROR is set.
*
* Advantage compared to ucnv_toUnicode() or ucnv_toUChars():
* - Faster for small amounts of data, for most converters, e.g.,
* US-ASCII, ISO-8859-1, UTF-8/16/32, and most "normal" charsets.
* (For complex converters, e.g., SCSU, UTF-7 and ISO 2022 variants,
* it uses ucnv_toUnicode() internally.)
* - Convenient.
*
* Limitations compared to ucnv_toUnicode():
* - Always assumes flush=true.
* This makes ucnv_getNextUChar() unsuitable for "streaming" conversion,
* that is, for where the input is supplied in multiple buffers,
* because ucnv_getNextUChar() will assume the end of the input at the end
* of the first buffer.
* - Does not provide offset output.
*
* It is possible to "mix" ucnv_getNextUChar() and ucnv_toUnicode() because
* ucnv_getNextUChar() uses the current state of the converter
* (unlike ucnv_toUChars() which always resets first).
* However, if ucnv_getNextUChar() is called after ucnv_toUnicode()
* stopped in the middle of a character sequence (with flush=false),
* then ucnv_getNextUChar() will always use the slower ucnv_toUnicode()
* internally until the next character boundary.
* (This is new in ICU 2.6. In earlier releases, ucnv_getNextUChar() had to
* start at a character boundary.)
*
* Instead of using ucnv_getNextUChar(), it is recommended
* to convert using ucnv_toUnicode() or ucnv_toUChars()
* and then iterate over the text using U16_NEXT() or a UCharIterator (uiter.h)
* or a C++ CharacterIterator or similar.
* This allows streaming conversion and offset output, for example.
*
* Handling of surrogate pairs and supplementary-plane code points:
* There are two different kinds of codepages that provide mappings for surrogate characters:
*
U_INDEX_OUTOFBOUNDS_ERROR
will be set if the input
* is empty or does not convert to any output (e.g.: pure state-change
* codes SI/SO, escape sequences for ISO 2022,
* or if the callback did not output anything, ...).
* This function will not set a U_BUFFER_OVERFLOW_ERROR
because
* the "buffer" is the return code. However, there might be subsequent output
* stored in the converter object
* that will be returned in following calls to this function.
* @return a UChar32 resulting from the partial conversion of source
* @see ucnv_toUnicode
* @see ucnv_toUChars
* @see ucnv_convert
* @stable ICU 2.0
*/
U_CAPI UChar32 U_EXPORT2
ucnv_getNextUChar(UConverter * converter,
const char **source,
const char * sourceLimit,
UErrorCode * err);
/**
* Convert from one external charset to another using two existing UConverters.
* Internally, two conversions - ucnv_toUnicode() and ucnv_fromUnicode() -
* are used, "pivoting" through 16-bit Unicode.
*
* Important: For streaming conversion (multiple function calls for successive
* parts of a text stream), the caller must provide a pivot buffer explicitly,
* and must preserve the pivot buffer and associated pointers from one
* call to another. (The buffer may be moved if its contents and the relative
* pointer positions are preserved.)
*
* There is a similar function, ucnv_convert(),
* which has the following limitations:
* - it takes charset names, not converter objects, so that
* - two converters are opened for each call
* - only single-string conversion is possible, not streaming operation
* - it does not provide enough information to find out,
* in case of failure, whether the toUnicode or
* the fromUnicode conversion failed
*
* By contrast, ucnv_convertEx()
* - takes UConverter parameters instead of charset names
* - fully exposes the pivot buffer for streaming conversion and complete error handling
*
* ucnv_convertEx() also provides further convenience:
* - an option to reset the converters at the beginning
* (if reset==true, see parameters;
* also sets *pivotTarget=*pivotSource=pivotStart)
* - allow NUL-terminated input
* (only a single NUL byte, will not work for charsets with multi-byte NULs)
* (if sourceLimit==NULL, see parameters)
* - terminate with a NUL on output
* (only a single NUL byte, not useful for charsets with multi-byte NULs),
* or set U_STRING_NOT_TERMINATED_WARNING if the output exactly fills
* the target buffer
* - the pivot buffer can be provided internally;
* possible only for whole-string conversion, not streaming conversion;
* in this case, the caller will not be able to get details about where an
* error occurred
* (if pivotStart==NULL, see below)
*
* The function returns when one of the following is true:
* - the entire source text has been converted successfully to the target buffer
* - a target buffer overflow occurred (U_BUFFER_OVERFLOW_ERROR)
* - a conversion error occurred
* (other U_FAILURE(), see description of pErrorCode)
*
* Limitation compared to the direct use of
* ucnv_fromUnicode() and ucnv_toUnicode():
* ucnv_convertEx() does not provide offset information.
*
* Limitation compared to ucnv_fromUChars() and ucnv_toUChars():
* ucnv_convertEx() does not support preflighting directly.
*
* Sample code for converting a single string from
* one external charset to UTF-8, ignoring the location of errors:
*
* \code
* int32_t
* myToUTF8(UConverter *cnv,
* const char *s, int32_t length,
* char *u8, int32_t capacity,
* UErrorCode *pErrorCode) {
* UConverter *utf8Cnv;
* char *target;
*
* if(U_FAILURE(*pErrorCode)) {
* return 0;
* }
*
* utf8Cnv=myGetCachedUTF8Converter(pErrorCode);
* if(U_FAILURE(*pErrorCode)) {
* return 0;
* }
*
* if(length<0) {
* length=strlen(s);
* }
* target=u8;
* ucnv_convertEx(utf8Cnv, cnv,
* &target, u8+capacity,
* &s, s+length,
* NULL, NULL, NULL, NULL,
* true, true,
* pErrorCode);
*
* myReleaseCachedUTF8Converter(utf8Cnv);
*
* // return the output string length, but without preflighting
* return (int32_t)(target-u8);
* }
* \endcode
*
* @param targetCnv Output converter, used to convert from the UTF-16 pivot
* to the target using ucnv_fromUnicode().
* @param sourceCnv Input converter, used to convert from the source to
* the UTF-16 pivot using ucnv_toUnicode().
* @param target I/O parameter, same as for ucnv_fromUChars().
* Input: *target points to the beginning of the target buffer.
* Output: *target points to the first unit after the last char written.
* @param targetLimit Pointer to the first unit after the target buffer.
* @param source I/O parameter, same as for ucnv_toUChars().
* Input: *source points to the beginning of the source buffer.
* Output: *source points to the first unit after the last char read.
* @param sourceLimit Pointer to the first unit after the source buffer.
* @param pivotStart Pointer to the UTF-16 pivot buffer. If pivotStart==NULL,
* then an internal buffer is used and the other pivot
* arguments are ignored and can be NULL as well.
* @param pivotSource I/O parameter, same as source in ucnv_fromUChars() for
* conversion from the pivot buffer to the target buffer.
* @param pivotTarget I/O parameter, same as target in ucnv_toUChars() for
* conversion from the source buffer to the pivot buffer.
* It must be pivotStart<=*pivotSource<=*pivotTarget<=pivotLimit
* and pivotStartucnv_countAliases()
string-pointers
* (const char *
) that will be filled in.
* The strings themselves are owned by the library.
* @param pErrorCode result of operation
* @stable ICU 2.0
*/
U_CAPI void U_EXPORT2
ucnv_getAliases(const char *alias, const char **aliases, UErrorCode *pErrorCode);
/**
* Return a new UEnumeration object for enumerating all the
* alias names for a given converter that are recognized by a standard.
* This method only enumerates the listed entries in the alias file.
* The convrtrs.txt file can be modified to change the results of
* this function.
* The first result in this list is the same result given by
* ucnv_getStandardName
, which is the default alias for
* the specified standard name. The returned object must be closed with
* uenum_close
when you are done with the object.
*
* @param convName original converter name
* @param standard name of the standard governing the names; MIME and IANA
* are such standards
* @param pErrorCode The error code
* @return A UEnumeration object for getting all aliases that are recognized
* by a standard. If any of the parameters are invalid, NULL
* is returned.
* @see ucnv_getStandardName
* @see uenum_close
* @see uenum_next
* @stable ICU 2.2
*/
U_CAPI UEnumeration * U_EXPORT2
ucnv_openStandardNames(const char *convName,
const char *standard,
UErrorCode *pErrorCode);
/**
* Gives the number of standards associated to converter names.
* @return number of standards
* @stable ICU 2.0
*/
U_CAPI uint16_t U_EXPORT2
ucnv_countStandards(void);
/**
* Gives the name of the standard at given index of standard list.
* @param n index in standard list
* @param pErrorCode result of operation
* @return returns the name of the standard at given index. Owned by the library.
* @stable ICU 2.0
*/
U_CAPI const char * U_EXPORT2
ucnv_getStandard(uint16_t n, UErrorCode *pErrorCode);
/**
* Returns a standard name for a given converter name.
*
* Example alias table:
* conv alias1 { STANDARD1 } alias2 { STANDARD1* }
*
* Result of ucnv_getStandardName("conv", "STANDARD1") from example
* alias table:
* "alias2"
*
* @param name original converter name
* @param standard name of the standard governing the names; MIME and IANA
* are such standards
* @param pErrorCode result of operation
* @return returns the standard converter name;
* if a standard converter name cannot be determined,
* then NULL
is returned. Owned by the library.
* @stable ICU 2.0
*/
U_CAPI const char * U_EXPORT2
ucnv_getStandardName(const char *name, const char *standard, UErrorCode *pErrorCode);
/**
* This function will return the internal canonical converter name of the
* tagged alias. This is the opposite of ucnv_openStandardNames, which
* returns the tagged alias given the canonical name.
*
* Example alias table:
* conv alias1 { STANDARD1 } alias2 { STANDARD1* }
*
* Result of ucnv_getStandardName("alias1", "STANDARD1") from example
* alias table:
* "conv"
*
* @return returns the canonical converter name;
* if a standard or alias name cannot be determined,
* then NULL
is returned. The returned string is
* owned by the library.
* @see ucnv_getStandardName
* @stable ICU 2.4
*/
U_CAPI const char * U_EXPORT2
ucnv_getCanonicalName(const char *alias, const char *standard, UErrorCode *pErrorCode);
/**
* Returns the current default converter name. If you want to open
* a default converter, you do not need to use this function.
* It is faster if you pass a NULL argument to ucnv_open the
* default converter.
*
* If U_CHARSET_IS_UTF8 is defined to 1 in utypes.h then this function
* always returns "UTF-8".
*
* @return returns the current default converter name.
* Storage owned by the library
* @see ucnv_setDefaultName
* @stable ICU 2.0
*/
U_CAPI const char * U_EXPORT2
ucnv_getDefaultName(void);
#ifndef U_HIDE_SYSTEM_API
/**
* This function is not thread safe. DO NOT call this function when ANY ICU
* function is being used from more than one thread! This function sets the
* current default converter name. If this function needs to be called, it
* should be called during application initialization. Most of the time, the
* results from ucnv_getDefaultName() or ucnv_open with a NULL string argument
* is sufficient for your application.
*
* If U_CHARSET_IS_UTF8 is defined to 1 in utypes.h then this function
* does nothing.
*
* @param name the converter name to be the default (must be known by ICU).
* @see ucnv_getDefaultName
* @system
* @stable ICU 2.0
*/
U_CAPI void U_EXPORT2
ucnv_setDefaultName(const char *name);
#endif /* U_HIDE_SYSTEM_API */
/**
* Fixes the backslash character mismapping. For example, in SJIS, the backslash
* character in the ASCII portion is also used to represent the yen currency sign.
* When mapping from Unicode character 0x005C, it's unclear whether to map the
* character back to yen or backslash in SJIS. This function will take the input
* buffer and replace all the yen sign characters with backslash. This is necessary
* when the user tries to open a file with the input buffer on Windows.
* This function will test the converter to see whether such mapping is
* required. You can sometimes avoid using this function by using the correct version
* of Shift-JIS.
*
* @param cnv The converter representing the target codepage.
* @param source the input buffer to be fixed
* @param sourceLen the length of the input buffer
* @see ucnv_isAmbiguous
* @stable ICU 2.0
*/
U_CAPI void U_EXPORT2
ucnv_fixFileSeparator(const UConverter *cnv, UChar *source, int32_t sourceLen);
/**
* Determines if the converter contains ambiguous mappings of the same
* character or not.
* @param cnv the converter to be tested
* @return true if the converter contains ambiguous mapping of the same
* character, false otherwise.
* @stable ICU 2.0
*/
U_CAPI UBool U_EXPORT2
ucnv_isAmbiguous(const UConverter *cnv);
/**
* Sets the converter to use fallback mappings or not.
* Regardless of this flag, the converter will always use
* fallbacks from Unicode Private Use code points, as well as
* reverse fallbacks (to Unicode).
* For details see ".ucm File Format"
* in the Conversion Data chapter of the ICU User Guide:
* https://unicode-org.github.io/icu/userguide/conversion/data.html#ucm-file-format
*
* @param cnv The converter to set the fallback mapping usage on.
* @param usesFallback true if the user wants the converter to take advantage of the fallback
* mapping, false otherwise.
* @stable ICU 2.0
* @see ucnv_usesFallback
*/
U_CAPI void U_EXPORT2
ucnv_setFallback(UConverter *cnv, UBool usesFallback);
/**
* Determines if the converter uses fallback mappings or not.
* This flag has restrictions, see ucnv_setFallback().
*
* @param cnv The converter to be tested
* @return true if the converter uses fallback, false otherwise.
* @stable ICU 2.0
* @see ucnv_setFallback
*/
U_CAPI UBool U_EXPORT2
ucnv_usesFallback(const UConverter *cnv);
/**
* Detects Unicode signature byte sequences at the start of the byte stream
* and returns the charset name of the indicated Unicode charset.
* NULL is returned when no Unicode signature is recognized.
* The number of bytes in the signature is output as well.
*
* The caller can ucnv_open() a converter using the charset name.
* The first code unit (UChar) from the start of the stream will be U+FEFF
* (the Unicode BOM/signature character) and can usually be ignored.
*
* For most Unicode charsets it is also possible to ignore the indicated
* number of initial stream bytes and start converting after them.
* However, there are stateful Unicode charsets (UTF-7 and BOCU-1) for which
* this will not work. Therefore, it is best to ignore the first output UChar
* instead of the input signature bytes.
*
* Usage: * * @param source The source string in which the signature should be detected. * @param sourceLength Length of the input string, or -1 if terminated with a NUL byte. * @param signatureLength A pointer to int32_t to receive the number of bytes that make up the signature * of the detected UTF. 0 if not detected. * Can be a NULL pointer. * @param pErrorCode ICU error code in/out parameter. * Must fulfill U_SUCCESS before the function call. * @return The name of the encoding detected. NULL if encoding is not detected. * @stable ICU 2.4 */ U_CAPI const char* U_EXPORT2 ucnv_detectUnicodeSignature(const char* source, int32_t sourceLength, int32_t *signatureLength, UErrorCode *pErrorCode); /** * Returns the number of UChars held in the converter's internal state * because more input is needed for completing the conversion. This function is * useful for mapping semantics of ICU's converter interface to those of iconv, * and this information is not needed for normal conversion. * @param cnv The converter in which the input is held * @param status ICU error code in/out parameter. * Must fulfill U_SUCCESS before the function call. * @return The number of UChars in the state. -1 if an error is encountered. * @stable ICU 3.4 */ U_CAPI int32_t U_EXPORT2 ucnv_fromUCountPending(const UConverter* cnv, UErrorCode* status); /** * Returns the number of chars held in the converter's internal state * because more input is needed for completing the conversion. This function is * useful for mapping semantics of ICU's converter interface to those of iconv, * and this information is not needed for normal conversion. * @param cnv The converter in which the input is held as internal state * @param status ICU error code in/out parameter. * Must fulfill U_SUCCESS before the function call. * @return The number of chars in the state. -1 if an error is encountered. * @stable ICU 3.4 */ U_CAPI int32_t U_EXPORT2 ucnv_toUCountPending(const UConverter* cnv, UErrorCode* status); /** * Returns whether or not the charset of the converter has a fixed number of bytes * per charset character. * An example of this are converters that are of the type UCNV_SBCS or UCNV_DBCS. * Another example is UTF-32 which is always 4 bytes per character. * A Unicode code point may be represented by more than one UTF-8 or UTF-16 code unit * but a UTF-32 converter encodes each code point with 4 bytes. * Note: This method is not intended to be used to determine whether the charset has a * fixed ratio of bytes to Unicode codes units for any particular Unicode encoding form. * false is returned with the UErrorCode if error occurs or cnv is NULL. * @param cnv The converter to be tested * @param status ICU error code in/out parameter * @return true if the converter is fixed-width * @stable ICU 4.8 */ U_CAPI UBool U_EXPORT2 ucnv_isFixedWidth(UConverter *cnv, UErrorCode *status); #endif #endif /*_UCNV*/