|
| 1 | +/* Copyright (c) 2026 VillageSQL Contributors |
| 2 | + * |
| 3 | + * This program is free software; you can redistribute it and/or |
| 4 | + * modify it under the terms of the GNU General Public License |
| 5 | + * as published by the Free Software Foundation; either version 2 |
| 6 | + * of the License, or (at your option) any later version. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "villagesql/types/type_decoder.h" |
| 18 | + |
| 19 | +#include <cassert> |
| 20 | +#include <cstring> |
| 21 | + |
| 22 | +#include "my_alloc.h" |
| 23 | +#include "my_base.h" |
| 24 | +#include "my_inttypes.h" |
| 25 | +#include "mysqld_error.h" |
| 26 | +#include "sql_string.h" |
| 27 | +#include "template_utils.h" |
| 28 | +#include "villagesql/include/error.h" |
| 29 | +#include "villagesql/schema/descriptor/type_descriptor.h" |
| 30 | +#include "villagesql/types/type_op.h" |
| 31 | + |
| 32 | +namespace villagesql { |
| 33 | + |
| 34 | +TypeDecoder::TypeDecoder(const TypeContext *tc, MEM_ROOT &mem_root) |
| 35 | + : mem_root_(&mem_root), |
| 36 | + buffer_size_(static_cast<size_t>(tc->max_decode_buffer_length())) { |
| 37 | + assert(tc != nullptr); |
| 38 | + assert(buffer_size_ > 0); |
| 39 | + |
| 40 | + const DecodeOp &op = tc->descriptor()->decode_op(); |
| 41 | + if (op.vdf() != nullptr) { |
| 42 | + vdf_ = op.vdf(); |
| 43 | + assert(vdf_->prerun == nullptr && vdf_->postrun == nullptr); |
| 44 | + ctx_.protocol = VEF_PROTOCOL_2; |
| 45 | + input_[0].type = VEF_TYPE_CUSTOM; |
| 46 | + input_[0].is_null = false; |
| 47 | + vdf_args_.user_data = nullptr; |
| 48 | + vdf_args_.value_count = 1; |
| 49 | + vdf_args_.values = input_; |
| 50 | + vdf_result_.error_msg = error_msg_; |
| 51 | + vdf_result_.max_str_len = buffer_size_; |
| 52 | + vdf_result_.alt_str_buf = &alt_str_buf_; |
| 53 | + } else { |
| 54 | + fn_ = op.fn(); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +bool TypeDecoder::Init() { |
| 59 | + buffer_ = new (mem_root_) char[buffer_size_]; |
| 60 | + if (!buffer_) { |
| 61 | + my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), buffer_size_); |
| 62 | + return false; |
| 63 | + } |
| 64 | + if (vdf_ != nullptr) { |
| 65 | + vdf_result_.str_buf = buffer_; |
| 66 | + } |
| 67 | + return true; |
| 68 | +} |
| 69 | + |
| 70 | +bool TypeDecoder::decode(const uchar *data, size_t len, String *out, |
| 71 | + bool &is_valid) { |
| 72 | + is_valid = true; |
| 73 | + |
| 74 | + if (vdf_ != nullptr) { |
| 75 | + input_[0].bin_len = len; |
| 76 | + input_[0].bin_value = data; |
| 77 | + vdf_result_.type = VEF_RESULT_VALUE; |
| 78 | + vdf_result_.actual_len = 0; |
| 79 | + alt_str_buf_ = nullptr; |
| 80 | + |
| 81 | + vdf_->vdf(&ctx_, &vdf_args_, &vdf_result_); |
| 82 | + |
| 83 | + if (vdf_result_.type != VEF_RESULT_VALUE) { |
| 84 | + is_valid = false; |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + const size_t actual_len = vdf_result_.actual_len; |
| 89 | + |
| 90 | + if (alt_str_buf_ != nullptr) { |
| 91 | + // VDF used its own buffer (output exceeded buffer_size_). Grow |
| 92 | + // overflow_buf_ if needed and reuse it across rows. |
| 93 | + if (actual_len > overflow_buf_size_) { |
| 94 | + auto *new_buf = new (mem_root_) char[actual_len]; |
| 95 | + if (should_assert_if_null(new_buf)) { |
| 96 | + my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), actual_len); |
| 97 | + return false; |
| 98 | + } |
| 99 | + overflow_buf_ = new_buf; |
| 100 | + overflow_buf_size_ = actual_len; |
| 101 | + } |
| 102 | + if (actual_len > 0) memcpy(overflow_buf_, alt_str_buf_, actual_len); |
| 103 | + out->set(overflow_buf_, actual_len, &my_charset_utf8mb4_bin); |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + // TODO(villagesql-beta): report an error or warning when the VDF overruns |
| 108 | + // the buffer rather than silently returning invalid. |
| 109 | + if (should_assert_if_false(actual_len <= buffer_size_)) { |
| 110 | + is_valid = false; |
| 111 | + return false; |
| 112 | + } |
| 113 | + out->set(buffer_, actual_len, &my_charset_utf8mb4_bin); |
| 114 | + } else { |
| 115 | + assert(fn_ != nullptr); |
| 116 | + size_t decoded_length = 0; |
| 117 | + if (fn_(data, len, buffer_, buffer_size_, &decoded_length)) { |
| 118 | + is_valid = false; |
| 119 | + return false; |
| 120 | + } |
| 121 | + // TODO(villagesql-beta): report an error or warning when the fn_ overruns |
| 122 | + // the buffer rather than silently returning invalid. |
| 123 | + if (should_assert_if_false(decoded_length <= buffer_size_)) { |
| 124 | + is_valid = false; |
| 125 | + return false; |
| 126 | + } |
| 127 | + out->set(buffer_, decoded_length, &my_charset_utf8mb4_bin); |
| 128 | + } |
| 129 | + |
| 130 | + return true; |
| 131 | +} |
| 132 | + |
| 133 | +} // namespace villagesql |
0 commit comments