-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
72 lines (60 loc) · 1.71 KB
/
utils.js
File metadata and controls
72 lines (60 loc) · 1.71 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
let Buffer = require('safe-buffer').Buffer;
let varint = require('varint');
let TrantorUtils = {};
/**
*
* @param {number} number
* @param {number} length
* @return {string}
*/
TrantorUtils.serializeNumber = function (number, length) {
let numberHex = number.toString(16);
let pairChars = numberHex.length % 2 === 0;
let neededChars;
if (length) {
neededChars = length * 2;
} else {
neededChars = pairChars ? numberHex.length : numberHex.length + 1;
}
let leadingZeros = neededChars - numberHex.length;
for (let x = 0; x < leadingZeros; x++) {
numberHex = '0' + numberHex;
}
return numberHex;
};
/**
* @param {string} text
*/
TrantorUtils.serializeText = function(text) {
if (text && text.length > 0) {
let textHex = Buffer.from(text, 'utf8').toString('hex');
let textBuffer = Buffer.from(textHex, 'hex');
return Buffer.from(varint.encode(textBuffer.length)).toString('hex') + textHex;
} else {
return Buffer.from(varint.encode(0)).toString('hex');
}
};
/**
*
* @param {Buffer} buffer
* @param {number} offset
* @return {{text: String, offset: Number|number}}
*/
TrantorUtils.deserializeText = function(buffer, offset) {
let varInt = varint.decode(buffer, offset);
offset += varint.decode.bytes;
let textHex = buffer.slice(offset, offset + varInt).toString('hex');
return {
text: Buffer.from(textHex, 'hex').toString('utf8'),
offset: varInt + varint.decode.bytes,
offset2: offset + varInt
}
};
TrantorUtils.inherit = function(child, parent) {
child.prototype = Object.create(parent.prototype);
};
if (module) {
module.exports = {
TrantorUtils: TrantorUtils
}
}