-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrelativeSearch.js
58 lines (48 loc) · 1.75 KB
/
relativeSearch.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const fs = require('fs'),
utils = require('./lib/utils.js');
let args = process.argv.slice(2, 4);
let romPath = args[0];
let text = args[1];
fs.readFile(romPath, (error, data) => {
let array = new Uint8Array(data);
let sentenceArray = buildSentence(text);
let distance = calculateDistance(sentenceArray);
let foundByteRoot, foundHexSentence;
if ([...Array(0xff).keys()].some(byte => {
let hexSentence = createHexSentence(distance, byte);
let result = utils.findInsideBuffer(hexSentence, array) >= 0;
if (result) {
console.log('Found!')
foundHexSentence = hexSentence
foundByteRoot = byte;
}
return result;
})) {
if (foundHexSentence) {
console.log('hexSentence:', foundHexSentence.map(byte => ('00' + Number(byte).toString(16)).substr(-2).toUpperCase()).join(' '))
console.log(`Byte for ${isUpperCase(text[0]) ? 'A' : 'a'}:`, getAByte(text, foundHexSentence).toString(16));
}
}
else {
console.log('not found');
}
});
const buildSentence = sentence => {
return Array.from(sentence).map((char) => ('00' + char.charCodeAt(0).toString('16')).substr(-2));
};
const calculateDistance = sentenceArray => {
return sentenceArray.map((char) => parseInt(sentenceArray[0], 16) - parseInt(char, 16));
};
const createHexSentence = (distance, root) => {
return distance.map(byte => root - byte);
};
const getAByte = (text, hexSentence) => {
let letterIndex;
if (isUpperCase(text[0])) {
letterIndex = utils.alphabet.toUpperCase().indexOf(text[0]);
return (hexSentence[0] - letterIndex).toString(16).toUpperCase();
}
letterIndex = utils.alphabet.indexOf(text[0]);
return (hexSentence[0] - letterIndex).toString(16).toUpperCase();
};
const isUpperCase = char => char === char.toUpperCase();