export type MemoryBlocks = | Iterable<[number, Uint8Array]> | {[addr: number]: Uint8Array} | undefined | null; export type Overlap = [T, Uint8Array][]; export type Overlaps = Map>; export type MemoryMapTuple = [T, MemoryMap]; export type MemoryMaps = MemoryMapTuple[]; declare class MemoryMap extends Map { /** * @param {MemoryBlocks} blocks The initial value for the memory blocks inside this * MemoryMap. All keys must be numeric, and all values must be instances of * Uint8Array. Optionally it can also be a plain Object with * only numeric keys. */ constructor(blocks?: MemoryBlocks); /** * Parses a string containing data formatted in "Intel HEX" format, and * returns an instance of {@linkcode MemoryMap}. *
* The insertion order of keys in the {@linkcode MemoryMap} is guaranteed to be strictly * ascending. In other words, when iterating through the {@linkcode MemoryMap}, 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 * {@link https://github.com/NordicSemiconductor/nrf-intel-hex#Features|README file} for details. *
* If maxBlockSize is given, any contiguous data block larger than that will * be split in several blocks. * * @param {String} hexText The contents of a .hex file. * @param {Number} [maxBlockSize=Infinity] Maximum size of the returned Uint8Arrays. * * @return {MemoryMap} * * @example * import MemoryMap from 'nrf-intel-hex'; * * let intelHexString = * ":100000000102030405060708090A0B0C0D0E0F1068\n" + * ":00000001FF"; * * let memMap = MemoryMap.fromHex(intelHexString); * * for (let [address, dataBlock] of memMap) { * console.log('Data block at ', address, ', bytes: ', dataBlock); * } */ static fromHex(hexText: string, maxBlockSize?: number): MemoryMap; /** * Returns a new instance of {@linkcode MemoryMap}, containing * the same data, but concatenating together those memory blocks that are adjacent. *
* The insertion order of keys in the {@linkcode MemoryMap} is guaranteed to be strictly * ascending. In other words, when iterating through the {@linkcode MemoryMap}, 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 * {@linkcode MemoryMap} might have more entries than the input one. *
* If there is any overlap between blocks, an error will be thrown. *
* The returned {@linkcode MemoryMap} will use newly allocated memory. * * @param {Number} [maxBlockSize=Infinity] Maximum size of the Uint8Arrays in the * returned {@linkcode MemoryMap}. * * @return {MemoryMap} */ join(maxBlockSize?: number): MemoryMap; /** * Given a {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map|Map} * of {@linkcode MemoryMap}s, indexed by a alphanumeric ID, * returns a Map of address to tuples (Arrayss of length 2) of the form * (id, Uint8Array)s. *
* The scenario for using this is having several {@linkcode MemoryMap}s, from several calls to * {@link module:nrf-intel-hex~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, Uint8Array) 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 Uint8Arrays in the output are * {@link 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. *
* * @param {Map.MemoryMap} memoryMaps The input memory block sets * * @example * import MemoryMap from 'nrf-intel-hex'; * * let memMap1 = MemoryMap.fromHex( hexdata1 ); * let memMap2 = MemoryMap.fromHex( hexdata2 ); * let memMap3 = MemoryMap.fromHex( hexdata3 ); * * let maps = new Map([ * ['file A', blocks1], * ['file B', blocks2], * ['file C', blocks3] * ]); * * let overlappings = MemoryMap.overlapMemoryMaps(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' * } * } * @return {Map.Array} The map of possibly overlapping memory blocks */ static overlapMemoryMaps(memoryMaps: MemoryMaps): Overlaps; /** * Given the output of the {@linkcode MemoryMap.overlapMemoryMaps|overlapMemoryMaps} * (a Map of address to an Array of (id, Uint8Array) tuples), * returns a {@linkcode MemoryMap}. 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 Uint8Array 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. *
* * @param {Map.Array} overlaps The (possibly overlapping) input memory blocks * @return {MemoryMap} The flattened memory blocks */ static flattenOverlaps(overlaps: Overlaps): MemoryMap; /** * Returns a new instance of {@linkcode MemoryMap}, where: * *
    *
  • Each key (the start address of each Uint8Array) is a multiple of * pageSize
  • *
  • The size of each Uint8Array 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 {@linkcode MemoryMap} is guaranteed * to be strictly ascending. In other words, when iterating through the * {@linkcode MemoryMap}, the addresses will be ordered in ascending order. *
* The Uint8Arrays in the output will be newly allocated. *
* * @param {Number} [pageSize=1024] The size of the output pages, in bytes * @param {Number} [pad=0xFF] The byte value to use for padding * @return {MemoryMap} */ paginate(pageSize?: number, pad?: number): MemoryMap; /** * Locates the Uint8Array which contains the given offset, * and returns the four bytes held at that offset, as a 32-bit unsigned integer. * *
* Behaviour is similar to {@linkcode https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint32|DataView.prototype.getUint32}, * except that this operates over a {@linkcode MemoryMap} instead of * over an ArrayBuffer, and that this may return undefined if * the address is not entirely contained within one of the Uint8Arrays. *
* * @param {Number} offset The memory offset to read the data * @param {Boolean} [littleEndian=false] Whether to fetch the 4 bytes as a little- or big-endian integer * @return {Number|undefined} An unsigned 32-bit integer number */ getUint32( offset: number, littleEndian?: boolean ): number | undefined; /** * Returns a String of text representing a .hex file. *
* The writer has an opinionated behaviour. Check the project's * {@link https://github.com/NordicSemiconductor/nrf-intel-hex#Features|README file} for details. * * @param {Number} [lineSize=16] Maximum number of bytes to be encoded in each data record. * Must have a value between 1 and 255, as per the specification. * * @return {String} String of text with the .hex representation of the input binary data * * @example * import MemoryMap from 'nrf-intel-hex'; * * let memMap = new MemoryMap(); * let bytes = new Uint8Array(....); * memMap.set(0x0FF80000, bytes); // The block with 'bytes' will start at offset 0x0FF80000 * * let string = memMap.asHexString(); */ asHexString(lineSize?: number): string; /** * Performs a deep copy of the current {@linkcode MemoryMap}, returning a new one * with exactly the same contents, but allocating new memory for each of its * Uint8Arrays. * * @return {MemoryMap} */ clone(): MemoryMap; /** * Given one Uint8Array, looks through its contents and returns a new * {@linkcode MemoryMap}, stripping away those regions where there are only * padding bytes. *
* The start of the input Uint8Array 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 Uint8Arrays. * * @param {Uint8Array} bytes The input data * @param {Number} [padByte=0xFF] The value of the byte assumed to be used as padding * @param {Number} [minPadLength=64] The minimum number of consecutive pad bytes to * be considered actual padding * * @return {MemoryMap} */ static fromPaddedUint8Array( bytes: Uint8Array, padByte?: number, minPadLength?: number ): MemoryMap; /** * Returns a new instance of {@linkcode MemoryMap}, containing only data between * the addresses address and address + length. * Behavior is similar to {@linkcode https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/slice|Array.prototype.slice}, * in that the return value is a portion of the current {@linkcode MemoryMap}. * *
* The returned {@linkcode MemoryMap} might be empty. * *
* Internally, this uses subarray, so new memory is not allocated. * * @param {Number} address The start address of the slice * @param {Number} length The length of memory map to slice out * @return {MemoryMap} */ slice(address: number, length?: number): MemoryMap; /** * Returns a new instance of {@linkcode https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint32|Uint8Array}, containing only data between * the addresses address and address + length. Any byte without a value * in the input {@linkcode MemoryMap} will have a value of padByte. * *
* This method allocates new memory. * * @param {Number} address The start address of the slice * @param {Number} length The length of memory map to slice out * @param {Number} [padByte=0xFF] The value of the byte assumed to be used as padding * @return {Uint8Array} */ slicePad(address: number, length: number, padByte?: number): Uint8Array; /** * 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. * * @param {MemoryMap} memMap The memory map to check * @return {Boolean} */ contains(memMap: MemoryMap): boolean; } export default MemoryMap;