Skip to content

Commit c072452

Browse files
universal-hex: Add isUniversalHex() function.
1 parent 34cb8ef commit c072452

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

src/__tests__/universal-hex.spec.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,84 @@ describe('Test createUniversalHex()', () => {
405405
*/
406406
});
407407

408+
describe('Test isUniversalHex()', () => {
409+
it('Detects a Universal Intel Hex.', () => {
410+
const normalHex =
411+
':020000040000FA\n' +
412+
':0400000A9900C0DEBB\n' +
413+
':1000000000400020218E01005D8E01005F8E010006\n' +
414+
':1000100000000000000000000000000000000000E0\n' +
415+
':10002000000000000000000000000000618E0100E0\n' +
416+
':100030000000000000000000638E0100658E0100DA\n' +
417+
':10004000678E01005D3D000065950100678E01002F\n' +
418+
':10005000678E010000000000218F0100678E010003\n' +
419+
':1000600069E80000D59A0100D9930100678E01006C\n' +
420+
':10007000678E0100678E0100678E0100678E0100A8\n' +
421+
':10008000678E0100678E0100678E0100678E010098\n' +
422+
':10009000678E01000D8A0100D98A0100A5E90000E0\n' +
423+
':0C00000BFFFFFFFFFFFFFFFFFFFFFFFFF5\n' +
424+
':00000001FF\n';
425+
426+
const result = uh.isUniversalHex(normalHex);
427+
428+
expect(result).toBeTruthy();
429+
});
430+
431+
it('Detects a Universal Intel Hex with Windows line endings.', () => {
432+
const normalHex =
433+
':020000040000FA\r\n' +
434+
':0400000A9900C0DEBB\r\n' +
435+
':1000000000400020218E01005D8E01005F8E010006\r\n' +
436+
':1000100000000000000000000000000000000000E0\r\n' +
437+
':10002000000000000000000000000000618E0100E0\r\n' +
438+
':100030000000000000000000638E0100658E0100DA\r\n' +
439+
':10004000678E01005D3D000065950100678E01002F\r\n' +
440+
':10005000678E010000000000218F0100678E010003\r\n' +
441+
':1000600069E80000D59A0100D9930100678E01006C\r\n' +
442+
':10007000678E0100678E0100678E0100678E0100A8\r\n' +
443+
':10008000678E0100678E0100678E0100678E010098\r\n' +
444+
':10009000678E01000D8A0100D98A0100A5E90000E0\r\n' +
445+
':0C00000BFFFFFFFFFFFFFFFFFFFFFFFFF5\r\n' +
446+
':00000001FF\r\n';
447+
448+
const result = uh.isUniversalHex(normalHex);
449+
450+
expect(result).toBeTruthy();
451+
});
452+
453+
it('Detects an empty string as false', () => {
454+
expect(uh.isUniversalHex('')).toBeFalsy();
455+
});
456+
457+
it('Detects a normal Intel Hex as false.', () => {
458+
const normalHex =
459+
':020000040000FA\n' +
460+
':10558000002EEDD1E9E70020EAE7C0464302F0B57E\n' +
461+
':1055900042005D0AC30F4802440A4800120E000E82\n' +
462+
':00000001FF\n';
463+
464+
const result = uh.isUniversalHex(normalHex);
465+
466+
expect(result).toBeFalsy();
467+
});
468+
469+
it('Detects a random string as false.', () => {
470+
const normalHex = 'This is just a random string';
471+
472+
const result = uh.isUniversalHex(normalHex);
473+
474+
expect(result).toBeFalsy();
475+
});
476+
477+
it('Returns false when failing to find the second record.', () => {
478+
const normalHex = ':02000004\nThis is just a random string, nor a record.';
479+
480+
const result = uh.isUniversalHex(normalHex);
481+
482+
expect(result).toBeFalsy();
483+
});
484+
});
485+
408486
describe('Separate a Universal Hex', () => {
409487
it('Throws an error on empty input', () => {
410488
expect(() => {

src/universal-hex.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,43 @@ function createUniversalHex(hexes: IndividualHex[]): string {
150150
return customHexes.join('');
151151
}
152152

153+
/**
154+
* Checks if the provided hex string is a fat binary.
155+
*
156+
* Very simple test only checking for the opening Extended Linear Address and
157+
* Block Start records.
158+
*
159+
* The string is manually checked as this method can be x20 faster than breaking
160+
* the string into records and checking their types with the ihex functions.
161+
*
162+
* @param hexStr Hex string to check
163+
* @return True if the hex is an Universal Hex.
164+
*/
165+
function isUniversalHex(hexStr: string): boolean {
166+
// Check the beginning of the Extended Linear Address record
167+
const startOfElaRecord = ':02000004';
168+
if (hexStr.slice(0, startOfElaRecord.length) !== startOfElaRecord) {
169+
return false;
170+
}
171+
// Find the index for the next record, as we have unknown line endings
172+
let i = startOfElaRecord.length;
173+
while (hexStr[++i] !== ':' && i < 50);
174+
// Check the beginning of the Block Start record
175+
const startOfBsRecord = ':0400000A';
176+
if (hexStr.slice(i, i + startOfBsRecord.length) !== startOfBsRecord) {
177+
return false;
178+
}
179+
return true;
180+
}
181+
153182
/**
154183
* Separates a Universal Hex into the individual hexes.
155184
*
156-
* @param intelHexStr Intel Hex string with the Universal Hex.
185+
* @param universalHexStr Universal Hex string with the Universal Hex.
157186
* @returns An array of object with boardId and hex keys.
158187
*/
159-
function separateUniversalHex(intelHexStr: string): IndividualHex[] {
160-
const records = ihex.iHexToRecordStrs(intelHexStr);
188+
function separateUniversalHex(universalHexStr: string): IndividualHex[] {
189+
const records = ihex.iHexToRecordStrs(universalHexStr);
161190
if (!records.length) throw new Error('Empty Universal Hex.');
162191

163192
// The format has to start with an Extended Linear Address and Block Start
@@ -237,4 +266,9 @@ function separateUniversalHex(intelHexStr: string): IndividualHex[] {
237266
return returnArray;
238267
}
239268

240-
export { iHexToCustomFormat, createUniversalHex, separateUniversalHex };
269+
export {
270+
iHexToCustomFormat,
271+
createUniversalHex,
272+
isUniversalHex,
273+
separateUniversalHex,
274+
};

0 commit comments

Comments
 (0)