xref: /aosp_15_r20/external/mesa3d/src/util/blob.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker  * Copyright © 2014 Intel Corporation
3*61046927SAndroid Build Coastguard Worker  *
4*61046927SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*61046927SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*61046927SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*61046927SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*61046927SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*61046927SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*61046927SAndroid Build Coastguard Worker  *
11*61046927SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*61046927SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*61046927SAndroid Build Coastguard Worker  * Software.
14*61046927SAndroid Build Coastguard Worker  *
15*61046927SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*61046927SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*61046927SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*61046927SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*61046927SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*61046927SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*61046927SAndroid Build Coastguard Worker  * IN THE SOFTWARE.
22*61046927SAndroid Build Coastguard Worker  */
23*61046927SAndroid Build Coastguard Worker 
24*61046927SAndroid Build Coastguard Worker #include <string.h>
25*61046927SAndroid Build Coastguard Worker 
26*61046927SAndroid Build Coastguard Worker #include "blob.h"
27*61046927SAndroid Build Coastguard Worker #include "u_math.h"
28*61046927SAndroid Build Coastguard Worker 
29*61046927SAndroid Build Coastguard Worker #ifdef HAVE_VALGRIND
30*61046927SAndroid Build Coastguard Worker #include <valgrind.h>
31*61046927SAndroid Build Coastguard Worker #include <memcheck.h>
32*61046927SAndroid Build Coastguard Worker #define VG(x) x
33*61046927SAndroid Build Coastguard Worker #else
34*61046927SAndroid Build Coastguard Worker #define VG(x)
35*61046927SAndroid Build Coastguard Worker #endif
36*61046927SAndroid Build Coastguard Worker 
37*61046927SAndroid Build Coastguard Worker #define BLOB_INITIAL_SIZE 4096
38*61046927SAndroid Build Coastguard Worker 
39*61046927SAndroid Build Coastguard Worker /* Ensure that \blob will be able to fit an additional object of size
40*61046927SAndroid Build Coastguard Worker  * \additional.  The growing (if any) will occur by doubling the existing
41*61046927SAndroid Build Coastguard Worker  * allocation.
42*61046927SAndroid Build Coastguard Worker  */
43*61046927SAndroid Build Coastguard Worker static bool
grow_to_fit(struct blob * blob,size_t additional)44*61046927SAndroid Build Coastguard Worker grow_to_fit(struct blob *blob, size_t additional)
45*61046927SAndroid Build Coastguard Worker {
46*61046927SAndroid Build Coastguard Worker    size_t to_allocate;
47*61046927SAndroid Build Coastguard Worker    uint8_t *new_data;
48*61046927SAndroid Build Coastguard Worker 
49*61046927SAndroid Build Coastguard Worker    if (blob->out_of_memory)
50*61046927SAndroid Build Coastguard Worker       return false;
51*61046927SAndroid Build Coastguard Worker 
52*61046927SAndroid Build Coastguard Worker    if (blob->size + additional <= blob->allocated)
53*61046927SAndroid Build Coastguard Worker       return true;
54*61046927SAndroid Build Coastguard Worker 
55*61046927SAndroid Build Coastguard Worker    if (blob->fixed_allocation) {
56*61046927SAndroid Build Coastguard Worker       blob->out_of_memory = true;
57*61046927SAndroid Build Coastguard Worker       return false;
58*61046927SAndroid Build Coastguard Worker    }
59*61046927SAndroid Build Coastguard Worker 
60*61046927SAndroid Build Coastguard Worker    if (blob->allocated == 0)
61*61046927SAndroid Build Coastguard Worker       to_allocate = BLOB_INITIAL_SIZE;
62*61046927SAndroid Build Coastguard Worker    else
63*61046927SAndroid Build Coastguard Worker       to_allocate = blob->allocated * 2;
64*61046927SAndroid Build Coastguard Worker 
65*61046927SAndroid Build Coastguard Worker    to_allocate = MAX2(to_allocate, blob->allocated + additional);
66*61046927SAndroid Build Coastguard Worker 
67*61046927SAndroid Build Coastguard Worker    new_data = realloc(blob->data, to_allocate);
68*61046927SAndroid Build Coastguard Worker    if (new_data == NULL) {
69*61046927SAndroid Build Coastguard Worker       blob->out_of_memory = true;
70*61046927SAndroid Build Coastguard Worker       return false;
71*61046927SAndroid Build Coastguard Worker    }
72*61046927SAndroid Build Coastguard Worker 
73*61046927SAndroid Build Coastguard Worker    blob->data = new_data;
74*61046927SAndroid Build Coastguard Worker    blob->allocated = to_allocate;
75*61046927SAndroid Build Coastguard Worker 
76*61046927SAndroid Build Coastguard Worker    return true;
77*61046927SAndroid Build Coastguard Worker }
78*61046927SAndroid Build Coastguard Worker 
79*61046927SAndroid Build Coastguard Worker /* Align the blob->size so that reading or writing a value at (blob->data +
80*61046927SAndroid Build Coastguard Worker  * blob->size) will result in an access aligned to a granularity of \alignment
81*61046927SAndroid Build Coastguard Worker  * bytes.
82*61046927SAndroid Build Coastguard Worker  *
83*61046927SAndroid Build Coastguard Worker  * \return True unless allocation fails
84*61046927SAndroid Build Coastguard Worker  */
85*61046927SAndroid Build Coastguard Worker bool
blob_align(struct blob * blob,size_t alignment)86*61046927SAndroid Build Coastguard Worker blob_align(struct blob *blob, size_t alignment)
87*61046927SAndroid Build Coastguard Worker {
88*61046927SAndroid Build Coastguard Worker    const size_t new_size = align_uintptr(blob->size, alignment);
89*61046927SAndroid Build Coastguard Worker 
90*61046927SAndroid Build Coastguard Worker    if (blob->size < new_size) {
91*61046927SAndroid Build Coastguard Worker       if (!grow_to_fit(blob, new_size - blob->size))
92*61046927SAndroid Build Coastguard Worker          return false;
93*61046927SAndroid Build Coastguard Worker 
94*61046927SAndroid Build Coastguard Worker       if (blob->data)
95*61046927SAndroid Build Coastguard Worker          memset(blob->data + blob->size, 0, new_size - blob->size);
96*61046927SAndroid Build Coastguard Worker       blob->size = new_size;
97*61046927SAndroid Build Coastguard Worker    }
98*61046927SAndroid Build Coastguard Worker 
99*61046927SAndroid Build Coastguard Worker    return true;
100*61046927SAndroid Build Coastguard Worker }
101*61046927SAndroid Build Coastguard Worker 
102*61046927SAndroid Build Coastguard Worker void
blob_reader_align(struct blob_reader * blob,size_t alignment)103*61046927SAndroid Build Coastguard Worker blob_reader_align(struct blob_reader *blob, size_t alignment)
104*61046927SAndroid Build Coastguard Worker {
105*61046927SAndroid Build Coastguard Worker    blob->current = blob->data + align_uintptr(blob->current - blob->data, alignment);
106*61046927SAndroid Build Coastguard Worker }
107*61046927SAndroid Build Coastguard Worker 
108*61046927SAndroid Build Coastguard Worker void
blob_init(struct blob * blob)109*61046927SAndroid Build Coastguard Worker blob_init(struct blob *blob)
110*61046927SAndroid Build Coastguard Worker {
111*61046927SAndroid Build Coastguard Worker    blob->data = NULL;
112*61046927SAndroid Build Coastguard Worker    blob->allocated = 0;
113*61046927SAndroid Build Coastguard Worker    blob->size = 0;
114*61046927SAndroid Build Coastguard Worker    blob->fixed_allocation = false;
115*61046927SAndroid Build Coastguard Worker    blob->out_of_memory = false;
116*61046927SAndroid Build Coastguard Worker }
117*61046927SAndroid Build Coastguard Worker 
118*61046927SAndroid Build Coastguard Worker void
blob_init_fixed(struct blob * blob,void * data,size_t size)119*61046927SAndroid Build Coastguard Worker blob_init_fixed(struct blob *blob, void *data, size_t size)
120*61046927SAndroid Build Coastguard Worker {
121*61046927SAndroid Build Coastguard Worker    blob->data = data;
122*61046927SAndroid Build Coastguard Worker    blob->allocated = size;
123*61046927SAndroid Build Coastguard Worker    blob->size = 0;
124*61046927SAndroid Build Coastguard Worker    blob->fixed_allocation = true;
125*61046927SAndroid Build Coastguard Worker    blob->out_of_memory = false;
126*61046927SAndroid Build Coastguard Worker }
127*61046927SAndroid Build Coastguard Worker 
128*61046927SAndroid Build Coastguard Worker void
blob_finish_get_buffer(struct blob * blob,void ** buffer,size_t * size)129*61046927SAndroid Build Coastguard Worker blob_finish_get_buffer(struct blob *blob, void **buffer, size_t *size)
130*61046927SAndroid Build Coastguard Worker {
131*61046927SAndroid Build Coastguard Worker    *buffer = blob->data;
132*61046927SAndroid Build Coastguard Worker    *size = blob->size;
133*61046927SAndroid Build Coastguard Worker    blob->data = NULL;
134*61046927SAndroid Build Coastguard Worker 
135*61046927SAndroid Build Coastguard Worker    /* Trim the buffer. */
136*61046927SAndroid Build Coastguard Worker    *buffer = realloc(*buffer, *size);
137*61046927SAndroid Build Coastguard Worker }
138*61046927SAndroid Build Coastguard Worker 
139*61046927SAndroid Build Coastguard Worker bool
blob_overwrite_bytes(struct blob * blob,size_t offset,const void * bytes,size_t to_write)140*61046927SAndroid Build Coastguard Worker blob_overwrite_bytes(struct blob *blob,
141*61046927SAndroid Build Coastguard Worker                      size_t offset,
142*61046927SAndroid Build Coastguard Worker                      const void *bytes,
143*61046927SAndroid Build Coastguard Worker                      size_t to_write)
144*61046927SAndroid Build Coastguard Worker {
145*61046927SAndroid Build Coastguard Worker    /* Detect an attempt to overwrite data out of bounds. */
146*61046927SAndroid Build Coastguard Worker    if (offset + to_write < offset || blob->size < offset + to_write)
147*61046927SAndroid Build Coastguard Worker       return false;
148*61046927SAndroid Build Coastguard Worker 
149*61046927SAndroid Build Coastguard Worker    VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
150*61046927SAndroid Build Coastguard Worker 
151*61046927SAndroid Build Coastguard Worker    if (blob->data)
152*61046927SAndroid Build Coastguard Worker       memcpy(blob->data + offset, bytes, to_write);
153*61046927SAndroid Build Coastguard Worker 
154*61046927SAndroid Build Coastguard Worker    return true;
155*61046927SAndroid Build Coastguard Worker }
156*61046927SAndroid Build Coastguard Worker 
157*61046927SAndroid Build Coastguard Worker bool
blob_write_bytes(struct blob * blob,const void * bytes,size_t to_write)158*61046927SAndroid Build Coastguard Worker blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write)
159*61046927SAndroid Build Coastguard Worker {
160*61046927SAndroid Build Coastguard Worker    if (! grow_to_fit(blob, to_write))
161*61046927SAndroid Build Coastguard Worker        return false;
162*61046927SAndroid Build Coastguard Worker 
163*61046927SAndroid Build Coastguard Worker    if (blob->data && to_write > 0) {
164*61046927SAndroid Build Coastguard Worker       VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
165*61046927SAndroid Build Coastguard Worker       memcpy(blob->data + blob->size, bytes, to_write);
166*61046927SAndroid Build Coastguard Worker    }
167*61046927SAndroid Build Coastguard Worker    blob->size += to_write;
168*61046927SAndroid Build Coastguard Worker 
169*61046927SAndroid Build Coastguard Worker    return true;
170*61046927SAndroid Build Coastguard Worker }
171*61046927SAndroid Build Coastguard Worker 
172*61046927SAndroid Build Coastguard Worker intptr_t
blob_reserve_bytes(struct blob * blob,size_t to_write)173*61046927SAndroid Build Coastguard Worker blob_reserve_bytes(struct blob *blob, size_t to_write)
174*61046927SAndroid Build Coastguard Worker {
175*61046927SAndroid Build Coastguard Worker    intptr_t ret;
176*61046927SAndroid Build Coastguard Worker 
177*61046927SAndroid Build Coastguard Worker    if (! grow_to_fit (blob, to_write))
178*61046927SAndroid Build Coastguard Worker       return -1;
179*61046927SAndroid Build Coastguard Worker 
180*61046927SAndroid Build Coastguard Worker    ret = blob->size;
181*61046927SAndroid Build Coastguard Worker    blob->size += to_write;
182*61046927SAndroid Build Coastguard Worker 
183*61046927SAndroid Build Coastguard Worker    return ret;
184*61046927SAndroid Build Coastguard Worker }
185*61046927SAndroid Build Coastguard Worker 
186*61046927SAndroid Build Coastguard Worker intptr_t
blob_reserve_uint32(struct blob * blob)187*61046927SAndroid Build Coastguard Worker blob_reserve_uint32(struct blob *blob)
188*61046927SAndroid Build Coastguard Worker {
189*61046927SAndroid Build Coastguard Worker    blob_align(blob, sizeof(uint32_t));
190*61046927SAndroid Build Coastguard Worker    return blob_reserve_bytes(blob, sizeof(uint32_t));
191*61046927SAndroid Build Coastguard Worker }
192*61046927SAndroid Build Coastguard Worker 
193*61046927SAndroid Build Coastguard Worker intptr_t
blob_reserve_intptr(struct blob * blob)194*61046927SAndroid Build Coastguard Worker blob_reserve_intptr(struct blob *blob)
195*61046927SAndroid Build Coastguard Worker {
196*61046927SAndroid Build Coastguard Worker    blob_align(blob, sizeof(intptr_t));
197*61046927SAndroid Build Coastguard Worker    return blob_reserve_bytes(blob, sizeof(intptr_t));
198*61046927SAndroid Build Coastguard Worker }
199*61046927SAndroid Build Coastguard Worker 
200*61046927SAndroid Build Coastguard Worker #define BLOB_WRITE_TYPE(name, type)                      \
201*61046927SAndroid Build Coastguard Worker bool                                                     \
202*61046927SAndroid Build Coastguard Worker name(struct blob *blob, type value)                      \
203*61046927SAndroid Build Coastguard Worker {                                                        \
204*61046927SAndroid Build Coastguard Worker    blob_align(blob, sizeof(value));                      \
205*61046927SAndroid Build Coastguard Worker    return blob_write_bytes(blob, &value, sizeof(value)); \
206*61046927SAndroid Build Coastguard Worker }
207*61046927SAndroid Build Coastguard Worker 
BLOB_WRITE_TYPE(blob_write_uint8,uint8_t)208*61046927SAndroid Build Coastguard Worker BLOB_WRITE_TYPE(blob_write_uint8, uint8_t)
209*61046927SAndroid Build Coastguard Worker BLOB_WRITE_TYPE(blob_write_uint16, uint16_t)
210*61046927SAndroid Build Coastguard Worker BLOB_WRITE_TYPE(blob_write_uint32, uint32_t)
211*61046927SAndroid Build Coastguard Worker BLOB_WRITE_TYPE(blob_write_uint64, uint64_t)
212*61046927SAndroid Build Coastguard Worker BLOB_WRITE_TYPE(blob_write_intptr, intptr_t)
213*61046927SAndroid Build Coastguard Worker 
214*61046927SAndroid Build Coastguard Worker #define ASSERT_ALIGNED(_offset, _align) \
215*61046927SAndroid Build Coastguard Worker    assert(align_uintptr((_offset), (_align)) == (_offset))
216*61046927SAndroid Build Coastguard Worker 
217*61046927SAndroid Build Coastguard Worker bool
218*61046927SAndroid Build Coastguard Worker blob_overwrite_uint8 (struct blob *blob,
219*61046927SAndroid Build Coastguard Worker                       size_t offset,
220*61046927SAndroid Build Coastguard Worker                       uint8_t value)
221*61046927SAndroid Build Coastguard Worker {
222*61046927SAndroid Build Coastguard Worker    ASSERT_ALIGNED(offset, sizeof(value));
223*61046927SAndroid Build Coastguard Worker    return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
224*61046927SAndroid Build Coastguard Worker }
225*61046927SAndroid Build Coastguard Worker 
226*61046927SAndroid Build Coastguard Worker bool
blob_overwrite_uint32(struct blob * blob,size_t offset,uint32_t value)227*61046927SAndroid Build Coastguard Worker blob_overwrite_uint32 (struct blob *blob,
228*61046927SAndroid Build Coastguard Worker                        size_t offset,
229*61046927SAndroid Build Coastguard Worker                        uint32_t value)
230*61046927SAndroid Build Coastguard Worker {
231*61046927SAndroid Build Coastguard Worker    ASSERT_ALIGNED(offset, sizeof(value));
232*61046927SAndroid Build Coastguard Worker    return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
233*61046927SAndroid Build Coastguard Worker }
234*61046927SAndroid Build Coastguard Worker 
235*61046927SAndroid Build Coastguard Worker bool
blob_overwrite_intptr(struct blob * blob,size_t offset,intptr_t value)236*61046927SAndroid Build Coastguard Worker blob_overwrite_intptr (struct blob *blob,
237*61046927SAndroid Build Coastguard Worker                        size_t offset,
238*61046927SAndroid Build Coastguard Worker                        intptr_t value)
239*61046927SAndroid Build Coastguard Worker {
240*61046927SAndroid Build Coastguard Worker    ASSERT_ALIGNED(offset, sizeof(value));
241*61046927SAndroid Build Coastguard Worker    return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
242*61046927SAndroid Build Coastguard Worker }
243*61046927SAndroid Build Coastguard Worker 
244*61046927SAndroid Build Coastguard Worker bool
blob_write_string(struct blob * blob,const char * str)245*61046927SAndroid Build Coastguard Worker blob_write_string(struct blob *blob, const char *str)
246*61046927SAndroid Build Coastguard Worker {
247*61046927SAndroid Build Coastguard Worker    return blob_write_bytes(blob, str, strlen(str) + 1);
248*61046927SAndroid Build Coastguard Worker }
249*61046927SAndroid Build Coastguard Worker 
250*61046927SAndroid Build Coastguard Worker void
blob_reader_init(struct blob_reader * blob,const void * data,size_t size)251*61046927SAndroid Build Coastguard Worker blob_reader_init(struct blob_reader *blob, const void *data, size_t size)
252*61046927SAndroid Build Coastguard Worker {
253*61046927SAndroid Build Coastguard Worker    blob->data = data;
254*61046927SAndroid Build Coastguard Worker    blob->end = blob->data + size;
255*61046927SAndroid Build Coastguard Worker    blob->current = data;
256*61046927SAndroid Build Coastguard Worker    blob->overrun = false;
257*61046927SAndroid Build Coastguard Worker }
258*61046927SAndroid Build Coastguard Worker 
259*61046927SAndroid Build Coastguard Worker /* Check that an object of size \size can be read from this blob.
260*61046927SAndroid Build Coastguard Worker  *
261*61046927SAndroid Build Coastguard Worker  * If not, set blob->overrun to indicate that we attempted to read too far.
262*61046927SAndroid Build Coastguard Worker  */
263*61046927SAndroid Build Coastguard Worker static bool
ensure_can_read(struct blob_reader * blob,size_t size)264*61046927SAndroid Build Coastguard Worker ensure_can_read(struct blob_reader *blob, size_t size)
265*61046927SAndroid Build Coastguard Worker {
266*61046927SAndroid Build Coastguard Worker    if (blob->overrun)
267*61046927SAndroid Build Coastguard Worker       return false;
268*61046927SAndroid Build Coastguard Worker 
269*61046927SAndroid Build Coastguard Worker    if (blob->current <= blob->end && blob->end - blob->current >= size)
270*61046927SAndroid Build Coastguard Worker       return true;
271*61046927SAndroid Build Coastguard Worker 
272*61046927SAndroid Build Coastguard Worker    blob->overrun = true;
273*61046927SAndroid Build Coastguard Worker 
274*61046927SAndroid Build Coastguard Worker    return false;
275*61046927SAndroid Build Coastguard Worker }
276*61046927SAndroid Build Coastguard Worker 
277*61046927SAndroid Build Coastguard Worker const void *
blob_read_bytes(struct blob_reader * blob,size_t size)278*61046927SAndroid Build Coastguard Worker blob_read_bytes(struct blob_reader *blob, size_t size)
279*61046927SAndroid Build Coastguard Worker {
280*61046927SAndroid Build Coastguard Worker    const void *ret;
281*61046927SAndroid Build Coastguard Worker 
282*61046927SAndroid Build Coastguard Worker    if (! ensure_can_read (blob, size))
283*61046927SAndroid Build Coastguard Worker       return NULL;
284*61046927SAndroid Build Coastguard Worker 
285*61046927SAndroid Build Coastguard Worker    ret = blob->current;
286*61046927SAndroid Build Coastguard Worker 
287*61046927SAndroid Build Coastguard Worker    blob->current += size;
288*61046927SAndroid Build Coastguard Worker 
289*61046927SAndroid Build Coastguard Worker    return ret;
290*61046927SAndroid Build Coastguard Worker }
291*61046927SAndroid Build Coastguard Worker 
292*61046927SAndroid Build Coastguard Worker void
blob_copy_bytes(struct blob_reader * blob,void * dest,size_t size)293*61046927SAndroid Build Coastguard Worker blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size)
294*61046927SAndroid Build Coastguard Worker {
295*61046927SAndroid Build Coastguard Worker    const void *bytes;
296*61046927SAndroid Build Coastguard Worker 
297*61046927SAndroid Build Coastguard Worker    bytes = blob_read_bytes(blob, size);
298*61046927SAndroid Build Coastguard Worker    if (bytes == NULL || size == 0)
299*61046927SAndroid Build Coastguard Worker       return;
300*61046927SAndroid Build Coastguard Worker 
301*61046927SAndroid Build Coastguard Worker    memcpy(dest, bytes, size);
302*61046927SAndroid Build Coastguard Worker }
303*61046927SAndroid Build Coastguard Worker 
304*61046927SAndroid Build Coastguard Worker void
blob_skip_bytes(struct blob_reader * blob,size_t size)305*61046927SAndroid Build Coastguard Worker blob_skip_bytes(struct blob_reader *blob, size_t size)
306*61046927SAndroid Build Coastguard Worker {
307*61046927SAndroid Build Coastguard Worker    if (ensure_can_read (blob, size))
308*61046927SAndroid Build Coastguard Worker       blob->current += size;
309*61046927SAndroid Build Coastguard Worker }
310*61046927SAndroid Build Coastguard Worker 
311*61046927SAndroid Build Coastguard Worker #define BLOB_READ_TYPE(name, type)         \
312*61046927SAndroid Build Coastguard Worker type                                       \
313*61046927SAndroid Build Coastguard Worker name(struct blob_reader *blob)             \
314*61046927SAndroid Build Coastguard Worker {                                          \
315*61046927SAndroid Build Coastguard Worker    type ret = 0;                           \
316*61046927SAndroid Build Coastguard Worker    int size = sizeof(ret);                 \
317*61046927SAndroid Build Coastguard Worker    blob_reader_align(blob, size);          \
318*61046927SAndroid Build Coastguard Worker    blob_copy_bytes(blob, &ret, size);      \
319*61046927SAndroid Build Coastguard Worker    return ret;                             \
320*61046927SAndroid Build Coastguard Worker }
321*61046927SAndroid Build Coastguard Worker 
BLOB_READ_TYPE(blob_read_uint8,uint8_t)322*61046927SAndroid Build Coastguard Worker BLOB_READ_TYPE(blob_read_uint8, uint8_t)
323*61046927SAndroid Build Coastguard Worker BLOB_READ_TYPE(blob_read_uint16, uint16_t)
324*61046927SAndroid Build Coastguard Worker BLOB_READ_TYPE(blob_read_uint32, uint32_t)
325*61046927SAndroid Build Coastguard Worker BLOB_READ_TYPE(blob_read_uint64, uint64_t)
326*61046927SAndroid Build Coastguard Worker BLOB_READ_TYPE(blob_read_intptr, intptr_t)
327*61046927SAndroid Build Coastguard Worker 
328*61046927SAndroid Build Coastguard Worker char *
329*61046927SAndroid Build Coastguard Worker blob_read_string(struct blob_reader *blob)
330*61046927SAndroid Build Coastguard Worker {
331*61046927SAndroid Build Coastguard Worker    int size;
332*61046927SAndroid Build Coastguard Worker    char *ret;
333*61046927SAndroid Build Coastguard Worker    uint8_t *nul;
334*61046927SAndroid Build Coastguard Worker 
335*61046927SAndroid Build Coastguard Worker    /* If we're already at the end, then this is an overrun. */
336*61046927SAndroid Build Coastguard Worker    if (blob->current >= blob->end) {
337*61046927SAndroid Build Coastguard Worker       blob->overrun = true;
338*61046927SAndroid Build Coastguard Worker       return NULL;
339*61046927SAndroid Build Coastguard Worker    }
340*61046927SAndroid Build Coastguard Worker 
341*61046927SAndroid Build Coastguard Worker    /* Similarly, if there is no zero byte in the data remaining in this blob,
342*61046927SAndroid Build Coastguard Worker     * we also consider that an overrun.
343*61046927SAndroid Build Coastguard Worker     */
344*61046927SAndroid Build Coastguard Worker    nul = memchr(blob->current, 0, blob->end - blob->current);
345*61046927SAndroid Build Coastguard Worker 
346*61046927SAndroid Build Coastguard Worker    if (nul == NULL) {
347*61046927SAndroid Build Coastguard Worker       blob->overrun = true;
348*61046927SAndroid Build Coastguard Worker       return NULL;
349*61046927SAndroid Build Coastguard Worker    }
350*61046927SAndroid Build Coastguard Worker 
351*61046927SAndroid Build Coastguard Worker    size = nul - blob->current + 1;
352*61046927SAndroid Build Coastguard Worker 
353*61046927SAndroid Build Coastguard Worker    assert(ensure_can_read(blob, size));
354*61046927SAndroid Build Coastguard Worker 
355*61046927SAndroid Build Coastguard Worker    ret = (char *) blob->current;
356*61046927SAndroid Build Coastguard Worker 
357*61046927SAndroid Build Coastguard Worker    blob->current += size;
358*61046927SAndroid Build Coastguard Worker 
359*61046927SAndroid Build Coastguard Worker    return ret;
360*61046927SAndroid Build Coastguard Worker }
361