-
Notifications
You must be signed in to change notification settings - Fork 5
/
ucs4.c
224 lines (185 loc) · 4.66 KB
/
ucs4.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
/*
* 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: ucs4.c,v 1.13 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 "ucs4.h"
# include "latin1.h"
# include "utf16.h"
# include "utf8.h"
id3_ucs4_t const id3_ucs4_empty[] = { 0 };
/*
* NAME: ucs4->length()
* DESCRIPTION: return the number of ucs4 chars represented by a ucs4 string
*/
id3_length_t id3_ucs4_length(id3_ucs4_t const *ucs4)
{
id3_ucs4_t const *ptr = ucs4;
while (*ptr)
++ptr;
return ptr - ucs4;
}
/*
* NAME: ucs4->size()
* DESCRIPTION: return the encoding size of a ucs4 string
*/
id3_length_t id3_ucs4_size(id3_ucs4_t const *ucs4)
{
return id3_ucs4_length(ucs4) + 1;
}
/*
* NAME: ucs4->latin1size()
* DESCRIPTION: return the encoding size of a latin1-encoded ucs4 string
*/
id3_length_t id3_ucs4_latin1size(id3_ucs4_t const *ucs4)
{
return id3_ucs4_size(ucs4);
}
/*
* NAME: ucs4->utf16size()
* DESCRIPTION: return the encoding size of a utf16-encoded ucs4 string
*/
id3_length_t id3_ucs4_utf16size(id3_ucs4_t const *ucs4)
{
id3_length_t size = 0;
while (*ucs4) {
++size;
if (*ucs4 >= 0x00010000L &&
*ucs4 <= 0x0010ffffL)
++size;
++ucs4;
}
return size + 1;
}
/*
* NAME: ucs4->utf8size()
* DESCRIPTION: return the encoding size of a utf8-encoded ucs4 string
*/
id3_length_t id3_ucs4_utf8size(id3_ucs4_t const *ucs4)
{
id3_length_t size = 0;
while (*ucs4) {
if (*ucs4 <= 0x0000007fL)
size += 1;
else if (*ucs4 <= 0x000007ffL)
size += 2;
else if (*ucs4 <= 0x0000ffffL)
size += 3;
else if (*ucs4 <= 0x001fffffL)
size += 4;
else if (*ucs4 <= 0x03ffffffL)
size += 5;
else if (*ucs4 <= 0x7fffffffL)
size += 6;
else
size += 2; /* based on U+00B7 replacement char */
++ucs4;
}
return size + 1;
}
/*
* NAME: ucs4->latin1duplicate()
* DESCRIPTION: duplicate and encode a ucs4 string into latin1
*/
id3_latin1_t *id3_ucs4_latin1duplicate(id3_ucs4_t const *ucs4)
{
id3_latin1_t *latin1;
latin1 = malloc(id3_ucs4_latin1size(ucs4) * sizeof(*latin1));
if (latin1)
id3_latin1_encode(latin1, ucs4);
return release(latin1);
}
/*
* NAME: ucs4->utf16duplicate()
* DESCRIPTION: duplicate and encode a ucs4 string into utf16
*/
id3_utf16_t *id3_ucs4_utf16duplicate(id3_ucs4_t const *ucs4)
{
id3_utf16_t *utf16;
utf16 = malloc(id3_ucs4_utf16size(ucs4) * sizeof(*utf16));
if (utf16)
id3_utf16_encode(utf16, ucs4);
return release(utf16);
}
/*
* NAME: ucs4->utf8duplicate()
* DESCRIPTION: duplicate and encode a ucs4 string into utf8
*/
id3_utf8_t *id3_ucs4_utf8duplicate(id3_ucs4_t const *ucs4)
{
id3_utf8_t *utf8;
utf8 = malloc(id3_ucs4_utf8size(ucs4) * sizeof(*utf8));
if (utf8)
id3_utf8_encode(utf8, ucs4);
return release(utf8);
}
/*
* NAME: ucs4->copy()
* DESCRIPTION: copy a ucs4 string
*/
void id3_ucs4_copy(id3_ucs4_t *dest, id3_ucs4_t const *src)
{
while ((*dest++ = *src++))
;
}
/*
* NAME: ucs4->duplicate()
* DESCRIPTION: duplicate a ucs4 string
*/
id3_ucs4_t *id3_ucs4_duplicate(id3_ucs4_t const *src)
{
id3_ucs4_t *ucs4;
ucs4 = malloc(id3_ucs4_size(src) * sizeof(*ucs4));
if (ucs4)
id3_ucs4_copy(ucs4, src);
return ucs4;
}
/*
* NAME: ucs4->putnumber()
* DESCRIPTION: write a ucs4 string containing a (positive) decimal number
*/
void id3_ucs4_putnumber(id3_ucs4_t *ucs4, unsigned long number)
{
int digits[10], *digit;
digit = digits;
do {
*digit++ = number % 10;
number /= 10;
}
while (number);
while (digit != digits)
*ucs4++ = '0' + *--digit;
*ucs4 = 0;
}
/*
* NAME: ucs4->getnumber()
* DESCRIPTION: read a ucs4 string containing a (positive) decimal number
*/
unsigned long id3_ucs4_getnumber(id3_ucs4_t const *ucs4)
{
unsigned long number = 0;
while (*ucs4 >= '0' && *ucs4 <= '9')
number = 10 * number + (*ucs4++ - '0');
return number;
}