Merge branch 'VLAN-MultiSSID' into Teaman-ND
[tomato.git] / release / src / router / libid3tag / parse.c
blob86a3f219bc2f256bd17dc090d687a38bb72655f7
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: parse.c,v 1.9 2004/01/23 09:41:32 rob Exp $
22 # ifdef HAVE_CONFIG_H
23 # include "config.h"
24 # endif
26 # include "global.h"
28 # ifdef HAVE_ASSERT_H
29 # include <assert.h>
30 # endif
32 # include <stdlib.h>
33 # include <string.h>
35 # include "id3tag.h"
36 # include "parse.h"
37 # include "latin1.h"
38 # include "utf16.h"
39 # include "utf8.h"
41 signed long id3_parse_int(id3_byte_t const **ptr, unsigned int bytes)
43 signed long value = 0;
45 assert(bytes >= 1 && bytes <= 4);
47 if (**ptr & 0x80)
48 value = ~0;
50 switch (bytes) {
51 case 4: value = (value << 8) | *(*ptr)++;
52 case 3: value = (value << 8) | *(*ptr)++;
53 case 2: value = (value << 8) | *(*ptr)++;
54 case 1: value = (value << 8) | *(*ptr)++;
57 return value;
60 unsigned long id3_parse_uint(id3_byte_t const **ptr, unsigned int bytes)
62 unsigned long value = 0;
64 assert(bytes >= 1 && bytes <= 4);
66 switch (bytes) {
67 case 4: value = (value << 8) | *(*ptr)++;
68 case 3: value = (value << 8) | *(*ptr)++;
69 case 2: value = (value << 8) | *(*ptr)++;
70 case 1: value = (value << 8) | *(*ptr)++;
73 return value;
76 unsigned long id3_parse_syncsafe(id3_byte_t const **ptr, unsigned int bytes)
78 unsigned long value = 0;
80 assert(bytes == 4 || bytes == 5);
82 switch (bytes) {
83 case 5: value = (value << 4) | (*(*ptr)++ & 0x0f);
84 case 4: value = (value << 7) | (*(*ptr)++ & 0x7f);
85 value = (value << 7) | (*(*ptr)++ & 0x7f);
86 value = (value << 7) | (*(*ptr)++ & 0x7f);
87 value = (value << 7) | (*(*ptr)++ & 0x7f);
90 return value;
93 void id3_parse_immediate(id3_byte_t const **ptr, unsigned int bytes,
94 char *value)
96 assert(value);
97 assert(bytes == 8 || bytes == 4 || bytes == 3);
99 switch (bytes) {
100 case 8: *value++ = *(*ptr)++;
101 *value++ = *(*ptr)++;
102 *value++ = *(*ptr)++;
103 *value++ = *(*ptr)++;
104 case 4: *value++ = *(*ptr)++;
105 case 3: *value++ = *(*ptr)++;
106 *value++ = *(*ptr)++;
107 *value++ = *(*ptr)++;
110 *value = 0;
113 id3_latin1_t *id3_parse_latin1(id3_byte_t const **ptr, id3_length_t length,
114 int full)
116 id3_byte_t const *end;
117 int terminated = 0;
118 id3_latin1_t *latin1;
120 end = memchr(*ptr, 0, length);
121 if (end == 0)
122 end = *ptr + length;
123 else {
124 length = end - *ptr;
125 terminated = 1;
128 latin1 = malloc(length + 1);
129 if (latin1) {
130 memcpy(latin1, *ptr, length);
131 latin1[length] = 0;
133 if (!full) {
134 id3_latin1_t *check;
136 for (check = latin1; *check; ++check) {
137 if (*check == '\n')
138 *check = ' ';
143 *ptr += length + terminated;
145 return latin1;
148 id3_ucs4_t *id3_parse_string(id3_byte_t const **ptr, id3_length_t length,
149 enum id3_field_textencoding encoding, int full)
151 id3_ucs4_t *ucs4 = 0;
152 enum id3_utf16_byteorder byteorder = ID3_UTF16_BYTEORDER_ANY;
154 switch (encoding) {
155 case ID3_FIELD_TEXTENCODING_ISO_8859_1:
156 ucs4 = id3_latin1_deserialize(ptr, length);
157 break;
159 case ID3_FIELD_TEXTENCODING_UTF_16BE:
160 byteorder = ID3_UTF16_BYTEORDER_BE;
161 case ID3_FIELD_TEXTENCODING_UTF_16:
162 ucs4 = id3_utf16_deserialize(ptr, length, byteorder);
163 break;
165 case ID3_FIELD_TEXTENCODING_UTF_8:
166 ucs4 = id3_utf8_deserialize(ptr, length);
167 break;
170 if (ucs4 && !full) {
171 id3_ucs4_t *check;
173 for (check = ucs4; *check; ++check) {
174 if (*check == '\n')
175 *check = ' ';
179 return ucs4;
182 id3_byte_t *id3_parse_binary(id3_byte_t const **ptr, id3_length_t length)
184 id3_byte_t *data;
186 if (length == 0)
187 return malloc(1);
189 data = malloc(length);
190 if (data)
191 memcpy(data, *ptr, length);
193 *ptr += length;
195 return data;