13c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com/* Copyright (c) 2012 Xiph.Org Foundation
23c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   Written by Jüri Aedla and Ralph Giles */
33c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com/*
43c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   Redistribution and use in source and binary forms, with or without
53c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   modification, are permitted provided that the following conditions
63c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   are met:
73c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
83c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   - Redistributions of source code must retain the above copyright
93c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   notice, this list of conditions and the following disclaimer.
103c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
113c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   - Redistributions in binary form must reproduce the above copyright
123c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   notice, this list of conditions and the following disclaimer in the
133c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   documentation and/or other materials provided with the distribution.
143c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
153c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
163c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
173c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
183c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
193c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
203c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
213c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
223c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
233c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
243c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
253c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
263c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com*/
273c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
283c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com/* Check for overflow in reading the padding length.
293c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com * http://lists.xiph.org/pipermail/opus/2012-November/001834.html
303c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com */
313c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
323c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com#include <stdio.h>
333c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com#include <stdlib.h>
343c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com#include <string.h>
353c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com#include "opus.h"
363c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com#include "test_opus_common.h"
373c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
383c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com#define PACKETSIZE 16909318
393c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com#define CHANNELS 2
403c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com#define FRAMESIZE 5760
413c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
423c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.comint test_overflow(void)
433c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com{
443c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  OpusDecoder *decoder;
453c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  int result;
463c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  int error;
473c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
483c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  unsigned char *in = malloc(PACKETSIZE);
493c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  opus_int16 *out = malloc(FRAMESIZE*CHANNELS*sizeof(*out));
503c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
513c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  fprintf(stderr, "  Checking for padding overflow... ");
523c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  if (!in || !out) {
533c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com    fprintf(stderr, "FAIL (out of memory)\n");
543c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com    return -1;
553c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  }
563c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  in[0] = 0xff;
573c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  in[1] = 0x41;
583c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  memset(in + 2, 0xff, PACKETSIZE - 3);
593c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  in[PACKETSIZE-1] = 0x0b;
603c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
613c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  decoder = opus_decoder_create(48000, CHANNELS, &error);
623c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  result = opus_decode(decoder, in, PACKETSIZE, out, FRAMESIZE, 0);
633c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  opus_decoder_destroy(decoder);
643c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
653c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  free(in);
663c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  free(out);
673c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
683c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  if (result != OPUS_INVALID_PACKET) {
693c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com    fprintf(stderr, "FAIL!\n");
703c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com    test_failed();
713c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  }
723c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
733c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  fprintf(stderr, "OK.\n");
743c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
753c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  return 1;
763c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com}
773c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
783c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.comint main(void)
793c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com{
803c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  const char *oversion;
813c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  int tests = 0;;
823c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
833c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  iseed = 0;
843c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  oversion = opus_get_version_string();
853c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  if (!oversion) test_failed();
863c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  fprintf(stderr, "Testing %s padding.\n", oversion);
873c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
883c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  tests += test_overflow();
893c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
903c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  fprintf(stderr, "All padding tests passed.\n");
913c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com
923c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com  return 0;
933c3902f0ac13428394f14f78f0fab05ef3468d69tlegrand@google.com}
94