mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-09 13:07:03 +03:00
more crap
This commit is contained in:
@@ -1,116 +0,0 @@
|
||||
#include "PbReader.h"
|
||||
#include <iostream>
|
||||
|
||||
PbReader::PbReader(std::vector<uint8_t> const &rawData) : rawData(rawData)
|
||||
{
|
||||
maxPosition = rawData.size();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T PbReader::decodeVarInt()
|
||||
{
|
||||
uint8_t byte;
|
||||
uint_fast8_t bitpos = 0;
|
||||
uint64_t storago = 0;
|
||||
do
|
||||
{
|
||||
byte = this->rawData[pos];
|
||||
pos++;
|
||||
|
||||
storago |= (uint64_t)(byte & 0x7F) << bitpos;
|
||||
bitpos = (uint_fast8_t)(bitpos + 7);
|
||||
} while (byte & 0x80);
|
||||
return static_cast<T>(storago);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T PbReader::decodeFixed()
|
||||
{
|
||||
pos += sizeof(T);
|
||||
return *(T*)(&this->rawData[pos - sizeof(T)]);
|
||||
}
|
||||
|
||||
|
||||
template int32_t PbReader::decodeFixed();
|
||||
template int64_t PbReader::decodeFixed();
|
||||
|
||||
template uint32_t PbReader::decodeVarInt();
|
||||
template int64_t PbReader::decodeVarInt();
|
||||
template int32_t PbReader::decodeVarInt();
|
||||
template bool PbReader::decodeVarInt();
|
||||
|
||||
void PbReader::resetMaxPosition()
|
||||
{
|
||||
maxPosition = rawData.size();
|
||||
}
|
||||
|
||||
void PbReader::decodeString(std::string &target)
|
||||
{
|
||||
nextFieldLength = decodeVarInt<uint32_t>();
|
||||
target.resize(nextFieldLength);
|
||||
// std::cout << "rawData.size() = " << rawData.size() << " pos = " << pos << " nextFieldLength =" << nextFieldLength;
|
||||
// printf("\n%d, \n", currentTag);
|
||||
// if (pos + nextFieldLength >= rawData.size())
|
||||
// {
|
||||
// std::cout << " \nBAD -- pos + nextFieldLength >= rawData.size() MSVC IS LITERLALLY SHAKING AND CRYING RN";
|
||||
// }
|
||||
// std::cout << std::endl;
|
||||
std::copy(rawData.begin() + pos, rawData.begin() + pos + nextFieldLength, target.begin());
|
||||
pos += nextFieldLength;
|
||||
}
|
||||
|
||||
void PbReader::decodeVector(std::vector<uint8_t> &target)
|
||||
{
|
||||
nextFieldLength = decodeVarInt<uint32_t>();
|
||||
target.resize(nextFieldLength);
|
||||
std::copy(rawData.begin() + pos, rawData.begin() + pos + nextFieldLength, target.begin());
|
||||
pos += nextFieldLength;
|
||||
}
|
||||
|
||||
bool PbReader::next()
|
||||
{
|
||||
if (pos >= maxPosition)
|
||||
return false;
|
||||
|
||||
currentWireValue = decodeVarInt<uint32_t>();
|
||||
currentTag = currentWireValue >> 3U;
|
||||
currentWireType = PbWireType(currentWireValue & 0x07U);
|
||||
return true;
|
||||
}
|
||||
|
||||
int64_t PbReader::decodeZigzag(uint64_t value)
|
||||
{
|
||||
return static_cast<int64_t>((value >> 1U) ^ static_cast<uint64_t>(-static_cast<int64_t>(value & 1U)));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T PbReader::decodeSVarInt()
|
||||
{
|
||||
skipVarIntDump = decodeVarInt<uint64_t>();
|
||||
return static_cast<T>(decodeZigzag(skipVarIntDump));
|
||||
}
|
||||
|
||||
template int32_t PbReader::decodeSVarInt();
|
||||
template int64_t PbReader::decodeSVarInt();
|
||||
|
||||
void PbReader::skip()
|
||||
{
|
||||
switch (currentWireType)
|
||||
{
|
||||
case PbWireType::varint:
|
||||
skipVarIntDump = decodeVarInt<uint64_t>();
|
||||
break;
|
||||
case PbWireType::fixed64:
|
||||
pos += 8;
|
||||
break;
|
||||
case PbWireType::length_delimited:
|
||||
nextFieldLength = decodeVarInt<uint32_t>();
|
||||
pos += nextFieldLength;
|
||||
break;
|
||||
case PbWireType::fixed32:
|
||||
pos += 4;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
#include "PbWriter.h"
|
||||
|
||||
PbWriter::PbWriter(std::vector<uint8_t> &rawData) : rawData(rawData)
|
||||
{
|
||||
}
|
||||
|
||||
void PbWriter::encodeVarInt(uint32_t low, uint32_t high, int32_t indexOffset)
|
||||
{
|
||||
size_t i = 0;
|
||||
uint8_t byte = (uint8_t)(low & 0x7F);
|
||||
low >>= 7;
|
||||
|
||||
while (i < 4 && (low != 0 || high != 0))
|
||||
{
|
||||
byte |= 0x80;
|
||||
rawData.insert(rawData.end() + indexOffset, byte);
|
||||
i++;
|
||||
byte = (uint8_t)(low & 0x7F);
|
||||
low >>= 7;
|
||||
}
|
||||
|
||||
if (high)
|
||||
{
|
||||
byte = (uint8_t)(byte | ((high & 0x07) << 4));
|
||||
high >>= 3;
|
||||
|
||||
while (high)
|
||||
{
|
||||
byte |= 0x80;
|
||||
rawData.insert(rawData.end() + indexOffset, byte);
|
||||
i++;
|
||||
byte = (uint8_t)(high & 0x7F);
|
||||
high >>= 7;
|
||||
}
|
||||
}
|
||||
|
||||
rawData.insert(rawData.end() + indexOffset, byte);
|
||||
}
|
||||
|
||||
template void PbWriter::encodeVarInt(uint8_t, int32_t);
|
||||
template void PbWriter::encodeVarInt(uint32_t, int32_t);
|
||||
template void PbWriter::encodeVarInt(uint64_t, int32_t);
|
||||
template void PbWriter::encodeVarInt(long long, int32_t);
|
||||
|
||||
template <typename T>
|
||||
void PbWriter::encodeVarInt(T data, int32_t offset)
|
||||
{
|
||||
auto value = static_cast<uint64_t>(data);
|
||||
if (value <= 0x7F)
|
||||
{
|
||||
rawData.insert(rawData.end() + offset, (uint8_t)value);
|
||||
}
|
||||
else
|
||||
{
|
||||
encodeVarInt((uint32_t)value, (uint32_t)(value >> 32), offset);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t PbWriter::encodeZigzag32(int32_t value) {
|
||||
return (static_cast<uint32_t>(value) << 1U) ^ static_cast<uint32_t>(-static_cast<int32_t>(static_cast<uint32_t>(value) >> 31U));
|
||||
}
|
||||
|
||||
uint64_t PbWriter::encodeZigzag64(int64_t value) {
|
||||
return (static_cast<uint64_t>(value) << 1U) ^ static_cast<uint64_t>(-static_cast<int64_t>(static_cast<uint64_t>(value) >> 63U));
|
||||
}
|
||||
|
||||
void PbWriter::addSVarInt32(uint32_t tag, int32_t data) {
|
||||
auto val = encodeZigzag32(data);
|
||||
addVarInt(tag, val);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void PbWriter::encodeFixed(T data) {
|
||||
auto val = reinterpret_cast<const char*>(&data);
|
||||
rawData.insert(rawData.end(), val, val + sizeof(T));
|
||||
}
|
||||
|
||||
template void PbWriter::encodeFixed(int64_t);
|
||||
template void PbWriter::encodeFixed(int32_t);
|
||||
|
||||
void PbWriter::addSVarInt64(uint32_t tag, int64_t data) {
|
||||
auto val = encodeZigzag64(data);
|
||||
addVarInt(tag, val);
|
||||
}
|
||||
|
||||
void PbWriter::addString(uint32_t tag, std::string &target)
|
||||
{
|
||||
addField(tag, PbWireType::length_delimited);
|
||||
uint32_t stringSize = target.size();
|
||||
encodeVarInt(stringSize);
|
||||
|
||||
rawData.insert(rawData.end(), target.begin(), target.end());
|
||||
}
|
||||
|
||||
void PbWriter::addVector(uint32_t tag, std::vector<uint8_t> &target)
|
||||
{
|
||||
addField(tag, PbWireType::length_delimited);
|
||||
uint32_t vectorSize = target.size();
|
||||
encodeVarInt(vectorSize);
|
||||
|
||||
rawData.insert(rawData.end(), target.begin(), target.end());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void PbWriter::addVarInt(uint32_t tag, T intType)
|
||||
{
|
||||
addField(tag, PbWireType::varint);
|
||||
encodeVarInt(intType);
|
||||
}
|
||||
|
||||
void PbWriter::addBool(uint32_t tag, bool value)
|
||||
{
|
||||
addField(tag, PbWireType::varint);
|
||||
rawData.push_back(char(value));
|
||||
}
|
||||
|
||||
template void PbWriter::addVarInt(uint32_t, uint8_t);
|
||||
template void PbWriter::addVarInt(uint32_t, uint32_t);
|
||||
template void PbWriter::addVarInt(uint32_t, uint64_t);
|
||||
template void PbWriter::addVarInt(uint32_t, int64_t);
|
||||
template void PbWriter::addVarInt(uint32_t, bool);
|
||||
|
||||
void PbWriter::addField(uint32_t tag, PbWireType wiretype)
|
||||
{
|
||||
const uint32_t value = (tag << 3U) | uint32_t(wiretype);
|
||||
encodeVarInt(value);
|
||||
}
|
||||
|
||||
uint32_t PbWriter::startMessage()
|
||||
{
|
||||
return rawData.size();
|
||||
}
|
||||
|
||||
void PbWriter::finishMessage(uint32_t tag, uint32_t lastMessagePosition)
|
||||
{
|
||||
uint32_t finalMessageSize = rawData.size() - lastMessagePosition;
|
||||
uint32_t msgHeader = (tag << 3U) | uint32_t(PbWireType::length_delimited);
|
||||
|
||||
int32_t offset = -finalMessageSize;
|
||||
encodeVarInt(msgHeader, offset);
|
||||
encodeVarInt(finalMessageSize, offset);
|
||||
}
|
||||
@@ -1,193 +0,0 @@
|
||||
#include "ProtoHelper.h"
|
||||
|
||||
|
||||
std::optional<AnyRef> findFieldWithProtobufTag(AnyRef ref, uint32_t tag)
|
||||
{
|
||||
auto info = ref.reflectType();
|
||||
for (int i = 0; i < info->fields.size(); i++)
|
||||
{
|
||||
|
||||
if (tag == info->fields[i].protobufTag)
|
||||
{
|
||||
return std::make_optional(ref.getField(i));
|
||||
}
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void decodeField(std::shared_ptr<PbReader> reader, AnyRef any)
|
||||
{
|
||||
auto fieldInfo = any.reflectType();
|
||||
|
||||
if (fieldInfo->kind == ReflectTypeKind::Optional)
|
||||
{
|
||||
auto optionalRef = AnyOptionalRef(any);
|
||||
optionalRef.emplaceEmpty();
|
||||
return decodeField(reader, optionalRef.get());
|
||||
}
|
||||
|
||||
if (fieldInfo->kind == ReflectTypeKind::Class)
|
||||
{
|
||||
// Handle submessage
|
||||
auto lastMaxPosition = reader->maxPosition;
|
||||
auto messageSize = reader->decodeVarInt<uint32_t>();
|
||||
|
||||
reader->maxPosition = messageSize + reader->pos;
|
||||
while (reader->next())
|
||||
{
|
||||
auto res = findFieldWithProtobufTag(any, reader->currentTag);
|
||||
if (res.has_value())
|
||||
{
|
||||
decodeField(reader, res.value());
|
||||
}
|
||||
else
|
||||
{
|
||||
reader->skip();
|
||||
}
|
||||
}
|
||||
reader->maxPosition = lastMaxPosition;
|
||||
return;
|
||||
} else if (any.is<std::vector<uint8_t>>())
|
||||
{
|
||||
reader->decodeVector(*any.as<std::vector<uint8_t>>());
|
||||
}
|
||||
// Handle repeated
|
||||
else if (fieldInfo->kind == ReflectTypeKind::Vector)
|
||||
{
|
||||
auto aVec = AnyVectorRef(any);
|
||||
aVec.emplace_back();
|
||||
auto value = aVec.at(aVec.size() - 1);
|
||||
auto valInfo = value.reflectType();
|
||||
|
||||
// Represents packed repeated encoding
|
||||
if (valInfo->kind == ReflectTypeKind::Primitive && !value.is<std::string>() && !value.is<std::vector<uint8_t>>())
|
||||
{
|
||||
// *any.as<int64_t>() = reader->decodeVarInt<int64_t>();
|
||||
reader->skip();
|
||||
}
|
||||
else
|
||||
{
|
||||
decodeField(reader, value);
|
||||
}
|
||||
}
|
||||
else if (fieldInfo->kind == ReflectTypeKind::Enum)
|
||||
{
|
||||
*any.as<uint32_t>() = reader->decodeVarInt<uint32_t>();
|
||||
}
|
||||
else if (any.is<std::string>())
|
||||
{
|
||||
reader->decodeString(*any.as<std::string>());
|
||||
}
|
||||
else if (any.is<bool>())
|
||||
{
|
||||
*any.as<bool>() = reader->decodeVarInt<bool>();
|
||||
}
|
||||
else if (any.is<uint32_t>())
|
||||
{
|
||||
*any.as<uint32_t>() = reader->decodeVarInt<uint32_t>();
|
||||
}
|
||||
else if (any.is<int64_t>())
|
||||
{
|
||||
*any.as<int64_t>() = reader->decodeVarInt<int64_t>();
|
||||
}
|
||||
else if (any.is<int32_t>())
|
||||
{
|
||||
*any.as<int32_t>() = reader->decodeVarInt<int32_t>();
|
||||
}
|
||||
else
|
||||
{
|
||||
reader->skip();
|
||||
}
|
||||
}
|
||||
|
||||
void decodeProtobuf(std::shared_ptr<PbReader> reader, AnyRef any)
|
||||
{
|
||||
while (reader->next())
|
||||
{
|
||||
auto res = findFieldWithProtobufTag(any, reader->currentTag);
|
||||
if (res.has_value())
|
||||
{
|
||||
decodeField(reader, res.value());
|
||||
}
|
||||
else
|
||||
{
|
||||
reader->skip();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void encodeProtobuf(std::shared_ptr<PbWriter> writer, AnyRef any, uint32_t protobufTag)
|
||||
{
|
||||
auto info = any.reflectType();
|
||||
|
||||
// Handle optionals, only encode if have value
|
||||
if (info->kind == ReflectTypeKind::Optional)
|
||||
{
|
||||
auto optionalRef = AnyOptionalRef(any);
|
||||
if (!optionalRef.has_value())
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
return encodeProtobuf(writer, optionalRef.get(), protobufTag);
|
||||
}
|
||||
}
|
||||
if (info->kind == ReflectTypeKind::Class)
|
||||
{
|
||||
uint32_t startMsgPosition;
|
||||
// 0 - default value, indicating top level message
|
||||
if (protobufTag > 0)
|
||||
{
|
||||
startMsgPosition = writer->startMessage();
|
||||
}
|
||||
|
||||
for (int i = 0; i < info->fields.size(); i++)
|
||||
{
|
||||
auto field = any.getField(i);
|
||||
encodeProtobuf(writer, field, info->fields[i].protobufTag);
|
||||
}
|
||||
|
||||
if (protobufTag > 0)
|
||||
{
|
||||
writer->finishMessage(protobufTag, startMsgPosition);
|
||||
}
|
||||
} else if (any.is<std::vector<uint8_t>>())
|
||||
{
|
||||
writer->addVector(protobufTag, *any.as<std::vector<uint8_t>>());
|
||||
}
|
||||
else if (info->kind == ReflectTypeKind::Vector) {
|
||||
auto aVec = AnyVectorRef(any);
|
||||
auto size = aVec.size();
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
auto valueAt = aVec.at(i);
|
||||
encodeProtobuf(writer, valueAt, protobufTag);
|
||||
}
|
||||
}
|
||||
else if (info->kind == ReflectTypeKind::Enum) {
|
||||
writer->addVarInt<uint32_t>(protobufTag, *any.as<uint32_t>());
|
||||
}
|
||||
else if (info->kind == ReflectTypeKind::Primitive)
|
||||
{
|
||||
if (any.is<std::string>())
|
||||
{
|
||||
writer->addString(protobufTag, *any.as<std::string>());
|
||||
}
|
||||
else if (any.is<uint32_t>())
|
||||
{
|
||||
writer->addVarInt<uint32_t>(protobufTag, *any.as<uint32_t>());
|
||||
}
|
||||
else if (any.is<uint64_t>())
|
||||
{
|
||||
writer->addVarInt<uint64_t>(protobufTag, *any.as<uint64_t>());
|
||||
}
|
||||
else if (any.is<int64_t>()) {
|
||||
writer->addVarInt<int64_t>(protobufTag, *any.as<int64_t>());
|
||||
} else if (any.is<bool>())
|
||||
{
|
||||
writer->addVarInt<bool>(protobufTag, *any.as<bool>());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user