node-opcua-binary-stream 2.64.0 → 2.64.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/binaryStream.d.ts +159 -159
- package/dist/binaryStream.js +425 -425
- package/dist/binaryStreamSizeCalculator.d.ts +29 -29
- package/dist/binaryStreamSizeCalculator.js +76 -76
- package/dist/index.d.ts +8 -8
- package/dist/index.js +14 -14
- package/package.json +36 -36
package/dist/binaryStream.js
CHANGED
|
@@ -1,426 +1,426 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateByteLength = exports.BinaryStream = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* @module node-opcua-binary-stream
|
|
6
|
-
*/
|
|
7
|
-
require("util");
|
|
8
|
-
const node_opcua_assert_1 = require("node-opcua-assert");
|
|
9
|
-
const node_opcua_buffer_utils_1 = require("node-opcua-buffer-utils");
|
|
10
|
-
const MAXUINT32 = 4294967295; // 2**32 -1;
|
|
11
|
-
const noAssert = false;
|
|
12
|
-
const performCheck = false;
|
|
13
|
-
/**
|
|
14
|
-
* a BinaryStream can be use to perform sequential read or write
|
|
15
|
-
* inside a buffer.
|
|
16
|
-
* The BinaryStream maintains a cursor up to date as the caller
|
|
17
|
-
* operates on the stream using the various read/write methods.
|
|
18
|
-
* It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
|
|
19
|
-
* It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
|
|
20
|
-
* convention.
|
|
21
|
-
*
|
|
22
|
-
* data can either be:
|
|
23
|
-
*
|
|
24
|
-
* * a Buffer , in this case the BinaryStream operates on this Buffer
|
|
25
|
-
* * null , in this case a BinaryStream with 1024 bytes is created
|
|
26
|
-
* * any data , in this case the object is converted into a binary buffer.
|
|
27
|
-
*
|
|
28
|
-
* example:
|
|
29
|
-
*
|
|
30
|
-
* ``` javascript
|
|
31
|
-
* var stream = new BinaryStream(32)
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @class BinaryStream
|
|
35
|
-
* @param {null|Buffer|Number} data
|
|
36
|
-
* @constructor
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*/
|
|
41
|
-
class BinaryStream {
|
|
42
|
-
constructor(data) {
|
|
43
|
-
if (data === undefined) {
|
|
44
|
-
this.buffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(1024);
|
|
45
|
-
}
|
|
46
|
-
else if (typeof data === "number") {
|
|
47
|
-
this.buffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(data);
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
(0, node_opcua_assert_1.assert)(data instanceof Buffer);
|
|
51
|
-
this.buffer = data;
|
|
52
|
-
}
|
|
53
|
-
this.length = 0;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* set the cursor to the begining of the stream
|
|
57
|
-
* @method BinaryStream.rewind
|
|
58
|
-
*/
|
|
59
|
-
rewind() {
|
|
60
|
-
this.length = 0;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* write a single signed byte (8 bits) to the stream.
|
|
64
|
-
* value must be in the range of [-127,128]
|
|
65
|
-
* @param value the value to write
|
|
66
|
-
*/
|
|
67
|
-
writeInt8(value) {
|
|
68
|
-
// istanbul ignore next
|
|
69
|
-
if (performCheck) {
|
|
70
|
-
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1, "not enough space in buffer");
|
|
71
|
-
(0, node_opcua_assert_1.assert)(value >= -128 && value < 128);
|
|
72
|
-
}
|
|
73
|
-
this.buffer.writeInt8(value, this.length);
|
|
74
|
-
this.length += 1;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* write a single unsigned byte (8 bits) to the stream.
|
|
78
|
-
* @param value the value to write
|
|
79
|
-
*/
|
|
80
|
-
writeUInt8(value) {
|
|
81
|
-
// istanbul ignore next
|
|
82
|
-
if (performCheck) {
|
|
83
|
-
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1, "not enough space in buffer");
|
|
84
|
-
(0, node_opcua_assert_1.assert)(value >= 0 && value < 256, " writeUInt8 : out of bound ");
|
|
85
|
-
}
|
|
86
|
-
this.buffer.writeUInt8(value, this.length);
|
|
87
|
-
this.length += 1;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* write a single 16 bit signed integer to the stream.
|
|
91
|
-
* @param value the value to write
|
|
92
|
-
*/
|
|
93
|
-
writeInt16(value) {
|
|
94
|
-
// istanbul ignore next
|
|
95
|
-
if (performCheck) {
|
|
96
|
-
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 2, "not enough space in buffer");
|
|
97
|
-
}
|
|
98
|
-
this.buffer.writeInt16LE(value, this.length);
|
|
99
|
-
this.length += 2;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* write a single 16 bit unsigned integer to the stream.
|
|
103
|
-
* @param value the value to write
|
|
104
|
-
*/
|
|
105
|
-
writeUInt16(value) {
|
|
106
|
-
// istanbul ignore next
|
|
107
|
-
if (performCheck) {
|
|
108
|
-
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 2, "not enough space in buffer");
|
|
109
|
-
}
|
|
110
|
-
this.buffer.writeUInt16LE(value, this.length);
|
|
111
|
-
this.length += 2;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* write a single 32 bit signed integer to the stream.
|
|
115
|
-
* @param value the value to write
|
|
116
|
-
*/
|
|
117
|
-
writeInteger(value) {
|
|
118
|
-
// istanbul ignore next
|
|
119
|
-
if (performCheck) {
|
|
120
|
-
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
|
|
121
|
-
}
|
|
122
|
-
this.buffer.writeInt32LE(value, this.length);
|
|
123
|
-
this.length += 4;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* write a single 32 bit unsigned integer to the stream.
|
|
127
|
-
*
|
|
128
|
-
* @param value the value to write
|
|
129
|
-
*/
|
|
130
|
-
writeUInt32(value) {
|
|
131
|
-
// istanbul ignore next
|
|
132
|
-
if (performCheck) {
|
|
133
|
-
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
|
|
134
|
-
(0, node_opcua_assert_1.assert)(isFinite(value));
|
|
135
|
-
(0, node_opcua_assert_1.assert)(value >= 0 && value <= MAXUINT32);
|
|
136
|
-
}
|
|
137
|
-
this.buffer.writeUInt32LE(value, this.length);
|
|
138
|
-
this.length += 4;
|
|
139
|
-
/*
|
|
140
|
-
assert(this.buffer[this.length - 4] === value % 256);
|
|
141
|
-
assert(this.buffer[this.length - 3] === (value >>> 8) % 256);
|
|
142
|
-
assert(this.buffer[this.length - 2] === (value >>> 16) % 256);
|
|
143
|
-
assert(this.buffer[this.length - 1] === (value >>> 24) % 256);
|
|
144
|
-
*/
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* write a single 32 bit floating number to the stream.
|
|
148
|
-
* @param value the value to write
|
|
149
|
-
*/
|
|
150
|
-
writeFloat(value) {
|
|
151
|
-
// istanbul ignore next
|
|
152
|
-
if (performCheck) {
|
|
153
|
-
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
|
|
154
|
-
}
|
|
155
|
-
this.buffer.writeFloatLE(value, this.length);
|
|
156
|
-
this.length += 4;
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* write a single 64 bit floating number to the stream.
|
|
160
|
-
* @param value the value to write
|
|
161
|
-
*/
|
|
162
|
-
writeDouble(value) {
|
|
163
|
-
// istanbul ignore next
|
|
164
|
-
if (performCheck) {
|
|
165
|
-
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 8, "not enough space in buffer");
|
|
166
|
-
}
|
|
167
|
-
this.buffer.writeDoubleLE(value, this.length);
|
|
168
|
-
this.length += 8;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* @param arrayBuf a buffer or byte array write
|
|
172
|
-
* @param offset the offset position (default =0)
|
|
173
|
-
* @param length the number of byte to write
|
|
174
|
-
*/
|
|
175
|
-
writeArrayBuffer(arrayBuf, offset = 0, length = 0) {
|
|
176
|
-
// istanbul ignore next
|
|
177
|
-
if (performCheck) {
|
|
178
|
-
(0, node_opcua_assert_1.assert)(arrayBuf instanceof ArrayBuffer);
|
|
179
|
-
}
|
|
180
|
-
const byteArr = new Uint8Array(arrayBuf);
|
|
181
|
-
const n = (length || byteArr.length) + offset;
|
|
182
|
-
for (let i = offset; i < n; i++) {
|
|
183
|
-
this.buffer[this.length++] = byteArr[i];
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
// writeArrayBuffer(arrayBuf, offset, length) {
|
|
187
|
-
// offset = offset || 0;
|
|
188
|
-
//
|
|
189
|
-
// assert(arrayBuf instanceof ArrayBuffer);
|
|
190
|
-
// const byteArr = new Uint8Array(arrayBuf);
|
|
191
|
-
// length = length || byteArr.length;
|
|
192
|
-
// if (length === 0) {
|
|
193
|
-
// return;
|
|
194
|
-
// }
|
|
195
|
-
// this.length += my_memcpy(this.buffer, this.length, byteArr, offset, offset + length);
|
|
196
|
-
// }
|
|
197
|
-
/**
|
|
198
|
-
* read a single signed byte (8 bits) from the stream.
|
|
199
|
-
* @return the value read
|
|
200
|
-
*/
|
|
201
|
-
readByte() {
|
|
202
|
-
const retVal = this.buffer.readInt8(this.length);
|
|
203
|
-
this.length += 1;
|
|
204
|
-
return retVal;
|
|
205
|
-
}
|
|
206
|
-
readInt8() {
|
|
207
|
-
return this.readByte();
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* read a single unsigned byte (8 bits) from the stream.
|
|
211
|
-
*/
|
|
212
|
-
readUInt8() {
|
|
213
|
-
// istanbul ignore next
|
|
214
|
-
if (performCheck) {
|
|
215
|
-
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1);
|
|
216
|
-
}
|
|
217
|
-
const retVal = this.buffer.readUInt8(this.length);
|
|
218
|
-
this.length += 1;
|
|
219
|
-
return retVal;
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* read a single signed 16-bit integer from the stream.
|
|
223
|
-
*/
|
|
224
|
-
readInt16() {
|
|
225
|
-
const retVal = this.buffer.readInt16LE(this.length);
|
|
226
|
-
this.length += 2;
|
|
227
|
-
return retVal;
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* read a single unsigned 16-bit integer from the stream.
|
|
231
|
-
*/
|
|
232
|
-
readUInt16() {
|
|
233
|
-
const retVal = this.buffer.readUInt16LE(this.length);
|
|
234
|
-
this.length += 2;
|
|
235
|
-
return retVal;
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* read a single signed 32-bit integer from the stream.
|
|
239
|
-
*/
|
|
240
|
-
readInteger() {
|
|
241
|
-
const retVal = this.buffer.readInt32LE(this.length);
|
|
242
|
-
this.length += 4;
|
|
243
|
-
return retVal;
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* read a single unsigned 32-bit integer from the stream.
|
|
247
|
-
*/
|
|
248
|
-
readUInt32() {
|
|
249
|
-
const retVal = this.buffer.readUInt32LE(this.length);
|
|
250
|
-
this.length += 4;
|
|
251
|
-
return retVal;
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* read a single 32-bit floating point number from the stream.
|
|
255
|
-
*/
|
|
256
|
-
readFloat() {
|
|
257
|
-
const retVal = this.buffer.readFloatLE(this.length);
|
|
258
|
-
this.length += 4;
|
|
259
|
-
return retVal;
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* read a single 64-bit floating point number from the stream.
|
|
263
|
-
*/
|
|
264
|
-
readDouble() {
|
|
265
|
-
const retVal = this.buffer.readDoubleLE(this.length);
|
|
266
|
-
this.length += 8;
|
|
267
|
-
return retVal;
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* write a byte stream to the stream.
|
|
271
|
-
* The method writes the length of the byte array into the stream as a 32 bits integer before the byte stream.
|
|
272
|
-
*
|
|
273
|
-
* @param buf the buffer to write.
|
|
274
|
-
*/
|
|
275
|
-
writeByteStream(buf) {
|
|
276
|
-
if (!buf) {
|
|
277
|
-
this.writeInteger(-1);
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
(0, node_opcua_assert_1.assert)(buf instanceof Buffer);
|
|
281
|
-
this.writeInteger(buf.length);
|
|
282
|
-
// make sure there is enough room in destination buffer
|
|
283
|
-
const remainingBytes = this.buffer.length - this.length;
|
|
284
|
-
/* istanbul ignore next */
|
|
285
|
-
if (remainingBytes < buf.length) {
|
|
286
|
-
throw new Error("BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " +
|
|
287
|
-
buf.length +
|
|
288
|
-
" but only " +
|
|
289
|
-
remainingBytes +
|
|
290
|
-
" left");
|
|
291
|
-
}
|
|
292
|
-
buf.copy(this.buffer, this.length, 0, buf.length);
|
|
293
|
-
this.length += buf.length;
|
|
294
|
-
}
|
|
295
|
-
writeString(value) {
|
|
296
|
-
if (value === undefined || value === null) {
|
|
297
|
-
this.writeUInt32(0xffffffff);
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
const byteLength = calculateByteLength(value);
|
|
301
|
-
this.writeInteger(byteLength);
|
|
302
|
-
if (byteLength === 0) {
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
305
|
-
// make sure there is enough room in destination buffer
|
|
306
|
-
const remainingBytes = this.buffer.length - this.length;
|
|
307
|
-
/* istanbul ignore next */
|
|
308
|
-
if (remainingBytes < byteLength) {
|
|
309
|
-
throw new Error("BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " +
|
|
310
|
-
byteLength +
|
|
311
|
-
" but only " +
|
|
312
|
-
remainingBytes +
|
|
313
|
-
" left");
|
|
314
|
-
}
|
|
315
|
-
this.buffer.write(value, this.length);
|
|
316
|
-
this.length += byteLength;
|
|
317
|
-
}
|
|
318
|
-
// readArrayBuffer(length: number): ArrayBuffer {
|
|
319
|
-
// assert(this.length + length <= this.buffer.length, "not enough bytes in buffer");
|
|
320
|
-
// const byteArr = new Uint8Array(new ArrayBuffer(length));
|
|
321
|
-
// my_memcpy(byteArr, 0, this.buffer, this.length, this.length + length);
|
|
322
|
-
// this.length += length;
|
|
323
|
-
// return byteArr;
|
|
324
|
-
// }
|
|
325
|
-
/**
|
|
326
|
-
* @method readArrayBuffer
|
|
327
|
-
* @param length
|
|
328
|
-
*/
|
|
329
|
-
readArrayBuffer(length) {
|
|
330
|
-
// istanbul ignore next
|
|
331
|
-
if (performCheck) {
|
|
332
|
-
(0, node_opcua_assert_1.assert)(this.length + length <= this.buffer.length, "not enough bytes in buffer");
|
|
333
|
-
}
|
|
334
|
-
const slice = this.buffer.slice(this.length, this.length + length);
|
|
335
|
-
// istanbul ignore next
|
|
336
|
-
if (performCheck) {
|
|
337
|
-
(0, node_opcua_assert_1.assert)(slice.length === length);
|
|
338
|
-
}
|
|
339
|
-
const byteArr = new Uint8Array(slice);
|
|
340
|
-
// istanbul ignore next
|
|
341
|
-
if (performCheck) {
|
|
342
|
-
(0, node_opcua_assert_1.assert)(byteArr.length === length);
|
|
343
|
-
}
|
|
344
|
-
this.length += length;
|
|
345
|
-
return byteArr;
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* read a byte stream to the stream.
|
|
349
|
-
* The method reads the length of the byte array from the stream as a 32 bits integer
|
|
350
|
-
* before reading the byte stream.
|
|
351
|
-
*
|
|
352
|
-
*/
|
|
353
|
-
readByteStream() {
|
|
354
|
-
const bufLen = this.readUInt32();
|
|
355
|
-
if (bufLen === 0xffffffff) {
|
|
356
|
-
return null;
|
|
357
|
-
}
|
|
358
|
-
if (bufLen === 0) {
|
|
359
|
-
return zeroLengthBuffer;
|
|
360
|
-
}
|
|
361
|
-
// check that there is enough space in the buffer
|
|
362
|
-
const remainingBytes = this.buffer.length - this.length;
|
|
363
|
-
// istanbul ignore next
|
|
364
|
-
if (remainingBytes < bufLen) {
|
|
365
|
-
throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " +
|
|
366
|
-
bufLen +
|
|
367
|
-
" but only " +
|
|
368
|
-
remainingBytes +
|
|
369
|
-
" left");
|
|
370
|
-
}
|
|
371
|
-
// create a shared memory buffer ! for speed
|
|
372
|
-
const buf = this.buffer.slice(this.length, this.length + bufLen);
|
|
373
|
-
this.length += bufLen;
|
|
374
|
-
return buf;
|
|
375
|
-
}
|
|
376
|
-
readString() {
|
|
377
|
-
const bufLen = this.readUInt32();
|
|
378
|
-
if (bufLen === 0xffffffff) {
|
|
379
|
-
return null;
|
|
380
|
-
}
|
|
381
|
-
if (bufLen === 0) {
|
|
382
|
-
return "";
|
|
383
|
-
}
|
|
384
|
-
// check that there is enough space in the buffer
|
|
385
|
-
const remainingBytes = this.buffer.length - this.length;
|
|
386
|
-
// istanbul ignore next
|
|
387
|
-
if (remainingBytes < bufLen) {
|
|
388
|
-
throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " +
|
|
389
|
-
bufLen +
|
|
390
|
-
" but only " +
|
|
391
|
-
remainingBytes +
|
|
392
|
-
" left");
|
|
393
|
-
}
|
|
394
|
-
const str = this.buffer.toString("utf-8", this.length, this.length + bufLen);
|
|
395
|
-
this.length += bufLen;
|
|
396
|
-
return str;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
exports.BinaryStream = BinaryStream;
|
|
400
|
-
/**
|
|
401
|
-
* @function calculateByteLength
|
|
402
|
-
* calculate the size in bytes of a utf8 string
|
|
403
|
-
* @param str {String}
|
|
404
|
-
* @internal
|
|
405
|
-
*/
|
|
406
|
-
function calculateByteLength(str) {
|
|
407
|
-
// returns the byte length of an utf8 string
|
|
408
|
-
let s = str.length;
|
|
409
|
-
for (let i = s - 1; i >= 0; i--) {
|
|
410
|
-
const code = str.charCodeAt(i);
|
|
411
|
-
if (code > 0x7f && code <= 0x7ff) {
|
|
412
|
-
s++;
|
|
413
|
-
}
|
|
414
|
-
else if (code > 0x7ff && code <= 0xffff) {
|
|
415
|
-
s += 2;
|
|
416
|
-
}
|
|
417
|
-
if (code >= 0xdc00 && code <= 0xdfff) {
|
|
418
|
-
// trail surrogate
|
|
419
|
-
i--;
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
return s;
|
|
423
|
-
}
|
|
424
|
-
exports.calculateByteLength = calculateByteLength;
|
|
425
|
-
const zeroLengthBuffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(0);
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateByteLength = exports.BinaryStream = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @module node-opcua-binary-stream
|
|
6
|
+
*/
|
|
7
|
+
require("util");
|
|
8
|
+
const node_opcua_assert_1 = require("node-opcua-assert");
|
|
9
|
+
const node_opcua_buffer_utils_1 = require("node-opcua-buffer-utils");
|
|
10
|
+
const MAXUINT32 = 4294967295; // 2**32 -1;
|
|
11
|
+
const noAssert = false;
|
|
12
|
+
const performCheck = false;
|
|
13
|
+
/**
|
|
14
|
+
* a BinaryStream can be use to perform sequential read or write
|
|
15
|
+
* inside a buffer.
|
|
16
|
+
* The BinaryStream maintains a cursor up to date as the caller
|
|
17
|
+
* operates on the stream using the various read/write methods.
|
|
18
|
+
* It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
|
|
19
|
+
* It uses the [Little Endian](http://en.wikipedia.org/wiki/Little_endian#Little-endian)
|
|
20
|
+
* convention.
|
|
21
|
+
*
|
|
22
|
+
* data can either be:
|
|
23
|
+
*
|
|
24
|
+
* * a Buffer , in this case the BinaryStream operates on this Buffer
|
|
25
|
+
* * null , in this case a BinaryStream with 1024 bytes is created
|
|
26
|
+
* * any data , in this case the object is converted into a binary buffer.
|
|
27
|
+
*
|
|
28
|
+
* example:
|
|
29
|
+
*
|
|
30
|
+
* ``` javascript
|
|
31
|
+
* var stream = new BinaryStream(32)
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @class BinaryStream
|
|
35
|
+
* @param {null|Buffer|Number} data
|
|
36
|
+
* @constructor
|
|
37
|
+
*
|
|
38
|
+
*
|
|
39
|
+
*
|
|
40
|
+
*/
|
|
41
|
+
class BinaryStream {
|
|
42
|
+
constructor(data) {
|
|
43
|
+
if (data === undefined) {
|
|
44
|
+
this.buffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(1024);
|
|
45
|
+
}
|
|
46
|
+
else if (typeof data === "number") {
|
|
47
|
+
this.buffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(data);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
(0, node_opcua_assert_1.assert)(data instanceof Buffer);
|
|
51
|
+
this.buffer = data;
|
|
52
|
+
}
|
|
53
|
+
this.length = 0;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* set the cursor to the begining of the stream
|
|
57
|
+
* @method BinaryStream.rewind
|
|
58
|
+
*/
|
|
59
|
+
rewind() {
|
|
60
|
+
this.length = 0;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* write a single signed byte (8 bits) to the stream.
|
|
64
|
+
* value must be in the range of [-127,128]
|
|
65
|
+
* @param value the value to write
|
|
66
|
+
*/
|
|
67
|
+
writeInt8(value) {
|
|
68
|
+
// istanbul ignore next
|
|
69
|
+
if (performCheck) {
|
|
70
|
+
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1, "not enough space in buffer");
|
|
71
|
+
(0, node_opcua_assert_1.assert)(value >= -128 && value < 128);
|
|
72
|
+
}
|
|
73
|
+
this.buffer.writeInt8(value, this.length);
|
|
74
|
+
this.length += 1;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* write a single unsigned byte (8 bits) to the stream.
|
|
78
|
+
* @param value the value to write
|
|
79
|
+
*/
|
|
80
|
+
writeUInt8(value) {
|
|
81
|
+
// istanbul ignore next
|
|
82
|
+
if (performCheck) {
|
|
83
|
+
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1, "not enough space in buffer");
|
|
84
|
+
(0, node_opcua_assert_1.assert)(value >= 0 && value < 256, " writeUInt8 : out of bound ");
|
|
85
|
+
}
|
|
86
|
+
this.buffer.writeUInt8(value, this.length);
|
|
87
|
+
this.length += 1;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* write a single 16 bit signed integer to the stream.
|
|
91
|
+
* @param value the value to write
|
|
92
|
+
*/
|
|
93
|
+
writeInt16(value) {
|
|
94
|
+
// istanbul ignore next
|
|
95
|
+
if (performCheck) {
|
|
96
|
+
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 2, "not enough space in buffer");
|
|
97
|
+
}
|
|
98
|
+
this.buffer.writeInt16LE(value, this.length);
|
|
99
|
+
this.length += 2;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* write a single 16 bit unsigned integer to the stream.
|
|
103
|
+
* @param value the value to write
|
|
104
|
+
*/
|
|
105
|
+
writeUInt16(value) {
|
|
106
|
+
// istanbul ignore next
|
|
107
|
+
if (performCheck) {
|
|
108
|
+
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 2, "not enough space in buffer");
|
|
109
|
+
}
|
|
110
|
+
this.buffer.writeUInt16LE(value, this.length);
|
|
111
|
+
this.length += 2;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* write a single 32 bit signed integer to the stream.
|
|
115
|
+
* @param value the value to write
|
|
116
|
+
*/
|
|
117
|
+
writeInteger(value) {
|
|
118
|
+
// istanbul ignore next
|
|
119
|
+
if (performCheck) {
|
|
120
|
+
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
|
|
121
|
+
}
|
|
122
|
+
this.buffer.writeInt32LE(value, this.length);
|
|
123
|
+
this.length += 4;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* write a single 32 bit unsigned integer to the stream.
|
|
127
|
+
*
|
|
128
|
+
* @param value the value to write
|
|
129
|
+
*/
|
|
130
|
+
writeUInt32(value) {
|
|
131
|
+
// istanbul ignore next
|
|
132
|
+
if (performCheck) {
|
|
133
|
+
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
|
|
134
|
+
(0, node_opcua_assert_1.assert)(isFinite(value));
|
|
135
|
+
(0, node_opcua_assert_1.assert)(value >= 0 && value <= MAXUINT32);
|
|
136
|
+
}
|
|
137
|
+
this.buffer.writeUInt32LE(value, this.length);
|
|
138
|
+
this.length += 4;
|
|
139
|
+
/*
|
|
140
|
+
assert(this.buffer[this.length - 4] === value % 256);
|
|
141
|
+
assert(this.buffer[this.length - 3] === (value >>> 8) % 256);
|
|
142
|
+
assert(this.buffer[this.length - 2] === (value >>> 16) % 256);
|
|
143
|
+
assert(this.buffer[this.length - 1] === (value >>> 24) % 256);
|
|
144
|
+
*/
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* write a single 32 bit floating number to the stream.
|
|
148
|
+
* @param value the value to write
|
|
149
|
+
*/
|
|
150
|
+
writeFloat(value) {
|
|
151
|
+
// istanbul ignore next
|
|
152
|
+
if (performCheck) {
|
|
153
|
+
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 4, "not enough space in buffer");
|
|
154
|
+
}
|
|
155
|
+
this.buffer.writeFloatLE(value, this.length);
|
|
156
|
+
this.length += 4;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* write a single 64 bit floating number to the stream.
|
|
160
|
+
* @param value the value to write
|
|
161
|
+
*/
|
|
162
|
+
writeDouble(value) {
|
|
163
|
+
// istanbul ignore next
|
|
164
|
+
if (performCheck) {
|
|
165
|
+
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 8, "not enough space in buffer");
|
|
166
|
+
}
|
|
167
|
+
this.buffer.writeDoubleLE(value, this.length);
|
|
168
|
+
this.length += 8;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* @param arrayBuf a buffer or byte array write
|
|
172
|
+
* @param offset the offset position (default =0)
|
|
173
|
+
* @param length the number of byte to write
|
|
174
|
+
*/
|
|
175
|
+
writeArrayBuffer(arrayBuf, offset = 0, length = 0) {
|
|
176
|
+
// istanbul ignore next
|
|
177
|
+
if (performCheck) {
|
|
178
|
+
(0, node_opcua_assert_1.assert)(arrayBuf instanceof ArrayBuffer);
|
|
179
|
+
}
|
|
180
|
+
const byteArr = new Uint8Array(arrayBuf);
|
|
181
|
+
const n = (length || byteArr.length) + offset;
|
|
182
|
+
for (let i = offset; i < n; i++) {
|
|
183
|
+
this.buffer[this.length++] = byteArr[i];
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// writeArrayBuffer(arrayBuf, offset, length) {
|
|
187
|
+
// offset = offset || 0;
|
|
188
|
+
//
|
|
189
|
+
// assert(arrayBuf instanceof ArrayBuffer);
|
|
190
|
+
// const byteArr = new Uint8Array(arrayBuf);
|
|
191
|
+
// length = length || byteArr.length;
|
|
192
|
+
// if (length === 0) {
|
|
193
|
+
// return;
|
|
194
|
+
// }
|
|
195
|
+
// this.length += my_memcpy(this.buffer, this.length, byteArr, offset, offset + length);
|
|
196
|
+
// }
|
|
197
|
+
/**
|
|
198
|
+
* read a single signed byte (8 bits) from the stream.
|
|
199
|
+
* @return the value read
|
|
200
|
+
*/
|
|
201
|
+
readByte() {
|
|
202
|
+
const retVal = this.buffer.readInt8(this.length);
|
|
203
|
+
this.length += 1;
|
|
204
|
+
return retVal;
|
|
205
|
+
}
|
|
206
|
+
readInt8() {
|
|
207
|
+
return this.readByte();
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* read a single unsigned byte (8 bits) from the stream.
|
|
211
|
+
*/
|
|
212
|
+
readUInt8() {
|
|
213
|
+
// istanbul ignore next
|
|
214
|
+
if (performCheck) {
|
|
215
|
+
(0, node_opcua_assert_1.assert)(this.buffer.length >= this.length + 1);
|
|
216
|
+
}
|
|
217
|
+
const retVal = this.buffer.readUInt8(this.length);
|
|
218
|
+
this.length += 1;
|
|
219
|
+
return retVal;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* read a single signed 16-bit integer from the stream.
|
|
223
|
+
*/
|
|
224
|
+
readInt16() {
|
|
225
|
+
const retVal = this.buffer.readInt16LE(this.length);
|
|
226
|
+
this.length += 2;
|
|
227
|
+
return retVal;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* read a single unsigned 16-bit integer from the stream.
|
|
231
|
+
*/
|
|
232
|
+
readUInt16() {
|
|
233
|
+
const retVal = this.buffer.readUInt16LE(this.length);
|
|
234
|
+
this.length += 2;
|
|
235
|
+
return retVal;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* read a single signed 32-bit integer from the stream.
|
|
239
|
+
*/
|
|
240
|
+
readInteger() {
|
|
241
|
+
const retVal = this.buffer.readInt32LE(this.length);
|
|
242
|
+
this.length += 4;
|
|
243
|
+
return retVal;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* read a single unsigned 32-bit integer from the stream.
|
|
247
|
+
*/
|
|
248
|
+
readUInt32() {
|
|
249
|
+
const retVal = this.buffer.readUInt32LE(this.length);
|
|
250
|
+
this.length += 4;
|
|
251
|
+
return retVal;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* read a single 32-bit floating point number from the stream.
|
|
255
|
+
*/
|
|
256
|
+
readFloat() {
|
|
257
|
+
const retVal = this.buffer.readFloatLE(this.length);
|
|
258
|
+
this.length += 4;
|
|
259
|
+
return retVal;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* read a single 64-bit floating point number from the stream.
|
|
263
|
+
*/
|
|
264
|
+
readDouble() {
|
|
265
|
+
const retVal = this.buffer.readDoubleLE(this.length);
|
|
266
|
+
this.length += 8;
|
|
267
|
+
return retVal;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* write a byte stream to the stream.
|
|
271
|
+
* The method writes the length of the byte array into the stream as a 32 bits integer before the byte stream.
|
|
272
|
+
*
|
|
273
|
+
* @param buf the buffer to write.
|
|
274
|
+
*/
|
|
275
|
+
writeByteStream(buf) {
|
|
276
|
+
if (!buf) {
|
|
277
|
+
this.writeInteger(-1);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
(0, node_opcua_assert_1.assert)(buf instanceof Buffer);
|
|
281
|
+
this.writeInteger(buf.length);
|
|
282
|
+
// make sure there is enough room in destination buffer
|
|
283
|
+
const remainingBytes = this.buffer.length - this.length;
|
|
284
|
+
/* istanbul ignore next */
|
|
285
|
+
if (remainingBytes < buf.length) {
|
|
286
|
+
throw new Error("BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " +
|
|
287
|
+
buf.length +
|
|
288
|
+
" but only " +
|
|
289
|
+
remainingBytes +
|
|
290
|
+
" left");
|
|
291
|
+
}
|
|
292
|
+
buf.copy(this.buffer, this.length, 0, buf.length);
|
|
293
|
+
this.length += buf.length;
|
|
294
|
+
}
|
|
295
|
+
writeString(value) {
|
|
296
|
+
if (value === undefined || value === null) {
|
|
297
|
+
this.writeUInt32(0xffffffff);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const byteLength = calculateByteLength(value);
|
|
301
|
+
this.writeInteger(byteLength);
|
|
302
|
+
if (byteLength === 0) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
// make sure there is enough room in destination buffer
|
|
306
|
+
const remainingBytes = this.buffer.length - this.length;
|
|
307
|
+
/* istanbul ignore next */
|
|
308
|
+
if (remainingBytes < byteLength) {
|
|
309
|
+
throw new Error("BinaryStream.writeByteStream error : not enough bytes left in buffer : bufferLength is " +
|
|
310
|
+
byteLength +
|
|
311
|
+
" but only " +
|
|
312
|
+
remainingBytes +
|
|
313
|
+
" left");
|
|
314
|
+
}
|
|
315
|
+
this.buffer.write(value, this.length);
|
|
316
|
+
this.length += byteLength;
|
|
317
|
+
}
|
|
318
|
+
// readArrayBuffer(length: number): ArrayBuffer {
|
|
319
|
+
// assert(this.length + length <= this.buffer.length, "not enough bytes in buffer");
|
|
320
|
+
// const byteArr = new Uint8Array(new ArrayBuffer(length));
|
|
321
|
+
// my_memcpy(byteArr, 0, this.buffer, this.length, this.length + length);
|
|
322
|
+
// this.length += length;
|
|
323
|
+
// return byteArr;
|
|
324
|
+
// }
|
|
325
|
+
/**
|
|
326
|
+
* @method readArrayBuffer
|
|
327
|
+
* @param length
|
|
328
|
+
*/
|
|
329
|
+
readArrayBuffer(length) {
|
|
330
|
+
// istanbul ignore next
|
|
331
|
+
if (performCheck) {
|
|
332
|
+
(0, node_opcua_assert_1.assert)(this.length + length <= this.buffer.length, "not enough bytes in buffer");
|
|
333
|
+
}
|
|
334
|
+
const slice = this.buffer.slice(this.length, this.length + length);
|
|
335
|
+
// istanbul ignore next
|
|
336
|
+
if (performCheck) {
|
|
337
|
+
(0, node_opcua_assert_1.assert)(slice.length === length);
|
|
338
|
+
}
|
|
339
|
+
const byteArr = new Uint8Array(slice);
|
|
340
|
+
// istanbul ignore next
|
|
341
|
+
if (performCheck) {
|
|
342
|
+
(0, node_opcua_assert_1.assert)(byteArr.length === length);
|
|
343
|
+
}
|
|
344
|
+
this.length += length;
|
|
345
|
+
return byteArr;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* read a byte stream to the stream.
|
|
349
|
+
* The method reads the length of the byte array from the stream as a 32 bits integer
|
|
350
|
+
* before reading the byte stream.
|
|
351
|
+
*
|
|
352
|
+
*/
|
|
353
|
+
readByteStream() {
|
|
354
|
+
const bufLen = this.readUInt32();
|
|
355
|
+
if (bufLen === 0xffffffff) {
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
if (bufLen === 0) {
|
|
359
|
+
return zeroLengthBuffer;
|
|
360
|
+
}
|
|
361
|
+
// check that there is enough space in the buffer
|
|
362
|
+
const remainingBytes = this.buffer.length - this.length;
|
|
363
|
+
// istanbul ignore next
|
|
364
|
+
if (remainingBytes < bufLen) {
|
|
365
|
+
throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " +
|
|
366
|
+
bufLen +
|
|
367
|
+
" but only " +
|
|
368
|
+
remainingBytes +
|
|
369
|
+
" left");
|
|
370
|
+
}
|
|
371
|
+
// create a shared memory buffer ! for speed
|
|
372
|
+
const buf = this.buffer.slice(this.length, this.length + bufLen);
|
|
373
|
+
this.length += bufLen;
|
|
374
|
+
return buf;
|
|
375
|
+
}
|
|
376
|
+
readString() {
|
|
377
|
+
const bufLen = this.readUInt32();
|
|
378
|
+
if (bufLen === 0xffffffff) {
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
if (bufLen === 0) {
|
|
382
|
+
return "";
|
|
383
|
+
}
|
|
384
|
+
// check that there is enough space in the buffer
|
|
385
|
+
const remainingBytes = this.buffer.length - this.length;
|
|
386
|
+
// istanbul ignore next
|
|
387
|
+
if (remainingBytes < bufLen) {
|
|
388
|
+
throw new Error("BinaryStream.readByteStream error : not enough bytes left in buffer : bufferLength is " +
|
|
389
|
+
bufLen +
|
|
390
|
+
" but only " +
|
|
391
|
+
remainingBytes +
|
|
392
|
+
" left");
|
|
393
|
+
}
|
|
394
|
+
const str = this.buffer.toString("utf-8", this.length, this.length + bufLen);
|
|
395
|
+
this.length += bufLen;
|
|
396
|
+
return str;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
exports.BinaryStream = BinaryStream;
|
|
400
|
+
/**
|
|
401
|
+
* @function calculateByteLength
|
|
402
|
+
* calculate the size in bytes of a utf8 string
|
|
403
|
+
* @param str {String}
|
|
404
|
+
* @internal
|
|
405
|
+
*/
|
|
406
|
+
function calculateByteLength(str) {
|
|
407
|
+
// returns the byte length of an utf8 string
|
|
408
|
+
let s = str.length;
|
|
409
|
+
for (let i = s - 1; i >= 0; i--) {
|
|
410
|
+
const code = str.charCodeAt(i);
|
|
411
|
+
if (code > 0x7f && code <= 0x7ff) {
|
|
412
|
+
s++;
|
|
413
|
+
}
|
|
414
|
+
else if (code > 0x7ff && code <= 0xffff) {
|
|
415
|
+
s += 2;
|
|
416
|
+
}
|
|
417
|
+
if (code >= 0xdc00 && code <= 0xdfff) {
|
|
418
|
+
// trail surrogate
|
|
419
|
+
i--;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return s;
|
|
423
|
+
}
|
|
424
|
+
exports.calculateByteLength = calculateByteLength;
|
|
425
|
+
const zeroLengthBuffer = (0, node_opcua_buffer_utils_1.createFastUninitializedBuffer)(0);
|
|
426
426
|
//# sourceMappingURL=binaryStream.js.map
|