forked from markjeee/libid3tag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utf16.c
286 lines (237 loc) · 6.22 KB
/
utf16.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* libid3tag - ID3 tag manipulation library
* Copyright (C) 2000-2004 Underbit Technologies, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: utf16.c,v 1.9 2004/01/23 09:41:32 rob Exp $
*/
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
# include "global.h"
# include <stdlib.h>
# include "id3tag.h"
# include "utf16.h"
# include "ucs4.h"
/*
* NAME: utf16->length()
* DESCRIPTION: return the number of ucs4 chars represented by a utf16 string
*/
id3_length_t id3_utf16_length(id3_utf16_t const *utf16)
{
id3_length_t length = 0;
while (*utf16) {
if (utf16[0] < 0xd800 || utf16[0] > 0xdfff)
++length;
else if (utf16[0] >= 0xd800 && utf16[0] <= 0xdbff &&
utf16[1] >= 0xdc00 && utf16[1] <= 0xdfff) {
++length;
++utf16;
}
++utf16;
}
return length;
}
/*
* NAME: utf16->size()
* DESCRIPTION: return the encoding size of a utf16 string
*/
id3_length_t id3_utf16_size(id3_utf16_t const *utf16)
{
id3_utf16_t const *ptr = utf16;
while (*ptr)
++ptr;
return ptr - utf16 + 1;
}
/*
* NAME: utf16->ucs4duplicate()
* DESCRIPTION: duplicate and decode a utf16 string into ucs4
*/
id3_ucs4_t *id3_utf16_ucs4duplicate(id3_utf16_t const *utf16)
{
id3_ucs4_t *ucs4;
ucs4 = malloc((id3_utf16_length(utf16) + 1) * sizeof(*ucs4));
if (ucs4)
id3_utf16_decode(utf16, ucs4);
return release(ucs4);
}
/*
* NAME: utf16->decodechar()
* DESCRIPTION: decode a series of utf16 chars into a single ucs4 char
*/
id3_length_t id3_utf16_decodechar(id3_utf16_t const *utf16, id3_ucs4_t *ucs4)
{
id3_utf16_t const *start = utf16;
while (1) {
if (utf16[0] < 0xd800 || utf16[0] > 0xdfff) {
*ucs4 = utf16[0];
return utf16 - start + 1;
}
else if (utf16[0] >= 0xd800 && utf16[0] <= 0xdbff &&
utf16[1] >= 0xdc00 && utf16[1] <= 0xdfff) {
*ucs4 = (((utf16[0] & 0x03ffL) << 10) |
((utf16[1] & 0x03ffL) << 0)) + 0x00010000L;
return utf16 - start + 2;
}
++utf16;
}
}
/*
* NAME: utf16->encodechar()
* DESCRIPTION: encode a single ucs4 char into a series of up to 2 utf16 chars
*/
id3_length_t id3_utf16_encodechar(id3_utf16_t *utf16, id3_ucs4_t ucs4)
{
if (ucs4 < 0x00010000L) {
utf16[0] = ucs4;
return 1;
}
else if (ucs4 < 0x00110000L) {
ucs4 -= 0x00010000L;
utf16[0] = ((ucs4 >> 10) & 0x3ff) | 0xd800;
utf16[1] = ((ucs4 >> 0) & 0x3ff) | 0xdc00;
return 2;
}
/* default */
return id3_utf16_encodechar(utf16, ID3_UCS4_REPLACEMENTCHAR);
}
/*
* NAME: utf16->decode()
* DESCRIPTION: decode a complete utf16 string into a ucs4 string
*/
void id3_utf16_decode(id3_utf16_t const *utf16, id3_ucs4_t *ucs4)
{
do
utf16 += id3_utf16_decodechar(utf16, ucs4);
while (*ucs4++);
}
/*
* NAME: utf16->encode()
* DESCRIPTION: encode a complete ucs4 string into a utf16 string
*/
void id3_utf16_encode(id3_utf16_t *utf16, id3_ucs4_t const *ucs4)
{
do
utf16 += id3_utf16_encodechar(utf16, *ucs4);
while (*ucs4++);
}
/*
* NAME: utf16->put()
* DESCRIPTION: serialize a single utf16 character
*/
id3_length_t id3_utf16_put(id3_byte_t **ptr, id3_utf16_t utf16,
enum id3_utf16_byteorder byteorder)
{
if (ptr) {
switch (byteorder) {
default:
case ID3_UTF16_BYTEORDER_BE:
(*ptr)[0] = (utf16 >> 8) & 0xff;
(*ptr)[1] = (utf16 >> 0) & 0xff;
break;
case ID3_UTF16_BYTEORDER_LE:
(*ptr)[0] = (utf16 >> 0) & 0xff;
(*ptr)[1] = (utf16 >> 8) & 0xff;
break;
}
*ptr += 2;
}
return 2;
}
/*
* NAME: utf16->get()
* DESCRIPTION: deserialize a single utf16 character
*/
id3_utf16_t id3_utf16_get(id3_byte_t const **ptr,
enum id3_utf16_byteorder byteorder)
{
id3_utf16_t utf16;
switch (byteorder) {
default:
case ID3_UTF16_BYTEORDER_BE:
utf16 =
((*ptr)[0] << 8) |
((*ptr)[1] << 0);
break;
case ID3_UTF16_BYTEORDER_LE:
utf16 =
((*ptr)[0] << 0) |
((*ptr)[1] << 8);
break;
}
*ptr += 2;
return utf16;
}
/*
* NAME: utf16->serialize()
* DESCRIPTION: serialize a ucs4 string using utf16 encoding
*/
id3_length_t id3_utf16_serialize(id3_byte_t **ptr, id3_ucs4_t const *ucs4,
enum id3_utf16_byteorder byteorder,
int terminate)
{
id3_length_t size = 0;
id3_utf16_t utf16[2], *out;
if (byteorder == ID3_UTF16_BYTEORDER_ANY)
size += id3_utf16_put(ptr, 0xfeff, byteorder);
while (*ucs4) {
switch (id3_utf16_encodechar(out = utf16, *ucs4++)) {
case 2: size += id3_utf16_put(ptr, *out++, byteorder);
case 1: size += id3_utf16_put(ptr, *out++, byteorder);
case 0: break;
}
}
if (terminate)
size += id3_utf16_put(ptr, 0, byteorder);
return size;
}
/*
* NAME: utf16->deserialize()
* DESCRIPTION: deserialize a ucs4 string using utf16 encoding
*/
id3_ucs4_t *id3_utf16_deserialize(id3_byte_t const **ptr, id3_length_t length,
enum id3_utf16_byteorder byteorder)
{
id3_byte_t const *end;
id3_utf16_t *utf16ptr, *utf16;
id3_ucs4_t *ucs4;
end = *ptr + (length & ~1);
utf16 = malloc((length / 2 + 1) * sizeof(*utf16));
if (utf16 == 0)
return 0;
if (byteorder == ID3_UTF16_BYTEORDER_ANY && end - *ptr > 0) {
switch (((*ptr)[0] << 8) |
((*ptr)[1] << 0)) {
case 0xfeff:
byteorder = ID3_UTF16_BYTEORDER_BE;
*ptr += 2;
break;
case 0xfffe:
byteorder = ID3_UTF16_BYTEORDER_LE;
*ptr += 2;
break;
}
}
utf16ptr = utf16;
while (end - *ptr > 0 && (*utf16ptr = id3_utf16_get(ptr, byteorder)))
++utf16ptr;
*utf16ptr = 0;
ucs4 = malloc((id3_utf16_length(utf16) + 1) * sizeof(*ucs4));
if (ucs4)
id3_utf16_decode(utf16, ucs4);
free(utf16);
return ucs4;
}