util: simplify 64bit writes using bn.js.

Signed-off-by: Fedor Indutny <fedor@indutny.com>
This commit is contained in:
Christopher Jeffrey 2014-05-18 10:24:10 -05:00 committed by Fedor Indutny
parent 5ee3fbcf72
commit d1853b2e52

View File

@ -180,19 +180,9 @@ utils.writeU64 = function writeU64(dst, num, off) {
if (!off)
off = 0;
var num = new bn(num);
var left = +num.shrn(32).toString(10);
var right = +num.maskn(32).toString(10);
dst[off] = right & 0xff;
dst[off + 1] = (right >>> 8) & 0xff;
dst[off + 2] = (right >>> 16) & 0xff;
dst[off + 3] = (right >>> 24) & 0xff;
dst[off + 4] = left & 0xff;
dst[off + 5] = (left >>> 8) & 0xff;
dst[off + 6] = (left >>> 16) & 0xff;
dst[off + 7] = (left >>> 24) & 0xff;
new bn(num).toArray().reverse().forEach(function(ch, i) {
dst[off + i] = ch;
});
return 8;
};
@ -219,19 +209,9 @@ utils.writeU64BE = function writeU64BE(dst, num, off) {
if (!off)
off = 0;
var num = new bn(num);
var left = +num.shrn(32).toString(10);
var right = +num.maskn(32).toString(10);
dst[off] = (left >>> 24) & 0xff;
dst[off + 1] = (left >>> 16) & 0xff;
dst[off + 2] = (left >>> 8) & 0xff;
dst[off + 3] = left & 0xff;
dst[off + 4] = (right >>> 24) & 0xff;
dst[off + 5] = (right >>> 16) & 0xff;
dst[off + 6] = (right >>> 8) & 0xff;
dst[off + 7] = right & 0xff;
new bn(num).toArray().forEach(function(ch, i) {
dst[off + i] = ch;
});
return 8;
};