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