#include "Serializer.h" #include #include #include "Base64.h" void Serializer::writeByte(char val) { buffer_.push_back(val); } void Serializer::writeInt(int val) { int b1 = (val & 0x000000FF) >> 0; int b2 = (val & 0x0000FF00) >> 8; int b3 = (val & 0x00FF0000) >> 16; int b4 = (val & 0xFF000000) >> 24; writeByte(b1); writeByte(b2); writeByte(b3); writeByte(b4); } void Serializer::writeLong(long val) { long i1 = val & 0xFFFFFFFF; long i2 = (val - i1) / 0x100000000; writeInt(i1); writeInt(i2); } void Serializer::writeColor(int val) { switch (val) { case 0x1: { // Black writeByte(0xFF); writeByte(0xFF); writeByte(0xFF); writeByte(0xFF); break; } case 0x2: { // White writeByte(0x00); writeByte(0x00); writeByte(0x00); writeByte(0xFF); break; } case 0x3: { // Red writeByte(0xFF); writeByte(0x00); writeByte(0x00); writeByte(0xFF); break; } case 0x4: { // Purple writeByte(0x80); writeByte(0x00); writeByte(0x80); writeByte(0xFF); break; } case 0x5: { // Green writeByte(0x00); writeByte(0xFF); writeByte(0x00); writeByte(0xFF); break; } case 0x6: { // Cyan writeByte(0x00); writeByte(0xFF); writeByte(0xFF); writeByte(0xFF); break; } case 0x7: { // Magenta writeByte(0xFF); writeByte(0x00); writeByte(0xFF); writeByte(0xFF); break; } case 0x8: { // Yellow writeByte(0xFF); writeByte(0xFF); writeByte(0x00); writeByte(0xFF); break; } case 0x9: { // Blue writeByte(0x00); writeByte(0x00); writeByte(0xFF); writeByte(0xFF); break; } case 0xA: { // Orange writeByte(0xFF); writeByte(0x8C); writeByte(0x00); writeByte(0xFF); break; } case 0xF: { // X???? break; } } } void Serializer::writeString(const std::string& val) { writeInt(val.length()); for (char ch : val) { writeByte(ch); } } std::string Serializer::str() const { int i = 0; for (char ch : buffer_) { std::cout << std::hex << static_cast(ch); if (i++ % 4 == 3) { std::cout << " "; } } std::cout << std::endl; return "_" + macaron::Base64::Encode(buffer_); }