michael@0: /* michael@0: * Copyright 2001-2004 Unicode, Inc. michael@0: * michael@0: * Disclaimer michael@0: * michael@0: * This source code is provided as is by Unicode, Inc. No claims are michael@0: * made as to fitness for any particular purpose. No warranties of any michael@0: * kind are expressed or implied. The recipient agrees to determine michael@0: * applicability of information provided. If this file has been michael@0: * purchased on magnetic or optical media from Unicode, Inc., the michael@0: * sole remedy for any claim will be exchange of defective media michael@0: * within 90 days of receipt. michael@0: * michael@0: * Limitations on Rights to Redistribute This Code michael@0: * michael@0: * Unicode, Inc. hereby grants the right to freely use the information michael@0: * supplied in this file in the creation of products supporting the michael@0: * Unicode Standard, and to make copies of this file in any form michael@0: * for internal or external distribution as long as this notice michael@0: * remains attached. michael@0: */ michael@0: michael@0: /* --------------------------------------------------------------------- michael@0: michael@0: Conversions between UTF32, UTF-16, and UTF-8. Header file. michael@0: michael@0: Several funtions are included here, forming a complete set of michael@0: conversions between the three formats. UTF-7 is not included michael@0: here, but is handled in a separate source file. michael@0: michael@0: Each of these routines takes pointers to input buffers and output michael@0: buffers. The input buffers are const. michael@0: michael@0: Each routine converts the text between *sourceStart and sourceEnd, michael@0: putting the result into the buffer between *targetStart and michael@0: targetEnd. Note: the end pointers are *after* the last item: e.g. michael@0: *(sourceEnd - 1) is the last item. michael@0: michael@0: The return result indicates whether the conversion was successful, michael@0: and if not, whether the problem was in the source or target buffers. michael@0: (Only the first encountered problem is indicated.) michael@0: michael@0: After the conversion, *sourceStart and *targetStart are both michael@0: updated to point to the end of last text successfully converted in michael@0: the respective buffers. michael@0: michael@0: Input parameters: michael@0: sourceStart - pointer to a pointer to the source buffer. michael@0: The contents of this are modified on return so that michael@0: it points at the next thing to be converted. michael@0: targetStart - similarly, pointer to pointer to the target buffer. michael@0: sourceEnd, targetEnd - respectively pointers to the ends of the michael@0: two buffers, for overflow checking only. michael@0: michael@0: These conversion functions take a ConversionFlags argument. When this michael@0: flag is set to strict, both irregular sequences and isolated surrogates michael@0: will cause an error. When the flag is set to lenient, both irregular michael@0: sequences and isolated surrogates are converted. michael@0: michael@0: Whether the flag is strict or lenient, all illegal sequences will cause michael@0: an error return. This includes sequences such as: , , michael@0: or in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code michael@0: must check for illegal sequences. michael@0: michael@0: When the flag is set to lenient, characters over 0x10FFFF are converted michael@0: to the replacement character; otherwise (when the flag is set to strict) michael@0: they constitute an error. michael@0: michael@0: Output parameters: michael@0: The value "sourceIllegal" is returned from some routines if the input michael@0: sequence is malformed. When "sourceIllegal" is returned, the source michael@0: value will point to the illegal value that caused the problem. E.g., michael@0: in UTF-8 when a sequence is malformed, it points to the start of the michael@0: malformed sequence. michael@0: michael@0: Author: Mark E. Davis, 1994. michael@0: Rev History: Rick McGowan, fixes & updates May 2001. michael@0: Fixes & updates, Sept 2001. michael@0: michael@0: ------------------------------------------------------------------------ */ michael@0: michael@0: /* --------------------------------------------------------------------- michael@0: The following 4 definitions are compiler-specific. michael@0: The C standard does not guarantee that wchar_t has at least michael@0: 16 bits, so wchar_t is no less portable than unsigned short! michael@0: All should be unsigned values to avoid sign extension during michael@0: bit mask & shift operations. michael@0: ------------------------------------------------------------------------ */ michael@0: michael@0: typedef unsigned long UTF32; /* at least 32 bits */ michael@0: typedef unsigned short UTF16; /* at least 16 bits */ michael@0: typedef unsigned char UTF8; /* typically 8 bits */ michael@0: typedef unsigned char Boolean; /* 0 or 1 */ michael@0: michael@0: /* Some fundamental constants */ michael@0: #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD michael@0: #define UNI_MAX_BMP (UTF32)0x0000FFFF michael@0: #define UNI_MAX_UTF16 (UTF32)0x0010FFFF michael@0: #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF michael@0: #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF michael@0: michael@0: typedef enum { michael@0: conversionOK, /* conversion successful */ michael@0: sourceExhausted, /* partial character in source, but hit end */ michael@0: targetExhausted, /* insuff. room in target for conversion */ michael@0: sourceIllegal /* source sequence is illegal/malformed */ michael@0: } ConversionResult; michael@0: michael@0: typedef enum { michael@0: strictConversion = 0, michael@0: lenientConversion michael@0: } ConversionFlags; michael@0: michael@0: /* This is for C++ and does no harm in C */ michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: ConversionResult ConvertUTF8toUTF16 (const UTF8** sourceStart, const UTF8* sourceEnd, michael@0: UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags); michael@0: michael@0: ConversionResult ConvertUTF16toUTF8 (const UTF16** sourceStart, const UTF16* sourceEnd, michael@0: UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags); michael@0: michael@0: ConversionResult ConvertUTF8toUTF32 (const UTF8** sourceStart, const UTF8* sourceEnd, michael@0: UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags); michael@0: michael@0: ConversionResult ConvertUTF32toUTF8 (const UTF32** sourceStart, const UTF32* sourceEnd, michael@0: UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags); michael@0: michael@0: ConversionResult ConvertUTF16toUTF32 (const UTF16** sourceStart, const UTF16* sourceEnd, michael@0: UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags); michael@0: michael@0: ConversionResult ConvertUTF32toUTF16 (const UTF32** sourceStart, const UTF32* sourceEnd, michael@0: UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags); michael@0: michael@0: Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd); michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: /* --------------------------------------------------------------------- */