This repository was archived by the owner on Nov 15, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMsgBuffer.js
More file actions
109 lines (93 loc) · 3.31 KB
/
Copy pathMsgBuffer.js
File metadata and controls
109 lines (93 loc) · 3.31 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
module.exports = function(dataBuffer) {
this.position = 0;
this.CompleteLength = 0;
var buffer = dataBuffer;
var storedBuffers = [];
//console.log('[MsgBuffer::__construct][Info]: buffer.length = ' + buffer.length + '.');
if(!(dataBuffer instanceof Buffer) && dataBuffer instanceof Array)
buffer = new Buffer(buffer);
else if(!dataBuffer)
buffer = new Buffer(0);
this.verifyNextByte = function(byte) {
var bool = (buffer[this.position] === byte);
this.position++;
var message = (bool) ? '[MsgBuffer::verifyNextByte][Info]: Byte verified - ' + byte + ' - good.' : '[MsgBuffer::verifyNextByte][Info]: Byte verified - ' + byte + ' - bad.';
//console.log(message);
return bool;
}
this.loadBytes = function() {
buffer = (storedBuffers.length > 0) ? Buffer.concat(storedBuffers) : buffer;
//console.log('[MsgBuffer::loadBytes][Info]: Loaded (buffer.length) ' + buffer.length + ' bytes.');
}
this.storeBuffer = function(bufferToBeStored) {
if(storedBuffers.length == 0)
storedBuffers.push(buffer);
storedBuffers.push(bufferToBeStored);
}
this.skipBytes = function(count) {
this.position += count;
}
this.getUint16 = function(bigEndian) {
//console.log('[MsgBuffer::getUint16][Info]: MsgBuffer.position = ' + this.position + ' against buffer.length = ' + buffer.length + '.');
if(this.position == buffer.length)
//console.log('[MsgBuffer::getUint16][Info]: MsgBuffer.position = ' + this.position + ' is equal to buffer.length = ' + buffer.length + '. Crash coming soon...');
return false;
else {
var uint16 = (bigEndian) ? buffer.readUInt16BE(this.position) : buffer.readUInt16LE(this.position);
this.position += 2;
return uint16;
}
}
this.getInt32 = function(bigEndian) {
var int32 = (bigEndian) ? buffer.readInt32BE(this.position) : buffer.readInt32LE(this.position);
this.position += 4;
return int32;
}
this.getString = function(byteLength, encoding) {
byteLength = (byteLength >= 0) ? byteLength : buffer.length - this.position;
var string = buffer.toString(encoding, this.position, (this.position + byteLength));
this.position += byteLength;
return string;
}
this.getBuffer = function(putLength) {
if(putLength) {
var lengthBuffer = new Buffer(2);
lengthBuffer.writeUInt16LE(buffer.length, 0);
buffer = Buffer.concat([lengthBuffer, buffer]);
}
return buffer;
}
this.putString = function(string, encoding, addLength) {
encoding = typeof encoding !== 'undefined' ? encoding : 'ASCII';
addLength = typeof addLength !== 'undefined' ? addLength : 'false';
if(addLength == true)
this.putUint16(string.length);
var stringBuffer = new Buffer(string, encoding);
buffer = Buffer.concat([buffer, stringBuffer]);
}
this.putUint16 = function(value, bigEndian) {
var uintBuffer = new Buffer(2);
if(bigEndian)
uintBuffer.writeUInt16BE(value, 0);
else
uintBuffer.writeUInt16LE(value, 0);
buffer = Buffer.concat([buffer, uintBuffer]);
}
this.putInt32 = function(value, bigEndian) {
intBuffer = new Buffer(4);
if(bigEndian)
intBuffer.writeInt32BE(value, 0);
else
intBuffer.writeInt32LE(value, 0);
buffer = Buffer.concat([buffer, intBuffer]);
}
this.getLength = function() {
return buffer.length;
}
this.getStoredBuffersLength = function() {
return storedBuffers.length;
}
this.resetPosition = function() {
this.position = 0;
}
}