Skip to content
BitVecPrefixMatch

BitVecPrefixMatch

Description

Match bitvector against an array of prefix patterns. Returns the index of the first matching prefix.

Parameters

Name Direction Description
bv in BitVec to check
patterns in Vec(BitVec) to check prefices against

Usage example (from documentation)

  BitVec prefixes[3] = { pattern1, pattern2, pattern3 };
  u64 match = BitVecPrefixMatch(&data, prefixes, 3);

Success

Index of matching pattern, or SIZE_MAX if no match

Failure

Returns SIZE_MAX when none of the candidate prefixes match bv.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    u64 BitVecPrefixMatch(BitVec *bv, BitVecs *patterns) {
        ValidateBitVec(bv);
        if (!patterns) {
    bool test_bitvec_prefix_match_null_source(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        WriteFmt("Testing BitVecPrefixMatch(NULL, patterns, 1) - should fatal\n");
        BitVecs vp = VecInitWithDeepCopy(NULL, BitVecDeinit, ALLOCATOR_OF(&alloc));
        BitVecPush(VecPtrAt(&vp, 0), true);
        BitVecs vp = VecInitWithDeepCopy(NULL, BitVecDeinit, ALLOCATOR_OF(&alloc));
        BitVecPush(VecPtrAt(&vp, 0), true);
        BitVecPrefixMatch(NULL, &vp);
        VecDeinit(&vp);
        DefaultAllocatorDeinit(&alloc);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecPrefixMatch(source, NULL, 1) - should fatal\n");
        BitVec source = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecPush(&source, true);
        BitVec source = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecPush(&source, true);
        BitVecPrefixMatch(&source, NULL);
        BitVecDeinit(&source);
        DefaultAllocatorDeinit(&alloc);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecPrefixMatch(NULL, empty) - should fatal\n");
    
        BitVecs patterns = VecInitWithDeepCopy(NULL, BitVecDeinit, ALLOCATOR_OF(&alloc));
        // Empty patterns: with validation present this aborts on the NULL bv;
        // with validation removed the empty loop falls through to SIZE_MAX.
        BitVecPrefixMatch(NULL, &patterns);
    
        VecDeinit(&patterns);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecPrefixMatch basic functionality\n");
    
        BitVec  source   = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecPush(p2, true);
    
        u64 match_idx = BitVecPrefixMatch(&source, &patterns);
        result        = result && (match_idx == 1);
Last updated on