Skip to content
IterRemainingLength

IterRemainingLength

Description

Elements remaining to read in the iteration direction. Returns 0 once the iterator is past the end (forward) or past the start (reverse).

Usage example (Cross-references)

Usage examples (Cross-references)
            }
            // Bounds check in iter space; pointer arithmetic stays inside the buffer.
            if (fmt_info.width > IterRemainingLength(iter)) {
                *iter = start;
                return false;
    
        for (u32 i = 0; i < ctx->ncmds; ++i) {
            u64 remaining = IterRemainingLength(&walker);
            if (remaining < 8) {
                LOG_ERROR("MachO: load command prefix truncated at {}", i);
    
        out->std_opcode_lengths_count = out->opcode_base ? (u64)(out->opcode_base - 1) : 0;
        if (IterRemainingLength(cur) < out->std_opcode_lengths_count)
            return false;
        out->standard_opcode_lengths = IterDataAt(cur, IterIndex(cur));
    
        bool ok = true;
        while (IterRemainingLength(&section_cur) > 0) {
            const u8 *unit_start  = IterDataAt(&section_cur, IterIndex(&section_cur));
            u32       unit_length = 0;
                break;
            }
            if (4u + (u64)unit_length > IterRemainingLength(&section_cur)) {
                ok = false;
                break;
    static bool parse_abbrev_table(BufIter cur, AbbrevTable *out, Allocator *alloc) {
        *out = VecInitT(*out, alloc);
        while (IterRemainingLength(&cur) > 0) {
            u64 code;
            if (!BufReadULeb128(&cur, &code)) {
        int depth = 0;
        for (;;) {
            if (IterRemainingLength(&cu_cur) == 0)
                return true;
    
        bool ok = true;
        while (IterRemainingLength(&info_cur) > 0) {
            size unit_start_pos = IterIndex(&info_cur);
            if (!BufReadULeb128(body, &aug_len))
                return false;
            if (aug_len > IterRemainingLength(body))
                return false;
            size aug_end_pos = IterIndex(body) + aug_len;
    
        out->initial_instructions      = IterDataAt(body, IterIndex(body));
        out->initial_instructions_size = IterRemainingLength(body);
        return true;
    }
            if (!BufReadULeb128(body, &aug_len))
                return false;
            if (aug_len > IterRemainingLength(body))
                return false;
            // Must-precondition: the `aug_len > IterRemainingLength` check
    
        out->instructions      = IterDataAt(body, IterIndex(body));
        out->instructions_size = IterRemainingLength(body);
        return true;
    }
    
        BufIter section_cur = BufIterFromMemory(section_data, eh->size);
        while (IterRemainingLength(&section_cur) > 0) {
            const u8 *rec_start = IterDataAt(&section_cur, IterIndex(&section_cur));
            u32       length32  = 0;
                break;
            }
            if (length32 > IterRemainingLength(&section_cur))
                break;
                if (!BufReadULeb128(cur, &expr_len))
                    return false;
                if (expr_len > IterRemainingLength(cur))
                    return false;
                // Must-precondition: the `expr_len > IterRemainingLength`
        BufIter cur      = BufIterFromMemory(insns, insns_size);
        bool    stop_now = false;
        while (IterRemainingLength(&cur) > 0) {
            if (!cfi_vm_step(vm, &cur, stop_at, &stop_now))
                return false;
    
        for (u32 i = 0; i < ctx->num_sections; ++i) {
            if (IterRemainingLength(&c) < 40) {
                LOG_ERROR("PE: section table truncated at index {}", i);
                return false;
                continue;
            }
            if (IterRemainingLength(&cv_cur) < 16 + 4)
                continue;
            // Same proof: 16 bytes are live.
        ok         = ok && BufReadU32BE(&it, &v32) && v32 == 0x12345678;
        ok         = ok && BufReadU64LE(&it, &v64) && v64 == 0xFEEDFACECAFEBEEFull;
        ok         = ok && IterRemainingLength(&it) == 0;
    
        BufDeinit(&b);
        bool    ok = BufReadULeb128(&it, &uv) && uv == 624485;
        ok         = ok && BufReadSLeb128(&it, &sv) && sv == -123456;
        ok         = ok && IterRemainingLength(&it) == 0;
    
        BufDeinit(&b);
        Zstr s2 = BufReadZstr(&it);
        bool    ok = s1 && s2 && s1[0] == 'h' && s2[0] == 'w';
        ok         = ok && IterRemainingLength(&it) == 0;
    
        BufDeinit(&b);
        bool    ok = BufReadFmt(&it, "{<2r}{>4r}{<8r}", v16, v32, v64);
        ok         = ok && v16 == 0x1234 && v32 == 0xDEADBEEF && v64 == 0x0102030405060708ull;
        ok         = ok && IterRemainingLength(&it) == 0;
    
        BufDeinit(&b);
        const u8 buf[3] = {1, 2, 3};
        BufIter  it     = BufIterFromMemory(buf, 3);
        if (IterRemainingLength(&it) != 3) {
            return false;
        }
        u8 v;
        IterRead(&it, &v);
        if (IterRemainingLength(&it) != 2) {
            return false;
        }
        IterRead(&it, &v);
        IterRead(&it, &v);
        return IterRemainingLength(&it) == 0;
    }
        const u8 buf[3] = {1, 2, 3};
        BufIter  it     = from_rev(buf, 3);
        if (IterRemainingLength(&it) != 3) {
            return false;
        }
        u8 v;
        IterRead(&it, &v);
        if (IterRemainingLength(&it) != 2) {
            return false;
        }
        IterRead(&it, &v);
        IterRead(&it, &v);
        return IterRemainingLength(&it) == 0;
    }
            return false;
        }
        return IterRemainingLength(&it) == 0;
    }
            return false;
        }
        return IterRemainingLength(&it) == 0;
    }
    /// TAGS: StrIter, Length, Remaining, Alias
    ///
    #define StrIterRemainingLength(mi) IterRemainingLength(mi)
    
    ///
    /// TAGS: Memory, Iter, Size
    ///
    #define IterRemainingSize(mi) (IterRemainingLength(mi) * ALIGN_UP(sizeof(ITER_DATA_TYPE(mi)), (mi)->alignment))
    
    ///
    ///
    #define IterPos(mi)                                                                                                    \
        (IterRemainingLength(mi) ?                                                                                         \
             (ITER_DATA_TYPE(mi) *)(((u64)(mi)->data) +                                                                    \
                                    (mi)->pos * ALIGN_UP(sizeof(ITER_DATA_TYPE(mi)), (mi)->alignment)) :                   \
    ///
    #define IterRead(mi, out)                                                                                              \
        (IterRemainingLength(mi) ? (*(out) = (mi)->data[(mi)->pos], (mi)->pos = (mi)->pos + (mi)->dir, true) : false)
    
    ///
Last updated on