You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1521 lines
51 KiB

  1. From 5b06d1cad5f6711667038169b7ed759d749334da Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Jan=20Bub=C3=ADk?= <jan.bubik@technodat.cz>
  3. Date: Wed, 13 May 2020 19:57:47 +0200
  4. Subject: [PATCH 1/3] arut's e0e278bc7fedd6f7465648d1d20df1a8422d60bf [removed
  5. endian-dependent code]
  6. ---
  7. ngx_rtmp.c | 4 ++
  8. ngx_rtmp.h | 12 +++--
  9. ngx_rtmp_amf.c | 6 +--
  10. ngx_rtmp_flv_module.c | 2 +-
  11. ngx_rtmp_handler.c | 108 +++++++++++++++++++++---------------------
  12. 5 files changed, 68 insertions(+), 64 deletions(-)
  13. --- a/nginx-rtmp/ngx_rtmp.c
  14. +++ b/nginx-rtmp/ngx_rtmp.c
  15. @@ -825,22 +825,6 @@ ngx_rtmp_fire_event(ngx_rtmp_session_t *
  16. }
  17. -void *
  18. -ngx_rtmp_rmemcpy(void *dst, const void* src, size_t n)
  19. -{
  20. - u_char *d, *s;
  21. -
  22. - d = dst;
  23. - s = (u_char*)src + n - 1;
  24. -
  25. - while(s >= (u_char*)src) {
  26. - *d++ = *s--;
  27. - }
  28. -
  29. - return dst;
  30. -}
  31. -
  32. -
  33. static ngx_int_t
  34. ngx_rtmp_init_process(ngx_cycle_t *cycle)
  35. {
  36. --- a/nginx-rtmp/ngx_rtmp.h
  37. +++ b/nginx-rtmp/ngx_rtmp.h
  38. @@ -417,34 +417,33 @@ ngx_int_t ngx_rtmp_fire_event(ngx_rtmp_s
  39. ngx_int_t ngx_rtmp_set_chunk_size(ngx_rtmp_session_t *s, ngx_uint_t size);
  40. -/* Bit reverse: we need big-endians in many places */
  41. -void * ngx_rtmp_rmemcpy(void *dst, const void* src, size_t n);
  42. -
  43. -#define ngx_rtmp_rcpymem(dst, src, n) \
  44. - (((u_char*)ngx_rtmp_rmemcpy(dst, src, n)) + (n))
  45. -
  46. -
  47. -static ngx_inline uint16_t
  48. -ngx_rtmp_r16(uint16_t n)
  49. +/* Bit agnosticism: we need network to host byte-order conversion in many places */
  50. +static ngx_inline uint64_t
  51. +ntohll(uint64_t n)
  52. {
  53. - return (n << 8) | (n >> 8);
  54. +#if (NGX_HAVE_LITTLE_ENDIAN)
  55. + return (uint64_t) ntohl((uint32_t) n) << 32 |
  56. + ntohl((uint32_t) (n >> 32));
  57. +#else
  58. + return n;
  59. +#endif
  60. }
  61. -
  62. static ngx_inline uint32_t
  63. -ngx_rtmp_r32(uint32_t n)
  64. +n3toh4(u_char* src)
  65. {
  66. - return (n << 24) | ((n << 8) & 0xff0000) | ((n >> 8) & 0xff00) | (n >> 24);
  67. + return ((uint32_t)src[0]<<16)|((uint32_t)src[1]<<8)|src[2];
  68. }
  69. -
  70. -static ngx_inline uint64_t
  71. -ngx_rtmp_r64(uint64_t n)
  72. +static ngx_inline u_char*
  73. +h4ton3(u_char* dst, uint32_t src)
  74. {
  75. - return (uint64_t) ngx_rtmp_r32((uint32_t) n) << 32 |
  76. - ngx_rtmp_r32((uint32_t) (n >> 32));
  77. -}
  78. + dst[0]=(u_char)(src>>16);
  79. + dst[1]=(u_char)(src>>8);
  80. + dst[2]=(u_char)src;
  81. + return dst+3;
  82. +}
  83. /* Receiving messages */
  84. ngx_int_t ngx_rtmp_receive_message(ngx_rtmp_session_t *s,
  85. --- a/nginx-rtmp/ngx_rtmp_amf.c
  86. +++ b/nginx-rtmp/ngx_rtmp_amf.c
  87. @@ -10,23 +10,6 @@
  88. #include "ngx_rtmp.h"
  89. #include <string.h>
  90. -
  91. -static ngx_inline void*
  92. -ngx_rtmp_amf_reverse_copy(void *dst, void* src, size_t len)
  93. -{
  94. - size_t k;
  95. -
  96. - if (dst == NULL || src == NULL) {
  97. - return NULL;
  98. - }
  99. -
  100. - for(k = 0; k < len; ++k) {
  101. - ((u_char*)dst)[k] = ((u_char*)src)[len - 1 - k];
  102. - }
  103. -
  104. - return dst;
  105. -}
  106. -
  107. #define NGX_RTMP_AMF_DEBUG_SIZE 72
  108. #ifdef NGX_DEBUG
  109. @@ -207,7 +190,7 @@ ngx_rtmp_amf_read_object(ngx_rtmp_amf_ct
  110. return NGX_ERROR;
  111. }
  112. - ngx_rtmp_amf_reverse_copy(&len, buf, 2);
  113. + len=ntohs(*(uint16_t*)&buf[0]);
  114. if (!len)
  115. break;
  116. @@ -258,7 +241,7 @@ ngx_rtmp_amf_read_array(ngx_rtmp_amf_ctx
  117. if (ngx_rtmp_amf_get(ctx, buf, 4) != NGX_OK)
  118. return NGX_ERROR;
  119. - ngx_rtmp_amf_reverse_copy(&len, buf, 4);
  120. + len=ntohl(*(uint32_t*)&buf[0]);
  121. for (n = 0; n < len; ++n) {
  122. if (ngx_rtmp_amf_read(ctx, n < nelts ? &elts[n] : NULL, 1) != NGX_OK)
  123. @@ -352,10 +335,9 @@ ngx_rtmp_amf_read(ngx_rtmp_amf_ctx_t *ct
  124. switch (type) {
  125. case NGX_RTMP_AMF_NUMBER:
  126. - if (ngx_rtmp_amf_get(ctx, buf, 8) != NGX_OK) {
  127. + if (ngx_rtmp_amf_get(ctx, data, 8) != NGX_OK) {
  128. return NGX_ERROR;
  129. }
  130. - ngx_rtmp_amf_reverse_copy(data, buf, 8);
  131. break;
  132. case NGX_RTMP_AMF_BOOLEAN:
  133. @@ -368,7 +350,7 @@ ngx_rtmp_amf_read(ngx_rtmp_amf_ctx_t *ct
  134. if (ngx_rtmp_amf_get(ctx, buf, 2) != NGX_OK) {
  135. return NGX_ERROR;
  136. }
  137. - ngx_rtmp_amf_reverse_copy(&len, buf, 2);
  138. + len=ntohs(*(uint16_t*)buf);
  139. if (data == NULL) {
  140. rc = ngx_rtmp_amf_get(ctx, data, len);
  141. @@ -438,14 +420,14 @@ ngx_rtmp_amf_read(ngx_rtmp_amf_ctx_t *ct
  142. if (ngx_rtmp_amf_get(ctx, buf, 2) != NGX_OK) {
  143. return NGX_ERROR;
  144. }
  145. - ngx_rtmp_amf_reverse_copy(data, buf, 2);
  146. + *(uint16_t*)data=ntohs(*(uint16_t*)buf);
  147. break;
  148. case NGX_RTMP_AMF_INT32:
  149. if (ngx_rtmp_amf_get(ctx, buf, 4) != NGX_OK) {
  150. return NGX_ERROR;
  151. }
  152. - ngx_rtmp_amf_reverse_copy(data, buf, 4);
  153. + *(uint32_t*)data=ntohs(*(uint32_t*)buf);
  154. break;
  155. case NGX_RTMP_AMF_END:
  156. @@ -476,9 +458,8 @@ ngx_rtmp_amf_write_object(ngx_rtmp_amf_c
  157. len = (uint16_t) elts[n].name.len;
  158. - if (ngx_rtmp_amf_put(ctx,
  159. - ngx_rtmp_amf_reverse_copy(buf,
  160. - &len, 2), 2) != NGX_OK)
  161. + *(uint16_t*)buf = htons(len);
  162. + if (ngx_rtmp_amf_put(ctx, buf, 2) != NGX_OK)
  163. {
  164. return NGX_ERROR;
  165. }
  166. @@ -509,9 +490,8 @@ ngx_rtmp_amf_write_array(ngx_rtmp_amf_ct
  167. u_char buf[4];
  168. len = nelts;
  169. - if (ngx_rtmp_amf_put(ctx,
  170. - ngx_rtmp_amf_reverse_copy(buf,
  171. - &len, 4), 4) != NGX_OK)
  172. + *(uint32_t*)buf = htonl(len);
  173. + if (ngx_rtmp_amf_put(ctx, buf, 4) != NGX_OK)
  174. {
  175. return NGX_ERROR;
  176. }
  177. @@ -554,9 +534,7 @@ ngx_rtmp_amf_write(ngx_rtmp_amf_ctx_t *c
  178. switch(type) {
  179. case NGX_RTMP_AMF_NUMBER:
  180. - if (ngx_rtmp_amf_put(ctx,
  181. - ngx_rtmp_amf_reverse_copy(buf,
  182. - data, 8), 8) != NGX_OK)
  183. + if (ngx_rtmp_amf_put(ctx, data, 8) != NGX_OK)
  184. {
  185. return NGX_ERROR;
  186. }
  187. @@ -573,9 +551,8 @@ ngx_rtmp_amf_write(ngx_rtmp_amf_ctx_t *c
  188. len = (uint16_t) ngx_strlen((u_char*) data);
  189. }
  190. - if (ngx_rtmp_amf_put(ctx,
  191. - ngx_rtmp_amf_reverse_copy(buf,
  192. - &len, 2), 2) != NGX_OK)
  193. + *(uint16_t*)buf = htons(len);
  194. + if (ngx_rtmp_amf_put(ctx, buf, 2) != NGX_OK)
  195. {
  196. return NGX_ERROR;
  197. }
  198. @@ -621,18 +598,16 @@ ngx_rtmp_amf_write(ngx_rtmp_amf_ctx_t *c
  199. break;
  200. case NGX_RTMP_AMF_INT16:
  201. - if (ngx_rtmp_amf_put(ctx,
  202. - ngx_rtmp_amf_reverse_copy(buf,
  203. - data, 2), 2) != NGX_OK)
  204. + *(uint16_t*)buf = htons(*(uint16_t*)data);
  205. + if (ngx_rtmp_amf_put(ctx, buf, 2) != NGX_OK)
  206. {
  207. return NGX_ERROR;
  208. }
  209. break;
  210. case NGX_RTMP_AMF_INT32:
  211. - if (ngx_rtmp_amf_put(ctx,
  212. - ngx_rtmp_amf_reverse_copy(buf,
  213. - data, 4), 4) != NGX_OK)
  214. + *(uint32_t*)buf = htonl(*(uint32_t*)data);
  215. + if (ngx_rtmp_amf_put(ctx, buf, 4) != NGX_OK)
  216. {
  217. return NGX_ERROR;
  218. }
  219. --- a/nginx-rtmp/ngx_rtmp_flv_module.c
  220. +++ b/nginx-rtmp/ngx_rtmp_flv_module.c
  221. @@ -102,7 +102,7 @@ ngx_rtmp_flv_fill_index(ngx_rtmp_amf_ctx
  222. return NGX_ERROR;
  223. }
  224. - ngx_rtmp_rmemcpy(&nelts, b->pos + ctx->offset, 4);
  225. + nelts=htonl(*(uint32_t*)(b->pos + ctx->offset));
  226. idx->nelts = nelts;
  227. idx->offset = ctx->offset + 4;
  228. @@ -201,11 +201,7 @@ ngx_rtmp_flv_init_index(ngx_rtmp_session
  229. static double
  230. ngx_rtmp_flv_index_value(void *src)
  231. {
  232. - double v;
  233. -
  234. - ngx_rtmp_rmemcpy(&v, src, 8);
  235. -
  236. - return v;
  237. + return *(double*)src;
  238. }
  239. @@ -352,8 +348,7 @@ ngx_rtmp_flv_read_meta(ngx_rtmp_session_
  240. h.msid = NGX_RTMP_MSID;
  241. h.csid = NGX_RTMP_CSID_AMF;
  242. - size = 0;
  243. - ngx_rtmp_rmemcpy(&size, ngx_rtmp_flv_header + 1, 3);
  244. + size = n3toh4(ngx_rtmp_flv_header + 1);
  245. ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  246. "flv: metadata size=%D", size);
  247. @@ -440,12 +435,9 @@ ngx_rtmp_flv_send(ngx_rtmp_session_t *s,
  248. h.msid = NGX_RTMP_MSID;
  249. h.type = ngx_rtmp_flv_header[0];
  250. - size = 0;
  251. -
  252. - ngx_rtmp_rmemcpy(&size, ngx_rtmp_flv_header + 1, 3);
  253. - ngx_rtmp_rmemcpy(&h.timestamp, ngx_rtmp_flv_header + 4, 3);
  254. -
  255. - ((u_char *) &h.timestamp)[3] = ngx_rtmp_flv_header[7];
  256. + size = n3toh4(ngx_rtmp_flv_header + 1);
  257. + h.timestamp = n3toh4(ngx_rtmp_flv_header + 4);
  258. + h.timestamp |= ((uint32_t) ngx_rtmp_flv_header[7] << 24);
  259. ctx->offset += (sizeof(ngx_rtmp_flv_header) + size + 4);
  260. --- a/nginx-rtmp/ngx_rtmp_handler.c
  261. +++ b/nginx-rtmp/ngx_rtmp_handler.c
  262. @@ -200,7 +200,7 @@ ngx_rtmp_recv(ngx_event_t *rev)
  263. ngx_rtmp_stream_t *st, *st0;
  264. ngx_chain_t *in, *head;
  265. ngx_buf_t *b;
  266. - u_char *p, *pp, *old_pos;
  267. + u_char *p, *old_pos;
  268. size_t size, fsize, old_size;
  269. uint8_t fmt, ext;
  270. uint32_t csid, timestamp;
  271. @@ -308,14 +308,14 @@ ngx_rtmp_recv(ngx_event_t *rev)
  272. if (b->last - p < 1)
  273. continue;
  274. csid = 64;
  275. - csid += *(uint8_t*)p++;
  276. + csid += *p++;
  277. } else if (csid == 1) {
  278. if (b->last - p < 2)
  279. continue;
  280. csid = 64;
  281. - csid += *(uint8_t*)p++;
  282. - csid += (uint32_t)256 * (*(uint8_t*)p++);
  283. + csid += *p++;
  284. + csid += ((uint32_t) *p++ << 8);
  285. }
  286. ngx_log_debug2(NGX_LOG_DEBUG_RTMP, c->log, 0,
  287. @@ -355,40 +355,37 @@ ngx_rtmp_recv(ngx_event_t *rev)
  288. if (fmt <= 2 ) {
  289. if (b->last - p < 3)
  290. continue;
  291. - /* timestamp:
  292. - * big-endian 3b -> little-endian 4b */
  293. - pp = (u_char*)&timestamp;
  294. - pp[2] = *p++;
  295. - pp[1] = *p++;
  296. - pp[0] = *p++;
  297. - pp[3] = 0;
  298. +
  299. + /* timestamp: big-endian 3 bytes */
  300. +
  301. + timestamp = ((uint32_t) *p++ << 16);
  302. + timestamp |= ((uint32_t) *p++ << 8);
  303. + timestamp |= *p++;
  304. ext = (timestamp == 0x00ffffff);
  305. if (fmt <= 1) {
  306. if (b->last - p < 4)
  307. continue;
  308. - /* size:
  309. - * big-endian 3b -> little-endian 4b
  310. - * type:
  311. - * 1b -> 1b*/
  312. - pp = (u_char*)&h->mlen;
  313. - pp[2] = *p++;
  314. - pp[1] = *p++;
  315. - pp[0] = *p++;
  316. - pp[3] = 0;
  317. - h->type = *(uint8_t*)p++;
  318. +
  319. + /* size: big-endian 3 bytes */
  320. +
  321. + h->mlen = ((uint32_t) *p++ << 16);
  322. + h->mlen |= ((uint32_t) *p++ << 8);
  323. + h->mlen |= *p++;
  324. +
  325. + h->type = *p++;
  326. if (fmt == 0) {
  327. if (b->last - p < 4)
  328. continue;
  329. - /* stream:
  330. - * little-endian 4b -> little-endian 4b */
  331. - pp = (u_char*)&h->msid;
  332. - pp[0] = *p++;
  333. - pp[1] = *p++;
  334. - pp[2] = *p++;
  335. - pp[3] = *p++;
  336. +
  337. + /* stream: little-endian 4 bytes */
  338. +
  339. + h->msid = *p++;
  340. + h->msid |= ((uint32_t) *p++ << 8);
  341. + h->msid |= ((uint32_t) *p++ << 16);
  342. + h->msid |= ((uint32_t) *p++ << 24);
  343. }
  344. }
  345. }
  346. @@ -397,13 +394,13 @@ ngx_rtmp_recv(ngx_event_t *rev)
  347. if (ext) {
  348. if (b->last - p < 4)
  349. continue;
  350. - pp = (u_char*)&timestamp;
  351. - /* extented time stamp:
  352. - * big-endian 4b -> little-endian 4b */
  353. - pp[3] = *p++;
  354. - pp[2] = *p++;
  355. - pp[1] = *p++;
  356. - pp[0] = *p++;
  357. +
  358. + /* timestamp: big-endian 4 bytes */
  359. +
  360. + timestamp = ((uint32_t) *p++ << 24);
  361. + timestamp |= ((uint32_t) *p++ << 16);
  362. + timestamp |= ((uint32_t) *p++ << 8);
  363. + timestamp |= *p++;
  364. ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0, "RTMP extended timestamp %uD", (uint32_t)timestamp);
  365. }
  366. @@ -584,7 +581,7 @@ ngx_rtmp_prepare_message(ngx_rtmp_sessio
  367. ngx_rtmp_header_t *lh, ngx_chain_t *out)
  368. {
  369. ngx_chain_t *l;
  370. - u_char *p, *pp;
  371. + u_char *p;
  372. ngx_int_t hsize, thsize, nbufs;
  373. uint32_t mlen, timestamp, ext_timestamp;
  374. static uint8_t hdrsize[] = { 12, 8, 4, 1 };
  375. @@ -677,33 +674,36 @@ ngx_rtmp_prepare_message(ngx_rtmp_sessio
  376. /* message header */
  377. if (fmt <= 2) {
  378. - pp = (u_char*)&timestamp;
  379. - *p++ = pp[2];
  380. - *p++ = pp[1];
  381. - *p++ = pp[0];
  382. +
  383. + *p++ = (u_char) (timestamp >> 16);
  384. + *p++ = (u_char) (timestamp >> 8);
  385. + *p++ = (u_char) timestamp;
  386. +
  387. if (fmt <= 1) {
  388. - pp = (u_char*)&mlen;
  389. - *p++ = pp[2];
  390. - *p++ = pp[1];
  391. - *p++ = pp[0];
  392. +
  393. + *p++ = (u_char) (mlen >> 16);
  394. + *p++ = (u_char) (mlen >> 8);
  395. + *p++ = (u_char) mlen;
  396. +
  397. *p++ = h->type;
  398. +
  399. if (fmt == 0) {
  400. - pp = (u_char*)&h->msid;
  401. - *p++ = pp[0];
  402. - *p++ = pp[1];
  403. - *p++ = pp[2];
  404. - *p++ = pp[3];
  405. +
  406. + *p++ = (u_char) h->msid;
  407. + *p++ = (u_char) (h->msid >> 8);
  408. + *p++ = (u_char) (h->msid >> 16);
  409. + *p++ = (u_char) (h->msid >> 24);
  410. }
  411. }
  412. }
  413. /* extended header */
  414. if (ext_timestamp) {
  415. - pp = (u_char*)&ext_timestamp;
  416. - *p++ = pp[3];
  417. - *p++ = pp[2];
  418. - *p++ = pp[1];
  419. - *p++ = pp[0];
  420. +
  421. + *p++ = (u_char) (ext_timestamp >> 24);
  422. + *p++ = (u_char) (ext_timestamp >> 16);
  423. + *p++ = (u_char) (ext_timestamp >> 8);
  424. + *p++ = (u_char) ext_timestamp;
  425. /* This CONTRADICTS the standard
  426. * but that's the way flash client
  427. --- a/nginx-rtmp/ngx_rtmp_send.c
  428. +++ b/nginx-rtmp/ngx_rtmp_send.c
  429. @@ -33,13 +33,13 @@
  430. *(__b->last++) = (u_char)(utype);
  431. #define NGX_RTMP_USER_OUT1(v) \
  432. - *(__b->last++) = ((u_char*)&v)[0];
  433. + *(__b->last++) = (u_char) v;
  434. #define NGX_RTMP_USER_OUT4(v) \
  435. - *(__b->last++) = ((u_char*)&v)[3]; \
  436. - *(__b->last++) = ((u_char*)&v)[2]; \
  437. - *(__b->last++) = ((u_char*)&v)[1]; \
  438. - *(__b->last++) = ((u_char*)&v)[0];
  439. + *(__b->last++) = (u_char) (v >> 24); \
  440. + *(__b->last++) = (u_char) (v >> 16); \
  441. + *(__b->last++) = (u_char) (v >> 8); \
  442. + *(__b->last++) = (u_char) v;
  443. #define NGX_RTMP_USER_END(s) \
  444. ngx_rtmp_prepare_message(s, &__h, NULL, __l); \
  445. --- a/nginx-rtmp/hls/ngx_rtmp_hls_module.c
  446. +++ b/nginx-rtmp/hls/ngx_rtmp_hls_module.c
  447. @@ -296,7 +296,7 @@ static ngx_command_t ngx_rtmp_hls_comman
  448. ngx_conf_set_enum_slot,
  449. NGX_RTMP_APP_CONF_OFFSET,
  450. offsetof(ngx_rtmp_hls_app_conf_t, allow_client_cache),
  451. - &ngx_rtmp_hls_cache },
  452. + &ngx_rtmp_hls_cache },
  453. { ngx_string("hls_variant"),
  454. NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_1MORE,
  455. @@ -816,7 +816,7 @@ ngx_rtmp_hls_append_sps_pps(ngx_rtmp_ses
  456. return NGX_ERROR;
  457. }
  458. - ngx_rtmp_rmemcpy(&len, &rlen, 2);
  459. + len=ntohs(rlen);
  460. ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  461. "hls: header NAL length: %uz", (size_t) len);
  462. @@ -2072,7 +2072,21 @@ ngx_rtmp_hls_video(ngx_rtmp_session_t *s
  463. }
  464. len = 0;
  465. - ngx_rtmp_rmemcpy(&len, &rlen, nal_bytes);
  466. +
  467. + switch (nal_bytes) {
  468. + case 1:
  469. + len=*(uint8_t*)&rlen;
  470. + break;
  471. + case 2:
  472. + len=ntohs(*(uint16_t*)&rlen);
  473. + break;
  474. + case 3:
  475. + len=n3toh4((u_char*)&rlen);
  476. + break;
  477. + case 4:
  478. + len=ntohl(rlen);
  479. + break;
  480. + };
  481. if (len == 0) {
  482. continue;
  483. --- a/nginx-rtmp/ngx_rtmp_bitop.h
  484. +++ b/nginx-rtmp/ngx_rtmp_bitop.h
  485. @@ -40,7 +40,7 @@ uint64_t ngx_rtmp_bit_read_golomb(ngx_rt
  486. ((uint32_t) ngx_rtmp_bit_read(br, 32))
  487. #define ngx_rtmp_bit_read_64(br) \
  488. - ((uint64_t) ngx_rtmp_read(br, 64))
  489. + ((uint64_t) ngx_rtmp_bit_read(br, 64))
  490. #endif /* _NGX_RTMP_BITOP_H_INCLUDED_ */
  491. --- a/nginx-rtmp/ngx_rtmp_eval.c
  492. +++ b/nginx-rtmp/ngx_rtmp_eval.c
  493. @@ -166,7 +166,7 @@ ngx_rtmp_eval(void *ctx, ngx_str_t *in,
  494. state = ESCAPE;
  495. continue;
  496. }
  497. -
  498. + /* fall through */
  499. case ESCAPE:
  500. ngx_rtmp_eval_append(&b, &c, 1, log);
  501. state = NORMAL;
  502. --- a/nginx-rtmp/ngx_rtmp_handshake.c
  503. +++ b/nginx-rtmp/ngx_rtmp_handshake.c
  504. @@ -264,7 +264,8 @@ ngx_rtmp_handshake_create_challenge(ngx_
  505. b = s->hs_buf;
  506. b->last = b->pos = b->start;
  507. *b->last++ = '\x03';
  508. - b->last = ngx_rtmp_rcpymem(b->last, &s->epoch, 4);
  509. + *(uint32_t*)b->last=htonl(s->epoch);
  510. + b->last +=4;
  511. b->last = ngx_cpymem(b->last, version, 4);
  512. ngx_rtmp_fill_random_buffer(b);
  513. ++b->pos;
  514. @@ -292,8 +293,7 @@ ngx_rtmp_handshake_parse_challenge(ngx_r
  515. return NGX_ERROR;
  516. }
  517. ++b->pos;
  518. - s->peer_epoch = 0;
  519. - ngx_rtmp_rmemcpy(&s->peer_epoch, b->pos, 4);
  520. + s->peer_epoch = ntohl(*(uint32_t*)b->pos);
  521. p = b->pos + 4;
  522. ngx_log_debug5(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  523. --- a/nginx-rtmp/ngx_rtmp_mp4_module.c
  524. +++ b/nginx-rtmp/ngx_rtmp_mp4_module.c
  525. @@ -528,9 +528,9 @@ ngx_rtmp_mp4_parse_mdhd(ngx_rtmp_session
  526. }
  527. pos += 12;
  528. - t->time_scale = ngx_rtmp_r32(*(uint32_t *) pos);
  529. + t->time_scale = ntohl(*(uint32_t *) pos);
  530. pos += 4;
  531. - t->duration = ngx_rtmp_r32(*(uint32_t *) pos);
  532. + t->duration = ntohl(*(uint32_t *) pos);
  533. break;
  534. case 1:
  535. @@ -539,9 +539,9 @@ ngx_rtmp_mp4_parse_mdhd(ngx_rtmp_session
  536. }
  537. pos += 20;
  538. - t->time_scale = ngx_rtmp_r32(*(uint32_t *) pos);
  539. + t->time_scale = ntohl(*(uint32_t *) pos);
  540. pos += 4;
  541. - t->duration = ngx_rtmp_r64(*(uint64_t *) pos);
  542. + t->duration = ntohll(*(uint64_t *) pos);
  543. break;
  544. default:
  545. @@ -616,11 +616,11 @@ ngx_rtmp_mp4_parse_video(ngx_rtmp_sessio
  546. pos += 24;
  547. - ctx->width = ngx_rtmp_r16(*(uint16_t *) pos);
  548. + ctx->width = ntohs(*(uint16_t *) pos);
  549. pos += 2;
  550. - ctx->height = ngx_rtmp_r16(*(uint16_t *) pos);
  551. + ctx->height = ntohs(*(uint16_t *) pos);
  552. pos += 52;
  553. @@ -660,19 +660,19 @@ ngx_rtmp_mp4_parse_audio(ngx_rtmp_sessio
  554. pos += 8;
  555. - version = ngx_rtmp_r16(*(uint16_t *) pos);
  556. + version = ntohs(*(uint16_t *) pos);
  557. pos += 8;
  558. - ctx->nchannels = ngx_rtmp_r16(*(uint16_t *) pos);
  559. + ctx->nchannels = ntohs(*(uint16_t *) pos);
  560. pos += 2;
  561. - ctx->sample_size = ngx_rtmp_r16(*(uint16_t *) pos);
  562. + ctx->sample_size = ntohs(*(uint16_t *) pos);
  563. pos += 6;
  564. - ctx->sample_rate = ngx_rtmp_r16(*(uint16_t *) pos);
  565. + ctx->sample_rate = ntohs(*(uint16_t *) pos);
  566. pos += 4;
  567. @@ -862,7 +862,7 @@ ngx_rtmp_mp4_parse_es(ngx_rtmp_session_t
  568. return NGX_ERROR;
  569. }
  570. - id = ngx_rtmp_r16(*(uint16_t *) pos);
  571. + id = ntohs(*(uint16_t *) pos);
  572. pos += 2;
  573. flags = *(uint8_t *) pos;
  574. @@ -1018,13 +1018,13 @@ ngx_rtmp_mp4_parse_stsc(ngx_rtmp_session
  575. t->chunks = (ngx_rtmp_mp4_chunks_t *) pos;
  576. - if (pos + sizeof(*t->chunks) + ngx_rtmp_r32(t->chunks->entry_count) *
  577. + if (pos + sizeof(*t->chunks) + ntohl(t->chunks->entry_count) *
  578. sizeof(t->chunks->entries[0])
  579. <= last)
  580. {
  581. ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  582. "mp4: chunks entries=%uD",
  583. - ngx_rtmp_r32(t->chunks->entry_count));
  584. + ntohl(t->chunks->entry_count));
  585. return NGX_OK;
  586. }
  587. @@ -1049,13 +1049,13 @@ ngx_rtmp_mp4_parse_stts(ngx_rtmp_session
  588. t->times = (ngx_rtmp_mp4_times_t *) pos;
  589. - if (pos + sizeof(*t->times) + ngx_rtmp_r32(t->times->entry_count) *
  590. + if (pos + sizeof(*t->times) + ntohl(t->times->entry_count) *
  591. sizeof(t->times->entries[0])
  592. <= last)
  593. {
  594. ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  595. "mp4: times entries=%uD",
  596. - ngx_rtmp_r32(t->times->entry_count));
  597. + ntohl(t->times->entry_count));
  598. return NGX_OK;
  599. }
  600. @@ -1080,13 +1080,13 @@ ngx_rtmp_mp4_parse_ctts(ngx_rtmp_session
  601. t->delays = (ngx_rtmp_mp4_delays_t *) pos;
  602. - if (pos + sizeof(*t->delays) + ngx_rtmp_r32(t->delays->entry_count) *
  603. + if (pos + sizeof(*t->delays) + ntohl(t->delays->entry_count) *
  604. sizeof(t->delays->entries[0])
  605. <= last)
  606. {
  607. ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  608. "mp4: delays entries=%uD",
  609. - ngx_rtmp_r32(t->delays->entry_count));
  610. + ntohl(t->delays->entry_count));
  611. return NGX_OK;
  612. }
  613. @@ -1111,13 +1111,13 @@ ngx_rtmp_mp4_parse_stss(ngx_rtmp_session
  614. t->keys = (ngx_rtmp_mp4_keys_t *) pos;
  615. - if (pos + sizeof(*t->keys) + ngx_rtmp_r32(t->keys->entry_count) *
  616. + if (pos + sizeof(*t->keys) + ntohl(t->keys->entry_count) *
  617. sizeof(t->keys->entries[0])
  618. <= last)
  619. {
  620. ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  621. "mp4: keys entries=%uD",
  622. - ngx_rtmp_r32(t->keys->entry_count));
  623. + ntohl(t->keys->entry_count));
  624. return NGX_OK;
  625. }
  626. @@ -1145,18 +1145,18 @@ ngx_rtmp_mp4_parse_stsz(ngx_rtmp_session
  627. if (pos + sizeof(*t->sizes) <= last && t->sizes->sample_size) {
  628. ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  629. "mp4: sizes size=%uD",
  630. - ngx_rtmp_r32(t->sizes->sample_size));
  631. + ntohl(t->sizes->sample_size));
  632. return NGX_OK;
  633. }
  634. - if (pos + sizeof(*t->sizes) + ngx_rtmp_r32(t->sizes->sample_count) *
  635. + if (pos + sizeof(*t->sizes) + ntohl(t->sizes->sample_count) *
  636. sizeof(t->sizes->entries[0])
  637. <= last)
  638. {
  639. ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  640. "mp4: sizes entries=%uD",
  641. - ngx_rtmp_r32(t->sizes->sample_count));
  642. + ntohl(t->sizes->sample_count));
  643. return NGX_OK;
  644. }
  645. @@ -1181,14 +1181,14 @@ ngx_rtmp_mp4_parse_stz2(ngx_rtmp_session
  646. t->sizes2 = (ngx_rtmp_mp4_sizes2_t *) pos;
  647. - if (pos + sizeof(*t->sizes) + ngx_rtmp_r32(t->sizes2->sample_count) *
  648. - ngx_rtmp_r32(t->sizes2->field_size) / 8
  649. + if (pos + sizeof(*t->sizes) + ntohl(t->sizes2->sample_count) *
  650. + ntohl(t->sizes2->field_size) / 8
  651. <= last)
  652. {
  653. ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  654. "mp4: sizes2 field_size=%uD entries=%uD",
  655. - ngx_rtmp_r32(t->sizes2->field_size),
  656. - ngx_rtmp_r32(t->sizes2->sample_count));
  657. + ntohl(t->sizes2->field_size),
  658. + ntohl(t->sizes2->sample_count));
  659. return NGX_OK;
  660. }
  661. @@ -1213,13 +1213,13 @@ ngx_rtmp_mp4_parse_stco(ngx_rtmp_session
  662. t->offsets = (ngx_rtmp_mp4_offsets_t *) pos;
  663. - if (pos + sizeof(*t->offsets) + ngx_rtmp_r32(t->offsets->entry_count) *
  664. + if (pos + sizeof(*t->offsets) + ntohl(t->offsets->entry_count) *
  665. sizeof(t->offsets->entries[0])
  666. <= last)
  667. {
  668. ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  669. "mp4: offsets entries=%uD",
  670. - ngx_rtmp_r32(t->offsets->entry_count));
  671. + ntohl(t->offsets->entry_count));
  672. return NGX_OK;
  673. }
  674. @@ -1244,13 +1244,13 @@ ngx_rtmp_mp4_parse_co64(ngx_rtmp_session
  675. t->offsets64 = (ngx_rtmp_mp4_offsets64_t *) pos;
  676. - if (pos + sizeof(*t->offsets64) + ngx_rtmp_r32(t->offsets64->entry_count) *
  677. + if (pos + sizeof(*t->offsets64) + ntohl(t->offsets64->entry_count) *
  678. sizeof(t->offsets64->entries[0])
  679. <= last)
  680. {
  681. ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  682. "mp4: offsets64 entries=%uD",
  683. - ngx_rtmp_r32(t->offsets64->entry_count));
  684. + ntohl(t->offsets64->entry_count));
  685. return NGX_OK;
  686. }
  687. @@ -1275,7 +1275,7 @@ ngx_rtmp_mp4_parse(ngx_rtmp_session_t *s
  688. }
  689. hdr = (uint32_t *) pos;
  690. - size = ngx_rtmp_r32(hdr[0]);
  691. + size = ntohl(hdr[0]);
  692. tag = hdr[1];
  693. if (pos + size > last) {
  694. @@ -1318,11 +1318,11 @@ ngx_rtmp_mp4_next_time(ngx_rtmp_session_
  695. cr = &t->cursor;
  696. - if (cr->time_pos >= ngx_rtmp_r32(t->times->entry_count)) {
  697. + if (cr->time_pos >= ntohl(t->times->entry_count)) {
  698. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  699. "mp4: track#%ui time[%ui/%uD] overflow",
  700. t->id, cr->time_pos,
  701. - ngx_rtmp_r32(t->times->entry_count));
  702. + ntohl(t->times->entry_count));
  703. return NGX_ERROR;
  704. }
  705. @@ -1330,22 +1330,22 @@ ngx_rtmp_mp4_next_time(ngx_rtmp_session_
  706. te = &t->times->entries[cr->time_pos];
  707. cr->last_timestamp = cr->timestamp;
  708. - cr->timestamp += ngx_rtmp_r32(te->sample_delta);
  709. + cr->timestamp += ntohl(te->sample_delta);
  710. cr->not_first = 1;
  711. ngx_log_debug8(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  712. "mp4: track#%ui time[%ui] [%ui/%uD][%ui/%uD]=%uD t=%uD",
  713. t->id, cr->pos, cr->time_pos,
  714. - ngx_rtmp_r32(t->times->entry_count),
  715. - cr->time_count, ngx_rtmp_r32(te->sample_count),
  716. - ngx_rtmp_r32(te->sample_delta),
  717. + ntohl(t->times->entry_count),
  718. + cr->time_count, ntohl(te->sample_count),
  719. + ntohl(te->sample_delta),
  720. cr->timestamp);
  721. cr->time_count++;
  722. cr->pos++;
  723. - if (cr->time_count >= ngx_rtmp_r32(te->sample_count)) {
  724. + if (cr->time_count >= ntohl(te->sample_count)) {
  725. cr->time_pos++;
  726. cr->time_count = 0;
  727. }
  728. @@ -1370,8 +1370,8 @@ ngx_rtmp_mp4_seek_time(ngx_rtmp_session_
  729. te = t->times->entries;
  730. - while (cr->time_pos < ngx_rtmp_r32(t->times->entry_count)) {
  731. - dt = ngx_rtmp_r32(te->sample_delta) * ngx_rtmp_r32(te->sample_count);
  732. + while (cr->time_pos < ntohl(t->times->entry_count)) {
  733. + dt = ntohl(te->sample_delta) * ntohl(te->sample_count);
  734. if (cr->timestamp + dt >= timestamp) {
  735. if (te->sample_delta == 0) {
  736. @@ -1379,24 +1379,24 @@ ngx_rtmp_mp4_seek_time(ngx_rtmp_session_
  737. }
  738. cr->time_count = (timestamp - cr->timestamp) /
  739. - ngx_rtmp_r32(te->sample_delta);
  740. - cr->timestamp += ngx_rtmp_r32(te->sample_delta) * cr->time_count;
  741. + ntohl(te->sample_delta);
  742. + cr->timestamp += ntohl(te->sample_delta) * cr->time_count;
  743. cr->pos += cr->time_count;
  744. break;
  745. }
  746. cr->timestamp += dt;
  747. - cr->pos += ngx_rtmp_r32(te->sample_count);
  748. + cr->pos += ntohl(te->sample_count);
  749. cr->time_pos++;
  750. te++;
  751. }
  752. - if (cr->time_pos >= ngx_rtmp_r32(t->times->entry_count)) {
  753. + if (cr->time_pos >= ntohl(t->times->entry_count)) {
  754. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  755. "mp4: track#%ui seek time[%ui/%uD] overflow",
  756. t->id, cr->time_pos,
  757. - ngx_rtmp_r32(t->times->entry_count));
  758. + ntohl(t->times->entry_count));
  759. return NGX_ERROR;
  760. }
  761. @@ -1405,10 +1405,10 @@ ngx_rtmp_mp4_seek_time(ngx_rtmp_session_
  762. "mp4: track#%ui seek time[%ui] [%ui/%uD][%ui/%uD]=%uD "
  763. "t=%uD",
  764. t->id, cr->pos, cr->time_pos,
  765. - ngx_rtmp_r32(t->times->entry_count),
  766. + ntohl(t->times->entry_count),
  767. cr->time_count,
  768. - ngx_rtmp_r32(te->sample_count),
  769. - ngx_rtmp_r32(te->sample_delta),
  770. + ntohl(te->sample_count),
  771. + ntohl(te->sample_delta),
  772. cr->timestamp);
  773. return NGX_OK;
  774. @@ -1433,44 +1433,44 @@ ngx_rtmp_mp4_update_offset(ngx_rtmp_sess
  775. chunk = cr->chunk - 1;
  776. if (t->offsets) {
  777. - if (chunk >= ngx_rtmp_r32(t->offsets->entry_count)) {
  778. + if (chunk >= ntohl(t->offsets->entry_count)) {
  779. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  780. "mp4: track#%ui offset[%ui/%uD] overflow",
  781. t->id, cr->chunk,
  782. - ngx_rtmp_r32(t->offsets->entry_count));
  783. + ntohl(t->offsets->entry_count));
  784. return NGX_ERROR;
  785. }
  786. - cr->offset = (off_t) ngx_rtmp_r32(t->offsets->entries[chunk]);
  787. + cr->offset = (off_t) ntohl(t->offsets->entries[chunk]);
  788. cr->size = 0;
  789. ngx_log_debug4(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  790. "mp4: track#%ui offset[%ui/%uD]=%O",
  791. t->id, cr->chunk,
  792. - ngx_rtmp_r32(t->offsets->entry_count),
  793. + ntohl(t->offsets->entry_count),
  794. cr->offset);
  795. return NGX_OK;
  796. }
  797. if (t->offsets64) {
  798. - if (chunk >= ngx_rtmp_r32(t->offsets64->entry_count)) {
  799. + if (chunk >= ntohl(t->offsets64->entry_count)) {
  800. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  801. "mp4: track#%ui offset64[%ui/%uD] overflow",
  802. t->id, cr->chunk,
  803. - ngx_rtmp_r32(t->offsets->entry_count));
  804. + ntohl(t->offsets->entry_count));
  805. return NGX_ERROR;
  806. }
  807. - cr->offset = (off_t) ngx_rtmp_r64(t->offsets64->entries[chunk]);
  808. + cr->offset = (off_t) ntohll(t->offsets64->entries[chunk]);
  809. cr->size = 0;
  810. ngx_log_debug4(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  811. "mp4: track#%ui offset64[%ui/%uD]=%O",
  812. t->id, cr->chunk,
  813. - ngx_rtmp_r32(t->offsets->entry_count),
  814. + ntohl(t->offsets->entry_count),
  815. cr->offset);
  816. return NGX_OK;
  817. @@ -1493,11 +1493,11 @@ ngx_rtmp_mp4_next_chunk(ngx_rtmp_session
  818. cr = &t->cursor;
  819. - if (cr->chunk_pos >= ngx_rtmp_r32(t->chunks->entry_count)) {
  820. + if (cr->chunk_pos >= ntohl(t->chunks->entry_count)) {
  821. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  822. "mp4: track#%ui chunk[%ui/%uD] overflow",
  823. t->id, cr->chunk_pos,
  824. - ngx_rtmp_r32(t->chunks->entry_count));
  825. + ntohl(t->chunks->entry_count));
  826. return NGX_ERROR;
  827. }
  828. @@ -1506,13 +1506,13 @@ ngx_rtmp_mp4_next_chunk(ngx_rtmp_session
  829. cr->chunk_count++;
  830. - if (cr->chunk_count >= ngx_rtmp_r32(ce->samples_per_chunk)) {
  831. + if (cr->chunk_count >= ntohl(ce->samples_per_chunk)) {
  832. cr->chunk_count = 0;
  833. cr->chunk++;
  834. - if (cr->chunk_pos + 1 < ngx_rtmp_r32(t->chunks->entry_count)) {
  835. + if (cr->chunk_pos + 1 < ntohl(t->chunks->entry_count)) {
  836. nce = ce + 1;
  837. - if (cr->chunk >= ngx_rtmp_r32(nce->first_chunk)) {
  838. + if (cr->chunk >= ntohl(nce->first_chunk)) {
  839. cr->chunk_pos++;
  840. ce = nce;
  841. }
  842. @@ -1527,10 +1527,10 @@ ngx_rtmp_mp4_next_chunk(ngx_rtmp_session
  843. ngx_log_debug7(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  844. "mp4: track#%ui chunk[%ui/%uD][%uD..%ui][%ui/%uD]",
  845. t->id, cr->chunk_pos,
  846. - ngx_rtmp_r32(t->chunks->entry_count),
  847. - ngx_rtmp_r32(ce->first_chunk),
  848. + ntohl(t->chunks->entry_count),
  849. + ntohl(ce->first_chunk),
  850. cr->chunk, cr->chunk_count,
  851. - ngx_rtmp_r32(ce->samples_per_chunk));
  852. + ntohl(ce->samples_per_chunk));
  853. if (new_chunk) {
  854. @@ -1558,12 +1558,12 @@ ngx_rtmp_mp4_seek_chunk(ngx_rtmp_session
  855. ce = t->chunks->entries;
  856. pos = 0;
  857. - while (cr->chunk_pos + 1 < ngx_rtmp_r32(t->chunks->entry_count)) {
  858. + while (cr->chunk_pos + 1 < ntohl(t->chunks->entry_count)) {
  859. nce = ce + 1;
  860. - dpos = (ngx_rtmp_r32(nce->first_chunk) -
  861. - ngx_rtmp_r32(ce->first_chunk)) *
  862. - ngx_rtmp_r32(ce->samples_per_chunk);
  863. + dpos = (ntohl(nce->first_chunk) -
  864. + ntohl(ce->first_chunk)) *
  865. + ntohl(ce->samples_per_chunk);
  866. if (pos + dpos > cr->pos) {
  867. break;
  868. @@ -1578,20 +1578,20 @@ ngx_rtmp_mp4_seek_chunk(ngx_rtmp_session
  869. return NGX_ERROR;
  870. }
  871. - dchunk = (cr->pos - pos) / ngx_rtmp_r32(ce->samples_per_chunk);
  872. + dchunk = (cr->pos - pos) / ntohl(ce->samples_per_chunk);
  873. - cr->chunk = ngx_rtmp_r32(ce->first_chunk) + dchunk;
  874. + cr->chunk = ntohl(ce->first_chunk) + dchunk;
  875. cr->chunk_pos = (ngx_uint_t) (ce - t->chunks->entries);
  876. cr->chunk_count = (ngx_uint_t) (cr->pos - pos - dchunk *
  877. - ngx_rtmp_r32(ce->samples_per_chunk));
  878. + ntohl(ce->samples_per_chunk));
  879. ngx_log_debug7(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  880. "mp4: track#%ui seek chunk[%ui/%uD][%uD..%ui][%ui/%uD]",
  881. t->id, cr->chunk_pos,
  882. - ngx_rtmp_r32(t->chunks->entry_count),
  883. - ngx_rtmp_r32(ce->first_chunk),
  884. + ntohl(t->chunks->entry_count),
  885. + ntohl(ce->first_chunk),
  886. cr->chunk, cr->chunk_count,
  887. - ngx_rtmp_r32(ce->samples_per_chunk));
  888. + ntohl(ce->samples_per_chunk));
  889. return ngx_rtmp_mp4_update_offset(s, t);
  890. }
  891. @@ -1608,7 +1608,7 @@ ngx_rtmp_mp4_next_size(ngx_rtmp_session_
  892. if (t->sizes) {
  893. if (t->sizes->sample_size) {
  894. - cr->size = ngx_rtmp_r32(t->sizes->sample_size);
  895. + cr->size = ntohl(t->sizes->sample_size);
  896. ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  897. "mp4: track#%ui size fix=%uz",
  898. @@ -1619,32 +1619,32 @@ ngx_rtmp_mp4_next_size(ngx_rtmp_session_
  899. cr->size_pos++;
  900. - if (cr->size_pos >= ngx_rtmp_r32(t->sizes->sample_count)) {
  901. + if (cr->size_pos >= ntohl(t->sizes->sample_count)) {
  902. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  903. "mp4: track#%ui size[%ui/%uD] overflow",
  904. t->id, cr->size_pos,
  905. - ngx_rtmp_r32(t->sizes->sample_count));
  906. + ntohl(t->sizes->sample_count));
  907. return NGX_ERROR;
  908. }
  909. - cr->size = ngx_rtmp_r32(t->sizes->entries[cr->size_pos]);
  910. + cr->size = ntohl(t->sizes->entries[cr->size_pos]);
  911. ngx_log_debug4(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  912. "mp4: track#%ui size[%ui/%uD]=%uz",
  913. t->id, cr->size_pos,
  914. - ngx_rtmp_r32(t->sizes->sample_count),
  915. + ntohl(t->sizes->sample_count),
  916. cr->size);
  917. return NGX_OK;
  918. }
  919. if (t->sizes2) {
  920. - if (cr->size_pos >= ngx_rtmp_r32(t->sizes2->sample_count)) {
  921. + if (cr->size_pos >= ntohl(t->sizes2->sample_count)) {
  922. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  923. "mp4: track#%ui size[%ui/%uD] overflow",
  924. t->id, cr->size_pos,
  925. - ngx_rtmp_r32(t->sizes2->sample_count));
  926. + ntohl(t->sizes2->sample_count));
  927. return NGX_ERROR;
  928. }
  929. @@ -1672,7 +1672,7 @@ ngx_rtmp_mp4_seek_size(ngx_rtmp_session_
  930. if (t->sizes) {
  931. if (t->sizes->sample_size) {
  932. - cr->size = ngx_rtmp_r32(t->sizes->sample_size);
  933. + cr->size = ntohl(t->sizes->sample_size);
  934. cr->offset += cr->size * cr->chunk_count;
  935. @@ -1683,37 +1683,37 @@ ngx_rtmp_mp4_seek_size(ngx_rtmp_session_
  936. return NGX_OK;
  937. }
  938. - if (cr->pos >= ngx_rtmp_r32(t->sizes->sample_count)) {
  939. + if (cr->pos >= ntohl(t->sizes->sample_count)) {
  940. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  941. "mp4: track#%ui seek size[%ui/%uD] overflow",
  942. t->id, cr->pos,
  943. - ngx_rtmp_r32(t->sizes->sample_count));
  944. + ntohl(t->sizes->sample_count));
  945. return NGX_ERROR;
  946. }
  947. for (pos = 1; pos <= cr->chunk_count; ++pos) {
  948. - cr->offset += ngx_rtmp_r32(t->sizes->entries[cr->pos - pos]);
  949. + cr->offset += ntohl(t->sizes->entries[cr->pos - pos]);
  950. }
  951. cr->size_pos = cr->pos;
  952. - cr->size = ngx_rtmp_r32(t->sizes->entries[cr->size_pos]);
  953. + cr->size = ntohl(t->sizes->entries[cr->size_pos]);
  954. ngx_log_debug4(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  955. "mp4: track#%ui seek size[%ui/%uD]=%uz",
  956. t->id, cr->size_pos,
  957. - ngx_rtmp_r32(t->sizes->sample_count),
  958. + ntohl(t->sizes->sample_count),
  959. cr->size);
  960. return NGX_OK;
  961. }
  962. if (t->sizes2) {
  963. - if (cr->size_pos >= ngx_rtmp_r32(t->sizes2->sample_count)) {
  964. + if (cr->size_pos >= ntohl(t->sizes2->sample_count)) {
  965. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  966. "mp4: track#%ui seek size2[%ui/%uD] overflow",
  967. t->id, cr->size_pos,
  968. - ngx_rtmp_r32(t->sizes->sample_count));
  969. + ntohl(t->sizes->sample_count));
  970. return NGX_ERROR;
  971. }
  972. @@ -1744,11 +1744,11 @@ ngx_rtmp_mp4_next_key(ngx_rtmp_session_t
  973. cr->key_pos++;
  974. }
  975. - if (cr->key_pos >= ngx_rtmp_r32(t->keys->entry_count)) {
  976. + if (cr->key_pos >= ntohl(t->keys->entry_count)) {
  977. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  978. "mp4: track#%ui key[%ui/%uD] overflow",
  979. t->id, cr->key_pos,
  980. - ngx_rtmp_r32(t->keys->entry_count));
  981. + ntohl(t->keys->entry_count));
  982. cr->key = 0;
  983. @@ -1756,13 +1756,13 @@ ngx_rtmp_mp4_next_key(ngx_rtmp_session_t
  984. }
  985. ke = &t->keys->entries[cr->key_pos];
  986. - cr->key = (cr->pos + 1 == ngx_rtmp_r32(*ke));
  987. + cr->key = (cr->pos + 1 == ntohl(*ke));
  988. ngx_log_debug6(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  989. "mp4: track#%ui key[%ui/%uD][%ui/%uD]=%s",
  990. t->id, cr->key_pos,
  991. - ngx_rtmp_r32(t->keys->entry_count),
  992. - cr->pos, ngx_rtmp_r32(*ke),
  993. + ntohl(t->keys->entry_count),
  994. + cr->pos, ntohl(*ke),
  995. cr->key ? "match" : "miss");
  996. return NGX_OK;
  997. @@ -1782,27 +1782,27 @@ ngx_rtmp_mp4_seek_key(ngx_rtmp_session_t
  998. return NGX_OK;
  999. }
  1000. - while (cr->key_pos < ngx_rtmp_r32(t->keys->entry_count)) {
  1001. - if (ngx_rtmp_r32(t->keys->entries[cr->key_pos]) > cr->pos) {
  1002. + while (cr->key_pos < ntohl(t->keys->entry_count)) {
  1003. + if (ntohl(t->keys->entries[cr->key_pos]) > cr->pos) {
  1004. break;
  1005. }
  1006. cr->key_pos++;
  1007. }
  1008. - if (cr->key_pos >= ngx_rtmp_r32(t->keys->entry_count)) {
  1009. + if (cr->key_pos >= ntohl(t->keys->entry_count)) {
  1010. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  1011. "mp4: track#%ui seek key[%ui/%uD] overflow",
  1012. t->id, cr->key_pos,
  1013. - ngx_rtmp_r32(t->keys->entry_count));
  1014. + ntohl(t->keys->entry_count));
  1015. return NGX_OK;
  1016. }
  1017. ke = &t->keys->entries[cr->key_pos];
  1018. - /*cr->key = (cr->pos + 1 == ngx_rtmp_r32(*ke));*/
  1019. + /*cr->key = (cr->pos + 1 == ntohl(*ke));*/
  1020. /* distance to the next keyframe */
  1021. - dpos = ngx_rtmp_r32(*ke) - cr->pos - 1;
  1022. + dpos = ntohl(*ke) - cr->pos - 1;
  1023. cr->key = 1;
  1024. /* TODO: range version needed */
  1025. @@ -1810,13 +1810,13 @@ ngx_rtmp_mp4_seek_key(ngx_rtmp_session_t
  1026. ngx_rtmp_mp4_next_time(s, t);
  1027. }
  1028. -/* cr->key = (cr->pos + 1 == ngx_rtmp_r32(*ke));*/
  1029. +/* cr->key = (cr->pos + 1 == ntohl(*ke));*/
  1030. ngx_log_debug6(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  1031. "mp4: track#%ui seek key[%ui/%uD][%ui/%uD]=%s",
  1032. t->id, cr->key_pos,
  1033. - ngx_rtmp_r32(t->keys->entry_count),
  1034. - cr->pos, ngx_rtmp_r32(*ke),
  1035. + ntohl(t->keys->entry_count),
  1036. + cr->pos, ntohl(*ke),
  1037. cr->key ? "match" : "miss");
  1038. return NGX_OK;
  1039. @@ -1835,11 +1835,11 @@ ngx_rtmp_mp4_next_delay(ngx_rtmp_session
  1040. return NGX_OK;
  1041. }
  1042. - if (cr->delay_pos >= ngx_rtmp_r32(t->delays->entry_count)) {
  1043. + if (cr->delay_pos >= ntohl(t->delays->entry_count)) {
  1044. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  1045. "mp4: track#%ui delay[%ui/%uD] overflow",
  1046. t->id, cr->delay_pos,
  1047. - ngx_rtmp_r32(t->delays->entry_count));
  1048. + ntohl(t->delays->entry_count));
  1049. return NGX_OK;
  1050. }
  1051. @@ -1847,29 +1847,29 @@ ngx_rtmp_mp4_next_delay(ngx_rtmp_session
  1052. cr->delay_count++;
  1053. de = &t->delays->entries[cr->delay_pos];
  1054. - if (cr->delay_count >= ngx_rtmp_r32(de->sample_count)) {
  1055. + if (cr->delay_count >= ntohl(de->sample_count)) {
  1056. cr->delay_pos++;
  1057. de++;
  1058. cr->delay_count = 0;
  1059. }
  1060. - if (cr->delay_pos >= ngx_rtmp_r32(t->delays->entry_count)) {
  1061. + if (cr->delay_pos >= ntohl(t->delays->entry_count)) {
  1062. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  1063. "mp4: track#%ui delay[%ui/%uD] overflow",
  1064. t->id, cr->delay_pos,
  1065. - ngx_rtmp_r32(t->delays->entry_count));
  1066. + ntohl(t->delays->entry_count));
  1067. return NGX_OK;
  1068. }
  1069. - cr->delay = ngx_rtmp_r32(de->sample_offset);
  1070. + cr->delay = ntohl(de->sample_offset);
  1071. ngx_log_debug6(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  1072. "mp4: track#%ui delay[%ui/%uD][%ui/%uD]=%ui",
  1073. t->id, cr->delay_pos,
  1074. - ngx_rtmp_r32(t->delays->entry_count),
  1075. + ntohl(t->delays->entry_count),
  1076. cr->delay_count,
  1077. - ngx_rtmp_r32(de->sample_count), cr->delay);
  1078. + ntohl(de->sample_count), cr->delay);
  1079. return NGX_OK;
  1080. }
  1081. @@ -1891,12 +1891,12 @@ ngx_rtmp_mp4_seek_delay(ngx_rtmp_session
  1082. pos = 0;
  1083. de = t->delays->entries;
  1084. - while (cr->delay_pos < ngx_rtmp_r32(t->delays->entry_count)) {
  1085. - dpos = ngx_rtmp_r32(de->sample_count);
  1086. + while (cr->delay_pos < ntohl(t->delays->entry_count)) {
  1087. + dpos = ntohl(de->sample_count);
  1088. if (pos + dpos > cr->pos) {
  1089. cr->delay_count = cr->pos - pos;
  1090. - cr->delay = ngx_rtmp_r32(de->sample_offset);
  1091. + cr->delay = ntohl(de->sample_offset);
  1092. break;
  1093. }
  1094. @@ -1905,11 +1905,11 @@ ngx_rtmp_mp4_seek_delay(ngx_rtmp_session
  1095. de++;
  1096. }
  1097. - if (cr->delay_pos >= ngx_rtmp_r32(t->delays->entry_count)) {
  1098. + if (cr->delay_pos >= ntohl(t->delays->entry_count)) {
  1099. ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  1100. "mp4: track#%ui seek delay[%ui/%uD] overflow",
  1101. t->id, cr->delay_pos,
  1102. - ngx_rtmp_r32(t->delays->entry_count));
  1103. + ntohl(t->delays->entry_count));
  1104. return NGX_OK;
  1105. }
  1106. @@ -1917,9 +1917,9 @@ ngx_rtmp_mp4_seek_delay(ngx_rtmp_session
  1107. ngx_log_debug6(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  1108. "mp4: track#%ui seek delay[%ui/%uD][%ui/%uD]=%ui",
  1109. t->id, cr->delay_pos,
  1110. - ngx_rtmp_r32(t->delays->entry_count),
  1111. + ntohl(t->delays->entry_count),
  1112. cr->delay_count,
  1113. - ngx_rtmp_r32(de->sample_count), cr->delay);
  1114. + ntohl(de->sample_count), cr->delay);
  1115. return NGX_OK;
  1116. }
  1117. @@ -2348,7 +2348,7 @@ ngx_rtmp_mp4_init(ngx_rtmp_session_t *s,
  1118. return NGX_ERROR;
  1119. }
  1120. - size = (size_t) ngx_rtmp_r32(hdr[0]);
  1121. + size = (size_t) ntohl(hdr[0]);
  1122. shift = sizeof(hdr);
  1123. if (size == 1) {
  1124. @@ -2362,7 +2362,7 @@ ngx_rtmp_mp4_init(ngx_rtmp_session_t *s,
  1125. return NGX_ERROR;
  1126. }
  1127. - size = (size_t) ngx_rtmp_r64(extended_size);
  1128. + size = (size_t) ntohll(extended_size);
  1129. shift += sizeof(extended_size);
  1130. } else if (size == 0) {
  1131. --- a/nginx-rtmp/ngx_rtmp_receive.c
  1132. +++ b/nginx-rtmp/ngx_rtmp_receive.c
  1133. @@ -17,7 +17,6 @@ ngx_rtmp_protocol_message_handler(ngx_rt
  1134. ngx_rtmp_header_t *h, ngx_chain_t *in)
  1135. {
  1136. ngx_buf_t *b;
  1137. - u_char *p;
  1138. uint32_t val;
  1139. uint8_t limit;
  1140. @@ -30,11 +29,7 @@ ngx_rtmp_protocol_message_handler(ngx_rt
  1141. return NGX_OK;
  1142. }
  1143. - p = (u_char*)&val;
  1144. - p[0] = b->pos[3];
  1145. - p[1] = b->pos[2];
  1146. - p[2] = b->pos[1];
  1147. - p[3] = b->pos[0];
  1148. + val=ntohl(*(uint32_t*)&b->pos[0]);
  1149. switch(h->type) {
  1150. case NGX_RTMP_MSG_CHUNK_SIZE:
  1151. @@ -88,7 +83,6 @@ ngx_rtmp_user_message_handler(ngx_rtmp_s
  1152. ngx_chain_t *in)
  1153. {
  1154. ngx_buf_t *b;
  1155. - u_char *p;
  1156. uint16_t evt;
  1157. uint32_t val;
  1158. @@ -101,21 +95,13 @@ ngx_rtmp_user_message_handler(ngx_rtmp_s
  1159. return NGX_OK;
  1160. }
  1161. - p = (u_char*)&evt;
  1162. -
  1163. - p[0] = b->pos[1];
  1164. - p[1] = b->pos[0];
  1165. + evt=ntohs(*(uint16_t*)&b->pos[0]);
  1166. ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  1167. "RTMP recv user evt %s (%i)",
  1168. ngx_rtmp_user_message_type(evt), (ngx_int_t) evt);
  1169. - p = (u_char *) &val;
  1170. -
  1171. - p[0] = b->pos[5];
  1172. - p[1] = b->pos[4];
  1173. - p[2] = b->pos[3];
  1174. - p[3] = b->pos[2];
  1175. + val=ntohl(*(uint32_t*)&b->pos[2]);
  1176. switch(evt) {
  1177. case NGX_RTMP_USER_STREAM_BEGIN:
  1178. @@ -164,12 +150,7 @@ ngx_rtmp_user_message_handler(ngx_rtmp_s
  1179. return NGX_OK;
  1180. }
  1181. - p = (u_char *) &v.buflen;
  1182. -
  1183. - p[0] = b->pos[9];
  1184. - p[1] = b->pos[8];
  1185. - p[2] = b->pos[7];
  1186. - p[3] = b->pos[6];
  1187. + v.buflen=ntohl(*(uint32_t*)&b->pos[6]);
  1188. ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
  1189. "receive: set_buflen msid=%uD buflen=%uD",
  1190. @@ -240,18 +221,20 @@ ngx_rtmp_fetch_uint8(ngx_chain_t **in, u
  1191. static ngx_int_t
  1192. ngx_rtmp_fetch_uint32(ngx_chain_t **in, uint32_t *ret, ngx_int_t n)
  1193. {
  1194. - u_char *r = (u_char *) ret;
  1195. + u_char b;
  1196. + uint32_t val=0;
  1197. ngx_int_t rc;
  1198. - *ret = 0;
  1199. -
  1200. while (--n >= 0) {
  1201. - rc = ngx_rtmp_fetch(in, &r[n]);
  1202. + rc = ngx_rtmp_fetch(in, &b);
  1203. if (rc != NGX_OK) {
  1204. + *ret = 0;
  1205. return rc;
  1206. }
  1207. + val = (val<<8)|b;
  1208. }
  1209. + *ret=val;
  1210. return NGX_OK;
  1211. }
  1212. --- a/nginx-rtmp/ngx_rtmp_record_module.c
  1213. +++ b/nginx-rtmp/ngx_rtmp_record_module.c
  1214. @@ -454,7 +454,7 @@ ngx_rtmp_record_node_open(ngx_rtmp_sessi
  1215. ngx_err_t err;
  1216. ngx_str_t path;
  1217. ngx_int_t mode, create_mode;
  1218. - u_char buf[8], *p;
  1219. + u_char buf[8];
  1220. off_t file_size;
  1221. uint32_t tag_size, mlen, timestamp;
  1222. @@ -551,11 +551,7 @@ ngx_rtmp_record_node_open(ngx_rtmp_sessi
  1223. goto done;
  1224. }
  1225. - p = (u_char *) &tag_size;
  1226. - p[0] = buf[3];
  1227. - p[1] = buf[2];
  1228. - p[2] = buf[1];
  1229. - p[3] = buf[0];
  1230. + tag_size=ntohl(*(uint32_t*)&buf[0]);
  1231. if (tag_size == 0 || tag_size + 4 > file_size) {
  1232. file_size = 0;
  1233. @@ -569,11 +565,7 @@ ngx_rtmp_record_node_open(ngx_rtmp_sessi
  1234. goto done;
  1235. }
  1236. - p = (u_char *) &mlen;
  1237. - p[0] = buf[3];
  1238. - p[1] = buf[2];
  1239. - p[2] = buf[1];
  1240. - p[3] = 0;
  1241. + mlen=n3toh4(&buf[1]);
  1242. if (tag_size != mlen + 11) {
  1243. ngx_log_error(NGX_LOG_CRIT, s->connection->log, ngx_errno,
  1244. @@ -582,11 +574,7 @@ ngx_rtmp_record_node_open(ngx_rtmp_sessi
  1245. goto done;
  1246. }
  1247. - p = (u_char *) &timestamp;
  1248. - p[3] = buf[7];
  1249. - p[0] = buf[6];
  1250. - p[1] = buf[5];
  1251. - p[2] = buf[4];
  1252. + timestamp=n3toh4(&buf[4])|((uint32_t)buf[7]<<24);
  1253. done:
  1254. rctx->file.offset = file_size;
  1255. @@ -891,7 +879,7 @@ ngx_rtmp_record_write_frame(ngx_rtmp_ses
  1256. ngx_rtmp_header_t *h, ngx_chain_t *in,
  1257. ngx_int_t inc_nframes)
  1258. {
  1259. - u_char hdr[11], *p, *ph;
  1260. + u_char hdr[11], *ph;
  1261. uint32_t timestamp, tag_size;
  1262. ngx_rtmp_record_app_conf_t *rracf;
  1263. @@ -937,16 +925,10 @@ ngx_rtmp_record_write_frame(ngx_rtmp_ses
  1264. *ph++ = (u_char)h->type;
  1265. - p = (u_char*)&h->mlen;
  1266. - *ph++ = p[2];
  1267. - *ph++ = p[1];
  1268. - *ph++ = p[0];
  1269. -
  1270. - p = (u_char*)&timestamp;
  1271. - *ph++ = p[2];
  1272. - *ph++ = p[1];
  1273. - *ph++ = p[0];
  1274. - *ph++ = p[3];
  1275. + ph = h4ton3(ph, h->mlen);
  1276. +
  1277. + ph = h4ton3(ph, timestamp);
  1278. + *ph++ = (u_char)(timestamp>>24);
  1279. *ph++ = 0;
  1280. *ph++ = 0;
  1281. @@ -985,12 +967,8 @@ ngx_rtmp_record_write_frame(ngx_rtmp_ses
  1282. /* write tag size */
  1283. ph = hdr;
  1284. - p = (u_char*)&tag_size;
  1285. -
  1286. - *ph++ = p[3];
  1287. - *ph++ = p[2];
  1288. - *ph++ = p[1];
  1289. - *ph++ = p[0];
  1290. + *(uint32_t*)ph = htonl(tag_size);
  1291. + ph += 4;
  1292. if (ngx_write_file(&rctx->file, hdr, ph - hdr,
  1293. rctx->file.offset)