| 1 |
/* Copyright (c) 2019, 2026, Oracle and/or its affiliates. |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License, version 2.0, |
| 5 |
as published by the Free Software Foundation. |
| 6 |
|
| 7 |
This program is designed to work with certain software (including |
| 8 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 9 |
as designated in a particular file or component or in included license |
| 10 |
documentation. The authors of MySQL hereby grant you an additional |
| 11 |
permission to link the program and your derivative works with the |
| 12 |
separately licensed software that they have either included with |
| 13 |
the program or referenced in the documentation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License, version 2.0, for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 |
|
| 24 |
#include <compression/zstd_dec.h> |
| 25 |
#include <my_byteorder.h> // TODO: fix this include |
| 26 |
#include <algorithm> |
| 27 |
#include "wrapper_functions.h" |
| 28 |
|
| 29 |
namespace binary_log::transaction::compression { |
| 30 |
|
| 31 |
Zstd_dec::Zstd_dec() : m_ctx(nullptr) {} |
| 32 |
|
| 33 |
Zstd_dec::~Zstd_dec() { destroy(); } |
| 34 |
|
| 35 |
void Zstd_dec::destroy() { |
| 36 |
if (m_ctx != nullptr) { |
| 37 |
ZSTD_freeDStream(m_ctx); |
| 38 |
m_ctx = nullptr; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
type Zstd_dec::do_get_type_code() const { return type_code; } |
| 43 |
|
| 44 |
void Zstd_dec::do_reset() { |
| 45 |
BAPI_TRACE; |
| 46 |
|
| 47 |
if (m_ctx != nullptr) { |
| 48 |
auto ret = ZSTD_initDStream(m_ctx); |
| 49 |
if (ZSTD_isError(ret) != 0U) { |
| 50 |
BAPI_LOG("info", BAPI_VAR(ZSTD_getErrorName(ret))); |
| 51 |
destroy(); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
void Zstd_dec::do_feed(const Char_t *input_data, Size_t input_size) { |
| 57 |
BAPI_TRACE; |
| 58 |
BAPI_LOG("info", BAPI_VAR(input_size)); |
| 59 |
|
| 60 |
// Protect against two successive calls to `feed` without a call to |
| 61 |
// `compress` between them. |
| 62 |
assert(m_ibuf.pos == m_ibuf.size); |
| 63 |
|
| 64 |
m_ibuf.src = input_data; |
| 65 |
m_ibuf.size = input_size; |
| 66 |
m_ibuf.pos = 0; |
| 67 |
} |
| 68 |
|
| 69 |
std::pair<Decompress_status, Decompressor::Size_t> Zstd_dec::do_decompress( |
| 70 |
Char_t *out, Size_t output_size) { |
| 71 |
BAPI_TRACE; |
| 72 |
assert(m_ibuf.src); |
| 73 |
|
| 74 |
// NOLINTBEGIN(cppcoreguidelines-macro-usage) |
| 75 |
#define TRACE_RETURN(status, size) \ |
| 76 |
{ \ |
| 77 |
BAPI_LOG("info", __FILE__ << ":" << __LINE__ << ": return [" \ |
| 78 |
<< debug_string(Decompress_status::status) \ |
| 79 |
<< ", " << (size) << "]"); \ |
| 80 |
return std::make_pair(Decompress_status::status, (size)); \ |
| 81 |
} |
| 82 |
// NOLINTEND(cppcoreguidelines-macro-usage) |
| 83 |
|
| 84 |
if (m_ctx == nullptr) { |
| 85 |
m_ctx = ZSTD_createDStream(); |
| 86 |
if (m_ctx == nullptr) TRACE_RETURN(out_of_memory, 0); |
| 87 |
} |
| 88 |
|
| 89 |
ZSTD_outBuffer obuf{/*.dst = */ out, |
| 90 |
/*.size = */ output_size, |
| 91 |
/*.pos = */ 0}; |
| 92 |
// Decompress. |
| 93 |
size_t ret = 0; |
| 94 |
do { |
| 95 |
// clang-format off |
| 96 |
BAPI_LOG("info", "before decompress:" |
| 97 |
<< " " << BAPI_VAR(output_size) |
| 98 |
<< " " << BAPI_VAR(m_ibuf.size) |
| 99 |
<< " " << BAPI_VAR(m_ibuf.pos) |
| 100 |
<< " " << BAPI_VAR(obuf.size) |
| 101 |
<< " " << BAPI_VAR(obuf.pos)); |
| 102 |
ret = ZSTD_decompressStream(m_ctx, &obuf, &m_ibuf); |
| 103 |
BAPI_LOG("info", "after decompress:" |
| 104 |
<< " " << BAPI_VAR(ret) |
| 105 |
<< " " << BAPI_VAR(output_size) |
| 106 |
<< " " << BAPI_VAR(m_ibuf.size) |
| 107 |
<< " " << BAPI_VAR(m_ibuf.pos) |
| 108 |
<< " " << BAPI_VAR(obuf.size) |
| 109 |
<< " " << BAPI_VAR(obuf.pos)); |
| 110 |
// clang-format on |
| 111 |
// ZSTD detected corrupt data. |
| 112 |
if (ZSTD_isError(ret) != 0U) { |
| 113 |
BAPI_LOG("info", BAPI_VAR(ZSTD_getErrorName(ret))); |
| 114 |
TRACE_RETURN(corrupted, 0); |
| 115 |
} |
| 116 |
// If there is a frame boundary in the middle of the input, ZSTD |
| 117 |
// will stop at the boundary, even if there is more input |
| 118 |
// available and more output space available. So in that case we |
| 119 |
// repeat. |
| 120 |
} while (m_ibuf.pos < m_ibuf.size && obuf.pos < obuf.size); |
| 121 |
|
| 122 |
auto was_frame_boundary = m_frame_boundary; |
| 123 |
m_frame_boundary = (ret == 0); |
| 124 |
if (obuf.pos == 0 && was_frame_boundary) TRACE_RETURN(end, 0); |
| 125 |
if (obuf.pos < output_size) TRACE_RETURN(truncated, obuf.pos); |
| 126 |
|
| 127 |
// ZSTD was able to decode all requested bytes. |
| 128 |
assert(obuf.pos == output_size); |
| 129 |
TRACE_RETURN(success, output_size); |
| 130 |
} |
| 131 |
|
| 132 |
Decompressor::Grow_constraint_t Zstd_dec::do_get_grow_constraint_hint() const { |
| 133 |
Grow_constraint_t ret; |
| 134 |
ret.set_grow_increment(ZSTD_DStreamOutSize()); |
| 135 |
// Todo: we may use ZSTD_getFrameContentSize at the beginning of |
| 136 |
// each frame to get an upper bound, and pass that to |
| 137 |
// ret.set_max_size. |
| 138 |
return ret; |
| 139 |
} |
| 140 |
|
| 141 |
} // namespace binary_log::transaction::compression |