-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathCborDecoder.java
More file actions
497 lines (445 loc) · 19.6 KB
/
CborDecoder.java
File metadata and controls
497 lines (445 loc) · 19.6 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package io.ipfs.api.cbor;
/*
* JACOB - CBOR implementation in Java.
*
* (C) Copyright - 2013 - J.W. Janssen <j.w.janssen@lxtreme.nl>
*/
import java.io.*;
import java.util.Arrays;
import static io.ipfs.api.cbor.CborConstants.*;
import static io.ipfs.api.cbor.CborType.*;
/**
* Provides a decoder capable of handling CBOR encoded data from a {@link InputStream}.
*/
public class CborDecoder {
protected final PushbackInputStream m_is;
/**
* Creates a new {@link CborDecoder} instance.
*
* @param is the actual input stream to read the CBOR-encoded data from, cannot be <code>null</code>.
*/
public CborDecoder(InputStream is) {
if (is == null) {
throw new IllegalArgumentException("InputStream cannot be null!");
}
m_is = (is instanceof PushbackInputStream) ? (PushbackInputStream) is : new PushbackInputStream(is);
}
private static void fail(String msg, Object... args) throws IOException {
throw new IOException(msg + Arrays.toString(args));
}
private static String lengthToString(int len) {
return (len < 0) ? "no payload" : (len == ONE_BYTE) ? "one byte" : (len == TWO_BYTES) ? "two bytes"
: (len == FOUR_BYTES) ? "four bytes" : (len == EIGHT_BYTES) ? "eight bytes" : "(unknown)";
}
/**
* Peeks in the input stream for the upcoming type.
*
* @return the upcoming type in the stream, or <code>null</code> in case of an end-of-stream.
* @throws IOException in case of I/O problems reading the CBOR-type from the underlying input stream.
*/
public CborType peekType() throws IOException {
int p = m_is.read();
if (p < 0) {
// EOF, nothing to peek at...
return null;
}
m_is.unread(p);
return valueOf(p);
}
/**
* Prolog to reading an array value in CBOR format.
*
* @return the number of elements in the array to read, or <tt>-1</tt> in case of infinite-length arrays.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public long readArrayLength() throws IOException {
return readMajorTypeWithSize(TYPE_ARRAY);
}
/**
* Reads a boolean value in CBOR format.
*
* @return the read boolean.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public boolean readBoolean() throws IOException {
int b = readMajorType(TYPE_FLOAT_SIMPLE);
if (b != FALSE && b != TRUE) {
fail("Unexpected boolean value: %d!", b);
}
return b == TRUE;
}
/**
* Reads a "break"/stop value in CBOR format.
*
* @return always <code>null</code>.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public Object readBreak() throws IOException {
readMajorTypeExact(TYPE_FLOAT_SIMPLE, BREAK);
return null;
}
/**
* Reads a byte string value in CBOR format.
*
* @return the read byte string, never <code>null</code>. In case the encoded string has a length of <tt>0</tt>, an empty string is returned.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public byte[] readByteString() throws IOException {
long len = readMajorTypeWithSize(TYPE_BYTE_STRING);
if (len < 0) {
fail("Infinite-length byte strings not supported!");
}
if (len > Integer.MAX_VALUE) {
fail("String length too long!");
}
return readFully(new byte[(int) len]);
}
/**
* Prolog to reading a byte string value in CBOR format.
*
* @return the number of bytes in the string to read, or <tt>-1</tt> in case of infinite-length strings.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public long readByteStringLength() throws IOException {
return readMajorTypeWithSize(TYPE_BYTE_STRING);
}
/**
* Reads a double-precision float value in CBOR format.
*
* @return the read double value, values from {@link Float#MIN_VALUE} to {@link Float#MAX_VALUE} are supported.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public double readDouble() throws IOException {
readMajorTypeExact(TYPE_FLOAT_SIMPLE, DOUBLE_PRECISION_FLOAT);
return Double.longBitsToDouble(readUInt64());
}
/**
* Reads a single-precision float value in CBOR format.
*
* @return the read float value, values from {@link Float#MIN_VALUE} to {@link Float#MAX_VALUE} are supported.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public float readFloat() throws IOException {
readMajorTypeExact(TYPE_FLOAT_SIMPLE, SINGLE_PRECISION_FLOAT);
return Float.intBitsToFloat((int) readUInt32());
}
/**
* Reads a half-precision float value in CBOR format.
*
* @return the read half-precision float value, values from {@link Float#MIN_VALUE} to {@link Float#MAX_VALUE} are supported.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public double readHalfPrecisionFloat() throws IOException {
readMajorTypeExact(TYPE_FLOAT_SIMPLE, HALF_PRECISION_FLOAT);
int half = readUInt16();
int exp = (half >> 10) & 0x1f;
int mant = half & 0x3ff;
double val;
if (exp == 0) {
val = mant * Math.pow(2, -24);
} else if (exp != 31) {
val = (mant + 1024) * Math.pow(2, exp - 25);
} else if (mant != 0) {
val = Double.NaN;
} else {
val = Double.POSITIVE_INFINITY;
}
return ((half & 0x8000) == 0) ? val : -val;
}
/**
* Reads a signed or unsigned integer value in CBOR format.
*
* @return the read integer value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public long readInt() throws IOException {
int ib = m_is.read();
// in case of negative integers, extends the sign to all bits; otherwise zero...
long ui = expectIntegerType(ib);
// in case of negative integers does a ones complement
return ui ^ readUInt(ib & 0x1f, false /* breakAllowed */);
}
/**
* Reads a signed or unsigned 16-bit integer value in CBOR format.
*
* @read the small integer value, values from <tt>[-65536..65535]</tt> are supported.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
*/
public int readInt16() throws IOException {
int ib = m_is.read();
// in case of negative integers, extends the sign to all bits; otherwise zero...
long ui = expectIntegerType(ib);
// in case of negative integers does a ones complement
return (int) (ui ^ readUIntExact(TWO_BYTES, ib & 0x1f));
}
/**
* Reads a signed or unsigned 32-bit integer value in CBOR format.
*
* @read the small integer value, values in the range <tt>[-4294967296..4294967295]</tt> are supported.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
*/
public long readInt32() throws IOException {
int ib = m_is.read();
// in case of negative integers, extends the sign to all bits; otherwise zero...
long ui = expectIntegerType(ib);
// in case of negative integers does a ones complement
return ui ^ readUIntExact(FOUR_BYTES, ib & 0x1f);
}
/**
* Reads a signed or unsigned 64-bit integer value in CBOR format.
*
* @read the small integer value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
*/
public long readInt64() throws IOException {
int ib = m_is.read();
// in case of negative integers, extends the sign to all bits; otherwise zero...
long ui = expectIntegerType(ib);
// in case of negative integers does a ones complement
return ui ^ readUIntExact(EIGHT_BYTES, ib & 0x1f);
}
/**
* Reads a signed or unsigned 8-bit integer value in CBOR format.
*
* @read the small integer value, values in the range <tt>[-256..255]</tt> are supported.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
*/
public int readInt8() throws IOException {
int ib = m_is.read();
// in case of negative integers, extends the sign to all bits; otherwise zero...
long ui = expectIntegerType(ib);
// in case of negative integers does a ones complement
return (int) (ui ^ readUIntExact(ONE_BYTE, ib & 0x1f));
}
/**
* Prolog to reading a map of key-value pairs in CBOR format.
*
* @return the number of entries in the map, >= 0.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public long readMapLength() throws IOException {
return readMajorTypeWithSize(TYPE_MAP);
}
/**
* Reads a <code>null</code>-value in CBOR format.
*
* @return always <code>null</code>.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public Object readNull() throws IOException {
readMajorTypeExact(TYPE_FLOAT_SIMPLE, NULL);
return null;
}
/**
* Reads a single byte value in CBOR format.
*
* @return the read byte value.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public byte readSimpleValue() throws IOException {
readMajorTypeExact(TYPE_FLOAT_SIMPLE, ONE_BYTE);
return (byte) readUInt8();
}
/**
* Reads a signed or unsigned small (<= 23) integer value in CBOR format.
*
* @read the small integer value, values in the range <tt>[-24..23]</tt> are supported.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying output stream.
*/
public int readSmallInt() throws IOException {
int ib = m_is.read();
// in case of negative integers, extends the sign to all bits; otherwise zero...
long ui = expectIntegerType(ib);
// in case of negative integers does a ones complement
return (int) (ui ^ readUIntExact(-1, ib & 0x1f));
}
/**
* Reads a semantic tag value in CBOR format.
*
* @return the read tag value.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public long readTag() throws IOException {
return readUInt(readMajorType(TYPE_TAG), false /* breakAllowed */);
}
/**
* Reads an UTF-8 encoded string value in CBOR format.
*
* @return the read UTF-8 encoded string, never <code>null</code>. In case the encoded string has a length of <tt>0</tt>, an empty string is returned.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public String readTextString() throws IOException {
long len = readMajorTypeWithSize(TYPE_TEXT_STRING);
if (len < 0) {
fail("Infinite-length text strings not supported!");
}
if (len > Integer.MAX_VALUE) {
fail("String length too long!");
}
return new String(readFully(new byte[(int) len]), "UTF-8");
}
/**
* Prolog to reading an UTF-8 encoded string value in CBOR format.
*
* @return the length of the string to read, or <tt>-1</tt> in case of infinite-length strings.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public long readTextStringLength() throws IOException {
return readMajorTypeWithSize(TYPE_TEXT_STRING);
}
/**
* Reads an undefined value in CBOR format.
*
* @return always <code>null</code>.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
public Object readUndefined() throws IOException {
readMajorTypeExact(TYPE_FLOAT_SIMPLE, UNDEFINED);
return null;
}
/**
* Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation.
*
* @param majorType the expected major type, cannot be <code>null</code> (unchecked).
* @return either <tt>-1</tt> if the major type was an signed integer, or <tt>0</tt> otherwise.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
protected long expectIntegerType(int ib) throws IOException {
int majorType = ((ib & 0xFF) >>> 5);
if ((majorType != TYPE_UNSIGNED_INTEGER) && (majorType != TYPE_NEGATIVE_INTEGER)) {
fail("Unexpected type: %s, expected type %s or %s!", getName(majorType), getName(TYPE_UNSIGNED_INTEGER),
getName(TYPE_NEGATIVE_INTEGER));
}
return -majorType;
}
/**
* Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation.
*
* @param majorType the expected major type, cannot be <code>null</code> (unchecked).
* @return the read subtype, or payload, of the read major type.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
protected int readMajorType(int majorType) throws IOException {
int ib = m_is.read();
if (majorType != ((ib >>> 5) & 0x07)) {
fail("Unexpected type: %s, expected: %s!", getName(ib), getName(majorType));
}
return ib & 0x1F;
}
/**
* Reads the next major type from the underlying input stream, and verifies whether it matches the given expectations.
*
* @param majorType the expected major type, cannot be <code>null</code> (unchecked);
* @param subtype the expected subtype.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
protected void readMajorTypeExact(int majorType, int subtype) throws IOException {
int st = readMajorType(majorType);
if ((st ^ subtype) != 0) {
fail("Unexpected subtype: %d, expected: %d!", st, subtype);
}
}
/**
* Reads the next major type from the underlying input stream, verifies whether it matches the given expectation, and decodes the payload into a size.
*
* @param majorType the expected major type, cannot be <code>null</code> (unchecked).
* @return the number of succeeding bytes, >= 0, or <tt>-1</tt> if an infinite-length type is read.
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
*/
protected long readMajorTypeWithSize(int majorType) throws IOException {
return readUInt(readMajorType(majorType), true /* breakAllowed */);
}
/**
* Reads an unsigned integer with a given length-indicator.
*
* @param length the length indicator to use;
* @return the read unsigned integer, as long value.
* @throws IOException in case of I/O problems reading the unsigned integer from the underlying input stream.
*/
protected long readUInt(int length, boolean breakAllowed) throws IOException {
long result = -1;
if (length < ONE_BYTE) {
result = length;
} else if (length == ONE_BYTE) {
result = readUInt8();
} else if (length == TWO_BYTES) {
result = readUInt16();
} else if (length == FOUR_BYTES) {
result = readUInt32();
} else if (length == EIGHT_BYTES) {
result = readUInt64();
} else if (breakAllowed && length == BREAK) {
return -1;
}
if (result < 0) {
fail("Not well-formed CBOR integer found, invalid length: %d!", result);
}
return result;
}
/**
* Reads an unsigned 16-bit integer value
*
* @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
* @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
*/
protected int readUInt16() throws IOException {
byte[] buf = readFully(new byte[2]);
return (buf[0] & 0xFF) << 8 | (buf[1] & 0xFF);
}
/**
* Reads an unsigned 32-bit integer value
*
* @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
* @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
*/
protected long readUInt32() throws IOException {
byte[] buf = readFully(new byte[4]);
return ((buf[0] & 0xFF) << 24 | (buf[1] & 0xFF) << 16 | (buf[2] & 0xFF) << 8 | (buf[3] & 0xFF)) & 0xffffffffL;
}
/**
* Reads an unsigned 64-bit integer value
*
* @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
* @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
*/
protected long readUInt64() throws IOException {
byte[] buf = readFully(new byte[8]);
return (buf[0] & 0xFFL) << 56 | (buf[1] & 0xFFL) << 48 | (buf[2] & 0xFFL) << 40 | (buf[3] & 0xFFL) << 32 | //
(buf[4] & 0xFFL) << 24 | (buf[5] & 0xFFL) << 16 | (buf[6] & 0xFFL) << 8 | (buf[7] & 0xFFL);
}
/**
* Reads an unsigned 8-bit integer value
*
* @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
* @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
*/
protected int readUInt8() throws IOException {
return m_is.read() & 0xff;
}
/**
* Reads an unsigned integer with a given length-indicator.
*
* @param length the length indicator to use;
* @return the read unsigned integer, as long value.
* @throws IOException in case of I/O problems reading the unsigned integer from the underlying input stream.
*/
protected long readUIntExact(int expectedLength, int length) throws IOException {
if (((expectedLength == -1) && (length >= ONE_BYTE)) || ((expectedLength >= 0) && (length != expectedLength))) {
fail("Unexpected payload/length! Expected %s, but got %s.", lengthToString(expectedLength),
lengthToString(length));
}
return readUInt(length, false /* breakAllowed */);
}
private byte[] readFully(byte[] buf) throws IOException {
int len = buf.length;
int n = 0, off = 0;
while (n < len) {
int count = m_is.read(buf, off + n, len - n);
if (count < 0) {
throw new EOFException();
}
n += count;
}
return buf;
}
}