-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
BinaryTag.java
346 lines (313 loc) · 12.1 KB
/
BinaryTag.java
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
package com.denizenscript.denizencore.objects.core;
import com.denizenscript.denizencore.objects.Fetchable;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.AsciiMatcher;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class BinaryTag implements ObjectTag {
// <--[ObjectType]
// @name BinaryTag
// @prefix binary
// @base ElementTag
// @ExampleTagBase binary[ff000010]
// @ExampleValues ff0000,48454c4c4f20574f524c44
// @ExampleForReturns
// - filewrite path:data/mypath.dat data:%VALUE%
// @format
// The identity format for BinaryTag is a hex encoding of the byte set, in order.
// Each byte is encoding as an individual big-endian hexadecimal pair, like "FF" for byte=255, "00" for byte=0, "0F" for 15, "F0" for 240, ... etc.
//
// @description
// BinaryTags represent raw binary data in Denizen.
// This is useful in particular for file or network interop.
// Generally you should only be interacting with BinaryTag if you have a specific reason to, otherwise there are usually more appropriate types to use.
// Most content you'll come across will be encoded in a text-based format, not a binary one.
//
// -->
public static byte[] EMPTY = new byte[0];
@Fetchable("binary")
public static BinaryTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
string = CoreUtilities.toLowerCase(string);
if (string.startsWith("binary@")) {
string = string.substring("binary@".length());
}
if (string.isEmpty()) {
return new BinaryTag(EMPTY);
}
if (!VALID_HEX.isOnlyMatches(string)) {
return null;
}
if (string.length() % 2 == 1) {
return null;
}
return new BinaryTag(CoreUtilities.hexDecode(string));
}
public static AsciiMatcher VALID_HEX = new AsciiMatcher(AsciiMatcher.DIGITS + "abcdefABCDEF");
public static boolean matches(String string) {
try {
if (string.startsWith("binary@")) {
return true;
}
if (!VALID_HEX.isOnlyMatches(string)) {
return false;
}
if (string.length() % 2 == 1) {
return false;
}
return true;
}
catch (Exception e) {
return false;
}
}
public byte[] data;
private String prefix = "Binary";
public BinaryTag(byte[] data) {
this.data = data;
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public ObjectTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
public static String hexEncode(byte[] data, boolean spaced) {
if (!spaced) {
return CoreUtilities.hexEncode(data);
}
char[] output = new char[data.length * 3];
for (int i = 0; i < data.length; i++) {
byte valA = (byte) ((data[i] & 0xF0) >> 4);
byte valB = (byte) (data[i] & 0x0F);
output[i * 3] = CoreUtilities.charForByte[valA];
output[(i * 3) + 1] = CoreUtilities.charForByte[valB];
output[(i * 3) + 2] = ' ';
}
return new String(output);
}
@Override
public String debuggable() {
return "binary@<GR>" + hexEncode(data, true);
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String getObjectType() {
return "binary";
}
@Override
public String identify() {
return "binary@" + hexEncode(data, false);
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
public static void registerTags() {
// <--[tag]
// @attribute <BinaryTag.length>
// @returns ElementTag(Number)
// @description
// Returns the number of bytes in this BinaryTag.
// @example
// # Narrates 3
// - narrate <binary[010203].length>
// -->
tagProcessor.registerStaticTag(ElementTag.class, "length", (attribute, object) -> {
return new ElementTag(object.data.length);
});
// <--[tag]
// @attribute <BinaryTag.to_hex>
// @returns ElementTag
// @description
// Returns a flat hexadecimal encoding of the binary data.
// @example
// # Narrates 010203
// - narrate <binary[010203].to_hex>
// -->
tagProcessor.registerStaticTag(ElementTag.class, "to_hex", (attribute, object) -> {
return new ElementTag(hexEncode(object.data, false));
});
// <--[tag]
// @attribute <BinaryTag.to_base64>
// @returns ElementTag
// @description
// Returns a base64 encoding of the binary data.
// See also <@link tag ElementTag.base64_to_binary>
// @example
// - define data <binary[48454c4c4f20574f524c44]>
// - define encoded <[data].to_base64>
// - define decoded <[encoded].base64_to_binary>
// - if <[decoded].to_hex> == <[data].to_hex>:
// - narrate "Everything works!"
// -->
tagProcessor.registerStaticTag(ElementTag.class, "to_base64", (attribute, object) -> {
return new ElementTag(Base64.getEncoder().encodeToString(object.data));
});
// <--[tag]
// @attribute <BinaryTag.utf8_decode>
// @returns ElementTag
// @description
// Returns the raw text represented by this binary data, decoding using the standard UTF-8 encoding.
// See also <@link tag ElementTag.utf8_encode>
// @example
// # narrates "HELLO WORLD"
// - narrate <binary[48454c4c4f20574f524c44].utf8_decode>
// -->
tagProcessor.registerStaticTag(ElementTag.class, "utf8_decode", (attribute, object) -> {
return new ElementTag(new String(object.data, StandardCharsets.UTF_8));
});
// <--[tag]
// @attribute <BinaryTag.text_decode[<encoding>]>
// @returns ElementTag
// @description
// Returns the raw text represented by this binary data, decoding using the specified encoding method.
// Input can be for example "utf-8" or "iso-8859-1".
// See also <@link tag ElementTag.text_encode>
// @example
// # narrates "HELLO WORLD"
// - narrate <binary[48454c4c4f20574f524c44].text_decode[us-ascii]>
// -->
tagProcessor.registerStaticTag(ElementTag.class, "text_decode", (attribute, object) -> {
if (!attribute.hasParam()) {
return null;
}
try {
return new ElementTag(new String(object.data, attribute.getParam()));
}
catch (UnsupportedEncodingException ex) {
attribute.echoError("Invalid encoding '" + attribute.getParam() + "'");
return null;
}
});
// <--[tag]
// @attribute <BinaryTag.decode_integer>
// @returns ElementTag(Number)
// @description
// Returns the integer number represented by this binary data.
// Data must be 1, 2, 4, or 8 bytes long.
// Uses big-endian twos-complement format.
// See also <@link tag ElementTag.integer_to_binary>
// @example
// # Narrates '255'
// - narrate <binary[000000ff].decode_integer>
// -->
tagProcessor.registerStaticTag(ElementTag.class, "decode_integer", (attribute, object) -> {
if (object.data.length == 1) {
return new ElementTag(object.data[0]);
}
if (object.data.length > 8) {
return null;
}
ByteBuffer buffer = ByteBuffer.wrap(object.data);
if (object.data.length == 2) {
return new ElementTag(buffer.getShort());
}
else if (object.data.length == 4) {
return new ElementTag(buffer.getInt());
}
if (object.data.length == 8) {
return new ElementTag(buffer.getLong());
}
return null;
});
// <--[tag]
// @attribute <BinaryTag.gzip_compress>
// @returns BinaryTag
// @description
// Returns the binary data, compressed via gzip.
// See also <@link tag BinaryTag.gzip_decompress>
// @example
// - define data <binary[48454c4c4f20574f524c44]>
// - define compressed <[data].gzip_compress>
// - define decompressed <[data].gzip_decompress>
// - if <[decompressed].to_hex> == <[data].to_hex>:
// - narrate "Everything works!"
// -->
tagProcessor.registerStaticTag(BinaryTag.class, "gzip_compress", (attribute, object) -> {
return new BinaryTag(compressGzip(object.data));
});
// <--[tag]
// @attribute <BinaryTag.gzip_decompress>
// @returns BinaryTag
// @description
// Returns the binary data, compressed via gzip.
// See also <@link tag BinaryTag.gzip_compress>
// @example
// - define data <binary[48454c4c4f20574f524c44]>
// - define compressed <[data].gzip_compress>
// - define decompressed <[data].gzip_decompress>
// - if <[decompressed].to_hex> == <[data].to_hex>:
// - narrate "Everything works!"
// -->
tagProcessor.registerStaticTag(BinaryTag.class, "gzip_decompress", (attribute, object) -> {
return new BinaryTag(decompressGzip(object.data));
});
}
private static byte[] decompressGzip(byte[] data) {
try {
ByteArrayInputStream bytesIn = new ByteArrayInputStream(data);
GZIPInputStream gzipIn = new GZIPInputStream(bytesIn);
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(data.length);
byte[] buffer = new byte[1024];
int len;
while ((len = gzipIn.read(buffer, 0, 1024)) != -1) {
bytesOut.write(buffer, 0, len);
}
byte[] result = bytesOut.toByteArray();
gzipIn.close();
bytesIn.close();
bytesOut.close();
return result;
}
catch (IOException ex) {
Debug.echoError(ex);
return null;
}
}
private static byte[] compressGzip(byte[] data) {
try {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(data.length);
GZIPOutputStream gzipOut = new GZIPOutputStream(bytesOut);
gzipOut.write(data);
gzipOut.finish();
byte[] result = bytesOut.toByteArray();
gzipOut.close();
bytesOut.close();
return result;
}
catch (IOException ex) {
Debug.echoError(ex);
return null;
}
}
public static ObjectTagProcessor<BinaryTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
}