Skip to content
BitVecRFindPattern

BitVecRFindPattern

Description

Search for a pattern starting from a specific position (reverse search).

Parameters

Name Direction Description
bv in Bitvector to search in
pattern in Pattern to search for
start in Position to start reverse search from

Usage example (from documentation)

  u64 index = BitVecRFindPattern(&flags, &pattern, 20);

Success

Index of occurrence, or SIZE_MAX if not found

Failure

Returns SIZE_MAX when start is out of range or no match exists at or before it.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    u64 BitVecRFindPattern(BitVec *bv, BitVec *pattern, u64 start) {
        ValidateBitVec(bv);
        ValidateBitVec(pattern);
        BitVecPush(&pattern, true);
    
        BitVecRFindPattern(NULL, &pattern, 0); // Should LOG_FATAL
    
        BitVecDeinit(&pattern);
        BitVecPush(&source, true);
    
        BitVecRFindPattern(&source, NULL, 0); // Should LOG_FATAL
    
        BitVecDeinit(&source);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecRFindPattern basic functionality\n");
    
        BitVec source  = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Search from index 8 backwards
        u64 pos = BitVecRFindPattern(&source, &pattern, 8);
        result  = result && (pos == 6); // Should find at position 6
    
        // Search from index 5 backwards
        pos    = BitVecRFindPattern(&source, &pattern, 5);
        result = result && (pos == 3); // Should find at position 3
        push_bits(&pattern, "101");
    
        bool result = (BitVecRFindPattern(&source, &pattern, 2) == 0);
    
        BitVecDeinit(&source);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecRFindPattern window-condition (ge_to_lt)\n");
    
        BitVec source  = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Narrow window for start=8 is [6,8], which has no match.
        u64 pos = BitVecRFindPattern(&source, &pattern, 8);
        result  = result && (pos == SIZE_MAX);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecRFindPattern window-condition (add_to_sub)\n");
    
        BitVec source  = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Real window for start=2 is [1,2]; no match there.
        u64 pos = BitVecRFindPattern(&source, &pattern, 2);
        result  = result && (pos == SIZE_MAX);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecRFindPattern window-value (add_to_sub)\n");
    
        BitVec source  = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Real window for start=4 is [2,4]; no match there.
        u64 pos = BitVecRFindPattern(&source, &pattern, 4);
        result  = result && (pos == SIZE_MAX);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecRFindPattern loop-seed (add_to_sub)\n");
    
        BitVec source  = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // start=6 lands exactly on a match.
        u64 pos = BitVecRFindPattern(&source, &pattern, 6);
        result  = result && (pos == 6);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecRFindPattern loop-guard (gt_to_ge)\n");
    
        BitVec source  = BitVecInit(ALLOCATOR_OF(&alloc));
        // start=3: search_end=1, window [1,3] has no match; the extra mutant
        // iteration would reach index 0.
        u64 pos = BitVecRFindPattern(&source, &pattern, 3);
        result  = result && (pos == SIZE_MAX);
Last updated on