Skip to content

Commit

Permalink
Misc. tweaks to PR #6
Browse files Browse the repository at this point in the history
  • Loading branch information
bminer committed Apr 12, 2023
1 parent a31d0bb commit 2f42ad7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
36 changes: 23 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2f42ad7

Please sign in to comment.