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 #pragma once
18
19 #include <fuzzer/FuzzedDataProvider.h>
20
21 #include <cstdint>
22 #include <vector>
23
24 #include "os/handler.h"
25
26 namespace bluetooth {
27 namespace fuzz {
28
29 std::vector<std::vector<uint8_t>> SplitInput(
30 const uint8_t* data, size_t size, const uint8_t* separator, size_t separatorSize);
31
32 std::vector<uint8_t> GetArbitraryBytes(FuzzedDataProvider* fdp);
33
34 #define CONSTRUCT_VALID_UNIQUE_OTHERWISE_BAIL(T, name, data) \
35 auto name = std::make_unique<T>(T::FromBytes(data)); \
36 if (!name->IsValid()) { \
37 return; \
38 }
39
40 template <typename TView>
InvokeIfValid(common::ContextualOnceCallback<void (TView)> callback,std::vector<uint8_t> data)41 void InvokeIfValid(common::ContextualOnceCallback<void(TView)> callback, std::vector<uint8_t> data) {
42 auto packet = TView::FromBytes(data);
43 if (!packet.IsValid()) {
44 return;
45 }
46 callback.InvokeIfNotEmpty(packet);
47 }
48
49 template <typename TView>
InvokeIfValid(common::ContextualCallback<void (TView)> callback,std::vector<uint8_t> data)50 void InvokeIfValid(common::ContextualCallback<void(TView)> callback, std::vector<uint8_t> data) {
51 auto packet = TView::FromBytes(data);
52 if (!packet.IsValid()) {
53 return;
54 }
55 callback.InvokeIfNotEmpty(packet);
56 }
57
58 } // namespace fuzz
59 } // namespace bluetooth
60