ec80b78284cea610445ad005b0aeec3eaa99681d
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).
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 * Add some unstructured, fixed-size data to a blob.
123 * \return True unless allocation failed.
126 blob_write_bytes(struct blob
*blob
, const void *bytes
, size_t to_write
);
129 * Reserve space in \blob for a number of bytes.
131 * Space will be allocated within the blob for these byes, but the bytes will
132 * be left uninitialized. The caller is expected to use the return value to
133 * write directly (and immediately) to these bytes.
135 * \note The return value is valid immediately upon return, but can be
136 * invalidated by any other call to a blob function. So the caller should call
137 * blob_reserve_byes immediately before writing through the returned pointer.
139 * This function is intended to be used when interfacing with an existing API
140 * that is not aware of the blob API, (so that blob_write_bytes cannot be
143 * \return A pointer to space allocated within \blob to which \to_write bytes
144 * can be written, (or NULL in case of any allocation error).
147 blob_reserve_bytes(struct blob
*blob
, size_t to_write
);
150 * Overwrite some data previously written to the blob.
152 * Writes data to an existing portion of the blob at an offset of \offset.
153 * This data range must have previously been written to the blob by one of the
154 * blob_write_* calls.
156 * For example usage, see blob_overwrite_uint32
158 * \return True unless the requested offset or offset+to_write lie outside
159 * the current blob's size.
162 blob_overwrite_bytes(struct blob
*blob
,
168 * Add a uint32_t to a blob.
170 * \note This function will only write to a uint32_t-aligned offset from the
171 * beginning of the blob's data, so some padding bytes may be added to the
172 * blob if this write follows some unaligned write (such as
173 * blob_write_string).
175 * \return True unless allocation failed.
178 blob_write_uint32(struct blob
*blob
, uint32_t value
);
181 * Overwrite a uint32_t previously written to the blob.
183 * Writes a uint32_t value to an existing portion of the blob at an offset of
184 * \offset. This data range must have previously been written to the blob by
185 * one of the blob_write_* calls.
188 * The expected usage is something like the following pattern:
192 * offset = blob->size;
193 * blob_write_uint32 (blob, 0); // placeholder
194 * ... various blob write calls, writing N items ...
195 * blob_overwrite_uint32 (blob, offset, N);
197 * \return True unless the requested position or position+to_write lie outside
198 * the current blob's size.
201 blob_overwrite_uint32(struct blob
*blob
,
206 * Add a uint64_t to a blob.
208 * \note This function will only write to a uint64_t-aligned offset from the
209 * beginning of the blob's data, so some padding bytes may be added to the
210 * blob if this write follows some unaligned write (such as
211 * blob_write_string).
213 * \return True unless allocation failed.
216 blob_write_uint64(struct blob
*blob
, uint64_t value
);
219 * Add an intptr_t to a blob.
221 * \note This function will only write to an intptr_t-aligned offset from the
222 * beginning of the blob's data, so some padding bytes may be added to the
223 * blob if this write follows some unaligned write (such as
224 * blob_write_string).
226 * \return True unless allocation failed.
229 blob_write_intptr(struct blob
*blob
, intptr_t value
);
232 * Add a NULL-terminated string to a blob, (including the NULL terminator).
234 * \return True unless allocation failed.
237 blob_write_string(struct blob
*blob
, const char *str
);
240 * Start reading a blob, (initializing the contents of \blob for reading).
242 * After this call, the caller can use the various blob_read_* functions to
243 * read elements from the data array.
245 * For all of the blob_read_* functions, if there is insufficient data
246 * remaining, the functions will do nothing, (perhaps returning default values
247 * such as 0). The caller can detect this by noting that the blob_reader's
248 * current value is unchanged before and after the call.
251 blob_reader_init(struct blob_reader
*blob
, uint8_t *data
, size_t size
);
254 * Read some unstructured, fixed-size data from the current location, (and
255 * update the current location to just past this data).
257 * \note The memory returned belongs to the data underlying the blob reader. The
258 * caller must copy the data in order to use it after the lifetime of the data
259 * underlying the blob reader.
261 * \return The bytes read (see note above about memory lifetime).
264 blob_read_bytes(struct blob_reader
*blob
, size_t size
);
267 * Read some unstructured, fixed-size data from the current location, copying
268 * it to \dest (and update the current location to just past this data)
271 blob_copy_bytes(struct blob_reader
*blob
, uint8_t *dest
, size_t size
);
274 * Read a uint32_t from the current location, (and update the current location
275 * to just past this uint32_t).
277 * \note This function will only read from a uint32_t-aligned offset from the
278 * beginning of the blob's data, so some padding bytes may be skipped.
280 * \return The uint32_t read
283 blob_read_uint32(struct blob_reader
*blob
);
286 * Read a uint64_t from the current location, (and update the current location
287 * to just past this uint64_t).
289 * \note This function will only read from a uint64_t-aligned offset from the
290 * beginning of the blob's data, so some padding bytes may be skipped.
292 * \return The uint64_t read
295 blob_read_uint64(struct blob_reader
*blob
);
298 * Read an intptr_t value from the current location, (and update the
299 * current location to just past this intptr_t).
301 * \note This function will only read from an intptr_t-aligned offset from the
302 * beginning of the blob's data, so some padding bytes may be skipped.
304 * \return The intptr_t read
307 blob_read_intptr(struct blob_reader
*blob
);
310 * Read a NULL-terminated string from the current location, (and update the
311 * current location to just past this string).
313 * \note The memory returned belongs to the data underlying the blob reader. The
314 * caller must copy the string in order to use the string after the lifetime
315 * of the data underlying the blob reader.
317 * \return The string read (see note above about memory lifetime). However, if
318 * there is no NULL byte remaining within the blob, this function returns
322 blob_read_string(struct blob_reader
*blob
);