BufPatchFmt
Description
Overwrite existing bytes of buf starting at offset. The encoded output must fit within the current buf->length; the buffer is not grown. Useful for back-patching placeholder fields (lengths, checksums) after the payload they describe has been built.
Success
Returns true; bytes [offset, offset + written) of buf are replaced.
Failure
Returns false on format-parse error or if the encoded output would extend past buf->length. On overflow, buf is left unchanged.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Io.c:1122:
if (offset > BufLength(out) || StrLen(&tmp) > BufLength(out) - offset) {
LOG_ERROR(
"BufPatchFmt: write of {} bytes at offset {} exceeds buf length {}",
StrLen(&tmp),
offset,- In
Buf.c:185:
BufWriteU64LE(&b, 0x1122334455667788ull);
// Patch the placeholder with the real value.
bool ok = BufPatchFmt(&b, 0, "{<4r}", (u32)0xCAFEBABE);
const u8 expect[] = {
0xBE,- In
Buf.c:205:
Buf snapshot = BufInit(&alloc);
BufPushBytes(&snapshot, BufData(&b), BufLength(&b));
ok = ok && !BufPatchFmt(&b, BufLength(&b), "{<2r}", (u16)0);
ok = ok && BufLength(&b) == BufLength(&snapshot);
ok = ok && MemCompare(BufData(&b), BufData(&snapshot), BufLength(&b)) == 0;
Last updated on