/* Ensure that \blob will be able to fit an additional object of size * \additional. The growing (if any) will occur by doubling the existing * allocation.
*/ staticbool
grow_to_fit(struct blob *blob, size_t additional)
{
size_t to_allocate;
uint8_t *new_data;
if (blob->out_of_memory) returnfalse;
if (blob->size + additional <= blob->allocated) returntrue;
if (blob->fixed_allocation) {
blob->out_of_memory = true; returnfalse;
}
/* Align the blob->size so that reading or writing a value at (blob->data + * blob->size) will result in an access aligned to a granularity of \alignment * bytes. * * \return True unless allocation fails
*/ staticbool
align_blob(struct blob *blob, size_t alignment)
{ const size_t new_size = align64(blob->size, alignment);
if (blob->size < new_size) { if (!grow_to_fit(blob, new_size - blob->size)) returnfalse;
/* Check that an object of size \size can be read from this blob. * * If not, set blob->overrun to indicate that we attempted to read too far.
*/ staticbool
ensure_can_read(struct blob_reader *blob, size_t size)
{ if (blob->overrun) returnfalse;
/* These next three read functions have identical form. If we add any beyond * these first three we should probably switch to generating these with a * preprocessor macro.
*/
#define BLOB_READ_TYPE(name, type) \
type \
name(struct blob_reader *blob) \
{ \
type ret; \ int size = sizeof(ret); \
align_blob_reader(blob, size); \ if (! ensure_can_read(blob, size)) \ return 0; \
ret = *((type*) blob->current); \
blob->current += size; \ return ret; \
}
/* If we're already at the end, then this is an overrun. */ if (blob->current >= blob->end) {
blob->overrun = true; return NULL;
}
/* Similarly, if there is no zero byte in the data remaining in this blob, * we also consider that an overrun.
*/
nul = memchr(blob->current, 0, blob->end - blob->current);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.