source: trunk/libtransmission/ConvertUTF.h

Last change on this file was 14724, checked in by jordan, 5 years ago

use '#pragma once' instead of #ifndef..#define..#endif guards

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