Henrik Lundin | fe32a76 | 2015-12-08 11:27:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/test/fuzzers/audio_decoder_fuzzer.h" |
| 12 | |
| 13 | #include "webrtc/base/checks.h" |
| 14 | #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | namespace { |
| 18 | size_t PacketSizeFromTwoBytes(const uint8_t* data, size_t size) { |
| 19 | if (size < 2) |
| 20 | return 0; |
| 21 | return static_cast<size_t>((data[0] << 8) + data[1]); |
| 22 | } |
| 23 | } // namespace |
| 24 | |
| 25 | // This function reads two bytes from the beginning of |data|, interprets them |
| 26 | // as the first packet length, and reads this many bytes if available. The |
| 27 | // payload is inserted into the decoder, and the process continues until no more |
| 28 | // data is available. |
| 29 | void FuzzAudioDecoder(const uint8_t* data, |
| 30 | size_t size, |
| 31 | AudioDecoder* decoder, |
| 32 | int sample_rate_hz, |
| 33 | size_t max_decoded_bytes, |
| 34 | int16_t* decoded) { |
| 35 | const uint8_t* data_ptr = data; |
| 36 | size_t remaining_size = size; |
| 37 | size_t packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); |
| 38 | while (packet_len != 0 && packet_len <= remaining_size - 2) { |
| 39 | data_ptr += 2; |
| 40 | remaining_size -= 2; |
| 41 | AudioDecoder::SpeechType speech_type; |
| 42 | decoder->Decode(data_ptr, packet_len, sample_rate_hz, max_decoded_bytes, |
| 43 | decoded, &speech_type); |
| 44 | data_ptr += packet_len; |
| 45 | remaining_size -= packet_len; |
| 46 | packet_len = PacketSizeFromTwoBytes(data_ptr, remaining_size); |
| 47 | } |
| 48 | } |
| 49 | } // namespace webrtc |