source: trunk/libtransmission/ConvertUTF.h @ 7656

Last change on this file since 7656 was 7656, checked in by charles, 12 years ago

(trunk libT) #1675: better utf8 validation of metainfo name, creator, and comment fields

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