-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathamf.cc
426 lines (392 loc) · 8.71 KB
/
amf.cc
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
#include "amf.h"
#include "utils.h"
#include <stdexcept>
#include <string.h>
#include <arpa/inet.h>
namespace {
uint8_t peek(const Decoder *dec)
{
if (dec->pos >= dec->buf.size()) {
throw std::runtime_error("Not enough data");
}
return uint8_t(dec->buf[dec->pos]);
}
uint8_t get_byte(Decoder *dec)
{
if (dec->version == 0 && peek(dec) == AMF0_SWITCH_AMF3) {
debug("entering AMF3 mode\n");
dec->pos++;
dec->version = 3;
}
if (dec->pos >= dec->buf.size()) {
throw std::runtime_error("Not enough data");
}
return uint8_t(dec->buf[dec->pos++]);
}
}
AMFValue::AMFValue(AMFType type) :
m_type(type)
{
switch (m_type) {
case AMF_OBJECT:
case AMF_ECMA_ARRAY:
m_value.object = new amf_object_t;
break;
case AMF_NUMBER:
case AMF_INTEGER:
case AMF_NULL:
case AMF_UNDEFINED:
break;
default:
assert(0);
}
}
AMFValue::AMFValue(const std::string &s) :
m_type(AMF_STRING)
{
m_value.string = new std::string(s);
}
AMFValue::AMFValue(double n) :
m_type(AMF_NUMBER)
{
m_value.number = n;
}
AMFValue::AMFValue(int i) :
m_type(AMF_INTEGER)
{
m_value.integer = i;
}
AMFValue::AMFValue(bool b) :
m_type(AMF_BOOLEAN)
{
m_value.boolean = b;
}
AMFValue::AMFValue(const amf_object_t &object) :
m_type(AMF_OBJECT)
{
m_value.object = new amf_object_t(object);
}
AMFValue::AMFValue(const AMFValue &from) :
m_type(AMF_NULL)
{
*this = from;
}
AMFValue::~AMFValue()
{
destroy();
}
void AMFValue::destroy()
{
switch (m_type) {
case AMF_STRING:
delete m_value.string;
break;
case AMF_OBJECT:
case AMF_ECMA_ARRAY:
delete m_value.object;
break;
case AMF_NULL:
case AMF_NUMBER:
case AMF_INTEGER:
case AMF_BOOLEAN:
case AMF_UNDEFINED:
break;
}
}
void AMFValue::operator = (const AMFValue &from)
{
destroy();
m_type = from.m_type;
switch (m_type) {
case AMF_STRING:
m_value.string = new std::string(*from.m_value.string);
break;
case AMF_OBJECT:
case AMF_ECMA_ARRAY:
m_value.object = new amf_object_t(*from.m_value.object);
break;
case AMF_NUMBER:
m_value.number = from.m_value.number;
break;
case AMF_INTEGER:
m_value.integer = from.m_value.integer;
break;
case AMF_BOOLEAN:
m_value.boolean = from.m_value.boolean;
break;
default:
break;
}
}
void amf_write(Encoder *enc, const std::string &s)
{
enc->buf += char(AMF0_STRING);
uint16_t str_len = htons(s.size());
enc->buf.append((char *) &str_len, 2);
enc->buf += s;
}
void amf_write(Encoder *enc, int i)
{
throw std::runtime_error("AMF0 does not have integers");
}
void amf_write(Encoder *enc, double n)
{
enc->buf += char(AMF0_NUMBER);
uint64_t encoded = 0;
#if defined(__i386__) || defined(__x86_64__)
/* Flash uses same floating point format as x86 */
memcpy(&encoded, &n, 8);
#endif
uint32_t val = htonl(encoded >> 32);
enc->buf.append((char *) &val, 4);
val = htonl(encoded);
enc->buf.append((char *) &val, 4);
}
void amf_write(Encoder *enc, bool b)
{
enc->buf += char(AMF0_BOOLEAN);
enc->buf += char(b);
}
void amf_write_key(Encoder *enc, const std::string &s)
{
uint16_t str_len = htons(s.size());
enc->buf.append((char *) &str_len, 2);
enc->buf += s;
}
void amf_write(Encoder *enc, const amf_object_t &object)
{
enc->buf += char(AMF0_OBJECT);
for (amf_object_t::const_iterator i = object.begin();
i != object.end(); ++i) {
amf_write_key(enc, i->first);
amf_write(enc, i->second);
}
amf_write_key(enc, "");
enc->buf += char(AMF0_OBJECT_END);
}
void amf_write_ecma(Encoder *enc, const amf_object_t &object)
{
enc->buf += char(AMF0_ECMA_ARRAY);
uint32_t zero = 0;
enc->buf.append((char *) &zero, 4);
for (amf_object_t::const_iterator i = object.begin();
i != object.end(); ++i) {
amf_write_key(enc, i->first);
amf_write(enc, i->second);
}
amf_write_key(enc, "");
enc->buf += char(AMF0_OBJECT_END);
}
void amf_write_null(Encoder *enc)
{
enc->buf += char(AMF0_NULL);
}
void amf_write(Encoder *enc, const AMFValue &value)
{
switch (value.type()) {
case AMF_STRING:
amf_write(enc, value.as_string());
break;
case AMF_NUMBER:
amf_write(enc, value.as_number());
break;
case AMF_INTEGER:
amf_write(enc, value.as_integer());
break;
case AMF_BOOLEAN:
amf_write(enc, value.as_boolean());
break;
case AMF_OBJECT:
amf_write(enc, value.as_object());
break;
case AMF_ECMA_ARRAY:
amf_write_ecma(enc, value.as_object());
break;
case AMF_NULL:
amf_write_null(enc);
break;
}
}
unsigned int load_amf3_integer(Decoder *dec)
{
unsigned int value = 0;
for (int i = 0; i < 4; ++i) {
uint8_t b = get_byte(dec);
if (i == 3) {
/* use all bits from 4th byte */
value = (value << 8) | b;
break;
}
value = (value << 7) | (b & 0x7f);
if ((b & 0x80) == 0)
break;
}
return value;
}
std::string amf_load_string(Decoder *dec)
{
size_t str_len = 0;
uint8_t type = get_byte(dec);
if (dec->version == 3) {
if (type != AMF3_STRING) {
throw std::runtime_error("Expected a string");
}
str_len = load_amf3_integer(dec) / 2;
} else {
if (type != AMF0_STRING) {
throw std::runtime_error("Expected a string");
}
if (dec->pos + 2 > dec->buf.size()) {
throw std::runtime_error("Not enough data");
}
str_len = load_be16(&dec->buf[dec->pos]);
dec->pos += 2;
}
if (dec->pos + str_len > dec->buf.size()) {
throw std::runtime_error("Not enough data");
}
std::string s(dec->buf, dec->pos, str_len);
dec->pos += str_len;
return s;
}
double amf_load_number(Decoder *dec)
{
if (get_byte(dec) != AMF0_NUMBER) {
throw std::runtime_error("Expected a string");
}
if (dec->pos + 8 > dec->buf.size()) {
throw std::runtime_error("Not enough data");
}
uint64_t val = ((uint64_t) load_be32(&dec->buf[dec->pos]) << 32) |
load_be32(&dec->buf[dec->pos + 4]);
double n = 0;
#if defined(__i386__) || defined(__x86_64__)
/* Flash uses same floating point format as x86 */
memcpy(&n, &val, 8);
#endif
dec->pos += 8;
return n;
}
int amf_load_integer(Decoder *dec)
{
if (dec->version == 3) {
return load_amf3_integer(dec);
} else {
return amf_load_number(dec);
}
}
bool amf_load_boolean(Decoder *dec)
{
if (get_byte(dec) != AMF0_BOOLEAN) {
throw std::runtime_error("Expected a boolean");
}
return get_byte(dec) != 0;
}
std::string amf_load_key(Decoder *dec)
{
if (dec->pos + 2 > dec->buf.size()) {
throw std::runtime_error("Not enough data");
}
size_t str_len = load_be16(&dec->buf[dec->pos]);
dec->pos += 2;
if (dec->pos + str_len > dec->buf.size()) {
throw std::runtime_error("Not enough data");
}
std::string s(dec->buf, dec->pos, str_len);
dec->pos += str_len;
return s;
}
amf_object_t amf_load_object(Decoder *dec)
{
amf_object_t object;
if (get_byte(dec) != AMF0_OBJECT) {
throw std::runtime_error("Expected an object");
}
while (1) {
std::string key = amf_load_key(dec);
if (key.empty())
break;
AMFValue value = amf_load(dec);
object.insert(std::make_pair(key, value));
}
if (get_byte(dec) != AMF0_OBJECT_END) {
throw std::runtime_error("expected object end");
}
return object;
}
amf_object_t amf_load_ecma(Decoder *dec)
{
/* ECMA array is the same as object, with 4 extra zero bytes */
amf_object_t object;
if (get_byte(dec) != AMF0_ECMA_ARRAY) {
throw std::runtime_error("Expected an ECMA array");
}
if (dec->pos + 4 > dec->buf.size()) {
throw std::runtime_error("Not enough data");
}
dec->pos += 4;
while (1) {
std::string key = amf_load_key(dec);
if (key.empty())
break;
AMFValue value = amf_load(dec);
object.insert(std::make_pair(key, value));
}
if (get_byte(dec) != AMF0_OBJECT_END) {
throw std::runtime_error("expected object end");
}
return object;
}
AMFValue amf_load(Decoder *dec)
{
uint8_t type = peek(dec);
if (dec->version == 3) {
switch (type) {
case AMF3_STRING:
return AMFValue(amf_load_string(dec));
case AMF3_NUMBER:
return AMFValue(amf_load_number(dec));
case AMF3_INTEGER:
return AMFValue(amf_load_integer(dec));
case AMF3_FALSE:
dec->pos++;
return AMFValue(false);
case AMF3_TRUE:
dec->pos++;
return AMFValue(true);
case AMF3_OBJECT:
return AMFValue(amf_load_object(dec));
case AMF3_ARRAY:
return AMFValue(amf_load_ecma(dec));
case AMF3_NULL:
dec->pos++;
return AMFValue(AMF_NULL);
case AMF3_UNDEFINED:
dec->pos++;
return AMFValue(AMF_UNDEFINED);
default:
throw std::runtime_error(strf("Unsupported AMF3 type: %02x", type));
}
} else {
switch (type) {
case AMF0_STRING:
return AMFValue(amf_load_string(dec));
case AMF0_NUMBER:
return AMFValue(amf_load_number(dec));
case AMF0_BOOLEAN:
return AMFValue(amf_load_boolean(dec));
case AMF0_OBJECT:
return AMFValue(amf_load_object(dec));
case AMF0_ECMA_ARRAY:
return AMFValue(amf_load_ecma(dec));
case AMF0_NULL:
dec->pos++;
return AMFValue(AMF_NULL);
case AMF0_UNDEFINED:
dec->pos++;
return AMFValue(AMF_UNDEFINED);
default:
throw std::runtime_error(strf("Unsupported AMF0 type: %02x", type));
}
}
}