From 556361e9708741072b8ae26f52536aec991e1d5d Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Tue, 27 May 2014 10:59:34 -0300 Subject: [PATCH] Fixed BlockExtractor --- lib/BlockExtractor.js | 48 ++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/BlockExtractor.js b/lib/BlockExtractor.js index 2c8a6aa2..2e92bd3a 100644 --- a/lib/BlockExtractor.js +++ b/lib/BlockExtractor.js @@ -97,29 +97,40 @@ BlockExtractor.prototype.getNextBlock = function(cb) { async.whilst( function() { - return (!magic); + return (!magic || magic === '00000000'); }, function(w_cb) { + magic = null; self.readCurrentFileSync(); - if (self.currentFileIndex < 0) return cb(); + var byte0 = self.currentParser ? self.currentParser.buffer(1).toString('hex') : null; - magic = self.currentParser ? self.currentParser.buffer(4).toString('hex') - : null ; + if (byte0) { + // Grab 3 bytes from block without removing them + var p = self.currentParser.pos; + var bytes123 = self.currentParser.subject.toString('hex',p,p+3); + magic = byte0 + bytes123; - if (!self.currentParser || self.currentParser.eof() || magic === '00000000') { - magic = null; - if (self.nextFile()) { + if (magic !=='00000000' && magic !== self.magic) { + + if (self.errorCount++ > 4) + return cb(new Error('CRITICAL ERROR: Magic number mismatch: ' + + magic + '!=' + self.magic)); + + magic=null; + } + } + + if (!self.currentParser || self.currentParser.eof() ) { + if (self.nextFile()) console.log('Moving forward to file:' + self.currentFile() ); - return w_cb(); - } - else { + else console.log('Finished all files'); - magic = null; - return w_cb(); - } + + magic = null; + return w_cb(); } else { return w_cb(); @@ -128,14 +139,8 @@ BlockExtractor.prototype.getNextBlock = function(cb) { }, function (a_cb) { if (!magic) return a_cb(); - if (magic !== self.magic) { - var e = new Error('CRITICAL ERROR: Magic number mismatch: ' + - magic + '!=' + self.magic); - return a_cb(e); - } - - // spacer? - self.currentParser.word32le(); + // Remove 3 bytes from magic and spacer + self.currentParser.buffer(3+4); return a_cb(); }, function (a_cb) { @@ -144,6 +149,7 @@ BlockExtractor.prototype.getNextBlock = function(cb) { b = new Block(); b.parse(self.currentParser); b.getHash(); + self.errorCount=0; return a_cb(); }, ], function(err) {