1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "fuzz/helpers.h"
18 
19 #include "common/bind.h"
20 
21 namespace bluetooth {
22 namespace fuzz {
23 
24 // cribbed from https://github.com/google/fuzzing/blob/master/docs/split-inputs.md#magic-separator
SplitInput(const uint8_t * data,size_t size,const uint8_t * separator,size_t separatorSize)25 std::vector<std::vector<uint8_t>> SplitInput(
26     const uint8_t* data, size_t size, const uint8_t* separator, size_t separatorSize) {
27   std::vector<std::vector<uint8_t>> result;
28   assert(separatorSize > 0);
29   auto beg = data;
30   auto end = data + size;
31   while (const uint8_t* pos = (const uint8_t*)memmem(beg, end - beg, separator, separatorSize)) {
32     result.push_back({beg, pos});
33     beg = pos + separatorSize;
34   }
35   if (beg < end) {
36     result.push_back({beg, end});
37   }
38   return result;
39 }
40 
GetArbitraryBytes(FuzzedDataProvider * fdp)41 std::vector<uint8_t> GetArbitraryBytes(FuzzedDataProvider* fdp) {
42   return fdp->ConsumeBytes<uint8_t>(fdp->ConsumeIntegral<size_t>());
43 }
44 
45 }  // namespace fuzz
46 }  // namespace bluetooth
47