@connectedyard/node-intelhex
Advanced tools
Comparing version
20
index.js
( function(){ | ||
var fs = require('fs'); | ||
var Promise = require('bluebird'); | ||
const fs = require('fs'); | ||
const Promise = require('bluebird'); | ||
const _ = require('lodash'); | ||
@@ -30,5 +31,2 @@ exports.BufferToIntelHexProcessor = require('./lib/IntelHexFileOut'); | ||
* startAtOffsetZero: output binary file begin at address zero, even when min data address in intelFile is > 0 | ||
* maxBinaryAddress: maximum binary address, defines size of buffer required in RAM | ||
* verbose: logs processing to console | ||
* littleEndian: display data as little endian | ||
* | ||
@@ -39,5 +37,13 @@ * @returns binary file data | ||
var processor = new exports.IntelToBufferProcessor( options ); | ||
processor.process( intelHexString ); | ||
var blocks = processor.getBlocks(); | ||
return processor.contents; | ||
if( options.selectBlocks ){ | ||
blocks = _.filter( blocks, function( block ){ | ||
return options.selectBlocks.indexOf( block.startAddress ) !== -1; | ||
}) | ||
} | ||
return processor.assembleBlocks( blocks ); | ||
}; | ||
@@ -81,4 +87,2 @@ | ||
* startAtOffsetZero: output binary file begin at address zero, even when min data address in intelFile is > 0 | ||
* maxBinaryAddress: maximum binary address, defines size of buffer required in RAM | ||
* verbose: logs processing to console | ||
* | ||
@@ -85,0 +89,0 @@ * @returns Promise, returning true |
( function(){ | ||
const utils = require('./utils'); | ||
const _ = require('lodash'); | ||
const BinaryBlock = require('./BinaryBlock'); | ||
@@ -38,6 +38,3 @@ /* | ||
function Parser(){ | ||
} | ||
Parser.prototype.parse = function( hexLine ){ | ||
function parseIntelHexLine( hexLine ){ | ||
if( hexLine.length === 0 ) return {}; | ||
@@ -71,3 +68,3 @@ | ||
return output; | ||
}; | ||
} | ||
@@ -81,88 +78,46 @@ /** | ||
this.options = _.extend({ | ||
maxBinaryAddress : 512 * 1024 * 1024, | ||
littleEndian: false, | ||
verbose: false | ||
}, options ); | ||
this.parser = new Parser(); | ||
this.contents = false; | ||
this.verboseStats = { | ||
blockStartAddress: 0, | ||
blockStartBytes: null, | ||
blockNumBytes: 0, | ||
totalBytes: 0 | ||
} | ||
this.blocks = []; | ||
} | ||
Processor.prototype._setExplainBlockStartAddress = function( address ){ | ||
if( this.verboseStats.blockNumBytes > 0 ){ | ||
this._endExplainBlock(); | ||
} | ||
this.verboseStats.blockNumBytes = 0; | ||
this.verboseStats.blockStartAddress = address; | ||
Processor.prototype._cullEmptyBlocks = function(){ | ||
const blocks = []; | ||
_.each( this.blocks, function( block ){ | ||
if( block.data.length !== 0 ){ | ||
blocks.push( block ); | ||
} | ||
}); | ||
this.blocks = blocks; | ||
}; | ||
Processor.prototype._startExplainBlock = function( buffer, address ){ | ||
this.verboseStats.blockStartAddress = address; | ||
this.verboseStats.blockStartBytes = new Buffer( buffer ); | ||
this.verboseStats.blockNumBytes = buffer.length; | ||
console.log(_.padEnd( "Block start at:", 20) + "0x" + utils.numberToHexString( address, 8 )); | ||
console.log(_.padEnd( "Block bytes begin:", 20) + _hexDumpWords( this.verboseStats.blockStartBytes, this.options.littleEndian )); | ||
}; | ||
Processor.prototype._continueExplainBlock = function( buffer, address ){ | ||
this.verboseStats.totalBytes += buffer.length; | ||
if( address === this.verboseStats.blockStartAddress + this.verboseStats.blockNumBytes ){ | ||
this.verboseStats.blockNumBytes += buffer.length; | ||
return true; | ||
Processor.prototype._processSetAddress = function( address ){ | ||
const lastBlock = _.last( this.blocks ); | ||
if( !lastBlock || lastBlock.endAddress() !== address ){ | ||
const nextBlock = new BinaryBlock( address ); | ||
this.blocks.push( nextBlock ); | ||
} | ||
return false; | ||
}; | ||
Processor.prototype._endExplainBlock = function(){ | ||
if( this.verboseStats.blockNumBytes > 0 ){ | ||
console.log( _.padEnd( "Block length:", 20) + "0x" + utils.numberToHexString( this.verboseStats.blockNumBytes, 8 ) + " (" + this.verboseStats.blockNumBytes + ")\n" ); | ||
Processor.prototype._processLineBuffer = function( buffer, address ){ | ||
const lastBlock = _.last( this.blocks ); | ||
if( lastBlock && address === lastBlock.endAddress() ){ | ||
lastBlock.appendBuffer( buffer ); | ||
}else { | ||
const nextBlock = new BinaryBlock( address ); | ||
nextBlock.appendBuffer( buffer ); | ||
this.blocks.push( nextBlock ); | ||
} | ||
}; | ||
Processor.prototype.explainAddBytesAtAddress = function( buffer, address ){ | ||
if( this.options.verbose ){ | ||
if( !buffer ){ | ||
this._setExplainBlockStartAddress( address ); | ||
}else{ | ||
var continueCurrentBlock = this. _continueExplainBlock( buffer, address ); | ||
if( !continueCurrentBlock ){ | ||
this._endExplainBlock(); | ||
this._startExplainBlock( buffer, address ); | ||
} | ||
} | ||
} | ||
}; | ||
Processor.prototype.explainEOF = function(){ | ||
if( this.options.verbose ){ | ||
if( this.verboseStats.blockNumBytes !== 0 ){ | ||
this._endExplainBlock(); | ||
} | ||
console.log( _.padEnd( "EOF. Total Bytes:", 20) + "0x" + utils.numberToHexString( this.verboseStats.totalBytes, 8 ) + " (" + this.verboseStats.totalBytes + ")\n" ); | ||
} | ||
}; | ||
Processor.prototype.process = function( hexFileData ){ | ||
if( !hexFileData ) return new Buffer(0); | ||
if( !hexFileData ) return new Buffer( 0 ); | ||
var maximumAddress = 0; | ||
var minimumAddress = 0xFFFFFFFF; | ||
var hexLines = hexFileData.split('\n'); | ||
var buf = new Buffer( this.options.maxBinaryAddress ); | ||
var hexLines = hexFileData.split( '\n' ); | ||
var baseAddress = 0; | ||
for( var i=0; i < hexLines.length; i++){ | ||
for( var i = 0; i < hexLines.length; i++ ){ | ||
var hexLine = hexLines[i]; | ||
var parsedLine = this.parser.parse( hexLine ); | ||
var parsedLine = parseIntelHexLine( hexLine ); | ||
@@ -172,32 +127,45 @@ if( parsedLine.recordType === RECORD_TYPE_ADDRESS ){ | ||
baseAddress = parsedLine.baseAddress << 16; | ||
this.explainAddBytesAtAddress( null, baseAddress ); | ||
this._processSetAddress( baseAddress ); | ||
} else if ( parsedLine.recordType === RECORD_TYPE_DATA ){ | ||
}else if( parsedLine.recordType === RECORD_TYPE_DATA ){ | ||
var lineAddress = parsedLine.address + baseAddress; | ||
var linePayload = parsedLine.payload; | ||
this.explainAddBytesAtAddress( parsedLine.payload, lineAddress ); | ||
maximumAddress = Math.max( (lineAddress + linePayload.length), maximumAddress ); | ||
minimumAddress = Math.min( lineAddress, minimumAddress ); | ||
if( maximumAddress > buf.length ){ | ||
console.log( "Buffer Overflow at address 0x" + utils.numberToHexString( lineAddress )); | ||
} else { | ||
linePayload.copy( buf, lineAddress ); | ||
} | ||
this._processLineBuffer( linePayload, lineAddress ); | ||
} | ||
this._cullEmptyBlocks(); | ||
} | ||
}; | ||
var startAddress = ( this.options.startAtAddressZero ) ? 0 : minimumAddress; | ||
this.contents = buf.slice( startAddress, maximumAddress ); | ||
Processor.prototype.getBlocks = function(){ | ||
return this.blocks; | ||
}; | ||
this.explainEOF(); | ||
Processor.prototype.assembleBlocks = function( blocks ){ | ||
if( !blocks || blocks.length === 0 ) return new Buffer(0); | ||
var maximumAddress = 0; | ||
var minimumAddress = 0xFFFFFFFF; | ||
_.each( blocks, function( block ){ | ||
if( block.startAddress < minimumAddress ) minimumAddress = block.startAddress; | ||
if( block.endAddress() > maximumAddress ) maximumAddress = block.endAddress(); | ||
}); | ||
const startAddress = ( this.options.startAtAddressZero ) ? 0 : minimumAddress; | ||
const buffer = new Buffer( maximumAddress - startAddress ); | ||
buffer.fill(0); | ||
_.each( blocks, function( block ){ | ||
block.data.copy( buffer, block.startAddress - startAddress); | ||
}); | ||
return buffer; | ||
}; | ||
function _hexDumpWords( buffer, littleEndian ){ | ||
return utils.hexStringByWords( buffer.toString('hex'), littleEndian ); | ||
} | ||
Processor.prototype.getContents = function(){ | ||
return this.assembleBlocks( this.blocks ); | ||
}; | ||
@@ -204,0 +172,0 @@ module.exports = Processor; |
{ | ||
"name": "@connectedyard/node-intelhex", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Building and converting between intel-hex and binary data", | ||
@@ -5,0 +5,0 @@ "bin": { |
@@ -19,3 +19,3 @@ ( function(){ | ||
processor.process( intelHexFileData ); | ||
var binaryResults = processor.contents; | ||
var binaryResults = processor.getContents(); | ||
@@ -22,0 +22,0 @@ expect( binaryResults ).toEqual( binaryFileData ); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
135002
110.22%37
8.82%694
-1.84%