2022-07-20 04:51:05 +00:00
|
|
|
#include "AMFDeserialize.h"
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include "Amf3.h"
|
2022-07-20 04:51:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AMF3 Reference document https://rtmp.veriskope.com/pdf/amf3-file-format-spec.pdf
|
|
|
|
* AMF3 Deserializer written by EmosewaMC
|
|
|
|
*/
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
AMFBaseValue* AMFDeserialize::Read(RakNet::BitStream* inStream) {
|
2022-07-20 04:51:05 +00:00
|
|
|
if (!inStream) return nullptr;
|
2023-05-13 22:22:00 +00:00
|
|
|
AMFBaseValue* returnValue = nullptr;
|
2022-07-20 04:51:05 +00:00
|
|
|
// Read in the value type from the bitStream
|
2023-06-02 11:44:49 +00:00
|
|
|
eAmf marker;
|
|
|
|
inStream->Read(marker);
|
2022-07-20 04:51:05 +00:00
|
|
|
// Based on the typing, create the value associated with that and return the base value class
|
|
|
|
switch (marker) {
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::Undefined: {
|
|
|
|
returnValue = new AMFBaseValue();
|
2022-07-20 04:51:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::Null: {
|
2022-07-20 04:51:05 +00:00
|
|
|
returnValue = new AMFNullValue();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::False: {
|
|
|
|
returnValue = new AMFBoolValue(false);
|
2022-07-20 04:51:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::True: {
|
|
|
|
returnValue = new AMFBoolValue(true);
|
2022-07-20 04:51:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::Integer: {
|
2022-07-20 04:51:05 +00:00
|
|
|
returnValue = ReadAmfInteger(inStream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::Double: {
|
2022-07-20 04:51:05 +00:00
|
|
|
returnValue = ReadAmfDouble(inStream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::String: {
|
2022-07-20 04:51:05 +00:00
|
|
|
returnValue = ReadAmfString(inStream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::Array: {
|
2022-07-20 04:51:05 +00:00
|
|
|
returnValue = ReadAmfArray(inStream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
// These values are unimplemented in the live client and will remain unimplemented
|
|
|
|
// unless someone modifies the client to allow serializing of these values.
|
|
|
|
case eAmf::XMLDoc:
|
|
|
|
case eAmf::Date:
|
|
|
|
case eAmf::Object:
|
|
|
|
case eAmf::XML:
|
|
|
|
case eAmf::ByteArray:
|
|
|
|
case eAmf::VectorInt:
|
|
|
|
case eAmf::VectorUInt:
|
|
|
|
case eAmf::VectorDouble:
|
|
|
|
case eAmf::VectorObject:
|
|
|
|
case eAmf::Dictionary: {
|
|
|
|
throw marker;
|
2022-07-20 04:51:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2023-05-13 22:22:00 +00:00
|
|
|
throw std::invalid_argument("Invalid AMF3 marker" + std::to_string(static_cast<int32_t>(marker)));
|
2022-07-20 04:51:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AMFDeserialize::ReadU29(RakNet::BitStream* inStream) {
|
|
|
|
bool byteFlag = true;
|
|
|
|
uint32_t actualNumber{};
|
|
|
|
uint8_t numberOfBytesRead{};
|
|
|
|
while (byteFlag && numberOfBytesRead < 4) {
|
|
|
|
uint8_t byte{};
|
|
|
|
inStream->Read(byte);
|
|
|
|
// Parse the byte
|
|
|
|
if (numberOfBytesRead < 3) {
|
|
|
|
byteFlag = byte & static_cast<uint8_t>(1 << 7);
|
|
|
|
byte = byte << 1UL;
|
|
|
|
}
|
|
|
|
// Combine the read byte with our current read in number
|
|
|
|
actualNumber <<= 8UL;
|
|
|
|
actualNumber |= static_cast<uint32_t>(byte);
|
|
|
|
// If we are not done reading in bytes, shift right 1 bit
|
|
|
|
if (numberOfBytesRead < 3) actualNumber = actualNumber >> 1UL;
|
|
|
|
numberOfBytesRead++;
|
|
|
|
}
|
|
|
|
return actualNumber;
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
const std::string AMFDeserialize::ReadString(RakNet::BitStream* inStream) {
|
2022-07-20 04:51:05 +00:00
|
|
|
auto length = ReadU29(inStream);
|
|
|
|
// Check if this is a reference
|
|
|
|
bool isReference = length % 2 == 1;
|
|
|
|
// Right shift by 1 bit to get index if reference or size of next string if value
|
|
|
|
length = length >> 1;
|
|
|
|
if (isReference) {
|
|
|
|
std::string value(length, 0);
|
|
|
|
inStream->Read(&value[0], length);
|
|
|
|
// Empty strings are never sent by reference
|
|
|
|
if (!value.empty()) accessedElements.push_back(value);
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
// Length is a reference to a previous index - use that as the read in value
|
2023-05-13 22:22:00 +00:00
|
|
|
return accessedElements.at(length);
|
2022-07-20 04:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
AMFBaseValue* AMFDeserialize::ReadAmfDouble(RakNet::BitStream* inStream) {
|
2022-07-20 04:51:05 +00:00
|
|
|
double value;
|
|
|
|
inStream->Read<double>(value);
|
2023-05-13 22:22:00 +00:00
|
|
|
return new AMFDoubleValue(value);
|
2022-07-20 04:51:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
AMFBaseValue* AMFDeserialize::ReadAmfArray(RakNet::BitStream* inStream) {
|
2022-07-20 04:51:05 +00:00
|
|
|
auto arrayValue = new AMFArrayValue();
|
2022-07-22 05:26:09 +00:00
|
|
|
|
|
|
|
// Read size of dense array
|
2022-07-20 04:51:05 +00:00
|
|
|
auto sizeOfDenseArray = (ReadU29(inStream) >> 1);
|
2023-05-13 22:22:00 +00:00
|
|
|
// Then read associative portion
|
2022-07-22 05:26:09 +00:00
|
|
|
while (true) {
|
|
|
|
auto key = ReadString(inStream);
|
2023-05-13 22:22:00 +00:00
|
|
|
// No more associative values when we encounter an empty string key
|
2022-07-22 05:26:09 +00:00
|
|
|
if (key.size() == 0) break;
|
2023-05-13 22:22:00 +00:00
|
|
|
arrayValue->Insert(key, Read(inStream));
|
2022-07-22 05:26:09 +00:00
|
|
|
}
|
|
|
|
// Finally read dense portion
|
|
|
|
for (uint32_t i = 0; i < sizeOfDenseArray; i++) {
|
2023-05-13 22:22:00 +00:00
|
|
|
arrayValue->Insert(i, Read(inStream));
|
2022-07-20 04:51:05 +00:00
|
|
|
}
|
|
|
|
return arrayValue;
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
AMFBaseValue* AMFDeserialize::ReadAmfString(RakNet::BitStream* inStream) {
|
|
|
|
return new AMFStringValue(ReadString(inStream));
|
2022-07-20 04:51:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 22:22:00 +00:00
|
|
|
AMFBaseValue* AMFDeserialize::ReadAmfInteger(RakNet::BitStream* inStream) {
|
|
|
|
return new AMFIntValue(ReadU29(inStream));
|
2022-07-20 04:51:05 +00:00
|
|
|
}
|