2007-06-16 18:19:51 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2007-06-16 18:19:51 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <inttypes.h>
|
2011-10-30 16:05:04 +00:00
|
|
|
#include "platform.h"
|
2008-10-15 06:38:51 +00:00
|
|
|
#include "metadata.h"
|
2007-06-16 18:19:51 +00:00
|
|
|
#include "metadata_common.h"
|
|
|
|
#include "metadata_parsers.h"
|
|
|
|
#include "structec.h"
|
2009-06-21 21:59:16 +00:00
|
|
|
|
|
|
|
/* Define LOGF_ENABLE to enable logf output in this file */
|
|
|
|
/*#define LOGF_ENABLE*/
|
2007-06-16 18:19:51 +00:00
|
|
|
#include "logf.h"
|
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
struct file
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
bool packet_ended;
|
|
|
|
long packet_remaining;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Read an Ogg page header. file->packet_remaining is set to the size of the
|
|
|
|
* first packet on the page; file->packet_ended is set to true if the packet
|
|
|
|
* ended on the current page. Returns true if the page header was
|
|
|
|
* successfully read.
|
2007-06-16 18:19:51 +00:00
|
|
|
*/
|
2009-05-30 14:24:16 +00:00
|
|
|
static bool file_read_page_header(struct file* file)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2009-05-30 14:24:16 +00:00
|
|
|
unsigned char buffer[64];
|
|
|
|
ssize_t table_left;
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
/* Size of page header without segment table */
|
|
|
|
if (read(file->fd, buffer, 27) != 27)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-30 14:24:16 +00:00
|
|
|
|
|
|
|
if (memcmp("OggS", buffer, 4))
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-30 14:24:16 +00:00
|
|
|
|
|
|
|
/* Skip pattern (4), version (1), flags (1), granule position (8),
|
|
|
|
* serial (4), pageno (4), checksum (4)
|
|
|
|
*/
|
|
|
|
table_left = buffer[26];
|
|
|
|
file->packet_remaining = 0;
|
|
|
|
|
|
|
|
/* Read segment table for the first packet */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ssize_t count = MIN(sizeof(buffer), (size_t) table_left);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (read(file->fd, buffer, count) < count)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
table_left -= count;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
file->packet_remaining += buffer[i];
|
|
|
|
|
|
|
|
if (buffer[i] < 255)
|
|
|
|
{
|
|
|
|
file->packet_ended = true;
|
|
|
|
|
|
|
|
/* Skip remainder of the table */
|
|
|
|
if (lseek(file->fd, table_left, SEEK_CUR) < 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
table_left = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (table_left > 0);
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
/* Read (up to) buffer_size of data from the file. If buffer is NULL, just
|
|
|
|
* skip ahead buffer_size bytes (like lseek). Returns number of bytes read,
|
|
|
|
* 0 if there is no more data to read (in the packet or the file), < 0 if a
|
|
|
|
* read error occurred.
|
|
|
|
*/
|
|
|
|
static ssize_t file_read(struct file* file, void* buffer, size_t buffer_size)
|
|
|
|
{
|
|
|
|
ssize_t done = 0;
|
|
|
|
ssize_t count = -1;
|
|
|
|
|
|
|
|
do
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2009-05-30 14:24:16 +00:00
|
|
|
if (file->packet_remaining <= 0)
|
|
|
|
{
|
|
|
|
if (file->packet_ended)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_read_page_header(file))
|
|
|
|
{
|
|
|
|
count = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count = MIN(buffer_size, (size_t) file->packet_remaining);
|
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
count = read(file->fd, buffer, count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (lseek(file->fd, count, SEEK_CUR) < 0)
|
|
|
|
{
|
|
|
|
count = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count <= 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
buffer += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_size -= count;
|
|
|
|
done += count;
|
|
|
|
file->packet_remaining -= count;
|
2007-06-16 18:19:51 +00:00
|
|
|
}
|
2009-05-30 14:24:16 +00:00
|
|
|
while (buffer_size > 0);
|
|
|
|
|
|
|
|
return (count < 0 ? count : done);
|
|
|
|
}
|
|
|
|
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
/* Read an int32 from file. Returns false if a read error occurred.
|
|
|
|
*/
|
|
|
|
static bool file_read_int32(struct file* file, int32_t* value)
|
|
|
|
{
|
|
|
|
char buf[sizeof(int32_t)];
|
|
|
|
|
|
|
|
if (file_read(file, buf, sizeof(buf)) < (ssize_t) sizeof(buf))
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2009-05-30 14:24:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = get_long_le(buf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Read a string from the file. Read up to buffer_size bytes, or, if eos
|
|
|
|
* != -1, until the eos character is found (eos is not stored in buf,
|
|
|
|
* unless it is nil). Writes up to buffer_size chars to buf, always
|
|
|
|
* terminating with a nil. Returns number of chars read or < 0 if a read
|
|
|
|
* error occurred.
|
|
|
|
*
|
|
|
|
* Unfortunately this is a slightly modified copy of read_string() in
|
|
|
|
* metadata_common.c...
|
|
|
|
*/
|
|
|
|
static long file_read_string(struct file* file, char* buffer,
|
|
|
|
long buffer_size, int eos, long size)
|
|
|
|
{
|
|
|
|
long read_bytes = 0;
|
|
|
|
|
|
|
|
while (size > 0)
|
|
|
|
{
|
|
|
|
char c;
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
if (file_read(file, &c, 1) != 1)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2009-05-30 14:24:16 +00:00
|
|
|
read_bytes = -1;
|
2007-06-16 18:19:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-05-30 14:24:16 +00:00
|
|
|
|
|
|
|
read_bytes++;
|
|
|
|
size--;
|
|
|
|
|
|
|
|
if ((eos != -1) && (eos == (unsigned char) c))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_size > 1)
|
|
|
|
{
|
|
|
|
*buffer++ = c;
|
|
|
|
buffer_size--;
|
|
|
|
}
|
|
|
|
else if (eos == -1)
|
|
|
|
{
|
|
|
|
/* No point in reading any more, skip remaining data */
|
|
|
|
if (file_read(file, NULL, size) < 0)
|
|
|
|
{
|
|
|
|
read_bytes = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
read_bytes += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*buffer = 0;
|
|
|
|
return read_bytes;
|
|
|
|
}
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
|
|
|
|
/* Init struct file for reading from fd. type is the AFMT_* codec type of
|
|
|
|
* the file, and determines if Ogg pages are to be read. remaining is the
|
|
|
|
* max amount to read if codec type is FLAC; it is ignored otherwise.
|
|
|
|
* Returns true if the file was successfully initialized.
|
|
|
|
*/
|
|
|
|
static bool file_init(struct file* file, int fd, int type, int remaining)
|
|
|
|
{
|
|
|
|
memset(file, 0, sizeof(*file));
|
|
|
|
file->fd = fd;
|
|
|
|
|
2012-07-26 12:38:32 +00:00
|
|
|
if (type == AFMT_OGG_VORBIS || type == AFMT_SPEEX || type == AFMT_OPUS)
|
2009-05-30 14:24:16 +00:00
|
|
|
{
|
|
|
|
if (!file_read_page_header(file))
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-30 14:24:16 +00:00
|
|
|
}
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
if (type == AFMT_OGG_VORBIS)
|
|
|
|
{
|
|
|
|
char buffer[7];
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
/* Read packet header (type and id string) */
|
|
|
|
if (file_read(file, buffer, sizeof(buffer)) < (ssize_t) sizeof(buffer))
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2009-05-30 14:24:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The first byte of a packet is the packet type; comment packets
|
|
|
|
* are type 3.
|
|
|
|
*/
|
|
|
|
if (buffer[0] != 3)
|
|
|
|
{
|
|
|
|
return false;
|
2007-06-16 18:19:51 +00:00
|
|
|
}
|
2009-05-30 14:24:16 +00:00
|
|
|
}
|
2012-07-26 12:38:32 +00:00
|
|
|
else if (type == AFMT_OPUS)
|
|
|
|
{
|
|
|
|
char buffer[8];
|
|
|
|
|
|
|
|
/* Read comment header */
|
|
|
|
if (file_read(file, buffer, sizeof(buffer)) < (ssize_t) sizeof(buffer))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should be equal to "OpusTags" */
|
|
|
|
if (memcmp(buffer, "OpusTags", 8) != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-05-30 14:24:16 +00:00
|
|
|
else if (type == AFMT_FLAC)
|
|
|
|
{
|
|
|
|
file->packet_remaining = remaining;
|
|
|
|
file->packet_ended = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
/* Read the items in a Vorbis comment packet. For Ogg files, the file must
|
|
|
|
* be located on a page start, for other files, the beginning of the comment
|
|
|
|
* data (i.e., the vendor string length). Returns total size of the
|
|
|
|
* comments, or 0 if there was a read error.
|
|
|
|
*/
|
|
|
|
long read_vorbis_tags(int fd, struct mp3entry *id3,
|
|
|
|
long tag_remaining)
|
|
|
|
{
|
|
|
|
struct file file;
|
|
|
|
char *buf = id3->id3v2buf;
|
|
|
|
int32_t comment_count;
|
|
|
|
int32_t len;
|
|
|
|
long comment_size = 0;
|
|
|
|
int buf_remaining = sizeof(id3->id3v2buf) + sizeof(id3->id3v1buf);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!file_init(&file, fd, id3->codectype, tag_remaining))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip vendor string */
|
|
|
|
|
|
|
|
if (!file_read_int32(&file, &len) || (file_read(&file, NULL, len) < 0))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_read_int32(&file, &comment_count))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
comment_size += 4 + len + 4;
|
|
|
|
|
|
|
|
for (i = 0; i < comment_count && file.packet_remaining > 0; i++)
|
|
|
|
{
|
|
|
|
char name[TAG_NAME_LENGTH];
|
|
|
|
int32_t read_len;
|
|
|
|
|
|
|
|
if (!file_read_int32(&file, &len))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
comment_size += 4 + len;
|
|
|
|
read_len = file_read_string(&file, name, sizeof(name), '=', len);
|
2007-06-16 18:19:51 +00:00
|
|
|
|
|
|
|
if (read_len < 0)
|
|
|
|
{
|
2009-05-30 14:24:16 +00:00
|
|
|
return 0;
|
2007-06-16 18:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
len -= read_len;
|
2011-12-16 10:09:41 +00:00
|
|
|
read_len = file_read_string(&file, id3->path, sizeof(id3->path), -1, len);
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2011-12-16 10:09:41 +00:00
|
|
|
if (read_len < 0)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2009-05-30 14:24:16 +00:00
|
|
|
return 0;
|
2007-06-16 18:19:51 +00:00
|
|
|
}
|
|
|
|
|
2009-06-22 18:40:49 +00:00
|
|
|
logf("Vorbis comment %d: %s=%s", i, name, id3->path);
|
2011-12-16 10:09:41 +00:00
|
|
|
|
|
|
|
/* Is it an embedded cuesheet? */
|
|
|
|
if (!strcasecmp(name, "CUESHEET"))
|
|
|
|
{
|
2011-12-22 18:56:11 +00:00
|
|
|
id3->has_embedded_cuesheet = true;
|
|
|
|
id3->embedded_cuesheet.pos = lseek(file.fd, 0, SEEK_CUR) - read_len;
|
|
|
|
id3->embedded_cuesheet.size = len;
|
|
|
|
id3->embedded_cuesheet.encoding = CHAR_ENC_UTF_8;
|
2011-12-16 10:09:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = parse_tag(name, id3->path, id3, buf, buf_remaining,
|
|
|
|
TAGTYPE_VORBIS);
|
|
|
|
}
|
|
|
|
|
2007-06-16 18:19:51 +00:00
|
|
|
buf += len;
|
|
|
|
buf_remaining -= len;
|
|
|
|
}
|
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
/* Skip to the end of the block (needed by FLAC) */
|
|
|
|
if (file.packet_remaining)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2009-05-30 14:24:16 +00:00
|
|
|
if (file_read(&file, NULL, file.packet_remaining) < 0)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2009-05-30 14:24:16 +00:00
|
|
|
return 0;
|
2007-06-16 18:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-30 14:24:16 +00:00
|
|
|
return comment_size;
|
2007-06-16 18:19:51 +00:00
|
|
|
}
|