-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
169 lines (131 loc) · 3 KB
/
util.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package gopwntools
import (
"fmt"
"strings"
"encoding/base64"
"encoding/binary"
"encoding/hex"
)
type number interface {
int | string
}
// UnHex hex-decodes a string
func UnHex(h string) []byte {
b, err := hex.DecodeString(h)
if err != nil {
Error(err.Error())
}
return b
}
// Hex creates a hex-string from a byte slice
func Hex(b []byte) string {
return hex.EncodeToString(b)
}
// Xor applies a byte-wise XOR operation to the byte slices passed as arguments
func Xor(a []byte, b ...[]byte) []byte {
length := len(a)
for j := 0; j < len(b); j++ {
if len(b[j]) > length {
length = len(b[j])
}
}
r := make([]byte, length)
for i := 0; i < length; i++ {
rr := a[i%len(a)]
for j := 0; j < len(b); j++ {
rr ^= b[j][i%len(b[j])]
}
r = append(r, rr)
}
return r
}
// B64d Base64-decodes a string
func B64d(e string) []byte {
d, err := base64.StdEncoding.DecodeString(e)
if err != nil {
Error(err.Error())
}
return d
}
// B64e Base64-encodes a string
func B64e(d []byte) string {
return base64.StdEncoding.EncodeToString(d)
}
// P64 packs a 64-bit unsigned integer
func P64(v uint64) []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, v)
return b
}
// P32 packs a 32-bit unsigned integer
func P32(v uint32) []byte {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, v)
return b
}
// P16 packs a 16-bit unsigned integer
func P16(v uint16) []byte {
b := make([]byte, 2)
binary.LittleEndian.PutUint16(b, v)
return b
}
// P8 packs an 8-bit unsigned integer
func P8(v uint8) []byte {
return []byte{v}
}
// U64 unpacks a 64-bit unsigned integer
func U64(b []byte) uint64 {
if len(b) != 8 {
Error("U64 requires a buffer of 8 bytes")
}
return binary.LittleEndian.Uint64(b)
}
// U32 unpacks a 32-bit unsigned integer
func U32(b []byte) uint32 {
if len(b) != 4 {
Error("U32 requires a buffer of 4 bytes")
}
return binary.LittleEndian.Uint32(b)
}
// U16 unpacks a 16-bit unsigned integer
func U16(b []byte) uint16 {
if len(b) != 2 {
Error("U16 requires a buffer of 2 bytes")
}
return binary.LittleEndian.Uint16(b)
}
// U8 unpacks an 8-bit unsigned integer
func U8(b []byte) uint8 {
if len(b) != 1 {
Error("U8 requires a buffer of 1 byte")
}
return b[0]
}
func raw(ss string) string {
lines := strings.Split(ss, "\n")
var rawLines []string
const replace = "\x07a\x08b\x09t\x0an\x0bv\x0cf\x0dr"
for i, s := range lines {
if len(s) == 0 {
continue
}
s = strings.ReplaceAll(s, `\`, `\\`)
s = strings.ReplaceAll(s, `"`, `\"`)
for b := byte(0); b < 0x20; b++ {
if b >= 7 && b <= 13 {
s = strings.ReplaceAll(s, replace[2*(b-7):][:1], `\`+replace[2*(b-7)+1:][:1])
} else {
s = strings.ReplaceAll(s, string([]byte{b}), fmt.Sprintf(`\x%02x`, b))
}
}
for b := byte(0x7f); b != 0; b++ {
s = strings.ReplaceAll(s, string([]byte{b}), fmt.Sprintf(`\x%02x`, b))
}
if i == len(lines)-1 {
rawLines = append(rawLines, `"`+s+`"`)
} else {
rawLines = append(rawLines, `"`+s+`\n"`)
}
}
return "\t" + strings.Join(rawLines, "\n\t")
}