-
Notifications
You must be signed in to change notification settings - Fork 0
/
eexec.go
115 lines (101 loc) · 2.41 KB
/
eexec.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// seehuhn.de/go/postscript - a rudimentary PostScript interpreter
// Copyright (C) 2023 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package type1
import "io"
func obfuscateCharstring(plain []byte, iv []byte) []byte {
var R uint16 = 4330
var c1 uint16 = eexecC1
var c2 uint16 = eexecC2
cipher := make([]byte, len(iv)+len(plain))
copy(cipher, iv)
copy(cipher[len(iv):], plain)
for i, c := range cipher {
c = c ^ byte(R>>8)
R = (uint16(c)+R)*c1 + c2
cipher[i] = c
}
return cipher
}
func deobfuscateCharstring(cipher []byte, n int) []byte {
if len(cipher) < n {
return nil
}
var R uint16 = 4330
var c1 uint16 = eexecC1
var c2 uint16 = eexecC2
plain := make([]byte, 0, len(cipher)-n)
for i, cipher := range cipher {
if i >= n {
plain = append(plain, cipher^byte(R>>8))
}
R = (uint16(cipher)+R)*c1 + c2
}
return plain
}
type eexecWriter struct {
w io.Writer
buf []byte
pos int
R uint16
}
func newEExecWriter(w io.Writer) (*eexecWriter, error) {
res := &eexecWriter{
w: w,
buf: make([]byte, 512),
R: eexecR0,
}
iv := []byte{'X' ^ byte(eexecR0>>8), 0, 0, 0}
_, err := res.Write(iv)
if err != nil {
return nil, err
}
return res, nil
}
func (w *eexecWriter) Write(p []byte) (n int, err error) {
for len(p) > 0 {
k := copy(w.buf[w.pos:], p)
w.pos += k
n += k
p = p[k:]
if w.pos >= len(w.buf) {
err = w.flush()
if err != nil {
return n, err
}
}
}
return n, nil
}
func (w *eexecWriter) Close() error {
return w.flush()
}
func (w *eexecWriter) flush() error {
for i := 0; i < w.pos; i++ {
w.buf[i] = w.buf[i] ^ byte(w.R>>8)
w.R = (uint16(w.buf[i])+w.R)*eexecC1 + eexecC2
}
_, err := w.w.Write(w.buf[:w.pos])
if err != nil {
return err
}
w.pos = 0
return nil
}
const (
eexecC1 = 52845
eexecC2 = 22719
eexecR0 = 55665
)