BitVecFromBytes
Description
Build a bitvector from raw bytes using an explicit allocator.
Parameters
| Name | Direction | Description |
|---|---|---|
bytes |
in | Source byte buffer. |
bit_len |
in | Number of bits to read from bytes. |
alloc |
in | Allocator to bind to the returned bitvector. |
Success
Returns initialized bitvector.
Failure
Returns an empty bitvector if allocation fails.
Usage example (Cross-references)
Usage examples (Cross-references)
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecFromBytes\n");
// Create byte array
// Test bytes to bitvec with 0 bits (should return empty bitvector)
u8 empty_bytes[1] = {0x05};
BitVec bv2 = BitVecFromBytes(empty_bytes, 0, ALLOCATOR_OF(&alloc)); // 0 bits
result = result && (BitVecLen(&bv2) == 0);
BitVecDeinit(&bv2); // Test single byte
u8 single_byte[1] = {0xFF};
BitVec bv3 = BitVecFromBytes(single_byte, 8, ALLOCATOR_OF(&alloc)); // 8 bits from 1 byte
result = result && (BitVecLen(&bv3) == 8);
BitVecDeinit(&bv3);
for (size i = 0; i < sizeof(test_bytes); i++) {
BitVec bv = BitVecFromBytes(&test_bytes[i], 8, ALLOCATOR_OF(&alloc));
u8 recovered_byte = 0;
u64 written = BitVecToBytes(&bv, &recovered_byte, 1); BitVec bv1 = BitVecFromStr("11010110", ALLOCATOR_OF(&alloc));
BitVec bv2 = BitVecFromInteger(0xD6, 8, ALLOCATOR_OF(&alloc)); // Assuming MSB-first: 11010110 = 0xD6
BitVec bv3 = BitVecFromBytes((u8[]) {0xD6}, 8, ALLOCATOR_OF(&alloc));
// All three should produce the same result when converted back
// Test round-trip from bytes
BitVec recovered_bv = BitVecFromBytes(large_bytes, 1000, ALLOCATOR_OF(&alloc));
result = result && (BitVecLen(&recovered_bv) == 1000); // Test fromBytes with 0 bit length - should return empty bitvec
u8 dummy_bytes[1] = {0xFF};
BitVec empty_bv = BitVecFromBytes(dummy_bytes, 0, ALLOCATOR_OF(&alloc));
bool result = (BitVecLen(&empty_bv) == 0);
BitVecDeinit(&empty_bv);
// Test NULL bytes - should abort
BitVecFromBytes(NULL, 8, ALLOCATOR_OF(&alloc)); // NULL bytes, 8 bits
DefaultAllocatorDeinit(&alloc);
Last updated on