source: trunk/libtransmission/ConvertUTF.h @ 7197

Last change on this file since 7197 was 7197, checked in by charles, 13 years ago

(libT) add the #include guards to "private" third party header files.

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