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