bun-memory 1.1.14 → 1.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/structs/Memory.ts +53 -2
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "url": "git://github.com/obscuritysrl/bun-memory.git"
23
23
  },
24
24
  "type": "module",
25
- "version": "1.1.14",
25
+ "version": "1.1.15",
26
26
  "main": "./index.ts",
27
27
  "keywords": [
28
28
  "bun",
package/structs/Memory.ts CHANGED
@@ -30,7 +30,6 @@ const { symbols: Kernel32 } = dlopen('kernel32.dll', {
30
30
  * supporting various data types including primitives, arrays, and custom structures like vectors and quaternions.
31
31
  *
32
32
  * @todo Reimplement `findPattern(…)`.
33
- * @todo Reimplement `indexOf(…)`.
34
33
  *
35
34
  * @example
36
35
  * ```typescript
@@ -322,7 +321,7 @@ class Memory {
322
321
  return;
323
322
  }
324
323
 
325
- // Public utility methods
324
+ // Public read / write methods
326
325
 
327
326
  /**
328
327
  * Closes the handle to the target process and releases resources.
@@ -1790,6 +1789,58 @@ class Memory {
1790
1789
 
1791
1790
  return this.quaternionArray(address, lengthOrValues);
1792
1791
  }
1792
+
1793
+ // Public utility methods…
1794
+
1795
+ /**
1796
+ * Searches a memory range for a byte sequence and returns the absolute address of the first match.
1797
+ *
1798
+ * This method reads `length` bytes starting at `address` into a temporary buffer and performs a
1799
+ * subsequence search. No region or protection checking is performed; ensure the range is readable
1800
+ * before calling.
1801
+ *
1802
+ * The `needle` accepts any Scratch-compatible buffer or view (`Buffer`, `TypedArray`, or `DataView`).
1803
+ * Bytes are matched exactly as laid out in memory. :contentReference[oaicite:0]{index=0}
1804
+ *
1805
+ * @param needle - Byte sequence to search for (Scratch-compatible buffer or view)
1806
+ * @param address - Base memory address to begin searching (inclusive)
1807
+ * @param length - Number of bytes to scan
1808
+ * @returns Absolute address (`bigint`) of the first match, or `-1n` when not found
1809
+ *
1810
+ * @example
1811
+ * ```typescript
1812
+ * // Search a loaded module for a byte sequence
1813
+ * const client = memory.modules['client.dll'];
1814
+ *
1815
+ * const needle = Buffer.from([0x48, 0x8B, 0x05, 0x00, 0x00, 0x00, 0x00]);
1816
+ * const matchAddress = memory.indexOf(needle, client.base, client.size);
1817
+ * ```
1818
+ *
1819
+ * @example
1820
+ * ```typescript
1821
+ * // Search using a Uint32Array needle (bytes are matched exactly as laid out in memory)
1822
+ * const needle = new Uint32Array([0xDEADBEEF, 0x11223344]);
1823
+ * const matchAddress = memory.indexOf(needle, client.base, client.size);
1824
+ * ```
1825
+ */
1826
+
1827
+ public indexOf(needle: Scratch, address: bigint, length: number): bigint {
1828
+ const haystackUint8Array = new Uint8Array(length);
1829
+
1830
+ this.read(address, haystackUint8Array);
1831
+
1832
+ const haystackBuffer = Buffer.from(haystackUint8Array.buffer, haystackUint8Array.byteOffset, haystackUint8Array.byteLength);
1833
+
1834
+ const needleUint8Array = ArrayBuffer.isView(needle) //
1835
+ ? new Uint8Array(needle.buffer, needle.byteOffset, needle.byteLength)
1836
+ : new Uint8Array(needle);
1837
+
1838
+ const needleBuffer = Buffer.from(needleUint8Array.buffer, needleUint8Array.byteOffset, needleUint8Array.byteLength);
1839
+
1840
+ const indexOf = haystackBuffer.indexOf(needleBuffer);
1841
+
1842
+ return indexOf !== -1 ? BigInt(indexOf) + address : -1n;
1843
+ }
1793
1844
  }
1794
1845
 
1795
1846
  export default Memory;