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
|
|
|
|
*/
|
|
|
|
|
2024-11-19 02:45:24 +00:00
|
|
|
std::unique_ptr<AMFBaseValue> AMFDeserialize::Read(RakNet::BitStream& inStream) {
|
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;
|
2024-02-26 14:17:22 +00:00
|
|
|
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) {
|
2024-11-19 02:45:24 +00:00
|
|
|
case eAmf::Undefined:
|
|
|
|
return std::make_unique<AMFBaseValue>();
|
|
|
|
case eAmf::Null:
|
|
|
|
return std::make_unique<AMFNullValue>();
|
|
|
|
case eAmf::False:
|
|
|
|
return std::make_unique<AMFBoolValue>(false);
|
|
|
|
case eAmf::True:
|
|
|
|
return std::make_unique<AMFBoolValue>(true);
|
|
|
|
case eAmf::Integer:
|
|
|
|
return ReadAmfInteger(inStream);
|
|
|
|
case eAmf::Double:
|
|
|
|
return ReadAmfDouble(inStream);
|
|
|
|
case eAmf::String:
|
|
|
|
return ReadAmfString(inStream);
|
|
|
|
case eAmf::Array:
|
|
|
|
return ReadAmfArray(inStream);
|
2022-07-20 04:51:05 +00:00
|
|
|
|
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:
|
2024-11-19 02:45:24 +00:00
|
|
|
[[fallthrough]];
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::Date:
|
2024-11-19 02:45:24 +00:00
|
|
|
[[fallthrough]];
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::Object:
|
2024-11-19 02:45:24 +00:00
|
|
|
[[fallthrough]];
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::XML:
|
2024-11-19 02:45:24 +00:00
|
|
|
[[fallthrough]];
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::ByteArray:
|
2024-11-19 02:45:24 +00:00
|
|
|
[[fallthrough]];
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::VectorInt:
|
2024-11-19 02:45:24 +00:00
|
|
|
[[fallthrough]];
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::VectorUInt:
|
2024-11-19 02:45:24 +00:00
|
|
|
[[fallthrough]];
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::VectorDouble:
|
2024-11-19 02:45:24 +00:00
|
|
|
[[fallthrough]];
|
2023-05-13 22:22:00 +00:00
|
|
|
case eAmf::VectorObject:
|
2024-11-19 02:45:24 +00:00
|
|
|
[[fallthrough]];
|
|
|
|
case eAmf::Dictionary:
|
2023-05-13 22:22:00 +00:00
|
|
|
throw marker;
|
2022-07-20 04:51:05 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 14:17:22 +00:00
|
|
|
uint32_t AMFDeserialize::ReadU29(RakNet::BitStream& inStream) {
|
2022-07-20 04:51:05 +00:00
|
|
|
bool byteFlag = true;
|
|
|
|
uint32_t actualNumber{};
|
|
|
|
uint8_t numberOfBytesRead{};
|
|
|
|
while (byteFlag && numberOfBytesRead < 4) {
|
|
|
|
uint8_t byte{};
|
2024-02-26 14:17:22 +00:00
|
|
|
inStream.Read(byte);
|
2022-07-20 04:51:05 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2024-02-26 14:17:22 +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);
|
2024-02-26 14:17:22 +00:00
|
|
|
inStream.Read(&value[0], length);
|
2022-07-20 04:51:05 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 02:45:24 +00:00
|
|
|
std::unique_ptr<AMFDoubleValue> AMFDeserialize::ReadAmfDouble(RakNet::BitStream& inStream) {
|
2022-07-20 04:51:05 +00:00
|
|
|
double value;
|
2024-02-26 14:17:22 +00:00
|
|
|
inStream.Read<double>(value);
|
2024-11-19 02:45:24 +00:00
|
|
|
return std::make_unique<AMFDoubleValue>(value);
|
2022-07-20 04:51:05 +00:00
|
|
|
}
|
|
|
|
|
2024-11-19 02:45:24 +00:00
|
|
|
std::unique_ptr<AMFArrayValue> AMFDeserialize::ReadAmfArray(RakNet::BitStream& inStream) {
|
|
|
|
auto arrayValue = std::make_unique<AMFArrayValue>();
|
2022-07-22 05:26:09 +00:00
|
|
|
|
|
|
|
// Read size of dense array
|
2024-02-27 07:29:51 +00:00
|
|
|
const 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) {
|
2024-02-27 07:29:51 +00:00
|
|
|
const 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;
|
|
|
|
}
|
|
|
|
|
2024-11-19 02:45:24 +00:00
|
|
|
std::unique_ptr<AMFStringValue> AMFDeserialize::ReadAmfString(RakNet::BitStream& inStream) {
|
|
|
|
return std::make_unique<AMFStringValue>(ReadString(inStream));
|
2022-07-20 04:51:05 +00:00
|
|
|
}
|
|
|
|
|
2024-11-19 02:45:24 +00:00
|
|
|
std::unique_ptr<AMFIntValue> AMFDeserialize::ReadAmfInteger(RakNet::BitStream& inStream) {
|
|
|
|
return std::make_unique<AMFIntValue>(ReadU29(inStream)); // NOTE: NARROWING CONVERSION FROM UINT TO INT. IS THIS INTENDED?
|
2022-07-20 04:51:05 +00:00
|
|
|
}
|