util: perhaps a more clever way of U64 writes using bn.js.
Signed-off-by: Fedor Indutny <fedor@indutny.com>
This commit is contained in:
parent
ca2609b7e7
commit
84b53a049d
@ -180,23 +180,12 @@ utils.writeU64 = function writeU64(dst, num, off) {
|
||||
if (!off)
|
||||
off = 0;
|
||||
|
||||
var num = new bn(num);
|
||||
num = new bn(num).maskn(64).toArray();
|
||||
while (num.length < 8) num.unshift(0);
|
||||
|
||||
var left = num.shrn(32).toArray();
|
||||
while (left.length < 4) left.unshift(0);
|
||||
|
||||
var right = num.maskn(32).toArray();
|
||||
while (right.length < 4) right.unshift(0);
|
||||
|
||||
dst[off] = right[3];
|
||||
dst[off + 1] = right[2];
|
||||
dst[off + 2] = right[1];
|
||||
dst[off + 3] = right[0];
|
||||
|
||||
dst[off + 4] = left[3];
|
||||
dst[off + 5] = left[2];
|
||||
dst[off + 6] = left[1];
|
||||
dst[off + 7] = left[0];
|
||||
num.reverse().forEach(function(ch) {
|
||||
dst[off++] = ch;
|
||||
});
|
||||
|
||||
return 8;
|
||||
};
|
||||
@ -223,23 +212,12 @@ utils.writeU64BE = function writeU64BE(dst, num, off) {
|
||||
if (!off)
|
||||
off = 0;
|
||||
|
||||
var num = new bn(num);
|
||||
num = new bn(num).maskn(64).toArray();
|
||||
while (num.length < 8) num.unshift(0);
|
||||
|
||||
var left = num.shrn(32).toArray();
|
||||
while (left.length < 4) left.unshift(0);
|
||||
|
||||
var right = num.maskn(32).toArray();
|
||||
while (right.length < 4) right.unshift(0);
|
||||
|
||||
dst[off] = left[0];
|
||||
dst[off + 1] = left[1];
|
||||
dst[off + 2] = left[2];
|
||||
dst[off + 3] = left[3];
|
||||
|
||||
dst[off + 4] = right[0];
|
||||
dst[off + 5] = right[1];
|
||||
dst[off + 6] = right[2];
|
||||
dst[off + 7] = right[3];
|
||||
num.forEach(function(ch) {
|
||||
dst[off++] = ch;
|
||||
});
|
||||
|
||||
return 8;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user