From 2f42ad792a08c9f315dca3f71f4c45743e1175da Mon Sep 17 00:00:00 2001 From: Blake Miner Date: Mon, 22 Aug 2022 23:43:27 -0400 Subject: [PATCH] Misc. tweaks to PR #6 --- README.md | 6 ++++-- index.js | 36 +++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7fda350..a24044b 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,10 @@ require("intel_hex").parse(data); The `parse` function takes 3 arguments: - `data` - Intel Hex file (string in ASCII format or Buffer Object) -- `bufferSize` - the size of the Buffer containing the data (optional), the data exceeding the buffer size will be discarded -- `addressOffset` - starting address offset (optional), the data before the starting address will be discarded +- `bufferSize` - the size of the Buffer containing the data (optional) + The data exceeding the buffer size will be discarded +- `addressOffset` - starting address offset (optional) + The data before the starting address will be discarded and returns an Object with the following properties: diff --git a/index.js b/index.js index 8efc518..e694644 100644 --- a/index.js +++ b/index.js @@ -9,17 +9,21 @@ const DATA = 0, const EMPTY_VALUE = 0xFF; /* intel_hex.parse(data) - `data` - Intel Hex file (string in ASCII format or Buffer Object) - `bufferSize` - the size of the Buffer containing the data (optional) - `addressOffset` - starting address offset (optional) + - `data` - Intel Hex file (string in ASCII format or Buffer Object) + - `bufferSize` - the size of the Buffer containing the data (optional) + The data exceeding the buffer size will be discarded + - `addressOffset` - starting address offset (optional) + The data before the starting address will be discarded returns an Object with the following properties: - - data - data as a Buffer Object, padded with 0xFF - where data is empty. - - startSegmentAddress - the address provided by the last - start segment address record; null, if not given - - startLinearAddress - the address provided by the last - start linear address record; null, if not given + + - `data` - data as a Buffer Object, **padded with 0xFF + where data is empty**. + - `startSegmentAddress` - the address provided by the last + start segment address record; null, if not given + - `startLinearAddress` - the address provided by the last + start linear address record; null, if not given + Special thanks to: http://en.wikipedia.org/wiki/Intel_HEX */ exports.parse = function parseIntelHex(data, bufferSize, addressOffset) { @@ -31,12 +35,10 @@ exports.parse = function parseIntelHex(data, bufferSize, addressOffset) { highAddress = 0, //upper address startSegmentAddress = null, startLinearAddress = null, - addressOffset = 0 || addressOffset, + addressOffset = addressOffset || 0, lineNum = 0, //Line number in the Intel Hex string pos = 0; //Current position in the Intel Hex string - buf.fill(EMPTY_VALUE); - const SMALLEST_LINE = 11; while(pos + SMALLEST_LINE <= data.length) { @@ -88,7 +90,15 @@ exports.parse = function parseIntelHex(data, bufferSize, addressOffset) { buf.fill(EMPTY_VALUE, bufLength, absoluteAddress); //Write the dataFieldBuf to buf dataFieldBuf.copy(buf, absoluteAddress); - bufLength = bufferSize || Math.max(bufLength, absoluteAddress + dataLength); + bufLength = Math.max(bufLength, absoluteAddress + dataLength); + // Safely abort if the buffer length is already sufficient + if(bufLength >= bufferSize) { + return { + "data": buf.slice(0, bufLength), + "startSegmentAddress": startSegmentAddress, + "startLinearAddress": startLinearAddress + }; + } break; case END_OF_FILE: if(dataLength != 0)