Replace io_* routines by standard I/O functions

This commit is contained in:
Julien BLACHE 2009-05-03 12:08:14 +02:00
parent e8843a99aa
commit f0ca987d78
2 changed files with 108 additions and 140 deletions

View File

@ -50,7 +50,6 @@
#include "daapd.h" #include "daapd.h"
#include "err.h" #include "err.h"
#include "io.h"
#include "misc.h" #include "misc.h"
#ifndef PACKAGE #ifndef PACKAGE
@ -65,7 +64,7 @@ typedef struct err_threadlist_t {
static int err_debuglevel=0; /**< current debuglevel, set from command line with -d */ static int err_debuglevel=0; /**< current debuglevel, set from command line with -d */
static int err_logdest=0; /**< current log destination */ static int err_logdest=0; /**< current log destination */
static char err_filename[PATH_MAX + 1]; static char err_filename[PATH_MAX + 1];
static IOHANDLE err_file = NULL; static FILE *err_file = NULL;
static unsigned int err_debugmask=0xFFFFFFFF; /**< modules to debug, see \ref log_categories */ static unsigned int err_debugmask=0xFFFFFFFF; /**< modules to debug, see \ref log_categories */
static int err_truncate = 0; static int err_truncate = 0;
static int err_syslog_open = 0; static int err_syslog_open = 0;
@ -176,29 +175,20 @@ uint32_t __err_get_threadid(void) {
* would help for log rotation * would help for log rotation
*/ */
void err_reopen(void) { void err_reopen(void) {
char *urltemp;
int ret;
if(!(err_logdest & LOGDEST_LOGFILE)) if(!(err_logdest & LOGDEST_LOGFILE))
return; return;
urltemp = io_urlencode(err_filename); fclose(err_file);
if (!urltemp)
return;
io_close(err_file); err_file = fopen(err_filename, "w");
if (!err_file) {
ret = io_open(err_file, "file://%s?mode=a&ascii=1", urltemp);
free(urltemp);
if (!ret) {
/* what to do when you lose your logging mechanism? Keep /* what to do when you lose your logging mechanism? Keep
* going? * going?
*/ */
err_setdest(err_logdest & (~LOGDEST_LOGFILE)); err_setdest(err_logdest & (~LOGDEST_LOGFILE));
err_setdest(err_logdest | LOGDEST_SYSLOG); err_setdest(err_logdest | LOGDEST_SYSLOG);
DPRINTF(E_LOG,L_MISC,"Could not rotate log file: %s\n", DPRINTF(E_LOG,L_MISC,"Could not rotate log file: %s\n", strerror(errno));
io_errstr(err_file));
return; return;
} }
@ -271,8 +261,8 @@ void err_log(int level, unsigned int cat, char *fmt, ...)
snprintf(timebuf,sizeof(timebuf),"%04d-%02d-%02d %02d:%02d:%02d", snprintf(timebuf,sizeof(timebuf),"%04d-%02d-%02d %02d:%02d:%02d",
tm_now.tm_year + 1900, tm_now.tm_mon + 1, tm_now.tm_mday, tm_now.tm_year + 1900, tm_now.tm_mon + 1, tm_now.tm_mday,
tm_now.tm_hour, tm_now.tm_min, tm_now.tm_sec); tm_now.tm_hour, tm_now.tm_min, tm_now.tm_sec);
io_printf(err_file,"%s (%08x): %s",timebuf,__err_get_threadid(),errbuf); fprintf(err_file,"%s (%08x): %s",timebuf,__err_get_threadid(),errbuf);
if(!level) io_printf(err_file,"%s: Aborting\n",timebuf); if(!level) fprintf(err_file,"%s: Aborting\n",timebuf);
} }
/* always log to stderr on fatal error */ /* always log to stderr on fatal error */
@ -347,8 +337,6 @@ int err_settruncate(int truncate) {
int err_setlogfile(char *file) { int err_setlogfile(char *file) {
char *mode; char *mode;
char *urltemp;
int ret;
int result=TRUE; int result=TRUE;
/* /*
@ -357,16 +345,7 @@ int err_setlogfile(char *file) {
*/ */
if(err_file) { if(err_file) {
io_close(err_file); fclose(err_file);
} else {
err_file = io_new();
if(!err_file) {
err_logdest &= ~LOGDEST_LOGFILE;
if(!err_syslog_open)
openlog(PACKAGE, LOG_PID, LOG_DAEMON);
syslog(lvl2syslog[1], "Error initializing logfile");
}
} }
mode = "a"; mode = "a";
@ -374,17 +353,9 @@ int err_setlogfile(char *file) {
strncpy(err_filename,file,sizeof(err_filename)-1); strncpy(err_filename,file,sizeof(err_filename)-1);
urltemp = io_urlencode(err_filename); err_file = fopen(err_filename, mode);
if (!urltemp) if (!err_file) {
{ fprintf(stderr,"Error opening logfile: %s", strerror(errno));
fprintf(stderr,"Error opening logfile: out of memory\n");
return FALSE;
}
ret = io_open(err_file, "file://%s?mode=%s&ascii=1", urltemp, mode);
free(urltemp);
if (!ret) {
fprintf(stderr,"Error opening logfile: %s",io_errstr(err_file));
err_logdest &= ~LOGDEST_LOGFILE; err_logdest &= ~LOGDEST_LOGFILE;
if(!err_syslog_open) if(!err_syslog_open)
@ -412,7 +383,7 @@ void err_setdest(int destination) {
if((err_logdest & LOGDEST_LOGFILE) && if((err_logdest & LOGDEST_LOGFILE) &&
(!(destination & LOGDEST_LOGFILE))) { (!(destination & LOGDEST_LOGFILE))) {
/* used to be logging to file, not any more */ /* used to be logging to file, not any more */
io_close(err_file); fclose(err_file);
} }
err_logdest=destination; err_logdest=destination;

View File

@ -30,9 +30,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "daapd.h" #include "daapd.h"
#include "io.h"
#include "err.h" #include "err.h"
#include "ff-dbstruct.h" #include "ff-dbstruct.h"
@ -262,21 +263,23 @@ unsigned short int wma_convert_short(unsigned char *src);
unsigned int wma_convert_int(unsigned char *src); unsigned int wma_convert_int(unsigned char *src);
unsigned long long wma_convert_ll(unsigned char *src); unsigned long long wma_convert_ll(unsigned char *src);
char *wma_utf16toutf8(unsigned char *utf16, int len); char *wma_utf16toutf8(unsigned char *utf16, int len);
int wma_parse_content_description(IOHANDLE hfile,int size, MP3FILE *pmp3); int wma_parse_content_description(int fd,int size, MP3FILE *pmp3);
int wma_parse_extended_content_description(IOHANDLE hfile,int size, MP3FILE *pmp3, int extended); int wma_parse_extended_content_description(int fd,int size, MP3FILE *pmp3, int extended);
int wma_parse_file_properties(IOHANDLE hfile,int size, MP3FILE *pmp3); int wma_parse_file_properties(int fd,int size, MP3FILE *pmp3);
int wma_parse_audio_media(IOHANDLE hfile, int size, MP3FILE *pmp3); int wma_parse_audio_media(int fd, int size, MP3FILE *pmp3);
int wma_parse_stream_properties(IOHANDLE hfile, int size, MP3FILE *pmp3); int wma_parse_stream_properties(int fd, int size, MP3FILE *pmp3);
int wma_parse_header_extension(IOHANDLE hfile, int size, MP3FILE *pmp3); int wma_parse_header_extension(int fd, int size, MP3FILE *pmp3);
/** /**
* read an unsigned short int from the fd * read an unsigned short int from the fd
*/ */
int wma_file_read_short(IOHANDLE hfile, unsigned short int *psi) { int wma_file_read_short(int fd, unsigned short int *psi) {
uint32_t len; uint32_t len;
int ret;
len = sizeof(unsigned short int); len = sizeof(unsigned short int);
if(!io_read(hfile,(unsigned char*)psi,&len) || (len != sizeof(unsigned short int))) { ret = read(fd, psi, len);
if ((ret < 0) || (ret != len)) {
return 0; return 0;
} }
@ -287,11 +290,13 @@ int wma_file_read_short(IOHANDLE hfile, unsigned short int *psi) {
/** /**
* read an unsigned int from the fd * read an unsigned int from the fd
*/ */
int wma_file_read_int(IOHANDLE hfile, unsigned int *pi) { int wma_file_read_int(int fd, unsigned int *pi) {
uint32_t len; uint32_t len;
int ret;
len = sizeof(unsigned int); len = sizeof(unsigned int);
if(!io_read(hfile,(unsigned char*)pi,&len) || (len != sizeof(unsigned int))) { ret = read(fd, pi, len);
if((ret < 0) || (ret != len)) {
return 0; return 0;
} }
@ -302,11 +307,13 @@ int wma_file_read_int(IOHANDLE hfile, unsigned int *pi) {
/** /**
* read an ll from the fd * read an ll from the fd
*/ */
int wma_file_read_ll(IOHANDLE hfile, unsigned long long *pll) { int wma_file_read_ll(int fd, unsigned long long *pll) {
uint32_t len; uint32_t len;
int ret;
len = sizeof(unsigned long long); len = sizeof(unsigned long long);
if(!io_read(hfile,(unsigned char *)pll,&len) || (len != sizeof(unsigned long long))) { ret = read(fd, pll, len);
if((ret < 0) || (ret != len)) {
return 0; return 0;
} }
@ -317,17 +324,17 @@ int wma_file_read_ll(IOHANDLE hfile, unsigned long long *pll) {
/** /**
* read a utf-16le string as a utf8 * read a utf-16le string as a utf8
*/ */
int wma_file_read_utf16(IOHANDLE hfile, int len, char **utf8) { int wma_file_read_utf16(int fd, int len, char **utf8) {
char *out; char *out;
unsigned char *utf16; unsigned char *utf16;
uint32_t rlen; int ret;
utf16=(unsigned char*)malloc(len); utf16=(unsigned char*)malloc(len);
if(!utf16) if(!utf16)
return 0; return 0;
rlen = len; ret = read(fd, utf16, len);
if(!io_read(hfile,utf16,&rlen) || (rlen != len)) if((ret < 0) || (ret != len))
return 0; return 0;
out = wma_utf16toutf8(utf16,len); out = wma_utf16toutf8(utf16,len);
@ -337,30 +344,32 @@ int wma_file_read_utf16(IOHANDLE hfile, int len, char **utf8) {
return 1; return 1;
} }
int wma_file_read_bytes(IOHANDLE hfile,int len, unsigned char **data) { int wma_file_read_bytes(int fd,int len, unsigned char **data) {
uint32_t rlen; int ret;
*data = (unsigned char *)malloc(len); *data = (unsigned char *)malloc(len);
if(!*data) if(!*data)
return 0; return 0;
rlen = len; ret = read(fd, *data, len);
if(!io_read(hfile,*data, &rlen) || (rlen != len)) if((ret < 0) || (ret != len))
return 0; return 0;
return 1; return 1;
} }
int wma_parse_header_extension(IOHANDLE hfile, int size, MP3FILE *pmp3) { int wma_parse_header_extension(int fd, int size, MP3FILE *pmp3) {
WMA_HEADER_EXT he; WMA_HEADER_EXT he;
WMA_SUBHEADER sh; WMA_SUBHEADER sh;
WMA_GUID *pguid; WMA_GUID *pguid;
int bytes_left; /* FIXME: uint32_t? */ int bytes_left; /* FIXME: uint32_t? */
uint64_t current; uint64_t current;
uint32_t len; uint32_t len;
int ret;
len = sizeof(he); len = sizeof(he);
if(!io_read(hfile,(unsigned char *)&he,&len) || (len != sizeof(he))) ret = read(fd, &he, len);
if((ret < 0) || (ret != len))
return FALSE; return FALSE;
he.data_size = wma_convert_int((unsigned char *)&he.data_size); he.data_size = wma_convert_int((unsigned char *)&he.data_size);
@ -369,10 +378,11 @@ int wma_parse_header_extension(IOHANDLE hfile, int size, MP3FILE *pmp3) {
while(bytes_left) { while(bytes_left) {
/* read in a subheader */ /* read in a subheader */
io_getpos(hfile,&current); current = lseek(fd, 0, SEEK_CUR);
len = sizeof(sh); len = sizeof(sh);
if(!io_read(hfile,(unsigned char *)&sh,&len) || (len != sizeof(sh))) ret = read(fd, &sh, len);
if((ret < 0) || (ret != len))
return FALSE; return FALSE;
sh.size = wma_convert_ll((unsigned char *)&sh.size); sh.size = wma_convert_ll((unsigned char *)&sh.size);
@ -393,7 +403,7 @@ int wma_parse_header_extension(IOHANDLE hfile, int size, MP3FILE *pmp3) {
} else { } else {
DPRINTF(E_DBG,L_SCAN," Found ext subheader: %s\n", pguid->name); DPRINTF(E_DBG,L_SCAN," Found ext subheader: %s\n", pguid->name);
if(strcmp(pguid->name,"ASF_Metadata_Library_Object")==0) { if(strcmp(pguid->name,"ASF_Metadata_Library_Object")==0) {
if(!wma_parse_extended_content_description(hfile,size,pmp3,1)) if(!wma_parse_extended_content_description(fd,size,pmp3,1))
return FALSE; return FALSE;
} }
} }
@ -403,7 +413,7 @@ int wma_parse_header_extension(IOHANDLE hfile, int size, MP3FILE *pmp3) {
return TRUE; /* guess we're done! */ return TRUE; /* guess we're done! */
bytes_left -= (long)sh.size; bytes_left -= (long)sh.size;
io_setpos(hfile,current + (uint64_t)sh.size,SEEK_SET); lseek(fd,current + (uint64_t)sh.size,SEEK_SET);
} }
return TRUE; return TRUE;
@ -417,13 +427,15 @@ int wma_parse_header_extension(IOHANDLE hfile, int size, MP3FILE *pmp3) {
* @param size size of the content description block * @param size size of the content description block
* @param pmp3 the mp3 struct we are filling with gleaned data * @param pmp3 the mp3 struct we are filling with gleaned data
*/ */
int wma_parse_stream_properties(IOHANDLE hfile, int size, MP3FILE *pmp3) { int wma_parse_stream_properties(int fd, int size, MP3FILE *pmp3) {
WMA_STREAM_PROP sp; WMA_STREAM_PROP sp;
WMA_GUID *pguid; WMA_GUID *pguid;
uint32_t len; uint32_t len;
int ret;
len = sizeof(sp); len = sizeof(sp);
if(!io_read(hfile,(unsigned char *)&sp,&len) || (len != sizeof(sp))) ret = read(fd, &sp, len);
if((ret < 0) || (ret != len))
return FALSE; return FALSE;
pguid = wma_find_guid(sp.stream_type); pguid = wma_find_guid(sp.stream_type);
@ -437,7 +449,7 @@ int wma_parse_stream_properties(IOHANDLE hfile, int size, MP3FILE *pmp3) {
* data should be a WAVEFORMATEX... so we'll leverage * data should be a WAVEFORMATEX... so we'll leverage
* wma_parse_audio_media * wma_parse_audio_media
*/ */
return wma_parse_audio_media(hfile,size - sizeof(WMA_STREAM_PROP),pmp3); return wma_parse_audio_media(fd,size - sizeof(WMA_STREAM_PROP),pmp3);
} }
/** /**
@ -449,13 +461,13 @@ int wma_parse_stream_properties(IOHANDLE hfile, int size, MP3FILE *pmp3) {
* @param size size of the content description block * @param size size of the content description block
* @param pmp3 the mp3 struct we are filling with gleaned data * @param pmp3 the mp3 struct we are filling with gleaned data
*/ */
int wma_parse_audio_media(IOHANDLE hfile, int size, MP3FILE *pmp3) { int wma_parse_audio_media(int fd, int size, MP3FILE *pmp3) {
unsigned short int codec; unsigned short int codec;
if(size < 18) if(size < 18)
return TRUE; /* we'll leave it wma. will work or not! */ return TRUE; /* we'll leave it wma. will work or not! */
if(!wma_file_read_short(hfile,&codec)) { if(!wma_file_read_short(fd,&codec)) {
return FALSE; return FALSE;
} }
@ -479,8 +491,8 @@ int wma_parse_audio_media(IOHANDLE hfile, int size, MP3FILE *pmp3) {
} }
/* might as well get the sample rate while we are at it */ /* might as well get the sample rate while we are at it */
io_setpos(hfile,2,SEEK_CUR); lseek(fd,2,SEEK_CUR);
if(!wma_file_read_int(hfile,(unsigned int *)&pmp3->samplerate)) if(!wma_file_read_int(fd,(unsigned int *)&pmp3->samplerate))
return FALSE; return FALSE;
return TRUE; return TRUE;
@ -494,7 +506,7 @@ int wma_parse_audio_media(IOHANDLE hfile, int size, MP3FILE *pmp3) {
* @param size size of the content description block * @param size size of the content description block
* @param pmp3 the mp3 struct we are filling with gleaned data * @param pmp3 the mp3 struct we are filling with gleaned data
*/ */
int wma_parse_extended_content_description(IOHANDLE hfile,int size, MP3FILE *pmp3, int extended) { int wma_parse_extended_content_description(int fd,int size, MP3FILE *pmp3, int extended) {
unsigned short descriptor_count; unsigned short descriptor_count;
int index; int index;
unsigned short descriptor_name_len; unsigned short descriptor_name_len;
@ -520,31 +532,31 @@ int wma_parse_extended_content_description(IOHANDLE hfile,int size, MP3FILE *pmp
DPRINTF(E_DBG,L_SCAN,"Reading extended content description object\n"); DPRINTF(E_DBG,L_SCAN,"Reading extended content description object\n");
if(!wma_file_read_short(hfile, &descriptor_count)) if(!wma_file_read_short(fd, &descriptor_count))
return FALSE; return FALSE;
for(index = 0; index < descriptor_count; index++) { for(index = 0; index < descriptor_count; index++) {
DPRINTF(E_DBG,L_SCAN,"Reading descr %d of %d\n",index,descriptor_count); DPRINTF(E_DBG,L_SCAN,"Reading descr %d of %d\n",index,descriptor_count);
if(!extended) { if(!extended) {
if(!wma_file_read_short(hfile,&descriptor_name_len)) return FALSE; if(!wma_file_read_short(fd,&descriptor_name_len)) return FALSE;
if(!wma_file_read_utf16(hfile,descriptor_name_len,&descriptor_name)) if(!wma_file_read_utf16(fd,descriptor_name_len,&descriptor_name))
return FALSE; return FALSE;
if(!wma_file_read_short(hfile,&descriptor_value_type)) { if(!wma_file_read_short(fd,&descriptor_value_type)) {
free(descriptor_name); free(descriptor_name);
return FALSE; return FALSE;
} }
if(!wma_file_read_short(hfile,&descriptor_value_len)) { if(!wma_file_read_short(fd,&descriptor_value_len)) {
free(descriptor_name); free(descriptor_name);
return FALSE; return FALSE;
} }
descriptor_value_int = descriptor_value_len; descriptor_value_int = descriptor_value_len;
} else { } else {
if(!wma_file_read_short(hfile,&language_list_index)) return FALSE; if(!wma_file_read_short(fd,&language_list_index)) return FALSE;
if(!wma_file_read_short(hfile,&stream_number)) return FALSE; if(!wma_file_read_short(fd,&stream_number)) return FALSE;
if(!wma_file_read_short(hfile,&descriptor_name_len)) return FALSE; if(!wma_file_read_short(fd,&descriptor_name_len)) return FALSE;
if(!wma_file_read_short(hfile,&descriptor_value_type)) return FALSE; if(!wma_file_read_short(fd,&descriptor_value_type)) return FALSE;
if(!wma_file_read_int(hfile,&descriptor_value_int)) return FALSE; if(!wma_file_read_int(fd,&descriptor_value_int)) return FALSE;
if(!wma_file_read_utf16(hfile,descriptor_name_len,&descriptor_name)) if(!wma_file_read_utf16(fd,descriptor_name_len,&descriptor_name))
return FALSE; return FALSE;
} }
@ -553,7 +565,7 @@ int wma_parse_extended_content_description(IOHANDLE hfile,int size, MP3FILE *pmp
/* see what kind it is */ /* see what kind it is */
switch(descriptor_value_type) { switch(descriptor_value_type) {
case 0x0000: /* string */ case 0x0000: /* string */
if(!wma_file_read_utf16(hfile,descriptor_value_int, if(!wma_file_read_utf16(fd,descriptor_value_int,
&descriptor_byte_value)) { &descriptor_byte_value)) {
fail=1; fail=1;
} }
@ -562,11 +574,11 @@ int wma_parse_extended_content_description(IOHANDLE hfile,int size, MP3FILE *pmp
break; break;
case 0x0001: /* byte array */ case 0x0001: /* byte array */
if(descriptor_value_int > 4096) { if(descriptor_value_int > 4096) {
io_setpos(hfile,(uint64_t)descriptor_value_int,SEEK_CUR); lseek(fd,(uint64_t)descriptor_value_int,SEEK_CUR);
descriptor_byte_value = NULL; descriptor_byte_value = NULL;
} else { } else {
ptr = (unsigned char *)descriptor_byte_value; ptr = (unsigned char *)descriptor_byte_value;
if(!wma_file_read_bytes(hfile,descriptor_value_int, if(!wma_file_read_bytes(fd,descriptor_value_int,
&ptr)){ &ptr)){
fail=1; fail=1;
} }
@ -575,25 +587,25 @@ int wma_parse_extended_content_description(IOHANDLE hfile,int size, MP3FILE *pmp
break; break;
case 0x0002: /* bool - dropthru */ case 0x0002: /* bool - dropthru */
case 0x0003: /* dword */ case 0x0003: /* dword */
if(!wma_file_read_int(hfile,&descriptor_int_value)) fail=1; if(!wma_file_read_int(fd,&descriptor_int_value)) fail=1;
DPRINTF(E_DBG,L_SCAN,"Type: int, value: %d\n",descriptor_int_value); DPRINTF(E_DBG,L_SCAN,"Type: int, value: %d\n",descriptor_int_value);
snprintf(numbuff,sizeof(numbuff)-1,"%d",descriptor_int_value); snprintf(numbuff,sizeof(numbuff)-1,"%d",descriptor_int_value);
descriptor_byte_value = strdup(numbuff); descriptor_byte_value = strdup(numbuff);
break; break;
case 0x0004: /* qword */ case 0x0004: /* qword */
if(!wma_file_read_ll(hfile,&descriptor_ll_value)) fail=1; if(!wma_file_read_ll(fd,&descriptor_ll_value)) fail=1;
DPRINTF(E_DBG,L_SCAN,"Type: ll, value: %lld\n",descriptor_ll_value); DPRINTF(E_DBG,L_SCAN,"Type: ll, value: %lld\n",descriptor_ll_value);
snprintf(numbuff,sizeof(numbuff)-1,"%lld",descriptor_ll_value); snprintf(numbuff,sizeof(numbuff)-1,"%lld",descriptor_ll_value);
descriptor_byte_value = strdup(numbuff); descriptor_byte_value = strdup(numbuff);
break; break;
case 0x0005: /* word */ case 0x0005: /* word */
if(!wma_file_read_short(hfile,&descriptor_short_value)) fail=1; if(!wma_file_read_short(fd,&descriptor_short_value)) fail=1;
DPRINTF(E_DBG,L_SCAN,"type: short, value %d\n",descriptor_short_value); DPRINTF(E_DBG,L_SCAN,"type: short, value %d\n",descriptor_short_value);
snprintf(numbuff,sizeof(numbuff)-1,"%d",descriptor_short_value); snprintf(numbuff,sizeof(numbuff)-1,"%d",descriptor_short_value);
descriptor_byte_value = strdup(numbuff); descriptor_byte_value = strdup(numbuff);
break; break;
case 0x0006: /* guid */ case 0x0006: /* guid */
io_setpos(hfile,16,SEEK_CUR); /* skip it */ lseek(fd,16,SEEK_CUR); /* skip it */
if(descriptor_name) if(descriptor_name)
free(descriptor_name); free(descriptor_name);
descriptor_name = strdup(""); descriptor_name = strdup("");
@ -711,7 +723,7 @@ int wma_parse_extended_content_description(IOHANDLE hfile,int size, MP3FILE *pmp
* @param size size of the content description block * @param size size of the content description block
* @param pmp3 the mp3 struct we are filling with gleaned data * @param pmp3 the mp3 struct we are filling with gleaned data
*/ */
int wma_parse_content_description(IOHANDLE hfile,int size, MP3FILE *pmp3) { int wma_parse_content_description(int fd,int size, MP3FILE *pmp3) {
unsigned short sizes[5]; unsigned short sizes[5];
int index; int index;
char *utf8; char *utf8;
@ -720,13 +732,13 @@ int wma_parse_content_description(IOHANDLE hfile,int size, MP3FILE *pmp3) {
return FALSE; return FALSE;
for(index=0; index < 5; index++) { for(index=0; index < 5; index++) {
if(!wma_file_read_short(hfile,&sizes[index])) if(!wma_file_read_short(fd,&sizes[index]))
return FALSE; return FALSE;
} }
for(index=0;index<5;index++) { for(index=0;index<5;index++) {
if(sizes[index]) { if(sizes[index]) {
if(!wma_file_read_utf16(hfile,sizes[index],&utf8)) if(!wma_file_read_utf16(fd,sizes[index],&utf8))
return FALSE; return FALSE;
DPRINTF(E_DBG,L_SCAN,"Got item of length %d: %s\n",sizes[index],utf8); DPRINTF(E_DBG,L_SCAN,"Got item of length %d: %s\n",sizes[index],utf8);
@ -771,7 +783,7 @@ int wma_parse_content_description(IOHANDLE hfile,int size, MP3FILE *pmp3) {
* @param size size of the content description block * @param size size of the content description block
* @param pmp3 the mp3 struct we are filling with gleaned data * @param pmp3 the mp3 struct we are filling with gleaned data
*/ */
int wma_parse_file_properties(IOHANDLE hfile,int size, MP3FILE *pmp3) { int wma_parse_file_properties(int fd,int size, MP3FILE *pmp3) {
unsigned long long play_duration; unsigned long long play_duration;
unsigned long long send_duration; unsigned long long send_duration;
unsigned long long preroll; unsigned long long preroll;
@ -781,15 +793,15 @@ int wma_parse_file_properties(IOHANDLE hfile,int size, MP3FILE *pmp3) {
/* skip guid (16 bytes), filesize (8), creation time (8), /* skip guid (16 bytes), filesize (8), creation time (8),
* data packets (8) * data packets (8)
*/ */
io_setpos(hfile,40,SEEK_CUR); lseek(fd,40,SEEK_CUR);
if(!wma_file_read_ll(hfile, &play_duration)) if(!wma_file_read_ll(fd, &play_duration))
return FALSE; return FALSE;
if(!wma_file_read_ll(hfile, &send_duration)) if(!wma_file_read_ll(fd, &send_duration))
return FALSE; return FALSE;
if(!wma_file_read_ll(hfile, &preroll)) if(!wma_file_read_ll(fd, &preroll))
return FALSE; return FALSE;
DPRINTF(E_DBG,L_SCAN,"play_duration: %lld, " DPRINTF(E_DBG,L_SCAN,"play_duration: %lld, "
@ -806,8 +818,8 @@ int wma_parse_file_properties(IOHANDLE hfile,int size, MP3FILE *pmp3) {
* min_packet_size (4), max_packet_size(4) * min_packet_size (4), max_packet_size(4)
*/ */
io_setpos(hfile,12,SEEK_CUR); lseek(fd,12,SEEK_CUR);
if(!wma_file_read_int(hfile,&max_bitrate)) if(!wma_file_read_int(fd,&max_bitrate))
return FALSE; return FALSE;
pmp3->bitrate = max_bitrate/1000; pmp3->bitrate = max_bitrate/1000;
@ -969,7 +981,6 @@ unsigned long long wma_convert_ll(unsigned char *src) {
* @param pmp3 MP3FILE struct to be filled with with metainfo * @param pmp3 MP3FILE struct to be filled with with metainfo
*/ */
int scan_get_wmainfo(char *filename, MP3FILE *pmp3) { int scan_get_wmainfo(char *filename, MP3FILE *pmp3) {
IOHANDLE hfile;
WMA_HEADER hdr; WMA_HEADER hdr;
WMA_SUBHEADER subhdr; WMA_SUBHEADER subhdr;
WMA_GUID *pguid; WMA_GUID *pguid;
@ -978,43 +989,29 @@ int scan_get_wmainfo(char *filename, MP3FILE *pmp3) {
int item; int item;
int res=TRUE; int res=TRUE;
int encrypted = 0; int encrypted = 0;
char *urltemp; int fd;
int ret; int ret;
if(!(hfile = io_new())) { fd = open(filename, O_RDONLY);
DPRINTF(E_LOG,L_SCAN,"Can't create new file handle\n"); if (fd < 0) {
return FALSE;
}
urltemp = io_urlencode(filename);
if (!urltemp)
{
DPRINTF(E_INF,L_SCAN,"Error opening WMA file (%s): out of memory\n", filename);
io_dispose(hfile);
return FALSE;
}
ret = io_open(hfile, "file://%s", urltemp);
free(urltemp);
if (!ret) {
DPRINTF(E_INF,L_SCAN,"Error opening WMA file (%s): %s\n",filename, DPRINTF(E_INF,L_SCAN,"Error opening WMA file (%s): %s\n",filename,
io_errstr(hfile)); strerror(errno));
io_dispose(hfile);
return FALSE; return FALSE;
} }
len = sizeof(hdr); len = sizeof(hdr);
if(!io_read(hfile,(unsigned char *)&hdr,&len) || (len != sizeof(hdr))) { ret = read(fd, &hdr, len);
if((ret < 0) || (ret != len)) {
DPRINTF(E_INF,L_SCAN,"Error reading from %s: %s\n",filename, DPRINTF(E_INF,L_SCAN,"Error reading from %s: %s\n",filename,
strerror(errno)); strerror(errno));
io_dispose(hfile); close(fd);
return FALSE; return FALSE;
} }
pguid = wma_find_guid(hdr.objectid); pguid = wma_find_guid(hdr.objectid);
if(!pguid) { if(!pguid) {
DPRINTF(E_INF,L_SCAN,"Could not find header in %s\n",filename); DPRINTF(E_INF,L_SCAN,"Could not find header in %s\n",filename);
io_dispose(hfile); close(fd);
return FALSE; return FALSE;
} }
@ -1032,17 +1029,18 @@ int scan_get_wmainfo(char *filename, MP3FILE *pmp3) {
*/ */
for(item=0; item < (int) hdr.objects; item++) { for(item=0; item < (int) hdr.objects; item++) {
if(!io_setpos(hfile,offset,SEEK_SET)) { if(!lseek(fd,offset,SEEK_SET)) {
DPRINTF(E_INF,L_SCAN,"Error seeking in %s\n",filename); DPRINTF(E_INF,L_SCAN,"Error seeking in %s\n",filename);
io_dispose(hfile); close(fd);
return FALSE; return FALSE;
} }
len = sizeof(subhdr); len = sizeof(subhdr);
if(!io_read(hfile,(unsigned char *)&subhdr,&len) || (len != sizeof(subhdr))) { ret = read(fd, &subhdr, len);
if((ret < 0) || (ret != len)) {
DPRINTF(E_INF,L_SCAN,"Error reading from %s: %s\n",filename, DPRINTF(E_INF,L_SCAN,"Error reading from %s: %s\n",filename,
io_errstr(hfile)); strerror(errno));
io_dispose(hfile); close(fd);
return FALSE; return FALSE;
} }
@ -1053,17 +1051,17 @@ int scan_get_wmainfo(char *filename, MP3FILE *pmp3) {
DPRINTF(E_DBG,L_SCAN,"%ll: Found subheader: %s\n", DPRINTF(E_DBG,L_SCAN,"%ll: Found subheader: %s\n",
offset,pguid->name); offset,pguid->name);
if(strcmp(pguid->name,"ASF_Content_Description_Object")==0) { if(strcmp(pguid->name,"ASF_Content_Description_Object")==0) {
res &= wma_parse_content_description(hfile,(int)subhdr.size,pmp3); res &= wma_parse_content_description(fd,(int)subhdr.size,pmp3);
} else if (strcmp(pguid->name,"ASF_Extended_Content_Description_Object")==0) { } else if (strcmp(pguid->name,"ASF_Extended_Content_Description_Object")==0) {
res &= wma_parse_extended_content_description(hfile,(int)subhdr.size,pmp3,0); res &= wma_parse_extended_content_description(fd,(int)subhdr.size,pmp3,0);
} else if (strcmp(pguid->name,"ASF_File_Properties_Object")==0) { } else if (strcmp(pguid->name,"ASF_File_Properties_Object")==0) {
res &= wma_parse_file_properties(hfile,(int)subhdr.size,pmp3); res &= wma_parse_file_properties(fd,(int)subhdr.size,pmp3);
} else if (strcmp(pguid->name,"ASF_Audio_Media")==0) { } else if (strcmp(pguid->name,"ASF_Audio_Media")==0) {
res &= wma_parse_audio_media(hfile,(int)subhdr.size,pmp3); res &= wma_parse_audio_media(fd,(int)subhdr.size,pmp3);
} else if (strcmp(pguid->name,"ASF_Stream_Properties_Object")==0) { } else if (strcmp(pguid->name,"ASF_Stream_Properties_Object")==0) {
res &= wma_parse_stream_properties(hfile,(int)subhdr.size,pmp3); res &= wma_parse_stream_properties(fd,(int)subhdr.size,pmp3);
} else if(strcmp(pguid->name,"ASF_Header_Extension_Object")==0) { } else if(strcmp(pguid->name,"ASF_Header_Extension_Object")==0) {
res &= wma_parse_header_extension(hfile,(int)subhdr.size,pmp3); res &= wma_parse_header_extension(fd,(int)subhdr.size,pmp3);
} else if(strstr(pguid->name,"Content_Encryption_Object")) { } else if(strstr(pguid->name,"Content_Encryption_Object")) {
encrypted=1; encrypted=1;
} }
@ -1092,8 +1090,7 @@ int scan_get_wmainfo(char *filename, MP3FILE *pmp3) {
DPRINTF(E_DBG,L_SCAN,"Successfully parsed file\n"); DPRINTF(E_DBG,L_SCAN,"Successfully parsed file\n");
} }
io_dispose(hfile); close(fd);
if(encrypted) { if(encrypted) {
if(pmp3->codectype) if(pmp3->codectype)