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