bip151: refactor parser.
This commit is contained in:
parent
01471d6e63
commit
3f6a93e180
@ -77,9 +77,6 @@ function BIP151Stream(cipher, key) {
|
||||
this.processed = 0;
|
||||
this.lastRekey = 0;
|
||||
|
||||
this.pendingHeader = [];
|
||||
this.pendingHeaderTotal = 0;
|
||||
this.hasHeader = false;
|
||||
this.pending = [];
|
||||
this.pendingTotal = 0;
|
||||
this.waiting = 0;
|
||||
@ -291,21 +288,21 @@ BIP151Stream.prototype.verify = function verify(tag) {
|
||||
BIP151Stream.prototype.feed = function feed(data) {
|
||||
var chunk, size, payload, tag, p, cmd, body;
|
||||
|
||||
while (data) {
|
||||
if (!this.hasHeader) {
|
||||
this.pendingHeaderTotal += data.length;
|
||||
this.pendingHeader.push(data);
|
||||
data = null;
|
||||
while (data.length) {
|
||||
if (!this.waiting) {
|
||||
this.pendingTotal += data.length;
|
||||
this.pending.push(data);
|
||||
|
||||
if (this.pendingHeaderTotal < 4)
|
||||
if (this.pendingTotal < 4)
|
||||
break;
|
||||
|
||||
chunk = concat(this.pendingHeader);
|
||||
chunk = concat(this.pending);
|
||||
|
||||
this.pendingHeaderTotal = 0;
|
||||
this.pendingHeader.length = 0;
|
||||
this.pendingTotal = 0;
|
||||
this.pending.length = 0;
|
||||
|
||||
size = this.decryptSize(chunk);
|
||||
data = chunk.slice(4);
|
||||
|
||||
// Allow 3 batched packets of max message size (12mb).
|
||||
// Not technically standard, but this protects us
|
||||
@ -314,23 +311,17 @@ BIP151Stream.prototype.feed = function feed(data) {
|
||||
// Note that 6 is the minimum size:
|
||||
// cmd=varint(1) string(1) length(4) data(0)
|
||||
if (size < 6 || size > constants.MAX_MESSAGE * 3) {
|
||||
assert(this.waiting === 0);
|
||||
this.emit('error', new Error('Bad packet size.'));
|
||||
continue;
|
||||
}
|
||||
|
||||
this.waiting = size + 16;
|
||||
this.hasHeader = true;
|
||||
|
||||
data = chunk.slice(4);
|
||||
|
||||
if (data.length === 0)
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
this.pendingTotal += data.length;
|
||||
this.pending.push(data);
|
||||
data = null;
|
||||
|
||||
if (this.pendingTotal < this.waiting)
|
||||
break;
|
||||
@ -340,12 +331,8 @@ BIP151Stream.prototype.feed = function feed(data) {
|
||||
tag = chunk.slice(this.waiting - 16, this.waiting);
|
||||
data = chunk.slice(this.waiting);
|
||||
|
||||
if (data.length === 0)
|
||||
data = null;
|
||||
|
||||
this.pendingTotal = 0;
|
||||
this.pending.length = 0;
|
||||
this.hasHeader = false;
|
||||
this.waiting = 0;
|
||||
|
||||
// Authenticate payload before decrypting.
|
||||
@ -355,6 +342,7 @@ BIP151Stream.prototype.feed = function feed(data) {
|
||||
this.finish();
|
||||
|
||||
if (!this.verify(tag)) {
|
||||
this.sequence();
|
||||
this.emit('error', new Error('Bad tag.'));
|
||||
continue;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user