-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternScanner.js
More file actions
40 lines (30 loc) · 930 Bytes
/
Copy pathPatternScanner.js
File metadata and controls
40 lines (30 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//How is there no stock way to patternscan a Buffer in Node?
//Written by me, lowkey proud
module.exports = (buffer, startOffset = 0, bytes) => {
let bytesToFind = [...bytes]
if(bytesToFind[0] === "?") throw "First byte can't be wildcard!"
let sweepSearch = []
while(bytesToFind.length) {
if(bytesToFind[0] === "?") break
sweepSearch.push(bytesToFind.shift())
}
sweepSearch = Buffer.from(sweepSearch)
let i = startOffset
let max = buffer.length - bytesToFind.length
while(i !== -1 && i < max) {
i = buffer.indexOf(sweepSearch, i)
if(i !== -1) {
let fullMatch = true
for(let occurenceSearch = 0; occurenceSearch < bytesToFind.length; occurenceSearch++) {
if(bytesToFind[occurenceSearch] !== "?" && buffer[i + occurenceSearch + sweepSearch.length] != bytesToFind[occurenceSearch]) {
fullMatch = false
break
}
}
if(fullMatch) return i
}
else break
i++
}
return -1
}