1*01826a49SYabin Cui /*
2*01826a49SYabin Cui * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui * All rights reserved.
4*01826a49SYabin Cui *
5*01826a49SYabin Cui * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui */
10*01826a49SYabin Cui
11*01826a49SYabin Cui /*-*************************************
12*01826a49SYabin Cui * Dependencies
13*01826a49SYabin Cui ***************************************/
14*01826a49SYabin Cui #include "zstd_compress_literals.h"
15*01826a49SYabin Cui
16*01826a49SYabin Cui
17*01826a49SYabin Cui /* **************************************************************
18*01826a49SYabin Cui * Debug Traces
19*01826a49SYabin Cui ****************************************************************/
20*01826a49SYabin Cui #if DEBUGLEVEL >= 2
21*01826a49SYabin Cui
showHexa(const void * src,size_t srcSize)22*01826a49SYabin Cui static size_t showHexa(const void* src, size_t srcSize)
23*01826a49SYabin Cui {
24*01826a49SYabin Cui const BYTE* const ip = (const BYTE*)src;
25*01826a49SYabin Cui size_t u;
26*01826a49SYabin Cui for (u=0; u<srcSize; u++) {
27*01826a49SYabin Cui RAWLOG(5, " %02X", ip[u]); (void)ip;
28*01826a49SYabin Cui }
29*01826a49SYabin Cui RAWLOG(5, " \n");
30*01826a49SYabin Cui return srcSize;
31*01826a49SYabin Cui }
32*01826a49SYabin Cui
33*01826a49SYabin Cui #endif
34*01826a49SYabin Cui
35*01826a49SYabin Cui
36*01826a49SYabin Cui /* **************************************************************
37*01826a49SYabin Cui * Literals compression - special cases
38*01826a49SYabin Cui ****************************************************************/
ZSTD_noCompressLiterals(void * dst,size_t dstCapacity,const void * src,size_t srcSize)39*01826a49SYabin Cui size_t ZSTD_noCompressLiterals (void* dst, size_t dstCapacity, const void* src, size_t srcSize)
40*01826a49SYabin Cui {
41*01826a49SYabin Cui BYTE* const ostart = (BYTE*)dst;
42*01826a49SYabin Cui U32 const flSize = 1 + (srcSize>31) + (srcSize>4095);
43*01826a49SYabin Cui
44*01826a49SYabin Cui DEBUGLOG(5, "ZSTD_noCompressLiterals: srcSize=%zu, dstCapacity=%zu", srcSize, dstCapacity);
45*01826a49SYabin Cui
46*01826a49SYabin Cui RETURN_ERROR_IF(srcSize + flSize > dstCapacity, dstSize_tooSmall, "");
47*01826a49SYabin Cui
48*01826a49SYabin Cui switch(flSize)
49*01826a49SYabin Cui {
50*01826a49SYabin Cui case 1: /* 2 - 1 - 5 */
51*01826a49SYabin Cui ostart[0] = (BYTE)((U32)set_basic + (srcSize<<3));
52*01826a49SYabin Cui break;
53*01826a49SYabin Cui case 2: /* 2 - 2 - 12 */
54*01826a49SYabin Cui MEM_writeLE16(ostart, (U16)((U32)set_basic + (1<<2) + (srcSize<<4)));
55*01826a49SYabin Cui break;
56*01826a49SYabin Cui case 3: /* 2 - 2 - 20 */
57*01826a49SYabin Cui MEM_writeLE32(ostart, (U32)((U32)set_basic + (3<<2) + (srcSize<<4)));
58*01826a49SYabin Cui break;
59*01826a49SYabin Cui default: /* not necessary : flSize is {1,2,3} */
60*01826a49SYabin Cui assert(0);
61*01826a49SYabin Cui }
62*01826a49SYabin Cui
63*01826a49SYabin Cui ZSTD_memcpy(ostart + flSize, src, srcSize);
64*01826a49SYabin Cui DEBUGLOG(5, "Raw (uncompressed) literals: %u -> %u", (U32)srcSize, (U32)(srcSize + flSize));
65*01826a49SYabin Cui return srcSize + flSize;
66*01826a49SYabin Cui }
67*01826a49SYabin Cui
allBytesIdentical(const void * src,size_t srcSize)68*01826a49SYabin Cui static int allBytesIdentical(const void* src, size_t srcSize)
69*01826a49SYabin Cui {
70*01826a49SYabin Cui assert(srcSize >= 1);
71*01826a49SYabin Cui assert(src != NULL);
72*01826a49SYabin Cui { const BYTE b = ((const BYTE*)src)[0];
73*01826a49SYabin Cui size_t p;
74*01826a49SYabin Cui for (p=1; p<srcSize; p++) {
75*01826a49SYabin Cui if (((const BYTE*)src)[p] != b) return 0;
76*01826a49SYabin Cui }
77*01826a49SYabin Cui return 1;
78*01826a49SYabin Cui }
79*01826a49SYabin Cui }
80*01826a49SYabin Cui
ZSTD_compressRleLiteralsBlock(void * dst,size_t dstCapacity,const void * src,size_t srcSize)81*01826a49SYabin Cui size_t ZSTD_compressRleLiteralsBlock (void* dst, size_t dstCapacity, const void* src, size_t srcSize)
82*01826a49SYabin Cui {
83*01826a49SYabin Cui BYTE* const ostart = (BYTE*)dst;
84*01826a49SYabin Cui U32 const flSize = 1 + (srcSize>31) + (srcSize>4095);
85*01826a49SYabin Cui
86*01826a49SYabin Cui assert(dstCapacity >= 4); (void)dstCapacity;
87*01826a49SYabin Cui assert(allBytesIdentical(src, srcSize));
88*01826a49SYabin Cui
89*01826a49SYabin Cui switch(flSize)
90*01826a49SYabin Cui {
91*01826a49SYabin Cui case 1: /* 2 - 1 - 5 */
92*01826a49SYabin Cui ostart[0] = (BYTE)((U32)set_rle + (srcSize<<3));
93*01826a49SYabin Cui break;
94*01826a49SYabin Cui case 2: /* 2 - 2 - 12 */
95*01826a49SYabin Cui MEM_writeLE16(ostart, (U16)((U32)set_rle + (1<<2) + (srcSize<<4)));
96*01826a49SYabin Cui break;
97*01826a49SYabin Cui case 3: /* 2 - 2 - 20 */
98*01826a49SYabin Cui MEM_writeLE32(ostart, (U32)((U32)set_rle + (3<<2) + (srcSize<<4)));
99*01826a49SYabin Cui break;
100*01826a49SYabin Cui default: /* not necessary : flSize is {1,2,3} */
101*01826a49SYabin Cui assert(0);
102*01826a49SYabin Cui }
103*01826a49SYabin Cui
104*01826a49SYabin Cui ostart[flSize] = *(const BYTE*)src;
105*01826a49SYabin Cui DEBUGLOG(5, "RLE : Repeated Literal (%02X: %u times) -> %u bytes encoded", ((const BYTE*)src)[0], (U32)srcSize, (U32)flSize + 1);
106*01826a49SYabin Cui return flSize+1;
107*01826a49SYabin Cui }
108*01826a49SYabin Cui
109*01826a49SYabin Cui /* ZSTD_minLiteralsToCompress() :
110*01826a49SYabin Cui * returns minimal amount of literals
111*01826a49SYabin Cui * for literal compression to even be attempted.
112*01826a49SYabin Cui * Minimum is made tighter as compression strategy increases.
113*01826a49SYabin Cui */
114*01826a49SYabin Cui static size_t
ZSTD_minLiteralsToCompress(ZSTD_strategy strategy,HUF_repeat huf_repeat)115*01826a49SYabin Cui ZSTD_minLiteralsToCompress(ZSTD_strategy strategy, HUF_repeat huf_repeat)
116*01826a49SYabin Cui {
117*01826a49SYabin Cui assert((int)strategy >= 0);
118*01826a49SYabin Cui assert((int)strategy <= 9);
119*01826a49SYabin Cui /* btultra2 : min 8 bytes;
120*01826a49SYabin Cui * then 2x larger for each successive compression strategy
121*01826a49SYabin Cui * max threshold 64 bytes */
122*01826a49SYabin Cui { int const shift = MIN(9-(int)strategy, 3);
123*01826a49SYabin Cui size_t const mintc = (huf_repeat == HUF_repeat_valid) ? 6 : (size_t)8 << shift;
124*01826a49SYabin Cui DEBUGLOG(7, "minLiteralsToCompress = %zu", mintc);
125*01826a49SYabin Cui return mintc;
126*01826a49SYabin Cui }
127*01826a49SYabin Cui }
128*01826a49SYabin Cui
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)129*01826a49SYabin Cui size_t ZSTD_compressLiterals (
130*01826a49SYabin Cui void* dst, size_t dstCapacity,
131*01826a49SYabin Cui const void* src, size_t srcSize,
132*01826a49SYabin Cui void* entropyWorkspace, size_t entropyWorkspaceSize,
133*01826a49SYabin Cui const ZSTD_hufCTables_t* prevHuf,
134*01826a49SYabin Cui ZSTD_hufCTables_t* nextHuf,
135*01826a49SYabin Cui ZSTD_strategy strategy,
136*01826a49SYabin Cui int disableLiteralCompression,
137*01826a49SYabin Cui int suspectUncompressible,
138*01826a49SYabin Cui int bmi2)
139*01826a49SYabin Cui {
140*01826a49SYabin Cui size_t const lhSize = 3 + (srcSize >= 1 KB) + (srcSize >= 16 KB);
141*01826a49SYabin Cui BYTE* const ostart = (BYTE*)dst;
142*01826a49SYabin Cui U32 singleStream = srcSize < 256;
143*01826a49SYabin Cui symbolEncodingType_e hType = set_compressed;
144*01826a49SYabin Cui size_t cLitSize;
145*01826a49SYabin Cui
146*01826a49SYabin Cui DEBUGLOG(5,"ZSTD_compressLiterals (disableLiteralCompression=%i, srcSize=%u, dstCapacity=%zu)",
147*01826a49SYabin Cui disableLiteralCompression, (U32)srcSize, dstCapacity);
148*01826a49SYabin Cui
149*01826a49SYabin Cui DEBUGLOG(6, "Completed literals listing (%zu bytes)", showHexa(src, srcSize));
150*01826a49SYabin Cui
151*01826a49SYabin Cui /* Prepare nextEntropy assuming reusing the existing table */
152*01826a49SYabin Cui ZSTD_memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
153*01826a49SYabin Cui
154*01826a49SYabin Cui if (disableLiteralCompression)
155*01826a49SYabin Cui return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
156*01826a49SYabin Cui
157*01826a49SYabin Cui /* if too small, don't even attempt compression (speed opt) */
158*01826a49SYabin Cui if (srcSize < ZSTD_minLiteralsToCompress(strategy, prevHuf->repeatMode))
159*01826a49SYabin Cui return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
160*01826a49SYabin Cui
161*01826a49SYabin Cui RETURN_ERROR_IF(dstCapacity < lhSize+1, dstSize_tooSmall, "not enough space for compression");
162*01826a49SYabin Cui { HUF_repeat repeat = prevHuf->repeatMode;
163*01826a49SYabin Cui int const flags = 0
164*01826a49SYabin Cui | (bmi2 ? HUF_flags_bmi2 : 0)
165*01826a49SYabin Cui | (strategy < ZSTD_lazy && srcSize <= 1024 ? HUF_flags_preferRepeat : 0)
166*01826a49SYabin Cui | (strategy >= HUF_OPTIMAL_DEPTH_THRESHOLD ? HUF_flags_optimalDepth : 0)
167*01826a49SYabin Cui | (suspectUncompressible ? HUF_flags_suspectUncompressible : 0);
168*01826a49SYabin Cui
169*01826a49SYabin Cui typedef size_t (*huf_compress_f)(void*, size_t, const void*, size_t, unsigned, unsigned, void*, size_t, HUF_CElt*, HUF_repeat*, int);
170*01826a49SYabin Cui huf_compress_f huf_compress;
171*01826a49SYabin Cui if (repeat == HUF_repeat_valid && lhSize == 3) singleStream = 1;
172*01826a49SYabin Cui huf_compress = singleStream ? HUF_compress1X_repeat : HUF_compress4X_repeat;
173*01826a49SYabin Cui cLitSize = huf_compress(ostart+lhSize, dstCapacity-lhSize,
174*01826a49SYabin Cui src, srcSize,
175*01826a49SYabin Cui HUF_SYMBOLVALUE_MAX, LitHufLog,
176*01826a49SYabin Cui entropyWorkspace, entropyWorkspaceSize,
177*01826a49SYabin Cui (HUF_CElt*)nextHuf->CTable,
178*01826a49SYabin Cui &repeat, flags);
179*01826a49SYabin Cui DEBUGLOG(5, "%zu literals compressed into %zu bytes (before header)", srcSize, cLitSize);
180*01826a49SYabin Cui if (repeat != HUF_repeat_none) {
181*01826a49SYabin Cui /* reused the existing table */
182*01826a49SYabin Cui DEBUGLOG(5, "reusing statistics from previous huffman block");
183*01826a49SYabin Cui hType = set_repeat;
184*01826a49SYabin Cui }
185*01826a49SYabin Cui }
186*01826a49SYabin Cui
187*01826a49SYabin Cui { size_t const minGain = ZSTD_minGain(srcSize, strategy);
188*01826a49SYabin Cui if ((cLitSize==0) || (cLitSize >= srcSize - minGain) || ERR_isError(cLitSize)) {
189*01826a49SYabin Cui ZSTD_memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
190*01826a49SYabin Cui return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
191*01826a49SYabin Cui } }
192*01826a49SYabin Cui if (cLitSize==1) {
193*01826a49SYabin Cui /* A return value of 1 signals that the alphabet consists of a single symbol.
194*01826a49SYabin Cui * However, in some rare circumstances, it could be the compressed size (a single byte).
195*01826a49SYabin Cui * For that outcome to have a chance to happen, it's necessary that `srcSize < 8`.
196*01826a49SYabin Cui * (it's also necessary to not generate statistics).
197*01826a49SYabin Cui * Therefore, in such a case, actively check that all bytes are identical. */
198*01826a49SYabin Cui if ((srcSize >= 8) || allBytesIdentical(src, srcSize)) {
199*01826a49SYabin Cui ZSTD_memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
200*01826a49SYabin Cui return ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize);
201*01826a49SYabin Cui } }
202*01826a49SYabin Cui
203*01826a49SYabin Cui if (hType == set_compressed) {
204*01826a49SYabin Cui /* using a newly constructed table */
205*01826a49SYabin Cui nextHuf->repeatMode = HUF_repeat_check;
206*01826a49SYabin Cui }
207*01826a49SYabin Cui
208*01826a49SYabin Cui /* Build header */
209*01826a49SYabin Cui switch(lhSize)
210*01826a49SYabin Cui {
211*01826a49SYabin Cui case 3: /* 2 - 2 - 10 - 10 */
212*01826a49SYabin Cui if (!singleStream) assert(srcSize >= MIN_LITERALS_FOR_4_STREAMS);
213*01826a49SYabin Cui { U32 const lhc = hType + ((U32)(!singleStream) << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<14);
214*01826a49SYabin Cui MEM_writeLE24(ostart, lhc);
215*01826a49SYabin Cui break;
216*01826a49SYabin Cui }
217*01826a49SYabin Cui case 4: /* 2 - 2 - 14 - 14 */
218*01826a49SYabin Cui assert(srcSize >= MIN_LITERALS_FOR_4_STREAMS);
219*01826a49SYabin Cui { U32 const lhc = hType + (2 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<18);
220*01826a49SYabin Cui MEM_writeLE32(ostart, lhc);
221*01826a49SYabin Cui break;
222*01826a49SYabin Cui }
223*01826a49SYabin Cui case 5: /* 2 - 2 - 18 - 18 */
224*01826a49SYabin Cui assert(srcSize >= MIN_LITERALS_FOR_4_STREAMS);
225*01826a49SYabin Cui { U32 const lhc = hType + (3 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<22);
226*01826a49SYabin Cui MEM_writeLE32(ostart, lhc);
227*01826a49SYabin Cui ostart[4] = (BYTE)(cLitSize >> 10);
228*01826a49SYabin Cui break;
229*01826a49SYabin Cui }
230*01826a49SYabin Cui default: /* not possible : lhSize is {3,4,5} */
231*01826a49SYabin Cui assert(0);
232*01826a49SYabin Cui }
233*01826a49SYabin Cui DEBUGLOG(5, "Compressed literals: %u -> %u", (U32)srcSize, (U32)(lhSize+cLitSize));
234*01826a49SYabin Cui return lhSize+cLitSize;
235*01826a49SYabin Cui }
236