|
| 1 | +// Compiler for PHP (aka KPHP) |
| 2 | +// Copyright (c) 2025 LLC «V Kontakte» |
| 3 | +// Distributed under the GPL v3 License, see LICENSE.notice.txt |
| 4 | + |
| 5 | +#include "runtime-light/stdlib/zstd/zstd-functions.h" |
| 6 | + |
| 7 | +#include <cstddef> |
| 8 | +#include <cstdint> |
| 9 | +#include <optional> |
| 10 | +#include <span> |
| 11 | + |
| 12 | +#define ZSTD_STATIC_LINKING_ONLY |
| 13 | +#include "zstd/zstd.h" |
| 14 | + |
| 15 | +#include "common/containers/final_action.h" |
| 16 | +#include "runtime-common/core/allocator/script-malloc-interface.h" |
| 17 | +#include "runtime-common/core/runtime-core.h" |
| 18 | +#include "runtime-common/stdlib/string/string-context.h" |
| 19 | +#include "runtime-light/stdlib/diagnostics/logs.h" |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +static_assert(2 * ZSTD_BLOCKSIZE_MAX < StringLibContext::STATIC_BUFFER_LENGTH, "double block size is expected to be less then buffer size"); |
| 24 | + |
| 25 | +constexpr ZSTD_customMem zstd_allocator{[](void*, size_t size) noexcept { return kphp::memory::script::alloc(size); }, |
| 26 | + [](void*, void* ptr) noexcept { return kphp::memory::script::free(ptr); }}; |
| 27 | + |
| 28 | +} // namespace |
| 29 | + |
| 30 | +namespace kphp::zstd { |
| 31 | + |
| 32 | +std::optional<string> compress(std::span<const std::byte> data, int64_t level, std::span<const std::byte> dict) noexcept { |
| 33 | + const int32_t min_level{ZSTD_minCLevel()}; |
| 34 | + const int32_t max_level{ZSTD_maxCLevel()}; |
| 35 | + if (level < min_level || max_level < level) { |
| 36 | + kphp::log::warning("zstd_compress: compression level ({}) must be within [{}..{}]", level, min_level, max_level); |
| 37 | + return {}; |
| 38 | + } |
| 39 | + |
| 40 | + ZSTD_CCtx* ctx{ZSTD_createCCtx_advanced(zstd_allocator)}; |
| 41 | + if (!ctx) { |
| 42 | + kphp::log::warning("zstd_compress: can not create context"); |
| 43 | + return {}; |
| 44 | + } |
| 45 | + const auto finalizer{vk::finally([&ctx]() noexcept { ZSTD_freeCCtx(ctx); })}; |
| 46 | + |
| 47 | + size_t result{ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, static_cast<int>(level))}; |
| 48 | + if (ZSTD_isError(result)) { |
| 49 | + kphp::log::warning("zstd_compress: can not init context: {}", ZSTD_getErrorName(result)); |
| 50 | + return {}; |
| 51 | + } |
| 52 | + |
| 53 | + result = ZSTD_CCtx_loadDictionary_byReference(ctx, dict.data(), dict.size()); |
| 54 | + if (ZSTD_isError(result)) { |
| 55 | + kphp::log::warning("zstd_compress: can not load dict: {}", ZSTD_getErrorName(result)); |
| 56 | + return {}; |
| 57 | + } |
| 58 | + |
| 59 | + kphp::log::assertion(ZSTD_CStreamOutSize() <= StringLibContext::STATIC_BUFFER_LENGTH); |
| 60 | + ZSTD_outBuffer out{StringLibContext::get().static_buf.get(), StringLibContext::STATIC_BUFFER_LENGTH, 0}; |
| 61 | + ZSTD_inBuffer in{data.data(), data.size(), 0}; |
| 62 | + |
| 63 | + string encoded_string{}; |
| 64 | + do { |
| 65 | + result = ZSTD_compressStream2(ctx, std::addressof(out), std::addressof(in), ZSTD_e_end); |
| 66 | + if (ZSTD_isError(result)) { |
| 67 | + kphp::log::warning("zstd_compress: got zstd stream compression error: {}", ZSTD_getErrorName(result)); |
| 68 | + return {}; |
| 69 | + } |
| 70 | + encoded_string.append(static_cast<char*>(out.dst), out.pos); |
| 71 | + out.pos = 0; |
| 72 | + } while (result); |
| 73 | + return encoded_string; |
| 74 | +} |
| 75 | + |
| 76 | +std::optional<string> uncompress(std::span<const std::byte> data, std::span<const std::byte> dict) noexcept { |
| 77 | + auto size{ZSTD_getFrameContentSize(data.data(), data.size())}; |
| 78 | + if (size == ZSTD_CONTENTSIZE_ERROR) { |
| 79 | + kphp::log::warning("zstd_uncompress: it was not compressed by zstd"); |
| 80 | + return {}; |
| 81 | + } |
| 82 | + |
| 83 | + ZSTD_DCtx* ctx{ZSTD_createDCtx_advanced(zstd_allocator)}; |
| 84 | + if (!ctx) { |
| 85 | + kphp::log::warning("zstd_uncompress: can not create context"); |
| 86 | + return {}; |
| 87 | + } |
| 88 | + const auto finalizer{vk::finally([&ctx]() noexcept { ZSTD_freeDCtx(ctx); })}; |
| 89 | + |
| 90 | + size_t result{ZSTD_DCtx_loadDictionary_byReference(ctx, dict.data(), dict.size())}; |
| 91 | + if (ZSTD_isError(result)) { |
| 92 | + kphp::log::warning("zstd_uncompress: can not load dict: {}", ZSTD_getErrorName(result)); |
| 93 | + return {}; |
| 94 | + } |
| 95 | + |
| 96 | + if (size != ZSTD_CONTENTSIZE_UNKNOWN) { |
| 97 | + if (size > string::max_size()) { |
| 98 | + kphp::log::warning("zstd_uncompress: trying to uncompress too large data"); |
| 99 | + return {}; |
| 100 | + } |
| 101 | + string decompressed{static_cast<string::size_type>(size), false}; |
| 102 | + result = ZSTD_decompressDCtx(ctx, decompressed.buffer(), size, data.data(), data.size()); |
| 103 | + if (ZSTD_isError(result)) { |
| 104 | + kphp::log::warning("zstd_uncompress: got zstd error: {}", ZSTD_getErrorName(result)); |
| 105 | + return {}; |
| 106 | + } |
| 107 | + return decompressed; |
| 108 | + } |
| 109 | + |
| 110 | + if (ZSTD_isError(result)) { |
| 111 | + kphp::log::warning("zstd_uncompress: can not init stream: {}", ZSTD_getErrorName(result)); |
| 112 | + return {}; |
| 113 | + } |
| 114 | + |
| 115 | + kphp::log::assertion(ZSTD_DStreamOutSize() <= StringLibContext::STATIC_BUFFER_LENGTH); |
| 116 | + ZSTD_inBuffer in{data.data(), data.size(), 0}; |
| 117 | + ZSTD_outBuffer out{StringLibContext::get().static_buf.get(), StringLibContext::STATIC_BUFFER_LENGTH, 0}; |
| 118 | + |
| 119 | + string decoded_string{}; |
| 120 | + while (in.pos < in.size) { |
| 121 | + if (out.pos == out.size) { |
| 122 | + decoded_string.append(static_cast<char*>(out.dst), static_cast<string::size_type>(out.pos)); |
| 123 | + out.pos = 0; |
| 124 | + } |
| 125 | + |
| 126 | + result = ZSTD_decompressStream(ctx, std::addressof(out), std::addressof(in)); |
| 127 | + if (ZSTD_isError(result)) { |
| 128 | + kphp::log::warning("zstd_uncompress: can not decompress stream: {}", ZSTD_getErrorName(result)); |
| 129 | + return {}; |
| 130 | + } |
| 131 | + if (result == 0) { |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + decoded_string.append(static_cast<char*>(out.dst), static_cast<string::size_type>(out.pos)); |
| 136 | + return decoded_string; |
| 137 | +} |
| 138 | + |
| 139 | +} // namespace kphp::zstd |
0 commit comments