BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslim_fuzzdataview.h
Go to the documentation of this file.
1/// @file bslim_fuzzdataview.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslim_fuzzdataview.h -*-C++-*-
8#ifndef INCLUDED_BSLIM_FUZZDATAVIEW
9#define INCLUDED_BSLIM_FUZZDATAVIEW
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslim_fuzzdataview bslim_fuzzdataview
15/// @brief Provide a view of a buffer of fuzz data bytes.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslim
19/// @{
20/// @addtogroup bslim_fuzzdataview
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslim_fuzzdataview-purpose"> Purpose</a>
25/// * <a href="#bslim_fuzzdataview-classes"> Classes </a>
26/// * <a href="#bslim_fuzzdataview-description"> Description </a>
27/// * <a href="#bslim_fuzzdataview-usage"> Usage </a>
28/// * <a href="#bslim_fuzzdataview-example-1-creating-a-bsl-string"> Example 1: Creating a bsl::string </a>
29///
30/// # Purpose {#bslim_fuzzdataview-purpose}
31/// Provide a view of a buffer of fuzz data bytes.
32///
33/// # Classes {#bslim_fuzzdataview-classes}
34///
35/// - bslim::FuzzDataView: reference-semantic type for fuzz data
36///
37/// @see bslim_fuzzutil
38///
39/// # Description {#bslim_fuzzdataview-description}
40/// This component defines a reference-semantic class,
41/// `bslim::FuzzDataView`, providing a view to a non-modifiable buffer of fuzz
42/// data obtained from a fuzz testing harness (e.g., `libFuzzer`).
43///
44/// See {http://bburl/BDEFuzzTesting} for details on how to build and run with
45/// fuzz testing enabled.
46///
47/// Typically, this class is intended to be employed by a utility that takes an
48/// object of this class as an in-out parameter, consumes the bytes (updating
49/// the view so that the bytes are not used again), and returns objects of the
50/// type requested.
51///
52/// ## Usage {#bslim_fuzzdataview-usage}
53///
54///
55/// This section illustrates intended use of this component.
56///
57/// ### Example 1: Creating a bsl::string {#bslim_fuzzdataview-example-1-creating-a-bsl-string}
58///
59///
60/// The following example demonstrates how to create a `bsl::string` object from
61/// a `FuzzDataView`.
62///
63/// First, we construct a `FuzzDataView` object, `view0`, from an array of
64/// bytes:
65/// @code
66/// const bsl::uint8_t data[] = {0x8A, 0x19, 0x0D, 0x44, 0x37, 0x0D,
67/// 0x38, 0x5E, 0x9B, 0xAA, 0xF3, 0xDA};
68///
69/// bslim::FuzzDataView view0(data, sizeof(data));
70///
71/// assert(12 == view0.length());
72/// @endcode
73/// Next, we take the first 3 bytes from `view0` and store them in a new
74/// `FuzzDataView` object, `view1`:
75/// @code
76/// bslim::FuzzDataView view1 = view0.removePrefix(3);
77///
78/// assert(3 == view1.length());
79/// assert(9 == view0.length());
80/// @endcode
81/// We confirm that `removePrefix(3)` removed 3 bytes from `view0` and that
82/// `view1` has length 3.
83///
84/// Then, we create a `bsl::string` object from `view1`:
85/// @code
86/// bsl::string s1(view1.begin(), view1.end());
87///
88/// assert(3 == s1.length());
89/// @endcode
90/// Finally, we create another `bsl::string` with the remaining bytes of
91/// `view0`:
92/// @code
93/// bsl::string s2(view0.begin(), view0.end());
94///
95/// assert(9 == s2.length());
96/// @endcode
97/// @}
98/** @} */
99/** @} */
100
101/** @addtogroup bsl
102 * @{
103 */
104/** @addtogroup bslim
105 * @{
106 */
107/** @addtogroup bslim_fuzzdataview
108 * @{
109 */
110
111#include <bslscm_version.h>
112
113#include <bsls_assert.h>
114
115#include <bsl_algorithm.h> // min
116#include <bsl_cstddef.h> // size_t
117#include <bsl_cstdint.h> // uint8_t
118
119
120namespace bslim {
121
122 // ==================
123 // class FuzzDataView
124 // ==================
125
126/// This type represents a view of a buffer of bytes provided by a fuzz
127/// testing harness.
128///
129/// See @ref bslim_fuzzdataview
131
132 private:
133 // DATA
134 const bsl::uint8_t *d_data_p; // pointer to the data
135 bsl::size_t d_length; // length of the view
136
137 public:
138 // CREATORS
139
140 /// Create a `FuzzDataView` object from the specified fuzz `data` and
141 /// `size`. The behavior is undefined unless `data || (0 == size)`.
142 FuzzDataView(const bsl::uint8_t *data, bsl::size_t size);
143
144 /// Create a `FuzzDataView` having the value of the specified `original`
145 /// `FuzzDataView`.
146 FuzzDataView(const FuzzDataView& original) = default;
147
148 /// Destroy this object.
149 ~FuzzDataView() = default;
150
151 // MANIPULATORS
152
153 /// Assign to this FuzzDataView the value of the specified `rhs`
154 /// `FuzzDataView`, and return a reference providing modifiable access to
155 /// this object. Note that this trivial assignment operation is generated
156 /// by the compiler.
157 // FuzzDataView &operator=(const FuzzDataView& rhs) = default;
158
159 /// Remove the specified initial `numBytes` from this view if
160 /// `numBytes <= length()` and remove `length()` bytes otherwise; return
161 /// a view to the bytes that were removed. Note that this method will
162 /// decrease the length of this view by `min(numBytes, length())` bytes.
163 FuzzDataView removePrefix(bsl::size_t numBytes);
164
165 /// Remove the specified last `numBytes` from this view if
166 /// `numBytes <= length()` and remove `length()` bytes otherwise; return
167 /// a view to the bytes that were removed. Note that this method will
168 /// decrease the length of this view by `min(numBytes, length())` bytes.
169 FuzzDataView removeSuffix(bsl::size_t numBytes);
170
171 // ACCESSORS
172
173 /// Return a const pointer to the beginning of the buffer.
174 const bsl::uint8_t *begin() const;
175
176 /// Return a const pointer to the end of the buffer.
177 const bsl::uint8_t *end() const;
178
179 /// Return the length in bytes of the buffer.
180 bsl::size_t length() const;
181
182 /// Return a pointer to the beginning of the buffer.
183 const bsl::uint8_t *data() const;
184};
185
186// ============================================================================
187// INLINE DEFINITIONS
188// ============================================================================
189
190 // ------------------
191 // class FuzzDataView
192 // ------------------
193
194// CREATORS
195inline
196FuzzDataView::FuzzDataView(const bsl::uint8_t *data, bsl::size_t size)
197: d_data_p(data)
198, d_length(size)
199{
200 BSLS_ASSERT(data || (0 == size));
201}
202
203// MANIPULATORS
204inline
206{
207 FuzzDataView prefix(d_data_p, bsl::min(numBytes, length()));
208
209 d_data_p += prefix.length();
210 d_length -= prefix.length();
211
212 return prefix;
213}
214
215inline
217{
218 bsl::size_t num = bsl::min(numBytes, length());
219
220 FuzzDataView suffix(end() - num, num);
221 d_length -= num;
222
223 return suffix;
224}
225
226// ACCESSORS
227inline
228const bsl::uint8_t* FuzzDataView::begin() const
229{
230 return d_data_p;
231}
232
233inline
234const bsl::uint8_t* FuzzDataView::end() const
235{
236 return d_data_p + d_length;
237}
238
239inline
240bsl::size_t FuzzDataView::length() const
241{
242 return d_length;
243}
244
245inline
246const bsl::uint8_t *FuzzDataView::data() const
247{
248 return d_data_p;
249}
250
251} // close package namespace
252
253
254#endif
255
256// ----------------------------------------------------------------------------
257// Copyright 2021 Bloomberg Finance L.P.
258//
259// Licensed under the Apache License, Version 2.0 (the "License");
260// you may not use this file except in compliance with the License.
261// You may obtain a copy of the License at
262//
263// http://www.apache.org/licenses/LICENSE-2.0
264//
265// Unless required by applicable law or agreed to in writing, software
266// distributed under the License is distributed on an "AS IS" BASIS,
267// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
268// See the License for the specific language governing permissions and
269// limitations under the License.
270// ----------------------------- END-OF-FILE ----------------------------------
271
272/** @} */
273/** @} */
274/** @} */
Definition bslim_fuzzdataview.h:130
~FuzzDataView()=default
Destroy this object.
FuzzDataView(const FuzzDataView &original)=default
const bsl::uint8_t * data() const
Return a pointer to the beginning of the buffer.
Definition bslim_fuzzdataview.h:246
const bsl::uint8_t * end() const
Return a const pointer to the end of the buffer.
Definition bslim_fuzzdataview.h:234
const bsl::uint8_t * begin() const
Return a const pointer to the beginning of the buffer.
Definition bslim_fuzzdataview.h:228
FuzzDataView removeSuffix(bsl::size_t numBytes)
Definition bslim_fuzzdataview.h:216
FuzzDataView removePrefix(bsl::size_t numBytes)
Definition bslim_fuzzdataview.h:205
FuzzDataView(const bsl::uint8_t *data, bsl::size_t size)
Definition bslim_fuzzdataview.h:196
bsl::size_t length() const
Return the length in bytes of the buffer.
Definition bslim_fuzzdataview.h:240
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslim_formatguard.h:120