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