Fix _v_writestring() bug reported by Erik Kruus, to fix an 8-bit cleanness
[platform/upstream/libvorbis.git] / lib / info.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10
11  ********************************************************************
12
13  function: maintain the info structure, info <-> header packets
14  last mod: $Id: info.c,v 1.47 2001/10/14 06:52:07 msmith Exp $
15
16  ********************************************************************/
17
18 /* general handling of the header and the vorbis_info structure (and
19    substructures) */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <ogg/ogg.h>
25 #include "vorbis/codec.h"
26 #include "codec_internal.h"
27 #include "codebook.h"
28 #include "registry.h"
29 #include "window.h"
30 #include "psy.h"
31 #include "misc.h"
32 #include "os.h"
33
34 /* helpers */
35 static int ilog2(unsigned int v){
36   int ret=0;
37   while(v>1){
38     ret++;
39     v>>=1;
40   }
41   return(ret);
42 }
43
44 static void _v_writestring(oggpack_buffer *o,char *s, int bytes){
45
46   while(bytes--){
47     oggpack_write(o,*s++,8);
48   }
49 }
50
51 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
52   while(bytes--){
53     *buf++=oggpack_read(o,8);
54   }
55 }
56
57 void vorbis_comment_init(vorbis_comment *vc){
58   memset(vc,0,sizeof(*vc));
59 }
60
61 void vorbis_comment_add(vorbis_comment *vc,char *comment){
62   vc->user_comments=_ogg_realloc(vc->user_comments,
63                             (vc->comments+2)*sizeof(*vc->user_comments));
64   vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
65                             (vc->comments+2)*sizeof(*vc->comment_lengths));
66   vc->user_comments[vc->comments]=strdup(comment);
67   vc->comment_lengths[vc->comments]=strlen(comment);
68   vc->comments++;
69   vc->user_comments[vc->comments]=NULL;
70 }
71
72 void vorbis_comment_add_tag(vorbis_comment *vc, char *tag, char *contents){
73   char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
74   strcpy(comment, tag);
75   strcat(comment, "=");
76   strcat(comment, contents);
77   vorbis_comment_add(vc, comment);
78 }
79
80 /* This is more or less the same as strncasecmp - but that doesn't exist
81  * everywhere, and this is a fairly trivial function, so we include it */
82 static int tagcompare(const char *s1, const char *s2, int n){
83   int c=0;
84   while(c < n){
85     if(toupper(s1[c]) != toupper(s2[c]))
86       return !0;
87     c++;
88   }
89   return 0;
90 }
91
92 char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){
93   long i;
94   int found = 0;
95   int taglen = strlen(tag)+1; /* +1 for the = we append */
96   char *fulltag = alloca(taglen+ 1);
97
98   strcpy(fulltag, tag);
99   strcat(fulltag, "=");
100   
101   for(i=0;i<vc->comments;i++){
102     if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
103       if(count == found)
104         /* We return a pointer to the data, not a copy */
105         return vc->user_comments[i] + taglen;
106       else
107         found++;
108     }
109   }
110   return NULL; /* didn't find anything */
111 }
112
113 int vorbis_comment_query_count(vorbis_comment *vc, char *tag){
114   int i,count=0;
115   int taglen = strlen(tag)+1; /* +1 for the = we append */
116   char *fulltag = alloca(taglen+1);
117   strcpy(fulltag,tag);
118   strcat(fulltag, "=");
119
120   for(i=0;i<vc->comments;i++){
121     if(!tagcompare(vc->user_comments[i], fulltag, taglen))
122       count++;
123   }
124
125   return count;
126 }
127
128 void vorbis_comment_clear(vorbis_comment *vc){
129   if(vc){
130     long i;
131     for(i=0;i<vc->comments;i++)
132       if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
133     if(vc->user_comments)_ogg_free(vc->user_comments);
134         if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
135     if(vc->vendor)_ogg_free(vc->vendor);
136   }
137   memset(vc,0,sizeof(*vc));
138 }
139
140 /* used by synthesis, which has a full, alloced vi */
141 void vorbis_info_init(vorbis_info *vi){
142   memset(vi,0,sizeof(*vi));
143   vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
144 }
145
146 void vorbis_info_clear(vorbis_info *vi){
147   codec_setup_info     *ci=vi->codec_setup;
148   int i;
149
150   if(ci){
151
152     for(i=0;i<ci->modes;i++)
153       if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
154
155     for(i=0;i<ci->maps;i++) /* unpack does the range checking */
156       _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
157
158     for(i=0;i<ci->times;i++) /* unpack does the range checking */
159       _time_P[ci->time_type[i]]->free_info(ci->time_param[i]);
160
161     for(i=0;i<ci->floors;i++) /* unpack does the range checking */
162       _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
163     
164     for(i=0;i<ci->residues;i++) /* unpack does the range checking */
165       _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
166
167     for(i=0;i<ci->books;i++){
168       if(ci->book_param[i]){
169         /* knows if the book was not alloced */
170         vorbis_staticbook_destroy(ci->book_param[i]);
171       }
172     }
173     
174     for(i=0;i<ci->psys;i++)
175       _vi_psy_free(ci->psy_param[i]);
176
177     _ogg_free(ci);
178   }
179
180   memset(vi,0,sizeof(*vi));
181 }
182
183 /* Header packing/unpacking ********************************************/
184
185 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
186   codec_setup_info     *ci=vi->codec_setup;
187   if(!ci)return(OV_EFAULT);
188
189   vi->version=oggpack_read(opb,32);
190   if(vi->version!=0)return(OV_EVERSION);
191
192   vi->channels=oggpack_read(opb,8);
193   vi->rate=oggpack_read(opb,32);
194
195   vi->bitrate_upper=oggpack_read(opb,32);
196   vi->bitrate_nominal=oggpack_read(opb,32);
197   vi->bitrate_lower=oggpack_read(opb,32);
198
199   ci->blocksizes[0]=1<<oggpack_read(opb,4);
200   ci->blocksizes[1]=1<<oggpack_read(opb,4);
201   
202   if(vi->rate<1)goto err_out;
203   if(vi->channels<1)goto err_out;
204   if(ci->blocksizes[0]<8)goto err_out; 
205   if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
206   
207   if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
208
209   return(0);
210  err_out:
211   vorbis_info_clear(vi);
212   return(OV_EBADHEADER);
213 }
214
215 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
216   int i;
217   int vendorlen=oggpack_read(opb,32);
218   if(vendorlen<0)goto err_out;
219   vc->vendor=_ogg_calloc(vendorlen+1,1);
220   _v_readstring(opb,vc->vendor,vendorlen);
221   vc->comments=oggpack_read(opb,32);
222   if(vc->comments<0)goto err_out;
223   vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
224   vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
225             
226   for(i=0;i<vc->comments;i++){
227     int len=oggpack_read(opb,32);
228     if(len<0)goto err_out;
229         vc->comment_lengths[i]=len;
230     vc->user_comments[i]=_ogg_calloc(len+1,1);
231     _v_readstring(opb,vc->user_comments[i],len);
232   }       
233   if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
234
235   return(0);
236  err_out:
237   vorbis_comment_clear(vc);
238   return(OV_EBADHEADER);
239 }
240
241 /* all of the real encoding details are here.  The modes, books,
242    everything */
243 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
244   codec_setup_info     *ci=vi->codec_setup;
245   int i;
246   if(!ci)return(OV_EFAULT);
247
248   /* codebooks */
249   ci->books=oggpack_read(opb,8)+1;
250   /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
251   for(i=0;i<ci->books;i++){
252     ci->book_param[i]=_ogg_calloc(1,sizeof(*ci->book_param[i]));
253     if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
254   }
255
256   /* time backend settings */
257   ci->times=oggpack_read(opb,6)+1;
258   /*ci->time_type=_ogg_malloc(ci->times*sizeof(*ci->time_type));*/
259   /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
260   for(i=0;i<ci->times;i++){
261     ci->time_type[i]=oggpack_read(opb,16);
262     if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
263     ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
264     if(!ci->time_param[i])goto err_out;
265   }
266
267   /* floor backend settings */
268   ci->floors=oggpack_read(opb,6)+1;
269   /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
270   /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
271   for(i=0;i<ci->floors;i++){
272     ci->floor_type[i]=oggpack_read(opb,16);
273     if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
274     ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
275     if(!ci->floor_param[i])goto err_out;
276   }
277
278   /* residue backend settings */
279   ci->residues=oggpack_read(opb,6)+1;
280   /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
281   /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
282   for(i=0;i<ci->residues;i++){
283     ci->residue_type[i]=oggpack_read(opb,16);
284     if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
285     ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
286     if(!ci->residue_param[i])goto err_out;
287   }
288
289   /* map backend settings */
290   ci->maps=oggpack_read(opb,6)+1;
291   /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
292   /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
293   for(i=0;i<ci->maps;i++){
294     ci->map_type[i]=oggpack_read(opb,16);
295     if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
296     ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
297     if(!ci->map_param[i])goto err_out;
298   }
299   
300   /* mode settings */
301   ci->modes=oggpack_read(opb,6)+1;
302   /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
303   for(i=0;i<ci->modes;i++){
304     ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i]));
305     ci->mode_param[i]->blockflag=oggpack_read(opb,1);
306     ci->mode_param[i]->windowtype=oggpack_read(opb,16);
307     ci->mode_param[i]->transformtype=oggpack_read(opb,16);
308     ci->mode_param[i]->mapping=oggpack_read(opb,8);
309
310     if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
311     if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
312     if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
313   }
314   
315   if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
316
317   return(0);
318  err_out:
319   vorbis_info_clear(vi);
320   return(OV_EBADHEADER);
321 }
322
323 /* The Vorbis header is in three packets; the initial small packet in
324    the first page that identifies basic parameters, a second packet
325    with bitstream comments and a third packet that holds the
326    codebook. */
327
328 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
329   oggpack_buffer opb;
330   
331   if(op){
332     oggpack_readinit(&opb,op->packet,op->bytes);
333
334     /* Which of the three types of header is this? */
335     /* Also verify header-ness, vorbis */
336     {
337       char buffer[6];
338       int packtype=oggpack_read(&opb,8);
339       memset(buffer,0,6);
340       _v_readstring(&opb,buffer,6);
341       if(memcmp(buffer,"vorbis",6)){
342         /* not a vorbis header */
343         return(OV_ENOTVORBIS);
344       }
345       switch(packtype){
346       case 0x01: /* least significant *bit* is read first */
347         if(!op->b_o_s){
348           /* Not the initial packet */
349           return(OV_EBADHEADER);
350         }
351         if(vi->rate!=0){
352           /* previously initialized info header */
353           return(OV_EBADHEADER);
354         }
355
356         return(_vorbis_unpack_info(vi,&opb));
357
358       case 0x03: /* least significant *bit* is read first */
359         if(vi->rate==0){
360           /* um... we didn't get the initial header */
361           return(OV_EBADHEADER);
362         }
363
364         return(_vorbis_unpack_comment(vc,&opb));
365
366       case 0x05: /* least significant *bit* is read first */
367         if(vi->rate==0 || vc->vendor==NULL){
368           /* um... we didn;t get the initial header or comments yet */
369           return(OV_EBADHEADER);
370         }
371
372         return(_vorbis_unpack_books(vi,&opb));
373
374       default:
375         /* Not a valid vorbis header type */
376         return(OV_EBADHEADER);
377         break;
378       }
379     }
380   }
381   return(OV_EBADHEADER);
382 }
383
384 /* pack side **********************************************************/
385
386 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
387   codec_setup_info     *ci=vi->codec_setup;
388   if(!ci)return(OV_EFAULT);
389
390   /* preamble */  
391   oggpack_write(opb,0x01,8);
392   _v_writestring(opb,"vorbis", 6);
393
394   /* basic information about the stream */
395   oggpack_write(opb,0x00,32);
396   oggpack_write(opb,vi->channels,8);
397   oggpack_write(opb,vi->rate,32);
398
399   oggpack_write(opb,vi->bitrate_upper,32);
400   oggpack_write(opb,vi->bitrate_nominal,32);
401   oggpack_write(opb,vi->bitrate_lower,32);
402
403   oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
404   oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
405   oggpack_write(opb,1,1);
406
407   return(0);
408 }
409
410 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
411   char temp[]="Xiphophorus libVorbis I 20010910";
412   int bytes = strlen(temp);
413
414   /* preamble */  
415   oggpack_write(opb,0x03,8);
416   _v_writestring(opb,"vorbis", 6);
417
418   /* vendor */
419   oggpack_write(opb,bytes,32);
420   _v_writestring(opb,temp, bytes);
421   
422   /* comments */
423
424   oggpack_write(opb,vc->comments,32);
425   if(vc->comments){
426     int i;
427     for(i=0;i<vc->comments;i++){
428       if(vc->user_comments[i]){
429         oggpack_write(opb,vc->comment_lengths[i],32);
430         _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]);
431       }else{
432         oggpack_write(opb,0,32);
433       }
434     }
435   }
436   oggpack_write(opb,1,1);
437
438   return(0);
439 }
440  
441 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
442   codec_setup_info     *ci=vi->codec_setup;
443   int i;
444   if(!ci)return(OV_EFAULT);
445
446   oggpack_write(opb,0x05,8);
447   _v_writestring(opb,"vorbis", 6);
448
449   /* books */
450   oggpack_write(opb,ci->books-1,8);
451   for(i=0;i<ci->books;i++)
452     if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
453
454   /* times */
455   oggpack_write(opb,ci->times-1,6);
456   for(i=0;i<ci->times;i++){
457     oggpack_write(opb,ci->time_type[i],16);
458     _time_P[ci->time_type[i]]->pack(ci->time_param[i],opb);
459   }
460
461   /* floors */
462   oggpack_write(opb,ci->floors-1,6);
463   for(i=0;i<ci->floors;i++){
464     oggpack_write(opb,ci->floor_type[i],16);
465     _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
466   }
467
468   /* residues */
469   oggpack_write(opb,ci->residues-1,6);
470   for(i=0;i<ci->residues;i++){
471     oggpack_write(opb,ci->residue_type[i],16);
472     _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
473   }
474
475   /* maps */
476   oggpack_write(opb,ci->maps-1,6);
477   for(i=0;i<ci->maps;i++){
478     oggpack_write(opb,ci->map_type[i],16);
479     _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
480   }
481
482   /* modes */
483   oggpack_write(opb,ci->modes-1,6);
484   for(i=0;i<ci->modes;i++){
485     oggpack_write(opb,ci->mode_param[i]->blockflag,1);
486     oggpack_write(opb,ci->mode_param[i]->windowtype,16);
487     oggpack_write(opb,ci->mode_param[i]->transformtype,16);
488     oggpack_write(opb,ci->mode_param[i]->mapping,8);
489   }
490   oggpack_write(opb,1,1);
491
492   return(0);
493 err_out:
494   return(-1);
495
496
497 int vorbis_commentheader_out(vorbis_comment *vc,
498                                       ogg_packet *op){
499
500   oggpack_buffer opb;
501
502   oggpack_writeinit(&opb);
503   if(_vorbis_pack_comment(&opb,vc)) return OV_EIMPL;
504
505   op->packet = _ogg_malloc(oggpack_bytes(&opb));
506   memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
507
508   op->bytes=oggpack_bytes(&opb);
509   op->b_o_s=0;
510   op->e_o_s=0;
511   op->granulepos=0;
512
513   return 0;
514 }
515
516 int vorbis_analysis_headerout(vorbis_dsp_state *v,
517                               vorbis_comment *vc,
518                               ogg_packet *op,
519                               ogg_packet *op_comm,
520                               ogg_packet *op_code){
521   int ret=OV_EIMPL;
522   vorbis_info *vi=v->vi;
523   oggpack_buffer opb;
524   backend_lookup_state *b=v->backend_state;
525
526   if(!b){
527     ret=OV_EFAULT;
528     goto err_out;
529   }
530
531   /* first header packet **********************************************/
532
533   oggpack_writeinit(&opb);
534   if(_vorbis_pack_info(&opb,vi))goto err_out;
535
536   /* build the packet */
537   if(b->header)_ogg_free(b->header);
538   b->header=_ogg_malloc(oggpack_bytes(&opb));
539   memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
540   op->packet=b->header;
541   op->bytes=oggpack_bytes(&opb);
542   op->b_o_s=1;
543   op->e_o_s=0;
544   op->granulepos=0;
545
546   /* second header packet (comments) **********************************/
547
548   oggpack_reset(&opb);
549   if(_vorbis_pack_comment(&opb,vc))goto err_out;
550
551   if(b->header1)_ogg_free(b->header1);
552   b->header1=_ogg_malloc(oggpack_bytes(&opb));
553   memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
554   op_comm->packet=b->header1;
555   op_comm->bytes=oggpack_bytes(&opb);
556   op_comm->b_o_s=0;
557   op_comm->e_o_s=0;
558   op_comm->granulepos=0;
559
560   /* third header packet (modes/codebooks) ****************************/
561
562   oggpack_reset(&opb);
563   if(_vorbis_pack_books(&opb,vi))goto err_out;
564
565   if(b->header2)_ogg_free(b->header2);
566   b->header2=_ogg_malloc(oggpack_bytes(&opb));
567   memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
568   op_code->packet=b->header2;
569   op_code->bytes=oggpack_bytes(&opb);
570   op_code->b_o_s=0;
571   op_code->e_o_s=0;
572   op_code->granulepos=0;
573
574   oggpack_writeclear(&opb);
575   return(0);
576  err_out:
577   oggpack_writeclear(&opb);
578   memset(op,0,sizeof(*op));
579   memset(op_comm,0,sizeof(*op_comm));
580   memset(op_code,0,sizeof(*op_code));
581
582   if(b->header)_ogg_free(b->header);
583   if(b->header1)_ogg_free(b->header1);
584   if(b->header2)_ogg_free(b->header2);
585   b->header=NULL;
586   b->header1=NULL;
587   b->header2=NULL;
588   return(ret);
589 }
590