Path: blob/master/dep/simpleini/include/ConvertUTF.h
4253 views
/*1* Copyright 2001-2004 Unicode, Inc.2*3* Disclaimer4*5* This source code is provided as is by Unicode, Inc. No claims are6* made as to fitness for any particular purpose. No warranties of any7* kind are expressed or implied. The recipient agrees to determine8* applicability of information provided. If this file has been9* purchased on magnetic or optical media from Unicode, Inc., the10* sole remedy for any claim will be exchange of defective media11* within 90 days of receipt.12*13* Limitations on Rights to Redistribute This Code14*15* Unicode, Inc. hereby grants the right to freely use the information16* supplied in this file in the creation of products supporting the17* Unicode Standard, and to make copies of this file in any form18* for internal or external distribution as long as this notice19* remains attached.20*/2122/* ---------------------------------------------------------------------2324Conversions between UTF32, UTF-16, and UTF-8. Header file.2526Several functions are included here, forming a complete set of27conversions between the three formats. UTF-7 is not included28here, but is handled in a separate source file.2930Each of these routines takes pointers to input buffers and output31buffers. The input buffers are const.3233Each routine converts the text between *sourceStart and sourceEnd,34putting the result into the buffer between *targetStart and35targetEnd. Note: the end pointers are *after* the last item: e.g.36*(sourceEnd - 1) is the last item.3738The return result indicates whether the conversion was successful,39and if not, whether the problem was in the source or target buffers.40(Only the first encountered problem is indicated.)4142After the conversion, *sourceStart and *targetStart are both43updated to point to the end of last text successfully converted in44the respective buffers.4546Input parameters:47sourceStart - pointer to a pointer to the source buffer.48The contents of this are modified on return so that49it points at the next thing to be converted.50targetStart - similarly, pointer to pointer to the target buffer.51sourceEnd, targetEnd - respectively pointers to the ends of the52two buffers, for overflow checking only.5354These conversion functions take a ConversionFlags argument. When this55flag is set to strict, both irregular sequences and isolated surrogates56will cause an error. When the flag is set to lenient, both irregular57sequences and isolated surrogates are converted.5859Whether the flag is strict or lenient, all illegal sequences will cause60an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,61or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code62must check for illegal sequences.6364When the flag is set to lenient, characters over 0x10FFFF are converted65to the replacement character; otherwise (when the flag is set to strict)66they constitute an error.6768Output parameters:69The value "sourceIllegal" is returned from some routines if the input70sequence is malformed. When "sourceIllegal" is returned, the source71value will point to the illegal value that caused the problem. E.g.,72in UTF-8 when a sequence is malformed, it points to the start of the73malformed sequence.7475Author: Mark E. Davis, 1994.76Rev History: Rick McGowan, fixes & updates May 2001.77Fixes & updates, Sept 2001.7879------------------------------------------------------------------------ */8081/* ---------------------------------------------------------------------82The following 4 definitions are compiler-specific.83The C standard does not guarantee that wchar_t has at least8416 bits, so wchar_t is no less portable than unsigned short!85All should be unsigned values to avoid sign extension during86bit mask & shift operations.87------------------------------------------------------------------------ */8889typedef unsigned int UTF32; /* at least 32 bits */90typedef unsigned short UTF16; /* at least 16 bits */91typedef unsigned char UTF8; /* typically 8 bits */92typedef unsigned char Boolean; /* 0 or 1 */9394/* Some fundamental constants */95#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD96#define UNI_MAX_BMP (UTF32)0x0000FFFF97#define UNI_MAX_UTF16 (UTF32)0x0010FFFF98#define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF99#define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF100101typedef enum {102conversionOK, /* conversion successful */103sourceExhausted, /* partial character in source, but hit end */104targetExhausted, /* insufficient room in target for conversion */105sourceIllegal /* source sequence is illegal/malformed */106} ConversionResult;107108typedef enum {109strictConversion = 0,110lenientConversion111} ConversionFlags;112113/* This is for C++ and does no harm in C */114#ifdef __cplusplus115extern "C" {116#endif117118ConversionResult ConvertUTF8toUTF16 (119const UTF8** sourceStart, const UTF8* sourceEnd,120UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);121122ConversionResult ConvertUTF16toUTF8 (123const UTF16** sourceStart, const UTF16* sourceEnd,124UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);125126ConversionResult ConvertUTF8toUTF32 (127const UTF8** sourceStart, const UTF8* sourceEnd,128UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);129130ConversionResult ConvertUTF32toUTF8 (131const UTF32** sourceStart, const UTF32* sourceEnd,132UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);133134ConversionResult ConvertUTF16toUTF32 (135const UTF16** sourceStart, const UTF16* sourceEnd,136UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);137138ConversionResult ConvertUTF32toUTF16 (139const UTF32** sourceStart, const UTF32* sourceEnd,140UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);141142Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);143144#ifdef __cplusplus145}146#endif147148/* --------------------------------------------------------------------- */149150151