-
Notifications
You must be signed in to change notification settings - Fork 1
/
sputter.go
224 lines (196 loc) · 3.96 KB
/
sputter.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package sputter
import (
"bytes"
crypto "crypto/rand"
"fmt"
"math/big"
"math/rand"
"regexp/syntax"
"time"
"unicode/utf8"
)
const (
repetitionMax = 100
maxUint64 = ^uint64(0)
maxInt64 = int64(maxUint64 >> 1)
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
// Gen takes a regular expression and attempts
// to generate a pseudo-randomized string that
// matches the input expression.
func Gen(exp string) (string, error) {
// cryptographically seed random package
bigSeed, err := crypto.Int(crypto.Reader, big.NewInt(maxInt64))
if err != nil {
return "", err
}
rand.Seed(bigSeed.Int64())
r, err := syntax.Parse(exp, 0)
if err != nil {
return "", err
}
return sput(r)
}
func GenInsecure(exp string) (string, error) {
r, err := syntax.Parse(exp, 0)
if err != nil {
return "", err
}
return sput(r)
}
// simple dfs syntax to string
func sput(r *syntax.Regexp) (string, error) {
switch r.Op {
case syntax.OpLiteral:
return literal(r), nil
case syntax.OpCharClass:
return charClass(r), nil
case syntax.OpAnyCharNotNL:
return any(r), nil
case syntax.OpBeginLine:
return "\n", nil
case syntax.OpEndLine:
return "\n", nil
case syntax.OpCapture:
return sput(r.Sub[0])
case syntax.OpRepeat:
return repeat(r)
case syntax.OpStar:
return star(r)
case syntax.OpPlus:
return plus(r)
case syntax.OpQuest:
return quest(r)
case syntax.OpConcat:
return concat(r)
case syntax.OpAlternate:
return alternate(r)
default:
return "", fmt.Errorf("unsupported syntax operation %d", r.Op)
}
}
func literal(r *syntax.Regexp) string {
return string(r.Rune)
}
func charClass(r *syntax.Regexp) string {
switch len(r.Rune) {
case 1:
return literal(r)
default:
// randomly select from available ranges
i := random(0, len(r.Rune)/2) * 2
n1, n2 := int(r.Rune[i]), int(r.Rune[i+1])
if n2-n1 == 0 {
return string(r.Rune[i : i+1])
}
randRune := rune(random(n1, n2+1))
s := string(
[]rune{
randRune,
},
)
return s
}
return ""
}
func any(r *syntax.Regexp) string {
c := '\n'
for utf8.ValidRune(c) && c == '\n' {
c = rune(random(1, int(utf8.MaxRune)))
}
return string([]rune{c})
}
func begin(r *syntax.Regexp) string {
c := '\n'
for utf8.ValidRune(c) && c == '\n' {
c = rune(random(1, int(utf8.MaxRune)))
}
return string([]rune{c})
}
func end(r *syntax.Regexp) string {
c := '\n'
for utf8.ValidRune(c) && c == '\n' {
c = rune(random(1, int(utf8.MaxRune)))
}
return string([]rune{c})
}
func repeat(r *syntax.Regexp) (string, error) {
var buffer bytes.Buffer
n := r.Min
if r.Max > r.Min {
n = random(r.Min, r.Max)
}
if r.Max == 0 {
return "", nil
}
for i := 0; i < n; i++ {
s, err := sput(r.Sub[0])
if err != nil {
return "", err
}
_, err = buffer.WriteString(s)
if err != nil {
return "", err
}
}
return buffer.String(), nil
}
func star(r *syntax.Regexp) (string, error) {
var buffer bytes.Buffer
n := random(0, repetitionMax)
for i := 0; i < n; i++ {
s, err := sput(r.Sub[0])
if err != nil {
return "", err
}
_, err = buffer.WriteString(s)
if err != nil {
return "", err
}
}
return buffer.String(), nil
}
func plus(r *syntax.Regexp) (string, error) {
var buffer bytes.Buffer
n := random(1, repetitionMax)
for i := 0; i < n; i++ {
s, err := sput(r.Sub[0])
if err != nil {
return "", err
}
_, err = buffer.WriteString(s)
if err != nil {
return "", err
}
}
return buffer.String(), nil
}
func quest(r *syntax.Regexp) (string, error) {
if rand.Int()%2 == 0 {
return sput(r.Sub[0])
}
return "", nil
}
func concat(r *syntax.Regexp) (string, error) {
var buffer bytes.Buffer
for _, sub := range r.Sub {
s, err := sput(sub)
if err != nil {
return "", err
}
_, err = buffer.WriteString(s)
if err != nil {
return "", err
}
}
return buffer.String(), nil
}
func alternate(r *syntax.Regexp) (string, error) {
i := random(0, len(r.Sub))
return sput(r.Sub[i])
}
func random(min, max int) int {
return rand.Intn(max-min) + min
}