/* * This file is part of the Flowee project * Copyright (C) 2016-2025 Tom Zander * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "StreamingUtils.h" #include #include #include #include #include int Streaming::writeCompactSize(char *data, uint64_t value) { int size = 1; if (value < 253) { // nothing to do } else if (value <= std::numeric_limits::max()) { uint16_t copy = static_cast(value); memcpy(data + 1, ©, 2); size += 2; value = 253; } else if (value <= std::numeric_limits::max()) { uint32_t copy = static_cast(value); memcpy(data + 1, ©, 4); size += 4; value = 254; } else { memcpy(data + 1, &value, 8); size += 8; value = 255; } *data = static_cast(value); return size; } uint64_t Streaming::readCompactSize(const char **data, const char *end) { if (end <= *data) return 0; uint8_t x = *(*data); if (x < 253) { (*data)++; return x; } if (x == 253) { if (end <= (*data) + 3) { *data = end; return 0; } (*data)++; uint64_t answer = *reinterpret_cast(*data); *data += 2; return answer; } if (x == 254) { if (end <= (*data) + 5) { *data = end; return 0; } (*data)++; uint64_t answer = *reinterpret_cast(*data); *data += 4; return answer; } assert(x == 255); if (end <= (*data) + 9) { *data = end; return 0; } (*data)++; uint64_t answer = *reinterpret_cast(*data); *data += 8; return answer; } std::string Streaming::bytesToHex(const std::vector &bytes) { std::ostringstream oss; oss << std::hex << std::setfill('0'); for (char c : bytes) { unsigned char byte = static_cast(c); oss << std::setw(2) << static_cast(byte); } return oss.str(); }