File size: 1,768 Bytes
82ebe5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9051c66
 
82ebe5f
 
 
 
 
 
 
9051c66
82ebe5f
 
 
 
9051c66
82ebe5f
 
 
 
 
 
 
 
 
 
 
 
 
9051c66
 
82ebe5f
9051c66
82ebe5f
 
 
 
 
 
 
9051c66
82ebe5f
 
9051c66
 
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
59
60
export function getEncodedSnapSave(data) {
    return data.split('decodeURIComponent(escape(r))}(')[1]
        ?.split('))')[0]
        ?.split(',')
        ?.map(v => v.replace(/"/g, '').trim()) || [];
}

export function getDecodedSnapSave(data) {
    return data.split('getElementById("download-section").innerHTML = "')[1]
        ?.split('"; document.getElementById("inputData").remove(); ')[0]
        ?.replace(/\\(\\)?/g, '') || '';
}

export function decryptSnapSave(data) {
    const args = getEncodedSnapSave(data);
    if (args.length < 6) {
        throw new Error("Invalid SnapSave decryption arguments.");
    }
    const decoded = decodeSnapSave(...args);
    return getDecodedSnapSave(decoded);
}

export function decodeSnapSave(...args) {
    let [h, u, n, t, e, r] = args;

    function decode(d, e, f) {
        const g = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split('');
        let h = g.slice(0, e);
        let i = g.slice(0, f);

        let j = d.split('').reverse().reduce((a, b, c) => {
            let index = h.indexOf(b);
            return index !== -1 ? a + index * Math.pow(e, c) : a;
        }, 0);

        let k = '';
        while (j > 0) {
            k = i[j % f] + k;
            j = Math.floor(j / f);
        }
        return k || '0';
    }

    // Remove duplicate `let r`
    let result = '';
    for (let i = 0, len = h.length; i < len; i++) {
        let s = '';
        while (h[i] !== n[e]) {
            s += h[i];
            i++;
        }
        for (let j = 0; j < n.length; j++) {
            s = s.replace(new RegExp(n[j], "g"), j.toString());
        }
        result += String.fromCharCode(decode(s, e, 10) - t);
    }

    return decodeURIComponent(encodeURIComponent(result));
}