• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <algorithm>
23 
24 #include "gsm.h"
25 
26 constexpr size_t kGsmInNumSamples = 160;
27 constexpr size_t kGsmInBufferSize = kGsmInNumSamples * sizeof(gsm_signal);
28 
29 enum { IDX_VERBOSITY, IDX_FAST, IDX_LTP_CUT, IDX_WAV49, IDX_LAST };
30 
31 class Codec {
32    public:
33     Codec() = default;
~Codec()34     ~Codec() { deInitEncoder(); }
35     bool initEncoder();
36     void encodeFrames(uint8_t *data, size_t size);
37     void deInitEncoder();
38 
39    private:
40     gsm mGsm = nullptr;
41 };
42 
initEncoder()43 bool Codec::initEncoder() {
44     mGsm = gsm_create();
45     return mGsm ? true : false;
46 }
47 
encodeFrames(uint8_t * data,size_t size)48 void Codec::encodeFrames(uint8_t *data, size_t size) {
49     int32_t verbosity = data[IDX_VERBOSITY] & 0x01;
50     int32_t fast = data[IDX_FAST] & 0x01;
51     int32_t ltp_cut = data[IDX_LTP_CUT] & 0x01;
52     int32_t wav49 = data[IDX_WAV49] & 0x01;
53     gsm_option(mGsm, GSM_OPT_VERBOSE, &verbosity);
54     gsm_option(mGsm, GSM_OPT_FAST, &fast);
55     gsm_option(mGsm, GSM_OPT_LTP_CUT, &ltp_cut);
56     gsm_option(mGsm, GSM_OPT_WAV49, &wav49);
57     data += IDX_LAST;
58     size -= IDX_LAST;
59 
60     while (size > 0) {
61         gsm_signal tmpData[kGsmInNumSamples];
62         gsm_frame out;
63         size_t frameSize = std::min(size, kGsmInBufferSize);
64 
65         memcpy(tmpData, data, frameSize);
66         if (frameSize < kGsmInBufferSize) {
67             memset(reinterpret_cast<uint8_t *>(tmpData) + frameSize, data[0],
68                    kGsmInBufferSize - frameSize);
69         }
70         gsm_encode(mGsm, tmpData, out);
71         data += frameSize;
72         size -= frameSize;
73     }
74 }
75 
deInitEncoder()76 void Codec::deInitEncoder() {
77     if (mGsm) {
78         gsm_destroy(mGsm);
79         mGsm = nullptr;
80     }
81 }
82 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
84     if (size < IDX_LAST) {
85         return 0;
86     }
87     Codec *codec = new Codec();
88     if (!codec) {
89         return 0;
90     }
91     if (codec->initEncoder()) {
92         codec->encodeFrames(const_cast<uint8_t *>(data), size);
93     }
94     delete codec;
95     return 0;
96 }
97