You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zstd_compress_literals.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2016-2020, 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* const)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. 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* const)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. {
  68. size_t const minGain = ZSTD_minGain(srcSize, strategy);
  69. size_t const lhSize = 3 + (srcSize >= 1 KB) + (srcSize >= 16 KB);
  70. BYTE* const ostart = (BYTE*)dst;
  71. U32 singleStream = srcSize < 256;
  72. symbolEncodingType_e hType = set_compressed;
  73. size_t cLitSize;
  74. DEBUGLOG(5,"ZSTD_compressLiterals (disableLiteralCompression=%i srcSize=%u)",
  75. disableLiteralCompression, (U32)srcSize);
  76. /* Prepare nextEntropy assuming reusing the existing table */
  77. memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
  78. if (disableLiteralCompression)
  79. return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
  80. /* small ? don't even attempt compression (speed opt) */
  81. # define COMPRESS_LITERALS_SIZE_MIN 63
  82. { size_t const minLitSize = (prevHuf->repeatMode == HUF_repeat_valid) ? 6 : COMPRESS_LITERALS_SIZE_MIN;
  83. if (srcSize <= minLitSize) return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
  84. }
  85. RETURN_ERROR_IF(dstCapacity < lhSize+1, dstSize_tooSmall, "not enough space for compression");
  86. { HUF_repeat repeat = prevHuf->repeatMode;
  87. int const preferRepeat = strategy < ZSTD_lazy ? srcSize <= 1024 : 0;
  88. if (repeat == HUF_repeat_valid && lhSize == 3) singleStream = 1;
  89. cLitSize = singleStream ?
  90. HUF_compress1X_repeat(
  91. ostart+lhSize, dstCapacity-lhSize, src, srcSize,
  92. HUF_SYMBOLVALUE_MAX, HUF_TABLELOG_DEFAULT, entropyWorkspace, entropyWorkspaceSize,
  93. (HUF_CElt*)nextHuf->CTable, &repeat, preferRepeat, bmi2) :
  94. HUF_compress4X_repeat(
  95. ostart+lhSize, dstCapacity-lhSize, src, srcSize,
  96. HUF_SYMBOLVALUE_MAX, HUF_TABLELOG_DEFAULT, entropyWorkspace, entropyWorkspaceSize,
  97. (HUF_CElt*)nextHuf->CTable, &repeat, preferRepeat, bmi2);
  98. if (repeat != HUF_repeat_none) {
  99. /* reused the existing table */
  100. DEBUGLOG(5, "Reusing previous huffman table");
  101. hType = set_repeat;
  102. }
  103. }
  104. if ((cLitSize==0) | (cLitSize >= srcSize - minGain) | ERR_isError(cLitSize)) {
  105. memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
  106. return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
  107. }
  108. if (cLitSize==1) {
  109. memcpy(nextHuf, prevHuf, sizeof(*prevHuf));
  110. return ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize);
  111. }
  112. if (hType == set_compressed) {
  113. /* using a newly constructed table */
  114. nextHuf->repeatMode = HUF_repeat_check;
  115. }
  116. /* Build header */
  117. switch(lhSize)
  118. {
  119. case 3: /* 2 - 2 - 10 - 10 */
  120. { U32 const lhc = hType + ((!singleStream) << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<14);
  121. MEM_writeLE24(ostart, lhc);
  122. break;
  123. }
  124. case 4: /* 2 - 2 - 14 - 14 */
  125. { U32 const lhc = hType + (2 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<18);
  126. MEM_writeLE32(ostart, lhc);
  127. break;
  128. }
  129. case 5: /* 2 - 2 - 18 - 18 */
  130. { U32 const lhc = hType + (3 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<22);
  131. MEM_writeLE32(ostart, lhc);
  132. ostart[4] = (BYTE)(cLitSize >> 10);
  133. break;
  134. }
  135. default: /* not possible : lhSize is {3,4,5} */
  136. assert(0);
  137. }
  138. DEBUGLOG(5, "Compressed literals: %u -> %u", (U32)srcSize, (U32)(lhSize+cLitSize));
  139. return lhSize+cLitSize;
  140. }