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