diff --git a/ucm/store/compress/cc/compress_lib/bitstream.h b/ucm/store/compress/cc/compress_lib/bitstream.h deleted file mode 100644 index 9cc47aa48..000000000 --- a/ucm/store/compress/cc/compress_lib/bitstream.h +++ /dev/null @@ -1,464 +0,0 @@ -/* ****************************************************************** - * bitstream - * Part of FSE library - * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. - * - * You can contact the author at : - * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - ****************************************************************** */ -#ifndef BITSTREAM_H_MODULE -#define BITSTREAM_H_MODULE - -#if defined(__cplusplus) -extern "C" { -#endif - -/* - * This API consists of small unitary functions, which must be inlined for best performance. - * Since link-time-optimization is not available for all compilers, - * these functions are defined into a .h to be included. - */ - -/*-**************************************** - * Dependencies - ******************************************/ -#include "compiler.h" /* UNLIKELY() */ -#include "debug.h" /* assert(), DEBUGLOG(), RAWLOG() */ -#include "error_private.h" /* error codes and messages */ -#include "mem.h" /* unaligned access routines */ - -/*========================================= -* Target specific -=========================================*/ -#if defined(__BMI__) && defined(__GNUC__) -#include /* support for bextr (experimental) */ -#elif defined(__ICCARM__) -#include -#endif - -#define STREAM_ACCUMULATOR_MIN_32 25 -#define STREAM_ACCUMULATOR_MIN_64 57 -#define STREAM_ACCUMULATOR_MIN \ - ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64)) - -/*-****************************************** - * bitStream encoding API (write forward) - ********************************************/ -/* bitStream can mix input from multiple sources. - * A critical property of these streams is that they encode and decode in **reverse** direction. - * So the first bit sequence you add will be the last to be read, like a LIFO stack. - */ -typedef struct { - size_t bitContainer; - unsigned bitPos; - char* startPtr; - char* ptr; - char* endPtr; -} BIT_CStream_t; - -MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity); -MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits); -MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC); -MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC); - -/* Start with initCStream, providing the size of buffer to write into. - * bitStream will never write outside of this buffer. - * `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code. - * - * bits are first added to a local register. - * Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems. - * Writing data into memory is an explicit operation, performed by the flushBits function. - * Hence keep track how many bits are potentially stored into local register to avoid register - * overflow. After a flushBits, a maximum of 7 bits might still be stored into local register. - * - * Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream - * readers. - * - * Last operation is to close the bitStream. - * The function returns the final size of CStream in bytes. - * If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable) - */ - -/*-******************************************** - * bitStream decoding API (read backward) - **********************************************/ -typedef struct { - size_t bitContainer; - unsigned bitsConsumed; - const char* ptr; - const char* start; - const char* limitPtr; -} BIT_DStream_t; - -typedef enum { - BIT_DStream_unfinished = 0, - BIT_DStream_endOfBuffer = 1, - BIT_DStream_completed = 2, - BIT_DStream_overflow = 3 -} BIT_DStream_status; /* result of BIT_reloadDStream() */ -/* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */ - -MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize); -MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits); -MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD); -MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD); - -/* Start by invoking BIT_initDStream(). - * A chunk of the bitStream is then stored into a local register. - * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t). - * You can then retrieve bitFields stored into the local register, **in reverse order**. - * Local register is explicitly reloaded from memory by the BIT_reloadDStream() method. - * A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer)) -7) bits when its result is - * BIT_DStream_unfinished. Otherwise, it can be less than that, so proceed accordingly. Checking if - * DStream has reached its end can be performed with BIT_endOfDStream(). - */ - -/*-**************************************** - * unsafe API - ******************************************/ -MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits); -/* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */ - -MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC); -/* unsafe version; does not check buffer overflow */ - -MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits); -/* faster, but works only if nbBits >= 1 */ - -/*-************************************************************** - * Internal functions - ****************************************************************/ -MEM_STATIC unsigned BIT_highbit32(U32 val) -{ - assert(val != 0); - { -#if defined(_MSC_VER) /* Visual */ - unsigned long r = 0; - return _BitScanReverse(&r, val) ? static_cast(r) : 0; -#elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */ - return __builtin_clz(val) ^ 31; -#elif defined(__ICCARM__) /* IAR Intrinsic */ - return 31 - __CLZ(val); -#else /* Software version */ - static const unsigned DeBruijnClz[32] = {0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, - 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, - 24, 7, 19, 27, 23, 6, 26, 5, 4, 31}; - U32 v = val; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - return DeBruijnClz[static_cast(v * 0x07C4ACDDU) >> 27]; -#endif - } -} - -/*===== Local Constants =====*/ -static const unsigned BIT_mask[] = { - 0, 1, 3, 7, 0xF, 0x1F, 0x3F, - 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, - 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, - 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, - 0xFFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF}; /* up to 31 bits */ -#define BIT_MASK_SIZE (sizeof(BIT_mask) / sizeof(BIT_mask[0])) - -/*-************************************************************** - * bitStream encoding - ****************************************************************/ -/*! BIT_initCStream() : - * `dstCapacity` must be > sizeof(size_t) - * @return : 0 if success, - * otherwise an error code (can be tested using ERR_isError()) */ -MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* startPtr, size_t dstCapacity) -{ - bitC->bitContainer = 0; - bitC->bitPos = 0; - bitC->startPtr = (char*)startPtr; - bitC->ptr = bitC->startPtr; - bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->bitContainer); - if (dstCapacity <= sizeof(bitC->bitContainer)) { return ERROR(dstSize_tooSmall); } - return 0; -} - -/*! BIT_addBits() : - * can add up to 31 bits into `bitC`. - * Note : does not check for register overflow ! */ -MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits) -{ - MEM_STATIC_ASSERT(BIT_MASK_SIZE == 32); - assert(nbBits < BIT_MASK_SIZE); - assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8); - bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos; - bitC->bitPos += nbBits; -} - -/*! BIT_addBitsFast() : - * works only if `value` is _clean_, - * meaning all high bits above nbBits are 0 */ -MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits) -{ - assert((value >> nbBits) == 0); - assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8); - bitC->bitContainer |= value << bitC->bitPos; - bitC->bitPos += nbBits; -} - -/*! BIT_flushBitsFast() : - * assumption : bitContainer has not overflowed - * unsafe version; does not check buffer overflow */ -MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC) -{ - size_t const nbBytes = bitC->bitPos >> 3; - assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8); - assert(bitC->ptr <= bitC->endPtr); - MEM_writeLEST(bitC->ptr, bitC->bitContainer); - bitC->ptr += nbBytes; - bitC->bitPos &= 7; - bitC->bitContainer >>= nbBytes * 8; -} - -/*! BIT_flushBits() : - * assumption : bitContainer has not overflowed - * safe version; check for buffer overflow, and prevents it. - * note : does not signal buffer overflow. - * overflow will be revealed later on using BIT_closeCStream() */ -MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC) -{ - size_t const nbBytes = bitC->bitPos >> 3; - assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8); - assert(bitC->ptr <= bitC->endPtr); - MEM_writeLEST(bitC->ptr, bitC->bitContainer); - bitC->ptr += nbBytes; - if (bitC->ptr > bitC->endPtr) { bitC->ptr = bitC->endPtr; } - bitC->bitPos &= 7; - bitC->bitContainer >>= nbBytes * 8; -} - -/*! BIT_closeCStream() : - * @return : size of CStream, in bytes, - * or 0 if it could not fit into dstBuffer */ -MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC) -{ - BIT_addBitsFast(bitC, 1, 1); /* endMark */ - BIT_flushBits(bitC); - if (bitC->ptr >= bitC->endPtr) { return 0; } /* overflow detected */ - return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0); -} - -/*-******************************************************** - * bitStream decoding - **********************************************************/ -/*! BIT_initDStream() : - * Initialize a BIT_DStream_t. - * `bitD` : a pointer to an already allocated BIT_DStream_t structure. - * `srcSize` must be the *exact* size of the bitStream, in bytes. - * @return : size of stream (== srcSize), or an errorCode if a problem is detected - */ -MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize) -{ - if (srcSize < 1) { - memset(bitD, 0, sizeof(*bitD)); - return ERROR(srcSize_wrong); - } - - bitD->start = (const char*)srcBuffer; - bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer); - - if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */ - bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer); - bitD->bitContainer = MEM_readLEST(bitD->ptr); - { - BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize - 1]; - bitD->bitsConsumed = - lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */ - if (lastByte == 0) { return ERROR(GENERIC); } /* endMark not present */ - } - } else { - bitD->ptr = bitD->start; - bitD->bitContainer = *(const BYTE*)(bitD->start); - switch (srcSize) { - case 7: - bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) - << (sizeof(bitD->bitContainer) * 8 - 16); - /* fall-through */ - - case 6: - bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) - << (sizeof(bitD->bitContainer) * 8 - 24); - /* fall-through */ - - case 5: - bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) - << (sizeof(bitD->bitContainer) * 8 - 32); - /* fall-through */ - - case 4: - bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24; - /* fall-through */ - - case 3: - bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16; - /* fall-through */ - - case 2: - bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8; - /* fall-through */ - - default: break; - } - { - BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize - 1]; - bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; - if (lastByte == 0) { return ERROR(corruption_detected); } /* endMark not present */ - } - bitD->bitsConsumed += static_cast(sizeof(bitD->bitContainer) - srcSize) * 8; - } - - return srcSize; -} - -MEM_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start) -{ - return bitContainer >> start; -} - -MEM_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits) -{ - U32 const regMask = sizeof(bitContainer) * 8 - 1; - /* if start > regMask, bitstream is corrupted, and result is undefined */ - assert(nbBits < BIT_MASK_SIZE); - return (bitContainer >> (start & regMask)) & BIT_mask[nbBits]; -} - -MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits) -{ - assert(nbBits < BIT_MASK_SIZE); - return bitContainer & BIT_mask[nbBits]; -} - -/*! BIT_lookBits() : - * Provides next n bits from local register. - * local register is not modified. - * On 32-bits, maxNbBits==24. - * On 64-bits, maxNbBits==56. - * @return : value extracted */ -MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits) -{ - /* arbitrate between double-shift and shift+mask */ -#if 1 - /* if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8, - * bitstream is likely corrupted, and result is undefined */ - return BIT_getMiddleBits( - bitD->bitContainer, (sizeof(bitD->bitContainer) * 8) - bitD->bitsConsumed - nbBits, nbBits); -#else - /* this code path is slower on my os-x laptop */ - U32 const regMask = sizeof(bitD->bitContainer) * 8 - 1; - return ((bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> 1) >> - ((regMask - nbBits) & regMask); -#endif -} - -/*! BIT_lookBitsFast() : - * unsafe version; only works if nbBits >= 1 */ -MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits) -{ - U32 const regMask = sizeof(bitD->bitContainer) * 8 - 1; - assert(nbBits >= 1); - return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> - (((regMask + 1) - nbBits) & regMask); -} - -MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits) { bitD->bitsConsumed += nbBits; } - -/*! BIT_readBits() : - * Read (consume) next n bits from local register and update. - * Pay attention to not read more than nbBits contained into local register. - * @return : extracted value. */ -MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits) -{ - size_t const value = BIT_lookBits(bitD, nbBits); - BIT_skipBits(bitD, nbBits); - return value; -} - -/*! BIT_readBitsFast() : - * unsafe version; only works only if nbBits >= 1 */ -MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits) -{ - size_t const value = BIT_lookBitsFast(bitD, nbBits); - assert(nbBits >= 1); - BIT_skipBits(bitD, nbBits); - return value; -} - -/*! BIT_reloadDStreamFast() : - * Similar to BIT_reloadDStream(), but with two differences: - * 1. bitsConsumed <= sizeof(bitD->bitContainer)*8 must hold! - * 2. Returns BIT_DStream_overflow when bitD->ptr < bitD->limitPtr, at this - * point you must use BIT_reloadDStream() to reload. - */ -MEM_STATIC BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD) -{ - if (UNLIKELY(bitD->ptr < bitD->limitPtr)) { return BIT_DStream_overflow; } - assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer) * 8); - bitD->ptr -= bitD->bitsConsumed >> 3; - bitD->bitsConsumed &= 7; - bitD->bitContainer = MEM_readLEST(bitD->ptr); - return BIT_DStream_unfinished; -} - -/*! BIT_reloadDStream() : - * Refill `bitD` from buffer previously set in BIT_initDStream() . - * This function is safe, it guarantees it will not read beyond src buffer. - * @return : status of `BIT_DStream_t` internal register. - * when status == BIT_DStream_unfinished, internal register is filled with at least 25 or - * 57 bits */ -MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD) -{ - if (bitD->bitsConsumed > - (sizeof(bitD->bitContainer) * 8)) { /* overflow detected, like end of stream */ - return BIT_DStream_overflow; - } - - if (bitD->ptr >= bitD->limitPtr) { return BIT_reloadDStreamFast(bitD); } - if (bitD->ptr == bitD->start) { - if (bitD->bitsConsumed < sizeof(bitD->bitContainer) * 8) { return BIT_DStream_endOfBuffer; } - return BIT_DStream_completed; - } - /* start < ptr < limitPtr */ - { - U32 nbBytes = bitD->bitsConsumed >> 3; - BIT_DStream_status result = BIT_DStream_unfinished; - if (bitD->ptr - nbBytes < bitD->start) { - nbBytes = static_cast(bitD->ptr - bitD->start); /* ptr > start */ - result = BIT_DStream_endOfBuffer; - } - bitD->ptr -= nbBytes; - bitD->bitsConsumed -= nbBytes * 8; - bitD->bitContainer = - MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD->bitContainer), otherwise - bitD->ptr == bitD->start */ - return result; - } -} - -/*! BIT_endOfDStream() : - * @return : 1 if DStream has _exactly_ reached its end (all bits consumed). - */ -MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream) -{ - return ((DStream->ptr == DStream->start) && - (DStream->bitsConsumed == sizeof(DStream->bitContainer) * 8)); -} - -#if defined(__cplusplus) -} -#endif - -#endif /* BITSTREAM_H_MODULE */ diff --git a/ucm/store/compress/cc/compress_lib/compiler.h b/ucm/store/compress/cc/compress_lib/compiler.h deleted file mode 100644 index a6e2ca719..000000000 --- a/ucm/store/compress/cc/compress_lib/compiler.h +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - */ - -#ifndef ZSTD_COMPILER_H -#define ZSTD_COMPILER_H - -/*-******************************************************* - * Compiler specifics - *********************************************************/ -/* force inlining */ - -#if !defined(ZSTD_NO_INLINE) -#if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || \ - defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ -#define INLINE_KEYWORD inline -#else -#define INLINE_KEYWORD -#endif - -#if defined(__GNUC__) || defined(__ICCARM__) -#define FORCE_INLINE_ATTR __attribute__((always_inline)) -#elif defined(_MSC_VER) -#define FORCE_INLINE_ATTR __forceinline -#else -#define FORCE_INLINE_ATTR -#endif - -#else - -#define INLINE_KEYWORD -#define FORCE_INLINE_ATTR - -#endif - -/** - On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC). - This explicitly marks such functions as __cdecl so that the code will still compile - if a CC other than __cdecl has been made the default. -*/ -#if defined(_MSC_VER) -#define WIN_CDECL __cdecl -#else -#define WIN_CDECL -#endif - -/** - * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant - * parameters. They must be inlined for the compiler to eliminate the constant - * branches. - */ -#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR -/** - * HINT_INLINE is used to help the compiler generate better code. It is *not* - * used for "templates", so it can be tweaked based on the compilers - * performance. - * - * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the - * always_inline attribute. - * - * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline - * attribute. - */ -#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5 -#define HINT_INLINE static INLINE_KEYWORD -#else -#define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR -#endif - -/* UNUSED_ATTR tells the compiler it is okay if the function is unused. */ -#if defined(__GNUC__) -#define UNUSED_ATTR __attribute__((unused)) -#else -#define UNUSED_ATTR -#endif - -/* force no inlining */ -#ifdef _MSC_VER -#define FORCE_NOINLINE static __declspec(noinline) -#else -#if defined(__GNUC__) || defined(__ICCARM__) -#define FORCE_NOINLINE static __attribute__((__noinline__)) -#else -#define FORCE_NOINLINE static -#endif -#endif - -/* target attribute */ -#ifndef __has_attribute -#define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ -#endif -#if defined(__GNUC__) || defined(__ICCARM__) -#define TARGET_ATTRIBUTE(target) __attribute__((__target__(target))) -#else -#define TARGET_ATTRIBUTE(target) -#endif - -/* Enable runtime BMI2 dispatch based on the CPU. - * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default. - */ -#ifndef DYNAMIC_BMI2 -#if ((defined(__clang__) && __has_attribute(__target__)) || \ - (defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) && \ - (defined(__x86_64__) || defined(_M_X86)) && !defined(__BMI2__) -#define DYNAMIC_BMI2 1 -#else -#define DYNAMIC_BMI2 0 -#endif -#endif - -/* prefetch - * can be disabled, by declaring NO_PREFETCH build macro */ -#if defined(NO_PREFETCH) -#define PREFETCH_L1(ptr) (void)(ptr) /* disabled */ -#define PREFETCH_L2(ptr) (void)(ptr) /* disabled */ -#else -#if defined(_MSC_VER) && \ - (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */ -#include /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */ -#define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0) -#define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1) -#elif defined(__aarch64__) -#define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr))) -#define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr))) -#elif defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))) -#define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */) -#define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */) -#else -#define PREFETCH_L1(ptr) (void)(ptr) /* disabled */ -#define PREFETCH_L2(ptr) (void)(ptr) /* disabled */ -#endif -#endif /* NO_PREFETCH */ - -#define CACHELINE_SIZE 64 - -#define PREFETCH_AREA(p, s) \ - { \ - const char* const _ptr = (const char*)(p); \ - size_t const _size = (size_t)(s); \ - size_t _pos; \ - for (_pos = 0; _pos < _size; _pos += CACHELINE_SIZE) { PREFETCH_L2(_ptr + _pos); } \ - } - -/* vectorization - * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */ -#if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) -#if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5) -#define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize"))) -#else -#define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")") -#endif -#else -#define DONT_VECTORIZE -#endif - -/* Tell the compiler that a branch is likely or unlikely. - * Only use these macros if it causes the compiler to generate better code. - * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc - * and clang, please do. - */ -#if defined(__GNUC__) -#define LIKELY(x) (__builtin_expect((x), 1)) -#define UNLIKELY(x) (__builtin_expect((x), 0)) -#else -#define LIKELY(x) (x) -#define UNLIKELY(x) (x) -#endif - -/* disable warnings */ -#ifdef _MSC_VER /* Visual Studio */ -#include /* For Visual 2005 */ -#pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */ -#pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ -#pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ -#pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */ -#pragma warning(disable : 4324) /* disable: C4324: padded structure */ -#endif - -#endif /* ZSTD_COMPILER_H */ diff --git a/ucm/store/compress/cc/compress_lib/compress_types.h b/ucm/store/compress/cc/compress_lib/compress_types.h new file mode 100644 index 000000000..e519291cb --- /dev/null +++ b/ucm/store/compress/cc/compress_lib/compress_types.h @@ -0,0 +1,22 @@ +#ifndef COMPRESS_TYPES_H +#define COMPRESS_TYPES_H + +typedef enum { + R1 = 32, + R133 = 24, // 32 / 24 = 1.33x + R139 = 23, // 32 / 23 = 1.39x + R145 = 22, // 32 / 22 = 1.45x + R152 = 21, // 32 / 21 = 1.52x + R160 = 20, // 32 / 21 = 1.60x + R200 = 16 // 32 / 16 = 2.00x +} FixedRatio; + +typedef enum { + DT_BF16 = 0, + DT_FP16 = 1, + DT_FP8E5M2 = 2, + DT_FP8E4M3 = 3, + DT_INVALID = 100 +} DataType; + +#endif // COMPRESS_TYPES_H diff --git a/ucm/store/compress/cc/compress_lib/debug.cc b/ucm/store/compress/cc/compress_lib/debug.cc deleted file mode 100644 index af8821dfe..000000000 --- a/ucm/store/compress/cc/compress_lib/debug.cc +++ /dev/null @@ -1,43 +0,0 @@ -/* ****************************************************************** - debug - Part of FSE library - Copyright (C) 2013-present, Yann Collet. - - BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - You can contact the author at : - - Source repository : https://github.com/Cyan4973/FiniteStateEntropy -****************************************************************** */ - -/* - * This module only hosts one global variable - * which can be used to dynamically influence the verbosity of traces, - * such as DEBUGLOG and RAWLOG - */ - -#include "debug.h" - -int g_debuglevel = DEBUGLEVEL; diff --git a/ucm/store/compress/cc/compress_lib/debug.h b/ucm/store/compress/cc/compress_lib/debug.h deleted file mode 100644 index 485d24527..000000000 --- a/ucm/store/compress/cc/compress_lib/debug.h +++ /dev/null @@ -1,114 +0,0 @@ -/* ****************************************************************** - * debug - * Part of FSE library - * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. - * - * You can contact the author at : - * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - ****************************************************************** */ - -/* - * The purpose of this header is to enable debug functions. - * They regroup assert(), DEBUGLOG() and RAWLOG() for run-time, - * and DEBUG_STATIC_ASSERT() for compile-time. - * - * By default, DEBUGLEVEL==0, which means run-time debug is disabled. - * - * Level 1 enables assert() only. - * Starting level 2, traces can be generated and pushed to stderr. - * The higher the level, the more verbose the traces. - * - * It's possible to dynamically adjust level using variable g_debug_level, - * which is only declared if DEBUGLEVEL>=2, - * and is a global variable, not multi-thread protected (use with care) - */ - -#ifndef DEBUG_H_12987983217 -#define DEBUG_H_12987983217 - -#if defined(__cplusplus) -extern "C" { -#endif - -/* static assert is triggered at compile time, leaving no runtime artefact. - * static assert only works with compile-time constants. - * Also, this variant can only be used inside a function. */ -#define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1]) - -/* DEBUGLEVEL is expected to be defined externally, - * typically through compiler command line. - * Value must be a number. */ -#ifndef DEBUGLEVEL -#define DEBUGLEVEL 0 -#endif - -/* DEBUGFILE can be defined externally, - * typically through compiler command line. - * note : currently useless. - * Value must be stderr or stdout */ -#ifndef DEBUGFILE -#define DEBUGFILE stderr -#endif - -/* recommended values for DEBUGLEVEL : - * 0 : release mode, no debug, all run-time checks disabled - * 1 : enables assert() only, no display - * 2 : reserved, for currently active debug path - * 3 : events once per object lifetime (CCtx, CDict, etc.) - * 4 : events once per frame - * 5 : events once per block - * 6 : events once per sequence (verbose) - * 7+: events at every position (*very* verbose) - * - * It's generally inconvenient to output traces > 5. - * In which case, it's possible to selectively trigger high verbosity levels - * by modifying g_debug_level. - */ - -#if (DEBUGLEVEL >= 1) -#include -#else -#ifndef assert /* assert may be already defined, due to prior #include */ -#define assert(condition) ((void)0) /* disable assert (default) */ -#endif -#endif - -#if (DEBUGLEVEL >= 2) -#include -extern int g_debuglevel; /* the variable is only declared, - it actually lives in debug.c, - and is shared by the whole process. - It's not thread-safe. - It's useful when enabling very verbose levels - on selective conditions (such as position in src) */ - -#define RAWLOG(l, ...) \ - { \ - if (l <= g_debuglevel) { fprintf(stderr, __VA_ARGS__); } \ - } -#define DEBUGLOG(l, ...) \ - { \ - if (l <= g_debuglevel) { \ - fprintf(stderr, __FILE__ ": " __VA_ARGS__); \ - fprintf(stderr, " \n"); \ - } \ - } -#else -#define RAWLOG(l, ...) \ - { \ - } /* disabled */ -#define DEBUGLOG(l, ...) \ - { \ - } /* disabled */ -#endif - -#if defined(__cplusplus) -} -#endif - -#endif /* DEBUG_H_12987983217 */ diff --git a/ucm/store/compress/cc/compress_lib/entropy_common.cc b/ucm/store/compress/cc/compress_lib/entropy_common.cc deleted file mode 100644 index 01b0e1725..000000000 --- a/ucm/store/compress/cc/compress_lib/entropy_common.cc +++ /dev/null @@ -1,234 +0,0 @@ -/* ****************************************************************** - * Common functions of New Generation Entropy library - * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. - * - * You can contact the author at : - * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy - * - Public forum : https://groups.google.com/forum/#!forum/lz4c - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - ****************************************************************** */ - -/* ************************************* - * Dependencies - ***************************************/ -#include "error_private.h" /* ERR_*, ERROR */ -#include "mem.h" -#define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */ -#include "fse.h" -#define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */ -#include "huf.h" - -/*=== Version ===*/ -unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; } - -/*=== Error Management ===*/ -unsigned FSE_isError(size_t code) { return ERR_isError(code); } -const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); } - -unsigned HUF_isError(size_t code) { return ERR_isError(code); } -const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); } - -/*-************************************************************** - * FSE NCount encoding-decoding - ****************************************************************/ -size_t FSE_readNCount(short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, - const void* headerBuffer, size_t hbSize) -{ - const BYTE* const istart = (const BYTE*)headerBuffer; - const BYTE* const iend = istart + hbSize; - const BYTE* ip = istart; - int nbBits; - int remaining; - int threshold; - U32 bitStream; - int bitCount; - unsigned charnum = 0; - int previous0 = 0; - - if (hbSize < 4) { - /* This function only works when hbSize >= 4 */ - char buffer[4] = {0}; - memcpy(buffer, headerBuffer, hbSize); - { - size_t const countSize = - FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr, buffer, sizeof(buffer)); - if (FSE_isError(countSize)) return countSize; - if (countSize > hbSize) return ERROR(corruption_detected); - return countSize; - } - } - assert(hbSize >= 4); - - /* init */ - memset(normalizedCounter, 0, - (*maxSVPtr + 1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount - have a frequency of 0 */ - bitStream = MEM_readLE32(ip); - nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */ - if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge); - bitStream >>= 4; - bitCount = 4; - *tableLogPtr = nbBits; - remaining = (1 << nbBits) + 1; - threshold = 1 << nbBits; - nbBits++; - - while ((remaining > 1) & (charnum <= *maxSVPtr)) { - if (previous0) { - unsigned n0 = charnum; - while ((bitStream & 0xFFFF) == 0xFFFF) { - n0 += 24; - if (ip < iend - 5) { - ip += 2; - bitStream = MEM_readLE32(ip) >> bitCount; - } else { - bitStream >>= 16; - bitCount += 16; - } - } - while ((bitStream & 3) == 3) { - n0 += 3; - bitStream >>= 2; - bitCount += 2; - } - n0 += bitStream & 3; - bitCount += 2; - if (n0 > *maxSVPtr) { return ERROR(maxSymbolValue_tooSmall); } - while (charnum < n0) { normalizedCounter[charnum++] = 0; } - if ((ip <= iend - 7) || (ip + (bitCount >> 3) <= iend - 4)) { - assert((bitCount >> 3) <= 3); /* For first condition to work */ - ip += bitCount >> 3; - bitCount &= 7; - bitStream = MEM_readLE32(ip) >> bitCount; - } else { - bitStream >>= 2; - } - } - { - int const max = (2 * threshold - 1) - remaining; - int count; - - if ((bitStream & (threshold - 1)) < static_cast(max)) { - count = bitStream & (threshold - 1); - bitCount += nbBits - 1; - } else { - count = bitStream & (2 * threshold - 1); - if (count >= threshold) count -= max; - bitCount += nbBits; - } - - count--; /* extra accuracy */ - remaining -= count < 0 ? -count : count; /* -1 means +1 */ - normalizedCounter[charnum++] = static_cast(count); - previous0 = !count; - while (remaining < threshold) { - nbBits--; - threshold >>= 1; - } - - if ((ip <= iend - 7) || (ip + (bitCount >> 3) <= iend - 4)) { - ip += bitCount >> 3; - bitCount &= 7; - } else { - bitCount -= static_cast(8 * (iend - 4 - ip)); - ip = iend - 4; - } - bitStream = MEM_readLE32(ip) >> (bitCount & 31); - } - } /* while ((remaining>1) & (charnum<=*maxSVPtr)) */ - if (remaining != 1) { return ERROR(corruption_detected); } - if (bitCount > 32) { return ERROR(corruption_detected); } - *maxSVPtr = charnum - 1; - - ip += (bitCount + 7) >> 3; - return ip - istart; -} - -/*! HUF_readStats() : - Read compact Huffman tree, saved by HUF_writeCTable(). - `huffWeight` is destination buffer. - `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32. - @return : size read from `src` , or an error Code . - Note : Needed by HUF_readCTable() and HUF_readDTableX?() . -*/ -size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats, U32* nbSymbolsPtr, - U32* tableLogPtr, const void* src, size_t srcSize) -{ - U32 weightTotal; - const BYTE* ip = (const BYTE*)src; - size_t iSize; - size_t oSize; - - if (!srcSize) { return ERROR(srcSize_wrong); } - iSize = ip[0]; - /* memset(huffWeight, 0, hwSize); */ /* is not necessary, even though some analyzer complain - ... */ - - if (iSize >= 128) { /* special header */ - oSize = iSize - 127; - iSize = ((oSize + 1) / 2); - if (iSize + 1 > srcSize) { return ERROR(srcSize_wrong); } - if (oSize >= hwSize) { return ERROR(corruption_detected); } - ip += 1; - { - U32 n; - for (n = 0; n < oSize; n += 2) { - huffWeight[n] = ip[n / 2] >> 4; - huffWeight[n + 1] = ip[n / 2] & 15; - } - } - } else { /* header compressed with FSE (normal case) */ - FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32( - 6)]; /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */ - if (iSize + 1 > srcSize) { return ERROR(srcSize_wrong); } - oSize = FSE_decompress_wksp(huffWeight, hwSize - 1, ip + 1, iSize, fseWorkspace, - 6); /* max (hwSize-1) values decoded, as last one is implied */ - if (FSE_isError(oSize)) return oSize; - } - - /* collect weight stats */ - memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32)); - weightTotal = 0; - { - U32 n; - for (n = 0; n < oSize; n++) { - if (huffWeight[n] >= HUF_TABLELOG_MAX) { return ERROR(corruption_detected); } - rankStats[huffWeight[n]]++; - weightTotal += (1 << huffWeight[n]) >> 1; - } - } - if (weightTotal == 0) { return ERROR(corruption_detected); } - - /* get last non-null symbol weight (implied, total must be 2^n) */ - { - U32 const tableLog = BIT_highbit32(weightTotal) + 1; - if (tableLog > HUF_TABLELOG_MAX) { return ERROR(corruption_detected); } - *tableLogPtr = tableLog; - /* determine last weight */ - { - U32 const total = 1 << tableLog; - U32 const rest = total - weightTotal; - U32 const verif = 1 << BIT_highbit32(rest); - U32 const lastWeight = BIT_highbit32(rest) + 1; - if (verif != rest) { - return ERROR(corruption_detected); /* last value must be a clean power of 2 */ - } - huffWeight[oSize] = static_cast(lastWeight); - rankStats[lastWeight]++; - } - } - - /* check tree construction validity */ - if ((rankStats[1] < 2) || (rankStats[1] & 1)) { - return ERROR( - corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */ - } - - /* results */ - *nbSymbolsPtr = static_cast(oSize + 1); - return iSize + 1; -} diff --git a/ucm/store/compress/cc/compress_lib/error_private.h b/ucm/store/compress/cc/compress_lib/error_private.h deleted file mode 100644 index aef4c05a9..000000000 --- a/ucm/store/compress/cc/compress_lib/error_private.h +++ /dev/null @@ -1,127 +0,0 @@ -/* ****************************************************************** - Error codes and messages - Copyright (C) 2013-2016, Yann Collet - - BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - You can contact the author at : - - Homepage : http://www.zstd.net -****************************************************************** */ -/* Note : this module is expected to remain private, do not expose it */ - -#ifndef ERROR_H_MODULE -#define ERROR_H_MODULE - -#if defined(__cplusplus) -extern "C" { -#endif - -/* **************************************** - * Dependencies - ******************************************/ -#include /* size_t */ -#include "error_public.h" /* enum list */ - -/* **************************************** - * Compiler-specific - ******************************************/ -#if defined(__GNUC__) -#define ERR_STATIC static __attribute__((unused)) -#elif defined(__cplusplus) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 \ - */ \ - ) -#define ERR_STATIC static inline -#elif defined(_MSC_VER) -#define ERR_STATIC static __inline -#else -#define ERR_STATIC \ - static /* this version may generate warnings for unused static functions; disable the relevant \ - warning */ -#endif - -/*-**************************************** - * Customization (error_public.h) - ******************************************/ -typedef FSE_ErrorCode ERR_enum; -#define PREFIX(name) FSE_error_##name - -/*-**************************************** - * Error codes handling - ******************************************/ -#ifdef ERROR -#undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */ -#endif -#define ERROR(name) ((size_t)-PREFIX(name)) - -ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); } - -ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) -{ - if (!ERR_isError(code)) { return static_cast(0); } - return static_cast(0 - code); -} - -/* check and forward error code */ -#define CHECK_V_F(e, f) \ - size_t const e = f; \ - if (ERR_isError(e)) return e -#define CHECK_F(f) \ - { \ - CHECK_V_F(_var_err__, f); \ - } - -/*-**************************************** - * Error Strings - ******************************************/ - -ERR_STATIC const char* ERR_getErrorString(ERR_enum code) -{ - static const char* notErrorCode = "Unspecified error code"; - switch (code) { - case PREFIX(no_error): return "No error detected"; - case PREFIX(GENERIC): return "Error (generic)"; - case PREFIX(dstSize_tooSmall): return "Destination buffer is too small"; - case PREFIX(srcSize_wrong): return "Src size is incorrect"; - case PREFIX(corruption_detected): return "Corrupted block detected"; - case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported"; - case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large"; - case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small"; - case PREFIX(workSpace_tooSmall): return "workspace buffer is too small"; - case PREFIX(maxCode): - default: return notErrorCode; - } -} - -ERR_STATIC const char* ERR_getErrorName(size_t code) -{ - return ERR_getErrorString(ERR_getErrorCode(code)); -} - -#if defined(__cplusplus) -} -#endif - -#endif /* ERROR_H_MODULE */ diff --git a/ucm/store/compress/cc/compress_lib/error_public.h b/ucm/store/compress/cc/compress_lib/error_public.h deleted file mode 100644 index 7fe0f528d..000000000 --- a/ucm/store/compress/cc/compress_lib/error_public.h +++ /dev/null @@ -1,63 +0,0 @@ -/* ****************************************************************** - Error codes list - Copyright (C) 2016, Yann Collet - - BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - You can contact the author at : - - Source repository : https://github.com/Cyan4973/FiniteStateEntropy - - Public forum : https://groups.google.com/forum/#!forum/lz4c -****************************************************************** */ -#ifndef ERROR_PUBLIC_H_MODULE -#define ERROR_PUBLIC_H_MODULE - -#if defined(__cplusplus) -extern "C" { -#endif - -/* ***************************************** - * error codes list - ******************************************/ -typedef enum { - FSE_error_no_error, - FSE_error_GENERIC, - FSE_error_dstSize_tooSmall, - FSE_error_srcSize_wrong, - FSE_error_corruption_detected, - FSE_error_tableLog_tooLarge, - FSE_error_maxSymbolValue_tooLarge, - FSE_error_maxSymbolValue_tooSmall, - FSE_error_workSpace_tooSmall, - FSE_error_maxCode -} FSE_ErrorCode; - -/* note : compare with size_t function results using FSE_getError() */ - -#if defined(__cplusplus) -} -#endif - -#endif /* ERROR_PUBLIC_H_MODULE */ diff --git a/ucm/store/compress/cc/compress_lib/fse.h b/ucm/store/compress/cc/compress_lib/fse.h deleted file mode 100644 index ee205f996..000000000 --- a/ucm/store/compress/cc/compress_lib/fse.h +++ /dev/null @@ -1,709 +0,0 @@ -/* ****************************************************************** - * FSE : Finite State Entropy codec - * Public Prototypes declaration - * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. - * - * You can contact the author at : - * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - ****************************************************************** */ - -#if defined(__cplusplus) -extern "C" { -#endif - -#ifndef FSE_H -#define FSE_H - -/*-***************************************** - * Dependencies - ******************************************/ -#include /* size_t, ptrdiff_t */ - -/*-***************************************** - * FSE_PUBLIC_API : control library symbols visibility - ******************************************/ -#if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT == 1) && defined(__GNUC__) && (__GNUC__ >= 4) -#define FSE_PUBLIC_API __attribute__((visibility("default"))) -#elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT == 1) /* Visual expected */ -#define FSE_PUBLIC_API __declspec(dllexport) -#elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT == 1) -#define FSE_PUBLIC_API \ - __declspec(dllimport) /* It isn't required but allows to generate better code, saving a \ - function pointer load from the IAT and an indirect jump.*/ -#else -#define FSE_PUBLIC_API -#endif - -/*------ Version ------*/ -#define FSE_VERSION_MAJOR 0 -#define FSE_VERSION_MINOR 9 -#define FSE_VERSION_RELEASE 0 - -#define FSE_LIB_VERSION FSE_VERSION_MAJOR.FSE_VERSION_MINOR.FSE_VERSION_RELEASE -#define FSE_QUOTE(str) #str -#define FSE_EXPAND_AND_QUOTE(str) FSE_QUOTE(str) -#define FSE_VERSION_STRING FSE_EXPAND_AND_QUOTE(FSE_LIB_VERSION) - -#define FSE_VERSION_NUMBER \ - (FSE_VERSION_MAJOR * 100 * 100 + FSE_VERSION_MINOR * 100 + FSE_VERSION_RELEASE) -FSE_PUBLIC_API unsigned FSE_versionNumber( - void); /**< library version number; to be used when checking dll version */ - -/*-**************************************** - * FSE simple functions - ******************************************/ -/*! FSE_compress() : - Compress content of buffer 'src', of size 'srcSize', into destination buffer 'dst'. - 'dst' buffer must be already allocated. Compression runs faster is dstCapacity >= - FSE_compressBound(srcSize). - @return : size of compressed data (<= dstCapacity). - Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!! - if return == 1, srcData is a single byte symbol * srcSize times. Use RLE - compression instead. if FSE_isError(return), compression failed (more details using - FSE_getErrorName()) -*/ -FSE_PUBLIC_API size_t FSE_compress(void* dst, size_t dstCapacity, const void* src, size_t srcSize); - -/*! FSE_decompress(): - Decompress FSE data from buffer 'cSrc', of size 'cSrcSize', - into already allocated destination buffer 'dst', of size 'dstCapacity'. - @return : size of regenerated data (<= maxDstSize), - or an error code, which can be tested using FSE_isError() . - - ** Important ** : FSE_decompress() does not decompress non-compressible nor RLE data !!! - Why ? : making this distinction requires a header. - Header management is intentionally delegated to the user layer, which can better manage special - cases. -*/ -FSE_PUBLIC_API size_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, - size_t cSrcSize); - -/*-***************************************** - * Tool functions - ******************************************/ -FSE_PUBLIC_API size_t FSE_compressBound(size_t size); /* maximum compressed size */ - -/* Error Management */ -FSE_PUBLIC_API unsigned FSE_isError(size_t code); /* tells if a return value is an error code */ -FSE_PUBLIC_API const char* FSE_getErrorName( - size_t code); /* provides error code string (useful for debugging) */ - -/*-***************************************** - * FSE advanced functions - ******************************************/ -/*! FSE_compress2() : - Same as FSE_compress(), but allows the selection of 'maxSymbolValue' and 'tableLog' - Both parameters can be defined as '0' to mean : use default value - @return : size of compressed data - Special values : if return == 0, srcData is not compressible => Nothing is stored within cSrc - !!! if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression. if - FSE_isError(return), it's an error code. -*/ -FSE_PUBLIC_API size_t FSE_compress2(void* dst, size_t dstSize, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned tableLog); - -/*-***************************************** - * FSE detailed API - ******************************************/ -/*! -FSE_compress() does the following: -1. count symbol occurrence from source[] into table count[] (see hist.h) -2. normalize counters so that sum(count[]) == Power_of_2 (2^tableLog) -3. save normalized counters to memory buffer using writeNCount() -4. build encoding table 'CTable' from normalized counters -5. encode the data stream using encoding table 'CTable' - -FSE_decompress() does the following: -1. read normalized counters with readNCount() -2. build decoding table 'DTable' from normalized counters -3. decode the data stream using decoding table 'DTable' - -The following API allows targeting specific sub-functions for advanced tasks. -For example, it's possible to compress several blocks using the same 'CTable', -or to save and provide normalized distribution using external method. -*/ - -/* *** COMPRESSION *** */ - -/*! FSE_optimalTableLog(): - dynamically downsize 'tableLog' when conditions are met. - It saves CPU time, by using smaller tables, while preserving or even improving compression - ratio. - @return : recommended tableLog (necessarily <= 'maxTableLog') */ -FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, - unsigned maxSymbolValue); - -/*! FSE_normalizeCount(): - normalize counts so that sum(count[]) == Power_of_2 (2^tableLog) - 'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1). - @return : tableLog, - or an errorCode, which can be tested using FSE_isError() */ -FSE_PUBLIC_API size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog, - const unsigned* count, size_t srcSize, - unsigned maxSymbolValue); - -/*! FSE_NCountWriteBound(): - Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and - 'tableLog'. Typically useful for allocation purpose. */ -FSE_PUBLIC_API size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog); - -/*! FSE_writeNCount(): - Compactly save 'normalizedCounter' into 'buffer'. - @return : size of the compressed table, - or an errorCode, which can be tested using FSE_isError(). */ -FSE_PUBLIC_API size_t FSE_writeNCount(void* buffer, size_t bufferSize, - const short* normalizedCounter, unsigned maxSymbolValue, - unsigned tableLog); - -/*! Constructor and Destructor of FSE_CTable. - Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */ -typedef unsigned - FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */ -FSE_PUBLIC_API FSE_CTable* FSE_createCTable(unsigned maxSymbolValue, unsigned tableLog); -FSE_PUBLIC_API void FSE_freeCTable(FSE_CTable* ct); - -/*! FSE_buildCTable(): - Builds `ct`, which must be already allocated, using FSE_createCTable(). - @return : 0, or an errorCode, which can be tested using FSE_isError() */ -FSE_PUBLIC_API size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, - unsigned maxSymbolValue, unsigned tableLog); - -/*! FSE_compress_usingCTable(): - Compress `src` using `ct` into `dst` which must be already allocated. - @return : size of compressed data (<= `dstCapacity`), - or 0 if compressed data could not fit into `dst`, - or an errorCode, which can be tested using FSE_isError() */ -FSE_PUBLIC_API size_t FSE_compress_usingCTable(void* dst, size_t dstCapacity, const void* src, - size_t srcSize, const FSE_CTable* ct); - -/*! -Tutorial : ----------- -The first step is to count all symbols. FSE_count() does this job very fast. -Result will be saved into 'count', a table of unsigned int, which must be already allocated, and -have 'maxSymbolValuePtr[0]+1' cells. 'src' is a table of bytes of size 'srcSize'. All values within -'src' MUST be <= maxSymbolValuePtr[0] maxSymbolValuePtr[0] will be updated, with its real value -(necessarily <= original value) FSE_count() will return the number of occurrence of the most -frequent symbol. This can be used to know if there is a single symbol within 'src', and to quickly -evaluate its compressibility. If there is an error, the function will return an ErrorCode (which can -be tested using FSE_isError()). - -The next step is to normalize the frequencies. -FSE_normalizeCount() will ensure that sum of frequencies is == 2 ^'tableLog'. -It also guarantees a minimum of 1 to any Symbol with frequency >= 1. -You can use 'tableLog'==0 to mean "use default tableLog value". -If you are unsure of which tableLog value to use, you can ask FSE_optimalTableLog(), -which will provide the optimal valid tableLog given sourceSize, maxSymbolValue, and a user-defined -maximum (0 means "default"). - -The result of FSE_normalizeCount() will be saved into a table, -called 'normalizedCounter', which is a table of signed short. -'normalizedCounter' must be already allocated, and have at least 'maxSymbolValue+1' cells. -The return value is tableLog if everything proceeded as expected. -It is 0 if there is a single symbol within distribution. -If there is an error (ex: invalid tableLog value), the function will return an ErrorCode (which can -be tested using FSE_isError()). - -'normalizedCounter' can be saved in a compact manner to a memory area using FSE_writeNCount(). -'buffer' must be already allocated. -For guaranteed success, buffer size must be at least FSE_headerBound(). -The result of the function is the number of bytes written into 'buffer'. -If there is an error, the function will return an ErrorCode (which can be tested using -FSE_isError(); ex : buffer size too small). - -'normalizedCounter' can then be used to create the compression table 'CTable'. -The space required by 'CTable' must be already allocated, using FSE_createCTable(). -You can then use FSE_buildCTable() to fill 'CTable'. -If there is an error, both functions will return an ErrorCode (which can be tested using -FSE_isError()). - -'CTable' can then be used to compress 'src', with FSE_compress_usingCTable(). -Similar to FSE_count(), the convention is that 'src' is assumed to be a table of char of size -'srcSize' The function returns the size of compressed data (without header), necessarily <= -`dstCapacity`. If it returns '0', compressed data could not fit into 'dst'. If there is an error, -the function will return an ErrorCode (which can be tested using FSE_isError()). -*/ - -/* *** DECOMPRESSION *** */ - -/*! FSE_readNCount(): - Read compactly saved 'normalizedCounter' from 'rBuffer'. - @return : size read from 'rBuffer', - or an errorCode, which can be tested using FSE_isError(). - maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective - values */ -FSE_PUBLIC_API size_t FSE_readNCount(short* normalizedCounter, unsigned* maxSymbolValuePtr, - unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize); - -/*! Constructor and Destructor of FSE_DTable. - Note that its size depends on 'tableLog' */ -typedef unsigned - FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */ -FSE_PUBLIC_API FSE_DTable* FSE_createDTable(unsigned tableLog); -FSE_PUBLIC_API void FSE_freeDTable(FSE_DTable* dt); - -/*! FSE_buildDTable(): - Builds 'dt', which must be already allocated, using FSE_createDTable(). - return : 0, or an errorCode, which can be tested using FSE_isError() */ -FSE_PUBLIC_API size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, - unsigned maxSymbolValue, unsigned tableLog); - -/*! FSE_decompress_usingDTable(): - Decompress compressed source `cSrc` of size `cSrcSize` using `dt` - into `dst` which must be already allocated. - @return : size of regenerated data (necessarily <= `dstCapacity`), - or an errorCode, which can be tested using FSE_isError() */ -FSE_PUBLIC_API size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, - size_t cSrcSize, const FSE_DTable* dt); - -/*! -Tutorial : ----------- -(Note : these functions only decompress FSE-compressed blocks. - If block is uncompressed, use memcpy() instead - If block is a single repeated byte, use memset() instead ) - -The first step is to obtain the normalized frequencies of symbols. -This can be performed by FSE_readNCount() if it was saved using FSE_writeNCount(). -'normalizedCounter' must be already allocated, and have at least 'maxSymbolValuePtr[0]+1' cells of -signed short. In practice, that means it's necessary to know 'maxSymbolValue' beforehand, or size -the table to handle worst case situations (typically 256). FSE_readNCount() will provide 'tableLog' -and 'maxSymbolValue'. The result of FSE_readNCount() is the number of bytes read from 'rBuffer'. -Note that 'rBufferSize' must be at least 4 bytes, even if useful information is less than that. -If there is an error, the function will return an error code, which can be tested using -FSE_isError(). - -The next step is to build the decompression tables 'FSE_DTable' from 'normalizedCounter'. -This is performed by the function FSE_buildDTable(). -The space required by 'FSE_DTable' must be already allocated using FSE_createDTable(). -If there is an error, the function will return an error code, which can be tested using -FSE_isError(). - -`FSE_DTable` can then be used to decompress `cSrc`, with FSE_decompress_usingDTable(). -`cSrcSize` must be strictly correct, otherwise decompression will fail. -FSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=`dstCapacity`). -If there is an error, the function will return an error code, which can be tested using -FSE_isError(). (ex: dst buffer too small) -*/ - -#endif /* FSE_H */ - -#if defined(FSE_STATIC_LINKING_ONLY) && !defined(FSE_H_FSE_STATIC_LINKING_ONLY) -#define FSE_H_FSE_STATIC_LINKING_ONLY - -/* *** Dependency *** */ -#include "bitstream.h" - -/* ***************************************** - * Static allocation - *******************************************/ -/* FSE buffer bounds */ -#define FSE_NCOUNTBOUND 512 -#define FSE_BLOCKBOUND(size) \ - ((size) + ((size) >> 7) + 4 /* fse states */ + sizeof(size_t) /* bitContainer */) -#define FSE_COMPRESSBOUND(size) \ - (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ - -/* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using - * below macros */ -#define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) \ - (1 + (1 << ((maxTableLog) - 1)) + (((maxSymbolValue) + 1) * 2)) -#define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1 << (maxTableLog))) - -/* or use the size to malloc() space directly. Pay attention to alignment restrictions though */ -#define FSE_CTABLE_SIZE(maxTableLog, maxSymbolValue) \ - (FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(FSE_CTable)) -#define FSE_DTABLE_SIZE(maxTableLog) (FSE_DTABLE_SIZE_U32(maxTableLog) * sizeof(FSE_DTable)) - -/* ***************************************** - * FSE advanced API - ***************************************** */ - -unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, - unsigned minus); -/**< same as FSE_optimalTableLog(), which used `minus==2` */ - -/* FSE_compress_wksp() : - * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`). - * FSE_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable. - */ -#define FSE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) \ - (FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + \ - ((maxTableLog > 12) ? (1 << (maxTableLog - 2)) : 1024)) -size_t FSE_compress_wksp(void* dst, size_t dstSize, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned tableLog, void* workSpace, - size_t wkspSize); - -size_t FSE_buildCTable_raw(FSE_CTable* ct, unsigned nbBits); -/**< build a fake FSE_CTable, designed for a flat distribution, where each symbol uses nbBits */ - -size_t FSE_buildCTable_rle(FSE_CTable* ct, unsigned char symbolValue); -/**< build a fake FSE_CTable, designed to compress always the same symbolValue */ - -/* FSE_buildCTable_wksp() : - * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`). - * `wkspSize` must be >= `(1<= -BIT_DStream_completed - -When it's done, verify decompression is fully completed, by checking both DStream and the relevant -states. Checking if DStream has reached its end is performed by : BIT_endOfDStream(&DStream); Check -also the states. There might be some symbols left there, if some high probability ones (>50%) are -possible. FSE_endOfDState(&DState); -*/ - -/* ***************************************** - * FSE unsafe API - *******************************************/ -static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD); -/* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */ - -/* ***************************************** - * Implementation of inlined functions - *******************************************/ -typedef struct { - int deltaFindState; - U32 deltaNbBits; -} FSE_symbolCompressionTransform; /* total 8 bytes */ - -MEM_STATIC void FSE_initCState(FSE_CState_t* statePtr, const FSE_CTable* ct) -{ - const void* ptr = ct; - const U16* u16ptr = (const U16*)ptr; - const U32 tableLog = MEM_read16(ptr); - statePtr->value = (ptrdiff_t)1 << tableLog; - statePtr->stateTable = u16ptr + 2; - statePtr->symbolTT = ct + 1 + (tableLog ? (1 << (tableLog - 1)) : 1); - statePtr->stateLog = tableLog; -} - -/*! FSE_initCState2() : - * Same as FSE_initCState(), but the first symbol to include (which will be the last to be read) - * uses the smallest state value possible, saving the cost of this symbol */ -MEM_STATIC void FSE_initCState2(FSE_CState_t* statePtr, const FSE_CTable* ct, U32 symbol) -{ - FSE_initCState(statePtr, ct); - { - const FSE_symbolCompressionTransform symbolTT = - ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol]; - const U16* stateTable = (const U16*)(statePtr->stateTable); - U32 nbBitsOut = (U32)((symbolTT.deltaNbBits + (1 << 15)) >> 16); - statePtr->value = (nbBitsOut << 16) - symbolTT.deltaNbBits; - statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState]; - } -} - -MEM_STATIC void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* statePtr, unsigned symbol) -{ - FSE_symbolCompressionTransform const symbolTT = - ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol]; - const U16* const stateTable = (const U16*)(statePtr->stateTable); - U32 const nbBitsOut = (U32)((statePtr->value + symbolTT.deltaNbBits) >> 16); - BIT_addBits(bitC, statePtr->value, nbBitsOut); - statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState]; -} - -MEM_STATIC void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* statePtr) -{ - BIT_addBits(bitC, statePtr->value, statePtr->stateLog); - BIT_flushBits(bitC); -} - -/* FSE_getMaxNbBits() : - * Approximate maximum cost of a symbol, in bits. - * Fractional get rounded up (i.e : a symbol with a normalized frequency of 3 gives the same result - * as a frequency of 2) note 1 : assume symbolValue is valid (<= maxSymbolValue) note 2 : if - * freq[symbolValue]==0, @return a fake cost of tableLog+1 bits */ -MEM_STATIC U32 FSE_getMaxNbBits(const void* symbolTTPtr, U32 symbolValue) -{ - const FSE_symbolCompressionTransform* symbolTT = - (const FSE_symbolCompressionTransform*)symbolTTPtr; - return (symbolTT[symbolValue].deltaNbBits + ((1 << 16) - 1)) >> 16; -} - -/* FSE_bitCost() : - * Approximate symbol cost, as fractional value, using fixed-point format (accuracyLog fractional - * bits) note 1 : assume symbolValue is valid (<= maxSymbolValue) note 2 : if freq[symbolValue]==0, - * @return a fake cost of tableLog+1 bits */ -MEM_STATIC U32 FSE_bitCost(const void* symbolTTPtr, U32 tableLog, U32 symbolValue, U32 accuracyLog) -{ - const FSE_symbolCompressionTransform* symbolTT = - (const FSE_symbolCompressionTransform*)symbolTTPtr; - U32 const minNbBits = symbolTT[symbolValue].deltaNbBits >> 16; - U32 const threshold = (minNbBits + 1) << 16; - assert(tableLog < 16); - assert(accuracyLog < 31 - tableLog); /* ensure enough room for renormalization double shift */ - { - U32 const tableSize = 1 << tableLog; - U32 const deltaFromThreshold = threshold - (symbolTT[symbolValue].deltaNbBits + tableSize); - U32 const normalizedDeltaFromThreshold = - (deltaFromThreshold << accuracyLog) >> - tableLog; /* linear interpolation (very approximate) */ - U32 const bitMultiplier = 1 << accuracyLog; - assert(symbolTT[symbolValue].deltaNbBits + tableSize <= threshold); - assert(normalizedDeltaFromThreshold <= bitMultiplier); - return (minNbBits + 1) * bitMultiplier - normalizedDeltaFromThreshold; - } -} - -/* ====== Decompression ====== */ - -typedef struct { - U16 tableLog; - U16 fastMode; -} FSE_DTableHeader; /* sizeof U32 */ - -typedef struct { - unsigned short newState; - unsigned char symbol; - unsigned char nbBits; -} FSE_decode_t; /* size == U32 */ - -MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt) -{ - const void* ptr = dt; - const FSE_DTableHeader* const DTableH = (const FSE_DTableHeader*)ptr; - DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog); - BIT_reloadDStream(bitD); - DStatePtr->table = dt + 1; -} - -MEM_STATIC BYTE FSE_peekSymbol(const FSE_DState_t* DStatePtr) -{ - FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; - return DInfo.symbol; -} - -MEM_STATIC void FSE_updateState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) -{ - FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; - U32 const nbBits = DInfo.nbBits; - size_t const lowBits = BIT_readBits(bitD, nbBits); - DStatePtr->state = DInfo.newState + lowBits; -} - -MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) -{ - FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; - U32 const nbBits = DInfo.nbBits; - BYTE const symbol = DInfo.symbol; - size_t const lowBits = BIT_readBits(bitD, nbBits); - - DStatePtr->state = DInfo.newState + lowBits; - return symbol; -} - -/*! FSE_decodeSymbolFast() : - unsafe, only works if no symbol has a probability > 50% */ -MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) -{ - FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; - U32 const nbBits = DInfo.nbBits; - BYTE const symbol = DInfo.symbol; - size_t const lowBits = BIT_readBitsFast(bitD, nbBits); - - DStatePtr->state = DInfo.newState + lowBits; - return symbol; -} - -MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr) { return DStatePtr->state == 0; } - -#ifndef FSE_COMMONDEFS_ONLY - -/* ************************************************************** - * Tuning parameters - ****************************************************************/ -/*!MEMORY_USAGE : - * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; - * etc.) Increasing memory usage improves compression ratio Reduced memory usage can improve speed, - * due to cache effect Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 - * cache */ -#ifndef FSE_MAX_MEMORY_USAGE -#define FSE_MAX_MEMORY_USAGE 14 -#endif -#ifndef FSE_DEFAULT_MEMORY_USAGE -#define FSE_DEFAULT_MEMORY_USAGE 13 -#endif -#if (FSE_DEFAULT_MEMORY_USAGE > FSE_MAX_MEMORY_USAGE) -#error "FSE_DEFAULT_MEMORY_USAGE must be <= FSE_MAX_MEMORY_USAGE" -#endif - -/*!FSE_MAX_SYMBOL_VALUE : - * Maximum symbol value authorized. - * Required for proper stack allocation */ -#ifndef FSE_MAX_SYMBOL_VALUE -#define FSE_MAX_SYMBOL_VALUE 255 -#endif - -/* ************************************************************** - * template functions type & suffix - ****************************************************************/ -#define FSE_FUNCTION_TYPE BYTE -#define FSE_FUNCTION_EXTENSION -#define FSE_DECODE_TYPE FSE_decode_t - -#endif /* !FSE_COMMONDEFS_ONLY */ - -/* *************************************************************** - * Constants - *****************************************************************/ -#define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE - 2) -#define FSE_MAX_TABLESIZE (1U << FSE_MAX_TABLELOG) -#define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE - 1) -#define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE - 2) -#define FSE_MIN_TABLELOG 5 - -#define FSE_TABLELOG_ABSOLUTE_MAX 15 -#if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX -#error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported" -#endif - -#define FSE_TABLESTEP(tableSize) (((tableSize) >> 1) + ((tableSize) >> 3) + 3) - -#endif /* FSE_STATIC_LINKING_ONLY */ - -#if defined(__cplusplus) -} -#endif diff --git a/ucm/store/compress/cc/compress_lib/fse_compress.cc b/ucm/store/compress/cc/compress_lib/fse_compress.cc deleted file mode 100644 index 2efddcd29..000000000 --- a/ucm/store/compress/cc/compress_lib/fse_compress.cc +++ /dev/null @@ -1,767 +0,0 @@ -/* ****************************************************************** - * FSE : Finite State Entropy encoder - * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. - * - * You can contact the author at : - * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy - * - Public forum : https://groups.google.com/forum/#!forum/lz4c - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - ****************************************************************** */ - -/* ************************************************************** - * Includes - ****************************************************************/ -#include /* malloc, free, qsort */ -#include /* memcpy, memset */ -#include "bitstream.h" -#include "compiler.h" -#include "debug.h" /* assert, DEBUGLOG */ -#include "hist.h" /* HIST_count_wksp */ -#include "mem.h" /* U32, U16, etc. */ -#define FSE_STATIC_LINKING_ONLY -#include "error_private.h" -#include "fse.h" - -/* ************************************************************** - * Error Management - ****************************************************************/ -#define FSE_isError ERR_isError - -/* ************************************************************** - * Templates - ****************************************************************/ -/* - designed to be included - for type-specific functions (template emulation in C) - Objective is to write these functions only once, for improved maintenance -*/ - -/* safety checks */ -#ifndef FSE_FUNCTION_EXTENSION -#error "FSE_FUNCTION_EXTENSION must be defined" -#endif -#ifndef FSE_FUNCTION_TYPE -#error "FSE_FUNCTION_TYPE must be defined" -#endif - -/* Function names */ -#define FSE_CAT(X, Y) X##Y -#define FSE_FUNCTION_NAME(X, Y) FSE_CAT(X, Y) -#define FSE_TYPE_NAME(X, Y) FSE_CAT(X, Y) - -/* Function templates */ - -/* FSE_buildCTable_wksp() : - * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`). - * wkspSize should be sized to handle worst case situation, which is `1<> 1 : 1); - FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*)(FSCT); - U32 const step = FSE_TABLESTEP(tableSize); - U32 cumul[FSE_MAX_SYMBOL_VALUE + 2]; - - FSE_FUNCTION_TYPE* const tableSymbol = (FSE_FUNCTION_TYPE*)workSpace; - U32 highThreshold = tableSize - 1; - - /* CTable header */ - if (((size_t)1 << tableLog) * sizeof(FSE_FUNCTION_TYPE) > wkspSize) { - return ERROR(tableLog_tooLarge); - } - tableU16[-2] = (U16)tableLog; - tableU16[-1] = (U16)maxSymbolValue; - assert(tableLog < 16); /* required for threshold strategy to work */ - - /* For explanations on how to distribute symbol values over the table : - * http://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */ - -#ifdef __clang_analyzer__ - memset(tableSymbol, 0, - sizeof(*tableSymbol) * - tableSize); /* useless initialization, just to keep scan-build happy */ -#endif - - /* symbol start positions */ - { - U32 u; - cumul[0] = 0; - for (u = 1; u <= maxSymbolValue + 1; u++) { - if (normalizedCounter[u - 1] == -1) { /* Low proba symbol */ - cumul[u] = cumul[u - 1] + 1; - tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(u - 1); - } else { - cumul[u] = cumul[u - 1] + normalizedCounter[u - 1]; - } - } - cumul[maxSymbolValue + 1] = tableSize + 1; - } - - /* Spread symbols */ - { - U32 position = 0; - U32 symbol; - for (symbol = 0; symbol <= maxSymbolValue; symbol++) { - int nbOccurrences; - int const freq = normalizedCounter[symbol]; - for (nbOccurrences = 0; nbOccurrences < freq; nbOccurrences++) { - tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol; - position = (position + step) & tableMask; - while (position > highThreshold) { - position = (position + step) & tableMask; - } /* Low proba area */ - } - } - - assert(position == 0); /* Must have initialized all positions */ - } - - /* Build table */ - { - U32 u; - for (u = 0; u < tableSize; u++) { - FSE_FUNCTION_TYPE s = tableSymbol[u]; /* note : static analyzer may not understand - tableSymbol is properly initialized */ - tableU16[cumul[s]++] = - (U16)(tableSize + - u); /* TableU16 : sorted by symbol order; gives next state value */ - } - } - - /* Build Symbol Transformation Table */ - { - unsigned total = 0; - unsigned s; - for (s = 0; s <= maxSymbolValue; s++) { - switch (normalizedCounter[s]) { - case 0: - /* filling nonetheless, for compatibility with FSE_getMaxNbBits() */ - symbolTT[s].deltaNbBits = ((tableLog + 1) << 16) - (1 << tableLog); - break; - - case -1: - case 1: - symbolTT[s].deltaNbBits = (tableLog << 16) - (1 << tableLog); - symbolTT[s].deltaFindState = total - 1; - total++; - break; - default: { - U32 const maxBitsOut = tableLog - BIT_highbit32(normalizedCounter[s] - 1); - U32 const minStatePlus = normalizedCounter[s] << maxBitsOut; - symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus; - symbolTT[s].deltaFindState = total - normalizedCounter[s]; - total += normalizedCounter[s]; - } - } - } - } - -#if 0 /* debug : symbol costs */ - DEBUGLOG(5, "\n --- table statistics : "); - { U32 symbol; - for (symbol=0; symbol<=maxSymbolValue; symbol++) { - DEBUGLOG(5, "%3u: w=%3i, maxBits=%u, fracBits=%.2f", - symbol, normalizedCounter[symbol], - FSE_getMaxNbBits(symbolTT, symbol), - (double)FSE_bitCost(symbolTT, tableLog, symbol, 8) / 256); - } - } -#endif - - return 0; -} - -size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, - unsigned tableLog) -{ - FSE_FUNCTION_TYPE tableSymbol[FSE_MAX_TABLESIZE]; /* memset() is not necessary, even if static - analyzer complain about it */ - return FSE_buildCTable_wksp(ct, normalizedCounter, maxSymbolValue, tableLog, tableSymbol, - sizeof(tableSymbol)); -} - -#ifndef FSE_COMMONDEFS_ONLY - -/*-************************************************************** - * FSE NCount encoding - ****************************************************************/ -size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog) -{ - size_t const maxHeaderSize = (((maxSymbolValue + 1) * tableLog) >> 3) + 3; - return maxSymbolValue ? maxHeaderSize : FSE_NCOUNTBOUND; /* maxSymbolValue==0 ? use default */ -} - -static size_t FSE_writeNCount_generic(void* header, size_t headerBufferSize, - const short* normalizedCounter, unsigned maxSymbolValue, - unsigned tableLog, unsigned writeIsSafe) -{ - BYTE* const ostart = (BYTE*)header; - BYTE* out = ostart; - BYTE* const oend = ostart + headerBufferSize; - int nbBits; - const int tableSize = 1 << tableLog; - int remaining; - int threshold; - U32 bitStream = 0; - int bitCount = 0; - unsigned symbol = 0; - unsigned const alphabetSize = maxSymbolValue + 1; - int previousIs0 = 0; - - /* Table Size */ - bitStream += (tableLog - FSE_MIN_TABLELOG) << bitCount; - bitCount += 4; - - /* Init */ - remaining = tableSize + 1; /* +1 for extra accuracy */ - threshold = tableSize; - nbBits = tableLog + 1; - - while ((symbol < alphabetSize) && (remaining > 1)) { /* stops at 1 */ - if (previousIs0) { - unsigned start = symbol; - while ((symbol < alphabetSize) && !normalizedCounter[symbol]) { symbol++; } - if (symbol == alphabetSize) { break; } /* incorrect distribution */ - while (symbol >= start + 24) { - start += 24; - bitStream += 0xFFFFU << bitCount; - if ((!writeIsSafe) && (out > oend - 2)) { - return ERROR(dstSize_tooSmall); - } /* Buffer overflow */ - out[0] = static_cast(bitStream); - out[1] = static_cast(bitStream >> 8); - out += 2; - bitStream >>= 16; - } - while (symbol >= start + 3) { - start += 3; - bitStream += 3 << bitCount; - bitCount += 2; - } - bitStream += (symbol - start) << bitCount; - bitCount += 2; - if (bitCount > 16) { - if ((!writeIsSafe) && (out > oend - 2)) { - return ERROR(dstSize_tooSmall); - } /* Buffer overflow */ - out[0] = static_cast(bitStream); - out[1] = static_cast(bitStream >> 8); - out += 2; - bitStream >>= 16; - bitCount -= 16; - } - } - { - int count = normalizedCounter[symbol++]; - int const max = (2 * threshold - 1) - remaining; - remaining -= count < 0 ? -count : count; - count++; /* +1 for extra accuracy */ - if (count >= threshold) { - count += max; - } /* [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[ */ - bitStream += count << bitCount; - bitCount += nbBits; - bitCount -= (count < max); - previousIs0 = (count == 1); - if (remaining < 1) { return ERROR(GENERIC); } - while (remaining < threshold) { - nbBits--; - threshold >>= 1; - } - } - if (bitCount > 16) { - if ((!writeIsSafe) && (out > oend - 2)) { - return ERROR(dstSize_tooSmall); - } /* Buffer overflow */ - out[0] = static_cast(bitStream); - out[1] = static_cast(bitStream >> 8); - out += 2; - bitStream >>= 16; - bitCount -= 16; - } - } - - if (remaining != 1) { return ERROR(GENERIC); } /* incorrect normalized distribution */ - assert(symbol <= alphabetSize); - - /* flush remaining bitStream */ - if ((!writeIsSafe) && (out > oend - 2)) { - return ERROR(dstSize_tooSmall); - } /* Buffer overflow */ - out[0] = static_cast(bitStream); - out[1] = static_cast(bitStream >> 8); - out += (bitCount + 7) / 8; - - return (out - ostart); -} - -size_t FSE_writeNCount(void* buffer, size_t bufferSize, const short* normalizedCounter, - unsigned maxSymbolValue, unsigned tableLog) -{ - if (tableLog > FSE_MAX_TABLELOG) { return ERROR(tableLog_tooLarge); } /* Unsupported */ - if (tableLog < FSE_MIN_TABLELOG) { return ERROR(GENERIC); } /* Unsupported */ - - if (bufferSize < FSE_NCountWriteBound(maxSymbolValue, tableLog)) { - return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, - tableLog, 0); - } - - return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, - 1 /* write in buffer is safe */); -} - -/*-************************************************************** - * FSE Compression Code - ****************************************************************/ - -FSE_CTable* FSE_createCTable(unsigned maxSymbolValue, unsigned tableLog) -{ - size_t size; - if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) { tableLog = FSE_TABLELOG_ABSOLUTE_MAX; } - size = FSE_CTABLE_SIZE_U32(tableLog, maxSymbolValue) * sizeof(U32); - return (FSE_CTable*)malloc(size); -} - -void FSE_freeCTable(FSE_CTable* ct) { free(ct); } - -/* provides the minimum logSize to safely represent a distribution */ -static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue) -{ - U32 minBitsSrc = BIT_highbit32((U32)(srcSize)) + 1; - U32 minBitsSymbols = BIT_highbit32(maxSymbolValue) + 2; - U32 minBits = minBitsSrc < minBitsSymbols ? minBitsSrc : minBitsSymbols; - assert(srcSize > 1); /* Not supported, RLE should be used instead */ - return minBits; -} - -unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, - unsigned minus) -{ - U32 maxBitsSrc = BIT_highbit32((U32)(srcSize - 1)) - minus; - U32 tableLog = maxTableLog; - U32 minBits = FSE_minTableLog(srcSize, maxSymbolValue); - assert(srcSize > 1); /* Not supported, RLE should be used instead */ - if (tableLog == 0) { tableLog = FSE_DEFAULT_TABLELOG; } - if (maxBitsSrc < tableLog) { tableLog = maxBitsSrc; } /* Accuracy can be reduced */ - if (minBits > tableLog) { - tableLog = minBits; - } /* Need a minimum to safely represent all symbol values */ - if (tableLog < FSE_MIN_TABLELOG) { tableLog = FSE_MIN_TABLELOG; } - if (tableLog > FSE_MAX_TABLELOG) { tableLog = FSE_MAX_TABLELOG; } - return tableLog; -} - -unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue) -{ - return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 2); -} - -/* Secondary normalization method. - To be used when primary method fails. */ - -static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count, size_t total, - U32 maxSymbolValue) -{ - short const NOT_YET_ASSIGNED = -2; - U32 s; - U32 distributed = 0; - U32 ToDistribute; - - /* Init */ - U32 const lowThreshold = (U32)(total >> tableLog); - U32 lowOne = (U32)((total * 3) >> (tableLog + 1)); - - for (s = 0; s <= maxSymbolValue; s++) { - if (count[s] == 0) { - norm[s] = 0; - continue; - } - if (count[s] <= lowThreshold) { - norm[s] = -1; - distributed++; - total -= count[s]; - continue; - } - if (count[s] <= lowOne) { - norm[s] = 1; - distributed++; - total -= count[s]; - continue; - } - - norm[s] = NOT_YET_ASSIGNED; - } - ToDistribute = (1 << tableLog) - distributed; - - if (ToDistribute == 0) { return 0; } - - if ((total / ToDistribute) > lowOne) { - /* risk of rounding to zero */ - lowOne = (U32)((total * 3) / (ToDistribute * 2)); - for (s = 0; s <= maxSymbolValue; s++) { - if ((norm[s] == NOT_YET_ASSIGNED) && (count[s] <= lowOne)) { - norm[s] = 1; - distributed++; - total -= count[s]; - continue; - } - } - ToDistribute = (1 << tableLog) - distributed; - } - - if (distributed == maxSymbolValue + 1) { - /* all values are pretty poor; - probably incompressible data (should have already been detected); - find max, then give all remaining points to max */ - U32 maxV = 0, maxC = 0; - for (s = 0; s <= maxSymbolValue; s++) { - if (count[s] > maxC) { - maxV = s; - maxC = count[s]; - } - } - norm[maxV] += static_cast(ToDistribute); - return 0; - } - - if (total == 0) { - /* all of the symbols were low enough for the lowOne or lowThreshold */ - for (s = 0; ToDistribute > 0; s = (s + 1) % (maxSymbolValue + 1)) { - if (norm[s] > 0) { - ToDistribute--; - norm[s]++; - } - } - return 0; - } - - { - U64 const vStepLog = 62 - tableLog; - U64 const mid = (1ULL << (vStepLog - 1)) - 1; - U64 const rStep = - ((((U64)1 << vStepLog) * ToDistribute) + mid) / total; /* scale on remaining */ - U64 tmpTotal = mid; - for (s = 0; s <= maxSymbolValue; s++) { - if (norm[s] == NOT_YET_ASSIGNED) { - U64 const end = tmpTotal + (count[s] * rStep); - U32 const sStart = (U32)(tmpTotal >> vStepLog); - U32 const sEnd = (U32)(end >> vStepLog); - U32 const weight = sEnd - sStart; - if (weight < 1) { return ERROR(GENERIC); } - norm[s] = static_cast(weight); - tmpTotal = end; - } - } - } - - return 0; -} - -size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog, const unsigned* count, - size_t total, unsigned maxSymbolValue) -{ - /* Sanity checks */ - if (tableLog == 0) { tableLog = FSE_DEFAULT_TABLELOG; } - if (tableLog < FSE_MIN_TABLELOG) { return ERROR(GENERIC); } /* Unsupported size */ - if (tableLog > FSE_MAX_TABLELOG) { return ERROR(tableLog_tooLarge); } /* Unsupported size */ - if (tableLog < FSE_minTableLog(total, maxSymbolValue)) { - return ERROR(GENERIC); - } /* Too small tableLog, compression potentially impossible */ - - { - static U32 const rtbTable[] = {0, 473195, 504333, 520860, 550000, 700000, 750000, 830000}; - U64 const scale = 62 - tableLog; - U64 const step = ((U64)1 << 62) / total; /* <== here, one division ! */ - U64 const vStep = 1ULL << (scale - 20); - int stillToDistribute = 1 << tableLog; - unsigned s; - unsigned largest = 0; - short largestP = 0; - U32 lowThreshold = (U32)(total >> tableLog); - - for (s = 0; s <= maxSymbolValue; s++) { - if (count[s] == total) { return 0; } /* rle special case */ - if (count[s] == 0) { - normalizedCounter[s] = 0; - continue; - } - if (count[s] <= lowThreshold) { - normalizedCounter[s] = -1; - stillToDistribute--; - } else { - short proba = static_cast((count[s] * step) >> scale); - if (proba < 8) { - U64 restToBeat = vStep * rtbTable[proba]; - proba += (count[s] * step) - ((U64)proba << scale) > restToBeat; - } - if (proba > largestP) { - largestP = proba; - largest = s; - } - normalizedCounter[s] = proba; - stillToDistribute -= proba; - } - } - if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) { - /* corner case, need another normalization method */ - size_t const errorCode = - FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue); - if (FSE_isError(errorCode)) { return errorCode; } - } else { - normalizedCounter[largest] += static_cast(stillToDistribute); - } - } - -#if 0 - { /* Print Table (debug) */ - U32 s; - U32 nTotal = 0; - for (s=0; s<=maxSymbolValue; s++) - RAWLOG(2, "%3i: %4i \n", s, normalizedCounter[s]); - for (s=0; s<=maxSymbolValue; s++) - nTotal += abs(normalizedCounter[s]); - if (nTotal != (1U<> 1); /* assumption : tableLog >= 1 */ - FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*)(FSCT); - unsigned s; - - /* Sanity checks */ - if (nbBits < 1) { return ERROR(GENERIC); } /* min size */ - - /* header */ - tableU16[-2] = (U16)nbBits; - tableU16[-1] = (U16)maxSymbolValue; - - /* Build table */ - for (s = 0; s < tableSize; s++) { tableU16[s] = (U16)(tableSize + s); } - - /* Build Symbol Transformation Table */ - { - const U32 deltaNbBits = (nbBits << 16) - (1 << nbBits); - for (s = 0; s <= maxSymbolValue; s++) { - symbolTT[s].deltaNbBits = deltaNbBits; - symbolTT[s].deltaFindState = s - 1; - } - } - - return 0; -} - -/* fake FSE_CTable, for rle input (always same symbol) */ -size_t FSE_buildCTable_rle(FSE_CTable* ct, BYTE symbolValue) -{ - void* ptr = ct; - U16* tableU16 = ((U16*)ptr) + 2; - void* FSCTptr = (U32*)ptr + 2; - FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*)FSCTptr; - - /* header */ - tableU16[-2] = (U16)0; - tableU16[-1] = (U16)symbolValue; - - /* Build table */ - tableU16[0] = 0; - tableU16[1] = 0; /* just in case */ - - /* Build Symbol Transformation Table */ - symbolTT[symbolValue].deltaNbBits = 0; - symbolTT[symbolValue].deltaFindState = 0; - - return 0; -} - -static size_t FSE_compress_usingCTable_generic(void* dst, size_t dstSize, const void* src, - size_t srcSize, const FSE_CTable* ct, - const unsigned fast) -{ - const BYTE* const istart = (const BYTE*)src; - const BYTE* const iend = istart + srcSize; - const BYTE* ip = iend; - - BIT_CStream_t bitC; - FSE_CState_t CState1, CState2; - - /* init */ - if (srcSize <= 2) { return 0; } - { - size_t const initError = BIT_initCStream(&bitC, dst, dstSize); - if (FSE_isError(initError)) { - return 0; - } /* not enough space available to write a bitstream */ - } - -#define FSE_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s)) - - if (srcSize & 1) { - FSE_initCState2(&CState1, ct, *--ip); - FSE_initCState2(&CState2, ct, *--ip); - FSE_encodeSymbol(&bitC, &CState1, *--ip); - FSE_FLUSHBITS(&bitC); - } else { - FSE_initCState2(&CState2, ct, *--ip); - FSE_initCState2(&CState1, ct, *--ip); - } - - /* join to mod 4 */ - srcSize -= 2; - if ((sizeof(bitC.bitContainer) * 8 > FSE_MAX_TABLELOG * 4 + 7) && - (srcSize & 2)) { /* test bit 2 */ - FSE_encodeSymbol(&bitC, &CState2, *--ip); - FSE_encodeSymbol(&bitC, &CState1, *--ip); - FSE_FLUSHBITS(&bitC); - } - - /* 2 or 4 encoding per loop */ - while (ip > istart) { - FSE_encodeSymbol(&bitC, &CState2, *--ip); - - if (sizeof(bitC.bitContainer) * 8 < - FSE_MAX_TABLELOG * 2 + 7) { /* this test must be static */ - FSE_FLUSHBITS(&bitC); - } - - FSE_encodeSymbol(&bitC, &CState1, *--ip); - - if (sizeof(bitC.bitContainer) * 8 > - FSE_MAX_TABLELOG * 4 + 7) { /* this test must be static */ - FSE_encodeSymbol(&bitC, &CState2, *--ip); - FSE_encodeSymbol(&bitC, &CState1, *--ip); - } - - FSE_FLUSHBITS(&bitC); - } - - FSE_flushCState(&bitC, &CState2); - FSE_flushCState(&bitC, &CState1); - return BIT_closeCStream(&bitC); -} - -size_t FSE_compress_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, - const FSE_CTable* ct) -{ - unsigned const fast = (dstSize >= FSE_BLOCKBOUND(srcSize)); - - if (fast) { - return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 1); - } else { - return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 0); - } -} - -size_t FSE_compressBound(size_t size) { return FSE_COMPRESSBOUND(size); } - -/* FSE_compress_wksp() : - * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`). - * `wkspSize` size must be `(1< not compressible */ - if (maxCount < (srcSize >> 7)) { return 0; } /* Heuristic : not compressible enough */ - } - - tableLog = FSE_optimalTableLog(tableLog, srcSize, maxSymbolValue); - CHECK_F(FSE_normalizeCount(norm, tableLog, count, srcSize, maxSymbolValue)); - - /* Write table description header */ - { - CHECK_V_F(nc_err, FSE_writeNCount(op, oend - op, norm, maxSymbolValue, tableLog)); - op += nc_err; - } - - /* Compress */ - CHECK_F(FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, - scratchBufferSize)); - { - CHECK_V_F(cSize, FSE_compress_usingCTable(op, oend - op, src, srcSize, CTable)); - if (cSize == 0) { return 0; } /* not enough space for compressed data */ - op += cSize; - } - - /* check compressibility */ - if ((size_t)(op - ostart) >= srcSize - 1) { return 0; } - - return op - ostart; -} - -typedef struct { - FSE_CTable CTable_max[FSE_CTABLE_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)]; - union { - U32 hist_wksp[HIST_WKSP_SIZE_U32]; - BYTE scratchBuffer[1 << FSE_MAX_TABLELOG]; - } workspace; -} fseWkspMax_t; - -size_t FSE_compress2(void* dst, size_t dstCapacity, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned tableLog) -{ - fseWkspMax_t scratchBuffer; - DEBUG_STATIC_ASSERT( - sizeof(scratchBuffer) >= - FSE_WKSP_SIZE_U32(FSE_MAX_TABLELOG, - FSE_MAX_SYMBOL_VALUE)); /* compilation failures here means scratchBuffer - is not large enough */ - if (tableLog > FSE_MAX_TABLELOG) { return ERROR(tableLog_tooLarge); } - return FSE_compress_wksp(dst, dstCapacity, src, srcSize, maxSymbolValue, tableLog, - &scratchBuffer, sizeof(scratchBuffer)); -} - -size_t FSE_compress(void* dst, size_t dstCapacity, const void* src, size_t srcSize) -{ - return FSE_compress2(dst, dstCapacity, src, srcSize, FSE_MAX_SYMBOL_VALUE, - FSE_DEFAULT_TABLELOG); -} - -#endif /* FSE_COMMONDEFS_ONLY */ diff --git a/ucm/store/compress/cc/compress_lib/fse_decompress.cc b/ucm/store/compress/cc/compress_lib/fse_decompress.cc deleted file mode 100644 index e8597d524..000000000 --- a/ucm/store/compress/cc/compress_lib/fse_decompress.cc +++ /dev/null @@ -1,304 +0,0 @@ -/* ****************************************************************** - * FSE : Finite State Entropy decoder - * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. - * - * You can contact the author at : - * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy - * - Public forum : https://groups.google.com/forum/#!forum/lz4c - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - ****************************************************************** */ - -/* ************************************************************** - * Includes - ****************************************************************/ -#include /* malloc, free, qsort */ -#include /* memcpy, memset */ -#include "bitstream.h" -#include "compiler.h" -#include "debug.h" /* assert */ -#define FSE_STATIC_LINKING_ONLY -#include "error_private.h" -#include "fse.h" - -/* ************************************************************** - * Error Management - ****************************************************************/ -#define FSE_isError ERR_isError -#define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */ - -/* ************************************************************** - * Templates - ****************************************************************/ -/* - designed to be included - for type-specific functions (template emulation in C) - Objective is to write these functions only once, for improved maintenance -*/ - -/* safety checks */ -#ifndef FSE_FUNCTION_EXTENSION -#error "FSE_FUNCTION_EXTENSION must be defined" -#endif -#ifndef FSE_FUNCTION_TYPE -#error "FSE_FUNCTION_TYPE must be defined" -#endif - -/* Function names */ -#define FSE_CAT(X, Y) X##Y -#define FSE_FUNCTION_NAME(X, Y) FSE_CAT(X, Y) -#define FSE_TYPE_NAME(X, Y) FSE_CAT(X, Y) - -/* Function templates */ -FSE_DTable* FSE_createDTable(unsigned tableLog) -{ - if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) { tableLog = FSE_TABLELOG_ABSOLUTE_MAX; } - return (FSE_DTable*)malloc(FSE_DTABLE_SIZE_U32(tableLog) * sizeof(U32)); -} - -void FSE_freeDTable(FSE_DTable* dt) { free(dt); } - -size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, - unsigned tableLog) -{ - void* const tdPtr = dt + 1; /* because *dt is unsigned, 32-bits aligned on 32-bits */ - FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*)(tdPtr); - U16 symbolNext[FSE_MAX_SYMBOL_VALUE + 1]; - - U32 const maxSV1 = maxSymbolValue + 1; - U32 const tableSize = 1 << tableLog; - U32 highThreshold = tableSize - 1; - - /* Sanity Checks */ - if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) { return ERROR(maxSymbolValue_tooLarge); } - if (tableLog > FSE_MAX_TABLELOG) { return ERROR(tableLog_tooLarge); } - - /* Init, lay down lowprob symbols */ - { - FSE_DTableHeader DTableH; - DTableH.tableLog = (U16)tableLog; - DTableH.fastMode = 1; - { - S16 const largeLimit = (S16)(1 << (tableLog - 1)); - U32 s; - for (s = 0; s < maxSV1; s++) { - if (normalizedCounter[s] == -1) { - tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s; - symbolNext[s] = 1; - } else { - if (normalizedCounter[s] >= largeLimit) { DTableH.fastMode = 0; } - symbolNext[s] = normalizedCounter[s]; - } - } - } - memcpy(dt, &DTableH, sizeof(DTableH)); - } - - /* Spread symbols */ - { - U32 const tableMask = tableSize - 1; - U32 const step = FSE_TABLESTEP(tableSize); - U32 s, position = 0; - for (s = 0; s < maxSV1; s++) { - int i; - for (i = 0; i < normalizedCounter[s]; i++) { - tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s; - position = (position + step) & tableMask; - while (position > highThreshold) { - position = (position + step) & tableMask; - } /* lowprob area */ - } - } - if (position != 0) { - return ERROR(GENERIC); - } /* position must reach all cells once, otherwise - normalizedCounter is incorrect */ - } - - /* Build Decoding table */ - { - U32 u; - for (u = 0; u < tableSize; u++) { - FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol); - U32 const nextState = symbolNext[symbol]++; - tableDecode[u].nbBits = static_cast(tableLog - BIT_highbit32(nextState)); - tableDecode[u].newState = (U16)((nextState << tableDecode[u].nbBits) - tableSize); - } - } - - return 0; -} - -#ifndef FSE_COMMONDEFS_ONLY - -/*-******************************************************* - * Decompression (Byte symbols) - *********************************************************/ -size_t FSE_buildDTable_rle(FSE_DTable* dt, BYTE symbolValue) -{ - void* ptr = dt; - FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr; - void* dPtr = dt + 1; - FSE_decode_t* const cell = (FSE_decode_t*)dPtr; - - DTableH->tableLog = 0; - DTableH->fastMode = 0; - - cell->newState = 0; - cell->symbol = symbolValue; - cell->nbBits = 0; - - return 0; -} - -size_t FSE_buildDTable_raw(FSE_DTable* dt, unsigned nbBits) -{ - void* ptr = dt; - FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr; - void* dPtr = dt + 1; - FSE_decode_t* const dinfo = (FSE_decode_t*)dPtr; - const unsigned tableSize = 1 << nbBits; - const unsigned tableMask = tableSize - 1; - const unsigned maxSV1 = tableMask + 1; - unsigned s; - - /* Sanity checks */ - if (nbBits < 1) { return ERROR(GENERIC); } /* min size */ - - /* Build Decoding Table */ - DTableH->tableLog = (U16)nbBits; - DTableH->fastMode = 1; - for (s = 0; s < maxSV1; s++) { - dinfo[s].newState = 0; - dinfo[s].symbol = static_cast(s); - dinfo[s].nbBits = static_cast(nbBits); - } - - return 0; -} - -FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(void* dst, size_t maxDstSize, - const void* cSrc, size_t cSrcSize, - const FSE_DTable* dt, - const unsigned fast) -{ - BYTE* const ostart = (BYTE*)dst; - BYTE* op = ostart; - BYTE* const omax = op + maxDstSize; - BYTE* const olimit = omax - 3; - - BIT_DStream_t bitD; - FSE_DState_t state1; - FSE_DState_t state2; - - /* Init */ - CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize)); - - FSE_initDState(&state1, &bitD, dt); - FSE_initDState(&state2, &bitD, dt); - -#define FSE_GETSYMBOL(statePtr) \ - fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD) - - /* 4 symbols per loop */ - for (; (BIT_reloadDStream(&bitD) == BIT_DStream_unfinished) & (op < olimit); op += 4) { - op[0] = FSE_GETSYMBOL(&state1); - - if (FSE_MAX_TABLELOG * 2 + 7 > - sizeof(bitD.bitContainer) * 8) { /* This test must be static */ - BIT_reloadDStream(&bitD); - } - - op[1] = FSE_GETSYMBOL(&state2); - - if (FSE_MAX_TABLELOG * 4 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */ - { - if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { - op += 2; - break; - } - } - - op[2] = FSE_GETSYMBOL(&state1); - - if (FSE_MAX_TABLELOG * 2 + 7 > - sizeof(bitD.bitContainer) * 8) { /* This test must be static */ - BIT_reloadDStream(&bitD); - } - - op[3] = FSE_GETSYMBOL(&state2); - } - - /* tail */ - /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly - * BIT_DStream_completed */ - while (1) { - if (op > (omax - 2)) { return ERROR(dstSize_tooSmall); } - *op++ = FSE_GETSYMBOL(&state1); - if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) { - *op++ = FSE_GETSYMBOL(&state2); - break; - } - - if (op > (omax - 2)) { return ERROR(dstSize_tooSmall); } - *op++ = FSE_GETSYMBOL(&state2); - if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) { - *op++ = FSE_GETSYMBOL(&state1); - break; - } - } - - return op - ostart; -} - -size_t FSE_decompress_usingDTable(void* dst, size_t originalSize, const void* cSrc, size_t cSrcSize, - const FSE_DTable* dt) -{ - const void* ptr = dt; - const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr; - const U32 fastMode = DTableH->fastMode; - - /* select fast mode (static) */ - if (fastMode) { - return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1); - } - return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0); -} - -size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, - FSE_DTable* workSpace, unsigned maxLog) -{ - const BYTE* const istart = (const BYTE*)cSrc; - const BYTE* ip = istart; - short counting[FSE_MAX_SYMBOL_VALUE + 1]; - unsigned tableLog; - unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE; - - /* normal FSE decoding mode */ - size_t const NCountLength = - FSE_readNCount(counting, &maxSymbolValue, &tableLog, istart, cSrcSize); - if (FSE_isError(NCountLength)) { return NCountLength; } - if (tableLog > maxLog) { return ERROR(tableLog_tooLarge); } - assert(NCountLength <= cSrcSize); - ip += NCountLength; - cSrcSize -= NCountLength; - - CHECK_F(FSE_buildDTable(workSpace, counting, maxSymbolValue, tableLog)); - - return FSE_decompress_usingDTable(dst, dstCapacity, ip, cSrcSize, - workSpace); /* always return, even if it is an error code */ -} - -typedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)]; - -size_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize) -{ - DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly - initialized later */ - return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, dt, FSE_MAX_TABLELOG); -} - -#endif /* FSE_COMMONDEFS_ONLY */ diff --git a/ucm/store/compress/cc/compress_lib/hist.cc b/ucm/store/compress/cc/compress_lib/hist.cc deleted file mode 100644 index a7ba64dd8..000000000 --- a/ucm/store/compress/cc/compress_lib/hist.cc +++ /dev/null @@ -1,509 +0,0 @@ -/* ****************************************************************** - * hist : Histogram functions - * part of Finite State Entropy project - * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. - * - * You can contact the author at : - * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy - * - Public forum : https://groups.google.com/forum/# !forum/lz4c - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - ****************************************************************** */ - -/* --- dependencies --- */ -#include "hist.h" -#include "debug.h" /* assert, DEBUGLOG */ -#include "error_private.h" /* ERROR */ -#include "mem.h" /* U32, BYTE, etc. */ - -/* --- Error management --- */ -unsigned HIST_isError(size_t code) { return ERR_isError(code); } - -/*-************************************************************** - * Histogram functions - ****************************************************************/ -unsigned HIST_count_simple(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize) -{ - const BYTE* ip = (const BYTE*)src; - const BYTE* const end = ip + srcSize; - unsigned maxSymbolValue = *maxSymbolValuePtr; - unsigned largestCount = 0; - - memset(count, 0, (maxSymbolValue + 1) * sizeof(*count)); - if (srcSize == 0) { - *maxSymbolValuePtr = 0; - return 0; - } - - while (ip < end) { - assert(*ip <= maxSymbolValue); - count[*ip++]++; - } - - while (!count[maxSymbolValue]) { maxSymbolValue--; } - *maxSymbolValuePtr = maxSymbolValue; - - { - U32 s; - for (s = 0; s <= maxSymbolValue; s++) { - if (count[s] > largestCount) { largestCount = count[s]; } - } - } - - return largestCount; -} - -typedef enum { trustInput, checkMaxSymbolValue } HIST_checkInput_e; - -/* HIST_count_parallel_wksp() : - * store histogram into 4 intermediate tables, recombined at the end. - * this design makes better use of OoO cpus, - * and is noticeably faster when some values are heavily repeated. - * But it needs some additional workspace for intermediate tables. - * `workSpace` must be a U32 table of size >= HIST_WKSP_SIZE_U32. - * @return : largest histogram frequency, - * or an error code (notably when histogram's alphabet is larger than *maxSymbolValuePtr) - */ -static size_t HIST_count_parallel_wksp(unsigned* count, unsigned* maxSymbolValuePtr, - const void* source, size_t sourceSize, - HIST_checkInput_e check, U32* const workSpace) -{ - const BYTE* ip = (const BYTE*)source; - const BYTE* const iend = ip + sourceSize; - size_t const countSize = (*maxSymbolValuePtr + 1) * sizeof(*count); - unsigned max = 0; - U32* const Counting1 = workSpace; - U32* const Counting2 = Counting1 + 256; - U32* const Counting3 = Counting2 + 256; - U32* const Counting4 = Counting3 + 256; - - /* safety checks */ - assert(*maxSymbolValuePtr <= 255); - if (!sourceSize) { - memset(count, 0, countSize); - *maxSymbolValuePtr = 0; - return 0; - } - memset(workSpace, 0, 4 * 256 * sizeof(unsigned)); - - /* by stripes of 16 bytes */ - { - U32 cached = MEM_read32(ip); - ip += 4; - while (ip < iend - 15) { - U32 c = cached; - cached = MEM_read32(ip); - ip += 4; - Counting1[static_cast(c)]++; - Counting2[static_cast(c >> 8)]++; - Counting3[static_cast(c >> 16)]++; - Counting4[c >> 24]++; - c = cached; - cached = MEM_read32(ip); - ip += 4; - Counting1[static_cast(c)]++; - Counting2[static_cast(c >> 8)]++; - Counting3[static_cast(c >> 16)]++; - Counting4[c >> 24]++; - c = cached; - cached = MEM_read32(ip); - ip += 4; - Counting1[static_cast(c)]++; - Counting2[static_cast(c >> 8)]++; - Counting3[static_cast(c >> 16)]++; - Counting4[c >> 24]++; - c = cached; - cached = MEM_read32(ip); - ip += 4; - Counting1[static_cast(c)]++; - Counting2[static_cast(c >> 8)]++; - Counting3[static_cast(c >> 16)]++; - Counting4[c >> 24]++; - } - ip -= 4; - } - - /* finish last symbols */ - while (ip < iend) { Counting1[*ip++]++; } - - { - U32 s; - for (s = 0; s < 256; s++) { - Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s]; - if (Counting1[s] > max) { max = Counting1[s]; } - } - } - - { - unsigned maxSymbolValue = 255; - while (!Counting1[maxSymbolValue]) { maxSymbolValue--; } - if (check && maxSymbolValue > *maxSymbolValuePtr) { return ERROR(maxSymbolValue_tooSmall); } - *maxSymbolValuePtr = maxSymbolValue; - memmove(count, Counting1, countSize); /* in case count & Counting1 are overlapping */ - } - return (size_t)max; -} - -size_t HIST_count_BF16(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, void* dst, size_t dstCapacity, size_t* compressionSize) -{ - const uint16_t* ip = (const uint16_t*)src; - const uint16_t* const iend = ip + srcSize; - size_t const countSize = (*maxSymbolValuePtr + 1) * sizeof(*count); - unsigned largestCount = 0; - BYTE* const ostart = (BYTE*)dst; - BYTE* op = ostart; - BYTE* oend = (BYTE*)dst + dstCapacity; - - /* safety checks */ - assert(*maxSymbolValuePtr <= 255); - if (!srcSize) { - memset(count, 0, countSize); - *maxSymbolValuePtr = 0; - return 0; - } - - /* counting */ - while (ip < iend && op < oend) { - // exponent_buffer[i] = (buffer[i] >> 7) & 0xFF; // 提取指数位 - count[((*ip) >> 7) & 0xFF]++; - *op++ = (((*ip >> 15) & 0x1) << 7) | (*ip & 0x7F); - ip++; - } - - *compressionSize = op - ostart; - { - U32 s; - for (s = 0; s < 256; s++) { - if (count[s] > largestCount) { largestCount = count[s]; } - } - } - - { - unsigned maxSymbolValue = 255; - while (!count[maxSymbolValue]) { maxSymbolValue--; } - if (maxSymbolValue > *maxSymbolValuePtr) { return ERROR(maxSymbolValue_tooSmall); } - *maxSymbolValuePtr = maxSymbolValue; - } - return (size_t)largestCount; -} - -size_t HIST_count_BF16_fixRatio(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize) -{ - if (srcSize == 0) { return 0; } - - // safety checks - assert(*maxSymbolValuePtr <= 255); - - // 统计直方图 - const uint16_t* ip = (const uint16_t*)src; - for (size_t i = 0; i < srcSize; i++) { - count[((ip[i]) >> 7) & 0xFF]++; // 提取指数位 - } - - // 找出统计值最大的symbol - U32 count_largest = 0; - U32 s_largest = 0; - { - U32 s; - for (s = 0; s < 256; s++) { - if (count_largest < count[s]) { - count_largest = count[s]; - s_largest = s; - } - } - } - - // 如果只有一个symbol,特殊处理一下,避免采用RLE编码 - if (count_largest == srcSize) { - if (s_largest == 0) { - count[1] = 1; - } else { - count[s_largest - 1] = 1; - } - } - - { - unsigned maxSymbolValue = 255; - while (!count[maxSymbolValue]) { maxSymbolValue--; } - if (maxSymbolValue > *maxSymbolValuePtr) { return ERROR(maxSymbolValue_tooSmall); } - *maxSymbolValuePtr = maxSymbolValue; - } - - return (size_t)count_largest; -} - -size_t HIST_count_FP16(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, void* dst, size_t dstCapacity, size_t* compressionSize) -{ - const uint16_t* ip = (const uint16_t*)src; - const uint16_t* const iend = ip + srcSize; - size_t const countSize = (*maxSymbolValuePtr + 1) * sizeof(*count); - unsigned largestCount = 0; - BYTE* const ostart = (BYTE*)dst; - BYTE* op = ostart; - BYTE* oend = (BYTE*)dst + dstCapacity; - - /* safety checks */ - assert(*maxSymbolValuePtr <= 255); - if (!srcSize) { - memset(count, 0, countSize); - *maxSymbolValuePtr = 0; - return 0; - } - - // 一次处理 64 个数 8*8 - // | 8B s | 16B m2 | 64B m8 | 8B s | 16B m2 | 64B m8 | 8B s | 16B m2 | 64B m8 | - uint8_t* sign = op; - uint16_t* mantissa2 = (uint16_t*)(op + 8); - uint64_t* mantissa8 = (uint64_t*)(op + 24); - - assert(srcSize % 64 == 0); - while (ip + 64 <= iend && op + 88 <= oend) { - for (int g = 0; g < 8; g++) { // 每组 8 个数 - // 一次读 8 个字节(两个 U32) - U64 c1 = MEM_read64(ip); - ip += 4; - U64 c2 = MEM_read64(ip); - ip += 4; - - // 展开 8 次,每次处理 1 个字节 - uint16_t vals[8] = {static_cast(c1), static_cast(c1 >> 16), - static_cast(c1 >> 32), static_cast(c1 >> 48), - static_cast(c2), static_cast(c2 >> 16), - static_cast(c2 >> 32), static_cast(c2 >> 48)}; - - for (int pos = 0; pos < 8; pos++) { - uint16_t v = vals[pos]; - unsigned sym = (v >> 10) & 0x1F; - count[sym]++; - - sign[pos] |= ((v >> 15) & 0x1) << g; - mantissa2[pos] |= static_cast((v >> 8) & 0x3) << (g * 2); - mantissa8[pos] |= static_cast(v & 0xFF) << (g * 8); - } - } - - op += 88; - sign = op; - mantissa2 = (uint16_t*)(op + 8); - mantissa8 = (uint64_t*)(op + 24); - } - - *compressionSize = op - ostart; - { - U32 s; - for (s = 0; s < 256; s++) { - if (count[s] > largestCount) { largestCount = count[s]; } - } - } - - { - unsigned maxSymbolValue = 255; - while (!count[maxSymbolValue]) { maxSymbolValue--; } - if (maxSymbolValue > *maxSymbolValuePtr) { return ERROR(maxSymbolValue_tooSmall); } - *maxSymbolValuePtr = maxSymbolValue; - } - return (size_t)largestCount; -} - -size_t HIST_lossy_count_FP16(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, void* dst, size_t dstCapacity, size_t* compressionSize) -{ - const uint16_t* ip = (const uint16_t*)src; - const uint16_t* const iend = ip + srcSize; - size_t const countSize = (*maxSymbolValuePtr + 1) * sizeof(*count); - unsigned largestCount = 0; - BYTE* const ostart = (BYTE*)dst; - BYTE* op = ostart; - BYTE* oend = (BYTE*)dst + dstCapacity; - - /* safety checks */ - assert(*maxSymbolValuePtr <= 255); - if (!srcSize) { - memset(count, 0, countSize); - *maxSymbolValuePtr = 0; - return 0; - } - - // 一次处理 64 个数 8*8 , 尾数部分截断保留4位 - // | 8B s | 32B m4 | 8B s | 32B m4| 8B s | 32B m4 | 8B s | 32B m4 | - uint8_t* sign = op; - uint32_t* mantissa4 = (uint32_t*)(op + 8); - - assert(srcSize % 64 == 0); - while (ip + 64 <= iend && op + 40 <= oend) { - for (int g = 0; g < 8; g++) { // 每组 8 个数 - // 一次读 8 个字节(两个 U32) - U64 c1 = MEM_read64(ip); - ip += 4; - U64 c2 = MEM_read64(ip); - ip += 4; - - // 展开 8 次,每次处理 1 个字节 - uint16_t vals[8] = {static_cast(c1), static_cast(c1 >> 16), - static_cast(c1 >> 32), static_cast(c1 >> 48), - static_cast(c2), static_cast(c2 >> 16), - static_cast(c2 >> 32), static_cast(c2 >> 48)}; - - for (int pos = 0; pos < 8; pos++) { - uint16_t v = vals[pos]; - unsigned sym = (v >> 10) & 0x1F; - count[sym]++; - - sign[pos] |= ((v >> 15) & 0x1) << g; - mantissa4[pos] |= static_cast((v >> 6) & 0xF) << (g * 4); - } - } - - op += 40; - sign = op; - mantissa4 = (uint32_t*)(op + 8); - } - - *compressionSize = op - ostart; - { - U32 s; - for (s = 0; s < 256; s++) { - if (count[s] > largestCount) { largestCount = count[s]; } - } - } - - { - unsigned maxSymbolValue = 255; - while (!count[maxSymbolValue]) { maxSymbolValue--; } - if (maxSymbolValue > *maxSymbolValuePtr) { return ERROR(maxSymbolValue_tooSmall); } - *maxSymbolValuePtr = maxSymbolValue; - } - return (size_t)largestCount; -} - -size_t HIST_count_FP8E5M2(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, void* dst, size_t dstCapacity, size_t* compressionSize) -{ - const uint8_t* ip = (const uint8_t*)src; - const uint8_t* const iend = ip + srcSize; - size_t const countSize = (*maxSymbolValuePtr + 1) * sizeof(*count); - unsigned largestCount = 0; - BYTE* const ostart = (BYTE*)dst; - BYTE* op = ostart; - BYTE* oend = (BYTE*)dst + dstCapacity; - - /* safety checks */ - assert(*maxSymbolValuePtr <= 255); - if (!srcSize) { - memset(count, 0, countSize); - *maxSymbolValuePtr = 0; - return 0; - } - - // 一次处理 64 个数 8*8 - // | 8B s | 16B m | 8B s | 16B m | 8B s | 16B m | - uint8_t* sign = op; - uint16_t* mantissa16 = (uint16_t*)(op + 8); - - assert(srcSize % 64 == 0); - while (ip + 64 <= iend && op + 24 <= oend) { - for (int g = 0; g < 8; g++) { // 每组 8 个数 - // 一次读 8 个字节(两个 U32) - U32 c1 = MEM_read32(ip); - ip += 4; - U32 c2 = MEM_read32(ip); - ip += 4; - - // 展开 8 次,每次处理 1 个字节 - uint8_t vals[8] = {static_cast(c1), static_cast(c1 >> 8), - static_cast(c1 >> 16), static_cast(c1 >> 24), - static_cast(c2), static_cast(c2 >> 8), - static_cast(c2 >> 16), static_cast(c2 >> 24)}; - - for (int pos = 0; pos < 8; pos++) { - uint8_t v = vals[pos]; - unsigned sym = (v >> 2) & 0x1F; - count[sym]++; - - sign[pos] |= ((v >> 7) & 0x1) << g; - mantissa16[pos] |= static_cast(v & 0x3) << (g * 2); - } - } - - op += 24; - sign = op; - mantissa16 = (uint16_t*)(op + 8); - } - - *compressionSize = op - ostart; - { - U32 s; - for (s = 0; s < 256; s++) { - if (count[s] > largestCount) { largestCount = count[s]; } - } - } - - { - unsigned maxSymbolValue = 255; - while (!count[maxSymbolValue]) { maxSymbolValue--; } - if (maxSymbolValue > *maxSymbolValuePtr) { return ERROR(maxSymbolValue_tooSmall); } - *maxSymbolValuePtr = maxSymbolValue; - } - return (size_t)largestCount; -} - -/* HIST_countFast_wksp() : - * Same as HIST_countFast(), but using an externally provided scratch buffer. - * `workSpace` is a writable buffer which must be 4-bytes aligned, - * `workSpaceSize` must be >= HIST_WKSP_SIZE - */ -size_t HIST_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr, const void* source, - size_t sourceSize, void* workSpace, size_t workSpaceSize) -{ - if (sourceSize < 1500) /* heuristic threshold */ { - return HIST_count_simple(count, maxSymbolValuePtr, source, sourceSize); - } - if ((size_t)workSpace & 3) { - return ERROR(GENERIC); - } /* must be aligned on 4-bytes boundaries */ - if (workSpaceSize < HIST_WKSP_SIZE) { return ERROR(workSpace_tooSmall); } - return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, trustInput, - (U32*)workSpace); -} - -/* fast variant (unsafe : won't check if src contains values beyond count[] limit) */ -size_t HIST_countFast(unsigned* count, unsigned* maxSymbolValuePtr, const void* source, - size_t sourceSize) -{ - unsigned tmpCounters[HIST_WKSP_SIZE_U32]; - return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, tmpCounters, - sizeof(tmpCounters)); -} - -/* HIST_count_wksp() : - * Same as HIST_count(), but using an externally provided scratch buffer. - * `workSpace` size must be table of >= HIST_WKSP_SIZE_U32 unsigned */ -size_t HIST_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr, const void* source, - size_t sourceSize, void* workSpace, size_t workSpaceSize) -{ - if ((size_t)workSpace & 3) { - return ERROR(GENERIC); - } /* must be aligned on 4-bytes boundaries */ - if (workSpaceSize < HIST_WKSP_SIZE) { return ERROR(workSpace_tooSmall); } - if (*maxSymbolValuePtr < 255) { - return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, - checkMaxSymbolValue, (U32*)workSpace); - } - *maxSymbolValuePtr = 255; - return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace, - workSpaceSize); -} - -size_t HIST_count(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize) -{ - unsigned tmpCounters[HIST_WKSP_SIZE_U32]; - return HIST_count_wksp(count, maxSymbolValuePtr, src, srcSize, tmpCounters, - sizeof(tmpCounters)); -} diff --git a/ucm/store/compress/cc/compress_lib/hist.h b/ucm/store/compress/cc/compress_lib/hist.h deleted file mode 100644 index b156564d7..000000000 --- a/ucm/store/compress/cc/compress_lib/hist.h +++ /dev/null @@ -1,87 +0,0 @@ -/* ****************************************************************** - * hist : Histogram functions - * part of Finite State Entropy project - * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. - * - * You can contact the author at : - * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy - * - Public forum : https://groups.google.com/forum/# !forum/lz4c - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - ****************************************************************** */ - -/* --- dependencies --- */ -#include /* size_t */ - -/* --- simple histogram functions --- */ - -/*! HIST_count(): - * Provides the precise count of each byte within a table 'count'. - * 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1). - * Updates *maxSymbolValuePtr with actual largest symbol value detected. - * @return : count of the most frequent symbol (which isn't identified). - * or an error code, which can be tested using HIST_isError(). - * note : if return == srcSize, there is only one symbol. - */ -size_t HIST_count(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize); - -unsigned HIST_isError(size_t code); /**< tells if a return value is an error code */ - -/* --- advanced histogram functions --- */ - -#define HIST_WKSP_SIZE_U32 1024 -#define HIST_WKSP_SIZE (HIST_WKSP_SIZE_U32 * sizeof(unsigned)) -/** HIST_count_wksp() : - * Same as HIST_count(), but using an externally provided scratch buffer. - * Benefit is this function will use very little stack space. - * `workSpace` is a writable buffer which must be 4-bytes aligned, - * `workSpaceSize` must be >= HIST_WKSP_SIZE - */ -size_t HIST_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, void* workSpace, size_t workSpaceSize); - -/** HIST_countFast() : - * same as HIST_count(), but blindly trusts that all byte values within src are <= - * *maxSymbolValuePtr. This function is unsafe, and will segfault if any value within `src` is `> - * *maxSymbolValuePtr` - */ -size_t HIST_countFast(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize); - -/** HIST_countFast_wksp() : - * Same as HIST_countFast(), but using an externally provided scratch buffer. - * `workSpace` is a writable buffer which must be 4-bytes aligned, - * `workSpaceSize` must be >= HIST_WKSP_SIZE - */ -size_t HIST_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, void* workSpace, size_t workSpaceSize); - -/*! HIST_count_simple() : - * Same as HIST_countFast(), this function is unsafe, - * and will segfault if any value within `src` is `> *maxSymbolValuePtr`. - * It is also a bit slower for large inputs. - * However, it does not need any additional memory (not even on stack). - * @return : count of the most frequent symbol. - * Note this function doesn't produce any error (i.e. it must succeed). - */ -unsigned HIST_count_simple(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize); - -size_t HIST_count_BF16(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, void* dst, size_t dstSize, size_t* compressionSize); - -size_t HIST_count_FP16(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, void* dst, size_t dstCapacity, size_t* compressionSize); - -size_t HIST_count_FP8E5M2(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, void* dst, size_t dstCapacity, size_t* compressionSize); - -size_t HIST_lossy_count_FP16(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, void* dst, size_t dstCapacity, - size_t* compressionSize); - -size_t HIST_count_BF16_fixRatio(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize); diff --git a/ucm/store/compress/cc/compress_lib/huf.h b/ucm/store/compress/cc/compress_lib/huf.h deleted file mode 100644 index 48425e1c7..000000000 --- a/ucm/store/compress/cc/compress_lib/huf.h +++ /dev/null @@ -1,415 +0,0 @@ -/* ****************************************************************** - * huff0 huffman codec, - * part of Finite State Entropy library - * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. - * - * You can contact the author at : - * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - ****************************************************************** */ - -#if defined(__cplusplus) -extern "C" { -#endif - -#ifndef HUF_H_298734234 -#define HUF_H_298734234 - -/* *** Dependencies *** */ -#include /* size_t */ - -/* *** library symbols visibility *** */ -/* Note : when linking with -fvisibility=hidden on gcc, or by default on Visual, - * HUF symbols remain "private" (internal symbols for library only). - * Set macro FSE_DLL_EXPORT to 1 if you want HUF symbols visible on DLL interface */ -#if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT == 1) && defined(__GNUC__) && (__GNUC__ >= 4) -#define HUF_PUBLIC_API __attribute__((visibility("default"))) -#elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT == 1) /* Visual expected */ -#define HUF_PUBLIC_API __declspec(dllexport) -#elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT == 1) -#define HUF_PUBLIC_API \ - __declspec(dllimport) /* not required, just to generate faster code (saves a function pointer \ - load from IAT and an indirect jump) */ -#else -#define HUF_PUBLIC_API -#endif - -typedef enum { - R1 = 32, - R133 = 24, // 32 / 24 = 1.33x - R139 = 23, // 32 / 23 = 1.39x - R145 = 22, // 32 / 22 = 1.45x - R152 = 21, // 32 / 21 = 1.52x - R200 = 16 // 32 / 16 = 2.00x -} FixedRatio; - -typedef enum { - DT_BF16 = 0, - DT_FP16 = 1, - DT_FP8E5M2 = 2, - DT_FP8E4M3 = 3, - DT_INVALID = 100 -} DataType; - -/* ========================== */ -/* *** simple functions *** */ -/* ========================== */ - -/** HUF_compress() : - * Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'. - * 'dst' buffer must be already allocated. - * Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize). - * `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB. - * @return : size of compressed data (<= `dstCapacity`). - * Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!! - * if HUF_isError(return), compression failed (more details using - * HUF_getErrorName()) - */ -HUF_PUBLIC_API size_t HUF_compress(void* dst, size_t dstCapacity, const void* src, size_t srcSize); - -size_t HUF_compress_float_fixRatio(void* dst, size_t maxDstSize, const void* src, size_t srcSize, - FixedRatio ratio, DataType dataType); -size_t HUF_decompress_float_fixRatio(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, - DataType* p_dataType); - -/** HUF_decompress() : - * Decompress HUF data from buffer 'cSrc', of size 'cSrcSize', - * into already allocated buffer 'dst', of minimum size 'dstSize'. - * `originalSize` : **must** be the ***exact*** size of original (uncompressed) data. - * Note : in contrast with FSE, HUF_decompress can regenerate - * RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data, - * because it knows size to regenerate (originalSize). - * @return : size of regenerated data (== originalSize), - * or an error code, which can be tested using HUF_isError() - */ -HUF_PUBLIC_API size_t HUF_decompress(void* dst, size_t originalSize, const void* cSrc, - size_t cSrcSize); - -/* *** Tool functions *** */ -#define HUF_BLOCKSIZE_MAX \ - (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */ -HUF_PUBLIC_API size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */ - -/* Error Management */ -HUF_PUBLIC_API unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */ -HUF_PUBLIC_API const char* HUF_getErrorName( - size_t code); /**< provides error code string (useful for debugging) */ - -/* *** Advanced function *** */ - -/** HUF_compress2() : - * Same as HUF_compress(), but offers control over `maxSymbolValue` and `tableLog`. - * `maxSymbolValue` must be <= HUF_SYMBOLVALUE_MAX . - * `tableLog` must be `<= HUF_TABLELOG_MAX` . */ -HUF_PUBLIC_API size_t HUF_compress2(void* dst, size_t dstCapacity, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned tableLog); - -/** HUF_compress4X_wksp() : - * Same as HUF_compress2(), but uses externally allocated `workSpace`. - * `workspace` must have minimum alignment of 4, and be at least as large as HUF_WORKSPACE_SIZE */ -#define HUF_WORKSPACE_SIZE ((6 << 10) + 256) -#define HUF_WORKSPACE_SIZE_U32 (HUF_WORKSPACE_SIZE / sizeof(U32)) -HUF_PUBLIC_API size_t HUF_compress4X_wksp(void* dst, size_t dstCapacity, const void* src, - size_t srcSize, unsigned maxSymbolValue, - unsigned tableLog, void* workSpace, size_t wkspSize); - -#endif /* HUF_H_298734234 */ - -/* ****************************************************************** - * WARNING !! - * The following section contains advanced and experimental definitions - * which shall never be used in the context of a dynamic library, - * because they are not guaranteed to remain stable in the future. - * Only consider them in association with static linking. - * *****************************************************************/ -#if defined(HUF_STATIC_LINKING_ONLY) && !defined(HUF_H_HUF_STATIC_LINKING_ONLY) -#define HUF_H_HUF_STATIC_LINKING_ONLY - -/* *** Dependencies *** */ -#include "mem.h" /* U32 */ - -/* *** Constants *** */ -#define HUF_TABLELOG_MAX \ - 12 /* max runtime value of tableLog (due to static allocation); can be modified up to \ - HUF_ABSOLUTEMAX_TABLELOG */ -#define HUF_TABLELOG_DEFAULT 11 /* default tableLog value when none specified */ -#define HUF_SYMBOLVALUE_MAX 255 - -#define HUF_TABLELOG_ABSOLUTEMAX \ - 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */ -#if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX) -#error "HUF_TABLELOG_MAX is too large !" -#endif - -/* **************************************** - * Static allocation - ******************************************/ -/* HUF buffer bounds */ -#define HUF_CTABLEBOUND 129 -#define HUF_BLOCKBOUND(size) \ - (size + (size >> 8) + 8) /* only true when incompressible is pre-filtered with fast heuristic \ - */ -#define HUF_COMPRESSBOUND(size) \ - (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ - -/* static allocation of HUF's Compression Table */ -#define HUF_CTABLE_SIZE_U32(maxSymbolValue) \ - ((maxSymbolValue) + 1) /* Use tables of U32, for proper alignment */ -#define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_U32(maxSymbolValue) * sizeof(U32)) -#define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \ - U32 name##hb[HUF_CTABLE_SIZE_U32(maxSymbolValue)]; \ - void* name##hv = &(name##hb); \ - HUF_CElt* name = (HUF_CElt*)(name##hv) /* no final ; */ - -/* static allocation of HUF's DTable */ -typedef U32 HUF_DTable; -#define HUF_DTABLE_SIZE(maxTableLog) (1 + (1 << (maxTableLog))) -#define HUF_CREATE_STATIC_DTABLEX1(DTable, maxTableLog) \ - HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog) - 1)] = { \ - ((U32)((maxTableLog) - 1) * 0x01000001)} -#define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \ - HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = {((U32)(maxTableLog) * 0x01000001)} - -/* **************************************** - * Advanced decompression functions - ******************************************/ -size_t HUF_decompress4X1(void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize); /**< single-symbol decoder */ -#ifndef HUF_FORCE_DECOMPRESS_X1 -size_t HUF_decompress4X2(void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize); /**< double-symbols decoder */ -#endif - -size_t HUF_decompress4X_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize); /**< decodes RLE and uncompressed */ -size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize); /**< considers RLE and uncompressed as errors */ -size_t HUF_decompress4X_hufOnly_wksp( - HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, - size_t wkspSize); /**< considers RLE and uncompressed as errors */ -size_t HUF_decompress4X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize); /**< single-symbol decoder */ -size_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, void* workSpace, - size_t wkspSize); /**< single-symbol decoder */ -#ifndef HUF_FORCE_DECOMPRESS_X1 -size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize); /**< double-symbols decoder */ -size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, void* workSpace, - size_t wkspSize); /**< double-symbols decoder */ -#endif - -/* **************************************** - * HUF detailed API - * ****************************************/ - -/*! HUF_compress() does the following: - * 1. count symbol occurrence from source[] into table count[] using FSE_count() (exposed within - * "fse.h") - * 2. (optional) refine tableLog using HUF_optimalTableLog() - * 3. build Huffman table from count using HUF_buildCTable() - * 4. save Huffman table to memory buffer using HUF_writeCTable() - * 5. encode the data stream using HUF_compress4X_usingCTable() - * - * The following API allows targeting specific sub-functions for advanced tasks. - * For example, it's possible to compress several blocks using the same 'CTable', - * or to save and regenerate 'CTable' using external methods. - */ -unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue); -typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */ -size_t HUF_buildCTable(HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, - unsigned maxNbBits); /* @return : maxNbBits; CTable and count can overlap. In - which case, CTable will overwrite count content */ -size_t HUF_writeCTable(void* dst, size_t maxDstSize, const HUF_CElt* CTable, - unsigned maxSymbolValue, unsigned huffLog); -size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, - const HUF_CElt* CTable); -size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, - unsigned maxSymbolValue); -int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue); - -typedef enum { - HUF_repeat_none, /**< Cannot use the previous table */ - HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous - table must have been constructed by HUF_compress{1, 4}X_repeat */ - HUF_repeat_valid /**< Can use the previous table and it is assumed to be valid */ -} HUF_repeat; -/** HUF_compress4X_repeat() : - * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none. - * If it uses hufTable it does not modify hufTable or repeat. - * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used. - * If preferRepeat then the old table will always be used if valid. */ -size_t HUF_compress4X_repeat( - void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, - unsigned tableLog, void* workSpace, - size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= - HUF_WORKSPACE_SIZE */ - HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2); - -/** HUF_buildCTable_wksp() : - * Same as HUF_buildCTable(), but using externally allocated scratch buffer. - * `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= - * HUF_CTABLE_WORKSPACE_SIZE. - */ -#define HUF_CTABLE_WORKSPACE_SIZE_U32 (2 * HUF_SYMBOLVALUE_MAX + 1 + 1) -#define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned)) -size_t HUF_buildCTable_wksp(HUF_CElt* tree, const unsigned* count, U32 maxSymbolValue, - U32 maxNbBits, void* workSpace, size_t wkspSize); - -/*! HUF_readStats() : - * Read compact Huffman tree, saved by HUF_writeCTable(). - * `huffWeight` is destination buffer. - * @return : size read from `src` , or an error Code . - * Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */ -size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats, U32* nbSymbolsPtr, - U32* tableLogPtr, const void* src, size_t srcSize); - -/** HUF_readCTable() : - * Loading a CTable saved with HUF_writeCTable() */ -size_t HUF_readCTable(HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, unsigned* hasZeroWeights); - -/** HUF_getNbBits() : - * Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX - * Note 1 : is not inlined, as HUF_CElt definition is private - * Note 2 : const void* used, so that it can provide a statically allocated table as argument - * (which uses type U32) */ -U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue); - -/* - * HUF_decompress() does the following: - * 1. select the decompression algorithm (X1, X2) based on pre-computed heuristics - * 2. build Huffman table from save, using HUF_readDTableX?() - * 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable() - */ - -/** HUF_selectDecoder() : - * Tells which decoder is likely to decode faster, - * based on a set of pre-computed metrics. - * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 . - * Assumption : 0 < dstSize <= 128 KB */ -U32 HUF_selectDecoder(size_t dstSize, size_t cSrcSize); - -/** - * The minimum workspace size for the `workSpace` used in - * HUF_readDTableX1_wksp() and HUF_readDTableX2_wksp(). - * - * The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when - * HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15. - * Buffer overflow errors may potentially occur if code modifications result in - * a required workspace size greater than that specified in the following - * macro. - */ -#define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10) -#define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32)) - -#ifndef HUF_FORCE_DECOMPRESS_X2 -size_t HUF_readDTableX1(HUF_DTable* DTable, const void* src, size_t srcSize); -size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, - size_t wkspSize); -#endif -#ifndef HUF_FORCE_DECOMPRESS_X1 -size_t HUF_readDTableX2(HUF_DTable* DTable, const void* src, size_t srcSize); -size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, - size_t wkspSize); -#endif - -size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, - const HUF_DTable* DTable); -#ifndef HUF_FORCE_DECOMPRESS_X2 -size_t HUF_decompress4X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, - size_t cSrcSize, const HUF_DTable* DTable); -#endif -#ifndef HUF_FORCE_DECOMPRESS_X1 -size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, - size_t cSrcSize, const HUF_DTable* DTable); -#endif - -/* ====================== */ -/* single stream variants */ -/* ====================== */ - -size_t HUF_compress1X(void* dst, size_t dstSize, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned tableLog); -size_t HUF_compress1X_wksp(void* dst, size_t dstSize, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned tableLog, void* workSpace, - size_t wkspSize); /**< `workSpace` must be a table of at least - HUF_WORKSPACE_SIZE_U32 unsigned */ - -/** HUF_compress1X_repeat() : - * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none. - * If it uses hufTable it does not modify hufTable or repeat. - * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used. - * If preferRepeat then the old table will always be used if valid. */ -size_t HUF_compress1X_repeat( - void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, - unsigned tableLog, void* workSpace, - size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= - HUF_WORKSPACE_SIZE */ - HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2); - -size_t HUF_decompress1X1(void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize); /* single-symbol decoder */ -#ifndef HUF_FORCE_DECOMPRESS_X1 -size_t HUF_decompress1X2(void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize); /* double-symbol decoder */ -#endif - -size_t HUF_decompress1X_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize); -size_t HUF_decompress1X_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, void* workSpace, size_t wkspSize); -#ifndef HUF_FORCE_DECOMPRESS_X2 -size_t HUF_decompress1X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize); /**< single-symbol decoder */ -size_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, void* workSpace, - size_t wkspSize); /**< single-symbol decoder */ -#endif -#ifndef HUF_FORCE_DECOMPRESS_X1 -size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize); /**< double-symbols decoder */ -size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, void* workSpace, - size_t wkspSize); /**< double-symbols decoder */ -#endif - -size_t HUF_decompress1X_usingDTable( - void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, - const HUF_DTable* - DTable); /**< automatic selection of sing or double symbol decoder, based on DTable */ -#ifndef HUF_FORCE_DECOMPRESS_X2 -size_t HUF_decompress1X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, - size_t cSrcSize, const HUF_DTable* DTable); -#endif -#ifndef HUF_FORCE_DECOMPRESS_X1 -size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, - size_t cSrcSize, const HUF_DTable* DTable); -#endif - -/* BMI2 variants. - * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0. - */ -size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, - size_t cSrcSize, const HUF_DTable* DTable, int bmi2); -#ifndef HUF_FORCE_DECOMPRESS_X2 -size_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, - const void* cSrc, size_t cSrcSize, void* workSpace, - size_t wkspSize, int bmi2); -#endif -size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, - size_t cSrcSize, const HUF_DTable* DTable, int bmi2); -size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, - const void* cSrc, size_t cSrcSize, void* workSpace, - size_t wkspSize, int bmi2); - -#endif /* HUF_STATIC_LINKING_ONLY */ - -#if defined(__cplusplus) -} -#endif diff --git a/ucm/store/compress/cc/compress_lib/huf_compress.cc b/ucm/store/compress/cc/compress_lib/huf_compress.cc deleted file mode 100644 index a628cffa5..000000000 --- a/ucm/store/compress/cc/compress_lib/huf_compress.cc +++ /dev/null @@ -1,1272 +0,0 @@ -/* ****************************************************************** - * Huffman encoder, part of New Generation Entropy library - * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. - * - * You can contact the author at : - * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy - * - Public forum : https://groups.google.com/forum/# !forum/lz4c - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - ****************************************************************** */ - -/* ************************************************************** - * Compiler specifics - ****************************************************************/ -#ifdef _MSC_VER /* Visual Studio */ -#pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ -#endif - -/* ************************************************************** - * Includes - ****************************************************************/ -#include /* printf (debug) */ -#include /* memcpy, memset */ -#include "bitstream.h" -#include "compiler.h" -#include "hist.h" -#define FSE_STATIC_LINKING_ONLY /* FSE_optimalTableLog_internal */ -#include "fse.h" /* header compression */ -#define HUF_STATIC_LINKING_ONLY -#include "error_private.h" -#include "huf.h" - -/* ************************************************************** - * Error Management - ****************************************************************/ -#define HUF_isError ERR_isError -#define HUF_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */ - -/* ************************************************************** - * Utils - ****************************************************************/ -unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue) -{ - return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 1); -} - -/* ******************************************************* - * HUF : Huffman block compression - *********************************************************/ -/* HUF_compressWeights() : - * Same as FSE_compress(), but dedicated to huff0's weights compression. - * The use case needs much less stack memory. - * Note : all elements within weightTable are supposed to be <= HUF_TABLELOG_MAX. - */ -#define MAX_FSE_TABLELOG_FOR_HUFF_HEADER 6 -static size_t HUF_compressWeights(void* dst, size_t dstSize, const void* weightTable, size_t wtSize) -{ - BYTE* const ostart = (BYTE*)dst; - BYTE* op = ostart; - BYTE* const oend = ostart + dstSize; - - unsigned maxSymbolValue = HUF_TABLELOG_MAX; - U32 tableLog = MAX_FSE_TABLELOG_FOR_HUFF_HEADER; - - FSE_CTable CTable[FSE_CTABLE_SIZE_U32(MAX_FSE_TABLELOG_FOR_HUFF_HEADER, HUF_TABLELOG_MAX)]; - BYTE scratchBuffer[1 << MAX_FSE_TABLELOG_FOR_HUFF_HEADER]; - - unsigned count[HUF_TABLELOG_MAX + 1]; - S16 norm[HUF_TABLELOG_MAX + 1]; - - /* init conditions */ - if (wtSize <= 1) { return 0; } /* Not compressible */ - - /* Scan input and build symbol stats */ - { - unsigned const maxCount = - HIST_count_simple(count, &maxSymbolValue, weightTable, wtSize); /* never fails */ - if (maxCount == wtSize) { return 1; } /* only a single symbol in src : rle */ - if (maxCount == 1) { return 0; } /* each symbol present maximum once => not compressible */ - } - - tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue); - CHECK_F(FSE_normalizeCount(norm, tableLog, count, wtSize, maxSymbolValue)); - - /* Write table description header */ - { - CHECK_V_F(hSize, FSE_writeNCount(op, (size_t)(oend - op), norm, maxSymbolValue, tableLog)); - op += hSize; - } - - /* Compress */ - CHECK_F(FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, - sizeof(scratchBuffer))); - { - CHECK_V_F(cSize, - FSE_compress_usingCTable(op, (size_t)(oend - op), weightTable, wtSize, CTable)); - if (cSize == 0) { return 0; } /* not enough space for compressed data */ - op += cSize; - } - - return (size_t)(op - ostart); -} - -struct HUF_CElt_s { - U16 val; - BYTE nbBits; -}; /* typedef'd to HUF_CElt within "huf.h" */ - -/*! HUF_writeCTable() : - `CTable` : Huffman tree to save, using huf representation. - @return : size of saved CTable */ -size_t HUF_writeCTable(void* dst, size_t maxDstSize, const HUF_CElt* CTable, - unsigned maxSymbolValue, unsigned huffLog) -{ - BYTE bitsToWeight[HUF_TABLELOG_MAX + 1]; /* precomputed conversion table */ - BYTE huffWeight[HUF_SYMBOLVALUE_MAX]; - BYTE* op = (BYTE*)dst; - U32 n; - - /* check conditions */ - if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) { return ERROR(maxSymbolValue_tooLarge); } - - /* convert to weight */ - bitsToWeight[0] = 0; - for (n = 1; n < huffLog + 1; n++) { bitsToWeight[n] = (BYTE)(huffLog + 1 - n); } - for (n = 0; n < maxSymbolValue; n++) { huffWeight[n] = bitsToWeight[CTable[n].nbBits]; } - - // printf("before maxSymbolValue %d ....\n", maxSymbolValue); - /* attempt weights compression by FSE */ - { - CHECK_V_F(hSize, HUF_compressWeights(op + 1, maxDstSize - 1, huffWeight, maxSymbolValue)); - if ((hSize > 1) & (hSize < maxSymbolValue / 2)) { /* FSE compressed */ - op[0] = (BYTE)hSize; - return hSize + 1; - } - } - - // printf("maxSymbolValue %d ....\n", maxSymbolValue); - /* write raw values as 4-bits (max : 15) */ - if (maxSymbolValue > (256 - 128)) { - return ERROR(GENERIC); /* should not happen : likely means source cannot be compressed */ - } - if (((maxSymbolValue + 1) / 2) + 1 > maxDstSize) { - return ERROR(dstSize_tooSmall); /* not enough space within dst buffer */ - } - op[0] = (BYTE)(128 /*special case*/ + (maxSymbolValue - 1)); - huffWeight[maxSymbolValue] = - 0; /* to be sure it doesn't cause msan issue in final combination */ - for (n = 0; n < maxSymbolValue; n += 2) { - op[(n / 2) + 1] = (BYTE)((huffWeight[n] << 4) + huffWeight[n + 1]); - } - return ((maxSymbolValue + 1) / 2) + 1; -} - -size_t HUF_readCTable(HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, - size_t srcSize, unsigned* hasZeroWeights) -{ - BYTE huffWeight[HUF_SYMBOLVALUE_MAX + - 1]; /* init not required, even though some static analyzer may complain */ - U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1]; /* large enough for values from 0 to 16 */ - U32 tableLog = 0; - U32 nbSymbols = 0; - - /* get symbol weights */ - CHECK_V_F(readSize, HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, - &tableLog, src, srcSize)); - - /* check result */ - if (tableLog > HUF_TABLELOG_MAX) { return ERROR(tableLog_tooLarge); } - if (nbSymbols > *maxSymbolValuePtr + 1) { return ERROR(maxSymbolValue_tooSmall); } - - /* Prepare base value per rank */ - { - U32 n, nextRankStart = 0; - for (n = 1; n <= tableLog; n++) { - U32 current = nextRankStart; - nextRankStart += (rankVal[n] << (n - 1)); - rankVal[n] = current; - } - } - - /* fill nbBits */ - *hasZeroWeights = 0; - { - U32 n; - for (n = 0; n < nbSymbols; n++) { - const U32 w = huffWeight[n]; - *hasZeroWeights |= (w == 0); - CTable[n].nbBits = (BYTE)(tableLog + 1 - w) & -(w != 0); - } - } - - /* fill val */ - { - U16 nbPerRank[HUF_TABLELOG_MAX + 2] = {0}; /* support w=0=>n=tableLog+1 */ - U16 valPerRank[HUF_TABLELOG_MAX + 2] = {0}; - { - U32 n; - for (n = 0; n < nbSymbols; n++) { nbPerRank[CTable[n].nbBits]++; } - } - /* determine stating value per rank */ - valPerRank[tableLog + 1] = 0; /* for w==0 */ - { - U16 min = 0; - U32 n; - for (n = tableLog; n > 0; n--) { /* start at n=tablelog <-> w=1 */ - valPerRank[n] = min; /* get starting value within each rank */ - min += nbPerRank[n]; - min >>= 1; - } - } - /* assign value within rank, symbol order */ - { - U32 n; - for (n = 0; n < nbSymbols; n++) { CTable[n].val = valPerRank[CTable[n].nbBits]++; } - } - } - - *maxSymbolValuePtr = nbSymbols - 1; - return readSize; -} - -U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue) -{ - const HUF_CElt* table = (const HUF_CElt*)symbolTable; - assert(symbolValue <= HUF_SYMBOLVALUE_MAX); - return table[symbolValue].nbBits; -} - -typedef struct nodeElt_s { - U32 count; - U16 parent; - BYTE byte; - BYTE nbBits; -} nodeElt; - -static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits) -{ - const U32 largestBits = huffNode[lastNonNull].nbBits; - if (largestBits <= maxNbBits) { return largestBits; } /* early exit : no elt > maxNbBits */ - - /* there are several too large elements (at least >= 2) */ - { - int totalCost = 0; - const U32 baseCost = 1 << (largestBits - maxNbBits); - int n = (int)lastNonNull; - - while (huffNode[n].nbBits > maxNbBits) { - totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits)); - huffNode[n].nbBits = (BYTE)maxNbBits; - n--; - } /* n stops at huffNode[n].nbBits <= maxNbBits */ - while (huffNode[n].nbBits == maxNbBits) { - n--; /* n end at index of smallest symbol using < maxNbBits */ - } - - /* renorm totalCost */ - totalCost >>= - (largestBits - maxNbBits); /* note : totalCost is necessarily a multiple of baseCost */ - - /* repay normalized cost */ - { - U32 const noSymbol = 0xF0F0F0F0; - U32 rankLast[HUF_TABLELOG_MAX + 2]; - - /* Get pos of last (smallest) symbol per rank */ - memset(rankLast, 0xF0, sizeof(rankLast)); - { - U32 currentNbBits = maxNbBits; - int pos; - for (pos = n; pos >= 0; pos--) { - if (huffNode[pos].nbBits >= currentNbBits) { continue; } - currentNbBits = huffNode[pos].nbBits; /* < maxNbBits */ - rankLast[maxNbBits - currentNbBits] = (U32)pos; - } - } - - while (totalCost > 0) { - U32 nBitsToDecrease = BIT_highbit32((U32)totalCost) + 1; - for (; nBitsToDecrease > 1; nBitsToDecrease--) { - U32 const highPos = rankLast[nBitsToDecrease]; - U32 const lowPos = rankLast[nBitsToDecrease - 1]; - if (highPos == noSymbol) { continue; } - if (lowPos == noSymbol) { break; } - { - U32 const highTotal = huffNode[highPos].count; - U32 const lowTotal = 2 * huffNode[lowPos].count; - if (highTotal <= lowTotal) { break; } - } - } - /* only triggered when no more rank 1 symbol left => find closest one (note : there - * is necessarily at least one !) */ - /* HUF_MAX_TABLELOG test just to please gcc 5+; but it should not be necessary */ - while ((nBitsToDecrease <= HUF_TABLELOG_MAX) && - (rankLast[nBitsToDecrease] == noSymbol)) { - nBitsToDecrease++; - } - totalCost -= 1 << (nBitsToDecrease - 1); - if (rankLast[nBitsToDecrease - 1] == noSymbol) { - rankLast[nBitsToDecrease - 1] = - rankLast[nBitsToDecrease]; /* this rank is no longer empty */ - } - huffNode[rankLast[nBitsToDecrease]].nbBits++; - if (rankLast[nBitsToDecrease] == 0) { /* special case, reached largest symbol */ - rankLast[nBitsToDecrease] = noSymbol; - } else { - rankLast[nBitsToDecrease]--; - if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits - nBitsToDecrease) { - rankLast[nBitsToDecrease] = noSymbol; /* this rank is now empty */ - } - } - } /* while (totalCost > 0) */ - - while (totalCost < 0) { /* Sometimes, cost correction overshoot */ - if (rankLast[1] == - noSymbol) { /* special case : no rank 1 symbol (using maxNbBits-1); let's create - one from largest rank 0 (using maxNbBits) */ - while (huffNode[n].nbBits == maxNbBits) { n--; } - huffNode[n + 1].nbBits--; - assert(n >= 0); - rankLast[1] = (U32)(n + 1); - totalCost++; - continue; - } - huffNode[rankLast[1] + 1].nbBits--; - rankLast[1]++; - totalCost++; - } - } - } /* there are several too large elements (at least >= 2) */ - - return maxNbBits; -} - -typedef struct { - U32 base; - U32 current; -} rankPos; - -typedef nodeElt huffNodeTable[HUF_CTABLE_WORKSPACE_SIZE_U32]; - -#define RANK_POSITION_TABLE_SIZE 32 - -typedef struct { - huffNodeTable huffNodeTbl; - rankPos rankPosition[RANK_POSITION_TABLE_SIZE]; -} HUF_buildCTable_wksp_tables; - -static void HUF_sort(nodeElt* huffNode, const unsigned* count, U32 maxSymbolValue, - rankPos* rankPosition) -{ - U32 n; - - memset(rankPosition, 0, sizeof(*rankPosition) * RANK_POSITION_TABLE_SIZE); - for (n = 0; n <= maxSymbolValue; n++) { - U32 r = BIT_highbit32(count[n] + 1); - rankPosition[r].base++; - } - for (n = 30; n > 0; n--) { rankPosition[n - 1].base += rankPosition[n].base; } - for (n = 0; n < 32; n++) { rankPosition[n].current = rankPosition[n].base; } - for (n = 0; n <= maxSymbolValue; n++) { - U32 const c = count[n]; - U32 const r = BIT_highbit32(c + 1) + 1; - U32 pos = rankPosition[r].current++; - while ((pos > rankPosition[r].base) && (c > huffNode[pos - 1].count)) { - huffNode[pos] = huffNode[pos - 1]; - pos--; - } - huffNode[pos].count = c; - huffNode[pos].byte = (BYTE)n; - } -} - -/** HUF_buildCTable_wksp() : - * Same as HUF_buildCTable(), but using externally allocated scratch buffer. - * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as - * sizeof(HUF_buildCTable_wksp_tables). - */ -#define STARTNODE (HUF_SYMBOLVALUE_MAX + 1) - -size_t HUF_buildCTable_wksp(HUF_CElt* tree, const unsigned* count, U32 maxSymbolValue, - U32 maxNbBits, void* workSpace, size_t wkspSize) -{ - HUF_buildCTable_wksp_tables* const wksp_tables = (HUF_buildCTable_wksp_tables*)workSpace; - nodeElt* const huffNode0 = wksp_tables->huffNodeTbl; - nodeElt* const huffNode = huffNode0 + 1; - int nonNullRank; - int lowS, lowN; - int nodeNb = STARTNODE; - int n, nodeRoot; - - /* safety checks */ - if (((size_t)workSpace & 3) != 0) { - return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */ - } - if (wkspSize < sizeof(HUF_buildCTable_wksp_tables)) { return ERROR(workSpace_tooSmall); } - if (maxNbBits == 0) { maxNbBits = HUF_TABLELOG_DEFAULT; } - if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) { return ERROR(maxSymbolValue_tooLarge); } - memset(huffNode0, 0, sizeof(huffNodeTable)); - - /* sort, decreasing order */ - HUF_sort(huffNode, count, maxSymbolValue, wksp_tables->rankPosition); - - /* init for parents */ - nonNullRank = (int)maxSymbolValue; - while (huffNode[nonNullRank].count == 0) { nonNullRank--; } - lowS = nonNullRank; - nodeRoot = nodeNb + lowS - 1; - lowN = nodeNb; - huffNode[nodeNb].count = huffNode[lowS].count + huffNode[lowS - 1].count; - huffNode[lowS].parent = huffNode[lowS - 1].parent = (U16)nodeNb; - nodeNb++; - lowS -= 2; - for (n = nodeNb; n <= nodeRoot; n++) { huffNode[n].count = (U32)(1U << 30); } - huffNode0[0].count = (U32)(1U << 31); /* fake entry, strong barrier */ - - /* create parents */ - while (nodeNb <= nodeRoot) { - int const n1 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++; - int const n2 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++; - huffNode[nodeNb].count = huffNode[n1].count + huffNode[n2].count; - huffNode[n1].parent = huffNode[n2].parent = (U16)nodeNb; - nodeNb++; - } - - /* distribute weights (unlimited tree height) */ - huffNode[nodeRoot].nbBits = 0; - for (n = nodeRoot - 1; n >= STARTNODE; n--) { - huffNode[n].nbBits = huffNode[huffNode[n].parent].nbBits + 1; - } - for (n = 0; n <= nonNullRank; n++) { - huffNode[n].nbBits = huffNode[huffNode[n].parent].nbBits + 1; - } - - /* enforce maxTableLog */ - maxNbBits = HUF_setMaxHeight(huffNode, (U32)nonNullRank, maxNbBits); - - /* fill result into tree (val, nbBits) */ - { - U16 nbPerRank[HUF_TABLELOG_MAX + 1] = {0}; - U16 valPerRank[HUF_TABLELOG_MAX + 1] = {0}; - int const alphabetSize = (int)(maxSymbolValue + 1); - if (maxNbBits > HUF_TABLELOG_MAX) { return ERROR(GENERIC); } /* check fit into table */ - for (n = 0; n <= nonNullRank; n++) { nbPerRank[huffNode[n].nbBits]++; } - /* determine stating value per rank */ - { - U16 min = 0; - for (n = (int)maxNbBits; n > 0; n--) { - valPerRank[n] = min; /* get starting value within each rank */ - min += nbPerRank[n]; - min >>= 1; - } - } - for (n = 0; n < alphabetSize; n++) { - tree[huffNode[n].byte].nbBits = - huffNode[n].nbBits; /* push nbBits per symbol, symbol order */ - } - for (n = 0; n < alphabetSize; n++) { - tree[n].val = valPerRank[tree[n].nbBits]++; /* assign value within rank, symbol order */ - } - } - - return maxNbBits; -} - -/** HUF_buildCTable() : - * @return : maxNbBits - * Note : count is used before tree is written, so they can safely overlap - */ -size_t HUF_buildCTable(HUF_CElt* tree, const unsigned* count, unsigned maxSymbolValue, - unsigned maxNbBits) -{ - HUF_buildCTable_wksp_tables workspace; - return HUF_buildCTable_wksp(tree, count, maxSymbolValue, maxNbBits, &workspace, - sizeof(workspace)); -} - -size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, - unsigned maxSymbolValue) -{ - size_t nbBits = 0; - int s; - for (s = 0; s <= (int)maxSymbolValue; ++s) { nbBits += CTable[s].nbBits * count[s]; } - return nbBits >> 3; -} - -int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue) -{ - int bad = 0; - int s; - for (s = 0; s <= (int)maxSymbolValue; ++s) { bad |= (count[s] != 0) & (CTable[s].nbBits == 0); } - return !bad; -} - -size_t HUF_compressBound(size_t size) { return HUF_COMPRESSBOUND(size); } - -FORCE_INLINE_TEMPLATE void HUF_encodeSymbol(BIT_CStream_t* bitCPtr, U32 symbol, - const HUF_CElt* CTable) -{ - BIT_addBitsFast(bitCPtr, CTable[symbol].val, CTable[symbol].nbBits); -} - -#define HUF_FLUSHBITS(s) BIT_flushBits(s) - -#define HUF_FLUSHBITS_1(stream) \ - if (sizeof((stream)->bitContainer) * 8 < HUF_TABLELOG_MAX * 2 + 7) HUF_FLUSHBITS(stream) - -#define HUF_FLUSHBITS_2(stream) \ - if (sizeof((stream)->bitContainer) * 8 < HUF_TABLELOG_MAX * 4 + 7) HUF_FLUSHBITS(stream) - -FORCE_INLINE_TEMPLATE size_t HUF_compress1X_usingCTable_internal_body(void* dst, size_t dstSize, - const void* src, - size_t srcSize, - const HUF_CElt* CTable) -{ - const BYTE* ip = (const BYTE*)src; - BYTE* const ostart = (BYTE*)dst; - BYTE* const oend = ostart + dstSize; - BYTE* op = ostart; - size_t n; - BIT_CStream_t bitC; - - /* init */ - if (dstSize < 8) { return 0; } /* not enough space to compress */ - { - size_t const initErr = BIT_initCStream(&bitC, op, (size_t)(oend - op)); - if (HUF_isError(initErr)) { return 0; } - } - - n = srcSize & ~3; /* join to mod 4 */ - switch (srcSize & 3) { - case 3: - HUF_encodeSymbol(&bitC, ip[n + 2], CTable); - HUF_FLUSHBITS_2(&bitC); - /* fall-through */ - case 2: - HUF_encodeSymbol(&bitC, ip[n + 1], CTable); - HUF_FLUSHBITS_1(&bitC); - /* fall-through */ - case 1: - HUF_encodeSymbol(&bitC, ip[n + 0], CTable); - HUF_FLUSHBITS(&bitC); - /* fall-through */ - case 0: /* fall-through */ - default: break; - } - - for (; n > 0; n -= 4) { /* note : n&3==0 at this stage */ - HUF_encodeSymbol(&bitC, ip[n - 1], CTable); - HUF_FLUSHBITS_1(&bitC); - HUF_encodeSymbol(&bitC, ip[n - 2], CTable); - HUF_FLUSHBITS_2(&bitC); - HUF_encodeSymbol(&bitC, ip[n - 3], CTable); - HUF_FLUSHBITS_1(&bitC); - HUF_encodeSymbol(&bitC, ip[n - 4], CTable); - HUF_FLUSHBITS(&bitC); - } - - return BIT_closeCStream(&bitC); -} - -/* 计算按 stride=8、起始偏移 startIndex 时的元素个数: - 统计 s + 8*k < srcSize 的 k 个数 */ -FORCE_INLINE_TEMPLATE size_t HUF_countStride(size_t srcSize, size_t startIndex, size_t streamNum) -{ - if (startIndex >= srcSize) { return 0; } - /* count = floor((srcSize - 1 - startIndex)/8) + 1 = floor((srcSize - startIndex + 7)/8) */ - return (srcSize - startIndex + streamNum - 1) / streamNum; -} - -/* 单流按 stride=8 的压缩实现: - - base 指向原始源首地址 - - srcSize 是源总长度 - - startIndex 是本流的起始偏移(0/1/2/3/4/5/6/7) - - CTable 为 Huffman 编码表 - 该函数会按索引 pos(i) = startIndex + 8*i 的序列进行编码,并保持与原 1X 顺序一致的 flush 策略。 */ -FORCE_INLINE_TEMPLATE size_t HUF_compress1X_stride_usingCTable_internal_body_BF16( - void* dst, size_t dstSize, const uint16_t* base, size_t srcSize, size_t startIndex, - size_t streamNum, const HUF_CElt* CTable) -{ - BYTE* const ostart = (BYTE*)dst; - BYTE* const oend = ostart + dstSize; - BIT_CStream_t bitC; - - /* init */ - if (dstSize < 8) { return 0; } /* not enough space to compress */ - { - size_t const initErr = BIT_initCStream(&bitC, ostart, (size_t)(oend - ostart)); - if (HUF_isError(initErr)) { return 0; } - } - - /* 该流的元素总数(按 stride=8) */ - size_t const count = HUF_countStride(srcSize, startIndex, streamNum); - size_t n = count & ~((size_t)3); /* 对齐到 4 的倍数 */ - n *= streamNum; - - base += startIndex; - - /* 处理尾数(count % 4 个),顺序与原版一致:先编码尾部,再主循环从尾往前成组编码 */ - switch (count & 3) { - case 3: - HUF_encodeSymbol(&bitC, (uint8_t)(base[n + streamNum * 2] >> 7), CTable); - HUF_FLUSHBITS_2(&bitC); - case 2: - HUF_encodeSymbol(&bitC, (uint8_t)(base[n + streamNum] >> 7), CTable); - HUF_FLUSHBITS_1(&bitC); - case 1: HUF_encodeSymbol(&bitC, (uint8_t)(base[n] >> 7), CTable); HUF_FLUSHBITS(&bitC); - } - - /* 主循环:每轮处理 4 个符号,按照原版的倒序写入与刷新节奏 */ - for (; n > 0; n -= (4 * streamNum)) { - HUF_encodeSymbol(&bitC, (uint8_t)(base[n - streamNum] >> 7), CTable); - HUF_FLUSHBITS_1(&bitC); - HUF_encodeSymbol(&bitC, (uint8_t)(base[n - streamNum * 2] >> 7), CTable); - HUF_FLUSHBITS_2(&bitC); - HUF_encodeSymbol(&bitC, (uint8_t)(base[n - streamNum * 3] >> 7), CTable); - HUF_FLUSHBITS_1(&bitC); - HUF_encodeSymbol(&bitC, (uint8_t)(base[n - streamNum * 4] >> 7), CTable); - HUF_FLUSHBITS(&bitC); - } - - return BIT_closeCStream(&bitC); -} - -FORCE_INLINE_TEMPLATE size_t HUF_compress1X_usingCTable_internal_body_BF16( - void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable) -{ - const uint16_t* ip = (const uint16_t*)src; - BYTE* const ostart = (BYTE*)dst; - BYTE* const oend = ostart + dstSize; - BYTE* op = ostart; - size_t n; - BIT_CStream_t bitC; - - /* init */ - if (dstSize < 8) { return 0; } /* not enough space to compress */ - { - size_t const initErr = BIT_initCStream(&bitC, op, (size_t)(oend - op)); - if (HUF_isError(initErr)) { return 0; } - } - - n = srcSize & ~3; /* join to mod 4 */ - // printf("n : %zu\n", n); - switch (srcSize & 3) { - case 3: - HUF_encodeSymbol(&bitC, (ip[n + 2] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_2(&bitC); - /* fall-through */ - case 2: - HUF_encodeSymbol(&bitC, (ip[n + 1] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_1(&bitC); - /* fall-through */ - case 1: - HUF_encodeSymbol(&bitC, (ip[n + 0] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS(&bitC); - /* fall-through */ - case 0: /* fall-through */ - default: break; - } - - for (; n > 0; n -= 4) { /* note : n&3==0 at this stage */ - HUF_encodeSymbol(&bitC, (ip[n - 1] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_1(&bitC); - HUF_encodeSymbol(&bitC, (ip[n - 2] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_2(&bitC); - HUF_encodeSymbol(&bitC, (ip[n - 3] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_1(&bitC); - HUF_encodeSymbol(&bitC, (ip[n - 4] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS(&bitC); - } - - return BIT_closeCStream(&bitC); -} - -FORCE_INLINE_TEMPLATE size_t HUF_compress1X8_usingCTable_internal_body_BF16( - void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable) -{ - const uint16_t* ip = (const uint16_t*)src; - BYTE* const ostart = (BYTE*)dst; - BYTE* const oend = ostart + dstSize; - BYTE* op = ostart; - size_t n; - BIT_CStream_t bitC; - - /* init */ - if (dstSize < 8) { return 0; } /* not enough space to compress */ - { - size_t const initErr = BIT_initCStream(&bitC, op, (size_t)(oend - op)); - if (HUF_isError(initErr)) { return 0; } - } - - /* 余数处理:先处理 srcSize % 8 的尾部符号 */ - n = srcSize & ~7; /* 对齐到 8 的倍数 */ - switch (srcSize & 7) { - case 7: - HUF_encodeSymbol(&bitC, (ip[n + 6] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_1(&bitC); - /* fall-through */ - case 6: - HUF_encodeSymbol(&bitC, (ip[n + 5] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_2(&bitC); - /* fall-through */ - case 5: - HUF_encodeSymbol(&bitC, (ip[n + 4] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_1(&bitC); - /* fall-through */ - case 4: - HUF_encodeSymbol(&bitC, (ip[n + 3] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS(&bitC); - /* fall-through */ - case 3: - HUF_encodeSymbol(&bitC, (ip[n + 2] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_2(&bitC); - /* fall-through */ - case 2: - HUF_encodeSymbol(&bitC, (ip[n + 1] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_1(&bitC); - /* fall-through */ - case 1: - HUF_encodeSymbol(&bitC, (ip[n + 0] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS(&bitC); - /* fall-through */ - case 0: /* nothing */ - default: break; - } - - /* 主循环:一次处理 8 个符号 */ - for (; n > 0; n -= 8) { /* note : n&7==0 at this stage */ - HUF_encodeSymbol(&bitC, (ip[n - 1] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_1(&bitC); - HUF_encodeSymbol(&bitC, (ip[n - 2] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_2(&bitC); - HUF_encodeSymbol(&bitC, (ip[n - 3] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_1(&bitC); - HUF_encodeSymbol(&bitC, (ip[n - 4] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS(&bitC); - - HUF_encodeSymbol(&bitC, (ip[n - 5] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_1(&bitC); - HUF_encodeSymbol(&bitC, (ip[n - 6] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_2(&bitC); - HUF_encodeSymbol(&bitC, (ip[n - 7] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS_1(&bitC); - HUF_encodeSymbol(&bitC, (ip[n - 8] >> 7) & 0xFF, CTable); - HUF_FLUSHBITS(&bitC); - } - - return BIT_closeCStream(&bitC); -} - -static size_t HUF_compress1X_usingCTable_internal(void* dst, size_t dstSize, const void* src, - size_t srcSize, const HUF_CElt* CTable, - const int bmi2) -{ - (void)bmi2; - return HUF_compress1X_usingCTable_internal_body(dst, dstSize, src, srcSize, CTable); -} - -static size_t HUF_compress4X_usingCTable_internal(void* dst, size_t dstSize, const void* src, - size_t srcSize, const HUF_CElt* CTable, int bmi2) -{ - size_t const segmentSize = (srcSize + 3) / 4; /* first 3 segments */ - const BYTE* ip = (const BYTE*)src; - const BYTE* const iend = ip + srcSize; - BYTE* const ostart = (BYTE*)dst; - BYTE* const oend = ostart + dstSize; - BYTE* op = ostart; - - if (dstSize < 6 + 1 + 1 + 1 + 8) { return 0; } /* minimum space to compress successfully */ - if (srcSize < 12) { return 0; } /* no saving possible : too small input */ - op += 6; /* jumpTable */ - - assert(op <= oend); - { - CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend - op), ip, - segmentSize, CTable, bmi2)); - if (cSize == 0) { return 0; } - assert(cSize <= 65535); - MEM_writeLE16(ostart, (U16)cSize); - op += cSize; - } - - ip += segmentSize; - assert(op <= oend); - { - CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend - op), ip, - segmentSize, CTable, bmi2)); - if (cSize == 0) { return 0; } - assert(cSize <= 65535); - MEM_writeLE16(ostart + 2, (U16)cSize); - op += cSize; - } - - ip += segmentSize; - assert(op <= oend); - { - CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend - op), ip, - segmentSize, CTable, bmi2)); - if (cSize == 0) { return 0; } - assert(cSize <= 65535); - MEM_writeLE16(ostart + 4, (U16)cSize); - op += cSize; - } - - ip += segmentSize; - assert(op <= oend); - assert(ip <= iend); - { - CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend - op), ip, - (size_t)(iend - ip), CTable, bmi2)); - if (cSize == 0) { return 0; } - op += cSize; - } - - return (size_t)(op - ostart); -} - -size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, - const HUF_CElt* CTable) -{ - return HUF_compress4X_usingCTable_internal(dst, dstSize, src, srcSize, CTable, /* bmi2 */ 0); -} - -typedef enum { HUF_singleStream, HUF_fourStreams } HUF_nbStreams_e; - -static size_t HUF_compressCTable_internal(BYTE* const ostart, BYTE* op, BYTE* const oend, - const void* src, size_t srcSize, - HUF_nbStreams_e nbStreams, const HUF_CElt* CTable, - const int bmi2) -{ - size_t const cSize = (nbStreams == HUF_singleStream) - ? HUF_compress1X_usingCTable_internal(op, (size_t)(oend - op), src, - srcSize, CTable, bmi2) - : HUF_compress4X_usingCTable_internal(op, (size_t)(oend - op), src, - srcSize, CTable, bmi2); - if (HUF_isError(cSize)) { return cSize; } - if (cSize == 0) { return 0; } /* incompressible */ - op += cSize; - /* check compressibility */ - assert(op >= ostart); - if ((size_t)(op - ostart) >= srcSize - 1) { return 0; } - return (size_t)(op - ostart); -} - -/* 8X 压缩:按 stride=8 的交错方式分成 8 个流: - 流0: 0,8,... - 流1: 1,9,... - 流2: 2,10,... - 流3: 3,11,... - .... - 跳表写入每个流的压缩大小(前 7 个写入 16-bit) */ -static size_t HUF_compressCTable8x_stride_float_BF16(void* dst, size_t dstSize, const void* src, - size_t srcSize, const HUF_CElt* CTable) -{ - const uint16_t* ip = (const uint16_t*)src; - BYTE* const ostart = (BYTE*)dst; - BYTE* const oend = ostart + dstSize; - BYTE* op = ostart; - - if (dstSize < 14 + 7 + 8) { return 0; } /* minimum space to compress successfully */ - if (srcSize < 12) { return 0; } /* no saving possible : too small input */ - - op += 14; - assert(op <= oend); - - /* 流 0:起始偏移 0 */ - { - CHECK_V_F(cSize, HUF_compress1X_stride_usingCTable_internal_body_BF16( - op, (size_t)(oend - op), ip, srcSize, 0, 8, CTable)); - if (cSize == 0) { return 0; } - assert(cSize <= 65535); - MEM_writeLE16(ostart, (U16)cSize); - op += cSize; - } - - /* 流 1:起始偏移 1 */ - assert(op <= oend); - { - CHECK_V_F(cSize, HUF_compress1X_stride_usingCTable_internal_body_BF16( - op, (size_t)(oend - op), ip, srcSize, 1, 8, CTable)); - if (cSize == 0) { return 0; } - assert(cSize <= 65535); - MEM_writeLE16(ostart + 2, (U16)cSize); - op += cSize; - } - - /* 流 2:起始偏移 2 */ - assert(op <= oend); - { - CHECK_V_F(cSize, HUF_compress1X_stride_usingCTable_internal_body_BF16( - op, (size_t)(oend - op), ip, srcSize, 2, 8, CTable)); - if (cSize == 0) { return 0; } - assert(cSize <= 65535); - MEM_writeLE16(ostart + 4, (U16)cSize); - op += cSize; - } - - /* 流 3:起始偏移 3 */ - assert(op <= oend); - { - CHECK_V_F(cSize, HUF_compress1X_stride_usingCTable_internal_body_BF16( - op, (size_t)(oend - op), ip, srcSize, 3, 8, CTable)); - if (cSize == 0) { return 0; } - assert(cSize <= 65535); - MEM_writeLE16(ostart + 6, (U16)cSize); - op += cSize; - } - - /* 流 4:起始偏移 4 */ - assert(op <= oend); - { - CHECK_V_F(cSize, HUF_compress1X_stride_usingCTable_internal_body_BF16( - op, (size_t)(oend - op), ip, srcSize, 4, 8, CTable)); - if (cSize == 0) { return 0; } - assert(cSize <= 65535); - MEM_writeLE16(ostart + 8, (U16)cSize); - op += cSize; - } - - /* 流 5:起始偏移 5 */ - assert(op <= oend); - { - CHECK_V_F(cSize, HUF_compress1X_stride_usingCTable_internal_body_BF16( - op, (size_t)(oend - op), ip, srcSize, 5, 8, CTable)); - if (cSize == 0) { return 0; } - assert(cSize <= 65535); - MEM_writeLE16(ostart + 10, (U16)cSize); - op += cSize; - } - - /* 流 6:起始偏移 6 */ - assert(op <= oend); - { - CHECK_V_F(cSize, HUF_compress1X_stride_usingCTable_internal_body_BF16( - op, (size_t)(oend - op), ip, srcSize, 6, 8, CTable)); - if (cSize == 0) { return 0; } - assert(cSize <= 65535); - MEM_writeLE16(ostart + 12, (U16)cSize); - op += cSize; - } - - /* 流 7:起始偏移 7(长度不写入跳表,直接跟在前七个之后) */ - assert(op <= oend); - assert(ip <= iend); - { - CHECK_V_F(cSize, HUF_compress1X_stride_usingCTable_internal_body_BF16( - op, (size_t)(oend - op), ip, srcSize, 7, 8, CTable)); - if (cSize == 0) { return 0; } - op += cSize; - } - - return (size_t)(op - ostart); -} - -typedef struct { - unsigned count[HUF_SYMBOLVALUE_MAX + 1]; - HUF_CElt CTable[HUF_SYMBOLVALUE_MAX + 1]; - HUF_buildCTable_wksp_tables buildCTable_wksp; -} HUF_compress_tables_t; - -/* HUF_compress_internal() : - * `workSpace` must a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */ -static size_t HUF_compress_internal(void* dst, size_t dstSize, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned huffLog, - HUF_nbStreams_e nbStreams, void* workSpace, size_t wkspSize, - HUF_CElt* oldHufTable, HUF_repeat* repeat, int preferRepeat, - const int bmi2) -{ - HUF_compress_tables_t* const table = (HUF_compress_tables_t*)workSpace; - BYTE* const ostart = (BYTE*)dst; - BYTE* const oend = ostart + dstSize; - BYTE* op = ostart; - - HUF_STATIC_ASSERT(sizeof(*table) <= HUF_WORKSPACE_SIZE); - - /* checks & inits */ - if (((size_t)workSpace & 3) != 0) { - return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */ - } - if (wkspSize < HUF_WORKSPACE_SIZE) { return ERROR(workSpace_tooSmall); } - if (!srcSize) { return 0; } /* Uncompressed */ - if (!dstSize) { return 0; } /* cannot fit anything within dst budget */ - if (srcSize > HUF_BLOCKSIZE_MAX) { return ERROR(srcSize_wrong); } /* current block size limit */ - if (huffLog > HUF_TABLELOG_MAX) { return ERROR(tableLog_tooLarge); } - if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) { return ERROR(maxSymbolValue_tooLarge); } - if (!maxSymbolValue) { maxSymbolValue = HUF_SYMBOLVALUE_MAX; } - if (!huffLog) { huffLog = HUF_TABLELOG_DEFAULT; } - - /* Heuristic : If old table is valid, use it for small inputs */ - if (preferRepeat && repeat && *repeat == HUF_repeat_valid) { - return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, oldHufTable, - bmi2); - } - - /* Scan input and build symbol stats */ - { - CHECK_V_F(largest, HIST_count_wksp(table->count, &maxSymbolValue, (const BYTE*)src, srcSize, - workSpace, wkspSize)); - if (largest == srcSize) { - *ostart = ((const BYTE*)src)[0]; - return 1; - } /* single symbol, rle */ - if (largest <= (srcSize >> 7) + 4) { - return 0; /* heuristic : probably not compressible enough */ - } - } - - /* Check validity of previous table */ - if (repeat && *repeat == HUF_repeat_check && - !HUF_validateCTable(oldHufTable, table->count, maxSymbolValue)) { - *repeat = HUF_repeat_none; - } - /* Heuristic : use existing table for small inputs */ - if (preferRepeat && repeat && *repeat != HUF_repeat_none) { - return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, oldHufTable, - bmi2); - } - - /* Build Huffman Tree */ - huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue); - { - size_t const maxBits = - HUF_buildCTable_wksp(table->CTable, table->count, maxSymbolValue, huffLog, - &table->buildCTable_wksp, sizeof(table->buildCTable_wksp)); - CHECK_F(maxBits); - huffLog = (U32)maxBits; - /* Zero unused symbols in CTable, so we can check it for validity */ - memset(table->CTable + (maxSymbolValue + 1), 0, - sizeof(table->CTable) - ((maxSymbolValue + 1) * sizeof(HUF_CElt))); - } - - /* Write table description header */ - { - CHECK_V_F(hSize, HUF_writeCTable(op, dstSize, table->CTable, maxSymbolValue, huffLog)); - /* Check if using previous huffman table is beneficial */ - if (repeat && *repeat != HUF_repeat_none) { - size_t const oldSize = - HUF_estimateCompressedSize(oldHufTable, table->count, maxSymbolValue); - size_t const newSize = - HUF_estimateCompressedSize(table->CTable, table->count, maxSymbolValue); - if (oldSize <= hSize + newSize || hSize + 12 >= srcSize) { - return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, - oldHufTable, bmi2); - } - } - - /* Use the new huffman table */ - if (hSize + 12ul >= srcSize) { return 0; } - op += hSize; - if (repeat) { *repeat = HUF_repeat_none; } - if (oldHufTable) { - memcpy(oldHufTable, table->CTable, sizeof(table->CTable)); /* Save new table */ - } - } - return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, table->CTable, - bmi2); -} - -// HUF_compress_float_fixRatio_internal_bf16() : `workSpace` must a table of at least -// HUF_WORKSPACE_SIZE_U32 unsigned -static size_t HUF_compress_float_fixRatio_internal_bf16(void* dst, size_t dstSize, const void* src, - size_t srcSize, unsigned maxSymbolValue, - unsigned huffLog, void* workSpace, - size_t wkspSize, FixedRatio ratio, - DataType dataType) -{ - HUF_compress_tables_t* const table = (HUF_compress_tables_t*)workSpace; - BYTE* const ostart = (BYTE*)dst; - BYTE* const oend = ostart + dstSize; - BYTE* op = ostart; - - HUF_STATIC_ASSERT(sizeof(*table) <= HUF_WORKSPACE_SIZE); - - /* checks & inits */ - if (((size_t)workSpace & 3) != 0) { - return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */ - } - if (wkspSize < HUF_WORKSPACE_SIZE) { return ERROR(workSpace_tooSmall); } - if (!srcSize) { return 0; } // Uncompressed - if (srcSize & 1) { return 0; } // 由于是BF16, srcsize必须是偶数 - if (dstSize <= 16) { return 0; } // cannot fit anything within dst budget - if (srcSize > HUF_BLOCKSIZE_MAX) { return ERROR(srcSize_wrong); } /* current block size limit */ - if (huffLog > HUF_TABLELOG_MAX) { return ERROR(tableLog_tooLarge); } - if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) { return ERROR(maxSymbolValue_tooLarge); } - if (!maxSymbolValue) { maxSymbolValue = HUF_SYMBOLVALUE_MAX; } - if (!huffLog) { huffLog = HUF_TABLELOG_DEFAULT; } - - size_t count_total = srcSize / sizeof(uint16_t); // 浮点数总数量 - size_t comp_len = - (srcSize * (size_t)ratio / 32) / 4096 * 4096; // 压缩后总大小 (对齐到4096整数倍) - - // 直方图统计 ------------------------------------------------- - { - CHECK_V_F(largest, - HIST_count_BF16_fixRatio(table->count, &maxSymbolValue, src, count_total)); - // printf("largest %d \n", largest); - if (largest == 0) { return 0; } - } - - // Build Huffman Tree ------------------------------------------------- - huffLog = HUF_optimalTableLog(huffLog, count_total, maxSymbolValue); - { - size_t const maxBits = - HUF_buildCTable_wksp(table->CTable, table->count, maxSymbolValue, huffLog, - &table->buildCTable_wksp, sizeof(table->buildCTable_wksp)); - CHECK_F(maxBits); - huffLog = (U32)maxBits; - memset( - table->CTable + (maxSymbolValue + 1), 0, - sizeof(table->CTable) - - ((maxSymbolValue + 1) * - sizeof( - HUF_CElt))); // Zero unused symbols in CTable, so we can check it for validity - } - - op += 16; // 预留16byte,存放4个数(元数据) - - // 向目的地址中写入huffman编码表 ------------------------------------------------- - { - CHECK_V_F(hSize, HUF_writeCTable(op, oend - op, table->CTable, maxSymbolValue, huffLog)); - op += hSize; // 编码表大小 - // printf("hSize %d \n", hSize); - if ((size_t)(op - ostart) >= comp_len) { return 0; } // 检查是否超出压缩buffer的大小 - } - - // 写入huffman压缩的指数部分 ------------------------------------------------- - { - CHECK_V_F(cSize, HUF_compressCTable8x_stride_float_BF16( - op, oend - op, src, count_total, table->CTable)); // cSize是压缩后大小 - if (cSize == 0) { return 0; } // not enough space for compressed data - op += cSize; - if ((size_t)(op - ostart) >= comp_len) { return 0; } // 检查是否超出压缩buffer的大小 - } - - // 测算元数据 ------------------------------------------------- - size_t e_len = (op - ostart); // 指数部分压缩后大小 (算上了最开始的元数据) - size_t count_trunc; // 低精度尾数的浮点数的数量 - if (e_len + count_total > comp_len) { // 如果全部使用全精度放不下 - count_trunc = - (e_len + count_total - comp_len) * - 2; // e_len+count - // 是假设全都用全精度所占用的字节数量,再减去comp_len是超出缓冲的字节数量。由于每将2个全精度数转化为低精度可以省出1字节,因此将该数字*2就是低精度的数量。 - } else { // 如果全部使用全精度能放下 - count_trunc = 0; // 没有低精度 - } // - if (count_trunc > count_total) { - return 0; - } // 该条件成立,就说明即使全部转化为低精度,也放不下。报错 - size_t count_full = count_total - count_trunc; // 全精度尾数的浮点数的数量 - - // 写入元数据 ------------------------------------------------- - uint32_t* op_meta = (uint32_t*)dst; - op_meta[0] = (((uint8_t)dataType) << 24) | comp_len; // 元数据0: ratio 和 dataType - op_meta[1] = (((uint8_t)ratio) << 24) | count_total; // 元数据1: 浮点数总量 - op_meta[2] = count_full; // 元数据2: 全精度尾数的浮点数的数量 - op_meta[3] = e_len; // 元数据3: 尾数的偏移量,用该变量可以定位到尾数的起始存放地址 - - // 写入全精度部分 ------------------------------------------------- - const uint16_t* ip = (const uint16_t*)src; - for (size_t i = 0; i < count_full; i++) { - *op++ = (((ip[0] >> 15) & 0x1) << 7) | (ip[0] & 0x7F); - ip++; - } - - // 写入低精度部分 ------------------------------------------------- - for (size_t i = 0; i < count_trunc; i += 2) { - *op++ = (((ip[0] >> 15) & 0x1) << 7) | (((ip[0] >> 4) & 0x7) << 4) | - (((ip[1] >> 15) & 0x1) << 3) | (((ip[1] >> 4) & 0x7)); - ip += 2; - } - - return comp_len; -} - -static size_t HUF_compress_float_fixRatio_bf16(void* dst, size_t dstSize, const void* src, - size_t srcSize, FixedRatio ratio, DataType dataType) -{ - uint8_t* istart = (uint8_t*)src; - uint8_t* ip = istart; - uint8_t* iend = istart + srcSize; - - uint8_t* ostart = (uint8_t*)dst; - uint8_t* op = ostart; - - while (ip < iend) { - size_t hufBlockSize = iend - ip > HUF_BLOCKSIZE_MAX ? HUF_BLOCKSIZE_MAX : iend - ip; - unsigned workSpace[HUF_WORKSPACE_SIZE_U32] = {0}; - size_t compLen = (hufBlockSize * (size_t)ratio / 32) / 4096 * 4096; - CHECK_V_F(cSize, HUF_compress_float_fixRatio_internal_bf16( - op, compLen, ip, hufBlockSize, 255, HUF_TABLELOG_DEFAULT, workSpace, - sizeof(workSpace), ratio, dataType)); - ip += hufBlockSize; - op += cSize; - } - return op - ostart; -} - -size_t HUF_compress1X_wksp(void* dst, size_t dstSize, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned huffLog, void* workSpace, - size_t wkspSize) -{ - return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, - HUF_singleStream, workSpace, wkspSize, NULL, NULL, 0, 0 /*bmi2*/); -} - -size_t HUF_compress1X_repeat(void* dst, size_t dstSize, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned huffLog, void* workSpace, - size_t wkspSize, HUF_CElt* hufTable, HUF_repeat* repeat, - int preferRepeat, int bmi2) -{ - return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, - HUF_singleStream, workSpace, wkspSize, hufTable, repeat, - preferRepeat, bmi2); -} - -size_t HUF_compress1X(void* dst, size_t dstSize, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned huffLog) -{ - unsigned workSpace[HUF_WORKSPACE_SIZE_U32]; - return HUF_compress1X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, - sizeof(workSpace)); -} - -/* HUF_compress4X_repeat(): - * compress input using 4 streams. - * provide workspace to generate compression tables */ -size_t HUF_compress4X_wksp(void* dst, size_t dstSize, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned huffLog, void* workSpace, - size_t wkspSize) -{ - return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, - HUF_fourStreams, workSpace, wkspSize, NULL, NULL, 0, 0 /*bmi2*/); -} - -/* HUF_compress4X_repeat(): - * compress input using 4 streams. - * re-use an existing huffman compression table */ -size_t HUF_compress4X_repeat(void* dst, size_t dstSize, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned huffLog, void* workSpace, - size_t wkspSize, HUF_CElt* hufTable, HUF_repeat* repeat, - int preferRepeat, int bmi2) -{ - return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, - HUF_fourStreams, workSpace, wkspSize, hufTable, repeat, - preferRepeat, bmi2); -} - -size_t HUF_compress2(void* dst, size_t dstSize, const void* src, size_t srcSize, - unsigned maxSymbolValue, unsigned huffLog) -{ - unsigned workSpace[HUF_WORKSPACE_SIZE_U32]; - return HUF_compress4X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, - sizeof(workSpace)); -} - -size_t HUF_compress_float_fixRatio(void* dst, size_t maxDstSize, const void* src, size_t srcSize, - FixedRatio ratio, DataType dataType) -{ - switch (dataType) { - case DT_BF16: - return HUF_compress_float_fixRatio_bf16(dst, maxDstSize, src, srcSize, ratio, dataType); - default: return 0; // 暂时不支持 - } -} - -size_t HUF_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSize) -{ - return HUF_compress2(dst, maxDstSize, src, srcSize, 255, HUF_TABLELOG_DEFAULT); -} diff --git a/ucm/store/compress/cc/compress_lib/huf_decompress.cc b/ucm/store/compress/cc/compress_lib/huf_decompress.cc deleted file mode 100644 index ff1d80940..000000000 --- a/ucm/store/compress/cc/compress_lib/huf_decompress.cc +++ /dev/null @@ -1,1653 +0,0 @@ -/* ****************************************************************** - * huff0 huffman decoder, - * part of Finite State Entropy library - * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc. - * - * You can contact the author at : - * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - ****************************************************************** */ - -/* ************************************************************** - * Dependencies - ****************************************************************/ -#include /* memcpy, memset */ -#include "bitstream.h" /* BIT_* */ -#include "compiler.h" -#include "fse.h" /* to compress headers */ -#define HUF_STATIC_LINKING_ONLY -#include -#include "error_private.h" -#include "huf.h" - -/* ************************************************************** - * Macros - ****************************************************************/ - -/* These two optional macros force the use one way or another of the two - * Huffman decompression implementations. You can't force in both directions - * at the same time. - */ -#if defined(HUF_FORCE_DECOMPRESS_X1) && defined(HUF_FORCE_DECOMPRESS_X2) -#error "Cannot force the use of the X1 and X2 decoders at the same time!" -#endif - -/* ************************************************************** - * Error Management - ****************************************************************/ -#define HUF_isError ERR_isError - -/* ************************************************************** - * Byte alignment for workSpace management - ****************************************************************/ -#define HUF_ALIGN(x, a) HUF_ALIGN_MASK((x), (a) - 1) -#define HUF_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) - -/* ************************************************************** - * BMI2 Variant Wrappers - ****************************************************************/ -#if DYNAMIC_BMI2 - -#define HUF_DGEN(fn) \ - \ - static size_t fn##_default(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, \ - const HUF_DTable* DTable) \ - { \ - return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \ - } \ - \ - static TARGET_ATTRIBUTE("bmi2") size_t fn##_bmi2(void* dst, size_t dstSize, const void* cSrc, \ - size_t cSrcSize, const HUF_DTable* DTable) \ - { \ - return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \ - } \ - \ - static size_t fn(void* dst, size_t dstSize, void const* cSrc, size_t cSrcSize, \ - HUF_DTable const* DTable, int bmi2) \ - { \ - if (bmi2) { return fn##_bmi2(dst, dstSize, cSrc, cSrcSize, DTable); } \ - return fn##_default(dst, dstSize, cSrc, cSrcSize, DTable); \ - } - -#else - -#define HUF_DGEN(fn) \ - static size_t fn(void* dst, size_t dstSize, void const* cSrc, size_t cSrcSize, \ - HUF_DTable const* DTable, int bmi2) \ - { \ - (void)bmi2; \ - return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable); \ - } - -#endif - -/*-***************************/ -/* generic DTableDesc */ -/*-***************************/ -typedef struct { - BYTE maxTableLog; - BYTE tableType; - BYTE tableLog; - BYTE reserved; -} DTableDesc; - -static DTableDesc HUF_getDTableDesc(const HUF_DTable* table) -{ - DTableDesc dtd; - memcpy(&dtd, table, sizeof(dtd)); - return dtd; -} - -#ifndef HUF_FORCE_DECOMPRESS_X2 - -/*-***************************/ -/* single-symbol decoding */ -/*-***************************/ -typedef struct { - BYTE byte; - BYTE nbBits; -} HUF_DEltX1; /* single-symbol decoding */ - -size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, - size_t wkspSize) -{ - U32 tableLog = 0; - U32 nbSymbols = 0; - size_t iSize; - void* const dtPtr = DTable + 1; - HUF_DEltX1* const dt = (HUF_DEltX1*)dtPtr; - - U32* rankVal; - BYTE* huffWeight; - size_t spaceUsed32 = 0; - - rankVal = (U32*)workSpace + spaceUsed32; - spaceUsed32 += HUF_TABLELOG_ABSOLUTEMAX + 1; - huffWeight = (BYTE*)((U32*)workSpace + spaceUsed32); - spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2; - - if ((spaceUsed32 << 2) > wkspSize) { return ERROR(tableLog_tooLarge); } - - DEBUG_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable)); - /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer - complain ... */ - - iSize = HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, - srcSize); - if (HUF_isError(iSize)) { return iSize; } - - /* Table header */ - { - DTableDesc dtd = HUF_getDTableDesc(DTable); - if (tableLog > (U32)(dtd.maxTableLog + 1)) { - return ERROR(tableLog_tooLarge); /* DTable too small, Huffman tree cannot fit in */ - } - dtd.tableType = 0; - dtd.tableLog = (BYTE)tableLog; - memcpy(DTable, &dtd, sizeof(dtd)); - } - - /* Calculate starting value for each rank */ - { - U32 n, nextRankStart = 0; - for (n = 1; n < tableLog + 1; n++) { - U32 const current = nextRankStart; - nextRankStart += (rankVal[n] << (n - 1)); - rankVal[n] = current; - } - } - - /* fill DTable */ - { - U32 n; - size_t const nEnd = nbSymbols; - for (n = 0; n < nEnd; n++) { - size_t const w = huffWeight[n]; - size_t const length = (1 << w) >> 1; - size_t const uStart = rankVal[w]; - size_t const uEnd = uStart + length; - size_t u; - HUF_DEltX1 D; - D.byte = (BYTE)n; - D.nbBits = (BYTE)(tableLog + 1 - w); - rankVal[w] = (U32)uEnd; - if (length < 4) { - /* Use length in the loop bound so the compiler knows it is short. */ - for (u = 0; u < length; ++u) { dt[uStart + u] = D; } - } else { - /* Unroll the loop 4 times, we know it is a power of 2. */ - for (u = uStart; u < uEnd; u += 4) { - dt[u + 0] = D; - dt[u + 1] = D; - dt[u + 2] = D; - dt[u + 3] = D; - } - } - } - } - return iSize; -} - -size_t HUF_readDTableX1(HUF_DTable* DTable, const void* src, size_t srcSize) -{ - U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; - return HUF_readDTableX1_wksp(DTable, src, srcSize, workSpace, sizeof(workSpace)); -} - -FORCE_INLINE_TEMPLATE BYTE HUF_decodeSymbolX1(BIT_DStream_t* Dstream, const HUF_DEltX1* dt, - const U32 dtLog) -{ - size_t const val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */ - BYTE const c = dt[val].byte; - BIT_skipBits(Dstream, dt[val].nbBits); - return c; -} - -#define HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr) *ptr++ = HUF_decodeSymbolX1(DStreamPtr, dt, dtLog) - -#define HUF_DECODE_SYMBOLX1_1(ptr, DStreamPtr) \ - if (MEM_64bits() || (HUF_TABLELOG_MAX <= 12)) HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr) - -#define HUF_DECODE_SYMBOLX1_2(ptr, DStreamPtr) \ - if (MEM_64bits()) HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr) - -HINT_INLINE size_t HUF_decodeStreamX1(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, - const HUF_DEltX1* const dt, const U32 dtLog) -{ - BYTE* const pStart = p; - - /* up to 4 symbols at a time */ - while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd - 3)) { - HUF_DECODE_SYMBOLX1_2(p, bitDPtr); - HUF_DECODE_SYMBOLX1_1(p, bitDPtr); - HUF_DECODE_SYMBOLX1_2(p, bitDPtr); - HUF_DECODE_SYMBOLX1_0(p, bitDPtr); - } - - /* [0-3] symbols remaining */ - if (MEM_32bits()) { - while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd)) { - HUF_DECODE_SYMBOLX1_0(p, bitDPtr); - } - } - - /* no more data to retrieve from bitstream, no need to reload */ - while (p < pEnd) { HUF_DECODE_SYMBOLX1_0(p, bitDPtr); } - - return pEnd - pStart; -} - -FORCE_INLINE_TEMPLATE size_t HUF_decompress1X1_usingDTable_internal_body(void* dst, size_t dstSize, - const void* cSrc, - size_t cSrcSize, - const HUF_DTable* DTable) -{ - BYTE* op = (BYTE*)dst; - BYTE* const oend = op + dstSize; - const void* dtPtr = DTable + 1; - const HUF_DEltX1* const dt = (const HUF_DEltX1*)dtPtr; - BIT_DStream_t bitD; - DTableDesc const dtd = HUF_getDTableDesc(DTable); - U32 const dtLog = dtd.tableLog; - - CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize)); - - HUF_decodeStreamX1(op, &bitD, oend, dt, dtLog); - - if (!BIT_endOfDStream(&bitD)) { return ERROR(corruption_detected); } - - return dstSize; -} - -FORCE_INLINE_TEMPLATE size_t HUF_decompress4X1_usingDTable_internal_body(void* dst, size_t dstSize, - const void* cSrc, - size_t cSrcSize, - const HUF_DTable* DTable) -{ - /* Check */ - if (cSrcSize < 10) { - return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ - } - - { - const BYTE* const istart = (const BYTE*)cSrc; - BYTE* const ostart = (BYTE*)dst; - BYTE* const oend = ostart + dstSize; - BYTE* const olimit = oend - 3; - const void* const dtPtr = DTable + 1; - const HUF_DEltX1* const dt = (const HUF_DEltX1*)dtPtr; - - /* Init */ - BIT_DStream_t bitD1; - BIT_DStream_t bitD2; - BIT_DStream_t bitD3; - BIT_DStream_t bitD4; - size_t const length1 = MEM_readLE16(istart); - size_t const length2 = MEM_readLE16(istart + 2); - size_t const length3 = MEM_readLE16(istart + 4); - size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6); - const BYTE* const istart1 = istart + 6; /* jumpTable */ - const BYTE* const istart2 = istart1 + length1; - const BYTE* const istart3 = istart2 + length2; - const BYTE* const istart4 = istart3 + length3; - const size_t segmentSize = (dstSize + 3) / 4; - BYTE* const opStart2 = ostart + segmentSize; - BYTE* const opStart3 = opStart2 + segmentSize; - BYTE* const opStart4 = opStart3 + segmentSize; - BYTE* op1 = ostart; - BYTE* op2 = opStart2; - BYTE* op3 = opStart3; - BYTE* op4 = opStart4; - DTableDesc const dtd = HUF_getDTableDesc(DTable); - U32 const dtLog = dtd.tableLog; - U32 endSignal = 1; - - if (length4 > cSrcSize) { return ERROR(corruption_detected); /* overflow */ } - CHECK_F(BIT_initDStream(&bitD1, istart1, length1)); - CHECK_F(BIT_initDStream(&bitD2, istart2, length2)); - CHECK_F(BIT_initDStream(&bitD3, istart3, length3)); - CHECK_F(BIT_initDStream(&bitD4, istart4, length4)); - - /* up to 16 symbols per loop (4 symbols per stream) in 64-bit mode */ - for (; (endSignal) & (op4 < olimit);) { - HUF_DECODE_SYMBOLX1_2(op1, &bitD1); - HUF_DECODE_SYMBOLX1_2(op2, &bitD2); - HUF_DECODE_SYMBOLX1_2(op3, &bitD3); - HUF_DECODE_SYMBOLX1_2(op4, &bitD4); - HUF_DECODE_SYMBOLX1_1(op1, &bitD1); - HUF_DECODE_SYMBOLX1_1(op2, &bitD2); - HUF_DECODE_SYMBOLX1_1(op3, &bitD3); - HUF_DECODE_SYMBOLX1_1(op4, &bitD4); - HUF_DECODE_SYMBOLX1_2(op1, &bitD1); - HUF_DECODE_SYMBOLX1_2(op2, &bitD2); - HUF_DECODE_SYMBOLX1_2(op3, &bitD3); - HUF_DECODE_SYMBOLX1_2(op4, &bitD4); - HUF_DECODE_SYMBOLX1_0(op1, &bitD1); - HUF_DECODE_SYMBOLX1_0(op2, &bitD2); - HUF_DECODE_SYMBOLX1_0(op3, &bitD3); - HUF_DECODE_SYMBOLX1_0(op4, &bitD4); - endSignal &= BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished; - endSignal &= BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished; - endSignal &= BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished; - endSignal &= BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished; - } - - /* check corruption */ - /* note : should not be necessary : op# advance in lock step, and we control op4. - * but curiously, binary generated by gcc 7.2 & 7.3 with -mbmi2 runs faster when >=1 - * test is present */ - if (op1 > opStart2) { return ERROR(corruption_detected); } - if (op2 > opStart3) { return ERROR(corruption_detected); } - if (op3 > opStart4) { return ERROR(corruption_detected); } - /* note : op4 supposed already verified within main loop */ - - /* finish bitStreams one by one */ - HUF_decodeStreamX1(op1, &bitD1, opStart2, dt, dtLog); - HUF_decodeStreamX1(op2, &bitD2, opStart3, dt, dtLog); - HUF_decodeStreamX1(op3, &bitD3, opStart4, dt, dtLog); - HUF_decodeStreamX1(op4, &bitD4, oend, dt, dtLog); - - /* check */ - { - U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & - BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4); - if (!endCheck) { return ERROR(corruption_detected); } - } - - /* decoded size */ - return dstSize; - } -} - -// allowed range x = 1~511 -inline static int8_t highbit_u9(uint16_t x) -{ - return static_cast(31 - __builtin_clz(static_cast(x))); -} - -// allowed range x = 1~((1<<64) -1) -inline static int8_t trailbit_u64(uint64_t val) -{ - return static_cast(__builtin_ctzll(val)); -} - -FORCE_INLINE_TEMPLATE size_t HUF_decompress8X1_usingDTable_interleaved_stream( - void* dst, size_t dstSize, const void* cHufSrc, size_t cHufSrcSize, const HUF_DTable* DTable) -{ - /* Check */ - if (cHufSrcSize < 10) { - return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ - } - { - const HUF_DEltX1* dtable = (const HUF_DEltX1*)(DTable + 1); - U32 const table_sft = (64 - HUF_getDTableDesc(DTable).tableLog) & 0x3F; - - const size_t segmentSize = ((dstSize + 7) >> 3); - const size_t groupCount = segmentSize / 5; - const size_t groupRemainder = segmentSize - groupCount * 5; - - uint8_t* op = (uint8_t*)dst; - - const uint8_t* ip1 = ((uint8_t*)cHufSrc) + 14 + ((const uint16_t*)cHufSrc)[0] - 8; - const uint8_t* ip2 = ip1 + ((const uint16_t*)cHufSrc)[1]; - const uint8_t* ip3 = ip2 + ((const uint16_t*)cHufSrc)[2]; - const uint8_t* ip4 = ip3 + ((const uint16_t*)cHufSrc)[3]; - const uint8_t* ip5 = ip4 + ((const uint16_t*)cHufSrc)[4]; - const uint8_t* ip6 = ip5 + ((const uint16_t*)cHufSrc)[5]; - const uint8_t* ip7 = ip6 + ((const uint16_t*)cHufSrc)[6]; - const uint8_t* ip8 = ((uint8_t*)cHufSrc) + cHufSrcSize - 8; - - const uint8_t* ip[] = {ip1, ip2, ip3, ip4, ip5, ip6, ip7, ip8}; - uint64_t d[8]; - - for (int k = 0; k < 8; k++) { - d[k] = (1 | (*(U64*)ip[k])); - d[k] <<= (8 - highbit_u9(ip[k][7])); - } - -#define HUF4X1_RELD(k) \ - { \ - int8_t c = trailbit_u64(d[k]); \ - ip[k] -= (c >> 3); \ - d[k] = (1 | (*(U64*)ip[k])); \ - d[k] <<= (c & 7); \ - } -#define HUF4X1_DECK(k) \ - { \ - HUF_DEltX1 item = dtable[(d[k] >> table_sft)]; \ - d[k] <<= item.nbBits; \ - *op++ = item.byte; \ - } - - /* up to 16 symbols per loop (4 symbols per stream) in 64-bit mode */ - for (size_t i = 0; i < groupCount; ++i) { - HUF4X1_DECK(0); // 0 1 2 3 4 5 6 7 - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - - HUF4X1_DECK(0); - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - - HUF4X1_DECK(0); - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - - HUF4X1_DECK(0); - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - - HUF4X1_DECK(0); - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - - HUF4X1_RELD(0); - HUF4X1_RELD(1); - HUF4X1_RELD(2); - HUF4X1_RELD(3); - HUF4X1_RELD(4); - HUF4X1_RELD(5); - HUF4X1_RELD(6); - HUF4X1_RELD(7); - } - - for (size_t i = 0; i < groupRemainder; ++i) { - HUF4X1_DECK(0); - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - } - -#undef HUF4X1_RELD -#undef HUF4X1_DECK - - return dstSize; - } -} - -FORCE_INLINE_TEMPLATE size_t HUF_decompress8X1_usingDTable_interleaved_stream_stride_2byte( - void* dst, size_t dstCount, const void* cHufSrc, size_t cHufSrcSize, const HUF_DTable* DTable) -{ - /* Check */ - if (cHufSrcSize < 10) { - return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ - } - { - const HUF_DEltX1* dtable = (const HUF_DEltX1*)(DTable + 1); - U32 const table_sft = (64 - HUF_getDTableDesc(DTable).tableLog) & 0x3F; - - const size_t segmentSize = ((dstCount + 7) >> 3); - const size_t groupCount = segmentSize / 5; - const size_t groupRemainder = segmentSize - groupCount * 5; - - uint16_t* op = (uint16_t*)dst; - - const uint8_t* ip1 = ((uint8_t*)cHufSrc) + 14 + ((const uint16_t*)cHufSrc)[0] - 8; - const uint8_t* ip2 = ip1 + ((const uint16_t*)cHufSrc)[1]; - const uint8_t* ip3 = ip2 + ((const uint16_t*)cHufSrc)[2]; - const uint8_t* ip4 = ip3 + ((const uint16_t*)cHufSrc)[3]; - const uint8_t* ip5 = ip4 + ((const uint16_t*)cHufSrc)[4]; - const uint8_t* ip6 = ip5 + ((const uint16_t*)cHufSrc)[5]; - const uint8_t* ip7 = ip6 + ((const uint16_t*)cHufSrc)[6]; - const uint8_t* ip8 = ((uint8_t*)cHufSrc) + cHufSrcSize - 8; - - const uint8_t* ip[] = {ip1, ip2, ip3, ip4, ip5, ip6, ip7, ip8}; - uint64_t d[8]; - - for (int k = 0; k < 8; k++) { - d[k] = (1 | (*(U64*)ip[k])); - d[k] <<= (8 - highbit_u9(ip[k][7])); - } - -#define HUF4X1_RELD(k) \ - { \ - int8_t c = trailbit_u64(d[k]); \ - ip[k] -= (c >> 3); \ - d[k] = (1 | (*(U64*)ip[k])); \ - d[k] <<= (c & 7); \ - } -#define HUF4X1_DECK(k) \ - { \ - HUF_DEltX1 item = dtable[(d[k] >> table_sft)]; \ - d[k] <<= item.nbBits; \ - *op++ = (uint16_t)item.byte; \ - } - - /* up to 16 symbols per loop (4 symbols per stream) in 64-bit mode */ - for (size_t i = 0; i < groupCount; ++i) { - HUF4X1_DECK(0); // 0 1 2 3 4 5 6 7 - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - - HUF4X1_DECK(0); - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - - HUF4X1_DECK(0); - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - - HUF4X1_DECK(0); - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - - HUF4X1_DECK(0); - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - - HUF4X1_RELD(0); - HUF4X1_RELD(1); - HUF4X1_RELD(2); - HUF4X1_RELD(3); - HUF4X1_RELD(4); - HUF4X1_RELD(5); - HUF4X1_RELD(6); - HUF4X1_RELD(7); - } - - for (size_t i = 0; i < groupRemainder; ++i) { - HUF4X1_DECK(0); - HUF4X1_DECK(1); - HUF4X1_DECK(2); - HUF4X1_DECK(3); - HUF4X1_DECK(4); - HUF4X1_DECK(5); - HUF4X1_DECK(6); - HUF4X1_DECK(7); - } - -#undef HUF4X1_RELD -#undef HUF4X1_DECK - - return dstCount; // 这里返回的是uint16_t的数量 - } -} - -typedef size_t (*HUF_decompress_usingDTable_t)(void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, const HUF_DTable* DTable); - -HUF_DGEN(HUF_decompress1X1_usingDTable_internal) -HUF_DGEN(HUF_decompress4X1_usingDTable_internal) - -size_t HUF_decompress1X1_usingDTable(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, - const HUF_DTable* DTable) -{ - DTableDesc dtd = HUF_getDTableDesc(DTable); - if (dtd.tableType != 0) { return ERROR(GENERIC); } - return HUF_decompress1X1_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, - /* bmi2 */ 0); -} - -size_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, void* workSpace, size_t wkspSize) -{ - const BYTE* ip = (const BYTE*)cSrc; - - size_t const hSize = HUF_readDTableX1_wksp(DCtx, cSrc, cSrcSize, workSpace, wkspSize); - if (HUF_isError(hSize)) { return hSize; } - if (hSize >= cSrcSize) { return ERROR(srcSize_wrong); } - ip += hSize; - cSrcSize -= hSize; - - return HUF_decompress1X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx, /* bmi2 */ 0); -} - -size_t HUF_decompress1X1_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize) -{ - U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; - return HUF_decompress1X1_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize, workSpace, - sizeof(workSpace)); -} - -size_t HUF_decompress1X1(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) -{ - HUF_CREATE_STATIC_DTABLEX1(DTable, HUF_TABLELOG_MAX); - return HUF_decompress1X1_DCtx(DTable, dst, dstSize, cSrc, cSrcSize); -} - -size_t HUF_decompress4X1_usingDTable(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, - const HUF_DTable* DTable) -{ - DTableDesc dtd = HUF_getDTableDesc(DTable); - if (dtd.tableType != 0) { return ERROR(GENERIC); } - return HUF_decompress4X1_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, - /* bmi2 */ 0); -} - -static size_t HUF_decompress4X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, - const void* cSrc, size_t cSrcSize, void* workSpace, - size_t wkspSize, int bmi2) -{ - const BYTE* ip = (const BYTE*)cSrc; - - size_t const hSize = HUF_readDTableX1_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize); - // printf("hSize %zu\n", hSize); - if (HUF_isError(hSize)) { return hSize; } - if (hSize >= cSrcSize) { return ERROR(srcSize_wrong); } - ip += hSize; - cSrcSize -= hSize; - - return HUF_decompress4X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2); -} - -size_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, void* workSpace, size_t wkspSize) -{ - return HUF_decompress4X1_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, - 0); -} - -size_t HUF_decompress4X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize) -{ - U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; - return HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, - sizeof(workSpace)); -} -size_t HUF_decompress4X1(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) -{ - HUF_CREATE_STATIC_DTABLEX1(DTable, HUF_TABLELOG_MAX); - return HUF_decompress4X1_DCtx(DTable, dst, dstSize, cSrc, cSrcSize); -} - -#endif /* HUF_FORCE_DECOMPRESS_X2 */ - -#ifndef HUF_FORCE_DECOMPRESS_X1 - -/* *************************/ -/* double-symbols decoding */ -/* *************************/ - -typedef struct { - U16 sequence; - BYTE nbBits; - BYTE length; -} HUF_DEltX2; /* double-symbols decoding */ -typedef struct { - BYTE symbol; - BYTE weight; -} sortedSymbol_t; -typedef U32 rankValCol_t[HUF_TABLELOG_MAX + 1]; -typedef rankValCol_t rankVal_t[HUF_TABLELOG_MAX]; - -/* HUF_fillDTableX2Level2() : - * `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */ -static void HUF_fillDTableX2Level2(HUF_DEltX2* DTable, U32 sizeLog, const U32 consumed, - const U32* rankValOrigin, const int minWeight, - const sortedSymbol_t* sortedSymbols, const U32 sortedListSize, - U32 nbBitsBaseline, U16 baseSeq) -{ - HUF_DEltX2 DElt; - U32 rankVal[HUF_TABLELOG_MAX + 1]; - - /* get pre-calculated rankVal */ - memcpy(rankVal, rankValOrigin, sizeof(rankVal)); - - /* fill skipped values */ - if (minWeight > 1) { - U32 i, skipSize = rankVal[minWeight]; - MEM_writeLE16(&(DElt.sequence), baseSeq); - DElt.nbBits = (BYTE)(consumed); - DElt.length = 1; - for (i = 0; i < skipSize; i++) { DTable[i] = DElt; } - } - - /* fill DTable */ - { - U32 s; - for (s = 0; s < sortedListSize; s++) { /* note : sortedSymbols already skipped */ - const U32 symbol = sortedSymbols[s].symbol; - const U32 weight = sortedSymbols[s].weight; - const U32 nbBits = nbBitsBaseline - weight; - const U32 length = 1 << (sizeLog - nbBits); - const U32 start = rankVal[weight]; - U32 i = start; - const U32 end = start + length; - - MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8))); - DElt.nbBits = (BYTE)(nbBits + consumed); - DElt.length = 2; - do { - DTable[i++] = DElt; - } while (i < end); /* since length >= 1 */ - - rankVal[weight] += length; - } - } -} - -static void HUF_fillDTableX2(HUF_DEltX2* DTable, const U32 targetLog, - const sortedSymbol_t* sortedList, const U32 sortedListSize, - const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight, - const U32 nbBitsBaseline) -{ - U32 rankVal[HUF_TABLELOG_MAX + 1]; - const int scaleLog = - nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */ - const U32 minBits = nbBitsBaseline - maxWeight; - U32 s; - - memcpy(rankVal, rankValOrigin, sizeof(rankVal)); - - /* fill DTable */ - for (s = 0; s < sortedListSize; s++) { - const U16 symbol = sortedList[s].symbol; - const U32 weight = sortedList[s].weight; - const U32 nbBits = nbBitsBaseline - weight; - const U32 start = rankVal[weight]; - const U32 length = 1 << (targetLog - nbBits); - - if (targetLog - nbBits >= minBits) { /* enough room for a second symbol */ - U32 sortedRank; - int minWeight = nbBits + scaleLog; - if (minWeight < 1) { minWeight = 1; } - sortedRank = rankStart[minWeight]; - HUF_fillDTableX2Level2(DTable + start, targetLog - nbBits, nbBits, - rankValOrigin[nbBits], minWeight, sortedList + sortedRank, - sortedListSize - sortedRank, nbBitsBaseline, symbol); - } else { - HUF_DEltX2 DElt; - MEM_writeLE16(&(DElt.sequence), symbol); - DElt.nbBits = (BYTE)(nbBits); - DElt.length = 1; - { - U32 const end = start + length; - U32 u; - for (u = start; u < end; u++) { DTable[u] = DElt; } - } - } - rankVal[weight] += length; - } -} - -size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, - size_t wkspSize) -{ - U32 tableLog, maxW, sizeOfSort, nbSymbols; - DTableDesc dtd = HUF_getDTableDesc(DTable); - U32 const maxTableLog = dtd.maxTableLog; - size_t iSize; - void* dtPtr = DTable + 1; /* force compiler to avoid strict-aliasing */ - HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr; - U32* rankStart; - - rankValCol_t* rankVal; - U32* rankStats; - U32* rankStart0; - sortedSymbol_t* sortedSymbol; - BYTE* weightList; - size_t spaceUsed32 = 0; - - rankVal = (rankValCol_t*)((U32*)workSpace + spaceUsed32); - spaceUsed32 += (sizeof(rankValCol_t) * HUF_TABLELOG_MAX) >> 2; - rankStats = (U32*)workSpace + spaceUsed32; - spaceUsed32 += HUF_TABLELOG_MAX + 1; - rankStart0 = (U32*)workSpace + spaceUsed32; - spaceUsed32 += HUF_TABLELOG_MAX + 2; - sortedSymbol = - (sortedSymbol_t*)workSpace + (spaceUsed32 * sizeof(U32)) / sizeof(sortedSymbol_t); - spaceUsed32 += HUF_ALIGN(sizeof(sortedSymbol_t) * (HUF_SYMBOLVALUE_MAX + 1), sizeof(U32)) >> 2; - weightList = (BYTE*)((U32*)workSpace + spaceUsed32); - spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2; - - if ((spaceUsed32 << 2) > wkspSize) { return ERROR(tableLog_tooLarge); } - - rankStart = rankStart0 + 1; - memset(rankStats, 0, sizeof(U32) * (2 * HUF_TABLELOG_MAX + 2 + 1)); - - DEBUG_STATIC_ASSERT(sizeof(HUF_DEltX2) == - sizeof(HUF_DTable)); /* if compiler fails here, assertion is wrong */ - if (maxTableLog > HUF_TABLELOG_MAX) { return ERROR(tableLog_tooLarge); } - /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer - complain ... */ - - iSize = HUF_readStats(weightList, HUF_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, - src, srcSize); - if (HUF_isError(iSize)) { return iSize; } - - /* check result */ - if (tableLog > maxTableLog) { - return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */ - } - - /* find maxWeight */ - for (maxW = tableLog; rankStats[maxW] == 0; maxW--) { - } /* necessarily finds a solution before 0 */ - - /* Get start index of each weight */ - { - U32 w, nextRankStart = 0; - for (w = 1; w < maxW + 1; w++) { - U32 current = nextRankStart; - nextRankStart += rankStats[w]; - rankStart[w] = current; - } - rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/ - sizeOfSort = nextRankStart; - } - - /* sort symbols by weight */ - { - U32 s; - for (s = 0; s < nbSymbols; s++) { - U32 const w = weightList[s]; - U32 const r = rankStart[w]++; - sortedSymbol[r].symbol = (BYTE)s; - sortedSymbol[r].weight = (BYTE)w; - } - rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */ - } - - /* Build rankVal */ - { - U32* const rankVal0 = rankVal[0]; - { - int const rescale = (maxTableLog - tableLog) - 1; /* tableLog <= maxTableLog */ - U32 nextRankVal = 0; - U32 w; - for (w = 1; w < maxW + 1; w++) { - U32 current = nextRankVal; - nextRankVal += rankStats[w] << (w + rescale); - rankVal0[w] = current; - } - } - { - U32 const minBits = tableLog + 1 - maxW; - U32 consumed; - for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) { - U32* const rankValPtr = rankVal[consumed]; - U32 w; - for (w = 1; w < maxW + 1; w++) { rankValPtr[w] = rankVal0[w] >> consumed; } - } - } - } - - HUF_fillDTableX2(dt, maxTableLog, sortedSymbol, sizeOfSort, rankStart0, rankVal, maxW, - tableLog + 1); - - dtd.tableLog = (BYTE)maxTableLog; - dtd.tableType = 1; - memcpy(DTable, &dtd, sizeof(dtd)); - return iSize; -} - -size_t HUF_readDTableX2(HUF_DTable* DTable, const void* src, size_t srcSize) -{ - U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; - return HUF_readDTableX2_wksp(DTable, src, srcSize, workSpace, sizeof(workSpace)); -} - -FORCE_INLINE_TEMPLATE U32 HUF_decodeSymbolX2(void* op, BIT_DStream_t* DStream, const HUF_DEltX2* dt, - const U32 dtLog) -{ - size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */ - memcpy(op, dt + val, 2); - BIT_skipBits(DStream, dt[val].nbBits); - return dt[val].length; -} - -FORCE_INLINE_TEMPLATE U32 HUF_decodeLastSymbolX2(void* op, BIT_DStream_t* DStream, - const HUF_DEltX2* dt, const U32 dtLog) -{ - size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */ - memcpy(op, dt + val, 1); - if (dt[val].length == 1) { - BIT_skipBits(DStream, dt[val].nbBits); - } else { - if (DStream->bitsConsumed < (sizeof(DStream->bitContainer) * 8)) { - BIT_skipBits(DStream, dt[val].nbBits); - if (DStream->bitsConsumed > (sizeof(DStream->bitContainer) * 8)) - /* ugly hack; works only because it's the last symbol. Note : can't easily extract - * nbBits from just this symbol */ - DStream->bitsConsumed = (sizeof(DStream->bitContainer) * 8); - } - } - return 1; -} - -#define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog) - -#define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \ - if (MEM_64bits() || (HUF_TABLELOG_MAX <= 12)) \ - ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog) - -#define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \ - if (MEM_64bits()) ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog) - -HINT_INLINE size_t HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, - const HUF_DEltX2* const dt, const U32 dtLog) -{ - BYTE* const pStart = p; - - /* up to 8 symbols at a time */ - while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & - (p < pEnd - (sizeof(bitDPtr->bitContainer) - 1))) { - HUF_DECODE_SYMBOLX2_2(p, bitDPtr); - HUF_DECODE_SYMBOLX2_1(p, bitDPtr); - HUF_DECODE_SYMBOLX2_2(p, bitDPtr); - HUF_DECODE_SYMBOLX2_0(p, bitDPtr); - } - - /* closer to end : up to 2 symbols at a time */ - while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p <= pEnd - 2)) { - HUF_DECODE_SYMBOLX2_0(p, bitDPtr); - } - - while (p <= pEnd - 2) { - HUF_DECODE_SYMBOLX2_0(p, bitDPtr); - } /* no need to reload : reached the end of DStream */ - - if (p < pEnd) { p += HUF_decodeLastSymbolX2(p, bitDPtr, dt, dtLog); } - - return p - pStart; -} - -FORCE_INLINE_TEMPLATE size_t HUF_decompress1X2_usingDTable_internal_body(void* dst, size_t dstSize, - const void* cSrc, - size_t cSrcSize, - const HUF_DTable* DTable) -{ - BIT_DStream_t bitD; - - /* Init */ - CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize)); - - /* decode */ - { - BYTE* const ostart = (BYTE*)dst; - BYTE* const oend = ostart + dstSize; - const void* const dtPtr = DTable + 1; /* force compiler to not use strict-aliasing */ - const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr; - DTableDesc const dtd = HUF_getDTableDesc(DTable); - HUF_decodeStreamX2(ostart, &bitD, oend, dt, dtd.tableLog); - } - - /* check */ - if (!BIT_endOfDStream(&bitD)) { return ERROR(corruption_detected); } - - /* decoded size */ - return dstSize; -} - -FORCE_INLINE_TEMPLATE size_t HUF_decompress4X2_usingDTable_internal_body(void* dst, size_t dstSize, - const void* cSrc, - size_t cSrcSize, - const HUF_DTable* DTable) -{ - if (cSrcSize < 10) { - return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ - } - - { - const BYTE* const istart = (const BYTE*)cSrc; - BYTE* const ostart = (BYTE*)dst; - BYTE* const oend = ostart + dstSize; - BYTE* const olimit = oend - (sizeof(size_t) - 1); - const void* const dtPtr = DTable + 1; - const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr; - - /* Init */ - BIT_DStream_t bitD1; - BIT_DStream_t bitD2; - BIT_DStream_t bitD3; - BIT_DStream_t bitD4; - size_t const length1 = MEM_readLE16(istart); - size_t const length2 = MEM_readLE16(istart + 2); - size_t const length3 = MEM_readLE16(istart + 4); - size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6); - const BYTE* const istart1 = istart + 6; /* jumpTable */ - const BYTE* const istart2 = istart1 + length1; - const BYTE* const istart3 = istart2 + length2; - const BYTE* const istart4 = istart3 + length3; - size_t const segmentSize = (dstSize + 3) / 4; - BYTE* const opStart2 = ostart + segmentSize; - BYTE* const opStart3 = opStart2 + segmentSize; - BYTE* const opStart4 = opStart3 + segmentSize; - BYTE* op1 = ostart; - BYTE* op2 = opStart2; - BYTE* op3 = opStart3; - BYTE* op4 = opStart4; - U32 endSignal = 1; - DTableDesc const dtd = HUF_getDTableDesc(DTable); - U32 const dtLog = dtd.tableLog; - - if (length4 > cSrcSize) { return ERROR(corruption_detected); /* overflow */ } - CHECK_F(BIT_initDStream(&bitD1, istart1, length1)); - CHECK_F(BIT_initDStream(&bitD2, istart2, length2)); - CHECK_F(BIT_initDStream(&bitD3, istart3, length3)); - CHECK_F(BIT_initDStream(&bitD4, istart4, length4)); - - /* 16-32 symbols per loop (4-8 symbols per stream) */ - for (; (endSignal) & (op4 < olimit);) { -#if defined(__clang__) && (defined(__x86_64__) || defined(__i386__)) - HUF_DECODE_SYMBOLX2_2(op1, &bitD1); - HUF_DECODE_SYMBOLX2_1(op1, &bitD1); - HUF_DECODE_SYMBOLX2_2(op1, &bitD1); - HUF_DECODE_SYMBOLX2_0(op1, &bitD1); - HUF_DECODE_SYMBOLX2_2(op2, &bitD2); - HUF_DECODE_SYMBOLX2_1(op2, &bitD2); - HUF_DECODE_SYMBOLX2_2(op2, &bitD2); - HUF_DECODE_SYMBOLX2_0(op2, &bitD2); - endSignal &= BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished; - endSignal &= BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished; - HUF_DECODE_SYMBOLX2_2(op3, &bitD3); - HUF_DECODE_SYMBOLX2_1(op3, &bitD3); - HUF_DECODE_SYMBOLX2_2(op3, &bitD3); - HUF_DECODE_SYMBOLX2_0(op3, &bitD3); - HUF_DECODE_SYMBOLX2_2(op4, &bitD4); - HUF_DECODE_SYMBOLX2_1(op4, &bitD4); - HUF_DECODE_SYMBOLX2_2(op4, &bitD4); - HUF_DECODE_SYMBOLX2_0(op4, &bitD4); - endSignal &= BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished; - endSignal &= BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished; -#else - HUF_DECODE_SYMBOLX2_2(op1, &bitD1); - HUF_DECODE_SYMBOLX2_2(op2, &bitD2); - HUF_DECODE_SYMBOLX2_2(op3, &bitD3); - HUF_DECODE_SYMBOLX2_2(op4, &bitD4); - HUF_DECODE_SYMBOLX2_1(op1, &bitD1); - HUF_DECODE_SYMBOLX2_1(op2, &bitD2); - HUF_DECODE_SYMBOLX2_1(op3, &bitD3); - HUF_DECODE_SYMBOLX2_1(op4, &bitD4); - HUF_DECODE_SYMBOLX2_2(op1, &bitD1); - HUF_DECODE_SYMBOLX2_2(op2, &bitD2); - HUF_DECODE_SYMBOLX2_2(op3, &bitD3); - HUF_DECODE_SYMBOLX2_2(op4, &bitD4); - HUF_DECODE_SYMBOLX2_0(op1, &bitD1); - HUF_DECODE_SYMBOLX2_0(op2, &bitD2); - HUF_DECODE_SYMBOLX2_0(op3, &bitD3); - HUF_DECODE_SYMBOLX2_0(op4, &bitD4); - endSignal = (U32)LIKELY((BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished) & - (BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished) & - (BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished) & - (BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished)); -#endif - } - - /* check corruption */ - if (op1 > opStart2) { return ERROR(corruption_detected); } - if (op2 > opStart3) { return ERROR(corruption_detected); } - if (op3 > opStart4) { return ERROR(corruption_detected); } - /* note : op4 already verified within main loop */ - - /* finish bitStreams one by one */ - HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog); - HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog); - HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog); - HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog); - - /* check */ - { - U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & - BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4); - if (!endCheck) { return ERROR(corruption_detected); } - } - - /* decoded size */ - return dstSize; - } -} - -HUF_DGEN(HUF_decompress1X2_usingDTable_internal) -HUF_DGEN(HUF_decompress4X2_usingDTable_internal) - -size_t HUF_decompress1X2_usingDTable(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, - const HUF_DTable* DTable) -{ - DTableDesc dtd = HUF_getDTableDesc(DTable); - if (dtd.tableType != 1) { return ERROR(GENERIC); } - return HUF_decompress1X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, - /* bmi2 */ 0); -} - -size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, void* workSpace, size_t wkspSize) -{ - const BYTE* ip = (const BYTE*)cSrc; - - size_t const hSize = HUF_readDTableX2_wksp(DCtx, cSrc, cSrcSize, workSpace, wkspSize); - if (HUF_isError(hSize)) { return hSize; } - if (hSize >= cSrcSize) { return ERROR(srcSize_wrong); } - ip += hSize; - cSrcSize -= hSize; - - return HUF_decompress1X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx, /* bmi2 */ 0); -} - -size_t HUF_decompress1X2_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize) -{ - U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; - return HUF_decompress1X2_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize, workSpace, - sizeof(workSpace)); -} - -size_t HUF_decompress1X2(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) -{ - HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX); - return HUF_decompress1X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize); -} - -size_t HUF_decompress4X2_usingDTable(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, - const HUF_DTable* DTable) -{ - DTableDesc dtd = HUF_getDTableDesc(DTable); - if (dtd.tableType != 1) { return ERROR(GENERIC); } - return HUF_decompress4X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, - /* bmi2 */ 0); -} - -static size_t HUF_decompress4X2_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, - const void* cSrc, size_t cSrcSize, void* workSpace, - size_t wkspSize, int bmi2) -{ - const BYTE* ip = (const BYTE*)cSrc; - - size_t hSize = HUF_readDTableX2_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize); - if (HUF_isError(hSize)) { return hSize; } - if (hSize >= cSrcSize) { return ERROR(srcSize_wrong); } - ip += hSize; - cSrcSize -= hSize; - - return HUF_decompress4X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2); -} - -size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, void* workSpace, size_t wkspSize) -{ - return HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, - /* bmi2 */ 0); -} - -size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize) -{ - U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; - return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, - sizeof(workSpace)); -} - -size_t HUF_decompress4X2(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) -{ - HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX); - return HUF_decompress4X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize); -} - -#endif /* HUF_FORCE_DECOMPRESS_X1 */ - -/* ***********************************/ -/* Universal decompression selectors */ -/* ***********************************/ - -size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, - const HUF_DTable* DTable) -{ - DTableDesc const dtd = HUF_getDTableDesc(DTable); -#if defined(HUF_FORCE_DECOMPRESS_X1) - (void)dtd; - assert(dtd.tableType == 0); - return HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, - /* bmi2 */ 0); -#elif defined(HUF_FORCE_DECOMPRESS_X2) - (void)dtd; - assert(dtd.tableType == 1); - return HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, - /* bmi2 */ 0); -#else - return dtd.tableType ? HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, - DTable, /* bmi2 */ 0) - : HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, - DTable, /* bmi2 */ 0); -#endif -} - -size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, - const HUF_DTable* DTable) -{ - DTableDesc const dtd = HUF_getDTableDesc(DTable); -#if defined(HUF_FORCE_DECOMPRESS_X1) - (void)dtd; - assert(dtd.tableType == 0); - return HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, - /* bmi2 */ 0); -#elif defined(HUF_FORCE_DECOMPRESS_X2) - (void)dtd; - assert(dtd.tableType == 1); - return HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, - /* bmi2 */ 0); -#else - return dtd.tableType ? HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, - DTable, /* bmi2 */ 0) - : HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, - DTable, /* bmi2 */ 0); -#endif -} - -#if !defined(HUF_FORCE_DECOMPRESS_X1) && !defined(HUF_FORCE_DECOMPRESS_X2) -typedef struct { - U32 tableTime; - U32 decode256Time; -} algo_time_t; -static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] = { - /* single, double, quad */ - {{0, 0}, {1, 1}, {2, 2} }, /* Q==0 : impossible */ - {{0, 0}, {1, 1}, {2, 2} }, /* Q==1 : impossible */ - {{38, 130}, {1313, 74}, {2151, 38} }, /* Q == 2 : 12-18% */ - {{448, 128}, {1353, 74}, {2238, 41} }, /* Q == 3 : 18-25% */ - {{556, 128}, {1353, 74}, {2238, 47} }, /* Q == 4 : 25-32% */ - {{714, 128}, {1418, 74}, {2436, 53} }, /* Q == 5 : 32-38% */ - {{883, 128}, {1437, 74}, {2464, 61} }, /* Q == 6 : 38-44% */ - {{897, 128}, {1515, 75}, {2622, 68} }, /* Q == 7 : 44-50% */ - {{926, 128}, {1613, 75}, {2730, 75} }, /* Q == 8 : 50-56% */ - {{947, 128}, {1729, 77}, {3359, 77} }, /* Q == 9 : 56-62% */ - {{1107, 128}, {2083, 81}, {4006, 84} }, /* Q ==10 : 62-69% */ - {{1177, 128}, {2379, 87}, {4785, 88} }, /* Q ==11 : 69-75% */ - {{1242, 128}, {2415, 93}, {5155, 84} }, /* Q ==12 : 75-81% */ - {{1349, 128}, {2644, 106}, {5260, 106}}, /* Q ==13 : 81-87% */ - {{1455, 128}, {2422, 124}, {4174, 124}}, /* Q ==14 : 87-93% */ - {{722, 128}, {1891, 145}, {1936, 146}}, /* Q ==15 : 93-99% */ -}; -#endif - -/** HUF_selectDecoder() : - * Tells which decoder is likely to decode faster, - * based on a set of pre-computed metrics. - * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 . - * Assumption : 0 < dstSize <= 128 KB */ -U32 HUF_selectDecoder(size_t dstSize, size_t cSrcSize) -{ - assert(dstSize > 0); - assert(dstSize <= 128 * 1024); -#if defined(HUF_FORCE_DECOMPRESS_X1) - (void)dstSize; - (void)cSrcSize; - return 0; -#elif defined(HUF_FORCE_DECOMPRESS_X2) - (void)dstSize; - (void)cSrcSize; - return 1; -#else - /* decoder timing evaluation */ - { - U32 const Q = (cSrcSize >= dstSize) ? 15 : (U32)(cSrcSize * 16 / dstSize); /* Q < 16 */ - U32 const D256 = (U32)(dstSize >> 8); - U32 const DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256); - U32 DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256); - DTime1 += - DTime1 >> 3; /* advantage to algorithm using less memory, to reduce cache eviction */ - return DTime1 < DTime0; - } -#endif -} - -typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); - -size_t HUF_decompress(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) -{ -#if !defined(HUF_FORCE_DECOMPRESS_X1) && !defined(HUF_FORCE_DECOMPRESS_X2) - static const decompressionAlgo decompress[2] = {HUF_decompress4X1, HUF_decompress4X2}; -#endif - - /* validation checks */ - if (dstSize == 0) { return ERROR(dstSize_tooSmall); } - if (cSrcSize > dstSize) { return ERROR(corruption_detected); /* invalid */ } - if (cSrcSize == dstSize) { - memcpy(dst, cSrc, dstSize); - return dstSize; - } /* not compressed */ - if (cSrcSize == 1) { - memset(dst, *(const BYTE*)cSrc, dstSize); - return dstSize; - } /* RLE */ - - { - U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize); -#if defined(HUF_FORCE_DECOMPRESS_X1) - (void)algoNb; - assert(algoNb == 0); - return HUF_decompress4X1(dst, dstSize, cSrc, cSrcSize); -#elif defined(HUF_FORCE_DECOMPRESS_X2) - (void)algoNb; - assert(algoNb == 1); - return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize); -#else - return decompress[algoNb](dst, dstSize, cSrc, cSrcSize); -#endif - } -} - -size_t HUF_decompress_float_fixRatio_internal_bf16(void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize) -{ - if (dstSize == 0) { return ERROR(dstSize_tooSmall); } - - // 解析元数据 - uint32_t count = (((const uint32_t*)cSrc)[1]) & 0x00FFFFFF; // 元数据1: 浮点数总量 - uint32_t count_full = ((const uint32_t*)cSrc)[2]; // 元数据2: 全精度尾数的浮点数的数量 - uint32_t e_len = - ((const uint32_t*)cSrc)[3]; // 元数据3: 尾数的偏移量,用该变量可以定位到尾数的起始存放地址 - - // 计算输入buffer上的的一些区域的起始指针 - const uint8_t* ip_e = (const BYTE*)cSrc + 16; // 起始指针: huffman表, 跳过16byte元数据 - const uint8_t* ip_full = (const BYTE*)cSrc + e_len; // 起始指针: 全精度尾数 - const uint8_t* ip_trunc = (const BYTE*)cSrc + e_len + count_full; // 起始指针: 低精度尾数 - - // 解析huffman表 - HUF_CREATE_STATIC_DTABLEX1(DTable, HUF_TABLELOG_MAX); - U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; - size_t hSize = HUF_readDTableX1_wksp(DTable, ip_e, (ip_full - ip_e), workSpace, - sizeof(workSpace)); // hSize是huffman表在压缩流中的大小 - if (HUF_isError(hSize)) { return hSize; } - - // 从huffman部分解码出指数, 直接放在dst里,稍后再拼装 - ip_e += hSize; - size_t ret_code = HUF_decompress8X1_usingDTable_interleaved_stream_stride_2byte( - dst, count, ip_e, (ip_full - ip_e), DTable); - if (HUF_isError(ret_code)) { return ret_code; } - - // 解析全精度尾数 - uint16_t* op = (uint16_t*)dst; - for (uint32_t i = 0; i < count_full; i++) { - op[0] = ((ip_full[0] & 0x80) << 8) | (op[0] << 7) | (ip_full[0] & 0x7F); - op++; - ip_full++; - } - - // 解析低精度尾数 - for (uint32_t i = 0; i < (count - count_full); i += 2) { - op[0] = ((ip_trunc[0] & 0x80) << 8) | (op[0] << 7) | (ip_trunc[0] & 0x70) | 0x7; - op[1] = ((ip_trunc[0] & 0x8) << 12) | (op[1] << 7) | ((ip_trunc[0] & 0x7) << 4) | 0x7; - op += 2; - ip_trunc++; - } - - return count * sizeof(uint16_t); // 返回解压后的字节数量 -} - -size_t HUF_decompress_float_fixRatio_bf16(void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize) -{ - uint8_t* istart = (uint8_t*)cSrc; - uint8_t* ip = istart; - uint8_t* iend = istart + cSrcSize; - - uint8_t* ostart = (uint8_t*)dst; - uint8_t* op = ostart; - - while (ip < iend) { - uint32_t srcSize = ((((uint32_t*)ip)[0]) & 0X00FFFFFF); - uint32_t segmentSize = ((((uint32_t*)ip)[1]) & 0X00FFFFFF); - CHECK_V_F(decSize, - HUF_decompress_float_fixRatio_internal_bf16(op, segmentSize, ip, srcSize)); - ip += srcSize; - op += decSize; - } - return op - ostart; -} - -size_t HUF_decompress_float_fixRatio(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, - DataType* p_dataType) -{ - if (cSrcSize <= 16) { - return 0; // srcbuffer不够大, 连元数据都放不下,报错 - } - DataType dt = (DataType)(((const uint32_t*)cSrc)[0] >> 24); - if (p_dataType) { *p_dataType = dt; } - switch (dt) { - case DT_BF16: return HUF_decompress_float_fixRatio_bf16(dst, dstSize, cSrc, cSrcSize); - default: return 0; - } -} - -size_t HUF_decompress4X_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize) -{ - /* validation checks */ - if (dstSize == 0) { return ERROR(dstSize_tooSmall); } - if (cSrcSize > dstSize) { return ERROR(corruption_detected); /* invalid */ } - if (cSrcSize == dstSize) { - memcpy(dst, cSrc, dstSize); - return dstSize; - } /* not compressed */ - if (cSrcSize == 1) { - memset(dst, *(const BYTE*)cSrc, dstSize); - return dstSize; - } /* RLE */ - - { - U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize); -#if defined(HUF_FORCE_DECOMPRESS_X1) - (void)algoNb; - assert(algoNb == 0); - return HUF_decompress4X1_DCtx(dctx, dst, dstSize, cSrc, cSrcSize); -#elif defined(HUF_FORCE_DECOMPRESS_X2) - (void)algoNb; - assert(algoNb == 1); - return HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize); -#else - return algoNb ? HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) - : HUF_decompress4X1_DCtx(dctx, dst, dstSize, cSrc, cSrcSize); -#endif - } -} - -size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize) -{ - U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; - return HUF_decompress4X_hufOnly_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, - sizeof(workSpace)); -} - -size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, void* workSpace, size_t wkspSize) -{ - /* validation checks */ - if (dstSize == 0) { return ERROR(dstSize_tooSmall); } - if (cSrcSize == 0) { return ERROR(corruption_detected); } - - { - U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize); -#if defined(HUF_FORCE_DECOMPRESS_X1) - (void)algoNb; - assert(algoNb == 0); - return HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize); -#elif defined(HUF_FORCE_DECOMPRESS_X2) - (void)algoNb; - assert(algoNb == 1); - return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize); -#else - return algoNb ? HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, - wkspSize) - : HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, - wkspSize); -#endif - } -} - -size_t HUF_decompress1X_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize, void* workSpace, size_t wkspSize) -{ - /* validation checks */ - if (dstSize == 0) { return ERROR(dstSize_tooSmall); } - if (cSrcSize > dstSize) { return ERROR(corruption_detected); /* invalid */ } - if (cSrcSize == dstSize) { - memcpy(dst, cSrc, dstSize); - return dstSize; - } /* not compressed */ - if (cSrcSize == 1) { - memset(dst, *(const BYTE*)cSrc, dstSize); - return dstSize; - } /* RLE */ - - { - U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize); -#if defined(HUF_FORCE_DECOMPRESS_X1) - (void)algoNb; - assert(algoNb == 0); - return HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize); -#elif defined(HUF_FORCE_DECOMPRESS_X2) - (void)algoNb; - assert(algoNb == 1); - return HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize); -#else - return algoNb ? HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, - wkspSize) - : HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, - wkspSize); -#endif - } -} - -size_t HUF_decompress1X_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, - size_t cSrcSize) -{ - U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32]; - return HUF_decompress1X_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, - sizeof(workSpace)); -} - -size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, - size_t cSrcSize, const HUF_DTable* DTable, int bmi2) -{ - DTableDesc const dtd = HUF_getDTableDesc(DTable); -#if defined(HUF_FORCE_DECOMPRESS_X1) - (void)dtd; - assert(dtd.tableType == 0); - return HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2); -#elif defined(HUF_FORCE_DECOMPRESS_X2) - (void)dtd; - assert(dtd.tableType == 1); - return HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2); -#else - return dtd.tableType ? HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, - DTable, bmi2) - : HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, - DTable, bmi2); -#endif -} - -#ifndef HUF_FORCE_DECOMPRESS_X2 -size_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, - const void* cSrc, size_t cSrcSize, void* workSpace, - size_t wkspSize, int bmi2) -{ - const BYTE* ip = (const BYTE*)cSrc; - - size_t const hSize = HUF_readDTableX1_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize); - if (HUF_isError(hSize)) { return hSize; } - if (hSize >= cSrcSize) { return ERROR(srcSize_wrong); } - ip += hSize; - cSrcSize -= hSize; - - return HUF_decompress1X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2); -} -#endif - -size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, - size_t cSrcSize, const HUF_DTable* DTable, int bmi2) -{ - DTableDesc const dtd = HUF_getDTableDesc(DTable); -#if defined(HUF_FORCE_DECOMPRESS_X1) - (void)dtd; - assert(dtd.tableType == 0); - return HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2); -#elif defined(HUF_FORCE_DECOMPRESS_X2) - (void)dtd; - assert(dtd.tableType == 1); - return HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2); -#else - return dtd.tableType ? HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, - DTable, bmi2) - : HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, - DTable, bmi2); -#endif -} - -size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, - const void* cSrc, size_t cSrcSize, void* workSpace, - size_t wkspSize, int bmi2) -{ - /* validation checks */ - if (dstSize == 0) { return ERROR(dstSize_tooSmall); } - if (cSrcSize == 0) { return ERROR(corruption_detected); } - - { - U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize); -#if defined(HUF_FORCE_DECOMPRESS_X1) - (void)algoNb; - assert(algoNb == 0); - return HUF_decompress4X1_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, - wkspSize, bmi2); -#elif defined(HUF_FORCE_DECOMPRESS_X2) - (void)algoNb; - assert(algoNb == 1); - return HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, - wkspSize, bmi2); -#else - return algoNb ? HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, - workSpace, wkspSize, bmi2) - : HUF_decompress4X1_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, - workSpace, wkspSize, bmi2); -#endif - } -} diff --git a/ucm/store/compress/cc/compress_lib/mem.h b/ucm/store/compress/cc/compress_lib/mem.h deleted file mode 100644 index 7819f956b..000000000 --- a/ucm/store/compress/cc/compress_lib/mem.h +++ /dev/null @@ -1,490 +0,0 @@ -/* - * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - */ - -#ifndef MEM_H_MODULE -#define MEM_H_MODULE - -#if defined(__cplusplus) -extern "C" { -#endif - -/*-**************************************** - * Dependencies - ******************************************/ -#include /* size_t, ptrdiff_t */ -#include /* memcpy */ - -/*-**************************************** - * Compiler specifics - ******************************************/ -#if defined(_MSC_VER) /* Visual Studio */ -#include /* _byteswap_* */ -#include /* _byteswap_ulong */ -#endif -#if defined(__GNUC__) -#define MEM_STATIC static __inline __attribute__((unused)) -#elif defined(__cplusplus) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 \ - */ \ - ) -#define MEM_STATIC static inline -#elif defined(_MSC_VER) -#define MEM_STATIC static __inline -#else -#define MEM_STATIC \ - static /* this version may generate warnings for unused static functions; disable the relevant \ - warning */ -#endif - -#ifndef __has_builtin -#define __has_builtin(x) 0 /* compat. with non-clang compilers */ -#endif - -/* code only tested on 32 and 64 bits systems */ -#define MEM_STATIC_ASSERT(c) \ - { \ - enum { MEM_static_assert = 1 / (int)(!!(c)) }; \ - } -MEM_STATIC void MEM_check(void) -{ - MEM_STATIC_ASSERT((sizeof(size_t) == 4) || (sizeof(size_t) == 8)); -} - -/* detects whether we are being compiled under msan */ -#if defined(__has_feature) -#if __has_feature(memory_sanitizer) -#define MEMORY_SANITIZER 1 -#endif -#endif - -#if defined(MEMORY_SANITIZER) -/* Not all platforms that support msan provide sanitizers/msan_interface.h. - * We therefore declare the functions we need ourselves, rather than trying to - * include the header file... */ - -#include /* intptr_t */ - -/* Make memory region fully initialized (without changing its contents). */ -void __msan_unpoison(const volatile void* a, size_t size); - -/* Make memory region fully uninitialized (without changing its contents). - This is a legacy interface that does not update origin information. Use - __msan_allocated_memory() instead. */ -void __msan_poison(const volatile void* a, size_t size); - -/* Returns the offset of the first (at least partially) poisoned byte in the - memory range, or -1 if the whole range is good. */ -intptr_t __msan_test_shadow(const volatile void* x, size_t size); -#endif - -/* detects whether we are being compiled under asan */ -#if defined(__has_feature) -#if __has_feature(address_sanitizer) -#define ADDRESS_SANITIZER 1 -#endif -#elif defined(__SANITIZE_ADDRESS__) -#define ADDRESS_SANITIZER 1 -#endif - -#if defined(ADDRESS_SANITIZER) -/* Not all platforms that support asan provide sanitizers/asan_interface.h. - * We therefore declare the functions we need ourselves, rather than trying to - * include the header file... */ - -/** - * Marks a memory region ([addr, addr+size)) as unaddressable. - * - * This memory must be previously allocated by your program. Instrumented - * code is forbidden from accessing addresses in this region until it is - * unpoisoned. This function is not guaranteed to poison the entire region - - * it could poison only a subregion of [addr, addr+size) due to ASan - * alignment restrictions. - * - * \note This function is not thread-safe because no two threads can poison or - * unpoison memory in the same memory region simultaneously. - * - * \param addr Start of memory region. - * \param size Size of memory region. */ -void __asan_poison_memory_region(void const volatile* addr, size_t size); - -/** - * Marks a memory region ([addr, addr+size)) as addressable. - * - * This memory must be previously allocated by your program. Accessing - * addresses in this region is allowed until this region is poisoned again. - * This function could unpoison a super-region of [addr, addr+size) due - * to ASan alignment restrictions. - * - * \note This function is not thread-safe because no two threads can - * poison or unpoison memory in the same memory region simultaneously. - * - * \param addr Start of memory region. - * \param size Size of memory region. */ -void __asan_unpoison_memory_region(void const volatile* addr, size_t size); -#endif - -/*-************************************************************** - * Basic Types - *****************************************************************/ -#if !defined(__VMS) && (defined(__cplusplus) || \ - (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)) -#include -typedef uint8_t BYTE; -typedef uint16_t U16; -typedef int16_t S16; -typedef uint32_t U32; -typedef int32_t S32; -typedef uint64_t U64; -typedef int64_t S64; -#else -#include -#if CHAR_BIT != 8 -#error "this implementation requires char to be exactly 8-bit type" -#endif -typedef unsigned char BYTE; -#if USHRT_MAX != 65535 -#error "this implementation requires short to be exactly 16-bit type" -#endif -typedef unsigned short U16; -typedef signed short S16; -#if UINT_MAX != 4294967295 -#error "this implementation requires int to be exactly 32-bit type" -#endif -typedef unsigned int U32; -typedef signed int S32; -/* note : there are no limits defined for long long type in C90. - * limits exist in C99, however, in such case, is preferred */ -typedef unsigned long long U64; -typedef signed long long S64; -#endif - -/*-************************************************************** - * Memory I/O - *****************************************************************/ -/* MEM_FORCE_MEMORY_ACCESS : - * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable. - * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal. - * The below switch allow to select different access method for improved performance. - * Method 0 (default) : use `memcpy()`. Safe and portable. - * Method 1 : `__packed` statement. It depends on compiler extension (i.e., not portable). - * This method is safe if your compiler supports it, and *generally* as fast or faster - * than `memcpy`. Method 2 : direct access. This method is portable but violate C standard. It can - * generate buggy code on targets depending on alignment. In some circumstances, it's the only known - * way to get the most performance (i.e. GCC + ARMv6) See - * http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details. Prefer - * these methods in priority order (0 > 1 > 2) - */ -#ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */ -#if defined(__GNUC__) && \ - (defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || \ - defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__)) -#define MEM_FORCE_MEMORY_ACCESS 2 -#elif defined(__INTEL_COMPILER) || defined(__GNUC__) || defined(__ICCARM__) -#define MEM_FORCE_MEMORY_ACCESS 1 -#endif -#endif - -MEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t) == 4; } -MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t) == 8; } - -MEM_STATIC unsigned MEM_isLittleEndian(void) -{ - const union { - U32 u; - BYTE c[4]; - } one = {1}; /* don't use static : performance detrimental */ - return one.c[0]; -} - -#if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS == 2) - -/* violates C standard, by lying on structure alignment. -Only use if no other choice to achieve best performance on target platform */ -MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*)memPtr; } -MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*)memPtr; } -MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*)memPtr; } -MEM_STATIC size_t MEM_readST(const void* memPtr) { return *(const size_t*)memPtr; } - -MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; } -MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } -MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; } - -#elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS == 1) - -/* __pack instructions are safer, but compiler specific, hence potentially problematic for some - * compilers */ -/* currently only defined for gcc and icc */ -#if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32)) -__pragma(pack(push, 1)) typedef struct { - U16 v; -} unalign16; -typedef struct { - U32 v; -} unalign32; -typedef struct { - U64 v; -} unalign64; -typedef struct { - size_t v; -} unalignArch; -__pragma(pack(pop)) -#else -typedef struct { - U16 v; -} __attribute__((packed)) unalign16; -typedef struct { - U32 v; -} __attribute__((packed)) unalign32; -typedef struct { - U64 v; -} __attribute__((packed)) unalign64; -typedef struct { - size_t v; -} __attribute__((packed)) unalignArch; -#endif - - MEM_STATIC U16 MEM_read16(const void* ptr) -{ - return ((const unalign16*)ptr)->v; -} -MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign32*)ptr)->v; } -MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign64*)ptr)->v; } -MEM_STATIC size_t MEM_readST(const void* ptr) { return ((const unalignArch*)ptr)->v; } - -MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign16*)memPtr)->v = value; } -MEM_STATIC void MEM_write32(void* memPtr, U32 value) { ((unalign32*)memPtr)->v = value; } -MEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign64*)memPtr)->v = value; } - -#else - -/* default method, safe and standard. - can sometimes prove slower */ - -MEM_STATIC U16 MEM_read16(const void* memPtr) -{ - U16 val; - memcpy(&val, memPtr, sizeof(val)); - return val; -} - -MEM_STATIC U32 MEM_read32(const void* memPtr) -{ - U32 val; - memcpy(&val, memPtr, sizeof(val)); - return val; -} - -MEM_STATIC U64 MEM_read64(const void* memPtr) -{ - U64 val; - memcpy(&val, memPtr, sizeof(val)); - return val; -} - -MEM_STATIC size_t MEM_readST(const void* memPtr) -{ - size_t val; - memcpy(&val, memPtr, sizeof(val)); - return val; -} - -MEM_STATIC void MEM_write16(void* memPtr, U16 value) { memcpy(memPtr, &value, sizeof(value)); } - -MEM_STATIC void MEM_write32(void* memPtr, U32 value) { memcpy(memPtr, &value, sizeof(value)); } - -MEM_STATIC void MEM_write64(void* memPtr, U64 value) { memcpy(memPtr, &value, sizeof(value)); } - -#endif /* MEM_FORCE_MEMORY_ACCESS */ - -MEM_STATIC U32 MEM_swap32(U32 in) -{ -#if defined(_MSC_VER) /* Visual Studio */ - return _byteswap_ulong(in); -#elif (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) || \ - (defined(__clang__) && __has_builtin(__builtin_bswap32)) - return __builtin_bswap32(in); -#else - return ((in << 24) & 0xff000000) | ((in << 8) & 0x00ff0000) | ((in >> 8) & 0x0000ff00) | - ((in >> 24) & 0x000000ff); -#endif -} - -MEM_STATIC U64 MEM_swap64(U64 in) -{ -#if defined(_MSC_VER) /* Visual Studio */ - return _byteswap_uint64(in); -#elif (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) || \ - (defined(__clang__) && __has_builtin(__builtin_bswap64)) - return __builtin_bswap64(in); -#else - return ((in << 56) & 0xff00000000000000ULL) | ((in << 40) & 0x00ff000000000000ULL) | - ((in << 24) & 0x0000ff0000000000ULL) | ((in << 8) & 0x000000ff00000000ULL) | - ((in >> 8) & 0x00000000ff000000ULL) | ((in >> 24) & 0x0000000000ff0000ULL) | - ((in >> 40) & 0x000000000000ff00ULL) | ((in >> 56) & 0x00000000000000ffULL); -#endif -} - -MEM_STATIC size_t MEM_swapST(size_t in) -{ - if (MEM_32bits()) { - return static_cast(MEM_swap32(static_cast(in))); - } else { - return static_cast(MEM_swap64(static_cast(in))); - } -} - -/*=== Little endian r/w ===*/ - -MEM_STATIC U16 MEM_readLE16(const void* memPtr) -{ - if (MEM_isLittleEndian()) { - return MEM_read16(memPtr); - } else { - const BYTE* p = (const BYTE*)memPtr; - return static_cast(p[0] + (p[1] << 8)); - } -} - -MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val) -{ - if (MEM_isLittleEndian()) { - MEM_write16(memPtr, val); - } else { - BYTE* p = (BYTE*)memPtr; - p[0] = static_cast(val); - p[1] = static_cast(val >> 8); - } -} - -MEM_STATIC U32 MEM_readLE24(const void* memPtr) -{ - return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16); -} - -MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val) -{ - MEM_writeLE16(memPtr, static_cast(val)); - ((BYTE*)memPtr)[2] = static_cast(val >> 16); -} - -MEM_STATIC U32 MEM_readLE32(const void* memPtr) -{ - if (MEM_isLittleEndian()) { - return MEM_read32(memPtr); - } else { - return MEM_swap32(MEM_read32(memPtr)); - } -} - -MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32) -{ - if (MEM_isLittleEndian()) { - MEM_write32(memPtr, val32); - } else { - MEM_write32(memPtr, MEM_swap32(val32)); - } -} - -MEM_STATIC U64 MEM_readLE64(const void* memPtr) -{ - if (MEM_isLittleEndian()) { - return MEM_read64(memPtr); - } else { - return MEM_swap64(MEM_read64(memPtr)); - } -} - -MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64) -{ - if (MEM_isLittleEndian()) { - MEM_write64(memPtr, val64); - } else { - MEM_write64(memPtr, MEM_swap64(val64)); - } -} - -MEM_STATIC size_t MEM_readLEST(const void* memPtr) -{ - if (MEM_32bits()) { - return static_cast(MEM_readLE32(memPtr)); - } else { - return static_cast(MEM_readLE64(memPtr)); - } -} - -MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val) -{ - if (MEM_32bits()) { - MEM_writeLE32(memPtr, static_cast(val)); - } else { - MEM_writeLE64(memPtr, static_cast(val)); - } -} - -/*=== Big endian r/w ===*/ - -MEM_STATIC U32 MEM_readBE32(const void* memPtr) -{ - if (MEM_isLittleEndian()) { - return MEM_swap32(MEM_read32(memPtr)); - } else { - return MEM_read32(memPtr); - } -} - -MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32) -{ - if (MEM_isLittleEndian()) { - MEM_write32(memPtr, MEM_swap32(val32)); - } else { - MEM_write32(memPtr, val32); - } -} - -MEM_STATIC U64 MEM_readBE64(const void* memPtr) -{ - if (MEM_isLittleEndian()) { - return MEM_swap64(MEM_read64(memPtr)); - } else { - return MEM_read64(memPtr); - } -} - -MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64) -{ - if (MEM_isLittleEndian()) { - MEM_write64(memPtr, MEM_swap64(val64)); - } else { - MEM_write64(memPtr, val64); - } -} - -MEM_STATIC size_t MEM_readBEST(const void* memPtr) -{ - if (MEM_32bits()) { - return static_cast(MEM_readBE32(memPtr)); - } else { - return static_cast(MEM_readBE64(memPtr)); - } -} - -MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val) -{ - if (MEM_32bits()) { - MEM_writeBE32(memPtr, static_cast(val)); - } else { - MEM_writeBE64(memPtr, static_cast(val)); - } -} - -#if defined(__cplusplus) -} -#endif - -#endif /* MEM_H_MODULE */ diff --git a/ucm/store/compress/cc/compress_lib/securec.h b/ucm/store/compress/cc/compress_lib/securec.h new file mode 100644 index 000000000..281db7908 --- /dev/null +++ b/ucm/store/compress/cc/compress_lib/securec.h @@ -0,0 +1,621 @@ +/* + * Copyright (c) [2019-2020] Huawei Technologies Co.,Ltd.All rights reserved. + * + * OpenArkCompiler is licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * + * http://license.coscl.org.cn/MulanPSL + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR + * FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v1 for more details. + */ + +#ifndef __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 +#define __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 + +#include "securectype.h" +#ifndef SECUREC_HAVE_STDARG_H +#define SECUREC_HAVE_STDARG_H 1 +#endif + +#if SECUREC_HAVE_STDARG_H +#include +#endif + +#ifndef SECUREC_HAVE_ERRNO_H +#if SECUREC_IN_KERNEL +#define SECUREC_HAVE_ERRNO_H 0 +#else +#define SECUREC_HAVE_ERRNO_H 1 +#endif +#endif + +/* EINVAL ERANGE may defined in errno.h */ +#if SECUREC_HAVE_ERRNO_H +#include +#endif + +/* Define error code */ +#if defined(SECUREC_NEED_ERRNO_TYPE) || !defined(__STDC_WANT_LIB_EXT1__) || \ + (defined(__STDC_WANT_LIB_EXT1__) && (__STDC_WANT_LIB_EXT1__ == 0)) +#ifndef SECUREC_DEFINED_ERRNO_TYPE +#define SECUREC_DEFINED_ERRNO_TYPE +/* Just check whether macrodefinition exists. */ +#ifndef errno_t +typedef int errno_t; +#endif +#endif +#endif + +/* Success */ +#ifndef EOK +#define EOK 0 +#endif + +#ifndef EINVAL +/* The src buffer is not correct and destination buffer can't not be reset */ +#define EINVAL 22 +#endif + +#ifndef EINVAL_AND_RESET +/* Once the error is detected, the dest buffer must be reset! */ +#define EINVAL_AND_RESET (22 | 128) +#endif + +#ifndef ERANGE +/* The destination buffer is not long enough and destination buffer can not be reset */ +#define ERANGE 34 +#endif + +#ifndef ERANGE_AND_RESET +/* Once the error is detected, the dest buffer must be reset! */ +#define ERANGE_AND_RESET (34 | 128) +#endif + +#ifndef EOVERLAP_AND_RESET +/* Once the buffer overlap is detected, the dest buffer must be reset! */ +#define EOVERLAP_AND_RESET (54 | 128) +#endif + +/* If you need export the function of this library in Win32 dll, use __declspec(dllexport) */ +#ifndef SECUREC_API +#if defined(SECUREC_DLL_EXPORT) +#define SECUREC_API __declspec(dllexport) +#elif defined(SECUREC_DLL_IMPORT) +#define SECUREC_API __declspec(dllimport) +#else +/* + * Standardized function declaration. If a security function is declared in the your code, + * it may cause a compilation alarm,Please delete the security function you declared. + * Adding extern under windows will cause the system to have inline functions to expand, + * so do not add the extern in default + */ +#if defined(_MSC_VER) +#define SECUREC_API +#else +#define SECUREC_API extern +#endif +#endif +#endif + +#ifdef __cplusplus +extern "C" { +#endif +/* + * Description: The GetHwSecureCVersion function get SecureC Version string and version number. + * Parameter: verNumber - to store version number + * Return: version string + */ +SECUREC_API const char *GetHwSecureCVersion(unsigned short *verNumber); + +#if SECUREC_ENABLE_MEMSET +/* + * Description: The memset_s function copies the value of c (converted to an unsigned char) into + * each of the first count characterrs of the object pointed to by dest. Parameter: dest - + * destination address Parameter: destMax - The maximum length of destination buffer Parameter: c - + * the value to be copied Parameter: count - copies count bytes of value to dest Return: EOK if + * there was no runtime-constraint violation + */ +SECUREC_API errno_t memset_s(void *dest, size_t destMax, int c, size_t count); +#endif + +#ifndef SECUREC_ONLY_DECLARE_MEMSET +#define SECUREC_ONLY_DECLARE_MEMSET 0 +#endif + +#if SECUREC_ONLY_DECLARE_MEMSET == 0 + +#if SECUREC_ENABLE_MEMMOVE +/* + * Description: The memmove_s function copies n characterrs from the object pointed to by src + * into the object pointed to by dest. + * Parameter: dest - destination address + * Parameter: destMax - The maximum length of destination buffer + * Parameter: src - source address + * Parameter: count - copies count bytes from the src + * Return: EOK if there was no runtime-constraint violation + */ +SECUREC_API errno_t memmove_s(void *dest, size_t destMax, const void *src, size_t count); +#endif + +#if SECUREC_ENABLE_MEMCPY +/* + * Description: The memcpy_s function copies n characterrs from the object pointed to + * by src into the object pointed to by dest. + * Parameter: dest - destination address + * Parameter: destMax - The maximum length of destination buffer + * Parameter: src - source address + * Parameter: count - copies count bytes from the src + * Return: EOK if there was no runtime-constraint violation + */ +SECUREC_API errno_t memcpy_s(void *dest, size_t destMax, const void *src, size_t count); +#endif + +#if SECUREC_ENABLE_STRCPY +/* + * Description: The strcpy_s function copies the string pointed to by strSrc (including + * the terminating null characterr) into the array pointed to by strDest + * Parameter: strDest - destination address + * Parameter: destMax - The maximum length of destination buffer(including the terminating null + * characterr) Parameter: strSrc - source address Return: EOK if there was no runtime-constraint + * violation + */ +SECUREC_API errno_t strcpy_s(char *strDest, size_t destMax, const char *strSrc); +#endif + +#if SECUREC_ENABLE_STRNCPY +/* + * Description: The strncpy_s function copies not more than n successive characterrs (not including + * the terminating null characterr) from the array pointed to by strSrc to the array pointed to by + * strDest. Parameter: strDest - destination address Parameter: destMax - The maximum length of + * destination buffer(including the terminating null characterr) Parameter: strSrc - source address + * Parameter: count - copies count characterrs from the src + * Return: EOK if there was no runtime-constraint violation + */ +SECUREC_API errno_t strncpy_s(char *strDest, size_t destMax, const char *strSrc, size_t count); +#endif + +#if SECUREC_ENABLE_STRCAT +/* + * Description: The strcat_s function appends a copy of the string pointed to by strSrc (including + * the terminating null characterr) to the end of the string pointed to by strDest. + * Parameter: strDest - destination address + * Parameter: destMax - The maximum length of destination buffer(including the terminating null wide + * characterr) Parameter: strSrc - source address Return: EOK if there was no runtime-constraint + * violation + */ +SECUREC_API errno_t strcat_s(char *strDest, size_t destMax, const char *strSrc); +#endif + +#if SECUREC_ENABLE_STRNCAT +/* + * Description: The strncat_s function appends not more than n successive characterrs (not + * including the terminating null characterr) from the array pointed to by strSrc to the end of the + * string pointed to by strDest. Parameter: strDest - destination address Parameter: destMax - The + * maximum length of destination buffer(including the terminating null characterr) Parameter: strSrc + * - source address Parameter: count - copies count characterrs from the src Return: EOK if + * there was no runtime-constraint violation + */ +SECUREC_API errno_t strncat_s(char *strDest, size_t destMax, const char *strSrc, size_t count); +#endif + +#if SECUREC_ENABLE_VSPRINTF +/* + * Description: The vsprintf_s function is equivalent to the vsprintf function except for the + * parameter destMax and the explicit runtime-constraints violation Parameter: strDest - produce + * output according to a format ,write to the characterr string strDest. Parameter: destMax - The + * maximum length of destination buffer(including the terminating null wide character) Parameter: + * format - format string Parameter: argList - instead of a variable number of arguments Return: + * the number of characterrs printed(not including the terminating null byte '\0'), If an error + * occurred Return: -1. + */ +SECUREC_API int vsprintf_s(char *strDest, size_t destMax, const char *format, va_list argList) + SECUREC_ATTRIBUTE(3, 0); +#endif + +#if SECUREC_ENABLE_SPRINTF +/* + * Description: The sprintf_s function is equivalent to the sprintf function except for the + * parameter destMax and the explicit runtime-constraints violation Parameter: strDest - produce + * output according to a format ,write to the characterr string strDest. Parameter: destMax - The + * maximum length of destination buffer(including the terminating null byte '\0') Parameter: format + * - format string Return: the number of characterrs printed(not including the terminating null + * byte '\0'), If an error occurred Return: -1. + */ +SECUREC_API int sprintf_s(char *strDest, size_t destMax, const char *format, ...) + SECUREC_ATTRIBUTE(3, 4); +#endif + +#if SECUREC_ENABLE_VSNPRINTF +/* + * Description: The vsnprintf_s function is equivalent to the vsnprintf function except for + * the parameter destMax/count and the explicit runtime-constraints violation + * Parameter: strDest - produce output according to a format ,write to the characterr string + * strDest. Parameter: destMax - The maximum length of destination buffer(including the terminating + * null byte '\0') Parameter: count - do not write more than count bytes to strDest(not including + * the terminating null byte '\0') Parameter: format - format string Parameter: argList - instead + * of a variable number of arguments Return: the number of characterrs printed(not including + * the terminating null byte '\0'), If an error occurred Return: -1.Pay special attention to + * returning -1 when truncation occurs + */ +SECUREC_API int vsnprintf_s(char *strDest, size_t destMax, size_t count, const char *format, + va_list argList) SECUREC_ATTRIBUTE(4, 0); +#endif + +#if SECUREC_ENABLE_SNPRINTF +/* + * Description: The snprintf_s function is equivalent to the snprintf function except for + * the parameter destMax/count and the explicit runtime-constraints violation + * Parameter: strDest - produce output according to a format ,write to the characterr string + * strDest. Parameter: destMax - The maximum length of destination buffer(including the terminating + * null byte '\0') Parameter: count - do not write more than count bytes to strDest(not including + * the terminating null byte '\0') Parameter: format - format string Return: the number of + * characterrs printed(not including the terminating null byte '\0'), If an error occurred Return: + * -1.Pay special attention to returning -1 when truncation occurs + */ +SECUREC_API int snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, ...) + SECUREC_ATTRIBUTE(4, 5); +#endif + +#if SECUREC_SNPRINTF_TRUNCATED +/* + * Description: The vsnprintf_truncated_s function is equivalent to the vsnprintf_s function except + * no count parameter and return value + * Parameter: strDest - produce output according to a format ,write to the characterr string + * strDest Parameter: destMax - The maximum length of destination buffer(including the terminating + * null byte '\0') Parameter: format - format string Parameter: argList - instead of a variable + * number of arguments Return: the number of characterrs printed(not including the terminating + * null byte '\0'), If an error occurred Return: -1.Pay special attention to returning destMax - 1 + * when truncation occurs + */ +SECUREC_API int vsnprintf_truncated_s(char *strDest, size_t destMax, const char *format, + va_list argList) SECUREC_ATTRIBUTE(3, 0); + +/* + * Description: The snprintf_truncated_s function is equivalent to the snprintf_2 function except + * no count parameter and return value + * Parameter: strDest - produce output according to a format ,write to the characterr string + * strDest. Parameter: destMax - The maximum length of destination buffer(including the terminating + * null byte '\0') Parameter: format - format string Return: the number of characterrs + * printed(not including the terminating null byte '\0'), If an error occurred Return: -1.Pay + * special attention to returning destMax - 1 when truncation occurs + */ +SECUREC_API int snprintf_truncated_s(char *strDest, size_t destMax, const char *format, ...) + SECUREC_ATTRIBUTE(3, 4); +#endif + +#if SECUREC_ENABLE_SCANF +/* + * Description: The scanf_s function is equivalent to fscanf_s with the argument stdin + * interposed before the arguments to scanf_s + * Parameter: format - format string + * Return: the number of input items assigned, If an error occurred Return: -1. + */ +SECUREC_API int scanf_s(const char *format, ...); +#endif + +#if SECUREC_ENABLE_VSCANF +/* + * Description: The vscanf_s function is equivalent to scanf_s, with the variable argument list + * replaced by argList Parameter: format - format string Parameter: argList - instead of a variable + * number of arguments Return: the number of input items assigned, If an error occurred Return: + * -1. + */ +SECUREC_API int vscanf_s(const char *format, va_list argList); +#endif + +#if SECUREC_ENABLE_SSCANF +/* + * Description: The sscanf_s function is equivalent to fscanf_s, except that input is obtained from + * a string (specified by the argument buffer) rather than from a stream Parameter: buffer - read + * characterr from buffer Parameter: format - format string Return: the number of input items + * assigned, If an error occurred Return: -1. + */ +SECUREC_API int sscanf_s(const char *buffer, const char *format, ...); +#endif + +#if SECUREC_ENABLE_VSSCANF +/* + * Description: The vsscanf_s function is equivalent to sscanf_s, with the variable argument list + * replaced by argList + * Parameter: buffer - read characterr from buffer + * Parameter: format - format string + * Parameter: argList - instead of a variable number of arguments + * Return: the number of input items assigned, If an error occurred Return: -1. + */ +SECUREC_API int vsscanf_s(const char *buffer, const char *format, va_list argList); +#endif + +#if SECUREC_ENABLE_FSCANF +/* + * Description: The fscanf_s function is equivalent to fscanf except that the c, s, and [ + * conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by + * a*) Parameter: stream - stdio file stream Parameter: format - format string Return: the number + * of input items assigned, If an error occurred Return: -1. + */ +SECUREC_API int fscanf_s(FILE *stream, const char *format, ...); +#endif + +#if SECUREC_ENABLE_VFSCANF +/* + * Description: The vfscanf_s function is equivalent to fscanf_s, with the variable argument list + * replaced by argList + * Parameter: stream - stdio file stream + * Parameter: format - format string + * Parameter: argList - instead of a variable number of arguments + * Return: the number of input items assigned, If an error occurred Return: -1. + */ +SECUREC_API int vfscanf_s(FILE *stream, const char *format, va_list argList); +#endif + +#if SECUREC_ENABLE_STRTOK +/* + * Description: The strtok_s function parses a string into a sequence of strToken, + * replace all characterrs in strToken string that match to strDelimit set with 0. + * On the first call to strtok_s the string to be parsed should be specified in strToken. + * In each subsequent call that should parse the same string, strToken should be NULL + * Parameter: strToken - the string to be delimited + * Parameter: strDelimit - specifies a set of characterrs that delimit the tokens in the parsed + * string Parameter: context - is a pointer to a char * variable that is used internally by strtok_s + * function Return: On the first call returns the address of the first non \0 characterr, otherwise + * NULL is returned. In subsequent calls, the strtoken is set to NULL, and the context set is the + * same as the previous call, return NULL if the *context string length is equal 0, otherwise return + * *context. + */ +SECUREC_API char *strtok_s(char *strToken, const char *strDelimit, char **context); +#endif + +#if SECUREC_ENABLE_GETS && SECUREC_IN_KERNEL == 0 +/* + * Description: The gets_s function reads at most one less than the number of characterrs specified + * by destMax from the stream pointed to by stdin, into the array pointed to by buffer + * Parameter: buffer - destination address + * Parameter: destMax - The maximum length of destination buffer(including the terminating null + * characterr) Return: buffer if there was no runtime-constraint violation,If an error occurred + * Return: NULL. + */ +SECUREC_API char *gets_s(char *buffer, size_t destMax); +#endif + +#if SECUREC_ENABLE_WCHAR_FUNC +#if SECUREC_ENABLE_MEMCPY +/* + * Description: The wmemcpy_s function copies n successive wide characterrs from the object pointed + * to by src into the object pointed to by dest. Parameter: dest - destination address Parameter: + * destMax - The maximum length of destination buffer Parameter: src - source address Parameter: + * count - copies count wide characterrs from the src Return: EOK if there was no + * runtime-constraint violation + */ +SECUREC_API errno_t wmemcpy_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count); +#endif + +#if SECUREC_ENABLE_MEMMOVE +/* + * Description: The wmemmove_s function copies n successive wide characterrs from the object + * pointed to by src into the object pointed to by dest. + * Parameter: dest - destination address + * Parameter: destMax - The maximum length of destination buffer + * Parameter: src - source address + * Parameter: count - copies count wide characterrs from the src + * Return: EOK if there was no runtime-constraint violation + */ +SECUREC_API errno_t wmemmove_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count); +#endif + +#if SECUREC_ENABLE_STRCPY +/* + * Description: The wcscpy_s function copies the wide string pointed to by strSrc (including + * theterminating null wide characterr) into the array pointed to by strDest Parameter: strDest - + * destination address Parameter: destMax - The maximum length of destination buffer Parameter: + * strSrc - source address Return: EOK if there was no runtime-constraint violation + */ +SECUREC_API errno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc); +#endif + +#if SECUREC_ENABLE_STRNCPY +/* + * Description: The wcsncpy_s function copies not more than n successive wide characterrs (not + * including the terminating null wide characterr) from the array pointed to by strSrc to the array + * pointed to by strDest Parameter: strDest - destination address Parameter: destMax - The maximum + * length of destination buffer(including the terminating wide characterr) Parameter: strSrc - + * source address Parameter: count - copies count wide characterrs from the src Return: EOK if + * there was no runtime-constraint violation + */ +SECUREC_API errno_t wcsncpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, + size_t count); +#endif + +#if SECUREC_ENABLE_STRCAT +/* + * Description: The wcscat_s function appends a copy of the wide string pointed to by strSrc + * (including the terminating null wide characterr) to the end of the wide string pointed to by + * strDest Parameter: strDest - destination address Parameter: destMax - The maximum length of + * destination buffer(including the terminating wide characterr) Parameter: strSrc - source address + * Return: EOK if there was no runtime-constraint violation + */ +SECUREC_API errno_t wcscat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc); +#endif + +#if SECUREC_ENABLE_STRNCAT +/* + * Description: The wcsncat_s function appends not more than n successive wide characterrs (not + * including the terminating null wide characterr) from the array pointed to by strSrc to the end of + * the wide string pointed to by strDest. Parameter: strDest - destination address Parameter: + * destMax - The maximum length of destination buffer(including the terminating wide characterr) + * Parameter: strSrc - source address + * Parameter: count - copies count wide characterrs from the src + * Return: EOK if there was no runtime-constraint violation + */ +SECUREC_API errno_t wcsncat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, + size_t count); +#endif + +#if SECUREC_ENABLE_STRTOK +/* + * Description: The wcstok_s function is the wide-characterr equivalent of the strtok_s + * function Parameter: strToken - the string to be delimited Parameter: strDelimit - specifies a set + * of characterrs that delimit the tokens in the parsed string Parameter: context - is a pointer to + * a char * variable that is used internally by strtok_s function Return: a pointer to the first + * characterr of a token, or a null pointer if there is no token or there is a runtime-constraint + * violation. + */ +SECUREC_API wchar_t *wcstok_s(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context); +#endif + +#if SECUREC_ENABLE_VSPRINTF +/* + * Description: The vswprintf_s function is the wide-characterr equivalent of the vsprintf_s + * function Parameter: strDest - produce output according to a format ,write to the characterr + * string strDest Parameter: destMax - The maximum length of destination buffer(including the + * terminating null ) Parameter: format - format string Parameter: argList - instead of a variable + * number of arguments Return: the number of characterrs printed(not including the terminating + * null wide character), If an error occurred Return: -1. + */ +SECUREC_API int vswprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, + va_list argList); +#endif + +#if SECUREC_ENABLE_SPRINTF + +/* + * Description: The swprintf_s function is the wide-characterr equivalent of the sprintf_s + * function Parameter: strDest - produce output according to a format ,write to the characterr + * string strDest Parameter: destMax - The maximum length of destination buffer(including the + * terminating null ) Parameter: format - format string Return: the number of characterrs + * printed(not including the terminating null wide character), If an error occurred Return: -1. + */ +SECUREC_API int swprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, ...); +#endif + +#if SECUREC_ENABLE_FSCANF +/* + * Description: The fwscanf_s function is the wide-characterr equivalent of the fscanf_s + * function Parameter: stream - stdio file stream Parameter: format - format string Return: the + * number of input items assigned, If an error occurred Return: -1. + */ +SECUREC_API int fwscanf_s(FILE *stream, const wchar_t *format, ...); +#endif + +#if SECUREC_ENABLE_VFSCANF +/* + * Description: The vfwscanf_s function is the wide-characterr equivalent of the vfscanf_s + * function Parameter: stream - stdio file stream Parameter: format - format string Parameter: + * argList - instead of a variable number of arguments Return: the number of input items + * assigned, If an error occurred Return: -1. + */ +SECUREC_API int vfwscanf_s(FILE *stream, const wchar_t *format, va_list argList); +#endif + +#if SECUREC_ENABLE_SCANF +/* + * Description: The wscanf_s function is the wide-characterr equivalent of the scanf_s + * function Parameter: format - format string Return: the number of input items assigned, If an + * error occurred Return: -1. + */ +SECUREC_API int wscanf_s(const wchar_t *format, ...); +#endif + +#if SECUREC_ENABLE_VSCANF +/* + * Description: The vwscanf_s function is the wide-characterr equivalent of the vscanf_s + * function Parameter: format - format string Parameter: argList - instead of a variable number of + * arguments Return: the number of input items assigned, If an error occurred Return: -1. + */ +SECUREC_API int vwscanf_s(const wchar_t *format, va_list argList); +#endif + +#if SECUREC_ENABLE_SSCANF +/* + * Description: The swscanf_s function is the wide-characterr equivalent of the sscanf_s + * function Parameter: buffer - read characterr from buffer Parameter: format - format string + * Return: the number of input items assigned, If an error occurred Return: -1. + */ +SECUREC_API int swscanf_s(const wchar_t *buffer, const wchar_t *format, ...); +#endif + +#if SECUREC_ENABLE_VSSCANF +/* + * Description: The vswscanf_s function is the wide-characterr equivalent of the vsscanf_s + * function Parameter: buffer - read characterr from buffer Parameter: format - format string + * Parameter: argList - instead of a variable number of arguments + * Return: the number of input items assigned, If an error occurred Return: -1. + */ +SECUREC_API int vswscanf_s(const wchar_t *buffer, const wchar_t *format, va_list argList); +#endif +#endif /* SECUREC_ENABLE_WCHAR_FUNC */ +#endif + +/* Those functions are used by macro ,must declare hare , also for without function declaration + * warning */ +extern errno_t strncpy_error(char *strDest, size_t destMax, const char *strSrc, size_t count); +extern errno_t strcpy_error(char *strDest, size_t destMax, const char *strSrc); + +#if SECUREC_WITH_PERFORMANCE_ADDONS +/* Those functions are used by macro */ +extern errno_t memset_sOptAsm(void *dest, size_t destMax, int c, size_t count); +extern errno_t memset_sOptTc(void *dest, size_t destMax, int c, size_t count); +extern errno_t memcpy_sOptAsm(void *dest, size_t destMax, const void *src, size_t count); +extern errno_t memcpy_sOptTc(void *dest, size_t destMax, const void *src, size_t count); + +/* The strcpy_sp is a macro, not a function in performance optimization mode. */ +#define strcpy_sp(dest, destMax, src) \ + ((__builtin_constant_p((destMax)) && __builtin_constant_p((src))) \ + ? SECUREC_STRCPY_SM((dest), (destMax), (src)) \ + : strcpy_s((dest), (destMax), (src))) + +/* The strncpy_sp is a macro, not a function in performance optimization mode. */ +#define strncpy_sp(dest, destMax, src, count) \ + ((__builtin_constant_p((count)) && __builtin_constant_p((destMax)) && \ + __builtin_constant_p((src))) \ + ? SECUREC_STRNCPY_SM((dest), (destMax), (src), (count)) \ + : strncpy_s((dest), (destMax), (src), (count))) + +/* The strcat_sp is a macro, not a function in performance optimization mode. */ +#define strcat_sp(dest, destMax, src) \ + ((__builtin_constant_p((destMax)) && __builtin_constant_p((src))) \ + ? SECUREC_STRCAT_SM((dest), (destMax), (src)) \ + : strcat_s((dest), (destMax), (src))) + +/* The strncat_sp is a macro, not a function in performance optimization mode. */ +#define strncat_sp(dest, destMax, src, count) \ + ((__builtin_constant_p((count)) && __builtin_constant_p((destMax)) && \ + __builtin_constant_p((src))) \ + ? SECUREC_STRNCAT_SM((dest), (destMax), (src), (count)) \ + : strncat_s((dest), (destMax), (src), (count))) + +/* The memcpy_sp is a macro, not a function in performance optimization mode. */ +#define memcpy_sp(dest, destMax, src, count) \ + (__builtin_constant_p((count)) \ + ? (SECUREC_MEMCPY_SM((dest), (destMax), (src), (count))) \ + : (__builtin_constant_p((destMax)) \ + ? (((size_t)(destMax) > 0 && (((unsigned long long)(destMax) & \ + (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) \ + ? memcpy_sOptTc((dest), (destMax), (src), (count)) \ + : ERANGE) \ + : memcpy_sOptAsm((dest), (destMax), (src), (count)))) + +/* The memset_sp is a macro, not a function in performance optimization mode. */ +#define memset_sp(dest, destMax, c, count) \ + (__builtin_constant_p((count)) ? (SECUREC_MEMSET_SM((dest), (destMax), (c), (count))) \ + : (__builtin_constant_p((destMax)) \ + ? (((((unsigned long long)(destMax) & \ + (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) \ + ? memset_sOptTc((dest), (destMax), (c), (count)) \ + : ERANGE) \ + : memset_sOptAsm((dest), (destMax), (c), (count)))) +#else +#define strcpy_sp strcpy_s +#define strncpy_sp strncpy_s +#define strcat_sp strcat_s +#define strncat_sp strncat_s +#define memcpy_sp memcpy_s +#define memset_sp memset_s +#endif + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 */ diff --git a/ucm/store/compress/cc/compress_lib/securec_check.h b/ucm/store/compress/cc/compress_lib/securec_check.h new file mode 100644 index 000000000..d40914fff --- /dev/null +++ b/ucm/store/compress/cc/compress_lib/securec_check.h @@ -0,0 +1,13 @@ +#ifndef SECUREC_CHECK_H +#define SECUREC_CHECK_H + +#include "securec.h" +#include "tunstall.h" + +#define SECUREC_CHECK(call) \ + do { \ + errno_t secErr = (call); \ + if (secErr != EOK) { return R_ERR_COPY_FAILED; } \ + } while (0) + +#endif // SECUREC_CHECK_H diff --git a/ucm/store/compress/cc/compress_lib/securectype.h b/ucm/store/compress/cc/compress_lib/securectype.h new file mode 100644 index 000000000..206579804 --- /dev/null +++ b/ucm/store/compress/cc/compress_lib/securectype.h @@ -0,0 +1,570 @@ +/* + * Copyright (c) [2019-2020] Huawei Technologies Co.,Ltd.All rights reserved. + * + * OpenArkCompiler is licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * + * http://license.coscl.org.cn/MulanPSL + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR + * FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v1 for more details. + */ +/* + * [Standardize-exceptions]: Performance-sensitive + * [reason]: Strict parameter verification has been done before use + */ + +#ifndef __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 +#define __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 + +#ifndef SECUREC_USING_STD_SECURE_LIB +#if defined(_MSC_VER) && _MSC_VER >= 1400 +#if defined(__STDC_WANT_SECURE_LIB__) && __STDC_WANT_SECURE_LIB__ == 0 +/* Security functions have been provided since vs2005, default use of system library functions */ +#define SECUREC_USING_STD_SECURE_LIB 0 +#else +#define SECUREC_USING_STD_SECURE_LIB 1 +#endif +#else +#define SECUREC_USING_STD_SECURE_LIB 0 +#endif +#endif + +/* Compatibility with older Secure C versions, shielding VC symbol redefinition warning */ +#if defined(_MSC_VER) && _MSC_VER >= 1400 && SECUREC_USING_STD_SECURE_LIB == 0 +#ifndef SECUREC_DISABLE_CRT_FUNC +#define SECUREC_DISABLE_CRT_FUNC 1 +#endif +#ifndef SECUREC_DISABLE_CRT_IMP +#define SECUREC_DISABLE_CRT_IMP 1 +#endif +#else /* MSC VER */ +#ifndef SECUREC_DISABLE_CRT_FUNC +#define SECUREC_DISABLE_CRT_FUNC 0 +#endif +#ifndef SECUREC_DISABLE_CRT_IMP +#define SECUREC_DISABLE_CRT_IMP 0 +#endif +#endif + +#if SECUREC_DISABLE_CRT_FUNC +#ifdef __STDC_WANT_SECURE_LIB__ +#undef __STDC_WANT_SECURE_LIB__ +#endif +#define __STDC_WANT_SECURE_LIB__ 0 +#endif + +#if SECUREC_DISABLE_CRT_IMP +#ifdef _CRTIMP_ALTERNATIVE +#undef _CRTIMP_ALTERNATIVE +#endif +#define _CRTIMP_ALTERNATIVE /* Comment microsoft *_s function */ +#endif + +/* Compile in kernel under macro control */ +#ifndef SECUREC_IN_KERNEL +#ifdef __KERNEL__ +#define SECUREC_IN_KERNEL 1 +#else +#define SECUREC_IN_KERNEL 0 +#endif +#endif + +#if SECUREC_IN_KERNEL +#ifndef SECUREC_ENABLE_SCANF_FILE +#define SECUREC_ENABLE_SCANF_FILE 0 +#endif +#ifndef SECUREC_ENABLE_WCHAR_FUNC +#define SECUREC_ENABLE_WCHAR_FUNC 0 +#endif +#else /* SECUREC_IN_KERNEL */ +#ifndef SECUREC_ENABLE_SCANF_FILE +#define SECUREC_ENABLE_SCANF_FILE 1 +#endif +#ifndef SECUREC_ENABLE_WCHAR_FUNC +#define SECUREC_ENABLE_WCHAR_FUNC 1 +#endif +#endif + +/* Default secure function declaration, default declarations for non-standard functions */ +#ifndef SECUREC_SNPRINTF_TRUNCATED +#define SECUREC_SNPRINTF_TRUNCATED 1 +#endif + +#if SECUREC_USING_STD_SECURE_LIB +#if defined(_MSC_VER) && _MSC_VER >= 1400 +/* Declare secure functions that are not available in the VS compiler */ +#ifndef SECUREC_ENABLE_MEMSET +#define SECUREC_ENABLE_MEMSET 1 +#endif +/* VS 2005 have vsnprintf_s function */ +#ifndef SECUREC_ENABLE_VSNPRINTF +#define SECUREC_ENABLE_VSNPRINTF 0 +#endif +#ifndef SECUREC_ENABLE_SNPRINTF +/* VS 2005 have vsnprintf_s function Adapt the snprintf_s of the security function */ +#define snprintf_s _snprintf_s +#define SECUREC_ENABLE_SNPRINTF 0 +#endif +/* Before VS 2010 do not have v functions */ +#if _MSC_VER <= 1600 || defined(SECUREC_FOR_V_SCANFS) +#ifndef SECUREC_ENABLE_VFSCANF +#define SECUREC_ENABLE_VFSCANF 1 +#endif +#ifndef SECUREC_ENABLE_VSCANF +#define SECUREC_ENABLE_VSCANF 1 +#endif +#ifndef SECUREC_ENABLE_VSSCANF +#define SECUREC_ENABLE_VSSCANF 1 +#endif +#endif + +#else /* MSC VER */ +#ifndef SECUREC_ENABLE_MEMSET +#define SECUREC_ENABLE_MEMSET 0 +#endif +#ifndef SECUREC_ENABLE_SNPRINTF +#define SECUREC_ENABLE_SNPRINTF 0 +#endif +#ifndef SECUREC_ENABLE_VSNPRINTF +#define SECUREC_ENABLE_VSNPRINTF 0 +#endif +#endif + +#ifndef SECUREC_ENABLE_MEMMOVE +#define SECUREC_ENABLE_MEMMOVE 0 +#endif +#ifndef SECUREC_ENABLE_MEMCPY +#define SECUREC_ENABLE_MEMCPY 0 +#endif +#ifndef SECUREC_ENABLE_STRCPY +#define SECUREC_ENABLE_STRCPY 0 +#endif +#ifndef SECUREC_ENABLE_STRNCPY +#define SECUREC_ENABLE_STRNCPY 0 +#endif +#ifndef SECUREC_ENABLE_STRCAT +#define SECUREC_ENABLE_STRCAT 0 +#endif +#ifndef SECUREC_ENABLE_STRNCAT +#define SECUREC_ENABLE_STRNCAT 0 +#endif +#ifndef SECUREC_ENABLE_SPRINTF +#define SECUREC_ENABLE_SPRINTF 0 +#endif +#ifndef SECUREC_ENABLE_VSPRINTF +#define SECUREC_ENABLE_VSPRINTF 0 +#endif +#ifndef SECUREC_ENABLE_SSCANF +#define SECUREC_ENABLE_SSCANF 0 +#endif +#ifndef SECUREC_ENABLE_VSSCANF +#define SECUREC_ENABLE_VSSCANF 0 +#endif +#ifndef SECUREC_ENABLE_SCANF +#define SECUREC_ENABLE_SCANF 0 +#endif +#ifndef SECUREC_ENABLE_VSCANF +#define SECUREC_ENABLE_VSCANF 0 +#endif + +#ifndef SECUREC_ENABLE_FSCANF +#define SECUREC_ENABLE_FSCANF 0 +#endif +#ifndef SECUREC_ENABLE_VFSCANF +#define SECUREC_ENABLE_VFSCANF 0 +#endif +#ifndef SECUREC_ENABLE_STRTOK +#define SECUREC_ENABLE_STRTOK 0 +#endif +#ifndef SECUREC_ENABLE_GETS +#define SECUREC_ENABLE_GETS 0 +#endif + +#else /* SECUREC USE STD SECURE LIB */ + +#ifndef SECUREC_ENABLE_MEMSET +#define SECUREC_ENABLE_MEMSET 1 +#endif +#ifndef SECUREC_ENABLE_MEMMOVE +#define SECUREC_ENABLE_MEMMOVE 1 +#endif +#ifndef SECUREC_ENABLE_MEMCPY +#define SECUREC_ENABLE_MEMCPY 1 +#endif +#ifndef SECUREC_ENABLE_STRCPY +#define SECUREC_ENABLE_STRCPY 1 +#endif +#ifndef SECUREC_ENABLE_STRNCPY +#define SECUREC_ENABLE_STRNCPY 1 +#endif +#ifndef SECUREC_ENABLE_STRCAT +#define SECUREC_ENABLE_STRCAT 1 +#endif +#ifndef SECUREC_ENABLE_STRNCAT +#define SECUREC_ENABLE_STRNCAT 1 +#endif +#ifndef SECUREC_ENABLE_SPRINTF +#define SECUREC_ENABLE_SPRINTF 1 +#endif +#ifndef SECUREC_ENABLE_VSPRINTF +#define SECUREC_ENABLE_VSPRINTF 1 +#endif +#ifndef SECUREC_ENABLE_SNPRINTF +#define SECUREC_ENABLE_SNPRINTF 1 +#endif +#ifndef SECUREC_ENABLE_VSNPRINTF +#define SECUREC_ENABLE_VSNPRINTF 1 +#endif +#ifndef SECUREC_ENABLE_SSCANF +#define SECUREC_ENABLE_SSCANF 1 +#endif +#ifndef SECUREC_ENABLE_VSSCANF +#define SECUREC_ENABLE_VSSCANF 1 +#endif +#ifndef SECUREC_ENABLE_SCANF +#if SECUREC_ENABLE_SCANF_FILE +#define SECUREC_ENABLE_SCANF 1 +#else +#define SECUREC_ENABLE_SCANF 0 +#endif +#endif +#ifndef SECUREC_ENABLE_VSCANF +#if SECUREC_ENABLE_SCANF_FILE +#define SECUREC_ENABLE_VSCANF 1 +#else +#define SECUREC_ENABLE_VSCANF 0 +#endif +#endif + +#ifndef SECUREC_ENABLE_FSCANF +#if SECUREC_ENABLE_SCANF_FILE +#define SECUREC_ENABLE_FSCANF 1 +#else +#define SECUREC_ENABLE_FSCANF 0 +#endif +#endif +#ifndef SECUREC_ENABLE_VFSCANF +#if SECUREC_ENABLE_SCANF_FILE +#define SECUREC_ENABLE_VFSCANF 1 +#else +#define SECUREC_ENABLE_VFSCANF 0 +#endif +#endif + +#ifndef SECUREC_ENABLE_STRTOK +#define SECUREC_ENABLE_STRTOK 1 +#endif +#ifndef SECUREC_ENABLE_GETS +#define SECUREC_ENABLE_GETS 1 +#endif +#endif /* SECUREC_USE_STD_SECURE_LIB */ + +#if SECUREC_ENABLE_SCANF_FILE == 0 +#if SECUREC_ENABLE_FSCANF +#undef SECUREC_ENABLE_FSCANF +#define SECUREC_ENABLE_FSCANF 0 +#endif +#if SECUREC_ENABLE_VFSCANF +#undef SECUREC_ENABLE_VFSCANF +#define SECUREC_ENABLE_VFSCANF 0 +#endif +#if SECUREC_ENABLE_SCANF +#undef SECUREC_ENABLE_SCANF +#define SECUREC_ENABLE_SCANF 0 +#endif +#if SECUREC_ENABLE_FSCANF +#undef SECUREC_ENABLE_FSCANF +#define SECUREC_ENABLE_FSCANF 0 +#endif + +#endif + +#if SECUREC_IN_KERNEL +#include +#include +#else +#ifndef SECUREC_HAVE_STDIO_H +#define SECUREC_HAVE_STDIO_H 1 +#endif +#ifndef SECUREC_HAVE_STRING_H +#define SECUREC_HAVE_STRING_H 1 +#endif +#ifndef SECUREC_HAVE_STDLIB_H +#define SECUREC_HAVE_STDLIB_H 1 +#endif +#if SECUREC_HAVE_STDIO_H +#include +#endif +#if SECUREC_HAVE_STRING_H +#include +#endif +#if SECUREC_HAVE_STDLIB_H +#include +#endif +#endif + +/* + * If you need high performance, enable the SECUREC_WITH_PERFORMANCE_ADDONS macro, default is + * enable. The macro is automatically closed on the windows platform and linux kernel + */ +#ifndef SECUREC_WITH_PERFORMANCE_ADDONS +#if SECUREC_IN_KERNEL +#define SECUREC_WITH_PERFORMANCE_ADDONS 0 +#else +#define SECUREC_WITH_PERFORMANCE_ADDONS 1 +#endif +#endif + +/* If enable SECUREC_COMPATIBLE_WIN_FORMAT, the output format will be compatible to Windows. */ +#if (defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER)) && \ + !defined(SECUREC_COMPATIBLE_LINUX_FORMAT) +#ifndef SECUREC_COMPATIBLE_WIN_FORMAT +#define SECUREC_COMPATIBLE_WIN_FORMAT +#endif +#endif + +#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) +/* On windows platform, can't use optimized function for there is no __builtin_constant_p like + * function */ +/* If need optimized macro, can define this: define __builtin_constant_p(x) 0 */ +#ifdef SECUREC_WITH_PERFORMANCE_ADDONS +#undef SECUREC_WITH_PERFORMANCE_ADDONS +#define SECUREC_WITH_PERFORMANCE_ADDONS 0 +#endif +#endif + +#if defined(__VXWORKS__) || defined(__vxworks) || defined(__VXWORKS) || \ + defined(_VXWORKS_PLATFORM_) || defined(SECUREC_VXWORKS_VERSION_5_4) +#ifndef SECUREC_VXWORKS_PLATFORM +#define SECUREC_VXWORKS_PLATFORM +#endif +#endif + +/* If enable SECUREC_COMPATIBLE_LINUX_FORMAT, the output format will be compatible to Linux. */ +#if !defined(SECUREC_COMPATIBLE_WIN_FORMAT) && !defined(SECUREC_VXWORKS_PLATFORM) +#ifndef SECUREC_COMPATIBLE_LINUX_FORMAT +#define SECUREC_COMPATIBLE_LINUX_FORMAT +#endif +#endif + +#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT +#ifndef SECUREC_HAVE_STDDEF_H +#define SECUREC_HAVE_STDDEF_H 1 +#endif +/* Some system may no stddef.h */ +#if SECUREC_HAVE_STDDEF_H +#include +#endif +#endif + +/* + * Add the -DSECUREC_SUPPORT_FORMAT_WARNING compiler option to support -Wformat. + * Default does not check the format is that the same data type in the actual code. + * In the product is different in the original data type definition of VxWorks and Linux. + */ +#ifndef SECUREC_SUPPORT_FORMAT_WARNING +#define SECUREC_SUPPORT_FORMAT_WARNING 0 +#endif + +/* SECUREC_PCLINT for tool do not recognize __attribute__ just for pclint */ +#if SECUREC_SUPPORT_FORMAT_WARNING && !defined(SECUREC_PCLINT) +#define SECUREC_ATTRIBUTE(x, y) __attribute__((format(printf, (x), (y)))) +#else +#define SECUREC_ATTRIBUTE(x, y) +#endif + +/* SECUREC_PCLINT for tool do not recognize __builtin_expect, just for pclint */ +#if defined(__GNUC__) && ((__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3))) && \ + !defined(SECUREC_PCLINT) +/* + * This is a built-in function that can be used without a declaration, if you encounter an + * undeclared compilation alarm, you can add -DSECUREC_NEED_BUILTIN_EXPECT_DECLARE to compiler + * options + */ +#ifdef SECUREC_NEED_BUILTIN_EXPECT_DECLARE +long __builtin_expect(long exp, long c); +#endif +#define SECUREC_LIKELY(x) __builtin_expect(!!(x), 1) +#define SECUREC_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +#define SECUREC_LIKELY(x) (x) +#define SECUREC_UNLIKELY(x) (x) +#endif + +/* Define the max length of the string */ +#ifndef SECUREC_STRING_MAX_LEN +#define SECUREC_STRING_MAX_LEN 0x7fffffffUL +#endif +#define SECUREC_WCHAR_STRING_MAX_LEN (SECUREC_STRING_MAX_LEN / sizeof(wchar_t)) + +/* Add SECUREC_MEM_MAX_LEN for memcpy and memmove */ +#ifndef SECUREC_MEM_MAX_LEN +#define SECUREC_MEM_MAX_LEN 0x7fffffffUL +#endif +#define SECUREC_WCHAR_MEM_MAX_LEN (SECUREC_MEM_MAX_LEN / sizeof(wchar_t)) + +#if SECUREC_STRING_MAX_LEN > 0x7fffffff +#error "max string is 2G" +#endif + +#if (defined(__GNUC__) && defined(__SIZEOF_POINTER__)) +#if (__SIZEOF_POINTER__ != 4) && (__SIZEOF_POINTER__ != 8) +#error "unsupported system" +#endif +#endif + +#if defined(_WIN64) || defined(WIN64) || defined(__LP64__) || defined(_LP64) +#define SECUREC_ON_64BITS +#endif + +#if (!defined(SECUREC_ON_64BITS) && defined(__GNUC__) && defined(__SIZEOF_POINTER__)) +#if __SIZEOF_POINTER__ == 8 +#define SECUREC_ON_64BITS +#endif +#endif + +#if defined(__SVR4) || defined(__svr4__) +#define SECUREC_ON_SOLARIS +#endif + +#if (defined(__hpux) || defined(_AIX) || defined(SECUREC_ON_SOLARIS)) +#define SECUREC_ON_UNIX +#endif + +/* + * Codes should run under the macro SECUREC_COMPATIBLE_LINUX_FORMAT in unknown system on default, + * and strtold. + * The function strtold is referenced first at ISO9899:1999(C99), and some old compilers can + * not support these functions. Here provides a macro to open these functions: + * SECUREC_SUPPORT_STRTOLD -- If defined, strtold will be used + */ +#ifndef SECUREC_SUPPORT_STRTOLD +#define SECUREC_SUPPORT_STRTOLD 0 +#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT)) +#if defined(__USE_ISOC99) || (defined(_AIX) && defined(_ISOC99_SOURCE)) || \ + (defined(__hpux) && defined(__ia64)) || \ + (defined(SECUREC_ON_SOLARIS) && (!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \ + defined(_STDC_C99) || defined(__EXTENSIONS__)) +#undef SECUREC_SUPPORT_STRTOLD +#define SECUREC_SUPPORT_STRTOLD 1 +#endif +#endif +#if ((defined(SECUREC_WRLINUX_BELOW4) || defined(_WRLINUX_BELOW4_))) +#undef SECUREC_SUPPORT_STRTOLD +#define SECUREC_SUPPORT_STRTOLD 0 +#endif +#endif + +#if SECUREC_WITH_PERFORMANCE_ADDONS + +#ifndef SECUREC_TWO_MIN +#define SECUREC_TWO_MIN(a, b) ((a) < (b) ? (a) : (b)) +#endif + +/* For strncpy_s performance optimization */ +#define SECUREC_STRNCPY_SM(dest, destMax, src, count) \ + (((void *)(dest) != NULL && (void *)(src) != NULL && (size_t)(destMax) > 0 && \ + (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ + (SECUREC_TWO_MIN((size_t)(count), strlen(src)) + 1) <= (size_t)(destMax)) \ + ? (((size_t)(count) < strlen(src)) \ + ? (memcpy((dest), (src), (count)), *((char *)(dest) + (count)) = '\0', EOK) \ + : (memcpy((dest), (src), strlen(src) + 1), EOK)) \ + : (strncpy_error((dest), (destMax), (src), (count)))) + +#define SECUREC_STRCPY_SM(dest, destMax, src) \ + (((void *)(dest) != NULL && (void *)(src) != NULL && (size_t)(destMax) > 0 && \ + (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ + (strlen(src) + 1) <= (size_t)(destMax)) \ + ? (memcpy((dest), (src), strlen(src) + 1), EOK) \ + : (strcpy_error((dest), (destMax), (src)))) + +/* For strcat_s performance optimization */ +#if defined(__GNUC__) +#define SECUREC_STRCAT_SM(dest, destMax, src) \ + ({ \ + int catRet = EOK; \ + if ((void *)(dest) != NULL && (void *)(src) != NULL && (size_t)(destMax) > 0 && \ + (((unsigned long long)(destMax) & (unsigned long long)(-2)) < \ + SECUREC_STRING_MAX_LEN)) { \ + char *catTmpDst = (char *)(dest); \ + size_t catRestSize = (destMax); \ + while (catRestSize > 0 && *catTmpDst != '\0') { \ + ++catTmpDst; \ + --catRestSize; \ + } \ + if (catRestSize == 0) { \ + catRet = EINVAL; \ + } else if ((strlen(src) + 1) <= catRestSize) { \ + memcpy(catTmpDst, (src), strlen(src) + 1); \ + catRet = EOK; \ + } else { \ + catRet = ERANGE; \ + } \ + if (catRet != EOK) { catRet = strcat_s((dest), (destMax), (src)); } \ + } else { \ + catRet = strcat_s((dest), (destMax), (src)); \ + } \ + catRet; \ + }) +#else +#define SECUREC_STRCAT_SM(dest, destMax, src) strcat_s((dest), (destMax), (src)) +#endif + +/* For strncat_s performance optimization */ +#if defined(__GNUC__) +#define SECUREC_STRNCAT_SM(dest, destMax, src, count) \ + ({ \ + int ncatRet = EOK; \ + if ((void *)(dest) != NULL && (void *)(src) != NULL && (size_t)(destMax) > 0 && \ + (((unsigned long long)(destMax) & (unsigned long long)(-2)) < \ + SECUREC_STRING_MAX_LEN) && \ + (((unsigned long long)(count) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN)) { \ + char *ncatTmpDest = (char *)(dest); \ + size_t ncatRestSize = (size_t)(destMax); \ + while (ncatRestSize > 0 && *ncatTmpDest != '\0') { \ + ++ncatTmpDest; \ + --ncatRestSize; \ + } \ + if (ncatRestSize == 0) { \ + ncatRet = EINVAL; \ + } else if ((SECUREC_TWO_MIN((count), strlen(src)) + 1) <= ncatRestSize) { \ + if ((size_t)(count) < strlen(src)) { \ + memcpy(ncatTmpDest, (src), (count)); \ + *(ncatTmpDest + (count)) = '\0'; \ + } else { \ + memcpy(ncatTmpDest, (src), strlen(src) + 1); \ + } \ + } else { \ + ncatRet = ERANGE; \ + } \ + if (ncatRet != EOK) { ncatRet = strncat_s((dest), (destMax), (src), (count)); } \ + } else { \ + ncatRet = strncat_s((dest), (destMax), (src), (count)); \ + } \ + ncatRet; \ + }) +#else +#define SECUREC_STRNCAT_SM(dest, destMax, src, count) strncat_s((dest), (destMax), (src), (count)) +#endif + +/* This macro do not check buffer overlap by default */ +#define SECUREC_MEMCPY_SM(dest, destMax, src, count) \ + (!(((size_t)(destMax) == 0) || \ + (((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \ + ((size_t)(count) > (size_t)(destMax)) || ((void *)(dest)) == NULL || \ + ((void *)(src) == NULL)) \ + ? (memcpy((dest), (src), (count)), EOK) \ + : (memcpy_s((dest), (destMax), (src), (count)))) + +#define SECUREC_MEMSET_SM(dest, destMax, c, count) \ + (!((((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \ + ((void *)(dest) == NULL) || ((size_t)(count) > (size_t)(destMax))) \ + ? (memset((dest), (c), (count)), EOK) \ + : (memset_s((dest), (destMax), (c), (count)))) + +#endif +#endif /* __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 */ \ No newline at end of file diff --git a/ucm/store/compress/cc/compress_lib/tunstall.cc b/ucm/store/compress/cc/compress_lib/tunstall.cc index c0b67d520..b68b624f0 100644 --- a/ucm/store/compress/cc/compress_lib/tunstall.cc +++ b/ucm/store/compress/cc/compress_lib/tunstall.cc @@ -1,12 +1,14 @@ -#include // from limits.h import UINT32_MAX -#include // from math.h import log -#include // from stdlib.h import qsort -#include // from string.h import memset, memcpy +#include // from climits import UINT32_MAX +#include // from cmath import log +#include // from cstdlib import qsort +#include // from cstring import memset, memcpy +#include "securec.h" #if TS_DEBUG_PRINT #include #endif +#include "securec_check.h" #include "tunstall.h" #define TS_V2_EXPAND \ @@ -18,16 +20,16 @@ #define TS_DEBUG_PRINT 0 #define RET_ERROR_IF(err_code, condition) \ - { \ + do { \ int c = (condition); \ int e = (err_code); \ if (c) { return e; } \ - } + } while (0) #define RET_WHEN_ERROR(err_code) \ - { \ + do { \ int e = (err_code); \ if (e) { return e; } \ - } + } while (0) // 获取 header 占用的字节数 static size_t ts_get_header_length(const ts_header_t* p_hdr) @@ -57,22 +59,25 @@ static int ts_do_not_compress(uint8_t* p_dst, size_t* p_dst_len, const uint8_t* p_hdr->mode = TS_MODE_UNCOMPRESS; // 调整非压缩模式 p_hdr->uncompress.length = src_len; p_dst = p_dst_base + ts_get_header_length(p_hdr); - memcpy(p_dst, p_src, src_len); + SECUREC_CHECK(memcpy_s(p_dst, src_len, p_src, src_len)); *p_dst_len = (p_dst + src_len) - p_dst_base; // dst大小 return R_TS_OK; } // 建立 idx -> symb 的映射表 (idx2symb) // 建立 symb -> idx 的映射表 (symb2idx) -static void ts_build_idx2symb_symb2idx(uint8_t* p_idx2symb, uint8_t* p_symb2idx, - const ts_hist_t* p_hist) +static int ts_build_idx2symb_symb2idx(uint8_t* p_idx2symb, size_t idx2symb_len, uint8_t* p_symb2idx, + size_t symb2idx_len, const ts_hist_t* p_hist) { + RET_ERROR_IF(R_ERR_DST_OVERFLOW, idx2symb_len < (size_t)p_hist->n_symb); + RET_ERROR_IF(R_ERR_DST_OVERFLOW, symb2idx_len < TS_N_SYMB); for (int idx = 0; idx < p_hist->n_symb; idx++) { // 这里 symb 循环变量类型要取 int 而不uint8_t ,避免死循环 uint8_t symb = p_hist->hist[idx].symb; p_idx2symb[idx] = symb; p_symb2idx[symb] = idx; } + return R_TS_OK; } // 功能: 使用函数构建预设表的归一化概率分布表 (PDF) ,也即ts_predef_lut_t->probs @@ -131,7 +136,7 @@ static int ts_cmp_hist(const void* p_a, const void* p_b) // 功能:统计输入数据中各符号的出现频率,构建直方图(也即概率表)同时检查符号值是否在有效范围 static int ts_get_hist_from_array(ts_hist_t* p_hist, const uint8_t* p_src, size_t src_len) { - memset(p_hist, 0, sizeof(ts_hist_t)); + SECUREC_CHECK(memset_s(p_hist, sizeof(ts_hist_t), 0, sizeof(ts_hist_t))); for (size_t i = 0; i < src_len; i++) { // 循环:遍历输入数组,统计频率freq uint8_t symb = p_src[i]; RET_ERROR_IF(R_ERR_SYMB_RANGE, symb >= TS_N_SYMB); // 检查符号是否超出范围 @@ -196,15 +201,15 @@ static uint32_t ts_search_largest_item(const ts_lut_item_t* p_lut, const double* } // 功能: 检查LUT -static int ts_check_lut(const ts_lut_item_t* p_lut) +static int ts_check_lut(const ts_lut_item_t* p_lut, size_t lut_size) { uint32_t mark = 0; - for (; mark < TS_LUT_SIZE; mark++) { + for (; mark < lut_size; mark++) { uint8_t cnt = p_lut[mark].c; RET_ERROR_IF(R_ERR_LUT_CHECK, cnt > TS_ITEM_SIZE); // 无效项检 if (cnt == 0) { break; } } - for (; mark < TS_LUT_SIZE; mark++) { + for (; mark < lut_size; mark++) { uint8_t cnt = p_lut[mark].c; RET_ERROR_IF(R_ERR_LUT_CHECK, cnt != 0); } @@ -226,20 +231,21 @@ static uint16_t ts_goto_state_by_lut_item(const ts_enc_state_item_t* p_enc_table // 功能: tunstall LUT 展开算法 // 每次LUT 中删除概率最大的 item, 并遍历所有非symb , 给这里item 末尾拼上这个 symb // 作为最新的一item 加入 LUT 顺便也会建立状态转移树p_enc_table , 用来压缩 -static int ts_build_lut(ts_lut_item_t* p_lut, - ts_enc_state_item_t* p_enc_table, // 建立状态转移树,用来进tunstall 压缩 - const ts_hist_t* p_hist) +static int ts_build_lut(ts_lut_item_t* p_lut, size_t max_lut_size, ts_enc_state_item_t* p_enc_table, + size_t enc_table_size, const ts_hist_t* p_hist) { + (void)max_lut_size; + (void)enc_table_size; int n_symb = p_hist->n_symb; double lut_prob[TS_LUT_SIZE] = {1.0}; // 数组:记录各 item 的条件概率。其init_mark 的概= 1.0 uint8_t lut_black_list[TS_LUT_SIZE] = { 0}; // 数组:黑名单,上了黑名单的LUT表项不能被展开,初始化全都不在黑名 - memset(p_lut, 0, TS_LUT_BYTES); // 清空 LUT - uint32_t lut_size = 1; // 初始只有一item,其 mark = 0 (init_mark) + SECUREC_CHECK(memset_s(p_lut, TS_LUT_BYTES, 0, TS_LUT_BYTES)); // 清空 LUT + uint32_t lut_size = 1; // 初始只有一item,其 mark = 0 (init_mark) - memset(p_enc_table, 0, TS_ENC_STATE_TABLE_BYTES); + SECUREC_CHECK(memset_s(p_enc_table, TS_ENC_STATE_TABLE_BYTES, 0, TS_ENC_STATE_TABLE_BYTES)); uint16_t new_state = 0; // 当前最新的state, 最开始只有一个状 p_enc_table[0].mark = 0; // init_mark = 0 @@ -333,10 +339,11 @@ int TunstallInitAllPredefTables(TunstallPredefTables_t* p_predef_luts) for (int lambda = 0; lambda < TS_COUNT_PREDEF; lambda++) { RET_WHEN_ERROR( ts_init_predef_probs(&(p_predef_luts->hist[lambda]), TS_N_SYMB_PREDEF, lambda)); - RET_WHEN_ERROR(ts_build_lut(p_predef_luts->lut[lambda], p_predef_luts->etree[lambda], + RET_WHEN_ERROR(ts_build_lut(p_predef_luts->lut[lambda], TS_LUT_SIZE, + p_predef_luts->etree[lambda], TS_ENC_STATE_TABLE_SIZE, &(p_predef_luts->hist[lambda]))); - RET_WHEN_ERROR( - ts_check_lut(p_predef_luts->lut[lambda])); // 检查刚建立LUT 表是否符合一些基本要 + RET_WHEN_ERROR(ts_check_lut(p_predef_luts->lut[lambda], + TS_LUT_SIZE)); // 检查刚建立LUT 表是否符合一些基本要 } p_predef_luts->initialized = 1; return R_TS_OK; @@ -485,9 +492,9 @@ int TunstallCompressDynamic(uint8_t* p_dst, size_t* p_dst_len, const uint8_t* p_ ts_enc_state_item_t etree[TS_ENC_STATE_TABLE_SIZE]; // TODO : TS_N_SYMB 太大小 etree 占空间太大,栈放不下 - RET_WHEN_ERROR( - ts_build_lut(p_hdr->dynamic.lut, etree, &hist)); // 建立 LUT, 直接建立p_hdr->dynamic.lut - RET_WHEN_ERROR(ts_check_lut(p_hdr->dynamic.lut)); // 检LUT 表是否符合一些基本要 + RET_WHEN_ERROR(ts_build_lut(p_hdr->dynamic.lut, TS_LUT_SIZE, etree, TS_ENC_STATE_TABLE_SIZE, + &hist)); // 建立 LUT, 直接建立p_hdr->dynamic.lut + RET_WHEN_ERROR(ts_check_lut(p_hdr->dynamic.lut, TS_LUT_SIZE)); // 检LUT 表是否符合一些基本要 p_hdr->mode = TS_MODE_DYNAMIC; p_hdr->dynamic.src_len = static_cast(src_len); // 标记为动态表模式,让解压器能识别模式 @@ -532,7 +539,8 @@ int TunstallCompressPredef(uint8_t* p_dst, size_t* p_dst_len, const uint8_t* p_s p_hdr->n_symb = hist.n_symb; p_hdr->src_len = static_cast(src_len); uint8_t symb2idx[TS_N_SYMB]; - ts_build_idx2symb_symb2idx(p_hdr->idx2symb, symb2idx, &hist); // 建立 idx2symb/symb2idx 映射 + RET_WHEN_ERROR( + ts_build_idx2symb_symb2idx(p_hdr->idx2symb, TS_N_SYMB_PREDEF, symb2idx, TS_N_SYMB, &hist)); #if TS_DEBUG_PRINT // ts_print_hist(&hist); // 打印符号统计 @@ -590,7 +598,7 @@ int TunstallDecompress(uint8_t* p_dst, size_t* p_dst_len, const uint8_t* p_src, if (p_hdr->mode == TS_MODE_UNCOMPRESS) { p_src += ts_get_header_length(p_hdr); - memcpy(p_dst, p_src, p_hdr->uncompress.length); + SECUREC_CHECK(memcpy_s(p_dst, p_hdr->uncompress.length, p_src, p_hdr->uncompress.length)); *p_dst_len = p_hdr->uncompress.length; } else { @@ -601,7 +609,7 @@ int TunstallDecompress(uint8_t* p_dst, size_t* p_dst_len, const uint8_t* p_src, if (p_hdr->mode == TS_MODE_DYNAMIC) { p_lut = p_hdr->dynamic.lut; - RET_WHEN_ERROR(ts_check_lut(p_lut)); + RET_WHEN_ERROR(ts_check_lut(p_lut, TS_LUT_SIZE)); n_mark = p_hdr->dynamic.n_mark; src_len = p_hdr->dynamic.src_len; } else if (TS_MODE_PREDEF_START <= p_hdr->mode && p_hdr->mode < TS_MODE_PREDEF_END) { @@ -627,10 +635,12 @@ int TunstallDecompress(uint8_t* p_dst, size_t* p_dst_len, const uint8_t* p_src, uint32_t mark2 = (mark21 >> TS_MARK_BITS) & ((1 << TS_MARK_BITS) - 1); p_src += (TS_MARK_BITS * 2 / 8); - memcpy(p_dst, p_lut[mark1].v, sizeof(ts_lut_item_t)); + SECUREC_CHECK( + memcpy_s(p_dst, sizeof(ts_lut_item_t), p_lut[mark1].v, sizeof(ts_lut_item_t))); p_dst += p_lut[mark1].c; - memcpy(p_dst, p_lut[mark2].v, sizeof(ts_lut_item_t)); + SECUREC_CHECK( + memcpy_s(p_dst, sizeof(ts_lut_item_t), p_lut[mark2].v, sizeof(ts_lut_item_t))); p_dst += p_lut[mark2].c; } diff --git a/ucm/store/compress/cc/compress_lib/tunstall.h b/ucm/store/compress/cc/compress_lib/tunstall.h index 7722ae7c0..7247f322d 100644 --- a/ucm/store/compress/cc/compress_lib/tunstall.h +++ b/ucm/store/compress/cc/compress_lib/tunstall.h @@ -1,8 +1,8 @@ #ifndef __TUNSTALL_H__ #define __TUNSTALL_H__ -#include -#include +#include +#include //--------------------------------------------------------------------------------------------------------------------------------------------------------- // 函数返回(错误 @@ -20,6 +20,7 @@ #define R_ERR_NORMALIZE 10 #define R_ERR_PREDEF_UNINIT 11 // 预设表尚未初始化 #define R_ERR_LARGER 12 +#define R_ERR_COPY_FAILED 13 //--------------------------------------------------------------------------------------------------------------------------------------------------------- // tunstall 算法超参数 diff --git a/ucm/store/compress/cc/compress_lib/tunstall_bf16.cc b/ucm/store/compress/cc/compress_lib/tunstall_bf16.cc index 46fa392c1..fadf765ea 100644 --- a/ucm/store/compress/cc/compress_lib/tunstall_bf16.cc +++ b/ucm/store/compress/cc/compress_lib/tunstall_bf16.cc @@ -1,7 +1,8 @@ #include "tunstall_bf16.h" // TunstallCompressDynamic -#include -#include -#include +#include +#include +#include +#include "securec_check.h" #define ENABLE_NEON_DECOMPPRESSION 1 #define ENABLE_EXTRA_DECOMPRESSION 1 @@ -14,11 +15,11 @@ #endif #define RET_ERROR_IF(err_code, condition) \ - { \ + do { \ int c = (condition); \ int e = (err_code); \ if (c) { return e; } \ - } + } while (0) #define BF16_EXP_MIN (128 - (1 << 4)) #define BF16_EXP_MAX (128 + (1 << 4) - 1) @@ -68,7 +69,7 @@ static void decompress_fp8_to_bf16(uint16_t* p_dst, const uint8_t* p_src, size_t } } -static void decompress_fp8_to_bf16_inplace(uint8_t* p_data, size_t n_bf16) +static int decompress_fp8_to_bf16_inplace(uint8_t* p_data, size_t n_bf16) { #if !defined(NEON_DECOMPRESSION) for (size_t i = n_bf16; i > 0; i--) { @@ -78,8 +79,9 @@ static void decompress_fp8_to_bf16_inplace(uint8_t* p_data, size_t n_bf16) uint16_t sign = static_cast((fp8 & 0x04) << 13); uint16_t mant2 = static_cast((fp8 & 0x03) << 5); uint16_t bf16 = sign | exp8 | mant2 | 0x10; - memcpy(p_data + ((i - 1) << 1), &bf16, sizeof(bf16)); + SECUREC_CHECK(memcpy_s(p_data + ((i - 1) << 1), sizeof(bf16), &bf16, sizeof(bf16))); } + return R_TS_OK; #else uint16_t* dst = (uint16_t*)p_data; const uint8_t* src = (const uint8_t*)p_data; @@ -147,6 +149,7 @@ static void decompress_fp8_to_bf16_inplace(uint8_t* p_data, size_t n_bf16) vst1q_u16(dst + i, res_low); vst1q_u16(dst + i + 8, res_high); } + return R_TS_OK; #endif } @@ -245,7 +248,8 @@ static int decompress_tunstall_trunc(uint16_t* p_dst, const uint8_t* p_src, size while (p_exp_write < p_exp_end) { const ts_lut_item_t* it = &(p_hdr->dynamic.lut[p_mark[0]]); p_mark++; - memcpy(p_exp_write, it->v, sizeof(ts_lut_item_t)); + SECUREC_CHECK( + memcpy_s(p_exp_write, sizeof(ts_lut_item_t), it->v, sizeof(ts_lut_item_t))); p_exp_write += it->c; } @@ -349,7 +353,7 @@ static int decompress_tunstall_trunc(uint16_t* p_dst, const uint8_t* p_src, size p_extra += 4; } - memcpy(buf_exp, (buf_exp + BLOCK_SIZE), BLOCK_TAIL); + SECUREC_CHECK(memcpy_s(buf_exp, BLOCK_TAIL, (buf_exp + BLOCK_SIZE), BLOCK_TAIL)); p_exp_write -= BLOCK_SIZE; } @@ -377,8 +381,8 @@ static int compact_tunstall_inplace_streams(uint8_t* p_stream_end, uint8_t** pp_ uint8_t* p_extra_new = p_sm_new - extra_len; uint8_t* p_mark_new = p_extra_new - mark_len; - if (extra_len > 0) { memmove(p_extra_new, *pp_extra, extra_len); } - if (mark_len > 0) { memmove(p_mark_new, *pp_mark, mark_len); } + if (extra_len > 0) { SECUREC_CHECK(memmove_s(p_extra_new, extra_len, *pp_extra, extra_len)); } + if (mark_len > 0) { SECUREC_CHECK(memmove_s(p_mark_new, mark_len, *pp_mark, mark_len)); } *pp_mark = p_mark_new; *pp_mark_end = p_extra_new; @@ -409,7 +413,7 @@ static int spill_tunstall_inplace_streams(uint8_t* p_tail, size_t tail_size, uin RET_ERROR_IF(R_ERR_SYNTAX, tail_len > tail_size); - memcpy(p_tail, *pp_mark, tail_len); + SECUREC_CHECK(memcpy_s(p_tail, tail_len, *pp_mark, tail_len)); *pp_mark = p_tail; *pp_mark_end = p_tail + mark_end_off; @@ -430,9 +434,9 @@ static int decompress_tunstall_trunc_inplace(uint8_t* p_data, size_t n_bf16) size_t sm_total = n_bf16 / 2; uint8_t* p_tunstall_src = p_data + sm_total; - ts_header_t hdr; + ts_header_t hdr = {}; RET_ERROR_IF(R_ERR_SYNTAX, (n_bf16 / 2) < sizeof(hdr.dynamic)); - memcpy(&hdr.dynamic, p_tunstall_src, sizeof(hdr.dynamic)); + SECUREC_CHECK(memcpy_s(&hdr.dynamic, sizeof(hdr.dynamic), p_tunstall_src, sizeof(hdr.dynamic))); RET_ERROR_IF(R_ERR_SYNTAX, hdr.mode != TS_MODE_DYNAMIC); RET_ERROR_IF(R_ERR_SYNTAX, (hdr.dynamic.n_mark & 1) != 0); @@ -442,8 +446,8 @@ static int decompress_tunstall_trunc_inplace(uint8_t* p_data, size_t n_bf16) RET_ERROR_IF(R_ERR_SYNTAX, tunstall_len > sm_total); // Repack upper half as {tunstall, extra, sm}; compact then moves only tunstall/extra. - memcpy(p_src, p_tunstall_src, sm_total); - memcpy(p_src + sm_total, p_data, sm_total); + SECUREC_CHECK(memcpy_s(p_src, sm_total, p_tunstall_src, sm_total)); + SECUREC_CHECK(memcpy_s(p_src + sm_total, sm_total, p_data, sm_total)); uint8_t* p_mark = p_src + sizeof(hdr.dynamic); uint8_t* p_mark_end = p_mark + (size_t)hdr.dynamic.n_mark; @@ -476,7 +480,8 @@ static int decompress_tunstall_trunc_inplace(uint8_t* p_data, size_t n_bf16) while (p_exp_write < p_exp_end) { const ts_lut_item_t* it = &(hdr.dynamic.lut[p_mark[0]]); p_mark++; - memcpy(p_exp_write, it->v, sizeof(ts_lut_item_t)); + SECUREC_CHECK( + memcpy_s(p_exp_write, sizeof(ts_lut_item_t), it->v, sizeof(ts_lut_item_t))); p_exp_write += it->c; } @@ -493,12 +498,12 @@ static int decompress_tunstall_trunc_inplace(uint8_t* p_data, size_t n_bf16) uint8_t extra[4] = {0}; RET_ERROR_IF(R_ERR_SYNTAX, (size_t)(p_sm_end - p_sm) < sizeof(sm)); - memcpy(sm, p_sm, sizeof(sm)); + SECUREC_CHECK(memcpy_s(sm, sizeof(sm), p_sm, sizeof(sm))); p_sm += sizeof(sm); int has_extra = 0; if ((size_t)(p_extra_end - p_extra) >= sizeof(extra)) { - memcpy(extra, p_extra, sizeof(extra)); + SECUREC_CHECK(memcpy_s(extra, sizeof(extra), p_extra, sizeof(extra))); p_extra += sizeof(extra); has_extra = 1; } else { @@ -618,7 +623,7 @@ static int decompress_tunstall_trunc_inplace(uint8_t* p_data, size_t n_bf16) } if (actual_block_size == BLOCK_SIZE) { - memcpy(buf_exp, (buf_exp + BLOCK_SIZE), BLOCK_TAIL); + SECUREC_CHECK(memcpy_s(buf_exp, BLOCK_TAIL, (buf_exp + BLOCK_SIZE), BLOCK_TAIL)); p_exp_write -= BLOCK_SIZE; } } @@ -635,8 +640,7 @@ int TunstallDecompressBF16Inplace( RET_ERROR_IF(R_ERR_UNSUPPORT, n_bf16 == 0 || n_bf16 > 0xFFFFFFFFU); if ((n_bf16 % 32 != 0) || (p_data[n_bf16 / 2] != TS_MODE_DYNAMIC)) { - decompress_fp8_to_bf16_inplace(p_data, n_bf16); - return R_TS_OK; + return decompress_fp8_to_bf16_inplace(p_data, n_bf16); } else { return decompress_tunstall_trunc_inplace(p_data, n_bf16); } diff --git a/ucm/store/compress/cc/compress_lib/tunstall_bf16.h b/ucm/store/compress/cc/compress_lib/tunstall_bf16.h index cc00bd54d..3feb7da84 100644 --- a/ucm/store/compress/cc/compress_lib/tunstall_bf16.h +++ b/ucm/store/compress/cc/compress_lib/tunstall_bf16.h @@ -1,8 +1,8 @@ #ifndef __TUNSTALL_BF16_H__ #define __TUNSTALL_BF16_H__ -#include -#include +#include +#include #include "tunstall.h" // p_src 里有 n_bf16 个 BF16 diff --git a/ucm/store/compress/cc/compressor_action.cc b/ucm/store/compress/cc/compressor_action.cc index 1bbf73ae2..b7f4d0d54 100644 --- a/ucm/store/compress/cc/compressor_action.cc +++ b/ucm/store/compress/cc/compressor_action.cc @@ -81,6 +81,8 @@ void CompressorAction::Compress_Load(CompressTask& ct) } UC_DEBUG("COMPRESS LOAD | shard: {}, done, decompressed_size: {}", shards[i].index, shardSize_); + } else { + UC_ERROR("COMPRESS LOAD FAILED | shard: {}, unsupported ratio", shards[i].index); } } } @@ -112,11 +114,6 @@ void CompressorAction::Compress_Dump(CompressTask& ct) std::vector blockToFree; std::unique_ptr dump_memoryPool_ = std::make_unique(compBufSize, ct.task->desc.size()); - if (!dump_memoryPool_) { - UC_ERROR("COMPRESS DUMP OOM | task_id: {}, required: {} B", ct.task->id, - shardSize_ * desc.size()); - Status::NoSpace(); - } for (const UC::Detail::Shard& s : desc) { UC_DEBUG("COMPRESS DUMP | task_id: {}, shard: {}, compressing...", ct.task->id, @@ -126,14 +123,25 @@ void CompressorAction::Compress_Dump(CompressTask& ct) uint16_t* src = static_cast(s.addrs[0]); size_t compBytes = 0; + bool ok = true; if (ratio == R200) { size_t n_bf16 = shardSize_ >> 1; int err = TunstallCompressBF16(compBuf, src, n_bf16); if (err != 0) [[unlikely]] { UC_ERROR("COMPRESS DUMP FAILED | task_id: {}, shard: {}, error: {}", ct.task->id, s.index, err); + ok = false; } compBytes = n_bf16; + } else { + UC_ERROR("COMPRESS DUMP FAILED | task_id: {}, shard: {}, unsupported ratio", + ct.task->id, s.index); + ok = false; + } + + if (!ok) { + dump_memoryPool_->deallocate({compBuf}); + continue; } std::vector _addrs{static_cast(compBuf)}; diff --git a/ucm/store/compress/cc/compressor_action.h b/ucm/store/compress/cc/compressor_action.h index 8fcb5d0d6..fdbb8c267 100644 --- a/ucm/store/compress/cc/compressor_action.h +++ b/ucm/store/compress/cc/compressor_action.h @@ -3,7 +3,7 @@ #include #include -#include "compress_lib/huf.h" +#include "compress_lib/compress_types.h" #include "compress_lib/tunstall_bf16.h" #include "global_config.h" #include "memory_pool.h" diff --git a/ucm/store/compress/cc/memory_pool.h b/ucm/store/compress/cc/memory_pool.h index 22a7a8ce7..1b4668fbf 100644 --- a/ucm/store/compress/cc/memory_pool.h +++ b/ucm/store/compress/cc/memory_pool.h @@ -1,6 +1,11 @@ +#ifndef MEMORY_POOL_H +#define MEMORY_POOL_H + #include #include +#include #include +#include #include #include "logger/logger.h" @@ -20,12 +25,16 @@ class MemoryPool { this->blockSize = (blockSize + 4095) & ~static_cast(4095); size_t totalSize = this->blockSize * poolSize; - if (posix_memalign(&pool, 4096, totalSize) != 0) {} + void* rawPool = nullptr; + if (posix_memalign(&rawPool, 4096, totalSize) != 0) { throw std::bad_alloc(); } + std::unique_ptr poolGuard(rawPool, &free); freeBlocks.reserve(poolSize); for (size_t i = 0; i < poolSize; ++i) { - freeBlocks.push_back(static_cast(pool) + i * this->blockSize); + freeBlocks.push_back(static_cast(rawPool) + i * this->blockSize); } + + pool = poolGuard.release(); } ~MemoryPool() @@ -37,6 +46,7 @@ class MemoryPool { void* allocate() { std::lock_guard lock(mutex_); + if (freeBlocks.empty()) { throw std::bad_alloc(); } void* block = freeBlocks.back(); freeBlocks.pop_back(); return block; @@ -51,4 +61,6 @@ class MemoryPool { } }; -} // namespace UC::Compressor \ No newline at end of file +} // namespace UC::Compressor + +#endif // MEMORY_POOL_H \ No newline at end of file diff --git a/ucm/store/compress/cpy/compressor.py.cc b/ucm/store/compress/cpy/compressor.py.cc index e29b572e7..13ed26dc4 100644 --- a/ucm/store/compress/cpy/compressor.py.cc +++ b/ucm/store/compress/cpy/compressor.py.cc @@ -20,10 +20,6 @@ PYBIND11_MODULE(ucmcompressor, module) config.def_readwrite("shardSize", &Config::shardSize); config.def_readwrite("blockSize", &Config::blockSize); config.def_readwrite("streamNumber", &Config::streamNumber); - // config.def_readwrite("bufferSize", &Config::bufferSize); - // config.def_readwrite("shareBufferEnable", &Config::shareBufferEnable); - // config.def_readwrite("waitingQueueDepth", &Config::waitingQueueDepth); - // config.def_readwrite("runningQueueDepth", &Config::runningQueueDepth); config.def_readwrite("timeoutMs", &Config::timeoutMs); store.def(py::init<>()); store.def("Self", &CompressorPy::Self);