Coverage Report

Created: 2025-08-26 06:13

/src/tremor/info.c
Line
Count
Source (jump to first uncovered line)
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
46.8k
static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
35
319k
  while(bytes--){
36
272k
    *buf++=oggpack_read(o,8);
37
272k
  }
38
46.8k
}
39
40
9.86k
void vorbis_comment_init(vorbis_comment *vc){
41
9.86k
  memset(vc,0,sizeof(*vc));
42
9.86k
}
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
12.6k
void vorbis_comment_clear(vorbis_comment *vc){
93
12.6k
  if(vc){
94
12.6k
    long i;
95
12.6k
    if(vc->user_comments){
96
10.7k
      for(i=0;i<vc->comments;i++)
97
2.07k
        if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
98
8.66k
      _ogg_free(vc->user_comments);
99
8.66k
    }
100
12.6k
    if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
101
12.6k
    if(vc->vendor)_ogg_free(vc->vendor);
102
12.6k
    memset(vc,0,sizeof(*vc));
103
12.6k
  }
104
12.6k
}
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
9.86k
void vorbis_info_init(vorbis_info *vi){
115
9.86k
  memset(vi,0,sizeof(*vi));
116
9.86k
  vi->codec_setup=(codec_setup_info *)_ogg_calloc(1,sizeof(codec_setup_info));
117
9.86k
}
118
119
13.4k
void vorbis_info_clear(vorbis_info *vi){
120
13.4k
  codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
121
13.4k
  int i;
122
123
13.4k
  if(ci){
124
125
20.2k
    for(i=0;i<ci->modes;i++)
126
10.3k
      if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
127
128
18.9k
    for(i=0;i<ci->maps;i++) /* unpack does the range checking */
129
9.07k
      if(ci->map_param[i])
130
7.89k
  _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
131
132
20.1k
    for(i=0;i<ci->floors;i++) /* unpack does the range checking */
133
10.2k
      if(ci->floor_param[i])
134
8.05k
  _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
135
    
136
20.3k
    for(i=0;i<ci->residues;i++) /* unpack does the range checking */
137
10.5k
      if(ci->residue_param[i])
138
9.10k
  _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
139
140
60.7k
    for(i=0;i<ci->books;i++){
141
50.8k
      if(ci->book_param[i]){
142
  /* knows if the book was not alloced */
143
2.33k
  vorbis_staticbook_destroy(ci->book_param[i]);
144
2.33k
      }
145
50.8k
      if(ci->fullbooks)
146
12.6k
  vorbis_book_clear(ci->fullbooks+i);
147
50.8k
    }
148
9.86k
    if(ci->fullbooks)
149
7.51k
  _ogg_free(ci->fullbooks);
150
    
151
9.86k
    _ogg_free(ci);
152
9.86k
  }
153
154
13.4k
  memset(vi,0,sizeof(*vi));
155
13.4k
}
156
157
/* Header packing/unpacking ********************************************/
158
159
9.40k
static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
160
9.40k
  codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
161
9.40k
  if(!ci)return(OV_EFAULT);
162
163
9.40k
  vi->version=oggpack_read(opb,32);
164
9.40k
  if(vi->version!=0)return(OV_EVERSION);
165
166
9.34k
  vi->channels=oggpack_read(opb,8);
167
9.34k
  vi->rate=oggpack_read(opb,32);
168
169
9.34k
  vi->bitrate_upper=oggpack_read(opb,32);
170
9.34k
  vi->bitrate_nominal=oggpack_read(opb,32);
171
9.34k
  vi->bitrate_lower=oggpack_read(opb,32);
172
173
9.34k
  ci->blocksizes[0]=1<<oggpack_read(opb,4);
174
9.34k
  ci->blocksizes[1]=1<<oggpack_read(opb,4);
175
  
176
9.34k
  if(vi->rate<1)goto err_out;
177
9.34k
  if(vi->channels<1)goto err_out;
178
9.32k
  if(ci->blocksizes[0]<64)goto err_out; 
179
9.29k
  if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
180
9.28k
  if(ci->blocksizes[1]>8192)goto err_out;
181
  
182
9.27k
  if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
183
184
9.27k
  return(0);
185
68
 err_out:
186
68
  vorbis_info_clear(vi);
187
68
  return(OV_EBADHEADER);
188
9.27k
}
189
190
8.88k
static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
191
8.88k
  int i;
192
8.88k
  int vendorlen;
193
8.88k
  vendorlen=oggpack_read(opb,32);
194
8.88k
  if(vendorlen<0)goto err_out;
195
8.84k
  if(vendorlen>opb->storage-oggpack_bytes(opb))goto err_out;
196
8.78k
  vc->vendor=(char *)_ogg_calloc(vendorlen+1,1);
197
8.78k
  if(vc->vendor==NULL)goto err_out;
198
8.78k
  _v_readstring(opb,vc->vendor,vendorlen);
199
8.78k
  i=oggpack_read(opb,32);
200
8.78k
  if(i<0||i>=INT_MAX||i>(opb->storage-oggpack_bytes(opb))>>2)goto err_out;
201
8.66k
  vc->user_comments=(char **)_ogg_calloc(i+1,sizeof(*vc->user_comments));
202
8.66k
  vc->comment_lengths=(int *)_ogg_calloc(i+1, sizeof(*vc->comment_lengths));
203
8.66k
  if(vc->user_comments==NULL||vc->comment_lengths==NULL)goto err_out;
204
8.66k
  vc->comments=i;
205
206
9.84k
  for(i=0;i<vc->comments;i++){
207
1.29k
    int len=oggpack_read(opb,32);
208
1.29k
    if(len<0||len>opb->storage-oggpack_bytes(opb))goto err_out;
209
1.17k
    vc->comment_lengths[i]=len;
210
1.17k
    vc->user_comments[i]=(char *)_ogg_calloc(len+1,1);
211
1.17k
    if(vc->user_comments[i]==NULL){
212
0
      vc->comments=i;
213
0
      goto err_out;
214
0
    }
215
1.17k
    _v_readstring(opb,vc->user_comments[i],len);
216
1.17k
  }
217
8.55k
  if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
218
219
8.54k
  return(0);
220
342
 err_out:
221
342
  vorbis_comment_clear(vc);
222
342
  return(OV_EBADHEADER);
223
8.55k
}
224
225
/* all of the real encoding details are here.  The modes, books,
226
   everything */
227
8.51k
static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
228
8.51k
  codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
229
8.51k
  int i;
230
231
  /* codebooks */
232
8.51k
  ci->books=oggpack_read(opb,8)+1;
233
8.51k
  if(ci->books<=0)goto err_out;
234
23.5k
  for(i=0;i<ci->books;i++){
235
15.3k
    ci->book_param[i]=vorbis_staticbook_unpack(opb);
236
15.3k
    if(!ci->book_param[i])goto err_out;
237
15.3k
  }
238
239
  /* time backend settings */
240
8.19k
  ci->times=oggpack_read(opb,6)+1;
241
8.19k
  if(ci->times<=0)goto err_out;
242
16.3k
  for(i=0;i<ci->times;i++){
243
8.28k
    ci->time_type[i]=oggpack_read(opb,16);
244
8.28k
    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
8.28k
  }
249
250
  /* floor backend settings */
251
8.10k
  ci->floors=oggpack_read(opb,6)+1;
252
8.10k
  if(ci->floors<=0)goto err_out;
253
16.1k
  for(i=0;i<ci->floors;i++){
254
8.26k
    ci->floor_type[i]=oggpack_read(opb,16);
255
8.26k
    if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
256
8.23k
    ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
257
8.23k
    if(!ci->floor_param[i])goto err_out;
258
8.23k
  }
259
260
  /* residue backend settings */
261
7.89k
  ci->residues=oggpack_read(opb,6)+1;
262
7.89k
  if(ci->residues<=0)goto err_out;
263
16.9k
  for(i=0;i<ci->residues;i++){
264
9.26k
    ci->residue_type[i]=oggpack_read(opb,16);
265
9.26k
    if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
266
9.22k
    ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
267
9.22k
    if(!ci->residue_param[i])goto err_out;
268
9.22k
  }
269
270
  /* map backend settings */
271
7.72k
  ci->maps=oggpack_read(opb,6)+1;
272
7.72k
  if(ci->maps<=0)goto err_out;
273
15.6k
  for(i=0;i<ci->maps;i++){
274
8.03k
    ci->map_type[i]=oggpack_read(opb,16);
275
8.03k
    if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
276
8.01k
    ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
277
8.01k
    if(!ci->map_param[i])goto err_out;
278
8.01k
  }
279
  
280
  /* mode settings */
281
7.57k
  ci->modes=oggpack_read(opb,6)+1;
282
7.57k
  if(ci->modes<=0)goto err_out;
283
17.2k
  for(i=0;i<ci->modes;i++){
284
9.73k
    ci->mode_param[i]=(vorbis_info_mode *)_ogg_calloc(1,sizeof(*ci->mode_param[i]));
285
9.73k
    ci->mode_param[i]->blockflag=oggpack_read(opb,1);
286
9.73k
    ci->mode_param[i]->windowtype=oggpack_read(opb,16);
287
9.73k
    ci->mode_param[i]->transformtype=oggpack_read(opb,16);
288
9.73k
    ci->mode_param[i]->mapping=oggpack_read(opb,8);
289
290
9.73k
    if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
291
9.72k
    if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
292
9.71k
    if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
293
9.70k
    if(ci->mode_param[i]->mapping<0)goto err_out;
294
9.70k
  }
295
  
296
7.52k
  if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
297
298
7.51k
  return(0);
299
1.00k
 err_out:
300
1.00k
  vorbis_info_clear(vi);
301
1.00k
  return(OV_EBADHEADER);
302
7.52k
}
303
304
/* Is this packet a vorbis ID header? */
305
11.5k
int vorbis_synthesis_idheader(ogg_packet *op){
306
11.5k
  oggpack_buffer opb;
307
11.5k
  char buffer[6];
308
309
11.5k
  if(op){
310
11.5k
    oggpack_readinit(&opb,op->packet,op->bytes);
311
312
11.5k
    if(!op->b_o_s)
313
286
      return(0); /* Not the initial packet */
314
315
11.2k
    if(oggpack_read(&opb,8) != 1)
316
1.25k
      return 0; /* not an ID header */
317
318
9.97k
    memset(buffer,0,6);
319
9.97k
    _v_readstring(&opb,buffer,6);
320
9.97k
    if(memcmp(buffer,"vorbis",6))
321
570
      return 0; /* not vorbis */
322
323
9.40k
    return 1;
324
9.97k
  }
325
326
0
  return 0;
327
11.5k
}
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
26.8k
int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
335
26.8k
  oggpack_buffer opb;
336
  
337
26.8k
  if(op){
338
26.8k
    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
26.8k
    {
343
26.8k
      char buffer[6];
344
26.8k
      int packtype=oggpack_read(&opb,8);
345
26.8k
      memset(buffer,0,6);
346
26.8k
      _v_readstring(&opb,buffer,6);
347
26.8k
      if(memcmp(buffer,"vorbis",6)){
348
  /* not a vorbis header */
349
21
  return(OV_ENOTVORBIS);
350
21
      }
351
26.8k
      switch(packtype){
352
9.44k
      case 0x01: /* least significant *bit* is read first */
353
9.44k
  if(!op->b_o_s){
354
    /* Not the initial packet */
355
1
    return(OV_EBADHEADER);
356
1
  }
357
9.43k
  if(vi->rate!=0){
358
    /* previously initialized info header */
359
32
    return(OV_EBADHEADER);
360
32
  }
361
362
9.40k
  return(_vorbis_unpack_info(vi,&opb));
363
364
8.88k
      case 0x03: /* least significant *bit* is read first */
365
8.88k
  if(vi->rate==0){
366
    /* um... we didn't get the initial header */
367
0
    return(OV_EBADHEADER);
368
0
  }
369
8.88k
        if(vc->vendor!=NULL){
370
          /* previously initialized comment header */
371
1
          return(OV_EBADHEADER);
372
1
        }
373
374
8.88k
  return(_vorbis_unpack_comment(vc,&opb));
375
376
8.55k
      case 0x05: /* least significant *bit* is read first */
377
8.55k
  if(vi->rate==0 || vc->vendor==NULL){
378
    /* um... we didn;t get the initial header or comments yet */
379
32
    return(OV_EBADHEADER);
380
32
  }
381
8.51k
        if(vi->codec_setup==NULL){
382
          /* improperly initialized vorbis_info */
383
0
          return(OV_EFAULT);
384
0
        }
385
8.51k
        if(((codec_setup_info *)vi->codec_setup)->books>0){
386
          /* previously initialized setup header */
387
0
          return(OV_EBADHEADER);
388
0
        }
389
390
8.51k
  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
26.8k
      }
397
26.8k
    }
398
26.8k
  }
399
0
  return(OV_EBADHEADER);
400
26.8k
}
401