Coverage Report

Created: 2025-10-10 07:02

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
21.5k
static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
35
146k
  while(bytes--){
36
125k
    *buf++=oggpack_read(o,8);
37
125k
  }
38
21.5k
}
39
40
4.27k
void vorbis_comment_init(vorbis_comment *vc){
41
4.27k
  memset(vc,0,sizeof(*vc));
42
4.27k
}
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
5.00k
void vorbis_comment_clear(vorbis_comment *vc){
93
5.00k
  if(vc){
94
5.00k
    long i;
95
5.00k
    if(vc->user_comments){
96
6.05k
      for(i=0;i<vc->comments;i++)
97
2.09k
        if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
98
3.96k
      _ogg_free(vc->user_comments);
99
3.96k
    }
100
5.00k
    if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
101
5.00k
    if(vc->vendor)_ogg_free(vc->vendor);
102
5.00k
    memset(vc,0,sizeof(*vc));
103
5.00k
  }
104
5.00k
}
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
4.27k
void vorbis_info_init(vorbis_info *vi){
115
4.27k
  memset(vi,0,sizeof(*vi));
116
4.27k
  vi->codec_setup=(codec_setup_info *)_ogg_calloc(1,sizeof(codec_setup_info));
117
4.27k
}
118
119
5.23k
void vorbis_info_clear(vorbis_info *vi){
120
5.23k
  codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
121
5.23k
  int i;
122
123
5.23k
  if(ci){
124
125
10.2k
    for(i=0;i<ci->modes;i++)
126
5.93k
      if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
127
128
8.49k
    for(i=0;i<ci->maps;i++) /* unpack does the range checking */
129
4.21k
      if(ci->map_param[i])
130
3.80k
  _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
131
132
9.20k
    for(i=0;i<ci->floors;i++) /* unpack does the range checking */
133
4.93k
      if(ci->floor_param[i])
134
3.81k
  _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
135
    
136
8.90k
    for(i=0;i<ci->residues;i++) /* unpack does the range checking */
137
4.62k
      if(ci->residue_param[i])
138
4.15k
  _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
139
140
19.6k
    for(i=0;i<ci->books;i++){
141
15.3k
      if(ci->book_param[i]){
142
  /* knows if the book was not alloced */
143
1.04k
  vorbis_staticbook_destroy(ci->book_param[i]);
144
1.04k
      }
145
15.3k
      if(ci->fullbooks)
146
6.29k
  vorbis_book_clear(ci->fullbooks+i);
147
15.3k
    }
148
4.27k
    if(ci->fullbooks)
149
4.27k
  _ogg_free(ci->fullbooks);
150
    
151
4.27k
    _ogg_free(ci);
152
4.27k
  }
153
154
5.23k
  memset(vi,0,sizeof(*vi));
155
5.23k
}
156
157
/* Header packing/unpacking ********************************************/
158
159
4.12k
static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
160
4.12k
  codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
161
4.12k
  if(!ci)return(OV_EFAULT);
162
163
4.12k
  vi->version=oggpack_read(opb,32);
164
4.12k
  if(vi->version!=0)return(OV_EVERSION);
165
166
4.11k
  vi->channels=oggpack_read(opb,8);
167
4.11k
  vi->rate=oggpack_read(opb,32);
168
169
4.11k
  vi->bitrate_upper=oggpack_read(opb,32);
170
4.11k
  vi->bitrate_nominal=oggpack_read(opb,32);
171
4.11k
  vi->bitrate_lower=oggpack_read(opb,32);
172
173
4.11k
  ci->blocksizes[0]=1<<oggpack_read(opb,4);
174
4.11k
  ci->blocksizes[1]=1<<oggpack_read(opb,4);
175
  
176
4.11k
  if(vi->rate<1)goto err_out;
177
4.10k
  if(vi->channels<1)goto err_out;
178
4.10k
  if(ci->blocksizes[0]<64)goto err_out; 
179
4.10k
  if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
180
4.10k
  if(ci->blocksizes[1]>8192)goto err_out;
181
  
182
4.10k
  if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
183
184
4.10k
  return(0);
185
11
 err_out:
186
11
  vorbis_info_clear(vi);
187
11
  return(OV_EBADHEADER);
188
4.10k
}
189
190
3.99k
static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
191
3.99k
  int i;
192
3.99k
  int vendorlen;
193
3.99k
  vendorlen=oggpack_read(opb,32);
194
3.99k
  if(vendorlen<0)goto err_out;
195
3.98k
  if(vendorlen>opb->storage-oggpack_bytes(opb))goto err_out;
196
3.98k
  vc->vendor=(char *)_ogg_calloc(vendorlen+1,1);
197
3.98k
  if(vc->vendor==NULL)goto err_out;
198
3.98k
  _v_readstring(opb,vc->vendor,vendorlen);
199
3.98k
  i=oggpack_read(opb,32);
200
3.98k
  if(i<0||i>=INT_MAX||i>(opb->storage-oggpack_bytes(opb))>>2)goto err_out;
201
3.96k
  vc->user_comments=(char **)_ogg_calloc(i+1,sizeof(*vc->user_comments));
202
3.96k
  vc->comment_lengths=(int *)_ogg_calloc(i+1, sizeof(*vc->comment_lengths));
203
3.96k
  if(vc->user_comments==NULL||vc->comment_lengths==NULL)goto err_out;
204
3.96k
  vc->comments=i;
205
206
5.06k
  for(i=0;i<vc->comments;i++){
207
1.12k
    int len=oggpack_read(opb,32);
208
1.12k
    if(len<0||len>opb->storage-oggpack_bytes(opb))goto err_out;
209
1.10k
    vc->comment_lengths[i]=len;
210
1.10k
    vc->user_comments[i]=(char *)_ogg_calloc(len+1,1);
211
1.10k
    if(vc->user_comments[i]==NULL){
212
0
      vc->comments=i;
213
0
      goto err_out;
214
0
    }
215
1.10k
    _v_readstring(opb,vc->user_comments[i],len);
216
1.10k
  }
217
3.93k
  if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
218
219
3.93k
  return(0);
220
58
 err_out:
221
58
  vorbis_comment_clear(vc);
222
58
  return(OV_EBADHEADER);
223
3.93k
}
224
225
/* all of the real encoding details are here.  The modes, books,
226
   everything */
227
3.91k
static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
228
3.91k
  codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
229
3.91k
  int i;
230
231
  /* codebooks */
232
3.91k
  ci->books=oggpack_read(opb,8)+1;
233
3.91k
  if(ci->books<=0)goto err_out;
234
11.2k
  for(i=0;i<ci->books;i++){
235
7.42k
    ci->book_param[i]=vorbis_staticbook_unpack(opb);
236
7.42k
    if(!ci->book_param[i])goto err_out;
237
7.42k
  }
238
239
  /* time backend settings */
240
3.83k
  ci->times=oggpack_read(opb,6)+1;
241
3.83k
  if(ci->times<=0)goto err_out;
242
7.73k
  for(i=0;i<ci->times;i++){
243
3.91k
    ci->time_type[i]=oggpack_read(opb,16);
244
3.91k
    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
3.91k
  }
249
250
  /* floor backend settings */
251
3.81k
  ci->floors=oggpack_read(opb,6)+1;
252
3.81k
  if(ci->floors<=0)goto err_out;
253
7.63k
  for(i=0;i<ci->floors;i++){
254
3.88k
    ci->floor_type[i]=oggpack_read(opb,16);
255
3.88k
    if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
256
3.87k
    ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
257
3.87k
    if(!ci->floor_param[i])goto err_out;
258
3.87k
  }
259
260
  /* residue backend settings */
261
3.74k
  ci->residues=oggpack_read(opb,6)+1;
262
3.74k
  if(ci->residues<=0)goto err_out;
263
7.89k
  for(i=0;i<ci->residues;i++){
264
4.19k
    ci->residue_type[i]=oggpack_read(opb,16);
265
4.19k
    if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
266
4.19k
    ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
267
4.19k
    if(!ci->residue_param[i])goto err_out;
268
4.19k
  }
269
270
  /* map backend settings */
271
3.69k
  ci->maps=oggpack_read(opb,6)+1;
272
3.69k
  if(ci->maps<=0)goto err_out;
273
7.50k
  for(i=0;i<ci->maps;i++){
274
3.84k
    ci->map_type[i]=oggpack_read(opb,16);
275
3.84k
    if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
276
3.84k
    ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
277
3.84k
    if(!ci->map_param[i])goto err_out;
278
3.84k
  }
279
  
280
  /* mode settings */
281
3.65k
  ci->modes=oggpack_read(opb,6)+1;
282
3.65k
  if(ci->modes<=0)goto err_out;
283
9.43k
  for(i=0;i<ci->modes;i++){
284
5.79k
    ci->mode_param[i]=(vorbis_info_mode *)_ogg_calloc(1,sizeof(*ci->mode_param[i]));
285
5.79k
    ci->mode_param[i]->blockflag=oggpack_read(opb,1);
286
5.79k
    ci->mode_param[i]->windowtype=oggpack_read(opb,16);
287
5.79k
    ci->mode_param[i]->transformtype=oggpack_read(opb,16);
288
5.79k
    ci->mode_param[i]->mapping=oggpack_read(opb,8);
289
290
5.79k
    if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
291
5.79k
    if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
292
5.78k
    if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
293
5.78k
    if(ci->mode_param[i]->mapping<0)goto err_out;
294
5.78k
  }
295
  
296
3.63k
  if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
297
298
3.63k
  return(0);
299
280
 err_out:
300
280
  vorbis_info_clear(vi);
301
280
  return(OV_EBADHEADER);
302
3.63k
}
303
304
/* Is this packet a vorbis ID header? */
305
5.37k
int vorbis_synthesis_idheader(ogg_packet *op){
306
5.37k
  oggpack_buffer opb;
307
5.37k
  char buffer[6];
308
309
5.37k
  if(op){
310
5.37k
    oggpack_readinit(&opb,op->packet,op->bytes);
311
312
5.37k
    if(!op->b_o_s)
313
181
      return(0); /* Not the initial packet */
314
315
5.19k
    if(oggpack_read(&opb,8) != 1)
316
760
      return 0; /* not an ID header */
317
318
4.43k
    memset(buffer,0,6);
319
4.43k
    _v_readstring(&opb,buffer,6);
320
4.43k
    if(memcmp(buffer,"vorbis",6))
321
311
      return 0; /* not vorbis */
322
323
4.12k
    return 1;
324
4.43k
  }
325
326
0
  return 0;
327
5.37k
}
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
12.0k
int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
335
12.0k
  oggpack_buffer opb;
336
  
337
12.0k
  if(op){
338
12.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
12.0k
    {
343
12.0k
      char buffer[6];
344
12.0k
      int packtype=oggpack_read(&opb,8);
345
12.0k
      memset(buffer,0,6);
346
12.0k
      _v_readstring(&opb,buffer,6);
347
12.0k
      if(memcmp(buffer,"vorbis",6)){
348
  /* not a vorbis header */
349
5
  return(OV_ENOTVORBIS);
350
5
      }
351
12.0k
      switch(packtype){
352
4.13k
      case 0x01: /* least significant *bit* is read first */
353
4.13k
  if(!op->b_o_s){
354
    /* Not the initial packet */
355
1
    return(OV_EBADHEADER);
356
1
  }
357
4.13k
  if(vi->rate!=0){
358
    /* previously initialized info header */
359
11
    return(OV_EBADHEADER);
360
11
  }
361
362
4.12k
  return(_vorbis_unpack_info(vi,&opb));
363
364
3.99k
      case 0x03: /* least significant *bit* is read first */
365
3.99k
  if(vi->rate==0){
366
    /* um... we didn't get the initial header */
367
0
    return(OV_EBADHEADER);
368
0
  }
369
3.99k
        if(vc->vendor!=NULL){
370
          /* previously initialized comment header */
371
1
          return(OV_EBADHEADER);
372
1
        }
373
374
3.99k
  return(_vorbis_unpack_comment(vc,&opb));
375
376
3.91k
      case 0x05: /* least significant *bit* is read first */
377
3.91k
  if(vi->rate==0 || vc->vendor==NULL){
378
    /* um... we didn;t get the initial header or comments yet */
379
6
    return(OV_EBADHEADER);
380
6
  }
381
3.91k
        if(vi->codec_setup==NULL){
382
          /* improperly initialized vorbis_info */
383
0
          return(OV_EFAULT);
384
0
        }
385
3.91k
        if(((codec_setup_info *)vi->codec_setup)->books>0){
386
          /* previously initialized setup header */
387
0
          return(OV_EBADHEADER);
388
0
        }
389
390
3.91k
  return(_vorbis_unpack_books(vi,&opb));
391
392
1
      default:
393
  /* Not a valid vorbis header type */
394
1
  return(OV_EBADHEADER);
395
0
  break;
396
12.0k
      }
397
12.0k
    }
398
12.0k
  }
399
0
  return(OV_EBADHEADER);
400
12.0k
}
401