Skip to content

BufData

Description

Pointer to the contiguous byte storage backing b. Read-only at the macro level; the bytes themselves are mutable through this pointer. Invalidated by any growth (BufReserve, BufResize, BufPushByte, BufPushBytes).

Usage example (Cross-references)

Usage examples (Cross-references)
                ok = false;
            } else if (StrLen(&tmp)) {
                MemCopy(BufData(out) + offset, StrBegin(&tmp), StrLen(&tmp));
            }
        }
        const MachoSection *str_sec    = MachoFindSection(&e->dsym, "__DWARF", "__debug_str");
    
        const u8 *info_b   = info_sec ? BufData(&e->dsym.data) + info_sec->offset : NULL;
        u64       info_n   = info_sec ? info_sec->size : 0;
        const u8 *abbrev_b = abbrev_sec ? BufData(&e->dsym.data) + abbrev_sec->offset : NULL;
        const u8 *info_b   = info_sec ? BufData(&e->dsym.data) + info_sec->offset : NULL;
        u64       info_n   = info_sec ? info_sec->size : 0;
        const u8 *abbrev_b = abbrev_sec ? BufData(&e->dsym.data) + abbrev_sec->offset : NULL;
        u64       abbrev_n = abbrev_sec ? abbrev_sec->size : 0;
        const u8 *str_b    = str_sec ? BufData(&e->dsym.data) + str_sec->offset : NULL;
        const u8 *abbrev_b = abbrev_sec ? BufData(&e->dsym.data) + abbrev_sec->offset : NULL;
        u64       abbrev_n = abbrev_sec ? abbrev_sec->size : 0;
        const u8 *str_b    = str_sec ? BufData(&e->dsym.data) + str_sec->offset : NULL;
        u64       str_n    = str_sec ? str_sec->size : 0;
            return false;
        }
        const u8 *str_base = BufData(&ctx->out->data) + ctx->stroff;
        BufIter   tab      = BufIterFromBuf(&ctx->out->data);
        IterMustMove(&tab, ctx->symoff);
        // read past the strtab. Scan forward; if no NUL is found inside
        // [idx, strtab_size), return an empty string.
        Zstr base = (Zstr)(BufData(&self->data) + strtab_offset);
        for (u64 p = idx; p < strtab_size; ++p) {
            if (base[p] == '\0') {
        }
    
        const u8 *id = BufData(&self->data);
        if (id[EI_MAG0] != ELF_MAG0 || id[EI_MAG1] != ELF_MAG1 || id[EI_MAG2] != ELF_MAG2 || id[EI_MAG3] != ELF_MAG3) {
            LOG_ERROR("Elf: bad magic");
            return;
        }
        const u8 *p   = BufData(&self->data) + note->offset;
        const u8 *end = p + note->size;
        if ((u64)(end - p) < 12)
            return;
        }
        Zstr base = (Zstr)(BufData(&self->data) + dl->offset);
        // filename runs up to (and including) the NUL; CRC follows in the
        // last 4 bytes of the section, after alignment padding.
        }
        // CRC is in the last 4 bytes.
        const u8 *crc_bytes = BufData(&self->data) + dl->offset + dl->size - 4;
        BufIter   crc_iter  = BufIterFromMemory(crc_bytes, 4);
        u32       crc;
        U64Vec pending_dir_offsets  = VecInitT(pending_dir_offsets, alloc);
    
        BufIter section_cur = BufIterFromMemory(BufData(&elf->data) + line_section->offset, line_section->size);
    
        bool ok = true;
        const ElfSection *str_sec    = ElfFindSection(elf, ".debug_str");
    
        const u8 *info_b   = info_sec ? BufData(&elf->data) + info_sec->offset : NULL;
        u64       info_n   = info_sec ? info_sec->size : 0;
        const u8 *abbrev_b = abbrev_sec ? BufData(&elf->data) + abbrev_sec->offset : NULL;
        const u8 *info_b   = info_sec ? BufData(&elf->data) + info_sec->offset : NULL;
        u64       info_n   = info_sec ? info_sec->size : 0;
        const u8 *abbrev_b = abbrev_sec ? BufData(&elf->data) + abbrev_sec->offset : NULL;
        u64       abbrev_n = abbrev_sec ? abbrev_sec->size : 0;
        const u8 *str_b    = str_sec ? BufData(&elf->data) + str_sec->offset : NULL;
        const u8 *abbrev_b = abbrev_sec ? BufData(&elf->data) + abbrev_sec->offset : NULL;
        u64       abbrev_n = abbrev_sec ? abbrev_sec->size : 0;
        const u8 *str_b    = str_sec ? BufData(&elf->data) + str_sec->offset : NULL;
        u64       str_n    = str_sec ? str_sec->size : 0;
        return DwarfFunctionsBuildFromSlices(out, info_b, info_n, abbrev_b, abbrev_n, str_b, str_n, alloc);
        if (off + self->block_size > BufLength(&self->data))
            return NULL;
        return BufData(&self->data) + off;
    }
            return false;
        }
        if (MemCompare(BufData(&self->data), kMsfMagic7, sizeof(kMsfMagic7)) != 0) {
            LOG_ERROR("PDB: bad MSF magic (not 7.00)");
            return false;
            return false;
        }
        BufIter sb = BufIterFromMemory(BufData(&self->data) + 32, BufLength(&self->data) - 32);
        u32     free_blk, num_blocks, unknown;
        if (!BufReadFmt(
            return false;
        }
        MemCopy(BufData(&copy), data, data_size);
        BufResize(&copy, (size)data_size);
        return PdbOpenFromMemory(out, &copy);
        out->eh_frame_addr = eh->addr;
    
        const u8 *section_data = BufData(&elf->data) + eh->offset;
    
        BufIter section_cur = BufIterFromMemory(section_data, eh->size);
            return false;
        }
        BufIter mz_iter = BufIterFromMemory(BufData(&ctx->out->data), 2);
        u16     mz;
        if (!BufReadU16LE(&mz_iter, &mz)) {
        u32     e_lfanew;
        BufIter c = BufIterFromMemory(
            BufData(&ctx->out->data) + DOS_E_LFANEW_OFFSET,
            BufLength(&ctx->out->data) - DOS_E_LFANEW_OFFSET
        );
    static bool pe_decode_nt(PeContext *ctx, u64 *out_opt_offset) {
        BufIter c =
            BufIterFromMemory(BufData(&ctx->out->data) + ctx->nt_offset, BufLength(&ctx->out->data) - ctx->nt_offset);
        u32 sig;
        if (!BufReadU32LE(&c, &sig) || sig != NT_SIGNATURE) {
        ctx->num_sections = num_sec;
        ctx->opt_hdr_size = size_opt;
        *out_opt_offset   = (u64)(c.data + c.pos - BufData(&ctx->out->data));
        return true;
    }
            return false;
        }
        BufIter c = BufIterFromMemory(BufData(&ctx->out->data) + opt_offset, ctx->opt_hdr_size);
    
        u16 magic;
    static bool pe_decode_sections(PeContext *ctx, u64 opt_offset) {
        u64     sec_offset = opt_offset + ctx->opt_hdr_size;
        BufIter c = BufIterFromMemory(BufData(&ctx->out->data) + sec_offset, BufLength(&ctx->out->data) - sec_offset);
    
        for (u32 i = 0; i < ctx->num_sections; ++i) {
        for (u32 i = 0; i < num_entries; ++i) {
            u64     entry_off = dir_offset + (u64)i * DEBUG_ENTRY_SIZE;
            BufIter c = BufIterFromMemory(BufData(&ctx->out->data) + entry_off, BufLength(&ctx->out->data) - entry_off);
            u32     charac, ts, type, sz, raddr, rptr;
            u16     ver_maj, ver_min;
            if (sz < 4 + 16 + 4 + 1)
                continue;
            BufIter cv_cur = BufIterFromMemory(BufData(&ctx->out->data) + rptr, sz);
            u32     cv_sig;
            if (!BufReadU32LE(&cv_cur, &cv_sig))
            return false;
        }
        MemCopy(BufData(&copy), data, data_size);
        BufResize(&copy, (size)data_size);
        return PeOpenFromMemory(out, &copy);
        BufWriteU8(&b, 0x42);
        BufWriteU8(&b, 0x43);
        ok = ok && BufLength(&b) == 2 && BufData(&b)[0] == 0x42 && BufData(&b)[1] == 0x43;
        BufClear(&b);
        ok = ok && BufLength(&b) == 0;
            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // u64 BE
        };
        bool ok = BufLength(&b) == sizeof(expect) && MemCompare(BufData(&b), expect, sizeof(expect)) == 0;
        BufDeinit(&b);
        DefaultAllocatorDeinit(&alloc);
        BufWriteULeb128(&b, 16384);
        const u8 expect_uleb[] = {0x00, 0x7F, 0x80, 0x01, 0x80, 0x80, 0x01};
        if (BufLength(&b) != sizeof(expect_uleb) || MemCompare(BufData(&b), expect_uleb, sizeof(expect_uleb)) != 0) {
            BufDeinit(&b);
            DefaultAllocatorDeinit(&alloc);
        BufWriteSLeb128(&b, -64);
        const u8 expect_sleb[] = {0x00, 0x7F, 0xC0, 0x00, 0x40};
        bool ok = BufLength(&b) == sizeof(expect_sleb) && MemCompare(BufData(&b), expect_sleb, sizeof(expect_sleb)) == 0;
    
        BufDeinit(&b);
        BufWriteZstr(&b, "hi");
        const u8 expect[] = {'h', 'i', 0};
        bool     ok       = BufLength(&b) == sizeof(expect) && MemCompare(BufData(&b), expect, sizeof(expect)) == 0;
        BufDeinit(&b);
        DefaultAllocatorDeinit(&alloc);
        bool     ok       = BufAppendFmt(&b, "{<2r}{>4r}", (u16)0xCAFE, (u32)0xDEADBEEF);
        const u8 expect[] = {0x99, 0xFE, 0xCA, 0xDE, 0xAD, 0xBE, 0xEF};
        ok                = ok && BufLength(&b) == sizeof(expect) && MemCompare(BufData(&b), expect, sizeof(expect)) == 0;
        BufDeinit(&b);
        DefaultAllocatorDeinit(&alloc);
        bool     ok       = BufWriteFmt(&b, "{<2r}", (u16)0x1234);
        const u8 expect[] = {0x34, 0x12};
        ok                = ok && BufLength(&b) == sizeof(expect) && MemCompare(BufData(&b), expect, sizeof(expect)) == 0;
        BufDeinit(&b);
        DefaultAllocatorDeinit(&alloc);
            0x11
        };
        ok = ok && BufLength(&b) == sizeof(expect) && MemCompare(BufData(&b), expect, sizeof(expect)) == 0;
    
        // Out-of-range patch must fail and leave the buf unchanged.
        // Out-of-range patch must fail and leave the buf unchanged.
        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 && !BufPatchFmt(&b, BufLength(&b), "{<2r}", (u16)0);
        ok = ok && BufLength(&b) == BufLength(&snapshot);
        ok = ok && MemCompare(BufData(&b), BufData(&snapshot), BufLength(&b)) == 0;
        BufDeinit(&snapshot);
        bool            match      = ok && BufLength(&buf) == sizeof(expected);
        for (u64 i = 0; match && i < sizeof(expected); ++i) {
            if (BufData(&buf)[i] != expected[i]) {
                match = false;
            }
        bool match = ok && BufLength(&no_dot) == BufLength(&w_dot);
        for (u64 i = 0; match && i < BufLength(&no_dot); ++i) {
            if (BufData(&no_dot)[i] != BufData(&w_dot)[i]) {
                match = false;
            }
Last updated on