-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathAMFEncode.mjs
More file actions
142 lines (128 loc) · 3.5 KB
/
Copy pathAMFEncode.mjs
File metadata and controls
142 lines (128 loc) · 3.5 KB
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
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2022
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import Utils from "../Utils.mjs";
import "reflect-metadata"; // Required as a shim for the amf library
import { AMF0 } from "@astronautlabs/amf";
const AMF3_MARKER = {
Null: 0x01,
False: 0x02,
True: 0x03,
Double: 0x05,
String: 0x06,
Array: 0x09,
Object: 0x0a
};
/**
* @param {number} value
* @returns {number[]}
*/
function encodeU29(value) {
if (value < 0x80) {
return [value];
}
if (value < 0x4000) {
return [
((value >> 7) & 0x7f) | 0x80,
value & 0x7f
];
}
if (value < 0x200000) {
return [
((value >> 14) & 0x7f) | 0x80,
((value >> 7) & 0x7f) | 0x80,
value & 0x7f
];
}
return [
((value >> 22) & 0x7f) | 0x80,
((value >> 15) & 0x7f) | 0x80,
((value >> 8) & 0x7f) | 0x80,
value & 0xff
];
}
/**
* @param {string} value
* @returns {number[]}
*/
function encodeAMF3Utf8(value) {
const bytes = Utils.strToUtf8ByteArray(value);
return encodeU29((bytes.length << 1) | 1).concat(bytes);
}
/**
* @param {number} value
* @returns {number[]}
*/
function encodeAMF3Double(value) {
const bytes = new Uint8Array(8);
new DataView(bytes.buffer).setFloat64(0, value, false);
return [AMF3_MARKER.Double].concat(Array.from(bytes));
}
/**
* @param {JSON} value
* @returns {number[]}
*/
function encodeAMF3Value(value) {
if (value === null) return [AMF3_MARKER.Null];
if (value === false) return [AMF3_MARKER.False];
if (value === true) return [AMF3_MARKER.True];
if (typeof value === "number") return encodeAMF3Double(value);
if (typeof value === "string") return [AMF3_MARKER.String].concat(encodeAMF3Utf8(value));
if (Array.isArray(value)) {
return [
AMF3_MARKER.Array,
...encodeU29((value.length << 1) | 1),
0x01,
...value.flatMap(encodeAMF3Value)
];
}
const keys = Object.keys(value);
return [
AMF3_MARKER.Object,
...encodeU29((keys.length << 4) | 0x03),
0x01,
...keys.flatMap(encodeAMF3Utf8),
...keys.flatMap(key => encodeAMF3Value(value[key]))
];
}
/**
* AMF Encode operation
*/
class AMFEncode extends Operation {
/**
* AMFEncode constructor
*/
constructor() {
super();
this.name = "AMF Encode";
this.module = "Encodings";
this.description = "Action Message Format (AMF) is a binary format used to serialize object graphs such as ActionScript objects and XML, or send messages between an Adobe Flash client and a remote service, usually a Flash Media Server or third party alternatives.";
this.infoURL = "https://wikipedia.org/wiki/Action_Message_Format";
this.inputType = "JSON";
this.outputType = "ArrayBuffer";
this.args = [
{
name: "Format",
type: "option",
value: ["AMF0", "AMF3"],
defaultIndex: 1
}
];
}
/**
* @param {JSON} input
* @param {Object[]} args
* @returns {ArrayBuffer}
*/
run(input, args) {
const [format] = args;
const output = format === "AMF0" ?
AMF0.Value.any(input).serialize() :
Uint8Array.from(encodeAMF3Value(input));
return output.buffer;
}
}
export default AMFEncode;