bun-memory 1.1.14 → 1.1.16

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.
package/README.md CHANGED
@@ -125,24 +125,35 @@ while (true) {
125
125
  }
126
126
  ```
127
127
 
128
- ```ts
129
- const scratch = Buffer.allocUnsafe(0x100);
130
- memory.read(myAddress, scratch);
131
- ```
128
+ ### Searching Memory
132
129
 
133
- ```ts
134
- const scratch = new Uint32Array(0x10);
135
- memory.read(myAddress, scratch);
136
- ```
137
-
138
- ### Pattern Scanning
130
+ #### `findPattern`
139
131
 
140
132
  Pattern scanning is temporarily disabled but will return shortly.
141
133
 
134
+ #### `indexOf`
135
+
136
+ Find the first occurrence of a byte sequence within a memory range and return the absolute address (`bigint`) of the first match, or `-1n` when not found. Accepts any Scratch-compatible input (`Buffer`, `TypedArray`, `DataView`, `ArrayBuffer`, or `SharedArrayBuffer`). No region or protection checking is performed; ensure the range is readable before calling.
137
+
142
138
  ```ts
143
- const offset = memory.findPattern('aa??bbccdd??ff', mainModule.modBaseAddr, mainModule.modBaseSize);
144
- const value = memory.bool(offset + 0x1234n);
145
- memory.close();
139
+ // Attach…
140
+ const memory = new Memory('cs2.exe');
141
+
142
+ // Get the loaded client.dll…
143
+ const client = cs2.modules['client.dll'];
144
+
145
+ // Choose any Scratch type for the needle (bytes are matched exactly as laid out in memory).
146
+ const needle = Buffer.from([0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00]);
147
+ // const needle = new Uint8Array([0x48, 0x8B, 0x05, 0x00, 0x00, 0x00, 0x00]);
148
+ // const needle = new Uint32Array([0xDEADBEEF, 0x11223344]);
149
+ // const needle = new DataView(Uint8Array.from([0xde, 0xad, 0xbe, 0xef]).buffer);
150
+
151
+ // Search client.dll for the byte sequence…
152
+ const addeess = memory.indexOf(needle, client.base, client.size);
153
+
154
+ if (address !== -1n) {
155
+ console.log(`Found at 0x${address.toString(16)}`);
156
+ }
146
157
  ```
147
158
 
148
159
  ## Notes
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.16",
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;