K2.6 patches and update.
[tomato.git] / release / src / router / libid3tag / latin1.c
blob67b8199bcea860d39a43d44d1e29b202b5dcb760
1 /*
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 $
22 # ifdef HAVE_CONFIG_H
23 # include "config.h"
24 # endif
26 # include "global.h"
28 # include <stdlib.h>
30 # include "id3tag.h"
31 # include "latin1.h"
32 # include "ucs4.h"
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;
42 while (*ptr)
43 ++ptr;
45 return 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)
73 id3_latin1_t *latin1;
75 latin1 = malloc(id3_latin1_size(src) * sizeof(*latin1));
76 if (latin1)
77 id3_latin1_copy(latin1, src);
79 return latin1;
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)
88 id3_ucs4_t *ucs4;
90 ucs4 = malloc((id3_latin1_length(latin1) + 1) * sizeof(*ucs4));
91 if (ucs4)
92 id3_latin1_decode(latin1, ucs4);
94 return release(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,
102 id3_ucs4_t *ucs4)
104 *ucs4 = *latin1;
106 return 1;
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)
115 *latin1 = ucs4;
116 if (ucs4 > 0x000000ffL)
117 *latin1 = ID3_UCS4_REPLACEMENTCHAR;
119 return 1;
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);
130 while (*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);
141 while (*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)
150 if (ptr)
151 *(*ptr)++ = latin1;
153 return 1;
157 * NAME: latin1->get()
158 * DESCRIPTION: deserialize a single latin1 character
160 id3_latin1_t id3_latin1_get(id3_byte_t const **ptr)
162 return *(*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,
170 int terminate)
172 id3_length_t size = 0;
173 id3_latin1_t latin1[1], *out;
175 while (*ucs4) {
176 switch (id3_latin1_encodechar(out = latin1, *ucs4++)) {
177 case 1: size += id3_latin1_put(ptr, *out++);
178 case 0: break;
182 if (terminate)
183 size += id3_latin1_put(ptr, 0);
185 return size;
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;
196 id3_ucs4_t *ucs4;
198 end = *ptr + length;
200 latin1 = malloc((length + 1) * sizeof(*latin1));
201 if (latin1 == 0)
202 return 0;
204 latin1ptr = latin1;
205 while (end - *ptr > 0 && (*latin1ptr = id3_latin1_get(ptr)))
206 ++latin1ptr;
208 *latin1ptr = 0;
210 ucs4 = malloc((id3_latin1_length(latin1) + 1) * sizeof(*ucs4));
211 if (ucs4)
212 id3_latin1_decode(latin1, ucs4);
214 free(latin1);
216 return ucs4;