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