annotate rtmppkt.c @ 5165:3d4203b9c2d7 libavformat

check entries against field_size, potential malloc overflow in read_stsz, fix #1357
author bcoudurier
date Mon, 07 Sep 2009 22:42:51 +0000
parents cc34279f0fab
children a280af9ae028
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5123
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
1 /*
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
2 * RTMP input format
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
3 * Copyright (c) 2009 Kostya Shishkov
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
4 *
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
5 * This file is part of FFmpeg.
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
6 *
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
11 *
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
15 * Lesser General Public License for more details.
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
16 *
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
20 */
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
21
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
22 #include "libavcodec/bytestream.h"
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
23 #include "libavutil/avstring.h"
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
24 #include "avformat.h"
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
25
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
26 #include "rtmppkt.h"
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
27 #include "flv.h"
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
28
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
29 void ff_amf_write_bool(uint8_t **dst, int val)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
30 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
31 bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
32 bytestream_put_byte(dst, val);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
33 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
34
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
35 void ff_amf_write_number(uint8_t **dst, double val)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
36 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
37 bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
38 bytestream_put_be64(dst, av_dbl2int(val));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
39 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
40
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
41 void ff_amf_write_string(uint8_t **dst, const char *str)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
42 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
43 bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
44 bytestream_put_be16(dst, strlen(str));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
45 bytestream_put_buffer(dst, str, strlen(str));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
46 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
47
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
48 void ff_amf_write_null(uint8_t **dst)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
49 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
50 bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
51 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
52
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
53 void ff_amf_write_object_start(uint8_t **dst)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
54 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
55 bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
56 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
57
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
58 void ff_amf_write_field_name(uint8_t **dst, const char *str)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
59 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
60 bytestream_put_be16(dst, strlen(str));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
61 bytestream_put_buffer(dst, str, strlen(str));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
62 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
63
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
64 void ff_amf_write_object_end(uint8_t **dst)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
65 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
66 /* first two bytes are field name length = 0,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
67 * AMF object should end with it and end marker
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
68 */
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
69 bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
70 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
71
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
72 int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
73 int chunk_size, RTMPPacket *prev_pkt)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
74 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
75 uint8_t hdr, t, buf[16];
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
76 int channel_id, timestamp, data_size, offset = 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
77 uint32_t extra = 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
78 uint8_t type;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
79
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
80 if (url_read(h, &hdr, 1) != 1)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
81 return AVERROR(EIO);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
82 channel_id = hdr & 0x3F;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
83
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
84 data_size = prev_pkt[channel_id].data_size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
85 type = prev_pkt[channel_id].type;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
86 extra = prev_pkt[channel_id].extra;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
87
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
88 hdr >>= 6;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
89 if (hdr == RTMP_PS_ONEBYTE) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
90 //todo
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
91 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
92 } else {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
93 if (url_read_complete(h, buf, 3) != 3)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
94 return AVERROR(EIO);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
95 timestamp = AV_RB24(buf);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
96 if (hdr != RTMP_PS_FOURBYTES) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
97 if (url_read_complete(h, buf, 3) != 3)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
98 return AVERROR(EIO);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
99 data_size = AV_RB24(buf);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
100 if (url_read_complete(h, &type, 1) != 1)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
101 return AVERROR(EIO);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
102 if (hdr == RTMP_PS_TWELVEBYTES) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
103 if (url_read_complete(h, buf, 4) != 4)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
104 return AVERROR(EIO);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
105 extra = AV_RL32(buf);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
106 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
107 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
108 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
109 if (ff_rtmp_packet_create(p, channel_id, type, timestamp, data_size))
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
110 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
111 p->extra = extra;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
112 // save history
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
113 prev_pkt[channel_id].channel_id = channel_id;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
114 prev_pkt[channel_id].type = type;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
115 prev_pkt[channel_id].data_size = data_size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
116 prev_pkt[channel_id].timestamp = timestamp;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
117 prev_pkt[channel_id].extra = extra;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
118 while (data_size > 0) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
119 int toread = FFMIN(data_size, chunk_size);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
120 if (url_read_complete(h, p->data + offset, toread) != toread) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
121 ff_rtmp_packet_destroy(p);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
122 return AVERROR(EIO);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
123 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
124 data_size -= chunk_size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
125 offset += chunk_size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
126 if (data_size > 0) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
127 url_read_complete(h, &t, 1); //marker
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
128 if (t != (0xC0 + channel_id))
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
129 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
130 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
131 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
132 return 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
133 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
134
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
135 int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
136 int chunk_size, RTMPPacket *prev_pkt)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
137 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
138 uint8_t pkt_hdr[16], *p = pkt_hdr;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
139 int mode = RTMP_PS_TWELVEBYTES;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
140 int off = 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
141
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
142 //TODO: header compression
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
143 bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
144 if (mode != RTMP_PS_ONEBYTE) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
145 bytestream_put_be24(&p, pkt->timestamp);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
146 if (mode != RTMP_PS_FOURBYTES) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
147 bytestream_put_be24(&p, pkt->data_size);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
148 bytestream_put_byte(&p, pkt->type);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
149 if (mode == RTMP_PS_TWELVEBYTES)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
150 bytestream_put_le32(&p, pkt->extra);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
151 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
152 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
153 url_write(h, pkt_hdr, p-pkt_hdr);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
154 while (off < pkt->data_size) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
155 int towrite = FFMIN(chunk_size, pkt->data_size - off);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
156 url_write(h, pkt->data + off, towrite);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
157 off += towrite;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
158 if (off < pkt->data_size) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
159 uint8_t marker = 0xC0 | pkt->channel_id;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
160 url_write(h, &marker, 1);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
161 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
162 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
163 return 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
164 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
165
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
166 int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
167 int timestamp, int size)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
168 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
169 pkt->data = av_malloc(size);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
170 if (!pkt->data)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
171 return AVERROR(ENOMEM);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
172 pkt->data_size = size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
173 pkt->channel_id = channel_id;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
174 pkt->type = type;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
175 pkt->timestamp = timestamp;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
176 pkt->extra = 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
177
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
178 return 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
179 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
180
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
181 void ff_rtmp_packet_destroy(RTMPPacket *pkt)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
182 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
183 if (!pkt)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
184 return;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
185 av_freep(&pkt->data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
186 pkt->data_size = 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
187 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
188
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
189 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
190 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
191 const uint8_t *base = data;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
192
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
193 if (data >= data_end)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
194 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
195 switch (*data++) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
196 case AMF_DATA_TYPE_NUMBER: return 9;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
197 case AMF_DATA_TYPE_BOOL: return 2;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
198 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
199 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
200 case AMF_DATA_TYPE_NULL: return 1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
201 case AMF_DATA_TYPE_ARRAY:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
202 data += 4;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
203 case AMF_DATA_TYPE_OBJECT:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
204 for (;;) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
205 int size = bytestream_get_be16(&data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
206 int t;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
207 if (!size) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
208 data++;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
209 break;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
210 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
211 if (data + size >= data_end || data + size < data)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
212 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
213 data += size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
214 t = ff_amf_tag_size(data, data_end);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
215 if (t < 0 || data + t >= data_end)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
216 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
217 data += t;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
218 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
219 return data - base;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
220 case AMF_DATA_TYPE_OBJECT_END: return 1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
221 default: return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
222 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
223 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
224
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
225 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
226 const uint8_t *name, uint8_t *dst, int dst_size)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
227 {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
228 int namelen = strlen(name);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
229 int len;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
230
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
231 if (data_end - data < 3)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
232 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
233 if (*data++ != AMF_DATA_TYPE_OBJECT)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
234 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
235 for (;;) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
236 int size = bytestream_get_be16(&data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
237 if (!size)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
238 break;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
239 if (data + size >= data_end || data + size < data)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
240 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
241 data += size;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
242 if (size == namelen && !memcmp(data-size, name, namelen)) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
243 switch (*data++) {
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
244 case AMF_DATA_TYPE_NUMBER:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
245 snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data)));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
246 break;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
247 case AMF_DATA_TYPE_BOOL:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
248 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
249 break;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
250 case AMF_DATA_TYPE_STRING:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
251 len = bytestream_get_be16(&data);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
252 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
253 break;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
254 default:
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
255 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
256 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
257 return 0;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
258 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
259 len = ff_amf_tag_size(data, data_end);
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
260 if (len < 0 || data + len >= data_end || data + len < data)
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
261 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
262 data += len;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
263 }
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
264 return -1;
cc34279f0fab RTMP protocol support (as a client)
kostya
parents:
diff changeset
265 }