vhlibmetacrc

vhlibmetacrc — PUSHK Metablock Hash Calculation Library

vhlibmetacrc is a lightweight C++ library designed for fast and efficient hash (CRC-16) calculation and validation of data metablocks. It features compile-time lookup table generation, automated disk-based caching of precomputed hash tables, and O(1) metadata validation.


Library Metadata


Key Features


Core Components

The library is divided into three main logical units:


API Reference

VHMetaBlockCRC16

| Method | Description | |——–|————-| | generate_table() | constexpr function that generates the 256-element CRC-16 lookup table at compile time. | | calculate(const uint8_t* data, size_t len, uint16_t initial) | Fast table-based CRC-16 calculation for a given data buffer. |

BlockMeta

| Method | Description | |——–|————-| | Generate(uint16_t blksize, uint8_t *blkdat) | Initializes metadata, calculates source CRC, and either loads or generates the cache file. | | crc(uint8_t ll, uint16_t pos, uint16_t blksize) const | Retrieves the precomputed CRC-16 value for a specific level (ll), position, and block size. |

BlockMetaCache

| Method | Description | |——–|————-| | init(uint16_t blen, uint8_t *bsrc) | Initializes the cache and generates/loads metadata for the source block. | | check(uint16_t srclen, uint16_t offs, uint8_t ll) const | Returns true if the specific segment CRC has been previously marked as valid. | | mark(uint16_t srclen, uint16_t offs, uint8_t ll) | Marks the specific segment CRC as valid in the internal sidx array. | | cleanmarks() | Resets all validation marks in the sidx array. |


Basic Usage Examples

Initialization and Validation

#include "vhmetablock_cache.hpp"
#include <iostream>
#include <vector>

int main() {
    // Sample data block
    std::vector<uint8_t> data = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
    uint16_t blksize = static_cast<uint16_t>(data.size());

    // Initialize the cache
    BlockMetaCache cache;
    if (!cache.init(blksize, data.data())) {
        std::cerr << "Failed to initialize BlockMetaCache" << std::endl;
        return 1;
    }

    uint16_t srclen = 4;
    uint16_t offs = 0;
    uint8_t ll = 1; // level/length descriptor

    // Check if this specific metablock segment was already processed
    if (cache.check(srclen, offs, ll)) {
        std::cout << "Segment already validated (cached)." << std::endl;
    } else {
        std::cout << "Segment not cached. Processing and marking..." << std::endl;
        // ... perform actual processing ...
        cache.mark(srclen, offs, ll);
    }

    return 0;
}

Data Format Specification

Cache File Naming

The library automatically generates a unique filename for the precomputed hash table based on the source block’s size and its overall CRC-16: metablock_<srcfile_size_hex>_<srcfile_crc_hex>.metacrc16

Cache File Structure


Integration & Requirements

Requirements

Configuration

#define CFG_DISABLE_CRC_CACHE 1
#include "vhmetablock_cache.hpp"

License

This project is licensed under the MIT License — see the source headers or the LICENSE file for details.

Copyright (c) 2026 Viktor Glebov / V01G04A81