amf.c 29 KB

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