1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef BLOB_H
25bf215546Sopenharmony_ci#define BLOB_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <stdbool.h>
28bf215546Sopenharmony_ci#include <stddef.h>
29bf215546Sopenharmony_ci#include <stdint.h>
30bf215546Sopenharmony_ci#include <stdlib.h>
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#ifdef __cplusplus
33bf215546Sopenharmony_ciextern "C" {
34bf215546Sopenharmony_ci#endif
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci/* The blob functions implement a simple, low-level API for serializing and
37bf215546Sopenharmony_ci * deserializing.
38bf215546Sopenharmony_ci *
39bf215546Sopenharmony_ci * All objects written to a blob will be serialized directly, (without any
40bf215546Sopenharmony_ci * additional meta-data to describe the data written). Therefore, it is the
41bf215546Sopenharmony_ci * caller's responsibility to ensure that any data can be read later, (either
42bf215546Sopenharmony_ci * by knowing exactly what data is expected, or by writing to the blob
43bf215546Sopenharmony_ci * sufficient meta-data to describe what has been written).
44bf215546Sopenharmony_ci *
45bf215546Sopenharmony_ci * A blob is efficient in that it dynamically grows by doubling in size, so
46bf215546Sopenharmony_ci * allocation costs are logarithmic.
47bf215546Sopenharmony_ci */
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_cistruct blob {
50bf215546Sopenharmony_ci   /* The data actually written to the blob. Never read or write this directly
51bf215546Sopenharmony_ci    * when serializing, use blob_reserve_* and blob_overwrite_* instead which
52bf215546Sopenharmony_ci    * check for out_of_memory and handle fixed-size blobs correctly.
53bf215546Sopenharmony_ci    */
54bf215546Sopenharmony_ci   uint8_t *data;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   /** Number of bytes that have been allocated for \c data. */
57bf215546Sopenharmony_ci   size_t allocated;
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci   /** The number of bytes that have actual data written to them. */
60bf215546Sopenharmony_ci   size_t size;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   /** True if \c data a fixed allocation that we cannot resize
63bf215546Sopenharmony_ci    *
64bf215546Sopenharmony_ci    * \see blob_init_fixed
65bf215546Sopenharmony_ci    */
66bf215546Sopenharmony_ci   bool fixed_allocation;
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   /**
69bf215546Sopenharmony_ci    * True if we've ever failed to realloc or if we go pas the end of a fixed
70bf215546Sopenharmony_ci    * allocation blob.
71bf215546Sopenharmony_ci    */
72bf215546Sopenharmony_ci   bool out_of_memory;
73bf215546Sopenharmony_ci};
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci/* When done reading, the caller can ensure that everything was consumed by
76bf215546Sopenharmony_ci * checking the following:
77bf215546Sopenharmony_ci *
78bf215546Sopenharmony_ci *   1. blob->current should be equal to blob->end, (if not, too little was
79bf215546Sopenharmony_ci *      read).
80bf215546Sopenharmony_ci *
81bf215546Sopenharmony_ci *   2. blob->overrun should be false, (otherwise, too much was read).
82bf215546Sopenharmony_ci */
83bf215546Sopenharmony_cistruct blob_reader {
84bf215546Sopenharmony_ci   const uint8_t *data;
85bf215546Sopenharmony_ci   const uint8_t *end;
86bf215546Sopenharmony_ci   const uint8_t *current;
87bf215546Sopenharmony_ci   bool overrun;
88bf215546Sopenharmony_ci};
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci/**
91bf215546Sopenharmony_ci * Init a new, empty blob.
92bf215546Sopenharmony_ci */
93bf215546Sopenharmony_civoid
94bf215546Sopenharmony_ciblob_init(struct blob *blob);
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci/**
97bf215546Sopenharmony_ci * Init a new, fixed-size blob.
98bf215546Sopenharmony_ci *
99bf215546Sopenharmony_ci * A fixed-size blob has a fixed block of data that will not be freed on
100bf215546Sopenharmony_ci * blob_finish and will never be grown.  If we hit the end, we simply start
101bf215546Sopenharmony_ci * returning false from the write functions.
102bf215546Sopenharmony_ci *
103bf215546Sopenharmony_ci * If a fixed-size blob has a NULL data pointer then the data is written but
104bf215546Sopenharmony_ci * it otherwise operates normally.  This can be used to determine the size
105bf215546Sopenharmony_ci * that will be required to write a given data structure.
106bf215546Sopenharmony_ci */
107bf215546Sopenharmony_civoid
108bf215546Sopenharmony_ciblob_init_fixed(struct blob *blob, void *data, size_t size);
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci/**
111bf215546Sopenharmony_ci * Finish a blob and free its memory.
112bf215546Sopenharmony_ci *
113bf215546Sopenharmony_ci * If \blob was initialized with blob_init_fixed, the data pointer is
114bf215546Sopenharmony_ci * considered to be owned by the user and will not be freed.
115bf215546Sopenharmony_ci */
116bf215546Sopenharmony_cistatic inline void
117bf215546Sopenharmony_ciblob_finish(struct blob *blob)
118bf215546Sopenharmony_ci{
119bf215546Sopenharmony_ci   if (!blob->fixed_allocation)
120bf215546Sopenharmony_ci      free(blob->data);
121bf215546Sopenharmony_ci}
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_civoid
124bf215546Sopenharmony_ciblob_finish_get_buffer(struct blob *blob, void **buffer, size_t *size);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci/**
127bf215546Sopenharmony_ci * Aligns the blob to the given alignment.
128bf215546Sopenharmony_ci *
129bf215546Sopenharmony_ci * \see blob_reader_align
130bf215546Sopenharmony_ci *
131bf215546Sopenharmony_ci * \return True unless allocation fails
132bf215546Sopenharmony_ci */
133bf215546Sopenharmony_cibool
134bf215546Sopenharmony_ciblob_align(struct blob *blob, size_t alignment);
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci/**
137bf215546Sopenharmony_ci * Add some unstructured, fixed-size data to a blob.
138bf215546Sopenharmony_ci *
139bf215546Sopenharmony_ci * \return True unless allocation failed.
140bf215546Sopenharmony_ci */
141bf215546Sopenharmony_cibool
142bf215546Sopenharmony_ciblob_write_bytes(struct blob *blob, const void *bytes, size_t to_write);
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci/**
145bf215546Sopenharmony_ci * Reserve space in \blob for a number of bytes.
146bf215546Sopenharmony_ci *
147bf215546Sopenharmony_ci * Space will be allocated within the blob for these byes, but the bytes will
148bf215546Sopenharmony_ci * be left uninitialized. The caller is expected to use \sa
149bf215546Sopenharmony_ci * blob_overwrite_bytes to write to these bytes.
150bf215546Sopenharmony_ci *
151bf215546Sopenharmony_ci * \return An offset to space allocated within \blob to which \to_write bytes
152bf215546Sopenharmony_ci * can be written, (or -1 in case of any allocation error).
153bf215546Sopenharmony_ci */
154bf215546Sopenharmony_ciintptr_t
155bf215546Sopenharmony_ciblob_reserve_bytes(struct blob *blob, size_t to_write);
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci/**
158bf215546Sopenharmony_ci * Similar to \sa blob_reserve_bytes, but only reserves an uint32_t worth of
159bf215546Sopenharmony_ci * space. Note that this must be used if later reading with \sa
160bf215546Sopenharmony_ci * blob_read_uint32, since it aligns the offset correctly.
161bf215546Sopenharmony_ci */
162bf215546Sopenharmony_ciintptr_t
163bf215546Sopenharmony_ciblob_reserve_uint32(struct blob *blob);
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci/**
166bf215546Sopenharmony_ci * Similar to \sa blob_reserve_bytes, but only reserves an intptr_t worth of
167bf215546Sopenharmony_ci * space. Note that this must be used if later reading with \sa
168bf215546Sopenharmony_ci * blob_read_intptr, since it aligns the offset correctly.
169bf215546Sopenharmony_ci */
170bf215546Sopenharmony_ciintptr_t
171bf215546Sopenharmony_ciblob_reserve_intptr(struct blob *blob);
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci/**
174bf215546Sopenharmony_ci * Overwrite some data previously written to the blob.
175bf215546Sopenharmony_ci *
176bf215546Sopenharmony_ci * Writes data to an existing portion of the blob at an offset of \offset.
177bf215546Sopenharmony_ci * This data range must have previously been written to the blob by one of the
178bf215546Sopenharmony_ci * blob_write_* calls.
179bf215546Sopenharmony_ci *
180bf215546Sopenharmony_ci * For example usage, see blob_overwrite_uint32
181bf215546Sopenharmony_ci *
182bf215546Sopenharmony_ci * \return True unless the requested offset or offset+to_write lie outside
183bf215546Sopenharmony_ci * the current blob's size.
184bf215546Sopenharmony_ci */
185bf215546Sopenharmony_cibool
186bf215546Sopenharmony_ciblob_overwrite_bytes(struct blob *blob,
187bf215546Sopenharmony_ci                     size_t offset,
188bf215546Sopenharmony_ci                     const void *bytes,
189bf215546Sopenharmony_ci                     size_t to_write);
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci/**
192bf215546Sopenharmony_ci * Add a uint8_t to a blob.
193bf215546Sopenharmony_ci *
194bf215546Sopenharmony_ci * \return True unless allocation failed.
195bf215546Sopenharmony_ci */
196bf215546Sopenharmony_cibool
197bf215546Sopenharmony_ciblob_write_uint8(struct blob *blob, uint8_t value);
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci/**
200bf215546Sopenharmony_ci * Overwrite a uint8_t previously written to the blob.
201bf215546Sopenharmony_ci *
202bf215546Sopenharmony_ci * Writes a uint8_t value to an existing portion of the blob at an offset of
203bf215546Sopenharmony_ci * \offset.  This data range must have previously been written to the blob by
204bf215546Sopenharmony_ci * one of the blob_write_* calls.
205bf215546Sopenharmony_ci *
206bf215546Sopenharmony_ci * \return True unless the requested position or position+to_write lie outside
207bf215546Sopenharmony_ci * the current blob's size.
208bf215546Sopenharmony_ci */
209bf215546Sopenharmony_cibool
210bf215546Sopenharmony_ciblob_overwrite_uint8(struct blob *blob,
211bf215546Sopenharmony_ci                     size_t offset,
212bf215546Sopenharmony_ci                     uint8_t value);
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci/**
215bf215546Sopenharmony_ci * Add a uint16_t to a blob.
216bf215546Sopenharmony_ci *
217bf215546Sopenharmony_ci * \note This function will only write to a uint16_t-aligned offset from the
218bf215546Sopenharmony_ci * beginning of the blob's data, so some padding bytes may be added to the
219bf215546Sopenharmony_ci * blob if this write follows some unaligned write (such as
220bf215546Sopenharmony_ci * blob_write_string).
221bf215546Sopenharmony_ci *
222bf215546Sopenharmony_ci * \return True unless allocation failed.
223bf215546Sopenharmony_ci */
224bf215546Sopenharmony_cibool
225bf215546Sopenharmony_ciblob_write_uint16(struct blob *blob, uint16_t value);
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci/**
228bf215546Sopenharmony_ci * Add a uint32_t to a blob.
229bf215546Sopenharmony_ci *
230bf215546Sopenharmony_ci * \note This function will only write to a uint32_t-aligned offset from the
231bf215546Sopenharmony_ci * beginning of the blob's data, so some padding bytes may be added to the
232bf215546Sopenharmony_ci * blob if this write follows some unaligned write (such as
233bf215546Sopenharmony_ci * blob_write_string).
234bf215546Sopenharmony_ci *
235bf215546Sopenharmony_ci * \return True unless allocation failed.
236bf215546Sopenharmony_ci */
237bf215546Sopenharmony_cibool
238bf215546Sopenharmony_ciblob_write_uint32(struct blob *blob, uint32_t value);
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_ci/**
241bf215546Sopenharmony_ci * Overwrite a uint32_t previously written to the blob.
242bf215546Sopenharmony_ci *
243bf215546Sopenharmony_ci * Writes a uint32_t value to an existing portion of the blob at an offset of
244bf215546Sopenharmony_ci * \offset.  This data range must have previously been written to the blob by
245bf215546Sopenharmony_ci * one of the blob_write_* calls.
246bf215546Sopenharmony_ci *
247bf215546Sopenharmony_ci *
248bf215546Sopenharmony_ci * The expected usage is something like the following pattern:
249bf215546Sopenharmony_ci *
250bf215546Sopenharmony_ci *	size_t offset;
251bf215546Sopenharmony_ci *
252bf215546Sopenharmony_ci *	offset = blob_reserve_uint32(blob);
253bf215546Sopenharmony_ci *	... various blob write calls, writing N items ...
254bf215546Sopenharmony_ci *	blob_overwrite_uint32 (blob, offset, N);
255bf215546Sopenharmony_ci *
256bf215546Sopenharmony_ci * \return True unless the requested position or position+to_write lie outside
257bf215546Sopenharmony_ci * the current blob's size.
258bf215546Sopenharmony_ci */
259bf215546Sopenharmony_cibool
260bf215546Sopenharmony_ciblob_overwrite_uint32(struct blob *blob,
261bf215546Sopenharmony_ci                      size_t offset,
262bf215546Sopenharmony_ci                      uint32_t value);
263bf215546Sopenharmony_ci
264bf215546Sopenharmony_ci/**
265bf215546Sopenharmony_ci * Add a uint64_t to a blob.
266bf215546Sopenharmony_ci *
267bf215546Sopenharmony_ci * \note This function will only write to a uint64_t-aligned offset from the
268bf215546Sopenharmony_ci * beginning of the blob's data, so some padding bytes may be added to the
269bf215546Sopenharmony_ci * blob if this write follows some unaligned write (such as
270bf215546Sopenharmony_ci * blob_write_string).
271bf215546Sopenharmony_ci *
272bf215546Sopenharmony_ci * \return True unless allocation failed.
273bf215546Sopenharmony_ci */
274bf215546Sopenharmony_cibool
275bf215546Sopenharmony_ciblob_write_uint64(struct blob *blob, uint64_t value);
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci/**
278bf215546Sopenharmony_ci * Add an intptr_t to a blob.
279bf215546Sopenharmony_ci *
280bf215546Sopenharmony_ci * \note This function will only write to an intptr_t-aligned offset from the
281bf215546Sopenharmony_ci * beginning of the blob's data, so some padding bytes may be added to the
282bf215546Sopenharmony_ci * blob if this write follows some unaligned write (such as
283bf215546Sopenharmony_ci * blob_write_string).
284bf215546Sopenharmony_ci *
285bf215546Sopenharmony_ci * \return True unless allocation failed.
286bf215546Sopenharmony_ci */
287bf215546Sopenharmony_cibool
288bf215546Sopenharmony_ciblob_write_intptr(struct blob *blob, intptr_t value);
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_ci/**
291bf215546Sopenharmony_ci * Overwrite an intptr_t previously written to the blob.
292bf215546Sopenharmony_ci *
293bf215546Sopenharmony_ci * Writes a intptr_t value to an existing portion of the blob at an offset of
294bf215546Sopenharmony_ci * \offset.  This data range must have previously been written to the blob by
295bf215546Sopenharmony_ci * one of the blob_write_* calls.
296bf215546Sopenharmony_ci *
297bf215546Sopenharmony_ci * For example usage, see blob_overwrite_uint32
298bf215546Sopenharmony_ci *
299bf215546Sopenharmony_ci * \return True unless the requested position or position+to_write lie outside
300bf215546Sopenharmony_ci * the current blob's size.
301bf215546Sopenharmony_ci */
302bf215546Sopenharmony_cibool
303bf215546Sopenharmony_ciblob_overwrite_intptr(struct blob *blob,
304bf215546Sopenharmony_ci                      size_t offset,
305bf215546Sopenharmony_ci                      intptr_t value);
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_ci/**
308bf215546Sopenharmony_ci * Add a NULL-terminated string to a blob, (including the NULL terminator).
309bf215546Sopenharmony_ci *
310bf215546Sopenharmony_ci * \return True unless allocation failed.
311bf215546Sopenharmony_ci */
312bf215546Sopenharmony_cibool
313bf215546Sopenharmony_ciblob_write_string(struct blob *blob, const char *str);
314bf215546Sopenharmony_ci
315bf215546Sopenharmony_ci/**
316bf215546Sopenharmony_ci * Start reading a blob, (initializing the contents of \blob for reading).
317bf215546Sopenharmony_ci *
318bf215546Sopenharmony_ci * After this call, the caller can use the various blob_read_* functions to
319bf215546Sopenharmony_ci * read elements from the data array.
320bf215546Sopenharmony_ci *
321bf215546Sopenharmony_ci * For all of the blob_read_* functions, if there is insufficient data
322bf215546Sopenharmony_ci * remaining, the functions will do nothing, (perhaps returning default values
323bf215546Sopenharmony_ci * such as 0). The caller can detect this by noting that the blob_reader's
324bf215546Sopenharmony_ci * current value is unchanged before and after the call.
325bf215546Sopenharmony_ci */
326bf215546Sopenharmony_civoid
327bf215546Sopenharmony_ciblob_reader_init(struct blob_reader *blob, const void *data, size_t size);
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_ci/**
330bf215546Sopenharmony_ci * Align the current offset of the blob reader to the given alignment.
331bf215546Sopenharmony_ci *
332bf215546Sopenharmony_ci * This may be useful if you need the result of blob_read_bytes to have a
333bf215546Sopenharmony_ci * particular alignment.  Note that this only aligns relative to blob->data
334bf215546Sopenharmony_ci * and the alignment of the resulting pointer is only guaranteed if blob->data
335bf215546Sopenharmony_ci * is also aligned to the requested alignment.
336bf215546Sopenharmony_ci */
337bf215546Sopenharmony_civoid
338bf215546Sopenharmony_ciblob_reader_align(struct blob_reader *blob, size_t alignment);
339bf215546Sopenharmony_ci
340bf215546Sopenharmony_ci/**
341bf215546Sopenharmony_ci * Read some unstructured, fixed-size data from the current location, (and
342bf215546Sopenharmony_ci * update the current location to just past this data).
343bf215546Sopenharmony_ci *
344bf215546Sopenharmony_ci * \note The memory returned belongs to the data underlying the blob reader. The
345bf215546Sopenharmony_ci * caller must copy the data in order to use it after the lifetime of the data
346bf215546Sopenharmony_ci * underlying the blob reader.
347bf215546Sopenharmony_ci *
348bf215546Sopenharmony_ci * \return The bytes read (see note above about memory lifetime).
349bf215546Sopenharmony_ci */
350bf215546Sopenharmony_ciconst void *
351bf215546Sopenharmony_ciblob_read_bytes(struct blob_reader *blob, size_t size);
352bf215546Sopenharmony_ci
353bf215546Sopenharmony_ci/**
354bf215546Sopenharmony_ci * Read some unstructured, fixed-size data from the current location, copying
355bf215546Sopenharmony_ci * it to \dest (and update the current location to just past this data)
356bf215546Sopenharmony_ci */
357bf215546Sopenharmony_civoid
358bf215546Sopenharmony_ciblob_copy_bytes(struct blob_reader *blob, void *dest, size_t size);
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci/**
361bf215546Sopenharmony_ci * Skip \size bytes within the blob.
362bf215546Sopenharmony_ci */
363bf215546Sopenharmony_civoid
364bf215546Sopenharmony_ciblob_skip_bytes(struct blob_reader *blob, size_t size);
365bf215546Sopenharmony_ci
366bf215546Sopenharmony_ci/**
367bf215546Sopenharmony_ci * Read a uint8_t from the current location, (and update the current location
368bf215546Sopenharmony_ci * to just past this uint8_t).
369bf215546Sopenharmony_ci *
370bf215546Sopenharmony_ci * \return The uint8_t read
371bf215546Sopenharmony_ci */
372bf215546Sopenharmony_ciuint8_t
373bf215546Sopenharmony_ciblob_read_uint8(struct blob_reader *blob);
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_ci/**
376bf215546Sopenharmony_ci * Read a uint16_t from the current location, (and update the current location
377bf215546Sopenharmony_ci * to just past this uint16_t).
378bf215546Sopenharmony_ci *
379bf215546Sopenharmony_ci * \note This function will only read from a uint16_t-aligned offset from the
380bf215546Sopenharmony_ci * beginning of the blob's data, so some padding bytes may be skipped.
381bf215546Sopenharmony_ci *
382bf215546Sopenharmony_ci * \return The uint16_t read
383bf215546Sopenharmony_ci */
384bf215546Sopenharmony_ciuint16_t
385bf215546Sopenharmony_ciblob_read_uint16(struct blob_reader *blob);
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_ci/**
388bf215546Sopenharmony_ci * Read a uint32_t from the current location, (and update the current location
389bf215546Sopenharmony_ci * to just past this uint32_t).
390bf215546Sopenharmony_ci *
391bf215546Sopenharmony_ci * \note This function will only read from a uint32_t-aligned offset from the
392bf215546Sopenharmony_ci * beginning of the blob's data, so some padding bytes may be skipped.
393bf215546Sopenharmony_ci *
394bf215546Sopenharmony_ci * \return The uint32_t read
395bf215546Sopenharmony_ci */
396bf215546Sopenharmony_ciuint32_t
397bf215546Sopenharmony_ciblob_read_uint32(struct blob_reader *blob);
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_ci/**
400bf215546Sopenharmony_ci * Read a uint64_t from the current location, (and update the current location
401bf215546Sopenharmony_ci * to just past this uint64_t).
402bf215546Sopenharmony_ci *
403bf215546Sopenharmony_ci * \note This function will only read from a uint64_t-aligned offset from the
404bf215546Sopenharmony_ci * beginning of the blob's data, so some padding bytes may be skipped.
405bf215546Sopenharmony_ci *
406bf215546Sopenharmony_ci * \return The uint64_t read
407bf215546Sopenharmony_ci */
408bf215546Sopenharmony_ciuint64_t
409bf215546Sopenharmony_ciblob_read_uint64(struct blob_reader *blob);
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci/**
412bf215546Sopenharmony_ci * Read an intptr_t value from the current location, (and update the
413bf215546Sopenharmony_ci * current location to just past this intptr_t).
414bf215546Sopenharmony_ci *
415bf215546Sopenharmony_ci * \note This function will only read from an intptr_t-aligned offset from the
416bf215546Sopenharmony_ci * beginning of the blob's data, so some padding bytes may be skipped.
417bf215546Sopenharmony_ci *
418bf215546Sopenharmony_ci * \return The intptr_t read
419bf215546Sopenharmony_ci */
420bf215546Sopenharmony_ciintptr_t
421bf215546Sopenharmony_ciblob_read_intptr(struct blob_reader *blob);
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_ci/**
424bf215546Sopenharmony_ci * Read a NULL-terminated string from the current location, (and update the
425bf215546Sopenharmony_ci * current location to just past this string).
426bf215546Sopenharmony_ci *
427bf215546Sopenharmony_ci * \note The memory returned belongs to the data underlying the blob reader. The
428bf215546Sopenharmony_ci * caller must copy the string in order to use the string after the lifetime
429bf215546Sopenharmony_ci * of the data underlying the blob reader.
430bf215546Sopenharmony_ci *
431bf215546Sopenharmony_ci * \return The string read (see note above about memory lifetime). However, if
432bf215546Sopenharmony_ci * there is no NULL byte remaining within the blob, this function returns
433bf215546Sopenharmony_ci * NULL.
434bf215546Sopenharmony_ci */
435bf215546Sopenharmony_cichar *
436bf215546Sopenharmony_ciblob_read_string(struct blob_reader *blob);
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_ci#ifdef __cplusplus
439bf215546Sopenharmony_ci}
440bf215546Sopenharmony_ci#endif
441bf215546Sopenharmony_ci
442bf215546Sopenharmony_ci#endif /* BLOB_H */
443