public static nuint ZSTD_compressLiterals(ZSTD_hufCTables_t *prevHuf, ZSTD_hufCTables_t *nextHuf, ZSTD_strategy strategy, int disableLiteralCompression, void *dst, nuint dstCapacity, void *src, nuint srcSize, void *entropyWorkspace, nuint entropyWorkspaceSize, int bmi2) { nuint minGain = ZSTD_minGain(srcSize, strategy); nuint lhSize = (nuint)(3 + ((srcSize >= (uint)(1 * (1 << 10))) ? 1 : 0) + ((srcSize >= (uint)(16 * (1 << 10))) ? 1 : 0)); byte *ostart = (byte *)(dst); uint singleStream = ((srcSize < 256) ? 1U : 0U); symbolEncodingType_e hType = symbolEncodingType_e.set_compressed; nuint cLitSize; memcpy((void *)(nextHuf), (void *)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); if (disableLiteralCompression != 0) { return(ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize)); } { nuint minLitSize = (nuint)((prevHuf->repeatMode == HUF_repeat.HUF_repeat_valid) ? 6 : 63); if (srcSize <= minLitSize) { return(ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize)); } } if (dstCapacity < lhSize + 1) { return(unchecked ((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); } { HUF_repeat repeat = prevHuf->repeatMode; int preferRepeat = strategy < ZSTD_strategy.ZSTD_lazy ? ((srcSize <= 1024) ? 1 : 0) : 0; if (repeat == HUF_repeat.HUF_repeat_valid && lhSize == 3) { singleStream = 1; } cLitSize = singleStream != 0 ? HUF_compress1X_repeat((void *)(ostart + lhSize), dstCapacity - lhSize, src, srcSize, 255, 11, entropyWorkspace, entropyWorkspaceSize, (HUF_CElt_s *)(nextHuf->CTable), &repeat, preferRepeat, bmi2) : HUF_compress4X_repeat((void *)(ostart + lhSize), dstCapacity - lhSize, src, srcSize, 255, 11, entropyWorkspace, entropyWorkspaceSize, (HUF_CElt_s *)(nextHuf->CTable), &repeat, preferRepeat, bmi2); if (repeat != HUF_repeat.HUF_repeat_none) { hType = symbolEncodingType_e.set_repeat; } } if (((uint)(((((cLitSize == 0) || (cLitSize >= srcSize - minGain))) ? 1 : 0)) | ERR_isError(cLitSize)) != 0) { memcpy((void *)(nextHuf), (void *)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); return(ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize)); } if (cLitSize == 1) { memcpy((void *)(nextHuf), (void *)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); return(ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize)); } if (hType == symbolEncodingType_e.set_compressed) { nextHuf->repeatMode = HUF_repeat.HUF_repeat_check; } switch (lhSize) { case 3: { uint lhc = (uint)(hType + ((singleStream == 0 ? 1 : 0) << 2)) + ((uint)(srcSize) << 4) + ((uint)(cLitSize) << 14); MEM_writeLE24((void *)ostart, lhc); break; } case 4: { uint lhc = (uint)(hType + (2 << 2)) + ((uint)(srcSize) << 4) + ((uint)(cLitSize) << 18); MEM_writeLE32((void *)ostart, lhc); break; } case 5: { uint lhc = (uint)(hType + (3 << 2)) + ((uint)(srcSize) << 4) + ((uint)(cLitSize) << 22); MEM_writeLE32((void *)ostart, lhc); ostart[4] = (byte)(cLitSize >> 10); break; } default: { assert(0 != 0); } break; } return(lhSize + cLitSize); }
/** ZSTD_buildSuperBlockEntropy_literal() : * Builds entropy for the super-block literals. * Stores literals block type (raw, rle, compressed, repeat) and * huffman description table to hufMetadata. * @return : size of huffman description table or error code */ private static nuint ZSTD_buildSuperBlockEntropy_literal(void *src, nuint srcSize, ZSTD_hufCTables_t *prevHuf, ZSTD_hufCTables_t *nextHuf, ZSTD_hufCTablesMetadata_t *hufMetadata, int disableLiteralsCompression, void *workspace, nuint wkspSize) { byte * wkspStart = (byte *)(workspace); byte * wkspEnd = wkspStart + wkspSize; byte * countWkspStart = wkspStart; uint * countWksp = (uint *)(workspace); nuint countWkspSize = (uint)((255 + 1)) * (nuint)(4); byte * nodeWksp = countWkspStart + countWkspSize; nuint nodeWkspSize = (nuint)(wkspEnd - nodeWksp); uint maxSymbolValue = 255; uint huffLog = 11; HUF_repeat repeat = prevHuf->repeatMode; memcpy((void *)(nextHuf), (void *)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); if (disableLiteralsCompression != 0) { hufMetadata->hType = symbolEncodingType_e.set_basic; return(0); } { nuint minLitSize = (nuint)((prevHuf->repeatMode == HUF_repeat.HUF_repeat_valid) ? 6 : 63); if (srcSize <= minLitSize) { hufMetadata->hType = symbolEncodingType_e.set_basic; return(0); } } { nuint largest = HIST_count_wksp(countWksp, &maxSymbolValue, (void *)(byte *)(src), srcSize, workspace, wkspSize); { nuint err_code = (largest); if ((ERR_isError(err_code)) != 0) { return(err_code); } } if (largest == srcSize) { hufMetadata->hType = symbolEncodingType_e.set_rle; return(0); } if (largest <= (srcSize >> 7) + 4) { hufMetadata->hType = symbolEncodingType_e.set_basic; return(0); } } if (repeat == HUF_repeat.HUF_repeat_check && (HUF_validateCTable((HUF_CElt_s *)(prevHuf->CTable), countWksp, maxSymbolValue)) == 0) { repeat = HUF_repeat.HUF_repeat_none; } memset((void *)(nextHuf->CTable), (0), ((nuint)(sizeof(HUF_CElt_s) * 256))); huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue); { nuint maxBits = HUF_buildCTable_wksp((HUF_CElt_s *)(nextHuf->CTable), countWksp, maxSymbolValue, huffLog, (void *)nodeWksp, nodeWkspSize); { nuint err_code = (maxBits); if ((ERR_isError(err_code)) != 0) { return(err_code); } } huffLog = (uint)(maxBits); { nuint newCSize = HUF_estimateCompressedSize((HUF_CElt_s *)(nextHuf->CTable), countWksp, maxSymbolValue); nuint hSize = HUF_writeCTable((void *)hufMetadata->hufDesBuffer, (nuint)(128), (HUF_CElt_s *)(nextHuf->CTable), maxSymbolValue, huffLog); if (repeat != HUF_repeat.HUF_repeat_none) { nuint oldCSize = HUF_estimateCompressedSize((HUF_CElt_s *)(prevHuf->CTable), countWksp, maxSymbolValue); if (oldCSize < srcSize && (oldCSize <= hSize + newCSize || hSize + 12 >= srcSize)) { memcpy((void *)(nextHuf), (void *)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); hufMetadata->hType = symbolEncodingType_e.set_repeat; return(0); } } if (newCSize + hSize >= srcSize) { memcpy((void *)(nextHuf), (void *)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); hufMetadata->hType = symbolEncodingType_e.set_basic; return(0); } hufMetadata->hType = symbolEncodingType_e.set_compressed; nextHuf->repeatMode = HUF_repeat.HUF_repeat_check; return(hSize); } } }