rtmp: Support reading interleaved chunks.
[libav.git] / libavformat / rtmppkt.c
CommitLineData
9fd6b843
KS
1/*
2 * RTMP input format
de421b20 3 * Copyright (c) 2009 Konstantin Shishkov
9fd6b843 4 *
2912e87a 5 * This file is part of Libav.
9fd6b843 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
9fd6b843
KS
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
9fd6b843
KS
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
9fd6b843
KS
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "libavcodec/bytestream.h"
23#include "libavutil/avstring.h"
3383a53e 24#include "libavutil/intfloat.h"
9fd6b843
KS
25#include "avformat.h"
26
27#include "rtmppkt.h"
28#include "flv.h"
bc371aca 29#include "url.h"
9fd6b843
KS
30
31void ff_amf_write_bool(uint8_t **dst, int val)
32{
33 bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
34 bytestream_put_byte(dst, val);
35}
36
37void ff_amf_write_number(uint8_t **dst, double val)
38{
39 bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
3383a53e 40 bytestream_put_be64(dst, av_double2int(val));
9fd6b843
KS
41}
42
43void ff_amf_write_string(uint8_t **dst, const char *str)
44{
45 bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
46 bytestream_put_be16(dst, strlen(str));
47 bytestream_put_buffer(dst, str, strlen(str));
48}
49
33f28a3b
MS
50void ff_amf_write_string2(uint8_t **dst, const char *str1, const char *str2)
51{
52 int len1 = 0, len2 = 0;
53 if (str1)
54 len1 = strlen(str1);
55 if (str2)
56 len2 = strlen(str2);
57 bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
58 bytestream_put_be16(dst, len1 + len2);
59 bytestream_put_buffer(dst, str1, len1);
60 bytestream_put_buffer(dst, str2, len2);
61}
62
9fd6b843
KS
63void ff_amf_write_null(uint8_t **dst)
64{
65 bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
66}
67
68void ff_amf_write_object_start(uint8_t **dst)
69{
70 bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
71}
72
73void ff_amf_write_field_name(uint8_t **dst, const char *str)
74{
75 bytestream_put_be16(dst, strlen(str));
76 bytestream_put_buffer(dst, str, strlen(str));
77}
78
79void ff_amf_write_object_end(uint8_t **dst)
80{
81 /* first two bytes are field name length = 0,
82 * AMF object should end with it and end marker
83 */
84 bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
85}
86
50468f93
JO
87int ff_amf_read_bool(GetByteContext *bc, int *val)
88{
89 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_BOOL)
90 return AVERROR_INVALIDDATA;
91 *val = bytestream2_get_byte(bc);
92 return 0;
93}
94
95int ff_amf_read_number(GetByteContext *bc, double *val)
96{
97 uint64_t read;
98 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_NUMBER)
99 return AVERROR_INVALIDDATA;
100 read = bytestream2_get_be64(bc);
101 *val = av_int2double(read);
102 return 0;
103}
104
105int ff_amf_read_string(GetByteContext *bc, uint8_t *str,
106 int strsize, int *length)
107{
108 int stringlen = 0;
109 int readsize;
110 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_STRING)
111 return AVERROR_INVALIDDATA;
112 stringlen = bytestream2_get_be16(bc);
113 if (stringlen + 1 > strsize)
114 return AVERROR(EINVAL);
115 readsize = bytestream2_get_buffer(bc, str, stringlen);
116 if (readsize != stringlen) {
117 av_log(NULL, AV_LOG_WARNING,
118 "Unable to read as many bytes as AMF string signaled\n");
119 }
120 str[readsize] = '\0';
121 *length = FFMIN(stringlen, readsize);
122 return 0;
123}
124
125int ff_amf_read_null(GetByteContext *bc)
126{
127 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_NULL)
128 return AVERROR_INVALIDDATA;
129 return 0;
130}
131
9fd6b843
KS
132int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
133 int chunk_size, RTMPPacket *prev_pkt)
134{
7dc747f5
SP
135 uint8_t hdr;
136
137 if (ffurl_read(h, &hdr, 1) != 1)
138 return AVERROR(EIO);
139
140 return ff_rtmp_packet_read_internal(h, p, chunk_size, prev_pkt, hdr);
141}
142
8583b142
JA
143static int rtmp_packet_read_one_chunk(URLContext *h, RTMPPacket *p,
144 int chunk_size, RTMPPacket *prev_pkt,
145 uint8_t hdr)
7dc747f5
SP
146{
147
8583b142
JA
148 uint8_t buf[16];
149 int channel_id, timestamp, size;
9fd6b843 150 uint32_t extra = 0;
0f87b771 151 enum RTMPPacketType type;
ba5393a6 152 int written = 0;
8583b142 153 int ret, toread;
9fd6b843 154
ba5393a6 155 written++;
9fd6b843
KS
156 channel_id = hdr & 0x3F;
157
5d660598
KS
158 if (channel_id < 2) { //special case for channel number >= 64
159 buf[1] = 0;
dce37564 160 if (ffurl_read_complete(h, buf, channel_id + 1) != channel_id + 1)
5d660598 161 return AVERROR(EIO);
ba5393a6 162 written += channel_id + 1;
5d660598
KS
163 channel_id = AV_RL16(buf) + 64;
164 }
ba5393a6
LB
165 size = prev_pkt[channel_id].size;
166 type = prev_pkt[channel_id].type;
167 extra = prev_pkt[channel_id].extra;
9fd6b843
KS
168
169 hdr >>= 6;
170 if (hdr == RTMP_PS_ONEBYTE) {
a352b605 171 timestamp = prev_pkt[channel_id].ts_delta;
9fd6b843 172 } else {
dce37564 173 if (ffurl_read_complete(h, buf, 3) != 3)
9fd6b843 174 return AVERROR(EIO);
ba5393a6 175 written += 3;
9fd6b843
KS
176 timestamp = AV_RB24(buf);
177 if (hdr != RTMP_PS_FOURBYTES) {
dce37564 178 if (ffurl_read_complete(h, buf, 3) != 3)
9fd6b843 179 return AVERROR(EIO);
ba5393a6
LB
180 written += 3;
181 size = AV_RB24(buf);
dce37564 182 if (ffurl_read_complete(h, buf, 1) != 1)
9fd6b843 183 return AVERROR(EIO);
ba5393a6 184 written++;
1ab21f19 185 type = buf[0];
9fd6b843 186 if (hdr == RTMP_PS_TWELVEBYTES) {
dce37564 187 if (ffurl_read_complete(h, buf, 4) != 4)
9fd6b843 188 return AVERROR(EIO);
ba5393a6 189 written += 4;
9fd6b843
KS
190 extra = AV_RL32(buf);
191 }
192 }
59b6482a 193 if (timestamp == 0xFFFFFF) {
dce37564 194 if (ffurl_read_complete(h, buf, 4) != 4)
59b6482a
S
195 return AVERROR(EIO);
196 timestamp = AV_RB32(buf);
197 }
9fd6b843 198 }
a352b605
S
199 if (hdr != RTMP_PS_TWELVEBYTES)
200 timestamp += prev_pkt[channel_id].timestamp;
201
8583b142
JA
202 if (!prev_pkt[channel_id].read) {
203 if ((ret = ff_rtmp_packet_create(p, channel_id, type, timestamp,
204 size)) < 0)
205 return ret;
206 p->read = written;
207 p->offset = 0;
208 prev_pkt[channel_id].ts_delta = timestamp -
209 prev_pkt[channel_id].timestamp;
210 prev_pkt[channel_id].timestamp = timestamp;
211 } else {
212 // previous packet in this channel hasn't completed reading
213 RTMPPacket *prev = &prev_pkt[channel_id];
214 p->data = prev->data;
215 p->size = prev->size;
216 p->channel_id = prev->channel_id;
217 p->type = prev->type;
218 p->ts_delta = prev->ts_delta;
219 p->extra = prev->extra;
220 p->offset = prev->offset;
221 p->read = prev->read + written;
222 p->timestamp = prev->timestamp;
223 prev->data = NULL;
224 }
9fd6b843
KS
225 p->extra = extra;
226 // save history
227 prev_pkt[channel_id].channel_id = channel_id;
228 prev_pkt[channel_id].type = type;
ba5393a6 229 prev_pkt[channel_id].size = size;
9fd6b843 230 prev_pkt[channel_id].extra = extra;
8583b142
JA
231 size = size - p->offset;
232
233 toread = FFMIN(size, chunk_size);
234 if (ffurl_read_complete(h, p->data + p->offset, toread) != toread) {
235 ff_rtmp_packet_destroy(p);
236 return AVERROR(EIO);
237 }
238 size -= toread;
239 p->read += toread;
240 p->offset += toread;
241
242 if (size > 0) {
243 RTMPPacket *prev = &prev_pkt[channel_id];
244 prev->data = p->data;
245 prev->read = p->read;
246 prev->offset = p->offset;
247 return AVERROR(EAGAIN);
248 }
249
250 prev_pkt[channel_id].read = 0; // read complete; reset if needed
251 return p->read;
252}
253
254int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
255 RTMPPacket *prev_pkt, uint8_t hdr)
256{
257 while (1) {
258 int ret = rtmp_packet_read_one_chunk(h, p, chunk_size, prev_pkt, hdr);
259 if (ret > 0 || ret != AVERROR(EAGAIN))
260 return ret;
261
262 if (ffurl_read(h, &hdr, 1) != 1)
9fd6b843 263 return AVERROR(EIO);
9fd6b843 264 }
9fd6b843
KS
265}
266
267int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
268 int chunk_size, RTMPPacket *prev_pkt)
269{
270 uint8_t pkt_hdr[16], *p = pkt_hdr;
271 int mode = RTMP_PS_TWELVEBYTES;
272 int off = 0;
ba5393a6 273 int written = 0;
bba287fd 274 int ret;
09b67cc4 275
a352b605 276 pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
9fd6b843 277
84a29dc1
S
278 //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
279 if (prev_pkt[pkt->channel_id].channel_id &&
280 pkt->extra == prev_pkt[pkt->channel_id].extra) {
281 if (pkt->type == prev_pkt[pkt->channel_id].type &&
ba5393a6 282 pkt->size == prev_pkt[pkt->channel_id].size) {
84a29dc1
S
283 mode = RTMP_PS_FOURBYTES;
284 if (pkt->ts_delta == prev_pkt[pkt->channel_id].ts_delta)
285 mode = RTMP_PS_ONEBYTE;
286 } else {
287 mode = RTMP_PS_EIGHTBYTES;
288 }
289 }
290
b1eb53ab
KS
291 if (pkt->channel_id < 64) {
292 bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
293 } else if (pkt->channel_id < 64 + 256) {
294 bytestream_put_byte(&p, 0 | (mode << 6));
295 bytestream_put_byte(&p, pkt->channel_id - 64);
296 } else {
297 bytestream_put_byte(&p, 1 | (mode << 6));
298 bytestream_put_le16(&p, pkt->channel_id - 64);
299 }
9fd6b843 300 if (mode != RTMP_PS_ONEBYTE) {
817d967d
KS
301 uint32_t timestamp = pkt->timestamp;
302 if (mode != RTMP_PS_TWELVEBYTES)
a352b605 303 timestamp = pkt->ts_delta;
817d967d 304 bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
9fd6b843 305 if (mode != RTMP_PS_FOURBYTES) {
ba5393a6 306 bytestream_put_be24(&p, pkt->size);
9fd6b843
KS
307 bytestream_put_byte(&p, pkt->type);
308 if (mode == RTMP_PS_TWELVEBYTES)
309 bytestream_put_le32(&p, pkt->extra);
310 }
817d967d
KS
311 if (timestamp >= 0xFFFFFF)
312 bytestream_put_be32(&p, timestamp);
9fd6b843 313 }
84a29dc1
S
314 // save history
315 prev_pkt[pkt->channel_id].channel_id = pkt->channel_id;
316 prev_pkt[pkt->channel_id].type = pkt->type;
ba5393a6 317 prev_pkt[pkt->channel_id].size = pkt->size;
84a29dc1
S
318 prev_pkt[pkt->channel_id].timestamp = pkt->timestamp;
319 if (mode != RTMP_PS_TWELVEBYTES) {
320 prev_pkt[pkt->channel_id].ts_delta = pkt->ts_delta;
321 } else {
322 prev_pkt[pkt->channel_id].ts_delta = pkt->timestamp;
323 }
324 prev_pkt[pkt->channel_id].extra = pkt->extra;
325
bba287fd
SP
326 if ((ret = ffurl_write(h, pkt_hdr, p - pkt_hdr)) < 0)
327 return ret;
ba5393a6
LB
328 written = p - pkt_hdr + pkt->size;
329 while (off < pkt->size) {
330 int towrite = FFMIN(chunk_size, pkt->size - off);
bba287fd
SP
331 if ((ret = ffurl_write(h, pkt->data + off, towrite)) < 0)
332 return ret;
9fd6b843 333 off += towrite;
ba5393a6 334 if (off < pkt->size) {
9fd6b843 335 uint8_t marker = 0xC0 | pkt->channel_id;
bba287fd
SP
336 if ((ret = ffurl_write(h, &marker, 1)) < 0)
337 return ret;
ba5393a6 338 written++;
9fd6b843
KS
339 }
340 }
ba5393a6 341 return written;
9fd6b843
KS
342}
343
344int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
345 int timestamp, int size)
346{
271c869c 347 if (size) {
48b1fb13
MS
348 pkt->data = av_malloc(size);
349 if (!pkt->data)
350 return AVERROR(ENOMEM);
271c869c 351 }
ba5393a6 352 pkt->size = size;
9fd6b843
KS
353 pkt->channel_id = channel_id;
354 pkt->type = type;
355 pkt->timestamp = timestamp;
356 pkt->extra = 0;
a352b605 357 pkt->ts_delta = 0;
9fd6b843
KS
358
359 return 0;
360}
361
362void ff_rtmp_packet_destroy(RTMPPacket *pkt)
363{
364 if (!pkt)
365 return;
366 av_freep(&pkt->data);
ba5393a6 367 pkt->size = 0;
9fd6b843
KS
368}
369
370int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
371{
372 const uint8_t *base = data;
373
374 if (data >= data_end)
375 return -1;
376 switch (*data++) {
377 case AMF_DATA_TYPE_NUMBER: return 9;
378 case AMF_DATA_TYPE_BOOL: return 2;
379 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
380 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
381 case AMF_DATA_TYPE_NULL: return 1;
382 case AMF_DATA_TYPE_ARRAY:
383 data += 4;
384 case AMF_DATA_TYPE_OBJECT:
385 for (;;) {
386 int size = bytestream_get_be16(&data);
387 int t;
388 if (!size) {
389 data++;
390 break;
391 }
3cff5336 392 if (size < 0 || size >= data_end - data)
9fd6b843
KS
393 return -1;
394 data += size;
395 t = ff_amf_tag_size(data, data_end);
3cff5336 396 if (t < 0 || t >= data_end - data)
9fd6b843
KS
397 return -1;
398 data += t;
399 }
400 return data - base;
401 case AMF_DATA_TYPE_OBJECT_END: return 1;
402 default: return -1;
403 }
404}
405
406int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
407 const uint8_t *name, uint8_t *dst, int dst_size)
408{
409 int namelen = strlen(name);
410 int len;
411
87ca1b8f
KS
412 while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
413 len = ff_amf_tag_size(data, data_end);
414 if (len < 0)
415 len = data_end - data;
416 data += len;
417 }
9fd6b843
KS
418 if (data_end - data < 3)
419 return -1;
87ca1b8f 420 data++;
9fd6b843
KS
421 for (;;) {
422 int size = bytestream_get_be16(&data);
423 if (!size)
424 break;
3cff5336 425 if (size < 0 || size >= data_end - data)
9fd6b843
KS
426 return -1;
427 data += size;
428 if (size == namelen && !memcmp(data-size, name, namelen)) {
429 switch (*data++) {
430 case AMF_DATA_TYPE_NUMBER:
3383a53e 431 snprintf(dst, dst_size, "%g", av_int2double(AV_RB64(data)));
9fd6b843
KS
432 break;
433 case AMF_DATA_TYPE_BOOL:
434 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
435 break;
436 case AMF_DATA_TYPE_STRING:
437 len = bytestream_get_be16(&data);
438 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
439 break;
440 default:
441 return -1;
442 }
443 return 0;
444 }
445 len = ff_amf_tag_size(data, data_end);
3cff5336 446 if (len < 0 || len >= data_end - data)
9fd6b843
KS
447 return -1;
448 data += len;
449 }
450 return -1;
451}
cfac91fe
KS
452
453static const char* rtmp_packet_type(int type)
454{
455 switch (type) {
456 case RTMP_PT_CHUNK_SIZE: return "chunk size";
457 case RTMP_PT_BYTES_READ: return "bytes read";
458 case RTMP_PT_PING: return "ping";
459 case RTMP_PT_SERVER_BW: return "server bandwidth";
460 case RTMP_PT_CLIENT_BW: return "client bandwidth";
461 case RTMP_PT_AUDIO: return "audio packet";
462 case RTMP_PT_VIDEO: return "video packet";
463 case RTMP_PT_FLEX_STREAM: return "Flex shared stream";
464 case RTMP_PT_FLEX_OBJECT: return "Flex shared object";
465 case RTMP_PT_FLEX_MESSAGE: return "Flex shared message";
466 case RTMP_PT_NOTIFY: return "notification";
467 case RTMP_PT_SHARED_OBJ: return "shared object";
468 case RTMP_PT_INVOKE: return "invoke";
469 case RTMP_PT_METADATA: return "metadata";
470 default: return "unknown";
471 }
472}
473
e926b5ce
DB
474static void amf_tag_contents(void *ctx, const uint8_t *data,
475 const uint8_t *data_end)
cfac91fe 476{
ecb918e5 477 unsigned int size;
cfac91fe
KS
478 char buf[1024];
479
480 if (data >= data_end)
481 return;
482 switch (*data++) {
483 case AMF_DATA_TYPE_NUMBER:
3383a53e 484 av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2double(AV_RB64(data)));
cfac91fe
KS
485 return;
486 case AMF_DATA_TYPE_BOOL:
487 av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
488 return;
489 case AMF_DATA_TYPE_STRING:
490 case AMF_DATA_TYPE_LONG_STRING:
491 if (data[-1] == AMF_DATA_TYPE_STRING) {
492 size = bytestream_get_be16(&data);
493 } else {
f3cb1cd0 494 size = bytestream_get_be32(&data);
cfac91fe 495 }
ecb918e5 496 size = FFMIN(size, sizeof(buf) - 1);
cfac91fe
KS
497 memcpy(buf, data, size);
498 buf[size] = 0;
499 av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
500 return;
501 case AMF_DATA_TYPE_NULL:
502 av_log(ctx, AV_LOG_DEBUG, " NULL\n");
503 return;
504 case AMF_DATA_TYPE_ARRAY:
505 data += 4;
506 case AMF_DATA_TYPE_OBJECT:
507 av_log(ctx, AV_LOG_DEBUG, " {\n");
508 for (;;) {
cfac91fe 509 int t;
ecb918e5
XW
510 size = bytestream_get_be16(&data);
511 av_strlcpy(buf, data, FFMIN(sizeof(buf), size + 1));
cfac91fe
KS
512 if (!size) {
513 av_log(ctx, AV_LOG_DEBUG, " }\n");
514 data++;
515 break;
516 }
ecb918e5 517 if (size >= data_end - data)
cfac91fe
KS
518 return;
519 data += size;
520 av_log(ctx, AV_LOG_DEBUG, " %s: ", buf);
e926b5ce 521 amf_tag_contents(ctx, data, data_end);
cfac91fe 522 t = ff_amf_tag_size(data, data_end);
3cff5336 523 if (t < 0 || t >= data_end - data)
cfac91fe
KS
524 return;
525 data += t;
526 }
527 return;
528 case AMF_DATA_TYPE_OBJECT_END:
529 av_log(ctx, AV_LOG_DEBUG, " }\n");
530 return;
531 default:
532 return;
533 }
534}
535
536void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
537{
538 av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
ba5393a6 539 rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->size);
cfac91fe 540 if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
ba5393a6 541 uint8_t *src = p->data, *src_end = p->data + p->size;
cfac91fe
KS
542 while (src < src_end) {
543 int sz;
e926b5ce 544 amf_tag_contents(ctx, src, src_end);
cfac91fe
KS
545 sz = ff_amf_tag_size(src, src_end);
546 if (sz < 0)
547 break;
548 src += sz;
549 }
550 } else if (p->type == RTMP_PT_SERVER_BW){
551 av_log(ctx, AV_LOG_DEBUG, "Server BW = %d\n", AV_RB32(p->data));
552 } else if (p->type == RTMP_PT_CLIENT_BW){
553 av_log(ctx, AV_LOG_DEBUG, "Client BW = %d\n", AV_RB32(p->data));
554 } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
555 int i;
ba5393a6 556 for (i = 0; i < p->size; i++)
cfac91fe
KS
557 av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
558 av_log(ctx, AV_LOG_DEBUG, "\n");
559 }
560}
5718e348
LB
561
562int ff_amf_match_string(const uint8_t *data, int size, const char *str)
563{
564 int len = strlen(str);
565 int amf_len, type;
566
567 if (size < 1)
568 return 0;
569
570 type = *data++;
571
572 if (type != AMF_DATA_TYPE_LONG_STRING &&
573 type != AMF_DATA_TYPE_STRING)
574 return 0;
575
576 if (type == AMF_DATA_TYPE_LONG_STRING) {
577 if ((size -= 4 + 1) < 0)
578 return 0;
579 amf_len = bytestream_get_be32(&data);
580 } else {
581 if ((size -= 2 + 1) < 0)
582 return 0;
583 amf_len = bytestream_get_be16(&data);
584 }
585
586 if (amf_len > size)
587 return 0;
588
589 if (amf_len != len)
590 return 0;
591
592 return !memcmp(data, str, len);
593}