amf.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. /*
  2. * Copyright (C) 2005-2008 Team XBMC
  3. * http://www.xbmc.org
  4. * Copyright (C) 2008-2009 Andrej Stepanchuk
  5. * Copyright (C) 2009-2010 Howard Chu
  6. *
  7. * This file is part of librtmp.
  8. *
  9. * librtmp is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as
  11. * published by the Free Software Foundation; either version 2.1,
  12. * or (at your option) any later version.
  13. *
  14. * librtmp is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with librtmp see the file COPYING. If not, write to
  21. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301, USA.
  23. * http://www.gnu.org/copyleft/lgpl.html
  24. */
  25. #include <string.h>
  26. #include <assert.h>
  27. #include <stdlib.h>
  28. #include "rtmp_sys.h"
  29. #include "amf.h"
  30. #include "log.h"
  31. #include "bytes.h"
  32. static const AMFObjectProperty AMFProp_Invalid = { {0, 0}, AMF_INVALID };
  33. static const AMFObject AMFObj_Invalid = { 0, 0 };
  34. static const AVal AV_empty = { 0, 0 };
  35. /* Data is Big-Endian */
  36. unsigned short
  37. AMF_DecodeInt16(const char *data)
  38. {
  39. unsigned char *c = (unsigned char *) data;
  40. unsigned short val;
  41. val = (c[0] << 8) | c[1];
  42. return val;
  43. }
  44. unsigned int
  45. AMF_DecodeInt24(const char *data)
  46. {
  47. unsigned char *c = (unsigned char *) data;
  48. unsigned int val;
  49. val = (c[0] << 16) | (c[1] << 8) | c[2];
  50. return val;
  51. }
  52. unsigned int
  53. AMF_DecodeInt32(const char *data)
  54. {
  55. unsigned char *c = (unsigned char *)data;
  56. unsigned int val;
  57. val = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
  58. return val;
  59. }
  60. void
  61. AMF_DecodeString(const char *data, AVal *bv)
  62. {
  63. bv->av_len = AMF_DecodeInt16(data);
  64. bv->av_val = (bv->av_len > 0) ? (char *)data + 2 : NULL;
  65. }
  66. void
  67. AMF_DecodeLongString(const char *data, AVal *bv)
  68. {
  69. bv->av_len = AMF_DecodeInt32(data);
  70. bv->av_val = (bv->av_len > 0) ? (char *)data + 4 : NULL;
  71. }
  72. double
  73. AMF_DecodeNumber(const char *data)
  74. {
  75. double dVal;
  76. #if __FLOAT_WORD_ORDER == __BYTE_ORDER
  77. #if __BYTE_ORDER == __BIG_ENDIAN
  78. memcpy(&dVal, data, 8);
  79. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  80. unsigned char *ci, *co;
  81. ci = (unsigned char *)data;
  82. co = (unsigned char *)&dVal;
  83. co[0] = ci[7];
  84. co[1] = ci[6];
  85. co[2] = ci[5];
  86. co[3] = ci[4];
  87. co[4] = ci[3];
  88. co[5] = ci[2];
  89. co[6] = ci[1];
  90. co[7] = ci[0];
  91. #endif
  92. #else
  93. #if __BYTE_ORDER == __LITTLE_ENDIAN /* __FLOAT_WORD_ORER == __BIG_ENDIAN */
  94. unsigned char *ci, *co;
  95. ci = (unsigned char *)data;
  96. co = (unsigned char *)&dVal;
  97. co[0] = ci[3];
  98. co[1] = ci[2];
  99. co[2] = ci[1];
  100. co[3] = ci[0];
  101. co[4] = ci[7];
  102. co[5] = ci[6];
  103. co[6] = ci[5];
  104. co[7] = ci[4];
  105. #else /* __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN */
  106. unsigned char *ci, *co;
  107. ci = (unsigned char *)data;
  108. co = (unsigned char *)&dVal;
  109. co[0] = ci[4];
  110. co[1] = ci[5];
  111. co[2] = ci[6];
  112. co[3] = ci[7];
  113. co[4] = ci[0];
  114. co[5] = ci[1];
  115. co[6] = ci[2];
  116. co[7] = ci[3];
  117. #endif
  118. #endif
  119. return dVal;
  120. }
  121. int
  122. AMF_DecodeBoolean(const char *data)
  123. {
  124. return *data != 0;
  125. }
  126. char *
  127. AMF_EncodeInt16(char *output, char *outend, short nVal)
  128. {
  129. if (output+2 > outend)
  130. return NULL;
  131. output[1] = nVal & 0xff;
  132. output[0] = nVal >> 8;
  133. return output+2;
  134. }
  135. char *
  136. AMF_EncodeInt24(char *output, char *outend, int nVal)
  137. {
  138. if (output+3 > outend)
  139. return NULL;
  140. output[2] = nVal & 0xff;
  141. output[1] = nVal >> 8;
  142. output[0] = nVal >> 16;
  143. return output+3;
  144. }
  145. char *
  146. AMF_EncodeInt32(char *output, char *outend, int nVal)
  147. {
  148. if (output+4 > outend)
  149. return NULL;
  150. output[3] = nVal & 0xff;
  151. output[2] = nVal >> 8;
  152. output[1] = nVal >> 16;
  153. output[0] = nVal >> 24;
  154. return output+4;
  155. }
  156. char *
  157. AMF_EncodeString(char *output, char *outend, const AVal *bv)
  158. {
  159. if ((bv->av_len < 65536 && output + 1 + 2 + bv->av_len > outend) ||
  160. output + 1 + 4 + bv->av_len > outend)
  161. return NULL;
  162. if (bv->av_len < 65536)
  163. {
  164. *output++ = AMF_STRING;
  165. output = AMF_EncodeInt16(output, outend, bv->av_len);
  166. }
  167. else
  168. {
  169. *output++ = AMF_LONG_STRING;
  170. output = AMF_EncodeInt32(output, outend, bv->av_len);
  171. }
  172. memcpy(output, bv->av_val, bv->av_len);
  173. output += bv->av_len;
  174. return output;
  175. }
  176. char *
  177. AMF_EncodeNumber(char *output, char *outend, double dVal)
  178. {
  179. if (output+1+8 > outend)
  180. return NULL;
  181. *output++ = AMF_NUMBER; /* type: Number */
  182. #if __FLOAT_WORD_ORDER == __BYTE_ORDER
  183. #if __BYTE_ORDER == __BIG_ENDIAN
  184. memcpy(output, &dVal, 8);
  185. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  186. {
  187. unsigned char *ci, *co;
  188. ci = (unsigned char *)&dVal;
  189. co = (unsigned char *)output;
  190. co[0] = ci[7];
  191. co[1] = ci[6];
  192. co[2] = ci[5];
  193. co[3] = ci[4];
  194. co[4] = ci[3];
  195. co[5] = ci[2];
  196. co[6] = ci[1];
  197. co[7] = ci[0];
  198. }
  199. #endif
  200. #else
  201. #if __BYTE_ORDER == __LITTLE_ENDIAN /* __FLOAT_WORD_ORER == __BIG_ENDIAN */
  202. {
  203. unsigned char *ci, *co;
  204. ci = (unsigned char *)&dVal;
  205. co = (unsigned char *)output;
  206. co[0] = ci[3];
  207. co[1] = ci[2];
  208. co[2] = ci[1];
  209. co[3] = ci[0];
  210. co[4] = ci[7];
  211. co[5] = ci[6];
  212. co[6] = ci[5];
  213. co[7] = ci[4];
  214. }
  215. #else /* __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN */
  216. {
  217. unsigned char *ci, *co;
  218. ci = (unsigned char *)&dVal;
  219. co = (unsigned char *)output;
  220. co[0] = ci[4];
  221. co[1] = ci[5];
  222. co[2] = ci[6];
  223. co[3] = ci[7];
  224. co[4] = ci[0];
  225. co[5] = ci[1];
  226. co[6] = ci[2];
  227. co[7] = ci[3];
  228. }
  229. #endif
  230. #endif
  231. return output+8;
  232. }
  233. char *
  234. AMF_EncodeBoolean(char *output, char *outend, int bVal)
  235. {
  236. if (output+2 > outend)
  237. return NULL;
  238. *output++ = AMF_BOOLEAN;
  239. *output++ = bVal ? 0x01 : 0x00;
  240. return output;
  241. }
  242. char *
  243. AMF_EncodeNamedString(char *output, char *outend, const AVal *strName, const AVal *strValue)
  244. {
  245. if (output+2+strName->av_len > outend)
  246. return NULL;
  247. output = AMF_EncodeInt16(output, outend, strName->av_len);
  248. memcpy(output, strName->av_val, strName->av_len);
  249. output += strName->av_len;
  250. return AMF_EncodeString(output, outend, strValue);
  251. }
  252. char *
  253. AMF_EncodeNamedNumber(char *output, char *outend, const AVal *strName, double dVal)
  254. {
  255. if (output+2+strName->av_len > outend)
  256. return NULL;
  257. output = AMF_EncodeInt16(output, outend, strName->av_len);
  258. memcpy(output, strName->av_val, strName->av_len);
  259. output += strName->av_len;
  260. return AMF_EncodeNumber(output, outend, dVal);
  261. }
  262. char *
  263. AMF_EncodeNamedBoolean(char *output, char *outend, const AVal *strName, int bVal)
  264. {
  265. if (output+2+strName->av_len > outend)
  266. return NULL;
  267. output = AMF_EncodeInt16(output, outend, strName->av_len);
  268. memcpy(output, strName->av_val, strName->av_len);
  269. output += strName->av_len;
  270. return AMF_EncodeBoolean(output, outend, bVal);
  271. }
  272. void
  273. AMFProp_GetName(AMFObjectProperty *prop, AVal *name)
  274. {
  275. *name = prop->p_name;
  276. }
  277. void
  278. AMFProp_SetName(AMFObjectProperty *prop, AVal *name)
  279. {
  280. prop->p_name = *name;
  281. }
  282. AMFDataType
  283. AMFProp_GetType(AMFObjectProperty *prop)
  284. {
  285. return prop->p_type;
  286. }
  287. double
  288. AMFProp_GetNumber(AMFObjectProperty *prop)
  289. {
  290. return prop->p_vu.p_number;
  291. }
  292. int
  293. AMFProp_GetBoolean(AMFObjectProperty *prop)
  294. {
  295. return prop->p_vu.p_number != 0;
  296. }
  297. void
  298. AMFProp_GetString(AMFObjectProperty *prop, AVal *str)
  299. {
  300. if (prop->p_type == AMF_STRING)
  301. *str = prop->p_vu.p_aval;
  302. else
  303. *str = AV_empty;
  304. }
  305. void
  306. AMFProp_GetObject(AMFObjectProperty *prop, AMFObject *obj)
  307. {
  308. if (prop->p_type == AMF_OBJECT)
  309. *obj = prop->p_vu.p_object;
  310. else
  311. *obj = AMFObj_Invalid;
  312. }
  313. int
  314. AMFProp_IsValid(AMFObjectProperty *prop)
  315. {
  316. return prop->p_type != AMF_INVALID;
  317. }
  318. char *
  319. AMFProp_Encode(AMFObjectProperty *prop, char *pBuffer, char *pBufEnd)
  320. {
  321. if (prop->p_type == AMF_INVALID)
  322. return NULL;
  323. if (prop->p_type != AMF_NULL && pBuffer + prop->p_name.av_len + 2 + 1 >= pBufEnd)
  324. return NULL;
  325. if (prop->p_type != AMF_NULL && prop->p_name.av_len)
  326. {
  327. *pBuffer++ = prop->p_name.av_len >> 8;
  328. *pBuffer++ = prop->p_name.av_len & 0xff;
  329. memcpy(pBuffer, prop->p_name.av_val, prop->p_name.av_len);
  330. pBuffer += prop->p_name.av_len;
  331. }
  332. switch (prop->p_type)
  333. {
  334. case AMF_NUMBER:
  335. pBuffer = AMF_EncodeNumber(pBuffer, pBufEnd, prop->p_vu.p_number);
  336. break;
  337. case AMF_BOOLEAN:
  338. pBuffer = AMF_EncodeBoolean(pBuffer, pBufEnd, prop->p_vu.p_number != 0);
  339. break;
  340. case AMF_STRING:
  341. pBuffer = AMF_EncodeString(pBuffer, pBufEnd, &prop->p_vu.p_aval);
  342. break;
  343. case AMF_NULL:
  344. if (pBuffer+1 >= pBufEnd)
  345. return NULL;
  346. *pBuffer++ = AMF_NULL;
  347. break;
  348. case AMF_OBJECT:
  349. pBuffer = AMF_Encode(&prop->p_vu.p_object, pBuffer, pBufEnd);
  350. break;
  351. case AMF_ECMA_ARRAY:
  352. pBuffer = AMF_EncodeEcmaArray(&prop->p_vu.p_object, pBuffer, pBufEnd);
  353. break;
  354. case AMF_STRICT_ARRAY:
  355. pBuffer = AMF_EncodeArray(&prop->p_vu.p_object, pBuffer, pBufEnd);
  356. break;
  357. default:
  358. RTMP_Log(RTMP_LOGERROR, "%s, invalid type. %d", __FUNCTION__, prop->p_type);
  359. pBuffer = NULL;
  360. };
  361. return pBuffer;
  362. }
  363. #define AMF3_INTEGER_MAX 268435455
  364. #define AMF3_INTEGER_MIN -268435456
  365. int
  366. AMF3ReadInteger(const char *data, int32_t *valp)
  367. {
  368. int i = 0;
  369. int32_t val = 0;
  370. while (i <= 2)
  371. { /* handle first 3 bytes */
  372. if (data[i] & 0x80)
  373. { /* byte used */
  374. val <<= 7; /* shift up */
  375. val |= (data[i] & 0x7f); /* add bits */
  376. i++;
  377. }
  378. else
  379. {
  380. break;
  381. }
  382. }
  383. if (i > 2)
  384. { /* use 4th byte, all 8bits */
  385. val <<= 8;
  386. val |= data[3];
  387. /* range check */
  388. if (val > AMF3_INTEGER_MAX)
  389. val -= (1 << 29);
  390. }
  391. else
  392. { /* use 7bits of last unparsed byte (0xxxxxxx) */
  393. val <<= 7;
  394. val |= data[i];
  395. }
  396. *valp = val;
  397. return i > 2 ? 4 : i + 1;
  398. }
  399. int
  400. AMF3ReadString(const char *data, AVal *str)
  401. {
  402. int32_t ref = 0;
  403. int len;
  404. assert(str != 0);
  405. len = AMF3ReadInteger(data, &ref);
  406. data += len;
  407. if ((ref & 0x1) == 0)
  408. { /* reference: 0xxx */
  409. uint32_t refIndex = (ref >> 1);
  410. RTMP_Log(RTMP_LOGDEBUG,
  411. "%s, string reference, index: %d, not supported, ignoring!",
  412. __FUNCTION__, refIndex);
  413. str->av_val = NULL;
  414. str->av_len = 0;
  415. return len;
  416. }
  417. else
  418. {
  419. uint32_t nSize = (ref >> 1);
  420. str->av_val = (char *)data;
  421. str->av_len = nSize;
  422. return len + nSize;
  423. }
  424. return len;
  425. }
  426. int
  427. AMF3Prop_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize,
  428. int bDecodeName)
  429. {
  430. int nOriginalSize = nSize;
  431. AMF3DataType type;
  432. prop->p_name.av_len = 0;
  433. prop->p_name.av_val = NULL;
  434. if (nSize == 0 || !pBuffer)
  435. {
  436. RTMP_Log(RTMP_LOGDEBUG, "empty buffer/no buffer pointer!");
  437. return -1;
  438. }
  439. /* decode name */
  440. if (bDecodeName)
  441. {
  442. AVal name;
  443. int nRes = AMF3ReadString(pBuffer, &name);
  444. if (name.av_len <= 0)
  445. return nRes;
  446. nSize -= nRes;
  447. if (nSize <= 0)
  448. return -1;
  449. prop->p_name = name;
  450. pBuffer += nRes;
  451. }
  452. /* decode */
  453. type = *pBuffer++;
  454. nSize--;
  455. switch (type)
  456. {
  457. case AMF3_UNDEFINED:
  458. case AMF3_NULL:
  459. prop->p_type = AMF_NULL;
  460. break;
  461. case AMF3_FALSE:
  462. prop->p_type = AMF_BOOLEAN;
  463. prop->p_vu.p_number = 0.0;
  464. break;
  465. case AMF3_TRUE:
  466. prop->p_type = AMF_BOOLEAN;
  467. prop->p_vu.p_number = 1.0;
  468. break;
  469. case AMF3_INTEGER:
  470. {
  471. int32_t res = 0;
  472. int len = AMF3ReadInteger(pBuffer, &res);
  473. prop->p_vu.p_number = (double)res;
  474. prop->p_type = AMF_NUMBER;
  475. nSize -= len;
  476. break;
  477. }
  478. case AMF3_DOUBLE:
  479. if (nSize < 8)
  480. return -1;
  481. prop->p_vu.p_number = AMF_DecodeNumber(pBuffer);
  482. prop->p_type = AMF_NUMBER;
  483. nSize -= 8;
  484. break;
  485. case AMF3_STRING:
  486. case AMF3_XML_DOC:
  487. case AMF3_XML:
  488. {
  489. int len = AMF3ReadString(pBuffer, &prop->p_vu.p_aval);
  490. prop->p_type = AMF_STRING;
  491. nSize -= len;
  492. break;
  493. }
  494. case AMF3_DATE:
  495. {
  496. int32_t res = 0;
  497. int len = AMF3ReadInteger(pBuffer, &res);
  498. nSize -= len;
  499. pBuffer += len;
  500. if ((res & 0x1) == 0)
  501. { /* reference */
  502. uint32_t nIndex = (res >> 1);
  503. RTMP_Log(RTMP_LOGDEBUG, "AMF3_DATE reference: %d, not supported!", nIndex);
  504. }
  505. else
  506. {
  507. if (nSize < 8)
  508. return -1;
  509. prop->p_vu.p_number = AMF_DecodeNumber(pBuffer);
  510. nSize -= 8;
  511. prop->p_type = AMF_NUMBER;
  512. }
  513. break;
  514. }
  515. case AMF3_OBJECT:
  516. {
  517. int nRes = AMF3_Decode(&prop->p_vu.p_object, pBuffer, nSize, TRUE);
  518. if (nRes == -1)
  519. return -1;
  520. nSize -= nRes;
  521. prop->p_type = AMF_OBJECT;
  522. break;
  523. }
  524. case AMF3_ARRAY:
  525. case AMF3_BYTE_ARRAY:
  526. default:
  527. RTMP_Log(RTMP_LOGDEBUG, "%s - AMF3 unknown/unsupported datatype 0x%02x, @%p",
  528. __FUNCTION__, (unsigned char)(*pBuffer), pBuffer);
  529. return -1;
  530. }
  531. if (nSize < 0)
  532. return -1;
  533. return nOriginalSize - nSize;
  534. }
  535. int
  536. AMFProp_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize,
  537. int bDecodeName)
  538. {
  539. int nOriginalSize = nSize;
  540. int nRes;
  541. prop->p_name.av_len = 0;
  542. prop->p_name.av_val = NULL;
  543. if (nSize == 0 || !pBuffer)
  544. {
  545. RTMP_Log(RTMP_LOGDEBUG, "%s: Empty buffer/no buffer pointer!", __FUNCTION__);
  546. return -1;
  547. }
  548. if (bDecodeName && nSize < 4)
  549. { /* at least name (length + at least 1 byte) and 1 byte of data */
  550. RTMP_Log(RTMP_LOGDEBUG,
  551. "%s: Not enough data for decoding with name, less than 4 bytes!",
  552. __FUNCTION__);
  553. return -1;
  554. }
  555. if (bDecodeName)
  556. {
  557. unsigned short nNameSize = AMF_DecodeInt16(pBuffer);
  558. if (nNameSize > nSize - 2)
  559. {
  560. RTMP_Log(RTMP_LOGDEBUG,
  561. "%s: Name size out of range: namesize (%d) > len (%d) - 2",
  562. __FUNCTION__, nNameSize, nSize);
  563. return -1;
  564. }
  565. AMF_DecodeString(pBuffer, &prop->p_name);
  566. nSize -= 2 + nNameSize;
  567. pBuffer += 2 + nNameSize;
  568. }
  569. if (nSize == 0)
  570. {
  571. return -1;
  572. }
  573. nSize--;
  574. prop->p_type = *pBuffer++;
  575. switch (prop->p_type)
  576. {
  577. case AMF_NUMBER:
  578. if (nSize < 8)
  579. return -1;
  580. prop->p_vu.p_number = AMF_DecodeNumber(pBuffer);
  581. nSize -= 8;
  582. break;
  583. case AMF_BOOLEAN:
  584. if (nSize < 1)
  585. return -1;
  586. prop->p_vu.p_number = (double)AMF_DecodeBoolean(pBuffer);
  587. nSize--;
  588. break;
  589. case AMF_STRING:
  590. {
  591. unsigned short nStringSize = AMF_DecodeInt16(pBuffer);
  592. if (nSize < (long)nStringSize + 2)
  593. return -1;
  594. AMF_DecodeString(pBuffer, &prop->p_vu.p_aval);
  595. nSize -= (2 + nStringSize);
  596. break;
  597. }
  598. case AMF_OBJECT:
  599. {
  600. int nRes = AMF_Decode(&prop->p_vu.p_object, pBuffer, nSize, TRUE);
  601. if (nRes == -1)
  602. return -1;
  603. nSize -= nRes;
  604. break;
  605. }
  606. case AMF_MOVIECLIP:
  607. {
  608. RTMP_Log(RTMP_LOGERROR, "AMF_MOVIECLIP reserved!");
  609. return -1;
  610. break;
  611. }
  612. case AMF_NULL:
  613. case AMF_UNDEFINED:
  614. case AMF_UNSUPPORTED:
  615. prop->p_type = AMF_NULL;
  616. break;
  617. case AMF_REFERENCE:
  618. {
  619. RTMP_Log(RTMP_LOGERROR, "AMF_REFERENCE not supported!");
  620. return -1;
  621. break;
  622. }
  623. case AMF_ECMA_ARRAY:
  624. {
  625. nSize -= 4;
  626. /* next comes the rest, mixed array has a final 0x000009 mark and names, so its an object */
  627. nRes = AMF_Decode(&prop->p_vu.p_object, pBuffer + 4, nSize, TRUE);
  628. if (nRes == -1)
  629. return -1;
  630. nSize -= nRes;
  631. break;
  632. }
  633. case AMF_OBJECT_END:
  634. {
  635. return -1;
  636. break;
  637. }
  638. case AMF_STRICT_ARRAY:
  639. {
  640. unsigned int nArrayLen = AMF_DecodeInt32(pBuffer);
  641. nSize -= 4;
  642. nRes = AMF_DecodeArray(&prop->p_vu.p_object, pBuffer + 4, nSize,
  643. nArrayLen, FALSE);
  644. if (nRes == -1)
  645. return -1;
  646. nSize -= nRes;
  647. break;
  648. }
  649. case AMF_DATE:
  650. {
  651. RTMP_Log(RTMP_LOGDEBUG, "AMF_DATE");
  652. if (nSize < 10)
  653. return -1;
  654. prop->p_vu.p_number = AMF_DecodeNumber(pBuffer);
  655. prop->p_UTCoffset = AMF_DecodeInt16(pBuffer + 8);
  656. nSize -= 10;
  657. break;
  658. }
  659. case AMF_LONG_STRING:
  660. case AMF_XML_DOC:
  661. {
  662. unsigned int nStringSize = AMF_DecodeInt32(pBuffer);
  663. if (nSize < (long)nStringSize + 4)
  664. return -1;
  665. AMF_DecodeLongString(pBuffer, &prop->p_vu.p_aval);
  666. nSize -= (4 + nStringSize);
  667. if (prop->p_type == AMF_LONG_STRING)
  668. prop->p_type = AMF_STRING;
  669. break;
  670. }
  671. case AMF_RECORDSET:
  672. {
  673. RTMP_Log(RTMP_LOGERROR, "AMF_RECORDSET reserved!");
  674. return -1;
  675. break;
  676. }
  677. case AMF_TYPED_OBJECT:
  678. {
  679. RTMP_Log(RTMP_LOGERROR, "AMF_TYPED_OBJECT not supported!");
  680. return -1;
  681. break;
  682. }
  683. case AMF_AVMPLUS:
  684. {
  685. int nRes = AMF3_Decode(&prop->p_vu.p_object, pBuffer, nSize, TRUE);
  686. if (nRes == -1)
  687. return -1;
  688. nSize -= nRes;
  689. prop->p_type = AMF_OBJECT;
  690. break;
  691. }
  692. default:
  693. RTMP_Log(RTMP_LOGDEBUG, "%s - unknown datatype 0x%02x, @%p", __FUNCTION__,
  694. prop->p_type, pBuffer - 1);
  695. return -1;
  696. }
  697. return nOriginalSize - nSize;
  698. }
  699. void
  700. AMFProp_Dump(AMFObjectProperty *prop)
  701. {
  702. char strRes[256];
  703. char str[256];
  704. AVal name;
  705. if (prop->p_type == AMF_INVALID)
  706. {
  707. RTMP_Log(RTMP_LOGDEBUG, "Property: INVALID");
  708. return;
  709. }
  710. if (prop->p_type == AMF_NULL)
  711. {
  712. RTMP_Log(RTMP_LOGDEBUG, "Property: NULL");
  713. return;
  714. }
  715. if (prop->p_name.av_len)
  716. {
  717. name = prop->p_name;
  718. }
  719. else
  720. {
  721. name.av_val = "no-name.";
  722. name.av_len = sizeof("no-name.") - 1;
  723. }
  724. if (name.av_len > 18)
  725. name.av_len = 18;
  726. snprintf(strRes, 255, "Name: %18.*s, ", name.av_len, name.av_val);
  727. if (prop->p_type == AMF_OBJECT)
  728. {
  729. RTMP_Log(RTMP_LOGDEBUG, "Property: <%sOBJECT>", strRes);
  730. AMF_Dump(&prop->p_vu.p_object);
  731. return;
  732. }
  733. else if (prop->p_type == AMF_ECMA_ARRAY)
  734. {
  735. RTMP_Log(RTMP_LOGDEBUG, "Property: <%sECMA_ARRAY>", strRes);
  736. AMF_Dump(&prop->p_vu.p_object);
  737. return;
  738. }
  739. else if (prop->p_type == AMF_STRICT_ARRAY)
  740. {
  741. RTMP_Log(RTMP_LOGDEBUG, "Property: <%sSTRICT_ARRAY>", strRes);
  742. AMF_Dump(&prop->p_vu.p_object);
  743. return;
  744. }
  745. switch (prop->p_type)
  746. {
  747. case AMF_NUMBER:
  748. snprintf(str, 255, "NUMBER:\t%.2f", prop->p_vu.p_number);
  749. break;
  750. case AMF_BOOLEAN:
  751. snprintf(str, 255, "BOOLEAN:\t%s",
  752. prop->p_vu.p_number != 0.0 ? "TRUE" : "FALSE");
  753. break;
  754. case AMF_STRING:
  755. snprintf(str, 255, "STRING:\t%.*s", prop->p_vu.p_aval.av_len,
  756. prop->p_vu.p_aval.av_val);
  757. break;
  758. case AMF_DATE:
  759. snprintf(str, 255, "DATE:\ttimestamp: %.2f, UTC offset: %d",
  760. prop->p_vu.p_number, prop->p_UTCoffset);
  761. break;
  762. default:
  763. snprintf(str, 255, "INVALID TYPE 0x%02x", (unsigned char)prop->p_type);
  764. }
  765. RTMP_Log(RTMP_LOGDEBUG, "Property: <%s%s>", strRes, str);
  766. }
  767. void
  768. AMFProp_Reset(AMFObjectProperty *prop)
  769. {
  770. if (prop->p_type == AMF_OBJECT || prop->p_type == AMF_ECMA_ARRAY ||
  771. prop->p_type == AMF_STRICT_ARRAY)
  772. AMF_Reset(&prop->p_vu.p_object);
  773. else
  774. {
  775. prop->p_vu.p_aval.av_len = 0;
  776. prop->p_vu.p_aval.av_val = NULL;
  777. }
  778. prop->p_type = AMF_INVALID;
  779. }
  780. /* AMFObject */
  781. char *
  782. AMF_Encode(AMFObject *obj, char *pBuffer, char *pBufEnd)
  783. {
  784. int i;
  785. if (pBuffer+4 >= pBufEnd)
  786. return NULL;
  787. *pBuffer++ = AMF_OBJECT;
  788. for (i = 0; i < obj->o_num; i++)
  789. {
  790. char *res = AMFProp_Encode(&obj->o_props[i], pBuffer, pBufEnd);
  791. if (res == NULL)
  792. {
  793. RTMP_Log(RTMP_LOGERROR, "AMF_Encode - failed to encode property in index %d",
  794. i);
  795. break;
  796. }
  797. else
  798. {
  799. pBuffer = res;
  800. }
  801. }
  802. if (pBuffer + 3 >= pBufEnd)
  803. return NULL; /* no room for the end marker */
  804. pBuffer = AMF_EncodeInt24(pBuffer, pBufEnd, AMF_OBJECT_END);
  805. return pBuffer;
  806. }
  807. char *
  808. AMF_EncodeEcmaArray(AMFObject *obj, char *pBuffer, char *pBufEnd)
  809. {
  810. int i;
  811. if (pBuffer+4 >= pBufEnd)
  812. return NULL;
  813. *pBuffer++ = AMF_ECMA_ARRAY;
  814. pBuffer = AMF_EncodeInt32(pBuffer, pBufEnd, obj->o_num);
  815. for (i = 0; i < obj->o_num; i++)
  816. {
  817. char *res = AMFProp_Encode(&obj->o_props[i], pBuffer, pBufEnd);
  818. if (res == NULL)
  819. {
  820. RTMP_Log(RTMP_LOGERROR, "AMF_Encode - failed to encode property in index %d",
  821. i);
  822. break;
  823. }
  824. else
  825. {
  826. pBuffer = res;
  827. }
  828. }
  829. if (pBuffer + 3 >= pBufEnd)
  830. return NULL; /* no room for the end marker */
  831. pBuffer = AMF_EncodeInt24(pBuffer, pBufEnd, AMF_OBJECT_END);
  832. return pBuffer;
  833. }
  834. char *
  835. AMF_EncodeArray(AMFObject *obj, char *pBuffer, char *pBufEnd)
  836. {
  837. int i;
  838. if (pBuffer+4 >= pBufEnd)
  839. return NULL;
  840. *pBuffer++ = AMF_STRICT_ARRAY;
  841. pBuffer = AMF_EncodeInt32(pBuffer, pBufEnd, obj->o_num);
  842. for (i = 0; i < obj->o_num; i++)
  843. {
  844. char *res = AMFProp_Encode(&obj->o_props[i], pBuffer, pBufEnd);
  845. if (res == NULL)
  846. {
  847. RTMP_Log(RTMP_LOGERROR, "AMF_Encode - failed to encode property in index %d",
  848. i);
  849. break;
  850. }
  851. else
  852. {
  853. pBuffer = res;
  854. }
  855. }
  856. //if (pBuffer + 3 >= pBufEnd)
  857. // return NULL; /* no room for the end marker */
  858. //pBuffer = AMF_EncodeInt24(pBuffer, pBufEnd, AMF_OBJECT_END);
  859. return pBuffer;
  860. }
  861. int
  862. AMF_DecodeArray(AMFObject *obj, const char *pBuffer, int nSize,
  863. int nArrayLen, int bDecodeName)
  864. {
  865. int nOriginalSize = nSize;
  866. int bError = FALSE;
  867. obj->o_num = 0;
  868. obj->o_props = NULL;
  869. while (nArrayLen > 0)
  870. {
  871. AMFObjectProperty prop;
  872. int nRes;
  873. nArrayLen--;
  874. if (nSize <= 0)
  875. {
  876. bError = TRUE;
  877. break;
  878. }
  879. nRes = AMFProp_Decode(&prop, pBuffer, nSize, bDecodeName);
  880. if (nRes == -1)
  881. {
  882. bError = TRUE;
  883. break;
  884. }
  885. else
  886. {
  887. nSize -= nRes;
  888. pBuffer += nRes;
  889. AMF_AddProp(obj, &prop);
  890. }
  891. }
  892. if (bError)
  893. return -1;
  894. return nOriginalSize - nSize;
  895. }
  896. int
  897. AMF3_Decode(AMFObject *obj, const char *pBuffer, int nSize, int bAMFData)
  898. {
  899. int nOriginalSize = nSize;
  900. int32_t ref;
  901. int len;
  902. obj->o_num = 0;
  903. obj->o_props = NULL;
  904. if (bAMFData)
  905. {
  906. if (*pBuffer != AMF3_OBJECT)
  907. RTMP_Log(RTMP_LOGERROR,
  908. "AMF3 Object encapsulated in AMF stream does not start with AMF3_OBJECT!");
  909. pBuffer++;
  910. nSize--;
  911. }
  912. ref = 0;
  913. len = AMF3ReadInteger(pBuffer, &ref);
  914. pBuffer += len;
  915. nSize -= len;
  916. if ((ref & 1) == 0)
  917. { /* object reference, 0xxx */
  918. uint32_t objectIndex = (ref >> 1);
  919. RTMP_Log(RTMP_LOGDEBUG, "Object reference, index: %d", objectIndex);
  920. }
  921. else /* object instance */
  922. {
  923. int32_t classRef = (ref >> 1);
  924. AMF3ClassDef cd = { {0, 0}
  925. };
  926. AMFObjectProperty prop;
  927. if ((classRef & 0x1) == 0)
  928. { /* class reference */
  929. uint32_t classIndex = (classRef >> 1);
  930. RTMP_Log(RTMP_LOGDEBUG, "Class reference: %d", classIndex);
  931. }
  932. else
  933. {
  934. int32_t classExtRef = (classRef >> 1);
  935. int i, cdnum;
  936. cd.cd_externalizable = (classExtRef & 0x1) == 1;
  937. cd.cd_dynamic = ((classExtRef >> 1) & 0x1) == 1;
  938. cdnum = classExtRef >> 2;
  939. /* class name */
  940. len = AMF3ReadString(pBuffer, &cd.cd_name);
  941. nSize -= len;
  942. pBuffer += len;
  943. /*std::string str = className; */
  944. RTMP_Log(RTMP_LOGDEBUG,
  945. "Class name: %s, externalizable: %d, dynamic: %d, classMembers: %d",
  946. cd.cd_name.av_val, cd.cd_externalizable, cd.cd_dynamic,
  947. cd.cd_num);
  948. for (i = 0; i < cdnum; i++)
  949. {
  950. AVal memberName;
  951. if (nSize <=0)
  952. {
  953. invalid:
  954. RTMP_Log(RTMP_LOGDEBUG, "%s, invalid class encoding!",
  955. __FUNCTION__);
  956. return nOriginalSize;
  957. }
  958. len = AMF3ReadString(pBuffer, &memberName);
  959. RTMP_Log(RTMP_LOGDEBUG, "Member: %s", memberName.av_val);
  960. AMF3CD_AddProp(&cd, &memberName);
  961. nSize -= len;
  962. pBuffer += len;
  963. }
  964. }
  965. /* add as referencable object */
  966. if (cd.cd_externalizable)
  967. {
  968. int nRes;
  969. AVal name = AVC("DEFAULT_ATTRIBUTE");
  970. RTMP_Log(RTMP_LOGDEBUG, "Externalizable, TODO check");
  971. nRes = AMF3Prop_Decode(&prop, pBuffer, nSize, FALSE);
  972. if (nRes == -1)
  973. RTMP_Log(RTMP_LOGDEBUG, "%s, failed to decode AMF3 property!",
  974. __FUNCTION__);
  975. else
  976. {
  977. nSize -= nRes;
  978. pBuffer += nRes;
  979. }
  980. AMFProp_SetName(&prop, &name);
  981. AMF_AddProp(obj, &prop);
  982. }
  983. else
  984. {
  985. int nRes, i;
  986. for (i = 0; i < cd.cd_num; i++) /* non-dynamic */
  987. {
  988. if (nSize <=0)
  989. goto invalid;
  990. nRes = AMF3Prop_Decode(&prop, pBuffer, nSize, FALSE);
  991. if (nRes == -1)
  992. RTMP_Log(RTMP_LOGDEBUG, "%s, failed to decode AMF3 property!",
  993. __FUNCTION__);
  994. AMFProp_SetName(&prop, AMF3CD_GetProp(&cd, i));
  995. AMF_AddProp(obj, &prop);
  996. pBuffer += nRes;
  997. nSize -= nRes;
  998. }
  999. if (cd.cd_dynamic)
  1000. {
  1001. int len = 0;
  1002. do
  1003. {
  1004. if (nSize <=0)
  1005. goto invalid;
  1006. nRes = AMF3Prop_Decode(&prop, pBuffer, nSize, TRUE);
  1007. AMF_AddProp(obj, &prop);
  1008. pBuffer += nRes;
  1009. nSize -= nRes;
  1010. len = prop.p_name.av_len;
  1011. }
  1012. while (len > 0);
  1013. }
  1014. }
  1015. RTMP_Log(RTMP_LOGDEBUG, "class object!");
  1016. }
  1017. return nOriginalSize - nSize;
  1018. }
  1019. int
  1020. AMF_Decode(AMFObject *obj, const char *pBuffer, int nSize, int bDecodeName)
  1021. {
  1022. int nOriginalSize = nSize;
  1023. int bError = FALSE; /* if there is an error while decoding - try to at least find the end mark AMF_OBJECT_END */
  1024. obj->o_num = 0;
  1025. obj->o_props = NULL;
  1026. while (nSize > 0)
  1027. {
  1028. AMFObjectProperty prop;
  1029. int nRes;
  1030. if (nSize >=3 && AMF_DecodeInt24(pBuffer) == AMF_OBJECT_END)
  1031. {
  1032. nSize -= 3;
  1033. bError = FALSE;
  1034. break;
  1035. }
  1036. if (bError)
  1037. {
  1038. RTMP_Log(RTMP_LOGERROR,
  1039. "DECODING ERROR, IGNORING BYTES UNTIL NEXT KNOWN PATTERN!");
  1040. nSize--;
  1041. pBuffer++;
  1042. continue;
  1043. }
  1044. nRes = AMFProp_Decode(&prop, pBuffer, nSize, bDecodeName);
  1045. if (nRes == -1)
  1046. {
  1047. bError = TRUE;
  1048. break;
  1049. }
  1050. else
  1051. {
  1052. nSize -= nRes;
  1053. if (nSize < 0)
  1054. {
  1055. bError = TRUE;
  1056. break;
  1057. }
  1058. pBuffer += nRes;
  1059. AMF_AddProp(obj, &prop);
  1060. }
  1061. }
  1062. if (bError)
  1063. return -1;
  1064. return nOriginalSize - nSize;
  1065. }
  1066. void
  1067. AMF_AddProp(AMFObject *obj, const AMFObjectProperty *prop)
  1068. {
  1069. if (!(obj->o_num & 0x0f))
  1070. obj->o_props =
  1071. realloc(obj->o_props, (obj->o_num + 16) * sizeof(AMFObjectProperty));
  1072. memcpy(&obj->o_props[obj->o_num++], prop, sizeof(AMFObjectProperty));
  1073. }
  1074. int
  1075. AMF_CountProp(AMFObject *obj)
  1076. {
  1077. return obj->o_num;
  1078. }
  1079. AMFObjectProperty *
  1080. AMF_GetProp(AMFObject *obj, const AVal *name, int nIndex)
  1081. {
  1082. if (nIndex >= 0)
  1083. {
  1084. if (nIndex < obj->o_num)
  1085. return &obj->o_props[nIndex];
  1086. }
  1087. else
  1088. {
  1089. int n;
  1090. for (n = 0; n < obj->o_num; n++)
  1091. {
  1092. if (AVMATCH(&obj->o_props[n].p_name, name))
  1093. return &obj->o_props[n];
  1094. }
  1095. }
  1096. return (AMFObjectProperty *)&AMFProp_Invalid;
  1097. }
  1098. void
  1099. AMF_Dump(AMFObject *obj)
  1100. {
  1101. int n;
  1102. RTMP_Log(RTMP_LOGDEBUG, "(object begin)");
  1103. for (n = 0; n < obj->o_num; n++)
  1104. {
  1105. AMFProp_Dump(&obj->o_props[n]);
  1106. }
  1107. RTMP_Log(RTMP_LOGDEBUG, "(object end)");
  1108. }
  1109. void
  1110. AMF_Reset(AMFObject *obj)
  1111. {
  1112. int n;
  1113. for (n = 0; n < obj->o_num; n++)
  1114. {
  1115. AMFProp_Reset(&obj->o_props[n]);
  1116. }
  1117. free(obj->o_props);
  1118. obj->o_props = NULL;
  1119. obj->o_num = 0;
  1120. }
  1121. /* AMF3ClassDefinition */
  1122. void
  1123. AMF3CD_AddProp(AMF3ClassDef *cd, AVal *prop)
  1124. {
  1125. if (!(cd->cd_num & 0x0f))
  1126. cd->cd_props = realloc(cd->cd_props, (cd->cd_num + 16) * sizeof(AVal));
  1127. cd->cd_props[cd->cd_num++] = *prop;
  1128. }
  1129. AVal *
  1130. AMF3CD_GetProp(AMF3ClassDef *cd, int nIndex)
  1131. {
  1132. if (nIndex >= cd->cd_num)
  1133. return (AVal *)&AV_empty;
  1134. return &cd->cd_props[nIndex];
  1135. }