1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
29 #include "metadata_common.h"
30 #include "metadata_parsers.h"
31 #include "replaygain.h"
34 /* Skip an ID3v2 tag if it can be found. We assume the tag is located at the
35 * start of the file, which should be true in all cases where we need to skip it.
36 * Returns true if successfully skipped or not skipped, and false if
37 * something went wrong while skipping.
39 bool skip_id3v2(int fd
, struct mp3entry
*id3
)
44 if (memcmp(buf
, "ID3", 3) == 0)
46 /* We have found an ID3v2 tag at the start of the file - find its
47 length and then skip it. */
48 if ((id3
->first_frame_offset
= getid3v2len(fd
)) == 0)
51 if ((lseek(fd
, id3
->first_frame_offset
, SEEK_SET
) < 0))
56 lseek(fd
, 0, SEEK_SET
);
57 id3
->first_frame_offset
= 0;
63 /* Read a string from the file. Read up to size bytes, or, if eos != -1,
64 * until the eos character is found (eos is not stored in buf, unless it is
65 * nil). Writes up to buf_size chars to buf, always terminating with a nil.
66 * Returns number of chars read or -1 on read error.
68 long read_string(int fd
, char* buf
, long buf_size
, int eos
, long size
)
75 if (read(fd
, &c
, 1) != 1)
84 if ((eos
!= -1) && (eos
== (unsigned char) c
))
99 /* Read an unsigned 8-bit integer from a file. */
100 int read_uint8(int fd
, uint8_t* buf
)
104 n
= read(fd
, (char*) buf
, 1);
108 #ifdef ROCKBOX_LITTLE_ENDIAN
109 /* Read an unsigned 16-bit integer from a big-endian file. */
110 int read_uint16be(int fd
, uint16_t* buf
)
114 n
= read(fd
, (char*) buf
, 2);
115 *buf
= betoh16(*buf
);
118 /* Read an unsigned 32-bit integer from a big-endian file. */
119 int read_uint32be(int fd
, uint32_t* buf
)
123 n
= read(fd
, (char*) buf
, 4);
124 *buf
= betoh32(*buf
);
128 /* Read unsigned integers from a little-endian file. */
129 int read_uint16le(int fd
, uint16_t* buf
)
133 n
= read(fd
, (char*) buf
, 2);
134 *buf
= letoh16(*buf
);
137 int read_uint32le(int fd
, uint32_t* buf
)
141 n
= read(fd
, (char*) buf
, 4);
142 *buf
= letoh32(*buf
);
145 int read_uint64le(int fd
, uint64_t* buf
)
151 n
= read(fd
, data
, 8);
153 for (i
=7, *buf
=0; i
>=0; i
--) {
162 /* Read an unaligned 32-bit little endian long from buffer. */
163 unsigned long get_long_le(void* buf
)
165 unsigned char* p
= (unsigned char*) buf
;
167 return p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
170 /* Read an unaligned 16-bit little endian short from buffer. */
171 unsigned short get_short_le(void* buf
)
173 unsigned char* p
= (unsigned char*) buf
;
175 return p
[0] | (p
[1] << 8);
178 /* Read an unaligned 32-bit big endian long from buffer. */
179 unsigned long get_long_be(void* buf
)
181 unsigned char* p
= (unsigned char*) buf
;
183 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
186 /* Read an unaligned 32-bit little endian long from buffer. */
187 long get_slong(void* buf
)
189 unsigned char* p
= (unsigned char*) buf
;
191 return p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
194 unsigned long get_itunes_int32(char* value
, int count
)
196 static const char hexdigits
[] = "0123456789ABCDEF";
202 value
= skip_whitespace(value
);
204 while (*value
&& !isspace(*value
))
210 value
= skip_whitespace(value
);
212 while (*value
&& ((c
= strchr(hexdigits
, toupper(*value
))) != NULL
))
214 r
= (r
<< 4) | (c
- hexdigits
);
221 /* Parse the tag (the name-value pair) and fill id3 and buffer accordingly.
222 * String values to keep are written to buf. Returns number of bytes written
223 * to buf (including end nil).
225 long parse_tag(const char* name
, char* value
, struct mp3entry
* id3
,
226 char* buf
, long buf_remaining
, enum tagtype type
)
231 if ((((strcasecmp(name
, "track") == 0) && (type
== TAGTYPE_APE
)))
232 || ((strcasecmp(name
, "tracknumber") == 0) && (type
== TAGTYPE_VORBIS
)))
234 id3
->tracknum
= atoi(value
);
235 p
= &(id3
->track_string
);
237 else if (strcasecmp(name
, "discnumber") == 0 || strcasecmp(name
, "disc") == 0)
239 id3
->discnum
= atoi(value
);
240 p
= &(id3
->disc_string
);
242 else if (((strcasecmp(name
, "year") == 0) && (type
== TAGTYPE_APE
))
243 || ((strcasecmp(name
, "date") == 0) && (type
== TAGTYPE_VORBIS
)))
245 /* Date's can be in any format in Vorbis. However most of them
246 * are in ISO8601 format so if we try and parse the first part
247 * of the tag as a number, we should get the year. If we get crap,
248 * then act like we never parsed it.
250 id3
->year
= atoi(value
);
251 if (id3
->year
< 1900)
252 { /* yeah, not likely */
255 p
= &(id3
->year_string
);
257 else if (strcasecmp(name
, "title") == 0)
261 else if (strcasecmp(name
, "artist") == 0)
265 else if (strcasecmp(name
, "album") == 0)
269 else if (strcasecmp(name
, "genre") == 0)
271 p
= &(id3
->genre_string
);
273 else if (strcasecmp(name
, "composer") == 0)
275 p
= &(id3
->composer
);
277 else if (strcasecmp(name
, "comment") == 0)
281 else if (strcasecmp(name
, "albumartist") == 0)
283 p
= &(id3
->albumartist
);
285 else if (strcasecmp(name
, "album artist") == 0)
287 p
= &(id3
->albumartist
);
289 else if (strcasecmp(name
, "ensemble") == 0)
291 p
= &(id3
->albumartist
);
293 else if (strcasecmp(name
, "grouping") == 0)
295 p
= &(id3
->grouping
);
297 else if (strcasecmp(name
, "content group") == 0)
299 p
= &(id3
->grouping
);
301 else if (strcasecmp(name
, "contentgroup") == 0)
303 p
= &(id3
->grouping
);
305 else if (strcasecmp(name
, "musicbrainz_trackid") == 0
306 || strcasecmp(name
, "http://musicbrainz.org") == 0 )
308 p
= &(id3
->mb_track_id
);
312 len
= parse_replaygain(name
, value
, id3
, buf
, buf_remaining
);
319 len
= MIN(len
, buf_remaining
- 1);
324 strlcpy(buf
, value
, len
);