Commit b204eb53 authored by Monty's avatar Monty
Browse files

Begin async error reporting interface; added to vorbis_comment and vorbis_info


svn path=/branches/vorbis-malloccheck/; revision=16097
parent 01dc791e
......@@ -26,7 +26,7 @@ extern "C"
#include <ogg/ogg.h>
typedef struct vorbis_info{
int version;
int version; /* < 0 indicates error state */
int channels;
long rate;
......@@ -164,6 +164,7 @@ typedef struct vorbis_comment{
extern void vorbis_info_init(vorbis_info *vi);
extern void vorbis_info_clear(vorbis_info *vi);
extern int vorbis_info_check(vorbis_info *vi);
extern int vorbis_info_blocksize(vorbis_info *vi,int zo);
extern void vorbis_comment_init(vorbis_comment *vc);
extern void vorbis_comment_add(vorbis_comment *vc, const char *comment);
......@@ -172,6 +173,7 @@ extern void vorbis_comment_add_tag(vorbis_comment *vc,
extern char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count);
extern int vorbis_comment_query_count(vorbis_comment *vc, const char *tag);
extern void vorbis_comment_clear(vorbis_comment *vc);
extern int vorbis_comment_check(vorbis_comment *vc);
extern int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb);
extern int vorbis_block_clear(vorbis_block *vb);
......@@ -234,6 +236,8 @@ extern int vorbis_synthesis_halfrate_p(vorbis_info *v);
#define OV_EBADPACKET -136
#define OV_EBADLINK -137
#define OV_ENOSEEK -138
#define OV_ESTACK -139
#define OV_EHEAP -140
#ifdef __cplusplus
}
......
......@@ -91,7 +91,7 @@ int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
vb->vd=v;
vb->localalloc=0;
vb->localstore=NULL;
if(v->analysisp){
if(v->analysisp>0){
vorbis_block_internal *vbi=
vb->internal=_ogg_calloc(1,sizeof(vorbis_block_internal));
vbi->ampmax=-9999;
......
......@@ -31,8 +31,8 @@
#include "misc.h"
#include "os.h"
#define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.2.2rc1"
#define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20090604"
#define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.2.3pre"
#define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20090610"
/* helpers */
static int ilog2(unsigned int v){
......@@ -46,7 +46,6 @@ static int ilog2(unsigned int v){
}
static void _v_writestring(oggpack_buffer *o,const char *s, int bytes){
while(bytes--){
oggpack_write(o,*s++,8);
}
......@@ -59,27 +58,49 @@ static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
}
void vorbis_comment_init(vorbis_comment *vc){
if(vc)
memset(vc,0,sizeof(*vc));
}
void vorbis_comment_add(vorbis_comment *vc,const char *comment){
vc->user_comments=_ogg_realloc(vc->user_comments,
if(vc && vc->comments>=0){
void *ret =_ogg_realloc(vc->user_comments,
(vc->comments+2)*sizeof(*vc->user_comments));
vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
if(!ret)goto err;
vc->user_comments=ret;
ret=_ogg_realloc(vc->comment_lengths,
(vc->comments+2)*sizeof(*vc->comment_lengths));
if(!ret)goto err;
vc->comment_lengths=ret;
vc->comment_lengths[vc->comments]=strlen(comment);
vc->user_comments[vc->comments]=_ogg_malloc(vc->comment_lengths[vc->comments]+1);
ret=_ogg_malloc(vc->comment_lengths[vc->comments]+1);
if(!ret)goto err;
vc->user_comments[vc->comments]=ret;
strcpy(vc->user_comments[vc->comments], comment);
vc->comments++;
vc->user_comments[vc->comments]=NULL;
}
return;
err:
vorbis_comment_clear(vc);
vc->comments=OV_EHEAP;
}
void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, const char *contents){
if(vc && vc->comments>=0 && tag && contents){
char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
if(comment==NULL){
vorbis_comment_clear(vc);
vc->comments=OV_ESTACK;
}else{
strcpy(comment, tag);
strcat(comment, "=");
strcat(comment, contents);
vorbis_comment_add(vc, comment);
}
}
}
/* This is more or less the same as strncasecmp - but that doesn't exist
......@@ -100,6 +121,7 @@ char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count){
int taglen = strlen(tag)+1; /* +1 for the = we append */
char *fulltag = alloca(taglen+ 1);
if(fulltag){
strcpy(fulltag, tag);
strcat(fulltag, "=");
......@@ -112,6 +134,10 @@ char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count){
found++;
}
}
}else{
vorbis_comment_clear(vc);
vc->comments=OV_ESTACK;
}
return NULL; /* didn't find anything */
}
......@@ -119,6 +145,8 @@ int vorbis_comment_query_count(vorbis_comment *vc, const char *tag){
int i,count=0;
int taglen = strlen(tag)+1; /* +1 for the = we append */
char *fulltag = alloca(taglen+1);
if(fulltag){
strcpy(fulltag,tag);
strcat(fulltag, "=");
......@@ -127,6 +155,11 @@ int vorbis_comment_query_count(vorbis_comment *vc, const char *tag){
count++;
}
}else{
vorbis_comment_clear(vc);
vc->comments=OV_ESTACK;
}
return count;
}
......@@ -144,20 +177,33 @@ void vorbis_comment_clear(vorbis_comment *vc){
}
}
int vorbis_comment_check(vorbis_comment *vc){
return (vc ? (vc->comments<0 ? vc->comments:0) : OV_EFAULT);
}
/* blocksize 0 is guaranteed to be short, 1 is guaranteed to be long.
They may be equal, but short will never ge greater than long */
int vorbis_info_blocksize(vorbis_info *vi,int zo){
if(vi && vi->version>=0){
codec_setup_info *ci = vi->codec_setup;
return ci ? ci->blocksizes[zo] : -1;
}
return -1;
}
/* used by synthesis, which has a full, alloced vi */
void vorbis_info_init(vorbis_info *vi){
if(vi){
memset(vi,0,sizeof(*vi));
vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
if(!vi->codec_setup){
vi->version = OV_EHEAP;
}
}
}
void vorbis_info_clear(vorbis_info *vi){
if(vi){
codec_setup_info *ci=vi->codec_setup;
int i;
......@@ -202,12 +248,19 @@ void vorbis_info_clear(vorbis_info *vi){
}
memset(vi,0,sizeof(*vi));
}
}
int vorbis_info_check(vorbis_info *vi){
return(vi ? (vi->version>=0?0:vi->version) : OV_EFAULT);
}
/* Header packing/unpacking ********************************************/
static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
if(vi && opb){
codec_setup_info *ci=vi->codec_setup;
if(vi->version<0)return vi->version;
if(!ci)return(OV_EFAULT);
vi->version=oggpack_read(opb,32);
......@@ -234,22 +287,35 @@ static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
return(0);
err_out:
vorbis_info_clear(vi);
return(OV_EBADHEADER);
vi->version=OV_EBADHEADER;
return(vi->version=OV_EBADHEADER);
}
return(OV_EFAULT);
}
static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
int i;
int ret=OV_EBADHEADER;
int vendorlen=oggpack_read(opb,32);
if(vendorlen<0)goto err_out;
if(vendorlen+8>opb->storage)goto err_out;
vc->vendor=_ogg_calloc(vendorlen+1,1);
if(!vc->vendor){
ret=OV_EHEAP;
goto err_out;
}
_v_readstring(opb,vc->vendor,vendorlen);
i=oggpack_read(opb,32);
if(i<0)goto err_out;
if(4*i+oggpack_bytes(opb)>opb->storage)goto err_out;
vc->comments=i;
vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
if(!vc->user_comments || !vc->comment_lengths){
ret=OV_EHEAP;
goto err_out;
}
for(i=0;i<vc->comments;i++){
int len=oggpack_read(opb,32);
......@@ -257,6 +323,10 @@ static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
if(len+oggpack_bytes(opb)>opb->storage)goto err_out;
vc->comment_lengths[i]=len;
vc->user_comments[i]=_ogg_calloc(len+1,1);
if(!vc->user_comments[i]){
ret=OV_EHEAP;
goto err_out;
}
_v_readstring(opb,vc->user_comments[i],len);
}
if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
......@@ -264,14 +334,19 @@ static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
return(0);
err_out:
vorbis_comment_clear(vc);
return(OV_EBADHEADER);
vc->comments= -1;
return(ret);
}
/* all of the real encoding details are here. The modes, books,
everything */
static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
if(vi){
codec_setup_info *ci=vi->codec_setup;
int i;
int ret=OV_EBADHEADER;
if(vi->version<0)return(vi->version);
if(!ci)return(OV_EFAULT);
/* codebooks */
......@@ -279,7 +354,11 @@ static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
/*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
for(i=0;i<ci->books;i++){
ci->book_param[i]=_ogg_calloc(1,sizeof(*ci->book_param[i]));
if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
if(!ci->book_param[i]){
ret=OV_EHEAP;
goto err_out;
}
if((ret=vorbis_staticbook_unpack(opb,ci->book_param[i])))goto err_out;
}
/* time backend settings; hooks are unused */
......@@ -329,6 +408,10 @@ static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
/*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
for(i=0;i<ci->modes;i++){
ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i]));
if(!ci->mode_param[i]){
ret=OV_EHEAP;
goto err_out;
}
ci->mode_param[i]->blockflag=oggpack_read(opb,1);
ci->mode_param[i]->windowtype=oggpack_read(opb,16);
ci->mode_param[i]->transformtype=oggpack_read(opb,16);
......@@ -343,8 +426,14 @@ static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
return(0);
err_out:
if(vi->version<0)ret=vi->version; /* hack to return error state
from unpack functions called
through dispatch table */
vorbis_info_clear(vi);
return(OV_EBADHEADER);
vi->version=ret;
return(ret);
}
return(OV_EFAULT);
}
/* Is this packet a vorbis ID header? */
......@@ -436,7 +525,9 @@ int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op)
/* pack side **********************************************************/
static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
if(opb && vi){
codec_setup_info *ci=vi->codec_setup;
if(vi->version<0)return(vi->version);
if(!ci)return(OV_EFAULT);
/* preamble */
......@@ -455,8 +546,9 @@ static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
oggpack_write(opb,1,1);
return(0);
return(oggpack_writecheck(opb)?OV_EHEAP:0);
}
return OV_EFAULT;
}
static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
......@@ -486,13 +578,16 @@ static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
}
oggpack_write(opb,1,1);
return(0);
return(oggpack_writecheck(opb)?OV_EHEAP:0);
}
static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
int ret=OV_EFAULT;
if(opb && vi){
codec_setup_info *ci=vi->codec_setup;
int i;
if(!ci)return(OV_EFAULT);
if(vi->version<0)return(vi->version);
if(!ci)goto err_out;
oggpack_write(opb,0x05,8);
_v_writestring(opb,"vorbis", 6);
......@@ -500,7 +595,7 @@ static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
/* books */
oggpack_write(opb,ci->books-1,8);
for(i=0;i<ci->books;i++)
if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
if((ret=vorbis_staticbook_pack(ci->book_param[i],opb)))goto err_out;
/* times; hook placeholders */
oggpack_write(opb,0,6);
......@@ -510,11 +605,13 @@ static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
oggpack_write(opb,ci->floors-1,6);
for(i=0;i<ci->floors;i++){
oggpack_write(opb,ci->floor_type[i],16);
if(_floor_P[ci->floor_type[i]]->pack)
if(_floor_P[ci->floor_type[i]]->pack){
_floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
else
}else{
ret=OV_EFAULT;
goto err_out;
}
}
/* residues */
oggpack_write(opb,ci->residues-1,6);
......@@ -540,20 +637,28 @@ static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
}
oggpack_write(opb,1,1);
return(0);
return(oggpack_writecheck(opb)?OV_EHEAP:0);
}
err_out:
return(-1);
return(ret);
}
int vorbis_commentheader_out(vorbis_comment *vc,
ogg_packet *op){
if(op){
oggpack_buffer opb;
int ret;
oggpack_writeinit(&opb);
if(_vorbis_pack_comment(&opb,vc)) return OV_EIMPL;
if((ret=_vorbis_pack_comment(&opb,vc)))return(ret);
op->packet = _ogg_malloc(oggpack_bytes(&opb));
if(!op->packet){
oggpack_writeclear(&opb);
memset(op,0,sizeof(*op));
return OV_EHEAP;
}
memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
op->bytes=oggpack_bytes(&opb);
......@@ -561,8 +666,11 @@ int vorbis_commentheader_out(vorbis_comment *vc,
op->e_o_s=0;
op->granulepos=0;
op->packetno=1;
oggpack_writeclear(&opb);
return 0;
}
return OV_EFAULT;
}
int vorbis_analysis_headerout(vorbis_dsp_state *v,
......@@ -570,24 +678,26 @@ int vorbis_analysis_headerout(vorbis_dsp_state *v,
ogg_packet *op,
ogg_packet *op_comm,
ogg_packet *op_code){
int ret=OV_EIMPL;
if(v){
vorbis_info *vi=v->vi;
oggpack_buffer opb;
private_state *b=v->backend_state;
int ret=0;
if(!b){
ret=OV_EFAULT;
goto err_out;
}
if(!vc || !b || !op || !op_comm || !op_code) goto err_out;
/* first header packet **********************************************/
oggpack_writeinit(&opb);
if(_vorbis_pack_info(&opb,vi))goto err_out;
if((ret=_vorbis_pack_info(&opb,vi)))goto err_out;
/* build the packet */
if(b->header)_ogg_free(b->header);
b->header=_ogg_malloc(oggpack_bytes(&opb));
if(!b->header){
ret=OV_EHEAP;
goto err_out;
}
memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
op->packet=b->header;
op->bytes=oggpack_bytes(&opb);
......@@ -599,10 +709,13 @@ int vorbis_analysis_headerout(vorbis_dsp_state *v,
/* second header packet (comments) **********************************/
oggpack_reset(&opb);
if(_vorbis_pack_comment(&opb,vc))goto err_out;
if((ret=_vorbis_pack_comment(&opb,vc)))goto err_out;
if(b->header1)_ogg_free(b->header1);
b->header1=_ogg_malloc(oggpack_bytes(&opb));
if(!b->header1){
ret=OV_EHEAP;
goto err_out;
}
memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
op_comm->packet=b->header1;
op_comm->bytes=oggpack_bytes(&opb);
......@@ -614,10 +727,14 @@ int vorbis_analysis_headerout(vorbis_dsp_state *v,
/* third header packet (modes/codebooks) ****************************/
oggpack_reset(&opb);
if(_vorbis_pack_books(&opb,vi))goto err_out;
if((ret=_vorbis_pack_books(&opb,vi)))goto err_out;
if(b->header2)_ogg_free(b->header2);
b->header2=_ogg_malloc(oggpack_bytes(&opb));
if(!b->header2){
ret=OV_EHEAP;
goto err_out;
}
memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
op_code->packet=b->header2;
op_code->bytes=oggpack_bytes(&opb);
......@@ -643,9 +760,13 @@ int vorbis_analysis_headerout(vorbis_dsp_state *v,
b->header2=NULL;
}
return(ret);
}
return(OV_EFAULT);
}
double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){
if(v && v->vi && v->vi->version>=0)
if(granulepos>=0)
return((double)granulepos/v->vi->rate);
return(-1);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment