Coverage Report

Created: 2026-12-01 06:42

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