ref: 76394d18253d10725b2e016a070e8d0d103922cb
parent: ad46470e40cdc0576d98fc755b054318757b02a0
author: Mark Harris <mark.hsj@gmail.com>
date: Mon Jan 1 18:12:22 EST 2018
save_range: Correctly handle multistream padding opus_packet_parse_impl() is updated based on the current Opus code. Also fixes compiler warnings, including use of uninitialized data if the packet is invalid.
--- a/src/diag_range.c
+++ b/src/diag_range.c
@@ -31,14 +31,6 @@
# include "config.h"
#endif
-#ifdef _WIN32
-#define I64FORMAT "I64d"
-#define I64uFORMAT "I64u"
-#else
-#define I64FORMAT "lld"
-#define I64uFORMAT "llu"
-#endif
-
#include <stdio.h>
#include <opus.h>
#include "diag_range.h"
@@ -49,7 +41,7 @@
*data for save-range. If you're thinking about copying it and you aren't
*making an opus stream diagnostic tool, you're probably doing something
*wrong.*/
-static int parse_size(const unsigned char *data, opus_int32 len, short *size)
+static int parse_size(const unsigned char *data, opus_int32 len, opus_int16 *size)
{
if (len<1)
{
@@ -70,8 +62,8 @@
}
static int opus_packet_parse_impl(const unsigned char *data, opus_int32 len,
- int self_delimited, unsigned char *out_toc,
- const unsigned char *frames[48], short size[48], int *payload_offset)
+ int self_delimited, opus_int16 size[48], int *payload_offset,
+ opus_int32 *packet_offset)
{
int i, bytes;
int count;
@@ -78,11 +70,14 @@
int cbr;
unsigned char ch, toc;
int framesize;
- int last_size;
+ opus_int32 last_size;
+ opus_int32 pad = 0;
const unsigned char *data0 = data;
- if (size==NULL)
+ if (size==NULL || len<0)
return OPUS_BAD_ARG;
+ if (len==0)
+ return OPUS_INVALID_PACKET;
framesize = opus_packet_get_samples_per_frame(data, 48000);
@@ -104,7 +99,9 @@
{
if (len&0x1)
return OPUS_INVALID_PACKET;
- size[0] = last_size = len/2;
+ last_size = len/2;
+ /* If last_size doesn't fit in size[0], we'll catch it later */
+ size[0] = (opus_int16)last_size;
}
break;
/* Two VBR frames */
@@ -118,7 +115,7 @@
last_size = len-size[0];
break;
/* Multiple CBR/VBR frames (from 0 to 120 ms) */
- case 3:
+ default: /*case 3:*/
if (len<1)
return OPUS_INVALID_PACKET;
/* Number of frames encoded in bits 0 to 5 */
@@ -130,16 +127,17 @@
/* Padding flag is bit 6 */
if (ch&0x40)
{
- int padding=0;
int p;
do {
+ int tmp;
if (len<=0)
return OPUS_INVALID_PACKET;
p = *data++;
len--;
- padding += p==255 ? 254: p;
+ tmp = p==255 ? 254: p;
+ len -= tmp;
+ pad += tmp;
} while (p==255);
- len -= padding;
}
if (len<0)
return OPUS_INVALID_PACKET;
@@ -167,7 +165,7 @@
if (last_size*count!=len)
return OPUS_INVALID_PACKET;
for (i=0;i<count-1;i++)
- size[i] = last_size;
+ size[i] = (opus_int16)last_size;
}
break;
}
@@ -186,7 +184,7 @@
return OPUS_INVALID_PACKET;
for (i=0;i<count-1;i++)
size[i] = size[count-1];
- } else if(size[count-1] > last_size)
+ } else if (bytes+size[count-1] > last_size)
return OPUS_INVALID_PACKET;
} else
{
@@ -195,51 +193,46 @@
1275. Reject them here.*/
if (last_size > 1275)
return OPUS_INVALID_PACKET;
- size[count-1] = last_size;
+ size[count-1] = (opus_int16)last_size;
}
- if (frames)
- {
- for (i=0;i<count;i++)
- {
- frames[i] = data;
- data += size[i];
- }
- }
+ if (payload_offset)
+ *payload_offset = (int)(data-data0);
- if (out_toc)
- *out_toc = toc;
+ for (i=0;i<count;i++)
+ data += size[i];
- if (payload_offset)
- *payload_offset = data-data0;
+ if (packet_offset)
+ *packet_offset = pad+(opus_int32)(data-data0);
return count;
}
-void save_range(FILE *frange, int frame_size, const unsigned char *packet, int nbBytes, opus_uint32 *rngs, int nb_streams){
- int i, parsed_size;
+void save_range(FILE *frange, int frame_size, const unsigned char *packet, opus_int32 nbBytes, opus_uint32 *rngs, int nb_streams){
+ int i;
+ opus_int32 parsed_size;
const unsigned char *subpkt;
static const char *bw_strings[5]={"NB","MB","WB","SWB","FB"};
static const char *mode_strings[3]={"LP","HYB","MDCT"};
- fprintf(frange,"%d, %d, ",frame_size,nbBytes);
+ fprintf(frange,"%d, %ld, ",frame_size,(long)nbBytes);
subpkt=packet;
parsed_size=nbBytes;
for(i=0;i<nb_streams;i++){
int j,payload_offset,nf;
- const unsigned char *frames[48];
- unsigned char toc;
- short size[48];
+ opus_int32 packet_offset;
+ opus_int16 size[48];
payload_offset=0;
+ packet_offset=0;
nf=opus_packet_parse_impl(subpkt,parsed_size,i+1!=nb_streams,
- &toc,frames,size,&payload_offset);
- fprintf(frange,"[[%d",(int)(frames[0]-subpkt));
- for(j=0;j<nf;j++)fprintf(frange,", %d",size[j]);
+ size,&payload_offset,&packet_offset);
+ fprintf(frange,"[[%d",payload_offset);
+ for(j=0;j<nf;j++)fprintf(frange,", %d",(int)size[j]);
fprintf(frange,"], %s, %s, %c, %d",
mode_strings[((((subpkt[0]>>3)+48)&92)+4)>>5],
bw_strings[opus_packet_get_bandwidth(subpkt)-OPUS_BANDWIDTH_NARROWBAND],
subpkt[0]&4?'S':'M',opus_packet_get_samples_per_frame(subpkt,48000));
- fprintf(frange,", %" I64uFORMAT "]%s",(unsigned long long)rngs[i],i+1==nb_streams?"\n":", ");
- parsed_size-=payload_offset;
- subpkt+=payload_offset;
+ fprintf(frange,", %lu]%s",(unsigned long)rngs[i],i+1==nb_streams?"\n":", ");
+ parsed_size-=packet_offset;
+ subpkt+=packet_offset;
}
}
--- a/src/diag_range.h
+++ b/src/diag_range.h
@@ -25,4 +25,4 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-void save_range(FILE *frange, int frame_size, const unsigned char *packet, int nbBytes, opus_uint32 *rngs, int nb_streams);
+void save_range(FILE *frange, int frame_size, const unsigned char *packet, opus_int32 nbBytes, opus_uint32 *rngs, int nb_streams);