1e0c1b49fSNick Terrell /*
2e0c1b49fSNick Terrell * Copyright (c) Yann Collet, Facebook, Inc.
3e0c1b49fSNick Terrell * All rights reserved.
4e0c1b49fSNick Terrell *
5e0c1b49fSNick Terrell * This source code is licensed under both the BSD-style license (found in the
6e0c1b49fSNick Terrell * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7e0c1b49fSNick Terrell * in the COPYING file in the root directory of this source tree).
8e0c1b49fSNick Terrell * You may select, at your option, one of the above-listed licenses.
9e0c1b49fSNick Terrell */
10e0c1b49fSNick Terrell
11e0c1b49fSNick Terrell /*-*************************************
12e0c1b49fSNick Terrell * Dependencies
13e0c1b49fSNick Terrell ***************************************/
14e0c1b49fSNick Terrell #include "zstd_compress_literals.h"
15e0c1b49fSNick Terrell
ZSTD_noCompressLiterals(void * dst,size_t dstCapacity,const void * src,size_t srcSize)16e0c1b49fSNick Terrell size_t ZSTD_noCompressLiterals (void* dst, size_t dstCapacity, const void* src, size_t srcSize)
17e0c1b49fSNick Terrell {
18e0c1b49fSNick Terrell BYTE* const ostart = (BYTE*)dst;
19e0c1b49fSNick Terrell U32 const flSize = 1 + (srcSize>31) + (srcSize>4095);
20e0c1b49fSNick Terrell
21e0c1b49fSNick Terrell RETURN_ERROR_IF(srcSize + flSize > dstCapacity, dstSize_tooSmall, "");
22e0c1b49fSNick Terrell
23e0c1b49fSNick Terrell switch(flSize)
24e0c1b49fSNick Terrell {
25e0c1b49fSNick Terrell case 1: /* 2 - 1 - 5 */
26e0c1b49fSNick Terrell ostart[0] = (BYTE)((U32)set_basic + (srcSize<<3));
27e0c1b49fSNick Terrell break;
28e0c1b49fSNick Terrell case 2: /* 2 - 2 - 12 */
29e0c1b49fSNick Terrell MEM_writeLE16(ostart, (U16)((U32)set_basic + (1<<2) + (srcSize<<4)));
30e0c1b49fSNick Terrell break;
31e0c1b49fSNick Terrell case 3: /* 2 - 2 - 20 */
32e0c1b49fSNick Terrell MEM_writeLE32(ostart, (U32)((U32)set_basic + (3<<2) + (srcSize<<4)));
33e0c1b49fSNick Terrell break;
34e0c1b49fSNick Terrell default: /* not necessary : flSize is {1,2,3} */
35e0c1b49fSNick Terrell assert(0);
36e0c1b49fSNick Terrell }
37e0c1b49fSNick Terrell
38e0c1b49fSNick Terrell ZSTD_memcpy(ostart + flSize, src, srcSize);
39e0c1b49fSNick Terrell DEBUGLOG(5, "Raw literals: %u -> %u", (U32)srcSize, (U32)(srcSize + flSize));
40e0c1b49fSNick Terrell return srcSize + flSize;
41e0c1b49fSNick Terrell }
42e0c1b49fSNick Terrell
ZSTD_compressRleLiteralsBlock(void * dst,size_t dstCapacity,const void * src,size_t srcSize)43e0c1b49fSNick Terrell size_t ZSTD_compressRleLiteralsBlock (void* dst, size_t dstCapacity, const void* src, size_t srcSize)
44e0c1b49fSNick Terrell {
45e0c1b49fSNick Terrell BYTE* const ostart = (BYTE*)dst;
46e0c1b49fSNick Terrell U32 const flSize = 1 + (srcSize>31) + (srcSize>4095);
47e0c1b49fSNick Terrell
48e0c1b49fSNick Terrell (void)dstCapacity; /* dstCapacity already guaranteed to be >=4, hence large enough */
49e0c1b49fSNick Terrell
50e0c1b49fSNick Terrell switch(flSize)
51e0c1b49fSNick Terrell {
52e0c1b49fSNick Terrell case 1: /* 2 - 1 - 5 */
53e0c1b49fSNick Terrell ostart[0] = (BYTE)((U32)set_rle + (srcSize<<3));
54e0c1b49fSNick Terrell break;
55e0c1b49fSNick Terrell case 2: /* 2 - 2 - 12 */
56e0c1b49fSNick Terrell MEM_writeLE16(ostart, (U16)((U32)set_rle + (1<<2) + (srcSize<<4)));
57e0c1b49fSNick Terrell break;
58e0c1b49fSNick Terrell case 3: /* 2 - 2 - 20 */
59e0c1b49fSNick Terrell MEM_writeLE32(ostart, (U32)((U32)set_rle + (3<<2) + (srcSize<<4)));
60e0c1b49fSNick Terrell break;
61e0c1b49fSNick Terrell default: /* not necessary : flSize is {1,2,3} */
62e0c1b49fSNick Terrell assert(0);
63e0c1b49fSNick Terrell }
64e0c1b49fSNick Terrell
65e0c1b49fSNick Terrell ostart[flSize] = *(const BYTE*)src;
66e0c1b49fSNick Terrell DEBUGLOG(5, "RLE literals: %u -> %u", (U32)srcSize, (U32)flSize + 1);
67e0c1b49fSNick Terrell return flSize+1;
68e0c1b49fSNick Terrell }
69e0c1b49fSNick Terrell
ZSTD_compressLiterals(ZSTD_hufCTables_t const * prevHuf,ZSTD_hufCTables_t * nextHuf,ZSTD_strategy strategy,int disableLiteralCompression,void * dst,size_t dstCapacity,const void * src,size_t srcSize,void * entropyWorkspace,size_t entropyWorkspaceSize,const int bmi2,unsigned suspectUncompressible)70e0c1b49fSNick Terrell size_t ZSTD_compressLiterals (ZSTD_hufCTables_t const* prevHuf,
71e0c1b49fSNick Terrell ZSTD_hufCTables_t* nextHuf,
72e0c1b49fSNick Terrell ZSTD_strategy strategy, int disableLiteralCompression,
73e0c1b49fSNick Terrell void* dst, size_t dstCapacity,
74e0c1b49fSNick Terrell const void* src, size_t srcSize,
75e0c1b49fSNick Terrell void* entropyWorkspace, size_t entropyWorkspaceSize,
76*2aa14b1aSNick Terrell const int bmi2,
77*2aa14b1aSNick Terrell unsigned suspectUncompressible)
78e0c1b49fSNick Terrell {
79e0c1b49fSNick Terrell size_t const minGain = ZSTD_minGain(srcSize, strategy);
80e0c1b49fSNick Terrell size_t const lhSize = 3 + (srcSize >= 1 KB) + (srcSize >= 16 KB);
81e0c1b49fSNick Terrell BYTE* const ostart = (BYTE*)dst;
82e0c1b49fSNick Terrell U32 singleStream = srcSize < 256;
83e0c1b49fSNick Terrell symbolEncodingType_e hType = set_compressed;
84e0c1b49fSNick Terrell size_t cLitSize;
85e0c1b49fSNick Terrell
86e0c1b49fSNick Terrell DEBUGLOG(5,"ZSTD_compressLiterals (disableLiteralCompression=%i srcSize=%u)",
87e0c1b49fSNick Terrell disableLiteralCompression, (U32)srcSize);
88e0c1b49fSNick Terrell
89e0c1b49fSNick Terrell /* Prepare nextEntropy assuming reusing the existing table */
90e0c1b49fSNick Terrell ZSTD_memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
91e0c1b49fSNick Terrell
92e0c1b49fSNick Terrell if (disableLiteralCompression)
93e0c1b49fSNick Terrell return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
94e0c1b49fSNick Terrell
95e0c1b49fSNick Terrell /* small ? don't even attempt compression (speed opt) */
96e0c1b49fSNick Terrell # define COMPRESS_LITERALS_SIZE_MIN 63
97e0c1b49fSNick Terrell { size_t const minLitSize = (prevHuf->repeatMode == HUF_repeat_valid) ? 6 : COMPRESS_LITERALS_SIZE_MIN;
98e0c1b49fSNick Terrell if (srcSize <= minLitSize) return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
99e0c1b49fSNick Terrell }
100e0c1b49fSNick Terrell
101e0c1b49fSNick Terrell RETURN_ERROR_IF(dstCapacity < lhSize+1, dstSize_tooSmall, "not enough space for compression");
102e0c1b49fSNick Terrell { HUF_repeat repeat = prevHuf->repeatMode;
103e0c1b49fSNick Terrell int const preferRepeat = strategy < ZSTD_lazy ? srcSize <= 1024 : 0;
104e0c1b49fSNick Terrell if (repeat == HUF_repeat_valid && lhSize == 3) singleStream = 1;
105e0c1b49fSNick Terrell cLitSize = singleStream ?
106e0c1b49fSNick Terrell HUF_compress1X_repeat(
107e0c1b49fSNick Terrell ostart+lhSize, dstCapacity-lhSize, src, srcSize,
108e0c1b49fSNick Terrell HUF_SYMBOLVALUE_MAX, HUF_TABLELOG_DEFAULT, entropyWorkspace, entropyWorkspaceSize,
109*2aa14b1aSNick Terrell (HUF_CElt*)nextHuf->CTable, &repeat, preferRepeat, bmi2, suspectUncompressible) :
110e0c1b49fSNick Terrell HUF_compress4X_repeat(
111e0c1b49fSNick Terrell ostart+lhSize, dstCapacity-lhSize, src, srcSize,
112e0c1b49fSNick Terrell HUF_SYMBOLVALUE_MAX, HUF_TABLELOG_DEFAULT, entropyWorkspace, entropyWorkspaceSize,
113*2aa14b1aSNick Terrell (HUF_CElt*)nextHuf->CTable, &repeat, preferRepeat, bmi2, suspectUncompressible);
114e0c1b49fSNick Terrell if (repeat != HUF_repeat_none) {
115e0c1b49fSNick Terrell /* reused the existing table */
116e0c1b49fSNick Terrell DEBUGLOG(5, "Reusing previous huffman table");
117e0c1b49fSNick Terrell hType = set_repeat;
118e0c1b49fSNick Terrell }
119e0c1b49fSNick Terrell }
120e0c1b49fSNick Terrell
121*2aa14b1aSNick Terrell if ((cLitSize==0) || (cLitSize >= srcSize - minGain) || ERR_isError(cLitSize)) {
122e0c1b49fSNick Terrell ZSTD_memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
123e0c1b49fSNick Terrell return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
124e0c1b49fSNick Terrell }
125e0c1b49fSNick Terrell if (cLitSize==1) {
126e0c1b49fSNick Terrell ZSTD_memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
127e0c1b49fSNick Terrell return ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize);
128e0c1b49fSNick Terrell }
129e0c1b49fSNick Terrell
130e0c1b49fSNick Terrell if (hType == set_compressed) {
131e0c1b49fSNick Terrell /* using a newly constructed table */
132e0c1b49fSNick Terrell nextHuf->repeatMode = HUF_repeat_check;
133e0c1b49fSNick Terrell }
134e0c1b49fSNick Terrell
135e0c1b49fSNick Terrell /* Build header */
136e0c1b49fSNick Terrell switch(lhSize)
137e0c1b49fSNick Terrell {
138e0c1b49fSNick Terrell case 3: /* 2 - 2 - 10 - 10 */
139e0c1b49fSNick Terrell { U32 const lhc = hType + ((!singleStream) << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<14);
140e0c1b49fSNick Terrell MEM_writeLE24(ostart, lhc);
141e0c1b49fSNick Terrell break;
142e0c1b49fSNick Terrell }
143e0c1b49fSNick Terrell case 4: /* 2 - 2 - 14 - 14 */
144e0c1b49fSNick Terrell { U32 const lhc = hType + (2 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<18);
145e0c1b49fSNick Terrell MEM_writeLE32(ostart, lhc);
146e0c1b49fSNick Terrell break;
147e0c1b49fSNick Terrell }
148e0c1b49fSNick Terrell case 5: /* 2 - 2 - 18 - 18 */
149e0c1b49fSNick Terrell { U32 const lhc = hType + (3 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<22);
150e0c1b49fSNick Terrell MEM_writeLE32(ostart, lhc);
151e0c1b49fSNick Terrell ostart[4] = (BYTE)(cLitSize >> 10);
152e0c1b49fSNick Terrell break;
153e0c1b49fSNick Terrell }
154e0c1b49fSNick Terrell default: /* not possible : lhSize is {3,4,5} */
155e0c1b49fSNick Terrell assert(0);
156e0c1b49fSNick Terrell }
157e0c1b49fSNick Terrell DEBUGLOG(5, "Compressed literals: %u -> %u", (U32)srcSize, (U32)(lhSize+cLitSize));
158e0c1b49fSNick Terrell return lhSize+cLitSize;
159e0c1b49fSNick Terrell }
160