Fixed many small issues in JSON/DTSC libraries.

This commit is contained in:
Thulinma 2013-07-03 16:06:07 +02:00
parent 5be3774e3c
commit 593805f0ef
3 changed files with 49 additions and 12 deletions

View file

@ -816,8 +816,14 @@ JSON::Value JSON::fromDTMI(const unsigned char * data, unsigned int len, unsigne
#if DEBUG >= 10
fprintf(stderr, "Note: AMF type %hhx found. %i bytes left\n", data[i], len-i);
#endif
if (i >= len){
return JSON::Value();
}
switch (data[i]){
case 0x01: { //integer
if (i+8 >= len){
return JSON::Value();
}
unsigned char tmpdbl[8];
tmpdbl[7] = data[i + 1];
tmpdbl[6] = data[i + 2];
@ -833,8 +839,14 @@ JSON::Value JSON::fromDTMI(const unsigned char * data, unsigned int len, unsigne
}
break;
case 0x02: { //string
if (i+4 >= len){
return JSON::Value();
}
unsigned int 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
std::string tmpstr = std::string((const char *)data + i + 5, (size_t)tmpi); //set the string data
if (i+4+tmpi >= len){
return JSON::Value();
}
i += tmpi + 5; //skip length+size+1 forwards
return JSON::Value(tmpstr);
}
@ -843,7 +855,10 @@ JSON::Value JSON::fromDTMI(const unsigned char * data, unsigned int len, unsigne
case 0xE0: { //object
++i;
JSON::Value ret;
while (data[i] + data[i + 1] != 0){ //while not encountering 0x0000 (we assume 0x0000EE)
while (data[i] + data[i + 1] != 0 && i < len){ //while not encountering 0x0000 (we assume 0x0000EE)
if (i+2 >= len){
return JSON::Value();
}
unsigned int tmpi = data[i] * 256 + data[i + 1]; //set tmpi to the UTF-8 length
std::string tmpstr = std::string((const char *)data + i + 2, (size_t)tmpi); //set the string data
i += tmpi + 2; //skip length+size forwards
@ -856,7 +871,7 @@ JSON::Value JSON::fromDTMI(const unsigned char * data, unsigned int len, unsigne
case 0x0A: { //array
JSON::Value ret;
++i;
while (data[i] + data[i + 1] != 0){ //while not encountering 0x0000 (we assume 0x0000EE)
while (data[i] + data[i + 1] != 0 && i < len){ //while not encountering 0x0000 (we assume 0x0000EE)
ret.append(fromDTMI(data, len, i)); //add content, recursively parsed, updating i
}
i += 3; //skip 0x0000EE
@ -865,8 +880,9 @@ JSON::Value JSON::fromDTMI(const unsigned char * data, unsigned int len, unsigne
break;
}
#if DEBUG >= 2
fprintf(stderr, "Error: Unimplemented DTMI type %hhx - returning.\n", data[i]);
fprintf(stderr, "Error: Unimplemented DTMI type %hhx, @ %i / %i - returning.\n", data[i], i, len);
#endif
i += 1;
return JSON::Value();
} //fromOneDTMI
@ -887,3 +903,17 @@ JSON::Value JSON::fromDTMI2(std::string data){
tmp["trackid"] = tmpTrackID;
return tmp;
}
JSON::Value JSON::fromDTMI2(const unsigned char * data, unsigned int len, unsigned int &i){
JSON::Value tmp;
if (len < 13){return tmp;}
long long int tmpTrackID = ntohl(((int*)data)[0]);
long long int tmpTime = ntohl(((int*)data)[1]);
tmpTime << 32;
tmpTime += ntohl(((int*)data)[2]);
i += 12;
tmp = fromDTMI(data, len, i);
tmp["time"] = tmpTime;
tmp["trackid"] = tmpTrackID;
return tmp;
}