1
/*******************************************************************************
3
* THIS FILE IS INCLUDED IN TAGLIB, BUT IS NOT COPYRIGHTED BY THE TAGLIB *
4
* AUTHORS, NOT PART OF THE TAGLIB API AND COULD GO AWAY AT ANY POINT IN TIME. *
5
* AS SUCH IT SHOULD BE CONSIERED FOR INTERNAL USE ONLY. *
7
*******************************************************************************/
10
* Copyright 2001 Unicode, Inc.
14
* This source code is provided as is by Unicode, Inc. No claims are
15
* made as to fitness for any particular purpose. No warranties of any
16
* kind are expressed or implied. The recipient agrees to determine
17
* applicability of information provided. If this file has been
18
* purchased on magnetic or optical media from Unicode, Inc., the
19
* sole remedy for any claim will be exchange of defective media
20
* within 90 days of receipt.
22
* Limitations on Rights to Redistribute This Code
24
* Unicode, Inc. hereby grants the right to freely use the information
25
* supplied in this file in the creation of products supporting the
26
* Unicode Standard, and to make copies of this file in any form
27
* for internal or external distribution as long as this notice
32
* This file has been modified by Scott Wheeler <wheeler@kde.org> to remove
33
* the UTF32 conversion functions and to place the appropriate functions
34
* in their own C++ namespace.
37
/* ---------------------------------------------------------------------
39
Conversions between UTF32, UTF-16, and UTF-8. Source code file.
40
Author: Mark E. Davis, 1994.
41
Rev History: Rick McGowan, fixes & updates May 2001.
42
Sept 2001: fixed const & error conditions per
43
mods suggested by S. Parent & A. Lillich.
45
See the header file "ConvertUTF.h" for complete documentation.
47
------------------------------------------------------------------------ */
53
#define UNI_SUR_HIGH_START (UTF32)0xD800
54
#define UNI_SUR_HIGH_END (UTF32)0xDBFF
55
#define UNI_SUR_LOW_START (UTF32)0xDC00
56
#define UNI_SUR_LOW_END (UTF32)0xDFFF
62
static const int halfShift = 10; /* used for shifting by 10 bits */
64
static const UTF32 halfBase = 0x0010000UL;
65
static const UTF32 halfMask = 0x3FFUL;
68
* Index into the table below with the first byte of a UTF-8 sequence to
69
* get the number of trailing bytes that are supposed to follow it.
71
static const char trailingBytesForUTF8[256] = {
72
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
73
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
74
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
75
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
76
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
78
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
79
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
83
* Magic values subtracted from a buffer value during UTF8 conversion.
84
* This table contains as many values as there might be trailing bytes
85
* in a UTF-8 sequence.
87
static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
88
0x03C82080UL, 0xFA082080UL, 0x82082080UL };
91
* Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
92
* into the first byte, depending on how many bytes follow. There are
93
* as many entries in this table as there are UTF-8 sequence types.
94
* (I.e., one byte sequence, two byte... six byte sequence.)
96
static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
98
/* --------------------------------------------------------------------- */
100
/* The interface converts a whole buffer to avoid function-call overhead.
101
* Constants have been gathered. Loops & conditionals have been removed as
102
* much as possible for efficiency, in favor of drop-through switches.
103
* (See "Note A" at the bottom of the file for equivalent code.)
104
* If your compiler supports it, the "isLegalUTF8" call can be turned
105
* into an inline function.
108
/* --------------------------------------------------------------------- */
110
ConversionResult ConvertUTF16toUTF8 (
111
const UTF16** sourceStart, const UTF16* sourceEnd,
112
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
113
ConversionResult result = conversionOK;
114
const UTF16* source = *sourceStart;
115
UTF8* target = *targetStart;
116
while (source < sourceEnd) {
118
unsigned short bytesToWrite = 0;
119
const UTF32 byteMask = 0xBF;
120
const UTF32 byteMark = 0x80;
121
const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
123
/* If we have a surrogate pair, convert to UTF32 first. */
124
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END && source < sourceEnd) {
126
if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
127
ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
128
+ (ch2 - UNI_SUR_LOW_START) + halfBase;
130
} else if (flags == strictConversion) { /* it's an unpaired high surrogate */
131
--source; /* return to the illegal value itself */
132
result = sourceIllegal;
135
} else if ((flags == strictConversion) && (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END)) {
136
--source; /* return to the illegal value itself */
137
result = sourceIllegal;
140
/* Figure out how many bytes the result will require */
141
if (ch < (UTF32)0x80) { bytesToWrite = 1;
142
} else if (ch < (UTF32)0x800) { bytesToWrite = 2;
143
} else if (ch < (UTF32)0x10000) { bytesToWrite = 3;
144
} else if (ch < (UTF32)0x200000) { bytesToWrite = 4;
145
} else { bytesToWrite = 2;
146
ch = UNI_REPLACEMENT_CHAR;
148
// printf("bytes to write = %i\n", bytesToWrite);
149
target += bytesToWrite;
150
if (target > targetEnd) {
151
source = oldSource; /* Back up source pointer! */
152
target -= bytesToWrite; result = targetExhausted; break;
154
switch (bytesToWrite) { /* note: everything falls through. */
155
case 4: *--target = (ch | byteMark) & byteMask; ch >>= 6;
156
case 3: *--target = (ch | byteMark) & byteMask; ch >>= 6;
157
case 2: *--target = (ch | byteMark) & byteMask; ch >>= 6;
158
case 1: *--target = ch | firstByteMark[bytesToWrite];
160
target += bytesToWrite;
162
*sourceStart = source;
163
*targetStart = target;
167
/* --------------------------------------------------------------------- */
170
* Utility routine to tell whether a sequence of bytes is legal UTF-8.
171
* This must be called with the length pre-determined by the first byte.
172
* If not calling this from ConvertUTF8to*, then the length can be set by:
173
* length = trailingBytesForUTF8[*source]+1;
174
* and the sequence is illegal right away if there aren't that many bytes
176
* If presented with a length > 4, this returns false. The Unicode
177
* definition of UTF-8 goes up to 4-byte sequences.
180
static Boolean isLegalUTF8(const UTF8 *source, int length) {
182
const UTF8 *srcptr = source+length;
184
default: return false;
185
/* Everything else falls through when "true"... */
186
case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
187
case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
188
case 2: if ((a = (*--srcptr)) > 0xBF) return false;
190
/* no fall-through in this inner switch */
191
case 0xE0: if (a < 0xA0) return false; break;
192
case 0xF0: if (a < 0x90) return false; break;
193
case 0xF4: if (a > 0x8F) return false; break;
194
default: if (a < 0x80) return false;
196
case 1: if (*source >= 0x80 && *source < 0xC2) return false;
197
if (*source > 0xF4) return false;
202
/* --------------------------------------------------------------------- */
205
* Exported function to return whether a UTF-8 sequence is legal or not.
206
* This is not used here; it's just exported.
208
Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) {
209
int length = trailingBytesForUTF8[*source]+1;
210
if (source+length > sourceEnd) {
213
return isLegalUTF8(source, length);
216
/* --------------------------------------------------------------------- */
218
ConversionResult ConvertUTF8toUTF16 (
219
const UTF8** sourceStart, const UTF8* sourceEnd,
220
UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
221
ConversionResult result = conversionOK;
222
const UTF8* source = *sourceStart;
223
UTF16* target = *targetStart;
224
while (source < sourceEnd) {
226
unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
227
if (source + extraBytesToRead >= sourceEnd) {
228
result = sourceExhausted; break;
230
/* Do this check whether lenient or strict */
231
if (! isLegalUTF8(source, extraBytesToRead+1)) {
232
result = sourceIllegal;
236
* The cases all fall through. See "Note A" below.
238
switch (extraBytesToRead) {
239
case 3: ch += *source++; ch <<= 6;
240
case 2: ch += *source++; ch <<= 6;
241
case 1: ch += *source++; ch <<= 6;
242
case 0: ch += *source++;
244
ch -= offsetsFromUTF8[extraBytesToRead];
246
if (target >= targetEnd) {
247
source -= (extraBytesToRead+1); /* Back up source pointer! */
248
result = targetExhausted; break;
250
if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
251
if ((flags == strictConversion) && (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END)) {
252
source -= (extraBytesToRead+1); /* return to the illegal value itself */
253
result = sourceIllegal;
256
*target++ = ch; /* normal case */
258
} else if (ch > UNI_MAX_UTF16) {
259
if (flags == strictConversion) {
260
result = sourceIllegal;
261
source -= (extraBytesToRead+1); /* return to the start */
262
break; /* Bail out; shouldn't continue */
264
*target++ = UNI_REPLACEMENT_CHAR;
267
/* target is a character in range 0xFFFF - 0x10FFFF. */
268
if (target + 1 >= targetEnd) {
269
source -= (extraBytesToRead+1); /* Back up source pointer! */
270
result = targetExhausted; break;
273
*target++ = (ch >> halfShift) + UNI_SUR_HIGH_START;
274
*target++ = (ch & halfMask) + UNI_SUR_LOW_START;
277
*sourceStart = source;
278
*targetStart = target;
284
/* ---------------------------------------------------------------------
287
The fall-through switches in UTF-8 reading code save a
288
temp variable, some decrements & conditionals. The switches
289
are equivalent to the following loop:
291
int tmpBytesToRead = extraBytesToRead+1;
295
if (tmpBytesToRead) ch <<= 6;
296
} while (tmpBytesToRead > 0);
298
In UTF-8 writing code, the switches on "bytesToWrite" are
299
similarly unrolled loops.
301
--------------------------------------------------------------------- */