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