Lines Matching refs:blob

27 #include "blob.h"
39 /* Ensure that \blob will be able to fit an additional object of size
44 grow_to_fit(struct blob *blob, size_t additional)
49 if (blob->out_of_memory)
52 if (blob->size + additional <= blob->allocated)
55 if (blob->fixed_allocation) {
56 blob->out_of_memory = true;
60 if (blob->allocated == 0)
63 to_allocate = blob->allocated * 2;
65 to_allocate = MAX2(to_allocate, blob->allocated + additional);
67 new_data = realloc(blob->data, to_allocate);
69 blob->out_of_memory = true;
73 blob->data = new_data;
74 blob->allocated = to_allocate;
79 /* Align the blob->size so that reading or writing a value at (blob->data +
80 * blob->size) will result in an access aligned to a granularity of \alignment
86 align_blob(struct blob *blob, size_t alignment)
88 const size_t new_size = ALIGN(blob->size, alignment);
90 if (blob->size < new_size) {
91 if (!grow_to_fit(blob, new_size - blob->size))
94 if (blob->data)
95 memset(blob->data + blob->size, 0, new_size - blob->size);
96 blob->size = new_size;
103 align_blob_reader(struct blob_reader *blob, size_t alignment)
105 blob->current = blob->data + ALIGN(blob->current - blob->data, alignment);
109 blob_init(struct blob *blob)
111 blob->data = NULL;
112 blob->allocated = 0;
113 blob->size = 0;
114 blob->fixed_allocation = false;
115 blob->out_of_memory = false;
119 blob_init_fixed(struct blob *blob, void *data, size_t size)
121 blob->data = data;
122 blob->allocated = size;
123 blob->size = 0;
124 blob->fixed_allocation = true;
125 blob->out_of_memory = false;
129 blob_overwrite_bytes(struct blob *blob,
135 if (offset + to_write < offset || blob->size < offset + to_write)
140 if (blob->data)
141 memcpy(blob->data + offset, bytes, to_write);
147 blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write)
149 if (! grow_to_fit(blob, to_write))
154 if (blob->data)
155 memcpy(blob->data + blob->size, bytes, to_write);
156 blob->size += to_write;
162 blob_reserve_bytes(struct blob *blob, size_t to_write)
166 if (! grow_to_fit (blob, to_write))
169 ret = blob->size;
170 blob->size += to_write;
176 blob_reserve_uint32(struct blob *blob)
178 align_blob(blob, sizeof(uint32_t));
179 return blob_reserve_bytes(blob, sizeof(uint32_t));
183 blob_reserve_intptr(struct blob *blob)
185 align_blob(blob, sizeof(intptr_t));
186 return blob_reserve_bytes(blob, sizeof(intptr_t));
190 blob_write_uint32(struct blob *blob, uint32_t value)
192 align_blob(blob, sizeof(value));
194 return blob_write_bytes(blob, &value, sizeof(value));
201 blob_overwrite_uint32 (struct blob *blob,
206 return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
210 blob_write_uint64(struct blob *blob, uint64_t value)
212 align_blob(blob, sizeof(value));
214 return blob_write_bytes(blob, &value, sizeof(value));
218 blob_write_intptr(struct blob *blob, intptr_t value)
220 align_blob(blob, sizeof(value));
222 return blob_write_bytes(blob, &value, sizeof(value));
226 blob_overwrite_intptr (struct blob *blob,
231 return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
235 blob_write_string(struct blob *blob, const char *str)
237 return blob_write_bytes(blob, str, strlen(str) + 1);
241 blob_reader_init(struct blob_reader *blob, const void *data, size_t size)
243 blob->data = data;
244 blob->end = blob->data + size;
245 blob->current = data;
246 blob->overrun = false;
249 /* Check that an object of size \size can be read from this blob.
251 * If not, set blob->overrun to indicate that we attempted to read too far.
254 ensure_can_read(struct blob_reader *blob, size_t size)
256 if (blob->overrun)
259 if (blob->current <= blob->end && blob->end - blob->current >= size)
262 blob->overrun = true;
268 blob_read_bytes(struct blob_reader *blob, size_t size)
272 if (! ensure_can_read (blob, size))
275 ret = blob->current;
277 blob->current += size;
283 blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size)
287 bytes = blob_read_bytes(blob, size);
295 blob_skip_bytes(struct blob_reader *blob, size_t size)
297 if (ensure_can_read (blob, size))
298 blob->current += size;
306 blob_read_uint32(struct blob_reader *blob)
311 align_blob_reader(blob, size);
313 if (! ensure_can_read(blob, size))
316 ret = *((uint32_t*) blob->current);
318 blob->current += size;
324 blob_read_uint64(struct blob_reader *blob)
329 align_blob_reader(blob, size);
331 if (! ensure_can_read(blob, size))
334 ret = *((uint64_t*) blob->current);
336 blob->current += size;
342 blob_read_intptr(struct blob_reader *blob)
347 align_blob_reader(blob, size);
349 if (! ensure_can_read(blob, size))
352 ret = *((intptr_t *) blob->current);
354 blob->current += size;
360 blob_read_string(struct blob_reader *blob)
367 if (blob->current >= blob->end) {
368 blob->overrun = true;
372 /* Similarly, if there is no zero byte in the data remaining in this blob,
375 nul = memchr(blob->current, 0, blob->end - blob->current);
378 blob->overrun = true;
382 size = nul - blob->current + 1;
384 assert(ensure_can_read(blob, size));
386 ret = (char *) blob->current;
388 blob->current += size;