1 /******************************************************************************
2 *
3 * Copyright (C) 2020 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 #include <stdint.h>
21 #include <string.h>
22 #include "gsm.h"
23
24 constexpr int16_t kGsmInFrameSize = 33;
25 constexpr int16_t kGsmInFrameSizeMinus1 = kGsmInFrameSize - 1;
26 constexpr int16_t kGsmOutFrameSize = 160;
27 constexpr int16_t kGsmTotalInFrameSize = (kGsmInFrameSize + kGsmInFrameSizeMinus1);
28 constexpr int16_t kGsmTotalOutFrameSize = (kGsmOutFrameSize * 2);
29
30 class Codec {
31 public:
32 Codec() = default;
~Codec()33 ~Codec() { deInitDecoder(); }
34 bool initDecoder();
35 void decodeFrames(const uint8_t *data, size_t size);
36 void deInitDecoder();
37
38 private:
39 gsm mGsm = nullptr;
40 };
41
initDecoder()42 bool Codec::initDecoder() {
43 mGsm = gsm_create();
44 if (mGsm) {
45 int msopt = 1;
46 gsm_option(mGsm, GSM_OPT_WAV49, &msopt);
47 return true;
48 } else {
49 return false;
50 }
51 }
52
decodeFrames(const uint8_t * data,size_t size)53 void Codec::decodeFrames(const uint8_t *data, size_t size) {
54 uint8_t firstByte = *data;
55 int32_t verbosity = firstByte & 0x01;
56 int32_t fast = (firstByte >> 1) & 0x01;
57 gsm_option(mGsm, GSM_OPT_VERBOSE, &verbosity);
58 gsm_option(mGsm, GSM_OPT_FAST, &fast);
59 gsm_byte *readPointer = const_cast<uint8_t *>(data);
60 uint8_t tmpData[kGsmTotalInFrameSize];
61 gsm_signal out[kGsmTotalOutFrameSize];
62
63 while (size > 0) {
64 if (size < kGsmTotalInFrameSize) {
65 memset(tmpData, 0, sizeof(tmpData));
66 memcpy(tmpData, data, size);
67 size = kGsmTotalInFrameSize;
68 readPointer = tmpData;
69 }
70 gsm_decode(mGsm, readPointer, out);
71 readPointer += kGsmInFrameSize;
72 size -= kGsmInFrameSize;
73
74 gsm_decode(mGsm, readPointer, out);
75 readPointer += kGsmInFrameSizeMinus1;
76 size -= kGsmInFrameSizeMinus1;
77 }
78 }
79
deInitDecoder()80 void Codec::deInitDecoder() {
81 gsm_destroy(mGsm);
82 mGsm = nullptr;
83 }
84
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
86 if (size < 1) {
87 return 0;
88 }
89 Codec *codec = new Codec();
90 if (!codec) {
91 return 0;
92 }
93 if (codec->initDecoder()) {
94 codec->decodeFrames(data, size);
95 }
96 delete codec;
97 return 0;
98 }
99