BDE 4.39.x 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 `size`.
141 ///
142 /// \pre The behavior is undefined unless `data || (0 == size)`.
143 FuzzDataView(const bsl::uint8_t *data, bsl::size_t size);
144
145 /// Create a `FuzzDataView` having the value of the specified `original`
146 /// `FuzzDataView`.
147 FuzzDataView(const FuzzDataView& original) = default;
148
149 /// Destroy this object.
150 ~FuzzDataView() = default;
151
152 // MANIPULATORS
153
154 /// Assign to this FuzzDataView the value of the specified `rhs`
155 /// `FuzzDataView`, and return a reference providing modifiable access to this object.
156 ///
157 /// \note Note that this trivial assignment operation is generated
158 /// by the compiler.
159 // FuzzDataView &operator=(const FuzzDataView& rhs) = default;
160
161 /// Remove the specified initial `numBytes` from this view if
162 /// `numBytes <= length()` and remove `length()` bytes otherwise; return a view to the bytes that were removed.
163 ///
164 /// \note Note that this method will
165 /// decrease the length of this view by `min(numBytes, length())` bytes.
166 FuzzDataView removePrefix(bsl::size_t numBytes);
167
168 /// Remove the specified last `numBytes` from this view if
169 /// `numBytes <= length()` and remove `length()` bytes otherwise; return a view to the bytes that were removed.
170 ///
171 /// \note Note that this method will
172 /// decrease the length of this view by `min(numBytes, length())` bytes.
173 FuzzDataView removeSuffix(bsl::size_t numBytes);
174
175 // ACCESSORS
176
177 /// Return a const pointer to the beginning of the buffer.
178 const bsl::uint8_t *begin() const;
179
180 /// Return a const pointer to the end of the buffer.
181 const bsl::uint8_t *end() const;
182
183 /// Return the length in bytes of the buffer.
184 bsl::size_t length() const;
185
186 /// Return a pointer to the beginning of the buffer.
187 const bsl::uint8_t *data() const;
188};
189
190// ============================================================================
191// INLINE DEFINITIONS
192// ============================================================================
193
194 // ------------------
195 // class FuzzDataView
196 // ------------------
197
198// CREATORS
199inline
200FuzzDataView::FuzzDataView(const bsl::uint8_t *data, bsl::size_t size)
201: d_data_p(data)
202, d_length(size)
203{
204 BSLS_ASSERT(data || (0 == size));
205}
206
207// MANIPULATORS
208inline
210{
211 FuzzDataView prefix(d_data_p, bsl::min(numBytes, length()));
212
213 d_data_p += prefix.length();
214 d_length -= prefix.length();
215
216 return prefix;
217}
218
219inline
221{
222 bsl::size_t num = bsl::min(numBytes, length());
223
224 FuzzDataView suffix(end() - num, num);
225 d_length -= num;
226
227 return suffix;
228}
229
230// ACCESSORS
231inline
232const bsl::uint8_t* FuzzDataView::begin() const
233{
234 return d_data_p;
235}
236
237inline
238const bsl::uint8_t* FuzzDataView::end() const
239{
240 return d_data_p + d_length;
241}
242
243inline
244bsl::size_t FuzzDataView::length() const
245{
246 return d_length;
247}
248
249inline
250const bsl::uint8_t *FuzzDataView::data() const
251{
252 return d_data_p;
253}
254
255} // close package namespace
256
257
258#endif
259
260// ----------------------------------------------------------------------------
261// Copyright 2021 Bloomberg Finance L.P.
262//
263// Licensed under the Apache License, Version 2.0 (the "License");
264// you may not use this file except in compliance with the License.
265// You may obtain a copy of the License at
266//
267// http://www.apache.org/licenses/LICENSE-2.0
268//
269// Unless required by applicable law or agreed to in writing, software
270// distributed under the License is distributed on an "AS IS" BASIS,
271// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
272// See the License for the specific language governing permissions and
273// limitations under the License.
274// ----------------------------- END-OF-FILE ----------------------------------
275
276/** @} */
277/** @} */
278/** @} */
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:250
const bsl::uint8_t * end() const
Return a const pointer to the end of the buffer.
Definition bslim_fuzzdataview.h:238
const bsl::uint8_t * begin() const
Return a const pointer to the beginning of the buffer.
Definition bslim_fuzzdataview.h:232
FuzzDataView removeSuffix(bsl::size_t numBytes)
Definition bslim_fuzzdataview.h:220
FuzzDataView removePrefix(bsl::size_t numBytes)
Definition bslim_fuzzdataview.h:209
FuzzDataView(const bsl::uint8_t *data, bsl::size_t size)
Definition bslim_fuzzdataview.h:200
bsl::size_t length() const
Return the length in bytes of the buffer.
Definition bslim_fuzzdataview.h:244
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslim_formatguard.h:120