SimpleIni
ConvertUTF.h
1 /*
2  * https://web.archive.org/web/20090529064329/http://www.unicode.org:80/Public/PROGRAMS/CVTUTF/
3  *
4  * Copyright 2001-2004 Unicode, Inc.
5  *
6  * Disclaimer
7  *
8  * This source code is provided as is by Unicode, Inc. No claims are
9  * made as to fitness for any particular purpose. No warranties of any
10  * kind are expressed or implied. The recipient agrees to determine
11  * applicability of information provided. If this file has been
12  * purchased on magnetic or optical media from Unicode, Inc., the
13  * sole remedy for any claim will be exchange of defective media
14  * within 90 days of receipt.
15  *
16  * Limitations on Rights to Redistribute This Code
17  *
18  * Unicode, Inc. hereby grants the right to freely use the information
19  * supplied in this file in the creation of products supporting the
20  * Unicode Standard, and to make copies of this file in any form
21  * for internal or external distribution as long as this notice
22  * remains attached.
23  */
24 
25  /* ---------------------------------------------------------------------
26 
27  Conversions between UTF32, UTF-16, and UTF-8. Header file.
28 
29  Several funtions are included here, forming a complete set of
30  conversions between the three formats. UTF-7 is not included
31  here, but is handled in a separate source file.
32 
33  Each of these routines takes pointers to input buffers and output
34  buffers. The input buffers are const.
35 
36  Each routine converts the text between *sourceStart and sourceEnd,
37  putting the result into the buffer between *targetStart and
38  targetEnd. Note: the end pointers are *after* the last item: e.g.
39  *(sourceEnd - 1) is the last item.
40 
41  The return result indicates whether the conversion was successful,
42  and if not, whether the problem was in the source or target buffers.
43  (Only the first encountered problem is indicated.)
44 
45  After the conversion, *sourceStart and *targetStart are both
46  updated to point to the end of last text successfully converted in
47  the respective buffers.
48 
49  Input parameters:
50  sourceStart - pointer to a pointer to the source buffer.
51  The contents of this are modified on return so that
52  it points at the next thing to be converted.
53  targetStart - similarly, pointer to pointer to the target buffer.
54  sourceEnd, targetEnd - respectively pointers to the ends of the
55  two buffers, for overflow checking only.
56 
57  These conversion functions take a ConversionFlags argument. When this
58  flag is set to strict, both irregular sequences and isolated surrogates
59  will cause an error. When the flag is set to lenient, both irregular
60  sequences and isolated surrogates are converted.
61 
62  Whether the flag is strict or lenient, all illegal sequences will cause
63  an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
64  or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
65  must check for illegal sequences.
66 
67  When the flag is set to lenient, characters over 0x10FFFF are converted
68  to the replacement character; otherwise (when the flag is set to strict)
69  they constitute an error.
70 
71  Output parameters:
72  The value "sourceIllegal" is returned from some routines if the input
73  sequence is malformed. When "sourceIllegal" is returned, the source
74  value will point to the illegal value that caused the problem. E.g.,
75  in UTF-8 when a sequence is malformed, it points to the start of the
76  malformed sequence.
77 
78  Author: Mark E. Davis, 1994.
79  Rev History: Rick McGowan, fixes & updates May 2001.
80  Fixes & updates, Sept 2001.
81 
82  ------------------------------------------------------------------------ */
83 
84  /* ---------------------------------------------------------------------
85  The following 4 definitions are compiler-specific.
86  The C standard does not guarantee that wchar_t has at least
87  16 bits, so wchar_t is no less portable than unsigned short!
88  All should be unsigned values to avoid sign extension during
89  bit mask & shift operations.
90  ------------------------------------------------------------------------ */
91 
92 typedef unsigned long UTF32; /* at least 32 bits */
93 typedef unsigned short UTF16; /* at least 16 bits */
94 typedef unsigned char UTF8; /* typically 8 bits */
95 typedef unsigned char Boolean; /* 0 or 1 */
96 
97 /* Some fundamental constants */
98 #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
99 #define UNI_MAX_BMP (UTF32)0x0000FFFF
100 #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
101 #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
102 #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
103 
104 typedef enum {
105  conversionOK, /* conversion successful */
106  sourceExhausted, /* partial character in source, but hit end */
107  targetExhausted, /* insuff. room in target for conversion */
108  sourceIllegal /* source sequence is illegal/malformed */
109 } ConversionResult;
110 
111 typedef enum {
112  strictConversion = 0,
113  lenientConversion
114 } ConversionFlags;
115 
116 /* This is for C++ and does no harm in C */
117 #ifdef __cplusplus
118 extern "C" {
119 #endif
120 
121  ConversionResult ConvertUTF8toUTF16(
122  const UTF8** sourceStart, const UTF8* sourceEnd,
123  UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
124 
125  ConversionResult ConvertUTF16toUTF8(
126  const UTF16** sourceStart, const UTF16* sourceEnd,
127  UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
128 
129  ConversionResult ConvertUTF8toUTF32(
130  const UTF8** sourceStart, const UTF8* sourceEnd,
131  UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
132 
133  ConversionResult ConvertUTF32toUTF8(
134  const UTF32** sourceStart, const UTF32* sourceEnd,
135  UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
136 
137  ConversionResult ConvertUTF16toUTF32(
138  const UTF16** sourceStart, const UTF16* sourceEnd,
139  UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
140 
141  ConversionResult ConvertUTF32toUTF16(
142  const UTF32** sourceStart, const UTF32* sourceEnd,
143  UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
144 
145  Boolean isLegalUTF8Sequence(const UTF8* source, const UTF8* sourceEnd);
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 /* --------------------------------------------------------------------- */