Use MSB 0 in bitfields

This commit is contained in:
Kagami Hiiragi 2015-01-21 21:05:00 +03:00
parent 3078dd2365
commit 26da0b1d2f
1 changed files with 17 additions and 15 deletions

View File

@ -497,7 +497,7 @@ exports.encrypted = {
},
};
// Creates bitfield (LSB 0) class of the specified size.
// Creates bitfield (MSB 0) class of the specified size.
var Bitfield = function(size) {
var bytesize = size / 8;
@ -530,8 +530,9 @@ var Bitfield = function(size) {
return bits.every(function(bit) {
assert(bit >= 0, "Bit number is too low");
assert(bit < size, "Bit number is too high");
var index = bytesize - Math.floor(bit / 8) - 1;
return (buf[index] & (1 << (bit % 8))) !== 0; // jshint ignore:line
var index = Math.floor(bit / 8);
var shift = 7 - (bit % 8);
return (buf[index] & (1 << shift)) !== 0; // jshint ignore:line
});
};
@ -543,8 +544,9 @@ var Bitfield = function(size) {
bits.forEach(function(bit) {
assert(bit >= 0, "Bit number is too low");
assert(bit < size, "Bit number is too high");
var index = bytesize - Math.floor(bit / 8) - 1;
buf[index] |= 1 << (bit % 8); // jshint ignore:line
var index = Math.floor(bit / 8);
var shift = 7 - (bit % 8);
buf[index] |= 1 << shift; // jshint ignore:line
});
return this;
};
@ -559,9 +561,12 @@ var Bitfield = function(size) {
* @static
*/
// TODO(Kagami): Document methods.
// NOTE(Kagami): Since pubkey bitfield uses MSB 0, we use it here too.
// See <https://github.com/Bitmessage/PyBitmessage/issues/769> for
// details.
var ServicesBitfield = exports.ServicesBitfield = objectAssign(Bitfield(64), {
/** This is a normal network node. */
NODE_NETWORK: 0,
NODE_NETWORK: 63,
});
/**
@ -570,19 +575,16 @@ var ServicesBitfield = exports.ServicesBitfield = objectAssign(Bitfield(64), {
* @namespace
*/
// TODO(Kagami): Document methods.
// XXX(Kagami): PyBitmessage uses MSB 0 scheme for this bitfield so we
// invert the numberes. See
// <https://github.com/Bitmessage/PyBitmessage/issues/769> for details.
exports.PubkeyBitfield = objectAssign(Bitfield(32), {
/**
* If true, the receiving node does send acknowledgements (rather than
* dropping them).
*/
DOES_ACK: 0,
/**
* Receiving node expects that the RIPE hash encoded in their address
* preceedes the encrypted message data of msg messages bound for
* them.
*/
INCLUDE_DESTINATION: 1,
INCLUDE_DESTINATION: 30,
/**
* If true, the receiving node does send acknowledgements (rather than
* dropping them).
*/
DOES_ACK: 31,
});