### Error Handling and Version Check Example Source: https://context7.com/cyan4973/finitestateentropy/llms.txt This example demonstrates checking for compression errors using FSE_isError and HUF_isError, retrieving error messages with FSE_getErrorName and HUF_getErrorName, and printing the FSE library version. Ensure the destination buffer is sufficiently large to avoid 'dstSize_tooSmall' errors. ```c #include "fse.h" #include "huf.h" #include void error_handling_example(void) { char tiny[4]; /* intentionally too small */ const char* src = "some data that clearly will not fit"; size_t srcSize = 35; size_t result = FSE_compress(tiny, sizeof(tiny), src, srcSize); if (FSE_isError(result)) { /* Prints: FSE error: dstSize_tooSmall */ printf("FSE error: %s\n", FSE_getErrorName(result)); } /* Version check */ printf("FSE library version: %u (major=%u minor=%u release=%u)\n", FSE_versionNumber(), FSE_VERSION_MAJOR, FSE_VERSION_MINOR, FSE_VERSION_RELEASE); /* Output: FSE library version: 900 (major=0 minor=9 release=0) */ /* HUF error check */ size_t hResult = HUF_compress(tiny, sizeof(tiny), src, srcSize); if (HUF_isError(hResult)) printf("HUF error: %s\n", HUF_getErrorName(hResult)); } ``` -------------------------------- ### Error Handling and Version Check Example Source: https://context7.com/cyan4973/finitestateentropy/llms.txt This C code example demonstrates how to use `FSE_isError` and `FSE_getErrorName` to check for compression errors and print the corresponding error messages. It also shows how to check the FSE library version and perform a similar error check for HUF compression. ```APIDOC ## `FSE_isError` / `FSE_getErrorName` / `HUF_isError` / `HUF_getErrorName` All public API functions return a `size_t` result. Error codes are large values close to `SIZE_MAX`. Check with `FSE_isError()` or `HUF_isError()` and retrieve a human-readable description with the corresponding `getErrorName` function. ### Example Usage ```c #include "fse.h" #include "huf.h" #include void error_handling_example(void) { char tiny[4]; /* intentionally too small */ const char* src = "some data that clearly will not fit"; size_t srcSize = 35; /* FSE Compression Error Check */ size_t result = FSE_compress(tiny, sizeof(tiny), src, srcSize); if (FSE_isError(result)) { /* Prints: FSE error: dstSize_tooSmall */ printf("FSE error: %s\n", FSE_getErrorName(result)); } /* Version check */ printf("FSE library version: %u (major=%u minor=%u release=%u)\n", FSE_versionNumber(), FSE_VERSION_MAJOR, FSE_VERSION_MINOR, FSE_VERSION_RELEASE); /* Output: FSE library version: 900 (major=0 minor=9 release=0) */ /* HUF Compression Error Check */ size_t hResult = HUF_compress(tiny, sizeof(tiny), src, srcSize); if (HUF_isError(hResult)) printf("HUF error: %s\n", HUF_getErrorName(hResult)); } ``` ### Functions - **`FSE_isError(size_t code)`**: Returns true if the provided `size_t` code is an error code. - **`FSE_getErrorName(size_t code)`**: Returns a human-readable string description of the error code. - **`HUF_isError(size_t code)`**: Returns true if the provided `size_t` code is an error code. - **`HUF_getErrorName(size_t code)`**: Returns a human-readable string description of the error code. - **`FSE_versionNumber()`**: Returns the FSE library version number. - **`FSE_VERSION_MAJOR`**: Macro representing the major version of FSE. - **`FSE_VERSION_MINOR`**: Macro representing the minor version of FSE. - **`FSE_VERSION_RELEASE`**: Macro representing the release version of FSE. ``` -------------------------------- ### FSE_compressU16 / FSE_decompressU16 Roundtrip Example Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Demonstrates encoding and decoding a stream of unsigned short values using FSE_compressU16 and FSE_decompressU16. This function simulates a deflate literal/length symbol stream and verifies the roundtrip integrity. ```c #include "fseU16.h" #include #include #include int u16_roundtrip(void) { /* Simulate a deflate literal/length symbol stream */ unsigned short src[] = {104,101,108,108,111,32,119,111,114,108,100, /* "hello world" */ 256,257,258,256,257,258}; /* back-references */ size_t srcSize = sizeof(src) / sizeof(src[0]); /* element count */ size_t dstCapacity = FSE_compressBound(srcSize * 2) + 16; void* compressed = malloc(dstCapacity); size_t cSize = FSE_compressU16(compressed, dstCapacity, src, srcSize, 285, /* maxSymbolValue (deflate: 285) */ 10); /* tableLog */ if (FSE_isError(cSize)) { fprintf(stderr, "U16 compress error: %s\n", FSE_getErrorName(cSize)); free(compressed); return 1; } printf("U16 compressed %zu symbols -> %zu bytes\n", srcSize, cSize); unsigned short restored[32]; size_t dSize = FSE_decompressU16(restored, srcSize, compressed, cSize); if (FSE_isError(dSize)) { fprintf(stderr, "U16 decompress error: %s\n", FSE_getErrorName(dSize)); free(compressed); return 1; } printf("U16 roundtrip: %s\n", memcmp(src, restored, srcSize * sizeof(unsigned short)) == 0 ? "OK" : "FAIL"); /* Output: U16 compressed 18 symbols -> ~14 bytes U16 roundtrip: OK */ free(compressed); return 0; } ``` -------------------------------- ### Bitstream API Demo: Encode and Decode Bitfields Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Demonstrates the low-level reversible bit I/O using BIT_CStream_t and BIT_DStream_t. This example encodes five arbitrary bit-fields and then decodes them back in reverse order, showing the LIFO nature of the stream. ```c #include "bitstream.h" #include #include /* Encode 5 arbitrary bit-fields then decode them back in reverse */ void bitstream_demo(void) { unsigned char buf[64]; memset(buf, 0, sizeof(buf)); /* ---- Encode ---- */ BIT_CStream_t cs; size_t err = BIT_initCStream(&cs, buf, sizeof(buf)); if (ERR_isError(err)) { printf("initCStream failed\n"); return; } /* Fields added in "forward" logical order; decoded in reverse */ BIT_addBits(&cs, 0b10110, 5); BIT_flushBits(&cs); /* field A: 5 bits */ BIT_addBits(&cs, 0b1101, 4); BIT_flushBits(&cs); /* field B: 4 bits */ BIT_addBits(&cs, 0b111000, 6); BIT_flushBits(&cs); /* field C: 6 bits */ BIT_addBits(&cs, 42, 8); BIT_flushBits(&cs); /* field D: 8 bits */ BIT_addBits(&cs, 1, 1); BIT_flushBits(&cs); /* field E: 1 bit */ size_t streamSize = BIT_closeCStream(&cs); printf("Encoded %zu bytes\n", streamSize); /* ---- Decode (reverse order) ---- */ BIT_DStream_t ds; BIT_initDStream(&ds, buf, streamSize); size_t E = BIT_readBits(&ds, 1); BIT_reloadDStream(&ds); size_t D = BIT_readBits(&ds, 8); BIT_reloadDStream(&ds); size_t C = BIT_readBits(&ds, 6); BIT_reloadDStream(&ds); size_t B = BIT_readBits(&ds, 4); BIT_reloadDStream(&ds); size_t A = BIT_readBits(&ds, 5); BIT_reloadDStream(&ds); printf("A=%zu B=%zu C=%zu D=%zu E=%zu\n", A, B, C, D, E); printf("Stream exhausted: %s\n", BIT_endOfDStream(&ds) ? "yes" : "no"); /* Output: Encoded 4 bytes A=22 B=13 C=56 D=42 E=1 Stream exhausted: yes */ } ``` -------------------------------- ### Compress Multiple Blocks with Reused CTable Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Builds a single FSE_CTable from the first block's frequency distribution and reuses it for subsequent blocks. This avoids repeated table-building overhead when blocks share statistical profiles. ```c #include "fse.h" #include "hist.h" #include #include #include #define MAX_SYMBOL 255 #define TABLE_LOG 11 int compress_multi_block(const void** blocks, const size_t* sizes, int n) { unsigned count[MAX_SYMBOL + 1]; short normalizedCounter[MAX_SYMBOL + 1]; unsigned maxSymbol = MAX_SYMBOL; /* Step 1: compute histogram from first block */ size_t mostFreq = HIST_count(count, &maxSymbol, blocks[0], sizes[0]); if (HIST_isError(mostFreq)) return 1; /* Step 2: compute optimal tableLog */ unsigned tableLog = FSE_optimalTableLog(TABLE_LOG, sizes[0], maxSymbol); /* Step 3: normalize counts */ size_t err = FSE_normalizeCount(normalizedCounter, tableLog, count, sizes[0], maxSymbol); if (FSE_isError(err)) return 1; /* Step 4: build CTable */ FSE_CTable* ct = FSE_createCTable(maxSymbol, tableLog); err = FSE_buildCTable(ct, normalizedCounter, maxSymbol, tableLog); if (FSE_isError(err)) { FSE_freeCTable(ct); return 1; } /* Step 5: compress each block using the same CTable */ for (int i = 0; i < n; i++) { size_t dstCapacity = FSE_compressBound(sizes[i]); void* dst = malloc(dstCapacity); size_t cSize = FSE_compress_usingCTable(dst, dstCapacity, blocks[i], sizes[i], ct); if (FSE_isError(cSize)) fprintf(stderr, "Block %d error: %s\n", i, FSE_getErrorName(cSize)); else printf("Block %d: %zu -> %zu bytes\n", i, sizes[i], cSize); free(dst); } FSE_freeCTable(ct); return 0; } ``` -------------------------------- ### Static Allocation Macros for FSE CTable and DTable Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Use these macros to pre-allocate FSE compression (CTable) and decompression (DTable) tables on the stack or in static storage. This avoids dynamic memory allocation. Ensure FSE_STATIC_LINKING_ONLY is defined before including 'fse.h'. ```c #include "fse.h" /* requires FSE_STATIC_LINKING_ONLY */ #define MY_MAX_SYMBOL 127 #define MY_TABLE_LOG 9 /* Stack-allocated CTable */ FSE_CTable myCTable[FSE_CTABLE_SIZE_U32(MY_TABLE_LOG, MY_MAX_SYMBOL)]; /* Stack-allocated DTable */ FSE_DTable myDTable[FSE_DTABLE_SIZE_U32(MY_TABLE_LOG)]; void demo_static_tables(const void* src, size_t srcSize, void* dst, size_t dstCapacity) { /* Build CTable from normalized frequencies (obtained separately) */ short normCnt[MY_MAX_SYMBOL + 1]; /* ... populate normCnt ... */ FSE_buildCTable(myCTable, normCnt, MY_MAX_SYMBOL, MY_TABLE_LOG); size_t cSize = FSE_compress_usingCTable(dst, dstCapacity, src, srcSize, myCTable); /* Build DTable for decompression */ FSE_buildDTable(myDTable, normCnt, MY_MAX_SYMBOL, MY_TABLE_LOG); /* use FSE_decompress_usingDTable(...) */ (void)cSize; } ``` -------------------------------- ### Huffman Compression with External Workspace Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Compresses data using Huffman coding without internal heap allocation by utilizing a caller-supplied scratch buffer. Ensure the workspace is at least 6 KB + 256 B. ```c #include "huf.h" #include /* Statically allocated workspace — no heap required */ static uint32_t wksp[HUF_WORKSPACE_SIZE_U32]; size_t compress_no_alloc(void* dst, size_t dstCap, const void* src, size_t srcSize) { return HUF_compress4X_wksp(dst, dstCap, src, srcSize, 255, /* maxSymbolValue */ 11, /* tableLog */ wksp, sizeof(wksp)); } ``` -------------------------------- ### FSE Compression with External Workspace Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Uses an external scratch buffer for FSE compression, avoiding heap allocation. This is beneficial in memory-constrained environments or when malloc is unavailable. ```c #include "fse.h" /* FSE_STATIC_LINKING_ONLY for FSE_WKSP_SIZE_U32 */ #define MAX_SYM 255 #define MAX_TLOG 12 /* workspace large enough for any valid (maxSymbolValue, tableLog) pair */ static FSE_CTable wksp[FSE_WKSP_SIZE_U32(MAX_TLOG, MAX_SYM)]; size_t compress_no_malloc(void* dst, size_t dstCap, const void* src, size_t srcSize) { return FSE_compress_wksp(dst, dstCap, src, srcSize, MAX_SYM, MAX_TLOG, wksp, sizeof(wksp)); } ``` -------------------------------- ### HUF_compress2 - Huffman compression with explicit parameters Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Compresses data with explicit control over `maxSymbolValue` (≤ 255) and `tableLog` (≤ 12). This allows fine-tuning for specific data distributions, such as ASCII-only data. ```c #include "huf.h" #include /* ASCII-only data: symbol values 0–127, aggressive tableLog */ size_t compress_ascii(const void* src, size_t srcSize, void* dst, size_t dstCapacity) { unsigned maxSymbolValue = 127; unsigned tableLog = 11; /* HUF_TABLELOG_DEFAULT == 11 */ size_t cSize = HUF_compress2(dst, dstCapacity, src, srcSize, maxSymbolValue, tableLog); if (HUF_isError(cSize)) return 0; return cSize; } ``` -------------------------------- ### FSE_compress2 Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Performs FSE compression with explicit control over `maxSymbolValue` (alphabet size) and `tableLog` (precision for compression tables). `maxSymbolValue` should not exceed 255, and `tableLog` can range from 5 to 12, or be set to 0 for the default auto-selection. ```APIDOC ## FSE_compress2 — FSE compression with explicit parameters Same as `FSE_compress` but allows direct control over `maxSymbolValue` (alphabet size, max 255) and `tableLog` (precision, range 5–12; pass `0` for the auto-selected default). ### Method FSE_compress2 ### Parameters - **dst** (void*): Pointer to the destination buffer for compressed data. - **dstCapacity** (size_t): The maximum size of the destination buffer. - **src** (const void*): Pointer to the source buffer containing data to compress. - **srcSize** (size_t): The size of the source buffer. - **maxSymbolValue** (unsigned): The maximum symbol value (alphabet size) present in the input data (0-255). - **tableLog** (unsigned): The precision for the compression table (5-12), or 0 for default auto-selection. ### Return Value - **size_t**: The size of the compressed data, or an error code. ### Request Example ```c #include "fse.h" #include #include /* Compress bytes known to be in [0..15] with a small, fast table */ size_t compress_nibble_stream(const void* src, size_t srcSize, void* dst, size_t dstCapacity) { unsigned maxSymbolValue = 15; /* only nibble values present */ unsigned tableLog = 9; /* small table = fast & cache-friendly */ size_t cSize = FSE_compress2(dst, dstCapacity, src, srcSize, maxSymbolValue, tableLog); if (FSE_isError(cSize)) return 0; /* signal error to caller */ return cSize; } ``` ``` -------------------------------- ### HUF_compress2 Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Huffman compression with explicit control over maxSymbolValue and tableLog for fine-tuning. ```APIDOC ## HUF_compress2 ### Description Provides direct control over `maxSymbolValue` (≤ 255) and `tableLog` (≤ 12), enabling fine-tuning for known data distributions. ### Method `HUF_compress2` ### Parameters - `dst` (void*): Pointer to the destination buffer for compressed data. - `dstCapacity` (size_t): The maximum capacity of the destination buffer. - `src` (const void*): Pointer to the source buffer containing data to compress. - `srcSize` (size_t): The size of the source data in bytes. - `maxSymbolValue` (unsigned): The maximum symbol value (0-255). - `tableLog` (unsigned): The logarithm base 2 of the table size (≤ 12). ### Return Value - `size_t`: The size of the compressed data in bytes. - Error code: If an error occurred during compression. ### Request Example ```c #include "huf.h" #include /* ASCII-only data: symbol values 0–127, aggressive tableLog */ size_t compress_ascii(const void* src, size_t srcSize, void* dst, size_t dstCapacity) { unsigned maxSymbolValue = 127; unsigned tableLog = 11; /* HUF_TABLELOG_DEFAULT == 11 */ size_t cSize = HUF_compress2(dst, dstCapacity, src, srcSize, maxSymbolValue, tableLog); if (HUF_isError(cSize)) return 0; return cSize; } ``` ### Response Example (No specific response example provided for this function, but it returns the compressed size or an error code.) ``` -------------------------------- ### Advanced Huffman Compression Pipeline Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Compresses multiple blocks efficiently by building a Huffman compression table once and reusing it. This amortizes the table-build cost. Requires memory allocation for the compression table and output buffers. ```c #include "huf.h" #include "hist.h" /* HIST_count */ #include #include int huf_multi_block_compress(const void** blocks, const size_t* sizes, int n, void** outputs, size_t* outSizes) { unsigned count[256]; unsigned maxSym = 255; /* Build frequency table from first block */ size_t freq = HIST_count(count, &maxSym, blocks[0], sizes[0]); if (HIST_isError(freq)) return 1; /* Allocate and build CTable */ HUF_CElt* ctable = malloc(HUF_CTABLE_SIZE(maxSym)); size_t maxNbBits = HUF_buildCTable(ctable, count, maxSym, 0 /* auto tableLog */); if (HUF_isError(maxNbBits)) { free(ctable); return 1; } /* Compress each block with the shared CTable */ for (int i = 0; i < n; i++) { size_t dstCap = HUF_compressBound(sizes[i]); outputs[i] = malloc(dstCap); outSizes[i] = HUF_compress4X_usingCTable(outputs[i], dstCap, blocks[i], sizes[i], ctable); if (HUF_isError(outSizes[i])) fprintf(stderr, "Block %d error: %s\n", i, HUF_getErrorName(outSizes[i])); else printf("Block %d: %zu -> %zu bytes\n", i, sizes[i], outSizes[i]); } free(ctable); return 0; } ``` -------------------------------- ### FSE Compression with Explicit Parameters Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Compresses data using FSE with explicit control over the maximum symbol value and table precision. Useful for optimizing compression of specific data ranges, like nibble streams. ```c #include "fse.h" #include #include /* Compress bytes known to be in [0..15] with a small, fast table */ size_t compress_nibble_stream(const void* src, size_t srcSize, void* dst, size_t dstCapacity) { unsigned maxSymbolValue = 15; /* only nibble values present */ unsigned tableLog = 9; /* small table = fast & cache-friendly */ size_t cSize = FSE_compress2(dst, dstCapacity, src, srcSize, maxSymbolValue, tableLog); if (FSE_isError(cSize)) return 0; /* signal error to caller */ return cSize; } ``` -------------------------------- ### FSE Decompression with External Workspace Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Utilizes an external scratch buffer for FSE decompression, avoiding heap allocation. This is useful in memory-constrained environments or when malloc is unavailable. ```c /* Decompress with external workspace */ static FSE_DTable dWksp[FSE_DTABLE_SIZE_U32(MAX_TLOG)]; size_t decompress_no_malloc(void* dst, size_t dstCap, const void* src, size_t srcSize) { return FSE_decompress_wksp(dst, dstCap, src, srcSize, dWksp, MAX_TLOG); } ``` -------------------------------- ### Stack-Safe Histogram Counting with External Workspace Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Provides a stack-safe variant of `HIST_count` by using a caller-provided buffer for histogram calculations. Ensure the provided workspace is at least 4 KB. ```c #include "hist.h" #include static uint32_t histogram_wksp[HIST_WKSP_SIZE_U32]; size_t safe_histogram(unsigned* count, unsigned* maxSym, const void* src, size_t srcSize) { return HIST_count_wksp(count, maxSym, src, srcSize, histogram_wksp, sizeof(histogram_wksp)); } ``` -------------------------------- ### FSE_compress Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Compresses a byte buffer in a single call using the FSE algorithm. It returns the compressed size, or specific codes for incompressible data (0) or single-byte repetition (1). Error codes can be checked using FSE_isError. ```APIDOC ## FSE_compress — One-call FSE compression Compresses a byte buffer in a single call. Returns the compressed size, `0` if the source is incompressible (caller must store raw), `1` if the source is a single repeated byte (use RLE instead), or an error code testable with `FSE_isError`. ### Method FSE_compress ### Parameters - **dst** (void*): Pointer to the destination buffer for compressed data. - **dstCapacity** (size_t): The maximum size of the destination buffer. - **src** (const void*): Pointer to the source buffer containing data to compress. - **srcSize** (size_t): The size of the source buffer. ### Return Value - **size_t**: The size of the compressed data, `0` for incompressible data, `1` for single-symbol RLE data, or an error code. ### Request Example ```c #include "fse.h" #include #include #include int main(void) { const char* src = "aaaaabbbbbbbcccdddddddddddddddddeeeeeeeeeeeeeee"; size_t srcSize = strlen(src); size_t dstCapacity = FSE_compressBound(srcSize); void* dst = malloc(dstCapacity); size_t cSize = FSE_compress(dst, dstCapacity, src, srcSize); if (FSE_isError(cSize)) { fprintf(stderr, "Compression error: %s\n", FSE_getErrorName(cSize)); free(dst); return 1; } if (cSize == 0) { printf("Data is incompressible — store raw.\n"); free(dst); return 0; } if (cSize == 1) { printf("Single-symbol RLE — use memset.\n"); free(dst); return 0; } printf("Compressed %zu -> %zu bytes (%.1f%%)\n", srcSize, cSize, 100.0 * cSize / srcSize); free(dst); return 0; } /* Output: Compressed 46 -> ~20 bytes (43.5%) */ ``` ``` -------------------------------- ### HUF_compress Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Compresses a byte buffer using an optimized 4-stream Huffman codec. Returns the compressed size, 0 if incompressible, or an error code. ```APIDOC ## HUF_compress ### Description Compresses a byte buffer (max 128 KB per call) using an optimized 4-stream Huffman codec. Returns compressed size, `0` if incompressible, or an error code. ### Method `HUF_compress` ### Parameters - `dst` (void*): Pointer to the destination buffer for compressed data. - `dstCapacity` (size_t): The maximum capacity of the destination buffer. - `src` (const void*): Pointer to the source buffer containing data to compress. - `srcSize` (size_t): The size of the source data in bytes. ### Return Value - `size_t`: The size of the compressed data in bytes. - `0`: If the data is incompressible. - Error code: If an error occurred during compression. ### Request Example ```c #include "huf.h" #include #include #include int main(void) { const char* src = "the quick brown fox jumps over the lazy dog"; size_t srcSize = strlen(src); size_t dstCapacity = HUF_compressBound(srcSize); void* dst = malloc(dstCapacity); size_t cSize = HUF_compress(dst, dstCapacity, src, srcSize); if (HUF_isError(cSize)) { fprintf(stderr, "HUF error: %s\n", HUF_getErrorName(cSize)); free(dst); return 1; } printf("Compressed size: %zu\n", cSize); free(dst); return 0; } ``` ### Response Example ``` Compressed size: ~82 ``` ``` -------------------------------- ### Decompress Multiple Blocks with Reused DTable Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Serializes the normalized counter table once and reconstructs a DTable for fast decompression of many blocks. This is efficient when decompressing multiple blocks with the same statistical profile. ```c #include "fse.h" #include #include int decompress_with_dtable(const void* header, size_t headerSize, const void* cSrc, size_t cSrcSize, void* dst, size_t dstCapacity) { short normalizedCounter[256]; unsigned maxSymbolValue, tableLog; /* Step 1: read normalized counter from header */ size_t hSize = FSE_readNCount(normalizedCounter, &maxSymbolValue, &tableLog, header, headerSize); if (FSE_isError(hSize)) { fprintf(stderr, "Header read error: %s\n", FSE_getErrorName(hSize)); return -1; } /* Step 2: build DTable */ FSE_DTable* dt = FSE_createDTable(tableLog); size_t err = FSE_buildDTable(dt, normalizedCounter, maxSymbolValue, tableLog); if (FSE_isError(err)) { FSE_freeDTable(dt); return -1; } /* Step 3: decompress */ size_t dSize = FSE_decompress_usingDTable(dst, dstCapacity, cSrc, cSrcSize, dt); FSE_freeDTable(dt); if (FSE_isError(dSize)) { fprintf(stderr, "Decompress error: %s\n", FSE_getErrorName(dSize)); return -1; } return (int)dSize; } ``` -------------------------------- ### One-call FSE Compression Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Compresses a byte buffer in a single call using FSE. Handles incompressible and single-repeated-byte cases by returning specific values. Ensure sufficient destination capacity using FSE_compressBound. ```c #include "fse.h" #include #include #include int main(void) { const char* src = "aaaaabbbbbbbcccdddddddddddddddddeeeeeeeeeeeeeee"; size_t srcSize = strlen(src); size_t dstCapacity = FSE_compressBound(srcSize); void* dst = malloc(dstCapacity); size_t cSize = FSE_compress(dst, dstCapacity, src, srcSize); if (FSE_isError(cSize)) { fprintf(stderr, "Compression error: %s\n", FSE_getErrorName(cSize)); free(dst); return 1; } if (cSize == 0) { printf("Data is incompressible — store raw.\n"); free(dst); return 0; } if (cSize == 1) { printf("Single-symbol RLE — use memset.\n"); free(dst); return 0; } printf("Compressed %zu -> %zu bytes (%.1f%%)\n", srcSize, cSize, 100.0 * cSize / srcSize); free(dst); return 0; } /* Output: Compressed 46 -> ~20 bytes (43.5%) */ ``` -------------------------------- ### HUF_compress - One-call Huffman compression Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Compresses a byte buffer up to 128 KB using an optimized 4-stream Huffman codec. Returns the compressed size, 0 if incompressible, or an error code. Ensure the input size does not exceed HUF_BLOCKSIZE_MAX. ```c #include "huf.h" #include #include #include int main(void) { /* Text-like data with skewed byte distribution — good for Huffman */ const char* src = "the quick brown fox jumps over the lazy dog " "the quick brown fox jumps over the lazy dog " "the quick brown fox jumps over the lazy dog"; size_t srcSize = strlen(src); if (srcSize > HUF_BLOCKSIZE_MAX) { fprintf(stderr, "Too large\n"); return 1; } size_t dstCapacity = HUF_compressBound(srcSize); void* dst = malloc(dstCapacity); size_t cSize = HUF_compress(dst, dstCapacity, src, srcSize); if (HUF_isError(cSize)) { fprintf(stderr, "HUF error: %s\n", HUF_getErrorName(cSize)); free(dst); return 1; } if (cSize == 0) { printf("Incompressible — store raw.\n"); } else printf("Huff0: %zu -> %zu bytes (%.1f%%)\n", srcSize, cSize, 100.0 * cSize / srcSize); free(dst); return 0; } /* Output: Huff0: 132 -> ~82 bytes (62.1%) */ ``` -------------------------------- ### FSE_decompress Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Decompresses a previously FSE-compressed block. The caller must provide the original size, as it's not stored in the FSE headers. This function does not handle raw or RLE blocks, which must be managed separately by the caller. ```APIDOC ## FSE_decompress — One-call FSE decompression Decompresses a previously FSE-compressed block. The caller is responsible for storing the original size; FSE headers do not include it. Does **not** handle raw (incompressible) or RLE blocks — those must be detected and handled by the caller using `memcpy`/`memset`. ### Method FSE_decompress ### Parameters - **dst** (void*): Pointer to the destination buffer for decompressed data. - **dstSize** (size_t): The size of the original data (required for decompression). - **src** (const void*): Pointer to the compressed data buffer. - **srcSize** (size_t): The size of the compressed data buffer. ### Return Value - **size_t**: The size of the decompressed data, or an error code. ### Request Example ```c #include "fse.h" #include #include #include int roundtrip_example(void) { const char* original = "aaaaabbbbbbbcccdddddddddddddddddeeeeeeeeeeeeeee"; size_t originalSize = strlen(original); /* Compress */ size_t dstCapacity = FSE_compressBound(originalSize); void* compressed = malloc(dstCapacity); size_t cSize = FSE_compress(compressed, dstCapacity, original, originalSize); if (FSE_isError(cSize) || cSize == 0 || cSize == 1) { free(compressed); return 1; } /* Decompress */ void* restored = malloc(originalSize); size_t dSize = FSE_decompress(restored, originalSize, compressed, cSize); if (FSE_isError(dSize)) { fprintf(stderr, "Decompression error: %s\n", FSE_getErrorName(dSize)); free(compressed); free(restored); return 1; } printf("Restored %zu bytes. Match: %s\n", dSize, memcmp(original, restored, originalSize) == 0 ? "YES" : "NO"); /* Output: Restored 46 bytes. Match: YES */ free(compressed); free(restored); return 0; } ``` ``` -------------------------------- ### Symbol Frequency Counting with HIST_count Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Counts the occurrences of each byte value in a source buffer. Updates the maximum symbol value found and returns the count of the most frequent symbol. Handles potential errors from the counting process. ```c #include "hist.h" #include #include void show_top_symbols(const char* text) { unsigned count[256] = {0}; unsigned maxSym = 255; size_t srcSize = strlen(text); size_t mostFreqCnt = HIST_count(count, &maxSym, text, srcSize); if (HIST_isError(mostFreqCnt)) { printf("HIST error\n"); return; } printf("maxSymbol=%u, mostFrequentCount=%zu\n", maxSym, mostFreqCnt); for (unsigned s = 0; s <= maxSym; s++) { if (count[s] > 0) printf(" '%c' (0x%02x): %u\n", (s>=32&&s<127)?s:'.', s, count[s]); } } /* show_top_symbols("hello world") => maxSymbol=119, mostFrequentCount=3 ' ' (0x20): 1 'd' (0x64): 1 'e' (0x65): 1 'h' (0x68): 1 'l' (0x6c): 3 'o' (0x6f): 2 'r' (0x72): 1 'w' (0x77): 1 */ ``` -------------------------------- ### One-call FSE Decompression Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Decompresses a previously FSE-compressed block. The caller must provide the original size and handle raw/RLE blocks separately. This function does not handle those cases. ```c #include "fse.h" #include #include #include int roundtrip_example(void) { const char* original = "aaaaabbbbbbbcccdddddddddddddddddeeeeeeeeeeeeeee"; size_t originalSize = strlen(original); /* Compress */ size_t dstCapacity = FSE_compressBound(originalSize); void* compressed = malloc(dstCapacity); size_t cSize = FSE_compress(compressed, dstCapacity, original, originalSize); if (FSE_isError(cSize) || cSize == 0 || cSize == 1) { free(compressed); return 1; } /* Decompress */ void* restored = malloc(originalSize); size_t dSize = FSE_decompress(restored, originalSize, compressed, cSize); if (FSE_isError(dSize)) { fprintf(stderr, "Decompression error: %s\n", FSE_getErrorName(dSize)); free(compressed); free(restored); return 1; } printf("Restored %zu bytes. Match: %s\n", dSize, memcmp(original, restored, originalSize) == 0 ? "YES" : "NO"); /* Output: Restored 46 bytes. Match: YES */ free(compressed); free(restored); return 0; } ``` -------------------------------- ### HUF_decompress - One-call Huffman decompression Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Decompresses a Huff0-encoded block. This function handles RLE and uncompressed blocks internally when the exact original size is provided. Ensure the destination buffer is large enough to hold the original data. ```c #include "huf.h" #include #include #include int huf_roundtrip(void) { const char* original = "entropy entropy entropy entropy entropy"; size_t originalSize = strlen(original); /* Compress */ size_t dstCap = HUF_compressBound(originalSize); void* compressed = malloc(dstCap); size_t cSize = HUF_compress(compressed, dstCap, original, originalSize); if (HUF_isError(cSize) || cSize == 0) { free(compressed); return 1; } /* Decompress — must supply exact original size */ void* restored = malloc(originalSize); size_t dSize = HUF_decompress(restored, originalSize, compressed, cSize); if (HUF_isError(dSize)) { fprintf(stderr, "HUF decompress error: %s\n", HUF_getErrorName(dSize)); free(compressed); free(restored); return 1; } printf("HUF roundtrip OK: %s\n", memcmp(original, restored, originalSize) == 0 ? "PASS" : "FAIL"); /* Output: HUF roundtrip OK: PASS */ free(compressed); free(restored); return 0; } ``` -------------------------------- ### HUF_decompress Source: https://context7.com/cyan4973/finitestateentropy/llms.txt Decompresses a Huff0-encoded block. Handles RLE and uncompressed blocks internally when originalSize is provided. ```APIDOC ## HUF_decompress ### Description Decompresses a Huff0-encoded block. Unlike FSE, Huff0 also handles RLE and uncompressed blocks internally when `originalSize` is provided. ### Method `HUF_decompress` ### Parameters - `dst` (void*): Pointer to the destination buffer for decompressed data. - `dstCapacity` (size_t): The maximum capacity of the destination buffer, which should be equal to the original data size. - `src` (const void*): Pointer to the source buffer containing the compressed data. - `srcSize` (size_t): The size of the compressed data in bytes. ### Return Value - `size_t`: The size of the decompressed data in bytes (should match `dstCapacity`). - Error code: If an error occurred during decompression. ### Request Example ```c #include "huf.h" #include #include #include int huf_roundtrip(void) { const char* original = "entropy entropy entropy entropy entropy"; size_t originalSize = strlen(original); size_t dstCap = HUF_compressBound(originalSize); void* compressed = malloc(dstCap); size_t cSize = HUF_compress(compressed, dstCap, original, originalSize); if (HUF_isError(cSize) || cSize == 0) { free(compressed); return 1; } void* restored = malloc(originalSize); size_t dSize = HUF_decompress(restored, originalSize, compressed, cSize); if (HUF_isError(dSize)) { fprintf(stderr, "HUF decompress error: %s\n", HUF_getErrorName(dSize)); free(compressed); free(restored); return 1; } printf("HUF roundtrip OK: %s\n", memcmp(original, restored, originalSize) == 0 ? "PASS" : "FAIL"); free(compressed); free(restored); return 0; } ``` ### Response Example ``` HUF roundtrip OK: PASS ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.