22static size_t showHexa(
const void* src,
size_t srcSize)
24 const BYTE*
const ip = (
const BYTE*)src;
26 for (u=0; u<srcSize; u++) {
27 RAWLOG(5,
" %02X", ip[u]); (void)ip;
42 U32 const flSize = 1 + (srcSize>31) + (srcSize>4095);
44 DEBUGLOG(5,
"ZSTD_noCompressLiterals: srcSize=%zu, dstCapacity=%zu", srcSize, dstCapacity);
64 DEBUGLOG(5,
"Raw (uncompressed) literals: %u -> %u", (
U32)srcSize, (
U32)(srcSize + flSize));
65 return srcSize + flSize;
68static int allBytesIdentical(
const void* src,
size_t srcSize)
72 {
const BYTE b = ((
const BYTE*)src)[0];
74 for (p=1; p<srcSize; p++) {
75 if (((
const BYTE*)src)[p] != b)
return 0;
84 U32 const flSize = 1 + (srcSize>31) + (srcSize>4095);
86 assert(dstCapacity >= 4); (void)dstCapacity;
87 assert(allBytesIdentical(src, srcSize));
104 ostart[flSize] = *(
const BYTE*)src;
105 DEBUGLOG(5,
"RLE : Repeated Literal (%02X: %u times) -> %u bytes encoded", ((
const BYTE*)src)[0], (
U32)srcSize, (
U32)flSize + 1);
117 assert((
int)strategy >= 0);
118 assert((
int)strategy <= 9);
122 {
int const shift =
MIN(9-(
int)strategy, 3);
123 size_t const mintc = (huf_repeat ==
HUF_repeat_valid) ? 6 : (
size_t)8 << shift;
124 DEBUGLOG(7,
"minLiteralsToCompress = %zu", mintc);
130 void* dst,
size_t dstCapacity,
131 const void* src,
size_t srcSize,
132 void* entropyWorkspace,
size_t entropyWorkspaceSize,
136 int disableLiteralCompression,
137 int suspectUncompressible,
140 size_t const lhSize = 3 + (srcSize >= 1
KB) + (srcSize >= 16
KB);
142 U32 singleStream = srcSize < 256;
146 DEBUGLOG(5,
"ZSTD_compressLiterals (disableLiteralCompression=%i, srcSize=%u, dstCapacity=%zu)",
147 disableLiteralCompression, (
U32)srcSize, dstCapacity);
149 DEBUGLOG(6,
"Completed literals listing (%zu bytes)", showHexa(src, srcSize));
154 if (disableLiteralCompression)
158 if (srcSize < ZSTD_minLiteralsToCompress(strategy, prevHuf->
repeatMode))
161 RETURN_ERROR_IF(dstCapacity < lhSize+1, dstSize_tooSmall,
"not enough space for compression");
169 typedef size_t (*huf_compress_f)(
void*, size_t,
const void*, size_t, unsigned, unsigned,
void*, size_t,
HUF_CElt*,
HUF_repeat*, int);
170 huf_compress_f huf_compress;
173 cLitSize = huf_compress(ostart+lhSize, dstCapacity-lhSize,
176 entropyWorkspace, entropyWorkspaceSize,
179 DEBUGLOG(5,
"%zu literals compressed into %zu bytes (before header)", srcSize, cLitSize);
182 DEBUGLOG(5,
"reusing statistics from previous huffman block");
187 {
size_t const minGain =
ZSTD_minGain(srcSize, strategy);
188 if ((cLitSize==0) || (cLitSize >= srcSize - minGain) ||
ERR_isError(cLitSize)) {
198 if ((srcSize >= 8) || allBytesIdentical(src, srcSize)) {
213 {
U32 const lhc = hType + ((
U32)(!singleStream) << 2) + ((
U32)srcSize<<4) + ((
U32)cLitSize<<14);
219 {
U32 const lhc = hType + (2 << 2) + ((
U32)srcSize<<4) + ((
U32)cLitSize<<18);
225 {
U32 const lhc = hType + (3 << 2) + ((
U32)srcSize<<4) + ((
U32)cLitSize<<22);
227 ostart[4] = (
BYTE)(cLitSize >> 10);
233 DEBUGLOG(5,
"Compressed literals: %u -> %u", (
U32)srcSize, (
U32)(lhSize+cLitSize));
234 return lhSize+cLitSize;
#define assert(condition)
ERR_STATIC unsigned ERR_isError(size_t code)
#define RETURN_ERROR_IF(cond, err,...)
#define HUF_SYMBOLVALUE_MAX
@ HUF_flags_suspectUncompressible
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, HUF_CElt *hufTable, HUF_repeat *repeat, int flags)
#define HUF_OPTIMAL_DEPTH_THRESHOLD
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, HUF_CElt *hufTable, HUF_repeat *repeat, int flags)
MEM_STATIC void MEM_writeLE16(void *memPtr, U16 val)
MEM_STATIC void MEM_writeLE32(void *memPtr, U32 val32)
MEM_STATIC void MEM_writeLE24(void *memPtr, U32 val)
HUF_CElt CTable[HUF_CTABLE_SIZE_ST(255)]
MEM_STATIC size_t ZSTD_minGain(size_t srcSize, ZSTD_strategy strat)
size_t ZSTD_noCompressLiterals(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
size_t ZSTD_compressRleLiteralsBlock(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
size_t ZSTD_compressLiterals(void *dst, size_t dstCapacity, const void *src, size_t srcSize, void *entropyWorkspace, size_t entropyWorkspaceSize, const ZSTD_hufCTables_t *prevHuf, ZSTD_hufCTables_t *nextHuf, ZSTD_strategy strategy, int disableLiteralCompression, int suspectUncompressible, int bmi2)
#define ZSTD_memcpy(d, s, l)
#define MIN_LITERALS_FOR_4_STREAMS