zstd_compress_literals.c 6.2 KB

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