Coverage Report

Created: 2026-04-12 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tremor/info.c
Line
Count
Source
1
/********************************************************************
2
 *                                                                  *
3
 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE.   *
4
 *                                                                  *
5
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
6
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
7
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
8
 *                                                                  *
9
 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003    *
10
 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
11
 *                                                                  *
12
 ********************************************************************
13
14
 function: maintain the info structure, info <-> header packets
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 <limits.h>
25
#include <ogg/ogg.h>
26
#include "ivorbiscodec.h"
27
#include "codec_internal.h"
28
#include "codebook.h"
29
#include "registry.h"
30
#include "window.h"
31
#include "misc.h"
32
33
/* helpers */
34
37.1k
static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
35
254k
  while(bytes--){
36
217k
    *buf++=oggpack_read(o,8);
37
217k
  }
38
37.1k
}
39
40
7.65k
void vorbis_comment_init(vorbis_comment *vc){
41
7.65k
  memset(vc,0,sizeof(*vc));
42
7.65k
}
43
44
/* This is more or less the same as strncasecmp - but that doesn't exist
45
 * everywhere, and this is a fairly trivial function, so we include it */
46
0
static int tagcompare(const char *s1, const char *s2, int n){
47
0
  int c=0;
48
0
  while(c < n){
49
0
    if(toupper(s1[c]) != toupper(s2[c]))
50
0
      return !0;
51
0
    c++;
52
0
  }
53
0
  return 0;
54
0
}
55
56
0
char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){
57
0
  long i;
58
0
  int found = 0;
59
0
  int taglen = strlen(tag)+1; /* +1 for the = we append */
60
0
  char *fulltag = (char *)alloca(taglen+ 1);
61
62
0
  strcpy(fulltag, tag);
63
0
  strcat(fulltag, "=");
64
  
65
0
  for(i=0;i<vc->comments;i++){
66
0
    if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
67
0
      if(count == found)
68
  /* We return a pointer to the data, not a copy */
69
0
        return vc->user_comments[i] + taglen;
70
0
      else
71
0
  found++;
72
0
    }
73
0
  }
74
0
  return NULL; /* didn't find anything */
75
0
}
76
77
0
int vorbis_comment_query_count(vorbis_comment *vc, char *tag){
78
0
  int i,count=0;
79
0
  int taglen = strlen(tag)+1; /* +1 for the = we append */
80
0
  char *fulltag = (char *)alloca(taglen+1);
81
0
  strcpy(fulltag,tag);
82
0
  strcat(fulltag, "=");
83
84
0
  for(i=0;i<vc->comments;i++){
85
0
    if(!tagcompare(vc->user_comments[i], fulltag, taglen))
86
0
      count++;
87
0
  }
88
89
0
  return count;
90
0
}
91
92
9.72k
void vorbis_comment_clear(vorbis_comment *vc){
93
9.72k
  if(vc){
94
9.72k
    long i;
95
9.72k
    if(vc->user_comments){
96
9.42k
      for(i=0;i<vc->comments;i++)
97
2.58k
        if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
98
6.84k
      _ogg_free(vc->user_comments);
99
6.84k
    }
100
9.72k
    if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
101
9.72k
    if(vc->vendor)_ogg_free(vc->vendor);
102
9.72k
    memset(vc,0,sizeof(*vc));
103
9.72k
  }
104
9.72k
}
105
106
/* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
107
   They may be equal, but short will never ge greater than long */
108
0
int vorbis_info_blocksize(vorbis_info *vi,int zo){
109
0
  codec_setup_info *ci = (codec_setup_info *)vi->codec_setup;
110
0
  return ci ? ci->blocksizes[zo] : -1;
111
0
}
112
113
/* used by synthesis, which has a full, alloced vi */
114
7.65k
void vorbis_info_init(vorbis_info *vi){
115
7.65k
  memset(vi,0,sizeof(*vi));
116
7.65k
  vi->codec_setup=(codec_setup_info *)_ogg_calloc(1,sizeof(codec_setup_info));
117
7.65k
}
118
119
10.4k
void vorbis_info_clear(vorbis_info *vi){
120
10.4k
  codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
121
10.4k
  int i;
122
123
10.4k
  if(ci){
124
125
16.1k
    for(i=0;i<ci->modes;i++)
126
8.47k
      if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
127
128
15.0k
    for(i=0;i<ci->maps;i++) /* unpack does the range checking */
129
7.37k
      if(ci->map_param[i])
130
6.19k
  _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
131
132
16.2k
    for(i=0;i<ci->floors;i++) /* unpack does the range checking */
133
8.64k
      if(ci->floor_param[i])
134
6.32k
  _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
135
    
136
16.0k
    for(i=0;i<ci->residues;i++) /* unpack does the range checking */
137
8.42k
      if(ci->residue_param[i])
138
7.31k
  _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
139
140
45.6k
    for(i=0;i<ci->books;i++){
141
37.9k
      if(ci->book_param[i]){
142
  /* knows if the book was not alloced */
143
1.73k
  vorbis_staticbook_destroy(ci->book_param[i]);
144
1.73k
      }
145
37.9k
      if(ci->fullbooks)
146
10.2k
  vorbis_book_clear(ci->fullbooks+i);
147
37.9k
    }
148
7.65k
    if(ci->fullbooks)
149
7.65k
  _ogg_free(ci->fullbooks);
150
    
151
7.65k
    _ogg_free(ci);
152
7.65k
  }
153
154
10.4k
  memset(vi,0,sizeof(*vi));
155
10.4k
}
156
157
/* Header packing/unpacking ********************************************/
158
159
7.34k
static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
160
7.34k
  codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
161
7.34k
  if(!ci)return(OV_EFAULT);
162
163
7.34k
  vi->version=oggpack_read(opb,32);
164
7.34k
  if(vi->version!=0)return(OV_EVERSION);
165
166
7.30k
  vi->channels=oggpack_read(opb,8);
167
7.30k
  vi->rate=oggpack_read(opb,32);
168
169
7.30k
  vi->bitrate_upper=oggpack_read(opb,32);
170
7.30k
  vi->bitrate_nominal=oggpack_read(opb,32);
171
7.30k
  vi->bitrate_lower=oggpack_read(opb,32);
172
173
7.30k
  ci->blocksizes[0]=1<<oggpack_read(opb,4);
174
7.30k
  ci->blocksizes[1]=1<<oggpack_read(opb,4);
175
  
176
7.30k
  if(vi->rate<1)goto err_out;
177
7.30k
  if(vi->channels<1)goto err_out;
178
7.29k
  if(ci->blocksizes[0]<64)goto err_out; 
179
7.26k
  if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
180
7.25k
  if(ci->blocksizes[1]>8192)goto err_out;
181
  
182
7.25k
  if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
183
184
7.24k
  return(0);
185
60
 err_out:
186
60
  vorbis_info_clear(vi);
187
60
  return(OV_EBADHEADER);
188
7.25k
}
189
190
6.94k
static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
191
6.94k
  int i;
192
6.94k
  int vendorlen;
193
6.94k
  vendorlen=oggpack_read(opb,32);
194
6.94k
  if(vendorlen<0)goto err_out;
195
6.93k
  if(vendorlen>opb->storage-oggpack_bytes(opb))goto err_out;
196
6.90k
  vc->vendor=(char *)_ogg_calloc(vendorlen+1,1);
197
6.90k
  if(vc->vendor==NULL)goto err_out;
198
6.90k
  _v_readstring(opb,vc->vendor,vendorlen);
199
6.90k
  i=oggpack_read(opb,32);
200
6.90k
  if(i<0||i>=INT_MAX||i>(opb->storage-oggpack_bytes(opb))>>2)goto err_out;
201
6.84k
  vc->user_comments=(char **)_ogg_calloc(i+1,sizeof(*vc->user_comments));
202
6.84k
  vc->comment_lengths=(int *)_ogg_calloc(i+1, sizeof(*vc->comment_lengths));
203
6.84k
  if(vc->user_comments==NULL||vc->comment_lengths==NULL)goto err_out;
204
6.84k
  vc->comments=i;
205
206
7.83k
  for(i=0;i<vc->comments;i++){
207
1.06k
    int len=oggpack_read(opb,32);
208
1.06k
    if(len<0||len>opb->storage-oggpack_bytes(opb))goto err_out;
209
990
    vc->comment_lengths[i]=len;
210
990
    vc->user_comments[i]=(char *)_ogg_calloc(len+1,1);
211
990
    if(vc->user_comments[i]==NULL){
212
0
      vc->comments=i;
213
0
      goto err_out;
214
0
    }
215
990
    _v_readstring(opb,vc->user_comments[i],len);
216
990
  }
217
6.77k
  if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
218
219
6.76k
  return(0);
220
185
 err_out:
221
185
  vorbis_comment_clear(vc);
222
185
  return(OV_EBADHEADER);
223
6.77k
}
224
225
/* all of the real encoding details are here.  The modes, books,
226
   everything */
227
6.73k
static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
228
6.73k
  codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
229
6.73k
  int i;
230
231
  /* codebooks */
232
6.73k
  ci->books=oggpack_read(opb,8)+1;
233
6.73k
  if(ci->books<=0)goto err_out;
234
18.7k
  for(i=0;i<ci->books;i++){
235
12.3k
    ci->book_param[i]=vorbis_staticbook_unpack(opb);
236
12.3k
    if(!ci->book_param[i])goto err_out;
237
12.3k
  }
238
239
  /* time backend settings */
240
6.45k
  ci->times=oggpack_read(opb,6)+1;
241
6.45k
  if(ci->times<=0)goto err_out;
242
12.9k
  for(i=0;i<ci->times;i++){
243
6.53k
    ci->time_type[i]=oggpack_read(opb,16);
244
6.53k
    if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
245
    /* ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
246
       Vorbis I has no time backend */
247
    /*if(!ci->time_param[i])goto err_out;*/
248
6.53k
  }
249
250
  /* floor backend settings */
251
6.36k
  ci->floors=oggpack_read(opb,6)+1;
252
6.36k
  if(ci->floors<=0)goto err_out;
253
12.6k
  for(i=0;i<ci->floors;i++){
254
6.51k
    ci->floor_type[i]=oggpack_read(opb,16);
255
6.51k
    if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
256
6.48k
    ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
257
6.48k
    if(!ci->floor_param[i])goto err_out;
258
6.48k
  }
259
260
  /* residue backend settings */
261
6.17k
  ci->residues=oggpack_read(opb,6)+1;
262
6.17k
  if(ci->residues<=0)goto err_out;
263
13.4k
  for(i=0;i<ci->residues;i++){
264
7.44k
    ci->residue_type[i]=oggpack_read(opb,16);
265
7.44k
    if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
266
7.41k
    ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
267
7.41k
    if(!ci->residue_param[i])goto err_out;
268
7.41k
  }
269
270
  /* map backend settings */
271
6.04k
  ci->maps=oggpack_read(opb,6)+1;
272
6.04k
  if(ci->maps<=0)goto err_out;
273
12.2k
  for(i=0;i<ci->maps;i++){
274
6.31k
    ci->map_type[i]=oggpack_read(opb,16);
275
6.31k
    if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
276
6.30k
    ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
277
6.30k
    if(!ci->map_param[i])goto err_out;
278
6.30k
  }
279
  
280
  /* mode settings */
281
5.91k
  ci->modes=oggpack_read(opb,6)+1;
282
5.91k
  if(ci->modes<=0)goto err_out;
283
13.4k
  for(i=0;i<ci->modes;i++){
284
7.52k
    ci->mode_param[i]=(vorbis_info_mode *)_ogg_calloc(1,sizeof(*ci->mode_param[i]));
285
7.52k
    ci->mode_param[i]->blockflag=oggpack_read(opb,1);
286
7.52k
    ci->mode_param[i]->windowtype=oggpack_read(opb,16);
287
7.52k
    ci->mode_param[i]->transformtype=oggpack_read(opb,16);
288
7.52k
    ci->mode_param[i]->mapping=oggpack_read(opb,8);
289
290
7.52k
    if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
291
7.50k
    if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
292
7.50k
    if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
293
7.49k
    if(ci->mode_param[i]->mapping<0)goto err_out;
294
7.49k
  }
295
  
296
5.87k
  if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
297
298
5.86k
  return(0);
299
864
 err_out:
300
864
  vorbis_info_clear(vi);
301
864
  return(OV_EBADHEADER);
302
5.87k
}
303
304
/* Is this packet a vorbis ID header? */
305
9.67k
int vorbis_synthesis_idheader(ogg_packet *op){
306
9.67k
  oggpack_buffer opb;
307
9.67k
  char buffer[6];
308
309
9.67k
  if(op){
310
9.67k
    oggpack_readinit(&opb,op->packet,op->bytes);
311
312
9.67k
    if(!op->b_o_s)
313
308
      return(0); /* Not the initial packet */
314
315
9.36k
    if(oggpack_read(&opb,8) != 1)
316
1.15k
      return 0; /* not an ID header */
317
318
8.21k
    memset(buffer,0,6);
319
8.21k
    _v_readstring(&opb,buffer,6);
320
8.21k
    if(memcmp(buffer,"vorbis",6))
321
866
      return 0; /* not vorbis */
322
323
7.34k
    return 1;
324
8.21k
  }
325
326
0
  return 0;
327
9.67k
}
328
329
/* The Vorbis header is in three packets; the initial small packet in
330
   the first page that identifies basic parameters, a second packet
331
   with bitstream comments and a third packet that holds the
332
   codebook. */
333
334
21.0k
int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
335
21.0k
  oggpack_buffer opb;
336
  
337
21.0k
  if(op){
338
21.0k
    oggpack_readinit(&opb,op->packet,op->bytes);
339
340
    /* Which of the three types of header is this? */
341
    /* Also verify header-ness, vorbis */
342
21.0k
    {
343
21.0k
      char buffer[6];
344
21.0k
      int packtype=oggpack_read(&opb,8);
345
21.0k
      memset(buffer,0,6);
346
21.0k
      _v_readstring(&opb,buffer,6);
347
21.0k
      if(memcmp(buffer,"vorbis",6)){
348
  /* not a vorbis header */
349
23
  return(OV_ENOTVORBIS);
350
23
      }
351
21.0k
      switch(packtype){
352
7.36k
      case 0x01: /* least significant *bit* is read first */
353
7.36k
  if(!op->b_o_s){
354
    /* Not the initial packet */
355
1
    return(OV_EBADHEADER);
356
1
  }
357
7.36k
  if(vi->rate!=0){
358
    /* previously initialized info header */
359
17
    return(OV_EBADHEADER);
360
17
  }
361
362
7.34k
  return(_vorbis_unpack_info(vi,&opb));
363
364
6.94k
      case 0x03: /* least significant *bit* is read first */
365
6.94k
  if(vi->rate==0){
366
    /* um... we didn't get the initial header */
367
0
    return(OV_EBADHEADER);
368
0
  }
369
6.94k
        if(vc->vendor!=NULL){
370
          /* previously initialized comment header */
371
1
          return(OV_EBADHEADER);
372
1
        }
373
374
6.94k
  return(_vorbis_unpack_comment(vc,&opb));
375
376
6.74k
      case 0x05: /* least significant *bit* is read first */
377
6.74k
  if(vi->rate==0 || vc->vendor==NULL){
378
    /* um... we didn;t get the initial header or comments yet */
379
9
    return(OV_EBADHEADER);
380
9
  }
381
6.73k
        if(vi->codec_setup==NULL){
382
          /* improperly initialized vorbis_info */
383
0
          return(OV_EFAULT);
384
0
        }
385
6.73k
        if(((codec_setup_info *)vi->codec_setup)->books>0){
386
          /* previously initialized setup header */
387
0
          return(OV_EBADHEADER);
388
0
        }
389
390
6.73k
  return(_vorbis_unpack_books(vi,&opb));
391
392
3
      default:
393
  /* Not a valid vorbis header type */
394
3
  return(OV_EBADHEADER);
395
0
  break;
396
21.0k
      }
397
21.0k
    }
398
21.0k
  }
399
0
  return(OV_EBADHEADER);
400
21.0k
}
401