-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathInsertBytes.mjs
81 lines (72 loc) · 2.12 KB
/
InsertBytes.mjs
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
/**
* @author Didier Stevens [[email protected]]
* @copyright Crown Copyright 2022
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import {BITWISE_OP_DELIMS} from "../lib/BitwiseOp.mjs";
import Utils from "../Utils.mjs";
/**
* Insert bytes operation
*/
class InsertBytes extends Operation {
/**
* InsertBytes constructor
*/
constructor() {
super();
this.name = "Insert bytes";
this.module = "Default";
this.description = "Insert bytes at arbitrary position. Options 'from end' and 'overwrite' available.";
this.infoURL = "";
this.inputType = "byteArray";
this.outputType = "byteArray";
this.args = [
{
"name": "Bytes",
"type": "toggleString",
"value": "",
"toggleValues": BITWISE_OP_DELIMS
},
{
"name": "Start",
"type": "number",
"value": 0
},
{
"name": "From end",
"type": "boolean",
"value": false
},
{
"name": "Overwrite",
"type": "boolean",
"value": false
}
];
}
/**
* @param {byteArray} input
* @param {Object[]} args
* @returns {byteArray}
*/
run(input, args) {
const value = Utils.convertToByteArray(args[0].string || "", args[0].option);
let start = args[1];
const fromend = args[2];
const overwrite = args[3];
if (start < 0)
throw new OperationError("Start must not be negative");
if (start > input.length)
throw new OperationError("Start must not be bigger than input");
if (fromend)
start = input.length - start;
const left = input.slice(0, start);
let right = input.slice(start);
if (overwrite)
right = right.slice(value.length);
return left.concat(value, right);
}
}
export default InsertBytes;