summaryrefslogtreecommitdiff
path: root/src/opus_multistream_decoder.c
diff options
context:
space:
mode:
authorJean-Marc Valin <jmvalin@jmvalin.ca>2013-10-11 18:06:00 -0400
committerJean-Marc Valin <jmvalin@jmvalin.ca>2013-10-11 18:13:01 -0400
commited4632345ef8edd8ce07403bb4a4996d8c8f440a (patch)
tree8ed1cca47a215a7602f1f86f3d15c3c875b23440 /src/opus_multistream_decoder.c
parentaee4d8057632ea0cfc1d55d88acf8466b47b7b4b (diff)
downloadopus-ed4632345ef8edd8ce07403bb4a4996d8c8f440a.tar.gz
Do up-front validation of multistream packets
Prevents the decoder from being out-of-sync on an invalid packet. Also returns OPUS_INVALID_PACKET on a corrupted FEC packet.
Diffstat (limited to 'src/opus_multistream_decoder.c')
-rw-r--r--src/opus_multistream_decoder.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/opus_multistream_decoder.c b/src/opus_multistream_decoder.c
index f3d2311e..d04a99f3 100644
--- a/src/opus_multistream_decoder.c
+++ b/src/opus_multistream_decoder.c
@@ -152,6 +152,37 @@ typedef void (*opus_copy_channel_out_func)(
int frame_size
);
+static int opus_multistream_packet_validate(const unsigned char *data,
+ opus_int32 len, int nb_streams)
+{
+ int s;
+ int i;
+ int count;
+ unsigned char toc;
+ opus_int16 size[48];
+ int offset;
+ int samples=0;
+
+ for (s=0;s<nb_streams;s++)
+ {
+ int tmp_samples;
+ if (len<=0)
+ return OPUS_INVALID_PACKET;
+ count = opus_packet_parse_impl(data, len, s!=nb_streams-1, &toc, NULL, size, &offset);
+ if (count<0)
+ return count;
+ for (i=0;i<count;i++)
+ offset += size[i];
+ tmp_samples = opus_packet_get_nb_samples(data, offset, 48000);
+ if (s!=0 && samples != tmp_samples)
+ return OPUS_INVALID_PACKET;
+ samples = tmp_samples;
+ data += offset;
+ len -= offset;
+ }
+ return OPUS_OK;
+}
+
static int opus_multistream_decode_native(
OpusMSDecoder *st,
const unsigned char *data,
@@ -183,9 +214,24 @@ static int opus_multistream_decode_native(
if (len==0)
do_plc = 1;
if (len < 0)
+ {
+ RESTORE_STACK;
return OPUS_BAD_ARG;
+ }
if (!do_plc && len < 2*st->layout.nb_streams-1)
+ {
+ RESTORE_STACK;
return OPUS_INVALID_PACKET;
+ }
+ if (!do_plc)
+ {
+ int ret = opus_multistream_packet_validate(data, len, st->layout.nb_coupled_streams);
+ if (ret < 0)
+ {
+ RESTORE_STACK;
+ return ret;
+ }
+ }
for (s=0;s<st->layout.nb_streams;s++)
{
OpusDecoder *dec;