2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
36 /* The blob functions implement a simple, low-level API for serializing and
39 * All objects written to a blob will be serialized directly, (without any
40 * additional meta-data to describe the data written). Therefore, it is the
41 * caller's responsibility to ensure that any data can be read later, (either
42 * by knowing exactly what data is expected, or by writing to the blob
43 * sufficient meta-data to describe what has been written).
45 * A blob is efficient in that it dynamically grows by doubling in size, so
46 * allocation costs are logarithmic.
50 /* The data actually written to the blob. */
53 /** Number of bytes that have been allocated for \c data. */
56 /** The number of bytes that have actual data written to them. */
59 /** True if \c data a fixed allocation that we cannot resize
61 * \see blob_init_fixed
63 bool fixed_allocation
;
66 * True if we've ever failed to realloc or if we go pas the end of a fixed
72 /* When done reading, the caller can ensure that everything was consumed by
73 * checking the following:
75 * 1. blob->current should be equal to blob->end, (if not, too little was
78 * 2. blob->overrun should be false, (otherwise, too much was read).
83 const uint8_t *current
;
88 * Init a new, empty blob.
91 blob_init(struct blob
*blob
);
94 * Init a new, fixed-size blob.
96 * A fixed-size blob has a fixed block of data that will not be freed on
97 * blob_finish and will never be grown. If we hit the end, we simply start
98 * returning false from the write functions.
100 * If a fixed-size blob has a NULL data pointer then the data is written but
101 * it otherwise operates normally. This can be used to determine the size
102 * that will be required to write a given data structure.
105 blob_init_fixed(struct blob
*blob
, void *data
, size_t size
);
108 * Finish a blob and free its memory.
110 * If \blob was initialized with blob_init_fixed, the data pointer is
111 * considered to be owned by the user and will not be freed.
114 blob_finish(struct blob
*blob
)
116 if (!blob
->fixed_allocation
)
121 blob_finish_get_buffer(struct blob
*blob
, void **buffer
, size_t *size
);
124 * Add some unstructured, fixed-size data to a blob.
126 * \return True unless allocation failed.
129 blob_write_bytes(struct blob
*blob
, const void *bytes
, size_t to_write
);
132 * Reserve space in \blob for a number of bytes.
134 * Space will be allocated within the blob for these byes, but the bytes will
135 * be left uninitialized. The caller is expected to use \sa
136 * blob_overwrite_bytes to write to these bytes.
138 * \return An offset to space allocated within \blob to which \to_write bytes
139 * can be written, (or -1 in case of any allocation error).
142 blob_reserve_bytes(struct blob
*blob
, size_t to_write
);
145 * Similar to \sa blob_reserve_bytes, but only reserves an uint32_t worth of
146 * space. Note that this must be used if later reading with \sa
147 * blob_read_uint32, since it aligns the offset correctly.
150 blob_reserve_uint32(struct blob
*blob
);
153 * Similar to \sa blob_reserve_bytes, but only reserves an intptr_t worth of
154 * space. Note that this must be used if later reading with \sa
155 * blob_read_intptr, since it aligns the offset correctly.
158 blob_reserve_intptr(struct blob
*blob
);
161 * Overwrite some data previously written to the blob.
163 * Writes data to an existing portion of the blob at an offset of \offset.
164 * This data range must have previously been written to the blob by one of the
165 * blob_write_* calls.
167 * For example usage, see blob_overwrite_uint32
169 * \return True unless the requested offset or offset+to_write lie outside
170 * the current blob's size.
173 blob_overwrite_bytes(struct blob
*blob
,
179 * Add a uint8_t to a blob.
181 * \return True unless allocation failed.
184 blob_write_uint8(struct blob
*blob
, uint8_t value
);
187 * Add a uint16_t to a blob.
189 * \note This function will only write to a uint16_t-aligned offset from the
190 * beginning of the blob's data, so some padding bytes may be added to the
191 * blob if this write follows some unaligned write (such as
192 * blob_write_string).
194 * \return True unless allocation failed.
197 blob_write_uint16(struct blob
*blob
, uint16_t value
);
200 * Add a uint32_t to a blob.
202 * \note This function will only write to a uint32_t-aligned offset from the
203 * beginning of the blob's data, so some padding bytes may be added to the
204 * blob if this write follows some unaligned write (such as
205 * blob_write_string).
207 * \return True unless allocation failed.
210 blob_write_uint32(struct blob
*blob
, uint32_t value
);
213 * Overwrite a uint32_t previously written to the blob.
215 * Writes a uint32_t value to an existing portion of the blob at an offset of
216 * \offset. This data range must have previously been written to the blob by
217 * one of the blob_write_* calls.
220 * The expected usage is something like the following pattern:
224 * offset = blob_reserve_uint32(blob);
225 * ... various blob write calls, writing N items ...
226 * blob_overwrite_uint32 (blob, offset, N);
228 * \return True unless the requested position or position+to_write lie outside
229 * the current blob's size.
232 blob_overwrite_uint32(struct blob
*blob
,
237 * Add a uint64_t to a blob.
239 * \note This function will only write to a uint64_t-aligned offset from the
240 * beginning of the blob's data, so some padding bytes may be added to the
241 * blob if this write follows some unaligned write (such as
242 * blob_write_string).
244 * \return True unless allocation failed.
247 blob_write_uint64(struct blob
*blob
, uint64_t value
);
250 * Add an intptr_t to a blob.
252 * \note This function will only write to an intptr_t-aligned offset from the
253 * beginning of the blob's data, so some padding bytes may be added to the
254 * blob if this write follows some unaligned write (such as
255 * blob_write_string).
257 * \return True unless allocation failed.
260 blob_write_intptr(struct blob
*blob
, intptr_t value
);
263 * Overwrite an intptr_t previously written to the blob.
265 * Writes a intptr_t value to an existing portion of the blob at an offset of
266 * \offset. This data range must have previously been written to the blob by
267 * one of the blob_write_* calls.
269 * For example usage, see blob_overwrite_uint32
271 * \return True unless the requested position or position+to_write lie outside
272 * the current blob's size.
275 blob_overwrite_intptr(struct blob
*blob
,
280 * Add a NULL-terminated string to a blob, (including the NULL terminator).
282 * \return True unless allocation failed.
285 blob_write_string(struct blob
*blob
, const char *str
);
288 * Start reading a blob, (initializing the contents of \blob for reading).
290 * After this call, the caller can use the various blob_read_* functions to
291 * read elements from the data array.
293 * For all of the blob_read_* functions, if there is insufficient data
294 * remaining, the functions will do nothing, (perhaps returning default values
295 * such as 0). The caller can detect this by noting that the blob_reader's
296 * current value is unchanged before and after the call.
299 blob_reader_init(struct blob_reader
*blob
, const void *data
, size_t size
);
302 * Read some unstructured, fixed-size data from the current location, (and
303 * update the current location to just past this data).
305 * \note The memory returned belongs to the data underlying the blob reader. The
306 * caller must copy the data in order to use it after the lifetime of the data
307 * underlying the blob reader.
309 * \return The bytes read (see note above about memory lifetime).
312 blob_read_bytes(struct blob_reader
*blob
, size_t size
);
315 * Read some unstructured, fixed-size data from the current location, copying
316 * it to \dest (and update the current location to just past this data)
319 blob_copy_bytes(struct blob_reader
*blob
, void *dest
, size_t size
);
322 * Skip \size bytes within the blob.
325 blob_skip_bytes(struct blob_reader
*blob
, size_t size
);
328 * Read a uint8_t from the current location, (and update the current location
329 * to just past this uint8_t).
331 * \return The uint8_t read
334 blob_read_uint8(struct blob_reader
*blob
);
337 * Read a uint16_t from the current location, (and update the current location
338 * to just past this uint16_t).
340 * \note This function will only read from a uint16_t-aligned offset from the
341 * beginning of the blob's data, so some padding bytes may be skipped.
343 * \return The uint16_t read
346 blob_read_uint16(struct blob_reader
*blob
);
349 * Read a uint32_t from the current location, (and update the current location
350 * to just past this uint32_t).
352 * \note This function will only read from a uint32_t-aligned offset from the
353 * beginning of the blob's data, so some padding bytes may be skipped.
355 * \return The uint32_t read
358 blob_read_uint32(struct blob_reader
*blob
);
361 * Read a uint64_t from the current location, (and update the current location
362 * to just past this uint64_t).
364 * \note This function will only read from a uint64_t-aligned offset from the
365 * beginning of the blob's data, so some padding bytes may be skipped.
367 * \return The uint64_t read
370 blob_read_uint64(struct blob_reader
*blob
);
373 * Read an intptr_t value from the current location, (and update the
374 * current location to just past this intptr_t).
376 * \note This function will only read from an intptr_t-aligned offset from the
377 * beginning of the blob's data, so some padding bytes may be skipped.
379 * \return The intptr_t read
382 blob_read_intptr(struct blob_reader
*blob
);
385 * Read a NULL-terminated string from the current location, (and update the
386 * current location to just past this string).
388 * \note The memory returned belongs to the data underlying the blob reader. The
389 * caller must copy the string in order to use the string after the lifetime
390 * of the data underlying the blob reader.
392 * \return The string read (see note above about memory lifetime). However, if
393 * there is no NULL byte remaining within the blob, this function returns
397 blob_read_string(struct blob_reader
*blob
);