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