forked from arut/nginx-rtmp-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_rtmp_amf0.c
459 lines (360 loc) · 11.1 KB
/
ngx_rtmp_amf0.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* Copyright (c) 2012 Roman Arutyunyan
*/
#include "ngx_rtmp_amf0.h"
#include "ngx_rtmp.h"
#include <string.h>
static inline void*
ngx_rtmp_amf0_reverse_copy(void *dst, void* src, size_t len)
{
size_t k;
if (dst == NULL || src == NULL) {
return NULL;
}
for(k = 0; k < len; ++k) {
((u_char*)dst)[k] = ((u_char*)src)[len - 1 - k];
}
return dst;
}
#define NGX_RTMP_AMF0_DEBUG_SIZE 16
#ifdef NGX_DEBUG
static void
ngx_rtmp_amf0_debug(const char* op, ngx_log_t *log, u_char *p, size_t n)
{
u_char hstr[3 * NGX_RTMP_AMF0_DEBUG_SIZE + 1];
u_char str[NGX_RTMP_AMF0_DEBUG_SIZE + 1];
u_char *hp, *sp;
static u_char hex[] = "0123456789ABCDEF";
size_t i;
hp = hstr;
sp = str;
for(i = 0; i < n && i < NGX_RTMP_AMF0_DEBUG_SIZE; ++i) {
*hp++ = ' ';
if (p) {
*hp++ = hex[(*p & 0xf0) >> 4];
*hp++ = hex[*p & 0x0f];
*sp++ = (*p >= 0x20 && *p <= 0x7e) ?
*p : (u_char)'?';
++p;
} else {
*hp++ = 'X';
*hp++ = 'X';
*sp++ = '?';
}
}
*hp = *sp = '\0';
ngx_log_debug4(NGX_LOG_DEBUG_RTMP, log, 0,
"AMF0 %s (%d)%s '%s'", op, n, hstr, str);
}
#endif
static ngx_int_t
ngx_rtmp_amf0_get(ngx_rtmp_amf0_ctx_t *ctx, void *p, size_t n)
{
ngx_buf_t *b;
size_t size;
ngx_chain_t *l;
#ifdef NGX_DEBUG
void *op = p;
size_t on = n;
#endif
if (!n)
return NGX_OK;
for(l = ctx->link; l; l = l->next) {
b = l->buf;
if (b->last >= n + b->pos) {
if (p) {
p = ngx_cpymem(p, b->pos, n);
}
b->pos += n;
ctx->link = l;
#ifdef NGX_DEBUG
ngx_rtmp_amf0_debug("read", ctx->log, (u_char*)op, on);
#endif
return NGX_OK;
}
size = b->last - b->pos;
if (p) {
p = ngx_cpymem(p, b->pos, size);
}
n -= size;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, ctx->log, 0,
"AMF0 read eof (%d)", n);
return NGX_ERROR;
}
static ngx_int_t
ngx_rtmp_amf0_put(ngx_rtmp_amf0_ctx_t *ctx, void *p, size_t n)
{
ngx_buf_t *b;
size_t size;
ngx_chain_t *l, *ln;
#ifdef NGX_DEBUG
ngx_rtmp_amf0_debug("write", ctx->log, (u_char*)p, n);
#endif
l = ctx->link;
while(n) {
b = l ? l->buf : NULL;
if (b == NULL || b->last == b->end) {
ln = ctx->alloc(ctx->cscf);
if (ln == NULL) {
return NGX_ERROR;
}
if (l == NULL) {
l = ln;
ctx->first = l;
} else {
l->next = ln;
l = ln;
}
ctx->link = l;
b = l->buf;
}
size = b->end - b->last;
if (size >= n) {
b->last = ngx_cpymem(b->last, p, n);
return NGX_OK;
}
b->last = ngx_cpymem(b->last, p, size);
p = (u_char*)p + size;
n -= size;
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_amf0_read_object(ngx_rtmp_amf0_ctx_t *ctx, ngx_rtmp_amf0_elt_t *elts,
size_t nelts)
{
uint8_t type;
uint16_t len;
size_t n, namelen, maxlen;
ngx_int_t rc;
u_char buf[2];
maxlen = 0;
for(n = 0; n < nelts; ++n) {
namelen = strlen(elts[n].name);
if (namelen > maxlen)
maxlen = namelen;
}
for(;;) {
char name[maxlen + 1];
/* read key */
if (ngx_rtmp_amf0_get(ctx, buf, 2) != NGX_OK)
return NGX_ERROR;
ngx_rtmp_amf0_reverse_copy(&len, buf, 2);
if (!len)
break;
if (len <= maxlen) {
rc = ngx_rtmp_amf0_get(ctx, name, len);
name[len] = 0;
} else {
rc = ngx_rtmp_amf0_get(ctx, name, maxlen);
if (rc != NGX_OK)
return NGX_ERROR;
name[maxlen] = 0;
rc = ngx_rtmp_amf0_get(ctx, 0, len - maxlen);
}
if (rc != NGX_OK)
return NGX_ERROR;
/* TODO: if we require array to be sorted on name
* then we could be able to use binary search */
for(n = 0; n < nelts && strcmp(name, elts[n].name); ++n);
if (ngx_rtmp_amf0_read(ctx, n < nelts ? &elts[n] : NULL, 1) != NGX_OK)
return NGX_ERROR;
}
if (ngx_rtmp_amf0_get(ctx, &type, 1) != NGX_OK
|| type != NGX_RTMP_AMF0_END)
{
return NGX_ERROR;
}
return NGX_OK;
}
#define NGX_RTMP_AMF0_TILL_END_FLAG ((size_t)1 << (sizeof(size_t) * 8 - 1))
ngx_int_t
ngx_rtmp_amf0_read(ngx_rtmp_amf0_ctx_t *ctx, ngx_rtmp_amf0_elt_t *elts,
size_t nelts)
{
void *data;
uint8_t type;
size_t n;
uint16_t len;
ngx_int_t rc;
int till_end;
u_char buf[8];
if (nelts & NGX_RTMP_AMF0_TILL_END_FLAG) {
till_end = 1;
nelts = nelts & ~NGX_RTMP_AMF0_TILL_END_FLAG;
} else {
till_end = 0;
}
for(n = 0; till_end || n < nelts; ++n) {
if (ngx_rtmp_amf0_get(ctx, &type, sizeof(type)) != NGX_OK)
return NGX_ERROR;
data = (n >= nelts || elts == NULL || elts->type != type)
? NULL
: elts->data;
switch(type) {
case NGX_RTMP_AMF0_NUMBER:
if (ngx_rtmp_amf0_get(ctx, buf, 8) != NGX_OK) {
return NGX_ERROR;
}
ngx_rtmp_amf0_reverse_copy(data, buf, 8);
break;
case NGX_RTMP_AMF0_BOOLEAN:
if (ngx_rtmp_amf0_get(ctx, data, 1) != NGX_OK) {
return NGX_ERROR;
}
break;
case NGX_RTMP_AMF0_STRING:
if (ngx_rtmp_amf0_get(ctx, buf, 2) != NGX_OK) {
return NGX_ERROR;
}
ngx_rtmp_amf0_reverse_copy(&len, buf, 2);
if (data == NULL) {
rc = ngx_rtmp_amf0_get(ctx, data, len);
} else if (elts->len <= len) {
rc = ngx_rtmp_amf0_get(ctx, data, elts->len - 1);
if (rc != NGX_OK)
return NGX_ERROR;
((char*)data)[elts->len - 1] = 0;
rc = ngx_rtmp_amf0_get(ctx, NULL, len - elts->len + 1);
} else {
rc = ngx_rtmp_amf0_get(ctx, data, len);
((char*)data)[len] = 0;
}
if (rc != NGX_OK) {
return NGX_ERROR;
}
break;
case NGX_RTMP_AMF0_NULL:
break;
case NGX_RTMP_AMF0_OBJECT:
if (ngx_rtmp_amf0_read_object(ctx, data,
elts ? elts->len / sizeof(ngx_rtmp_amf0_elt_t) : 0
) != NGX_OK)
{
return NGX_ERROR;
}
break;
case NGX_RTMP_AMF0_ARRAY:
if (ngx_rtmp_amf0_read(ctx, data,
elts ? (elts->len / sizeof(ngx_rtmp_amf0_elt_t))
| NGX_RTMP_AMF0_TILL_END_FLAG : 0
) != NGX_OK)
{
return NGX_ERROR;
}
break;
case NGX_RTMP_AMF0_END:
return NGX_OK;
default:
return NGX_ERROR;
}
if (elts) {
++elts;
}
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_amf0_write_object(ngx_rtmp_amf0_ctx_t *ctx,
ngx_rtmp_amf0_elt_t *elts, size_t nelts)
{
uint16_t len, len_sb;
size_t n;
char *name;
u_char buf[2];
for(n = 0; n < nelts; ++n) {
name = elts[n].name;
len_sb = len = strlen(name);
if (ngx_rtmp_amf0_put(ctx,
ngx_rtmp_amf0_reverse_copy(buf,
&len, 2), 2) != NGX_OK)
{
return NGX_ERROR;
}
if (ngx_rtmp_amf0_put(ctx, name, len) != NGX_OK) {
return NGX_ERROR;
}
if (ngx_rtmp_amf0_write(ctx, &elts[n], 1) != NGX_OK) {
return NGX_ERROR;
}
}
len = 0;
if (ngx_rtmp_amf0_put(ctx, "\00\00", 2) != NGX_OK) {
return NGX_ERROR;
}
return NGX_OK;
}
ngx_int_t
ngx_rtmp_amf0_write(ngx_rtmp_amf0_ctx_t *ctx,
ngx_rtmp_amf0_elt_t *elts, size_t nelts)
{
size_t n;
uint8_t type;
void *data;
uint16_t len;
u_char buf[8];
for(n = 0; n < nelts; ++n) {
type = elts[n].type;
data = elts[n].data;
len = elts[n].len;
if (ngx_rtmp_amf0_put(ctx, &type, sizeof(type)) != NGX_OK)
return NGX_ERROR;
switch(type) {
case NGX_RTMP_AMF0_NUMBER:
if (ngx_rtmp_amf0_put(ctx,
ngx_rtmp_amf0_reverse_copy(buf,
data, 8), 8) != NGX_OK)
{
return NGX_ERROR;
}
break;
case NGX_RTMP_AMF0_BOOLEAN:
if (ngx_rtmp_amf0_put(ctx, data, 1) != NGX_OK) {
return NGX_ERROR;
}
break;
case NGX_RTMP_AMF0_STRING:
if (len == 0 && data) {
len = ngx_strlen((u_char*)data);
}
if (ngx_rtmp_amf0_put(ctx,
ngx_rtmp_amf0_reverse_copy(buf,
&len, 2), 2) != NGX_OK)
{
return NGX_ERROR;
}
if (ngx_rtmp_amf0_put(ctx, data, len) != NGX_OK) {
return NGX_ERROR;
}
break;
case NGX_RTMP_AMF0_NULL:
break;
case NGX_RTMP_AMF0_OBJECT:
type = NGX_RTMP_AMF0_END;
if (ngx_rtmp_amf0_write_object(ctx, data,
elts[n].len / sizeof(ngx_rtmp_amf0_elt_t)) != NGX_OK
|| ngx_rtmp_amf0_put(ctx, &type,
sizeof(type)) != NGX_OK)
{
return NGX_ERROR;
}
break;
case NGX_RTMP_AMF0_ARRAY:
type = NGX_RTMP_AMF0_END;
if (ngx_rtmp_amf0_write(ctx, data,
elts[n].len / sizeof(ngx_rtmp_amf0_elt_t)) != NGX_OK
|| ngx_rtmp_amf0_put(ctx, &type,
sizeof(type)) != NGX_OK)
{
return NGX_ERROR;
}
break;
case NGX_RTMP_AMF0_END:
return NGX_OK;
default:
return NGX_ERROR;
}
}
return NGX_OK;
}