-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
278 lines (232 loc) · 8.17 KB
/
index.js
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
/**
* Support arbitrary lenght unsigned integer number convertion to string representation
* in the specified radix (base) in Node.js.
*
* JavaScript Numbers are IEEE-754 binary double-precision floats, which limits the
* range of values that can be represented with integer precision to:
*
* 2^53 <= N <= 2^53
*
* For more details about IEEE-754 see: http://en.wikipedia.org/wiki/IEEE_floating_point
*/
/*jslint bitwise: true */
(function () {
"use strict";
var FORMATS = {
'dec': toDecimalString,
'hex': toHexString,
'bin': toBinaryString,
'oct': toOctetString
};
module.exports = function (buffer, base, options) {
base = base || 'dec';
var buf = _toBuffer(buffer), format = FORMATS[base];
if (format) {
return format(buf, options);
}
};
function createBuffer(size) {
var buf;
if (Buffer.alloc) {
buf = Buffer.alloc(size);
} else {
buf = new Buffer(size);
buf.fill(0);
}
return buf;
}
function createBufferFrom(object) {
var buf;
if (Buffer.from) {
buf = Buffer.from(object);
} else {
buf = new Buffer(object);
}
return buf;
}
function toDecimalString(buffer, options) {
options = options || {};
var bits = buffer.length * 8, // number of bits in the buffer
lastBit = buffer.length - 1, // last bit index
digits = createBuffer(Math.floor(bits / 3 + 1 + 1)), // digits buffer
lastDigit = digits.length - 1, carry; // last digit index, digit index, carry flag
// reset digits buffer
digits.fill(0);
// reverse buffer if not in LE format
if ((options.format || 'BE') !== 'LE') {
_reverseBuffer(buffer);
}
for (var i = 0; i < bits; i++) {
carry = buffer[lastBit] >= 0x80;
_leftShift(buffer); // shift buffer bits
for (var d = lastDigit; d >= 0; d--) {
digits[d] += digits[d] + (carry ? 1 : 0);
carry = (digits[d] > 9);
if (carry) {
digits[d] -= 10;
}
}
}
// get rid of leading 0's; reuse d for the first non-zero value index
var idx = _lastHeadIndex(digits, 0);
// if there are only 0's use the last digit
idx = idx >= 0 ? idx : lastDigit;
// convert numbers to ascii digits
_toAsciiDigits(digits, idx);
return _pad(
_split(digits.toString('ascii', idx), options.groupsize, options.delimiter),
'', options.padstr, options.size);
}
function toBinaryString(buffer, options) {
options = options || {};
var digits = new Array(buffer.length),
size = options.groupsize || -1, num,
prefix = options.prefix || '',
output;
if ((options.format || 'BE') !== 'BE') {
_reverseBuffer(buffer);
}
for (var i = 0; i < buffer.length; i++) {
num = buffer[i].toString(2);
digits[i] = '00000000'.slice(0, 8 - num.length) + buffer[i].toString(2);
}
output = digits.join('');
if (options.trim) {
output = output.substr(output.indexOf('1'));
}
if (size > 0) {
output = _split(output, size, options.delimiter);
}
return prefix + _pad(output, prefix, options.padstr, options.size);
}
/*
* Converts given input (node Buffer or array of bytes) to hexadecimal string 0xDDDD where D is [0-9a-f].
* All leading 0's are stripped out i.e. [0x00, 0x00, 0x00, 0x01] -> '0x1'
*/
function toHexString(buffer, options) {
options = options || {};
var prefix = options.prefix || '', digits, idx;
if ((options.format || 'BE') !== 'BE') {
_reverseBuffer(buffer);
}
digits = buffer.toString('hex');
idx = _lastHeadIndex(digits, '0');
// if there are only 0's use the last digit
idx = idx >= 0 ? idx : digits.length - 1;
return prefix + _pad(
_split(digits.slice(idx), options.groupsize, options.delimiter), prefix, options.padstr, options.size);
}
function toOctetString(buffer, options) {
options = options || {};
var shifts = Math.floor(buffer.length * 8 / 3),
lastIdx = buffer.length - 1,
digits = createBuffer(shifts),
prefix = options.prefix || '', idx;
digits.fill(0); // reset digits buffer
if ((options.format || 'BE') !== 'BE') {
_reverseBuffer(buffer);
}
for (var i = digits.length - 1; i >= 0; i--) {
digits[i] = buffer[lastIdx] & 0x7;
// right shift buffer by 3 bits
_rightShift(buffer);
_rightShift(buffer);
_rightShift(buffer);
}
// get rid of leading 0's; reuse d for the first non-zero value index
idx = _lastHeadIndex(digits, 0);
idx = idx >= 0 ? idx : lastIdx;
// convert numbers to ascii digits
_toAsciiDigits(digits, idx);
return prefix + _pad(
_split(digits.toString('ascii', idx), options.groupsize, options.delimiter), prefix, options.padstr, options.size);
}
function _split(string, size, delim) {
if (typeof delim === 'undefined') {
delim = ' ';
}
if (typeof string !== 'undefined' && +size > 0) {
string = string.replace(new RegExp('(.)(?=(.{' + +size + '})+(?!.))', 'g'), "$1" + delim);
}
return string;
}
function _pad(str, prefix, pad, size) {
if ('undefined' === typeof pad || 'undefined' === typeof size || pad.length === 0 || str.length + prefix.length >= size) {
return str;
}
var padlen = size - str.length - prefix.length;
return new Array(Math.ceil(padlen / pad.length) + 1).join(pad).substr(0, padlen) + str;
}
function _toAsciiDigits(buffer, offset) {
for (var i = offset; i < buffer.length; i++) {
buffer[i] += 48;
}
}
/*
* Finds last head index of the given value in the given buffer
* Otherwise it returns -1.
*/
function _lastHeadIndex(buffer, value) {
for (var i = 0; i < buffer.length; i++) {
if (buffer[i] !== value) {
return i;
}
}
return -1;
}
/*
* Checks type of data and perform conversion if necessary
*/
function _toBuffer(buffer) {
var _buffer, nums;
if (Buffer.isBuffer(buffer) || Array.isArray(buffer)) {
_buffer = createBufferFrom(buffer);
}
else if (typeof buffer === 'string') {
nums = buffer.replace(/^0x/i, '').match(/.{1,2}(?=(..)+(?!.))|..?$/g);
_buffer = createBuffer(nums.length);
for (var i = nums.length - 1; i >= 0; i--) {
_buffer.writeUInt8(parseInt(nums[i], 16), i);
}
}
return _buffer;
}
/*
* Performs byte order reverse
*/
function _reverseBuffer(buffer) {
var tmp, len = buffer.length - 1, half = Math.floor(buffer.length / 2);
for (var i = len; i >= half; i--) {
tmp = buffer[i];
buffer[i] = buffer[len - i];
buffer[len - i] = tmp;
}
}
/*
* Performs buffer left bits shift
*/
function _leftShift(buffer) {
var carry;
for (var i = buffer.length; i >= 0; i--) {
carry = (buffer[i] & 0x80) !== 0;
buffer[i] = (buffer[i] << 1) & 0xFF;
if (carry && i >= 0) {
buffer[i + 1] |= 0x01;
}
}
}
/*
* Performs buffer right bits shift
*/
function _rightShift(buffer) {
var carry, prevcarry;
for (var i = 0; i < buffer.length; i++) {
carry = prevcarry;
prevcarry = (buffer[i] & 0x1) !== 0;
buffer[i] >>= 1;
if (carry && i > 0) {
buffer[i] |= 0x80;
}
}
}
}());