2 * libid3tag - ID3 tag manipulation library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * $Id: latin1.c,v 1.10 2004/01/23 09:41:32 rob Exp $
35 * NAME: latin1->length()
36 * DESCRIPTION: return the number of ucs4 chars represented by a latin1 string
38 id3_length_t
id3_latin1_length(id3_latin1_t
const *latin1
)
40 id3_latin1_t
const *ptr
= latin1
;
49 * NAME: latin1->size()
50 * DESCRIPTION: return the encoding size of a latin1 string
52 id3_length_t
id3_latin1_size(id3_latin1_t
const *latin1
)
54 return id3_latin1_length(latin1
) + 1;
58 * NAME: latin1->copy()
59 * DESCRIPTION: copy a latin1 string
61 void id3_latin1_copy(id3_latin1_t
*dest
, id3_latin1_t
const *src
)
63 while ((*dest
++ = *src
++))
68 * NAME: latin1->duplicate()
69 * DESCRIPTION: duplicate a latin1 string
71 id3_latin1_t
*id3_latin1_duplicate(id3_latin1_t
const *src
)
75 latin1
= malloc(id3_latin1_size(src
) * sizeof(*latin1
));
77 id3_latin1_copy(latin1
, src
);
83 * NAME: latin1->ucs4duplicate()
84 * DESCRIPTION: duplicate and decode a latin1 string into ucs4
86 id3_ucs4_t
*id3_latin1_ucs4duplicate(id3_latin1_t
const *latin1
)
90 ucs4
= malloc((id3_latin1_length(latin1
) + 1) * sizeof(*ucs4
));
92 id3_latin1_decode(latin1
, ucs4
);
98 * NAME: latin1->decodechar()
99 * DESCRIPTION: decode a (single) latin1 char into a single ucs4 char
101 id3_length_t
id3_latin1_decodechar(id3_latin1_t
const *latin1
,
110 * NAME: latin1->encodechar()
111 * DESCRIPTION: encode a single ucs4 char into a (single) latin1 char
113 id3_length_t
id3_latin1_encodechar(id3_latin1_t
*latin1
, id3_ucs4_t ucs4
)
116 if (ucs4
> 0x000000ffL
)
117 *latin1
= ID3_UCS4_REPLACEMENTCHAR
;
123 * NAME: latin1->decode()
124 * DESCRIPTION: decode a complete latin1 string into a ucs4 string
126 void id3_latin1_decode(id3_latin1_t
const *latin1
, id3_ucs4_t
*ucs4
)
129 latin1
+= id3_latin1_decodechar(latin1
, ucs4
);
134 * NAME: latin1->encode()
135 * DESCRIPTION: encode a complete ucs4 string into a latin1 string
137 void id3_latin1_encode(id3_latin1_t
*latin1
, id3_ucs4_t
const *ucs4
)
140 latin1
+= id3_latin1_encodechar(latin1
, *ucs4
);
145 * NAME: latin1->put()
146 * DESCRIPTION: serialize a single latin1 character
148 id3_length_t
id3_latin1_put(id3_byte_t
**ptr
, id3_latin1_t latin1
)
157 * NAME: latin1->get()
158 * DESCRIPTION: deserialize a single latin1 character
160 id3_latin1_t
id3_latin1_get(id3_byte_t
const **ptr
)
166 * NAME: latin1->serialize()
167 * DESCRIPTION: serialize a ucs4 string using latin1 encoding
169 id3_length_t
id3_latin1_serialize(id3_byte_t
**ptr
, id3_ucs4_t
const *ucs4
,
172 id3_length_t size
= 0;
173 id3_latin1_t latin1
[1], *out
;
176 switch (id3_latin1_encodechar(out
= latin1
, *ucs4
++)) {
177 case 1: size
+= id3_latin1_put(ptr
, *out
++);
183 size
+= id3_latin1_put(ptr
, 0);
189 * NAME: latin1->deserialize()
190 * DESCRIPTION: deserialize a ucs4 string using latin1 encoding
192 id3_ucs4_t
*id3_latin1_deserialize(id3_byte_t
const **ptr
, id3_length_t length
)
194 id3_byte_t
const *end
;
195 id3_latin1_t
*latin1ptr
, *latin1
;
200 latin1
= malloc((length
+ 1) * sizeof(*latin1
));
205 while (end
- *ptr
> 0 && (*latin1ptr
= id3_latin1_get(ptr
)))
210 ucs4
= malloc((id3_latin1_length(latin1
) + 1) * sizeof(*ucs4
));
212 id3_latin1_decode(latin1
, ucs4
);