1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef BLOB_H
25 #define BLOB_H
26
27 #include <stdbool.h>
28 #include <stddef.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /* The blob functions implement a simple, low-level API for serializing and
37 * deserializing.
38 *
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).
44 *
45 * A blob is efficient in that it dynamically grows by doubling in size, so
46 * allocation costs are logarithmic.
47 */
48
49 struct blob {
50 /* The data actually written to the blob. */
51 uint8_t *data;
52
53 /** Number of bytes that have been allocated for \c data. */
54 size_t allocated;
55
56 /** The number of bytes that have actual data written to them. */
57 size_t size;
58
59 /** True if \c data a fixed allocation that we cannot resize
60 *
61 * \see blob_init_fixed
62 */
63 bool fixed_allocation;
64
65 /**
66 * True if we've ever failed to realloc or if we go pas the end of a fixed
67 * allocation blob.
68 */
69 bool out_of_memory;
70 };
71
72 /* When done reading, the caller can ensure that everything was consumed by
73 * checking the following:
74 *
75 * 1. blob->current should be equal to blob->end, (if not, too little was
76 * read).
77 *
78 * 2. blob->overrun should be false, (otherwise, too much was read).
79 */
80 struct blob_reader {
81 const uint8_t *data;
82 const uint8_t *end;
83 const uint8_t *current;
84 bool overrun;
85 };
86
87 /**
88 * Init a new, empty blob.
89 */
90 void
91 blob_init(struct blob *blob);
92
93 /**
94 * Init a new, fixed-size blob.
95 *
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.
99 *
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.
103 */
104 void
105 blob_init_fixed(struct blob *blob, void *data, size_t size);
106
107 /**
108 * Finish a blob and free its memory.
109 *
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.
112 */
113 static inline void
blob_finish(struct blob * blob)114 blob_finish(struct blob *blob)
115 {
116 if (!blob->fixed_allocation)
117 free(blob->data);
118 }
119
120 void
121 blob_finish_get_buffer(struct blob *blob, void **buffer, size_t *size);
122
123 /**
124 * Add some unstructured, fixed-size data to a blob.
125 *
126 * \return True unless allocation failed.
127 */
128 bool
129 blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write);
130
131 /**
132 * Reserve space in \blob for a number of bytes.
133 *
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.
137 *
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).
140 */
141 intptr_t
142 blob_reserve_bytes(struct blob *blob, size_t to_write);
143
144 /**
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.
148 */
149 intptr_t
150 blob_reserve_uint32(struct blob *blob);
151
152 /**
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.
156 */
157 intptr_t
158 blob_reserve_intptr(struct blob *blob);
159
160 /**
161 * Overwrite some data previously written to the blob.
162 *
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.
166 *
167 * For example usage, see blob_overwrite_uint32
168 *
169 * \return True unless the requested offset or offset+to_write lie outside
170 * the current blob's size.
171 */
172 bool
173 blob_overwrite_bytes(struct blob *blob,
174 size_t offset,
175 const void *bytes,
176 size_t to_write);
177
178 /**
179 * Add a uint8_t to a blob.
180 *
181 * \return True unless allocation failed.
182 */
183 bool
184 blob_write_uint8(struct blob *blob, uint8_t value);
185
186 /**
187 * Overwrite a uint8_t previously written to the blob.
188 *
189 * Writes a uint8_t value to an existing portion of the blob at an offset of
190 * \offset. This data range must have previously been written to the blob by
191 * one of the blob_write_* calls.
192 *
193 * \return True unless the requested position or position+to_write lie outside
194 * the current blob's size.
195 */
196 bool
197 blob_overwrite_uint8(struct blob *blob,
198 size_t offset,
199 uint8_t value);
200
201 /**
202 * Add a uint16_t to a blob.
203 *
204 * \note This function will only write to a uint16_t-aligned offset from the
205 * beginning of the blob's data, so some padding bytes may be added to the
206 * blob if this write follows some unaligned write (such as
207 * blob_write_string).
208 *
209 * \return True unless allocation failed.
210 */
211 bool
212 blob_write_uint16(struct blob *blob, uint16_t value);
213
214 /**
215 * Add a uint32_t to a blob.
216 *
217 * \note This function will only write to a uint32_t-aligned offset from the
218 * beginning of the blob's data, so some padding bytes may be added to the
219 * blob if this write follows some unaligned write (such as
220 * blob_write_string).
221 *
222 * \return True unless allocation failed.
223 */
224 bool
225 blob_write_uint32(struct blob *blob, uint32_t value);
226
227 /**
228 * Overwrite a uint32_t previously written to the blob.
229 *
230 * Writes a uint32_t value to an existing portion of the blob at an offset of
231 * \offset. This data range must have previously been written to the blob by
232 * one of the blob_write_* calls.
233 *
234 *
235 * The expected usage is something like the following pattern:
236 *
237 * size_t offset;
238 *
239 * offset = blob_reserve_uint32(blob);
240 * ... various blob write calls, writing N items ...
241 * blob_overwrite_uint32 (blob, offset, N);
242 *
243 * \return True unless the requested position or position+to_write lie outside
244 * the current blob's size.
245 */
246 bool
247 blob_overwrite_uint32(struct blob *blob,
248 size_t offset,
249 uint32_t value);
250
251 /**
252 * Add a uint64_t to a blob.
253 *
254 * \note This function will only write to a uint64_t-aligned offset from the
255 * beginning of the blob's data, so some padding bytes may be added to the
256 * blob if this write follows some unaligned write (such as
257 * blob_write_string).
258 *
259 * \return True unless allocation failed.
260 */
261 bool
262 blob_write_uint64(struct blob *blob, uint64_t value);
263
264 /**
265 * Add an intptr_t to a blob.
266 *
267 * \note This function will only write to an intptr_t-aligned offset from the
268 * beginning of the blob's data, so some padding bytes may be added to the
269 * blob if this write follows some unaligned write (such as
270 * blob_write_string).
271 *
272 * \return True unless allocation failed.
273 */
274 bool
275 blob_write_intptr(struct blob *blob, intptr_t value);
276
277 /**
278 * Overwrite an intptr_t previously written to the blob.
279 *
280 * Writes a intptr_t value to an existing portion of the blob at an offset of
281 * \offset. This data range must have previously been written to the blob by
282 * one of the blob_write_* calls.
283 *
284 * For example usage, see blob_overwrite_uint32
285 *
286 * \return True unless the requested position or position+to_write lie outside
287 * the current blob's size.
288 */
289 bool
290 blob_overwrite_intptr(struct blob *blob,
291 size_t offset,
292 intptr_t value);
293
294 /**
295 * Add a NULL-terminated string to a blob, (including the NULL terminator).
296 *
297 * \return True unless allocation failed.
298 */
299 bool
300 blob_write_string(struct blob *blob, const char *str);
301
302 /**
303 * Start reading a blob, (initializing the contents of \blob for reading).
304 *
305 * After this call, the caller can use the various blob_read_* functions to
306 * read elements from the data array.
307 *
308 * For all of the blob_read_* functions, if there is insufficient data
309 * remaining, the functions will do nothing, (perhaps returning default values
310 * such as 0). The caller can detect this by noting that the blob_reader's
311 * current value is unchanged before and after the call.
312 */
313 void
314 blob_reader_init(struct blob_reader *blob, const void *data, size_t size);
315
316 /**
317 * Read some unstructured, fixed-size data from the current location, (and
318 * update the current location to just past this data).
319 *
320 * \note The memory returned belongs to the data underlying the blob reader. The
321 * caller must copy the data in order to use it after the lifetime of the data
322 * underlying the blob reader.
323 *
324 * \return The bytes read (see note above about memory lifetime).
325 */
326 const void *
327 blob_read_bytes(struct blob_reader *blob, size_t size);
328
329 /**
330 * Read some unstructured, fixed-size data from the current location, copying
331 * it to \dest (and update the current location to just past this data)
332 */
333 void
334 blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size);
335
336 /**
337 * Skip \size bytes within the blob.
338 */
339 void
340 blob_skip_bytes(struct blob_reader *blob, size_t size);
341
342 /**
343 * Read a uint8_t from the current location, (and update the current location
344 * to just past this uint8_t).
345 *
346 * \return The uint8_t read
347 */
348 uint8_t
349 blob_read_uint8(struct blob_reader *blob);
350
351 /**
352 * Read a uint16_t from the current location, (and update the current location
353 * to just past this uint16_t).
354 *
355 * \note This function will only read from a uint16_t-aligned offset from the
356 * beginning of the blob's data, so some padding bytes may be skipped.
357 *
358 * \return The uint16_t read
359 */
360 uint16_t
361 blob_read_uint16(struct blob_reader *blob);
362
363 /**
364 * Read a uint32_t from the current location, (and update the current location
365 * to just past this uint32_t).
366 *
367 * \note This function will only read from a uint32_t-aligned offset from the
368 * beginning of the blob's data, so some padding bytes may be skipped.
369 *
370 * \return The uint32_t read
371 */
372 uint32_t
373 blob_read_uint32(struct blob_reader *blob);
374
375 /**
376 * Read a uint64_t from the current location, (and update the current location
377 * to just past this uint64_t).
378 *
379 * \note This function will only read from a uint64_t-aligned offset from the
380 * beginning of the blob's data, so some padding bytes may be skipped.
381 *
382 * \return The uint64_t read
383 */
384 uint64_t
385 blob_read_uint64(struct blob_reader *blob);
386
387 /**
388 * Read an intptr_t value from the current location, (and update the
389 * current location to just past this intptr_t).
390 *
391 * \note This function will only read from an intptr_t-aligned offset from the
392 * beginning of the blob's data, so some padding bytes may be skipped.
393 *
394 * \return The intptr_t read
395 */
396 intptr_t
397 blob_read_intptr(struct blob_reader *blob);
398
399 /**
400 * Read a NULL-terminated string from the current location, (and update the
401 * current location to just past this string).
402 *
403 * \note The memory returned belongs to the data underlying the blob reader. The
404 * caller must copy the string in order to use the string after the lifetime
405 * of the data underlying the blob reader.
406 *
407 * \return The string read (see note above about memory lifetime). However, if
408 * there is no NULL byte remaining within the blob, this function returns
409 * NULL.
410 */
411 char *
412 blob_read_string(struct blob_reader *blob);
413
414 #ifdef __cplusplus
415 }
416 #endif
417
418 #endif /* BLOB_H */
419