2021-12-05 17:54:36 +00:00
|
|
|
#include "AMFFormat_BitStream.h"
|
|
|
|
|
|
|
|
// Writes an AMFValue pointer to a RakNet::BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFValue*>(AMFValue* value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
if (value != nullptr) {
|
|
|
|
AMFValueType type = value->GetValueType();
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
switch (type) {
|
|
|
|
case AMFUndefined: {
|
|
|
|
AMFUndefinedValue* v = (AMFUndefinedValue*)value;
|
|
|
|
this->Write(*v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case AMFNull: {
|
|
|
|
AMFNullValue* v = (AMFNullValue*)value;
|
|
|
|
this->Write(*v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case AMFFalse: {
|
|
|
|
AMFFalseValue* v = (AMFFalseValue*)value;
|
|
|
|
this->Write(*v);
|
2022-07-28 13:39:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
case AMFTrue: {
|
|
|
|
AMFTrueValue* v = (AMFTrueValue*)value;
|
|
|
|
this->Write(*v);
|
|
|
|
break;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
case AMFInteger: {
|
|
|
|
AMFIntegerValue* v = (AMFIntegerValue*)value;
|
|
|
|
this->Write(*v);
|
2022-07-28 13:39:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
case AMFDouble: {
|
|
|
|
AMFDoubleValue* v = (AMFDoubleValue*)value;
|
|
|
|
this->Write(*v);
|
2022-07-28 13:39:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
case AMFString: {
|
|
|
|
AMFStringValue* v = (AMFStringValue*)value;
|
|
|
|
this->Write(*v);
|
2022-07-28 13:39:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
case AMFXMLDoc: {
|
|
|
|
AMFXMLDocValue* v = (AMFXMLDocValue*)value;
|
|
|
|
this->Write(*v);
|
|
|
|
break;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
case AMFDate: {
|
|
|
|
AMFDateValue* v = (AMFDateValue*)value;
|
|
|
|
this->Write(*v);
|
|
|
|
break;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
case AMFArray: {
|
|
|
|
this->Write((AMFArrayValue*)value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A private function to write an value to a RakNet::BitStream
|
|
|
|
* RakNet writes in the correct byte order - do not reverse this.
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void WriteUInt29(RakNet::BitStream* bs, uint32_t v) {
|
2022-07-22 05:26:09 +00:00
|
|
|
unsigned char b4 = (unsigned char)v;
|
|
|
|
if (v < 0x00200000) {
|
|
|
|
b4 = b4 & 0x7F;
|
|
|
|
if (v > 0x7F) {
|
|
|
|
unsigned char b3;
|
|
|
|
v = v >> 7;
|
|
|
|
b3 = ((unsigned char)(v)) | 0x80;
|
|
|
|
if (v > 0x7F) {
|
|
|
|
unsigned char b2;
|
|
|
|
v = v >> 7;
|
|
|
|
b2 = ((unsigned char)(v)) | 0x80;
|
|
|
|
bs->Write(b2);
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
bs->Write(b3);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsigned char b1;
|
|
|
|
unsigned char b2;
|
|
|
|
unsigned char b3;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
v = v >> 8;
|
|
|
|
b3 = ((unsigned char)(v)) | 0x80;
|
|
|
|
v = v >> 7;
|
|
|
|
b2 = ((unsigned char)(v)) | 0x80;
|
|
|
|
v = v >> 7;
|
|
|
|
b1 = ((unsigned char)(v)) | 0x80;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
bs->Write(b1);
|
|
|
|
bs->Write(b2);
|
|
|
|
bs->Write(b3);
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
bs->Write(b4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a flag number to a RakNet::BitStream
|
|
|
|
* RakNet writes in the correct byte order - do not reverse this.
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void WriteFlagNumber(RakNet::BitStream* bs, uint32_t v) {
|
2022-07-22 05:26:09 +00:00
|
|
|
v = (v << 1) | 0x01;
|
|
|
|
WriteUInt29(bs, v);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
/**
|
|
|
|
* Writes an AMFString to a RakNet::BitStream
|
|
|
|
*
|
|
|
|
* RakNet writes in the correct byte order - do not reverse this.
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void WriteAMFString(RakNet::BitStream* bs, const std::string& str) {
|
2022-07-22 05:26:09 +00:00
|
|
|
WriteFlagNumber(bs, (uint32_t)str.size());
|
|
|
|
bs->Write(str.c_str(), (uint32_t)str.size());
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
/**
|
|
|
|
* Writes an U16 to a bitstream
|
|
|
|
*
|
|
|
|
* RakNet writes in the correct byte order - do not reverse this.
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void WriteAMFU16(RakNet::BitStream* bs, uint16_t value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
bs->Write(value);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
/**
|
|
|
|
* Writes an U32 to a bitstream
|
|
|
|
*
|
|
|
|
* RakNet writes in the correct byte order - do not reverse this.
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void WriteAMFU32(RakNet::BitStream* bs, uint32_t value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
bs->Write(value);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
/**
|
|
|
|
* Writes an U64 to a bitstream
|
|
|
|
*
|
|
|
|
* RakNet writes in the correct byte order - do not reverse this.
|
|
|
|
*/
|
2021-12-05 17:54:36 +00:00
|
|
|
void WriteAMFU64(RakNet::BitStream* bs, uint64_t value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
bs->Write(value);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Writes an AMFUndefinedValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFUndefinedValue>(AMFUndefinedValue value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
this->Write(AMFUndefined);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFNullValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFNullValue>(AMFNullValue value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
this->Write(AMFNull);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFFalseValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFFalseValue>(AMFFalseValue value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
this->Write(AMFFalse);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFTrueValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFTrueValue>(AMFTrueValue value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
this->Write(AMFTrue);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFIntegerValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFIntegerValue>(AMFIntegerValue value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
this->Write(AMFInteger);
|
|
|
|
WriteUInt29(this, value.GetIntegerValue());
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFDoubleValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFDoubleValue>(AMFDoubleValue value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
this->Write(AMFDouble);
|
|
|
|
double d = value.GetDoubleValue();
|
|
|
|
WriteAMFU64(this, *((unsigned long long*) & d));
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFStringValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFStringValue>(AMFStringValue value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
this->Write(AMFString);
|
|
|
|
std::string v = value.GetStringValue();
|
|
|
|
WriteAMFString(this, v);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFXMLDocValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFXMLDocValue>(AMFXMLDocValue value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
this->Write(AMFXMLDoc);
|
|
|
|
std::string v = value.GetXMLDocValue();
|
|
|
|
WriteAMFString(this, v);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFDateValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFDateValue>(AMFDateValue value) {
|
2022-07-22 05:26:09 +00:00
|
|
|
this->Write(AMFDate);
|
|
|
|
uint64_t date = value.GetDateValue();
|
|
|
|
WriteAMFU64(this, date);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFArrayValue to BitStream
|
|
|
|
template<>
|
2022-07-22 05:26:09 +00:00
|
|
|
void RakNet::BitStream::Write<AMFArrayValue*>(AMFArrayValue* value) {
|
|
|
|
this->Write(AMFArray);
|
|
|
|
uint32_t denseSize = value->GetDenseValueSize();
|
|
|
|
WriteFlagNumber(this, denseSize);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
_AMFArrayMap_::iterator it = value->GetAssociativeIteratorValueBegin();
|
|
|
|
_AMFArrayMap_::iterator end = value->GetAssociativeIteratorValueEnd();
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
while (it != end) {
|
|
|
|
WriteAMFString(this, it->first);
|
|
|
|
this->Write(it->second);
|
|
|
|
it++;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
this->Write(AMFNull);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
if (denseSize > 0) {
|
|
|
|
_AMFArrayList_::iterator it2 = value->GetDenseIteratorBegin();
|
|
|
|
_AMFArrayList_::iterator end2 = value->GetDenseIteratorEnd();
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-22 05:26:09 +00:00
|
|
|
while (it2 != end2) {
|
|
|
|
this->Write(*it2);
|
|
|
|
it2++;
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|