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 "common/contextual_callback.h"
25
26 namespace bluetooth {
27 namespace fuzz {
28
29 std::vector<std::vector<uint8_t>> SplitInput(const uint8_t* data, size_t size,
30 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,
42 std::vector<uint8_t> data) {
43 auto packet = TView::FromBytes(data);
44 if (!packet.IsValid()) {
45 return;
46 }
47 if (callback) {
48 callback(packet);
49 }
50 }
51
52 template <typename TView>
InvokeIfValid(common::ContextualCallback<void (TView)> callback,std::vector<uint8_t> data)53 void InvokeIfValid(common::ContextualCallback<void(TView)> callback, std::vector<uint8_t> data) {
54 auto packet = TView::FromBytes(data);
55 if (!packet.IsValid()) {
56 return;
57 }
58 if (callback) {
59 callback(packet);
60 }
61 }
62
63 } // namespace fuzz
64 } // namespace bluetooth
65