Coverage Report

Created: 2026-08-31 07:18

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