import {HexMemoryMap} from 'ECB';

let memMap1 = new HexMemoryMap();
let memMap2 = new HexMemoryMap([[0, new Buffer(1,2,3,4)]]);
let memMap3 = new HexMemoryMap({0: new Buffer(1,2,3,4)});
let memMap4 = new HexMemoryMap({0xCF0: new Buffer(1,2,3,4)});

const block = HexMemoryMap.fromHex(hexText);

Constructors

  • Parameters

    • Optionalblocks: null | Iterable<[number, Buffer]> | { [key: string]: Buffer }

      The initial value for the memory blocks inside this HexMemoryMap. All keys must be numeric, and all values must be instances of Buffer. Optionally it can also be a plain Object with only numeric keys.

    Returns HexMemoryMap

Accessors

  • get size(): number
  • Returns number

Methods

  • Returns IterableIterator<[number, Buffer]>

  • Returns a String of text representing a .hex file.
    The writer has an opinionated behaviour. Check the project's file for details.

    Parameters

    • OptionallineSize: number = 16

      Maximum number of bytes to be encoded in each data record. Must have a value between 1 and 255, as per the specification.

    Returns string

    String of text with the .hex representation of the input binary data

    import {HexMemoryMap} from 'ECB';

    let memMap = new HexMemoryMap();
    let bytes = new Buffer(....);
    memMap.set(0x0FF80000, bytes); // The block with 'bytes' will start at offset 0x0FF80000

    let string = memMap.asHexString();
  • Returns void

  • Performs a deep copy of the current HexMemoryMap, returning a new one with exactly the same contents, but allocating new memory for each of its Buffers.

    Returns HexMemoryMap

  • Checks whether the current memory map contains the one given as a parameter.


    "Contains" means that all the offsets that have a byte value in the given memory map have a value in the current memory map, and that the byte values are the same.
    An empty memory map is always contained in any other memory map.
    Returns boolean true if the memory map is contained, false otherwise.

    Parameters

    Returns boolean

  • Parameters

    • addr: number

    Returns boolean

  • Returns IterableIterator<[number, Buffer]>

  • Parameters

    • callback: (value: Buffer, key: number, map: Map<number, Buffer>) => void
    • OptionalthisArg: any

    Returns void

  • Parameters

    • addr: number

    Returns undefined | Buffer

  • Locates the Buffer which contains the given offset, and returns the four bytes held at that offset, as a 32-bit unsigned integer.


    Behaviour is similar to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint32|DataView.prototype.getUint32, except that this operates over a HexMemoryMap instead of over an ArrayBuffer, and that this may return undefined if the address is not entirely contained within one of the Buffers.

    Parameters

    • offset: number

      The memory offset to read the data

    • OptionallittleEndian: boolean

      Whether to fetch the 4 bytes as a little- or big-endian integer

    Returns undefined | number

    An unsigned 32-bit integer number

  • Parameters

    • addr: number

    Returns boolean

  • Returns a new instance of HexMemoryMap, containing the same data, but concatenating together those memory blocks that are adjacent.
    The insertion order of keys in the HexMemoryMap is guaranteed to be strictly ascending. In other words, when iterating through the HexMemoryMap, the addresses will be ordered in ascending order.
    If maxBlockSize is given, blocks will be concatenated together only until the joined block reaches this size in bytes. This means that the output HexMemoryMap might have more entries than the input one.
    If there is any overlap between blocks, an error will be thrown.
    The returned HexMemoryMap will use newly allocated memory.

    Parameters

    • OptionalmaxBlockSize: number = Infinity

      Maximum size of the Buffers in the returned HexMemoryMap.

    Returns HexMemoryMap

  • Returns IterableIterator<number>

  • Returns a new instance of HexMemoryMap, where:

    • Each key (the start address of each Buffer) is a multiple of pageSize
    • The size of each Buffer is exactly pageSize
    • Bytes from the input map to bytes in the output
    • Bytes not in the input are replaced by a padding value

    The scenario is wanting to prepare pages of bytes for a write operation, where the write operation affects a whole page/sector at once.
    The insertion order of keys in the output HexMemoryMap is guaranteed to be strictly ascending. In other words, when iterating through the HexMemoryMap, the addresses will be ordered in ascending order.
    The Buffers in the output will be newly allocated.

    Parameters

    • OptionalpageSize: number = 1024

      The size of the output pages, in bytes

    • Optionalpad: number = 0xff

      The byte value to use for padding

    Returns HexMemoryMap

  • Parameters

    • addr: number
    • value: Buffer

    Returns Map<number, Buffer>

  • Returns a new instance of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint32|Buffer, containing only data between the addresses address and address + length. Any byte without a value in the input HexMemoryMap will have a value of padByte.


    This method allocates new memory.

    Parameters

    • address: number

      The start address of the slice

    • length: number

      The length of memory map to slice out

    • OptionalpadByte: number = 0xff

      The value of the byte assumed to be used as padding

    Returns Buffer

  • Returns IterableIterator<Buffer>

  • Given the output of the overlapHexMemoryMaps (a Map of address to an Array of (id, Buffer) tuples), returns a HexMemoryMap. This discards the IDs in the process.
    The output Map contains as many entries as the input one (using the same addresses as keys), but the value for each entry will be the Buffer of the last tuple for each address in the input data.
    The scenario is wanting to join together several parsed .hex files, not worrying about their overlaps.

    Parameters

    • overlaps: Map<number, [string, Buffer][]>

      The (possibly overlapping) input memory blocks

    Returns HexMemoryMap

    The flattened memory blocks

  • Parses a string containing data formatted in "Intel HEX" format, and returns an instance of HexMemoryMap.
    The insertion order of keys in the HexMemoryMap is guaranteed to be strictly ascending. In other words, when iterating through the HexMemoryMap, the addresses will be ordered in ascending order.
    The parser has an opinionated behaviour, and will throw a descriptive error if it encounters some malformed input. Check the project's file for details.
    If maxBlockSize is given, any contiguous data block larger than that will be split in several blocks.

    Parameters

    • hexText: string

      The contents of a .hex file.

    • OptionalmaxBlockSize: number = Infinity

      Maximum size of the returned Buffers.

    Returns HexMemoryMap

    import {HexMemoryMap} from 'ECB';

    let intelHexString =
    ":100000000102030405060708090A0B0C0D0E0F1068\n" +
    ":00000001FF";

    let memMap = HexMemoryMap.fromHex(intelHexString);

    for (let [address, dataBlock] of memMap) {
    console.log('Data block at ', address, ', bytes: ', dataBlock);
    }
  • Given one Buffer, looks through its contents and returns a new HexMemoryMap, stripping away those regions where there are only padding bytes.
    The start of the input Buffer is assumed to be offset zero for the output.
    The use case here is dumping memory from a working device and try to see the "interesting" memory regions it has. This assumes that there is a constant, predefined padding byte value being used in the "non-interesting" regions. In other words: this will work as long as the dump comes from a flash memory which has been previously erased (thus 0xFFs for padding), or from a previously blanked HDD (thus 0x00s for padding).
    This method uses subarray on the input data, and thus does not allocate memory for the Buffers.

    Parameters

    • bytes: Buffer

      The input data

    • OptionalpadByte: number = 0xff

      The value of the byte assumed to be used as padding

    • OptionalminPadLength: number = 64

      The minimum number of consecutive pad bytes to be considered actual padding

    Returns HexMemoryMap

  • Given a https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map|Map of HexMemoryMaps, indexed by a alphanumeric ID, returns a Map of address to tuples (Arrayss of length 2) of the form (id, Buffer)s.
    The scenario for using this is having several HexMemoryMaps, from several calls to module:ECB~hexToArrays|hexToArrays, each having a different identifier. This function locates where those memory block sets overlap, and returns a Map containing addresses as keys, and arrays as values. Each array will contain 1 or more (id, Buffer) tuples: the identifier of the memory block set that has data in that region, and the data itself. When memory block sets overlap, there will be more than one tuple.
    The Buffers in the output are https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray|subarrays of the input data; new memory is not allocated for them.
    The insertion order of keys in the output Map is guaranteed to be strictly ascending. In other words, when iterating through the Map, the addresses will be ordered in ascending order.
    When two blocks overlap, the corresponding array of tuples will have the tuples ordered in the insertion order of the input Map of block sets.

    Parameters

    • HexMemoryMaps: Map<string, HexMemoryMap>

      The input memory block sets

    Returns Map<any, any>

    The map of possibly overlapping memory blocks

    import {HexMemoryMap} from 'ECB';

    let memMap1 = HexMemoryMap.fromHex( hexdata1 );
    let memMap2 = HexMemoryMap.fromHex( hexdata2 );
    let memMap3 = HexMemoryMap.fromHex( hexdata3 );

    let maps = new Map([
    ['file A', blocks1],
    ['file B', blocks2],
    ['file C', blocks3]
    ]);

    let overlappings = HexMemoryMap.overlapHexMemoryMaps(maps);

    for (let [address, tuples] of overlappings) {
    // if 'tuples' has length > 1, there is an overlap starting at 'address'

    for (let [address, tuples] of overlappings) {
    let [id, bytes] = tuple;
    // 'id' in this example is either 'file A', 'file B' or 'file C'
    }
    }