mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-09 21:17:18 +03:00
more crap
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
#ifndef PBCOMMON_H
|
||||
#define PBCOMMON_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <PbWriter.h>
|
||||
#include <optional>
|
||||
#include <PbReader.h>
|
||||
class BaseProtobufMessage
|
||||
{
|
||||
private:
|
||||
public:
|
||||
bool firstField = true;
|
||||
uint32_t lastMessagePosition;
|
||||
void parseFromVector(std::vector<uint8_t> const &rawData)
|
||||
{
|
||||
auto reader = std::make_shared<PbReader>(rawData);
|
||||
parseWithReader(reader);
|
||||
}
|
||||
void encodeToVector(std::vector<uint8_t> &rawData)
|
||||
{
|
||||
auto writer = std::make_shared<PbWriter>(rawData);
|
||||
encodeWithWriter(writer);
|
||||
}
|
||||
|
||||
void parseWithReader(std::shared_ptr<PbReader> reader)
|
||||
{
|
||||
firstField = true;
|
||||
while (reader->next())
|
||||
{
|
||||
if (!decodeField(reader))
|
||||
{
|
||||
reader->skip();
|
||||
} else {
|
||||
firstField = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual void encodeWithWriter(std::shared_ptr<PbWriter> writer) = 0;
|
||||
virtual bool decodeField(std::shared_ptr<PbReader> reader) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,42 +0,0 @@
|
||||
#ifndef PBREADER_H
|
||||
#define PBREADER_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <PbWireType.h>
|
||||
|
||||
class PbReader
|
||||
{
|
||||
private:
|
||||
std::vector<uint8_t> const &rawData;
|
||||
uint32_t currentWireValue = 0;
|
||||
uint64_t skipVarIntDump = 0;
|
||||
uint32_t nextFieldLength = 0;
|
||||
int64_t decodeZigzag(uint64_t value);
|
||||
|
||||
public:
|
||||
PbReader(std::vector<uint8_t> const &rawData);
|
||||
uint32_t maxPosition = 0;
|
||||
|
||||
PbWireType currentWireType = PbWireType::unknown;
|
||||
uint32_t currentTag = 0;
|
||||
uint32_t pos = 0;
|
||||
|
||||
template <typename T>
|
||||
T decodeVarInt();
|
||||
|
||||
template <typename T>
|
||||
T decodeFixed();
|
||||
|
||||
template <typename T>
|
||||
T decodeSVarInt();
|
||||
void decodeString(std::string &target);
|
||||
void decodeVector(std::vector<uint8_t> &target);
|
||||
|
||||
bool next();
|
||||
void skip();
|
||||
void resetMaxPosition();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,15 +0,0 @@
|
||||
#ifndef PBWIRETYPE_H
|
||||
#define PBWIRETYPE_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
enum class PbWireType : uint32_t
|
||||
{
|
||||
varint = 0, // int32/64, uint32/64, sint32/64, bool, enum
|
||||
fixed64 = 1, // fixed64, sfixed64, double
|
||||
length_delimited = 2, // string, bytes, nested messages, packed repeated fields
|
||||
fixed32 = 5, // fixed32, sfixed32, float
|
||||
unknown = 99
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,39 +0,0 @@
|
||||
#ifndef PBWRITER_H
|
||||
#define PBWRITER_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <PbWireType.h>
|
||||
|
||||
class PbWriter
|
||||
{
|
||||
private:
|
||||
std::vector<uint8_t> &rawData;
|
||||
uint32_t pos;
|
||||
uint32_t msgStartPos = 0;
|
||||
void encodeVarInt(uint32_t low, uint32_t high, int32_t atIndex = 0);
|
||||
uint32_t encodeZigzag32(int32_t value);
|
||||
uint64_t encodeZigzag64(int64_t value);
|
||||
public:
|
||||
PbWriter(std::vector<uint8_t> &rawData);
|
||||
|
||||
template <typename T>
|
||||
void encodeVarInt(T, int32_t atIndex = 0);
|
||||
template <typename T>
|
||||
void encodeFixed(T);
|
||||
void addSVarInt32(uint32_t tag, int32_t);
|
||||
void addSVarInt64(uint32_t tag, int64_t);
|
||||
void addString(uint32_t tag, std::string &target);
|
||||
void addVector(uint32_t tag, std::vector<uint8_t> &target);
|
||||
|
||||
template <typename T>
|
||||
void addVarInt(uint32_t tag, T intType);
|
||||
void addBool(uint32_t tag, bool value);
|
||||
|
||||
void addField(uint32_t tag, PbWireType wiretype);
|
||||
uint32_t startMessage();
|
||||
void finishMessage(uint32_t tag, uint32_t pos);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,37 +0,0 @@
|
||||
#ifndef PROTOBUF_H
|
||||
#define PROTOBUF_H
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include "protobuf.h"
|
||||
#include <PbReader.h>
|
||||
#include <PbCommon.h>
|
||||
|
||||
std::optional<AnyRef> findFieldWithProtobufTag(AnyRef ref, uint32_t tag);
|
||||
void decodeField(std::shared_ptr<PbReader> reader, AnyRef any);
|
||||
void decodeProtobuf(std::shared_ptr<PbReader> reader, AnyRef any);
|
||||
void encodeProtobuf(std::shared_ptr<PbWriter> writer, AnyRef any, uint32_t protobufTag = 0);
|
||||
|
||||
template <typename T>
|
||||
std::vector<uint8_t> encodePb(T & data)
|
||||
{
|
||||
auto ref = AnyRef::of(&data);
|
||||
std::vector<uint8_t> rawData;;
|
||||
auto writer = std::make_shared<PbWriter>(rawData);
|
||||
encodeProtobuf(writer, ref);
|
||||
|
||||
return rawData;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
T decodePb(std::vector<uint8_t> & bytes)
|
||||
{
|
||||
T data = {};
|
||||
auto ref = AnyRef::of(&data);
|
||||
auto writer = std::make_shared<PbReader>(bytes);
|
||||
decodeProtobuf(writer, ref);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user