Documented and nicified AMF parsing. Updated all existing code for this change, except for RTMP connector (which gets a rewrite, like, today).

This commit is contained in:
Thulinma 2011-04-10 22:25:51 +02:00
parent 69b1f85972
commit e416612a33
2 changed files with 358 additions and 290 deletions

View file

@ -1,264 +1,256 @@
#include <vector>
#include <string.h>
#include <string>
#include "amf.h"
#define AMF0_NUMBER 0x00
#define AMF0_BOOL 0x01
#define AMF0_STRING 0x02
#define AMF0_OBJECT 0x03
#define AMF0_MOVIECLIP 0x04
#define AMF0_NULL 0x05
#define AMF0_UNDEFINED 0x06
#define AMF0_REFERENCE 0x07
#define AMF0_ECMA_ARRAY 0x08
#define AMF0_OBJ_END 0x09
#define AMF0_STRICT_ARRAY 0x0A
#define AMF0_DATE 0x0B
#define AMF0_LONGSTRING 0x0C
#define AMF0_UNSUPPORTED 0x0D
#define AMF0_RECORDSET 0x0E
#define AMF0_XMLDOC 0x0F
#define AMF0_TYPED_OBJ 0x10
#define AMF0_UPGRADE 0x11
#define AMF0_DDV_CONTAINER 0xFF
/// Returns the std::string Indice for the current object, if available.
/// Returns an empty string if no indice exists.
std::string AMF::Object::Indice(){return myIndice;};
class AMFType {
public:
std::string Indice(){return myIndice;};
unsigned char GetType(){return myType;};
double NumValue(){return numval;};
std::string StrValue(){return strval;};
const char * Str(){return strval.c_str();};
int hasContent(){
if (!contents){return 0;}
return contents->size();
};
void addContent(AMFType c){if (contents != 0){contents->push_back(c);}};
AMFType* getContentP(int i){if (contents != 0){return &contents->at(i);}else{return 0;}};
AMFType getContent(int i){if (contents != 0){return contents->at(i);}else{return AMFType("error");}};
AMFType* getContentP(std::string s){
if (contents != 0){
for (std::vector<AMFType>::iterator it = contents->begin(); it != contents->end(); it++){
if (it->Indice() == s){
return &(*it);
}
/// Returns the AMF::obj0type AMF0 object type for this object.
AMF::obj0type AMF::Object::GetType(){return myType;};
/// Returns the numeric value of this object, if available.
/// If this object holds no numeric value, 0 is returned.
double AMF::Object::NumValue(){return numval;};
/// Returns the std::string value of this object, if available.
/// If this object holds no string value, an empty string is returned.
std::string AMF::Object::StrValue(){return strval;};
/// Returns the C-string value of this object, if available.
/// If this object holds no string value, an empty C-string is returned.
const char * AMF::Object::Str(){return strval.c_str();};
/// Returns a count of the amount of objects this object currently holds.
/// If this object is not a container type, this function will always return 0.
int AMF::Object::hasContent(){return contents.size();};
/// Adds an AMF::Object to this object. Works for all types, but only makes sense for container types.
void AMF::Object::addContent(AMF::Object c){contents.push_back(c);};
/// Returns a pointer to the object held at indice i.
/// Returns AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice.
/// \param i The indice of the object in this container.
AMF::Object* AMF::Object::getContentP(int i){return &contents.at(i);};
/// Returns a copy of the object held at indice i.
/// Returns a AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice.
/// \param i The indice of the object in this container.
AMF::Object AMF::Object::getContent(int i){return contents.at(i);};
/// Returns a pointer to the object held at indice s.
/// Returns NULL if no object is held at this indice.
/// \param s The indice of the object in this container.
AMF::Object* AMF::Object::getContentP(std::string s){
for (std::vector<AMF::Object>::iterator it = contents.begin(); it != contents.end(); it++){
if (it->Indice() == s){return &(*it);}
}
return this;
};
/// Returns a copy of the object held at indice s.
/// Returns a AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice.
/// \param s The indice of the object in this container.
AMF::Object AMF::Object::getContent(std::string s){
for (std::vector<AMF::Object>::iterator it = contents.begin(); it != contents.end(); it++){
if (it->Indice() == s){return *it;}
}
return AMF::Object("error", AMF0_DDV_CONTAINER);
};
/// Constructor for numeric objects.
/// The object type is by default AMF::AMF0_NUMBER, but this can be forced to a different value.
/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic.
/// \param val The numeric value of this object. Numeric AMF0 objects only support double-type values.
/// \param setType The object type to force this object to.
AMF::Object::Object(std::string indice, double val, AMF::obj0type setType){//num type initializer
myIndice = indice;
myType = setType;
strval = "";
numval = val;
};
/// Constructor for string objects.
/// The object type is by default AMF::AMF0_STRING, but this can be forced to a different value.
/// There is no need to manually change the type to AMF::AMF0_LONGSTRING, this will be done automatically.
/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic.
/// \param val The string value of this object.
/// \param setType The object type to force this object to.
AMF::Object::Object(std::string indice, std::string val, AMF::obj0type setType){//str type initializer
myIndice = indice;
myType = setType;
strval = val;
numval = 0;
};
/// Constructor for container objects.
/// The object type is by default AMF::AMF0_OBJECT, but this can be forced to a different value.
/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic.
/// \param setType The object type to force this object to.
AMF::Object::Object(std::string indice, AMF::obj0type setType){//object type initializer
myIndice = indice;
myType = setType;
strval = "";
numval = 0;
};
/// Prints the contents of this object to std::cerr.
/// If this object contains other objects, it will call itself recursively
/// and print all nested content in a nice human-readable format.
void AMF::Object::Print(std::string indent){
std::cerr << indent;
// print my type
switch (myType){
case AMF::AMF0_NUMBER: std::cerr << "Number"; break;
case AMF::AMF0_BOOL: std::cerr << "Bool"; break;
case AMF::AMF0_STRING://short string
case AMF::AMF0_LONGSTRING: std::cerr << "String"; break;
case AMF::AMF0_OBJECT: std::cerr << "Object"; break;
case AMF::AMF0_MOVIECLIP: std::cerr << "MovieClip"; break;
case AMF::AMF0_NULL: std::cerr << "Null"; break;
case AMF::AMF0_UNDEFINED: std::cerr << "Undefined"; break;
case AMF::AMF0_REFERENCE: std::cerr << "Reference"; break;
case AMF::AMF0_ECMA_ARRAY: std::cerr << "ECMA Array"; break;
case AMF::AMF0_OBJ_END: std::cerr << "Object end"; break;
case AMF::AMF0_STRICT_ARRAY: std::cerr << "Strict Array"; break;
case AMF::AMF0_DATE: std::cerr << "Date"; break;
case AMF::AMF0_UNSUPPORTED: std::cerr << "Unsupported"; break;
case AMF::AMF0_RECORDSET: std::cerr << "Recordset"; break;
case AMF::AMF0_XMLDOC: std::cerr << "XML Document"; break;
case AMF::AMF0_TYPED_OBJ: std::cerr << "Typed Object"; break;
case AMF::AMF0_UPGRADE: std::cerr << "Upgrade to AMF3"; break;
case AMF::AMF0_DDV_CONTAINER: std::cerr << "DDVTech Container"; break;
}
// print my string indice, if available
std::cerr << " " << myIndice << " ";
// print my numeric or string contents
switch (myType){
case AMF::AMF0_NUMBER: case AMF::AMF0_BOOL: case AMF::AMF0_REFERENCE: case AMF::AMF0_DATE: std::cerr << numval; break;
case AMF::AMF0_STRING: case AMF::AMF0_LONGSTRING: case AMF::AMF0_XMLDOC: case AMF::AMF0_TYPED_OBJ: std::cerr << strval; break;
default: break;//we don't care about the rest, and don't want a compiler warning...
}
std::cerr << std::endl;
// if I hold other objects, print those too, recursively.
if (contents.size() > 0){
for (std::vector<AMF::Object>::iterator it = contents.begin(); it != contents.end(); it++){it->Print(indent+" ");}
}
};//print
/// Packs the AMF object to a std::string for transfer over the network.
/// If the object is a container type, this function will call itself recursively and contain all contents.
/// Tip: When sending multiple AMF objects in one go, put them in a single AMF::AMF0_DDV_CONTAINER for easy transfer.
std::string AMF::Object::Pack(){
std::string r = "";
//check for string/longstring conversion
if ((myType == AMF::AMF0_STRING) && (strval.size() > 0xFFFF)){myType = AMF::AMF0_LONGSTRING;}
//skip output of DDV container types, they do not exist. Only output their contents.
if (myType != AMF::AMF0_DDV_CONTAINER){r += myType;}
//output the properly formatted AMF0 data stream for this object's contents.
switch (myType){
case AMF::AMF0_NUMBER:
r += *(((char*)&numval)+7); r += *(((char*)&numval)+6);
r += *(((char*)&numval)+5); r += *(((char*)&numval)+4);
r += *(((char*)&numval)+3); r += *(((char*)&numval)+2);
r += *(((char*)&numval)+1); r += *(((char*)&numval));
break;
case AMF::AMF0_DATE:
r += *(((char*)&numval)+7); r += *(((char*)&numval)+6);
r += *(((char*)&numval)+5); r += *(((char*)&numval)+4);
r += *(((char*)&numval)+3); r += *(((char*)&numval)+2);
r += *(((char*)&numval)+1); r += *(((char*)&numval));
r += (char)0;//timezone always 0
r += (char)0;//timezone always 0
break;
case AMF::AMF0_BOOL:
r += (char)numval;
break;
case AMF::AMF0_STRING:
r += strval.size() / 256;
r += strval.size() % 256;
r += strval;
break;
case AMF::AMF0_LONGSTRING:
case AMF::AMF0_XMLDOC://is always a longstring
r += strval.size() / (256*256*256);
r += strval.size() / (256*256);
r += strval.size() / 256;
r += strval.size() % 256;
r += strval;
break;
case AMF::AMF0_TYPED_OBJ:
r += Indice().size() / 256;
r += Indice().size() % 256;
r += Indice();
//is an object, with the classname first
case AMF::AMF0_OBJECT:
if (contents.size() > 0){
for (std::vector<AMF::Object>::iterator it = contents.begin(); it != contents.end(); it++){
r += it->Indice().size() / 256;
r += it->Indice().size() % 256;
r += it->Indice();
r += it->Pack();
}
}
return this;
};
AMFType getContent(std::string s){
if (contents != 0){
for (std::vector<AMFType>::iterator it = contents->begin(); it != contents->end(); it++){
if (it->Indice() == s){
return *it;
}
}
}
return AMFType("error");
};
AMFType(std::string indice, double val, unsigned char setType = AMF0_NUMBER){//num type initializer
myIndice = indice;
myType = setType;
strval = "";
numval = val;
contents = 0;
};
AMFType(std::string indice, std::string val, unsigned char setType = AMF0_STRING){//str type initializer
myIndice = indice;
myType = setType;
strval = val;
numval = 0;
contents = 0;
};
AMFType(std::string indice, unsigned char setType = AMF0_OBJECT){//object type initializer
myIndice = indice;
myType = setType;
strval = "";
numval = 0;
contents = new std::vector<AMFType>;
};
~AMFType(){if (contents != 0){delete contents;contents=0;}};
AMFType& operator=(const AMFType &a) {
myIndice = a.myIndice;
myType = a.myType;
strval = a.strval;
numval = a.numval;
if (contents){
if (a.contents != contents){
delete contents;
if (a.contents){
contents = new std::vector<AMFType>;
for (std::vector<AMFType>::iterator it = a.contents->begin(); it < a.contents->end(); it++){
contents->push_back(*it);
}
}else{
contents = 0;
}
r += (char)0; r += (char)0; r += (char)9;
break;
case AMF::AMF0_MOVIECLIP:
case AMF::AMF0_OBJ_END:
case AMF::AMF0_UPGRADE:
case AMF::AMF0_NULL:
case AMF::AMF0_UNDEFINED:
case AMF::AMF0_RECORDSET:
case AMF::AMF0_UNSUPPORTED:
//no data to add
break;
case AMF::AMF0_REFERENCE:
r += (char)((int)numval / 256);
r += (char)((int)numval % 256);
break;
case AMF::AMF0_ECMA_ARRAY:{
int arrlen = 0;
if (contents.size() > 0){
arrlen = contents.size();
r += arrlen / (256*256*256); r += arrlen / (256*256); r += arrlen / 256; r += arrlen % 256;
for (std::vector<AMF::Object>::iterator it = contents.begin(); it != contents.end(); it++){
r += it->Indice().size() / 256;
r += it->Indice().size() % 256;
r += it->Indice();
r += it->Pack();
}
}else{
if (a.contents){
contents = new std::vector<AMFType>;
for (std::vector<AMFType>::iterator it = a.contents->begin(); it < a.contents->end(); it++){
contents->push_back(*it);
}
r += (char)0; r += (char)0; r += (char)0; r += (char)0;
}
r += (char)0; r += (char)0; r += (char)9;
} break;
case AMF::AMF0_STRICT_ARRAY:{
int arrlen = 0;
if (contents.size() > 0){
arrlen = contents.size();
r += arrlen / (256*256*256); r += arrlen / (256*256); r += arrlen / 256; r += arrlen % 256;
for (std::vector<AMF::Object>::iterator it = contents.begin(); it != contents.end(); it++){
r += it->Pack();
}
}else{
r += (char)0; r += (char)0; r += (char)0; r += (char)0;
}
} break;
case AMF::AMF0_DDV_CONTAINER://only send contents
if (contents.size() > 0){
for (std::vector<AMF::Object>::iterator it = contents.begin(); it != contents.end(); it++){
r += it->Pack();
}
}
return *this;
};//= operator
AMFType(const AMFType &a){
myIndice = a.myIndice;
myType = a.myType;
strval = a.strval;
numval = a.numval;
if (a.contents){
contents = new std::vector<AMFType>;
for (std::vector<AMFType>::iterator it = a.contents->begin(); it < a.contents->end(); it++){
contents->push_back(*it);
}
}else{contents = 0;}
};//copy constructor
void Print(std::string indent = ""){
std::cerr << indent;
switch (myType){
case AMF0_NUMBER: std::cerr << "Number"; break;
case AMF0_BOOL: std::cerr << "Bool"; break;
case AMF0_STRING://short string
case AMF0_LONGSTRING: std::cerr << "String"; break;
case AMF0_OBJECT: std::cerr << "Object"; break;
case AMF0_MOVIECLIP: std::cerr << "MovieClip"; break;
case AMF0_NULL: std::cerr << "Null"; break;
case AMF0_UNDEFINED: std::cerr << "Undefined"; break;
case AMF0_REFERENCE: std::cerr << "Reference"; break;
case AMF0_ECMA_ARRAY: std::cerr << "ECMA Array"; break;
case AMF0_OBJ_END: std::cerr << "Object end"; break;
case AMF0_STRICT_ARRAY: std::cerr << "Strict Array"; break;
case AMF0_DATE: std::cerr << "Date"; break;
case AMF0_UNSUPPORTED: std::cerr << "Unsupported"; break;
case AMF0_RECORDSET: std::cerr << "Recordset"; break;
case AMF0_XMLDOC: std::cerr << "XML Document"; break;
case AMF0_TYPED_OBJ: std::cerr << "Typed Object"; break;
case AMF0_UPGRADE: std::cerr << "Upgrade to AMF3"; break;
case AMF0_DDV_CONTAINER: std::cerr << "DDVTech Container"; break;
}
std::cerr << " " << myIndice << " ";
switch (myType){
case AMF0_NUMBER: case AMF0_BOOL: case AMF0_REFERENCE: case AMF0_DATE: std::cerr << numval; break;
case AMF0_STRING: case AMF0_LONGSTRING: case AMF0_XMLDOC: case AMF0_TYPED_OBJ: std::cerr << strval; break;
}
std::cerr << std::endl;
if (contents){
for (std::vector<AMFType>::iterator it = contents->begin(); it != contents->end(); it++){it->Print(indent+" ");}
}
};//print
std::string Pack(){
std::string r = "";
if ((myType == AMF0_STRING) && (strval.size() > 0xFFFF)){myType = AMF0_LONGSTRING;}
if (myType != AMF0_DDV_CONTAINER){r += myType;}
switch (myType){
case AMF0_NUMBER:
r += *(((char*)&numval)+7); r += *(((char*)&numval)+6);
r += *(((char*)&numval)+5); r += *(((char*)&numval)+4);
r += *(((char*)&numval)+3); r += *(((char*)&numval)+2);
r += *(((char*)&numval)+1); r += *(((char*)&numval));
break;
case AMF0_DATE:
r += *(((char*)&numval)+7); r += *(((char*)&numval)+6);
r += *(((char*)&numval)+5); r += *(((char*)&numval)+4);
r += *(((char*)&numval)+3); r += *(((char*)&numval)+2);
r += *(((char*)&numval)+1); r += *(((char*)&numval));
r += (char)0;//timezone always 0
r += (char)0;//timezone always 0
break;
case AMF0_BOOL:
r += (char)numval;
break;
case AMF0_STRING:
r += strval.size() / 256;
r += strval.size() % 256;
r += strval;
break;
case AMF0_LONGSTRING:
case AMF0_XMLDOC://is always a longstring
r += strval.size() / (256*256*256);
r += strval.size() / (256*256);
r += strval.size() / 256;
r += strval.size() % 256;
r += strval;
break;
case AMF0_TYPED_OBJ:
r += Indice().size() / 256;
r += Indice().size() % 256;
r += Indice();
//is an object, with the classname first
case AMF0_OBJECT:
if (contents){
for (std::vector<AMFType>::iterator it = contents->begin(); it != contents->end(); it++){
r += it->Indice().size() / 256;
r += it->Indice().size() % 256;
r += it->Indice();
r += it->Pack();
}
}
r += (char)0; r += (char)0; r += (char)9;
break;
case AMF0_MOVIECLIP:
case AMF0_NULL:
case AMF0_UNDEFINED:
case AMF0_RECORDSET:
case AMF0_UNSUPPORTED:
//no data to add
break;
case AMF0_REFERENCE:
r += (char)((int)numval / 256);
r += (char)((int)numval % 256);
break;
case AMF0_ECMA_ARRAY:{
int arrlen = 0;
if (contents){
arrlen = contents->size();
r += arrlen / (256*256*256); r += arrlen / (256*256); r += arrlen / 256; r += arrlen % 256;
for (std::vector<AMFType>::iterator it = contents->begin(); it != contents->end(); it++){
r += it->Indice().size() / 256;
r += it->Indice().size() % 256;
r += it->Indice();
r += it->Pack();
}
}else{
r += (char)0; r += (char)0; r += (char)0; r += (char)0;
}
r += (char)0; r += (char)0; r += (char)9;
} break;
case AMF0_STRICT_ARRAY:{
int arrlen = 0;
if (contents){
arrlen = contents->size();
r += arrlen / (256*256*256); r += arrlen / (256*256); r += arrlen / 256; r += arrlen % 256;
for (std::vector<AMFType>::iterator it = contents->begin(); it != contents->end(); it++){
r += it->Pack();
}
}else{
r += (char)0; r += (char)0; r += (char)0; r += (char)0;
}
} break;
case AMF0_DDV_CONTAINER://only send contents
if (contents){
for (std::vector<AMFType>::iterator it = contents->begin(); it != contents->end(); it++){
r += it->Pack();
}
}
break;
}
return r;
};//pack
protected:
std::string myIndice;
unsigned char myType;
std::string strval;
double numval;
std::vector<AMFType> * contents;
};//AMFType
break;
}
return r;
};//pack
AMFType parseOneAMF(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name){
/// Parses a single AMF0 type - used recursively by the AMF::parse() functions.
/// This function updates i every call with the new position in the data.
/// \param data The raw data to parse.
/// \param len The size of the raw data.
/// \param i Current parsing position in the raw data.
/// \param name Indice name for any new object created.
/// \returns A single AMF::Object, parsed from the raw data.
AMF::Object AMF::parseOne(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name){
std::string tmpstr;
unsigned int tmpi = 0;
unsigned char tmpdbl[8];
@ -266,7 +258,7 @@ AMFType parseOneAMF(const unsigned char *& data, unsigned int &len, unsigned int
fprintf(stderr, "Note: AMF type %hhx found. %i bytes left\n", data[i], len-i);
#endif
switch (data[i]){
case AMF0_NUMBER:
case AMF::AMF0_NUMBER:
tmpdbl[7] = data[i+1];
tmpdbl[6] = data[i+2];
tmpdbl[5] = data[i+3];
@ -276,9 +268,9 @@ AMFType parseOneAMF(const unsigned char *& data, unsigned int &len, unsigned int
tmpdbl[1] = data[i+7];
tmpdbl[0] = data[i+8];
i+=9;//skip 8(a double)+1 forwards
return AMFType(name, *(double*)tmpdbl, AMF0_NUMBER);
return AMF::Object(name, *(double*)tmpdbl, AMF::AMF0_NUMBER);
break;
case AMF0_DATE:
case AMF::AMF0_DATE:
tmpdbl[7] = data[i+1];
tmpdbl[6] = data[i+2];
tmpdbl[5] = data[i+3];
@ -288,97 +280,97 @@ AMFType parseOneAMF(const unsigned char *& data, unsigned int &len, unsigned int
tmpdbl[1] = data[i+7];
tmpdbl[0] = data[i+8];
i+=11;//skip 8(a double)+1+timezone(2) forwards
return AMFType(name, *(double*)tmpdbl, AMF0_DATE);
return AMF::Object(name, *(double*)tmpdbl, AMF::AMF0_DATE);
break;
case AMF0_BOOL:
case AMF::AMF0_BOOL:
i+=2;//skip bool+1 forwards
if (data[i-1] == 0){
return AMFType(name, (double)0, AMF0_BOOL);
return AMF::Object(name, (double)0, AMF::AMF0_BOOL);
}else{
return AMFType(name, (double)1, AMF0_BOOL);
return AMF::Object(name, (double)1, AMF::AMF0_BOOL);
}
break;
case AMF0_REFERENCE:
case AMF::AMF0_REFERENCE:
tmpi = data[i+1]*256+data[i+2];//get the ref number value as a double
i+=3;//skip ref+1 forwards
return AMFType(name, (double)tmpi, AMF0_REFERENCE);
return AMF::Object(name, (double)tmpi, AMF::AMF0_REFERENCE);
break;
case AMF0_XMLDOC:
case AMF::AMF0_XMLDOC:
tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4];//set tmpi to UTF-8-long length
tmpstr.clear();//clean tmpstr, just to be sure
tmpstr.append((const char *)data+i+5, (size_t)tmpi);//add the string data
i += tmpi + 5;//skip length+size+1 forwards
return AMFType(name, tmpstr, AMF0_XMLDOC);
return AMF::Object(name, tmpstr, AMF::AMF0_XMLDOC);
break;
case AMF0_LONGSTRING:
case AMF::AMF0_LONGSTRING:
tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4];//set tmpi to UTF-8-long length
tmpstr.clear();//clean tmpstr, just to be sure
tmpstr.append((const char *)data+i+5, (size_t)tmpi);//add the string data
i += tmpi + 5;//skip length+size+1 forwards
return AMFType(name, tmpstr, AMF0_LONGSTRING);
return AMF::Object(name, tmpstr, AMF::AMF0_LONGSTRING);
break;
case AMF0_STRING:
case AMF::AMF0_STRING:
tmpi = data[i+1]*256+data[i+2];//set tmpi to UTF-8 length
tmpstr.clear();//clean tmpstr, just to be sure
tmpstr.append((const char *)data+i+3, (size_t)tmpi);//add the string data
i += tmpi + 3;//skip length+size+1 forwards
return AMFType(name, tmpstr, AMF0_STRING);
return AMF::Object(name, tmpstr, AMF::AMF0_STRING);
break;
case AMF0_NULL:
case AMF0_UNDEFINED:
case AMF0_UNSUPPORTED:
case AMF::AMF0_NULL:
case AMF::AMF0_UNDEFINED:
case AMF::AMF0_UNSUPPORTED:
++i;
return AMFType(name, (double)0, data[i-1]);
return AMF::Object(name, (double)0, (AMF::obj0type)data[i-1]);
break;
case AMF0_OBJECT:{
case AMF::AMF0_OBJECT:{
++i;
AMFType ret = AMFType(name, (unsigned char)AMF0_OBJECT);
AMF::Object ret(name, AMF::AMF0_OBJECT);
while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x000009)
tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length
tmpstr.clear();//clean tmpstr, just to be sure
tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data
i += tmpi + 2;//skip length+size forwards
ret.addContent(parseOneAMF(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr
ret.addContent(AMF::parseOne(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr
}
i += 3;//skip 0x000009
return ret;
} break;
case AMF0_TYPED_OBJ:{
case AMF::AMF0_TYPED_OBJ:{
++i;
tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length
tmpstr.clear();//clean tmpstr, just to be sure
tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data
AMFType ret = AMFType(tmpstr, (unsigned char)AMF0_TYPED_OBJ);//the object is not named "name" but tmpstr
AMF::Object ret(tmpstr, AMF::AMF0_TYPED_OBJ);//the object is not named "name" but tmpstr
while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x000009)
tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length
tmpstr.clear();//clean tmpstr, just to be sure
tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data
i += tmpi + 2;//skip length+size forwards
ret.addContent(parseOneAMF(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr
ret.addContent(AMF::parseOne(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr
}
i += 3;//skip 0x000009
return ret;
} break;
case AMF0_ECMA_ARRAY:{
case AMF::AMF0_ECMA_ARRAY:{
++i;
AMFType ret = AMFType(name, (unsigned char)AMF0_ECMA_ARRAY);
AMF::Object ret(name, AMF::AMF0_ECMA_ARRAY);
i += 4;//ignore the array length, we re-calculate it
while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x000009)
tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length
tmpstr.clear();//clean tmpstr, just to be sure
tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data
i += tmpi + 2;//skip length+size forwards
ret.addContent(parseOneAMF(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr
ret.addContent(AMF::parseOne(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr
}
i += 3;//skip 0x000009
return ret;
} break;
case AMF0_STRICT_ARRAY:{
AMFType ret = AMFType(name, (unsigned char)AMF0_STRICT_ARRAY);
case AMF::AMF0_STRICT_ARRAY:{
AMF::Object ret(name, AMF::AMF0_STRICT_ARRAY);
tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4];//set tmpi to array length
i += 5;//skip size+1 forwards
while (tmpi > 0){//while not done parsing array
ret.addContent(parseOneAMF(data, len, i, "arrVal"));//add content, recursively parsed, updating i
ret.addContent(AMF::parseOne(data, len, i, "arrVal"));//add content, recursively parsed, updating i
--tmpi;
}
return ret;
@ -387,17 +379,25 @@ AMFType parseOneAMF(const unsigned char *& data, unsigned int &len, unsigned int
#if DEBUG >= 2
fprintf(stderr, "Error: Unimplemented AMF type %hhx - returning.\n", data[i]);
#endif
return AMFType("error", (unsigned char)0xFF);
}//parseOneAMF
return AMF::Object("error", AMF::AMF0_DDV_CONTAINER);
}//parseOne
AMFType parseAMF(const unsigned char * data, unsigned int len){
AMFType ret("returned", (unsigned char)0xFF);//container type
/// Parses a C-string to a valid AMF::Object.
/// This function will find all AMF objects in the string and return
/// them all packed in a single AMF::AMF0_DDV_CONTAINER AMF::Object.
AMF::Object AMF::parse(const unsigned char * data, unsigned int len){
AMF::Object ret("returned", AMF::AMF0_DDV_CONTAINER);//container type
unsigned int i = 0, j = 0;
while (i < len){
ret.addContent(parseOneAMF(data, len, i, ""));
ret.addContent(AMF::parseOne(data, len, i, ""));
if (i > j){j = i;}else{return ret;}
}
return ret;
}//parseAMF
AMFType parseAMF(std::string data){return parseAMF((const unsigned char*)data.c_str(), data.size());}
}//parse
/// Parses a std::string to a valid AMF::Object.
/// This function will find all AMF objects in the string and return
/// them all packed in a single AMF::AMF0_DDV_CONTAINER AMF::Object.
AMF::Object AMF::parse(std::string data){
return AMF::parse((const unsigned char*)data.c_str(), data.size());
}//parse

68
util/amf.h Normal file
View file

@ -0,0 +1,68 @@
#pragma once
#include <vector>
#include <iostream>
//#include <string.h>
#include <string>
/// Holds all AMF parsing and creation related functions and classes.
namespace AMF{
/// Enumerates all possible AMF0 types, adding a special DDVTECH container type for ease of use.
enum obj0type {
AMF0_NUMBER = 0x00,
AMF0_BOOL = 0x01,
AMF0_STRING = 0x02,
AMF0_OBJECT = 0x03,
AMF0_MOVIECLIP = 0x04,
AMF0_NULL = 0x05,
AMF0_UNDEFINED = 0x06,
AMF0_REFERENCE = 0x07,
AMF0_ECMA_ARRAY = 0x08,
AMF0_OBJ_END = 0x09,
AMF0_STRICT_ARRAY = 0x0A,
AMF0_DATE = 0x0B,
AMF0_LONGSTRING = 0x0C,
AMF0_UNSUPPORTED = 0x0D,
AMF0_RECORDSET = 0x0E,
AMF0_XMLDOC = 0x0F,
AMF0_TYPED_OBJ = 0x10,
AMF0_UPGRADE = 0x11,
AMF0_DDV_CONTAINER = 0xFF
};
/// Recursive class that holds AMF0 objects.
/// It supports all AMF0 types (defined in AMF::obj0type), adding support for a special DDVTECH container type.
class Object {
public:
std::string Indice();
obj0type GetType();
double NumValue();
std::string StrValue();
const char * Str();
int hasContent();
void addContent(AMF::Object c);
Object* getContentP(int i);
Object getContent(int i);
Object* getContentP(std::string s);
Object getContent(std::string s);
Object(std::string indice, double val, obj0type setType = AMF0_NUMBER);
Object(std::string indice, std::string val, obj0type setType = AMF0_STRING);
Object(std::string indice, obj0type setType = AMF0_OBJECT);
void Print(std::string indent = "");
std::string Pack();
protected:
std::string myIndice; ///< Holds this objects indice, if any.
obj0type myType; ///< Holds this objects AMF0 type.
std::string strval; ///< Holds this objects string value, if any.
double numval; ///< Holds this objects numeric value, if any.
std::vector<Object> contents; ///< Holds this objects contents, if any (for container types).
};//AMFType
/// Parses a C-string to a valid AMF::Object.
Object parse(const unsigned char * data, unsigned int len);
/// Parses a std::string to a valid AMF::Object.
Object parse(std::string data);
/// Parses a single AMF0 type - used recursively by the AMF::parse() functions.
Object parseOne(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name);
};//AMF namespace