|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Adriano dos Santos Fernandes |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to do so, subject to the |
| 11 | + * following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include "Blob.h" |
| 26 | +#include "Attachment.h" |
| 27 | +#include "Client.h" |
| 28 | +#include "Transaction.h" |
| 29 | +#include "fb-api/firebird/impl/inf_pub.h" |
| 30 | +#include <limits> |
| 31 | + |
| 32 | +using namespace fbcpp; |
| 33 | +using namespace fbcpp::impl; |
| 34 | + |
| 35 | +namespace |
| 36 | +{ |
| 37 | + std::pair<unsigned, const unsigned char*> getBpb(const BlobOptions& options) |
| 38 | + { |
| 39 | + const auto& bpb = options.getBpb(); |
| 40 | + |
| 41 | + if (bpb.size() > std::numeric_limits<unsigned>::max()) |
| 42 | + throw FbCppException("BPB too large"); |
| 43 | + |
| 44 | + const auto length = static_cast<unsigned>(bpb.size()); |
| 45 | + const auto data = bpb.empty() ? nullptr : reinterpret_cast<const unsigned char*>(bpb.data()); |
| 46 | + |
| 47 | + return {length, data}; |
| 48 | + } |
| 49 | +} // namespace |
| 50 | + |
| 51 | + |
| 52 | +Blob::Blob(Attachment& attachment, Transaction& transaction, const BlobOptions& options) |
| 53 | + : attachment{attachment}, |
| 54 | + transaction{transaction}, |
| 55 | + status{attachment.getClient().newStatus()}, |
| 56 | + statusWrapper{attachment.getClient(), status.get()} |
| 57 | +{ |
| 58 | + assert(attachment.isValid()); |
| 59 | + assert(transaction.isValid()); |
| 60 | + |
| 61 | + const auto [bpbLength, bpbData] = getBpb(options); |
| 62 | + |
| 63 | + handle.reset( |
| 64 | + attachment.getHandle()->createBlob(&statusWrapper, transaction.getHandle().get(), &id.id, bpbLength, bpbData)); |
| 65 | +} |
| 66 | + |
| 67 | +Blob::Blob(Attachment& attachment, Transaction& transaction, const BlobId& blobId, const BlobOptions& options) |
| 68 | + : attachment{attachment}, |
| 69 | + transaction{transaction}, |
| 70 | + id{blobId}, |
| 71 | + status{attachment.getClient().newStatus()}, |
| 72 | + statusWrapper{attachment.getClient(), status.get()} |
| 73 | +{ |
| 74 | + assert(attachment.isValid()); |
| 75 | + assert(transaction.isValid()); |
| 76 | + |
| 77 | + const auto [bpbLength, bpbData] = getBpb(options); |
| 78 | + |
| 79 | + handle.reset( |
| 80 | + attachment.getHandle()->openBlob(&statusWrapper, transaction.getHandle().get(), &id.id, bpbLength, bpbData)); |
| 81 | +} |
| 82 | + |
| 83 | +unsigned Blob::getLength() |
| 84 | +{ |
| 85 | + assert(isValid()); |
| 86 | + |
| 87 | + const unsigned char items[] = {isc_info_blob_total_length}; |
| 88 | + unsigned char buffer[16]{}; |
| 89 | + |
| 90 | + handle->getInfo(&statusWrapper, sizeof(items), items, sizeof(buffer), buffer); |
| 91 | + |
| 92 | + const auto* ptr = buffer; |
| 93 | + const auto* end = buffer + sizeof(buffer); |
| 94 | + |
| 95 | + while (ptr < end) |
| 96 | + { |
| 97 | + const auto item = *ptr++; |
| 98 | + |
| 99 | + if (item == isc_info_end) |
| 100 | + break; |
| 101 | + |
| 102 | + if (item == isc_info_truncated) |
| 103 | + throw FbCppException("Blob::getLength truncated response"); |
| 104 | + |
| 105 | + if (item == isc_info_error) |
| 106 | + throw FbCppException("Blob::getLength error response"); |
| 107 | + |
| 108 | + if (ptr + 2 > end) |
| 109 | + throw FbCppException("Blob::getLength malformed response"); |
| 110 | + |
| 111 | + const auto itemLength = static_cast<std::uint16_t>((ptr[0]) | (ptr[1] << 8)); |
| 112 | + ptr += 2; |
| 113 | + |
| 114 | + if (ptr + itemLength > end) |
| 115 | + throw FbCppException("Blob::getLength invalid length"); |
| 116 | + |
| 117 | + if (item == isc_info_blob_total_length) |
| 118 | + { |
| 119 | + unsigned result = 0; |
| 120 | + |
| 121 | + for (std::uint16_t i = 0; i < itemLength; ++i) |
| 122 | + result |= static_cast<unsigned>(ptr[i]) << (8u * i); |
| 123 | + |
| 124 | + return result; |
| 125 | + } |
| 126 | + |
| 127 | + ptr += itemLength; |
| 128 | + } |
| 129 | + |
| 130 | + throw FbCppException("Blob::getLength value not found"); |
| 131 | +} |
| 132 | + |
| 133 | +unsigned Blob::read(std::span<std::byte> buffer) |
| 134 | +{ |
| 135 | + assert(isValid()); |
| 136 | + |
| 137 | + unsigned totalRead = 0; |
| 138 | + const unsigned maxChunkSize = std::numeric_limits<std::uint16_t>::max(); |
| 139 | + |
| 140 | + while (!buffer.empty()) |
| 141 | + { |
| 142 | + const auto chunkSize = buffer.size() < maxChunkSize ? buffer.size() : maxChunkSize; |
| 143 | + const auto chunk = buffer.first(chunkSize); |
| 144 | + const auto readNow = readSegment(chunk); |
| 145 | + |
| 146 | + if (readNow == 0) |
| 147 | + break; |
| 148 | + |
| 149 | + totalRead += readNow; |
| 150 | + buffer = buffer.subspan(readNow); |
| 151 | + } |
| 152 | + |
| 153 | + return totalRead; |
| 154 | +} |
| 155 | + |
| 156 | +unsigned Blob::readSegment(std::span<std::byte> buffer) |
| 157 | +{ |
| 158 | + assert(isValid()); |
| 159 | + |
| 160 | + if (buffer.empty()) |
| 161 | + return 0; |
| 162 | + |
| 163 | + unsigned segmentLength = 0; |
| 164 | + const auto result = |
| 165 | + handle->getSegment(&statusWrapper, static_cast<unsigned>(buffer.size()), buffer.data(), &segmentLength); |
| 166 | + |
| 167 | + switch (result) |
| 168 | + { |
| 169 | + case fb::IStatus::RESULT_OK: |
| 170 | + case fb::IStatus::RESULT_SEGMENT: |
| 171 | + return segmentLength; |
| 172 | + |
| 173 | + case fb::IStatus::RESULT_NO_DATA: |
| 174 | + return 0; |
| 175 | + |
| 176 | + default: |
| 177 | + return 0; |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +void Blob::write(std::span<const std::byte> buffer) |
| 182 | +{ |
| 183 | + assert(isValid()); |
| 184 | + |
| 185 | + const unsigned maxChunkSize = std::numeric_limits<std::uint16_t>::max(); |
| 186 | + |
| 187 | + while (!buffer.empty()) |
| 188 | + { |
| 189 | + const auto chunkSize = buffer.size() < maxChunkSize ? buffer.size() : maxChunkSize; |
| 190 | + const auto chunk = buffer.first(chunkSize); |
| 191 | + writeSegment(chunk); |
| 192 | + buffer = buffer.subspan(chunkSize); |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +void Blob::writeSegment(std::span<const std::byte> buffer) |
| 197 | +{ |
| 198 | + assert(isValid()); |
| 199 | + |
| 200 | + if (buffer.empty()) |
| 201 | + return; |
| 202 | + |
| 203 | + if (buffer.size() > std::numeric_limits<unsigned>::max()) |
| 204 | + throw FbCppException("Segment too large"); |
| 205 | + |
| 206 | + handle->putSegment(&statusWrapper, static_cast<unsigned>(buffer.size()), buffer.data()); |
| 207 | +} |
| 208 | + |
| 209 | +int Blob::seek(BlobSeekMode mode, int offset) |
| 210 | +{ |
| 211 | + assert(isValid()); |
| 212 | + return handle->seek(&statusWrapper, static_cast<int>(mode), offset); |
| 213 | +} |
| 214 | + |
| 215 | +void Blob::cancel() |
| 216 | +{ |
| 217 | + assert(isValid()); |
| 218 | + |
| 219 | + handle->cancel(&statusWrapper); |
| 220 | + handle.reset(); |
| 221 | +} |
| 222 | + |
| 223 | +void Blob::close() |
| 224 | +{ |
| 225 | + assert(isValid()); |
| 226 | + |
| 227 | + handle->close(&statusWrapper); |
| 228 | + handle.reset(); |
| 229 | +} |
0 commit comments