-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathxdr-reader.js
More file actions
168 lines (153 loc) · 3.39 KB
/
xdr-reader.js
File metadata and controls
168 lines (153 loc) · 3.39 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
/**
* @internal
*/
import { XdrReaderError } from '../errors';
export class XdrReader {
/**
* @constructor
* @param {Buffer} source - Buffer containing serialized data
*/
constructor(source) {
if (!Buffer.isBuffer(source)) {
if (
source instanceof Array ||
Array.isArray(source) ||
ArrayBuffer.isView(source)
) {
source = Buffer.from(source);
} else {
throw new XdrReaderError(`source invalid: ${source}`);
}
}
this._buffer = source;
this._length = source.length;
this._index = 0;
}
/**
* @type {Buffer}
* @private
* @readonly
*/
_buffer;
/**
* @type {Number}
* @private
* @readonly
*/
_length;
/**
* @type {Number}
* @private
* @readonly
*/
_index;
/**
* Check if the reader reached the end of the input buffer
* @return {Boolean}
*/
get eof() {
return this._index === this._length;
}
/**
* Advance reader position, check padding and overflow
* @param {Number} size - Bytes to read
* @return {Number} Position to read from
* @private
*/
advance(size) {
const from = this._index;
// advance cursor position
this._index += size;
// check buffer boundaries
if (this._length < this._index)
throw new XdrReaderError(
'attempt to read outside the boundary of the buffer'
);
// check that padding is correct for Opaque and String
const padding = 4 - (size % 4 || 4);
if (padding > 0) {
for (let i = 0; i < padding; i++)
if (this._buffer[this._index + i] !== 0)
// all bytes in the padding should be zeros
throw new XdrReaderError('invalid padding');
this._index += padding;
}
return from;
}
/**
* Reset reader position
* @return {void}
*/
rewind() {
this._index = 0;
}
/**
* Remaining unread bytes in the source buffer
* @return {Number}
*/
remainingBytes() {
return this._length - this._index;
}
/**
* Read byte array from the buffer
* @param {Number} size - Bytes to read
* @return {Buffer} - Sliced portion of the underlying buffer
*/
read(size) {
const from = this.advance(size);
return this._buffer.subarray(from, from + size);
}
/**
* Read i32 from buffer
* @return {Number}
*/
readInt32BE() {
return this._buffer.readInt32BE(this.advance(4));
}
/**
* Read u32 from buffer
* @return {Number}
*/
readUInt32BE() {
return this._buffer.readUInt32BE(this.advance(4));
}
/**
* Read i64 from buffer
* @return {BigInt}
*/
readBigInt64BE() {
return this._buffer.readBigInt64BE(this.advance(8));
}
/**
* Read u64 from buffer
* @return {BigInt}
*/
readBigUInt64BE() {
return this._buffer.readBigUInt64BE(this.advance(8));
}
/**
* Read float from buffer
* @return {Number}
*/
readFloatBE() {
return this._buffer.readFloatBE(this.advance(4));
}
/**
* Read double from buffer
* @return {Number}
*/
readDoubleBE() {
return this._buffer.readDoubleBE(this.advance(8));
}
/**
* Ensure that input buffer has been consumed in full, otherwise it's a type mismatch
* @return {void}
* @throws {XdrReaderError}
*/
ensureInputConsumed() {
if (this._index !== this._length)
throw new XdrReaderError(
`invalid XDR contract typecast - source buffer not entirely consumed`
);
}
}