-
Notifications
You must be signed in to change notification settings - Fork 1
/
regexp.go
515 lines (440 loc) · 13.6 KB
/
regexp.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package passit
import (
"errors"
"fmt"
"io"
"math/bits"
"regexp/syntax"
"strconv"
"strings"
"unicode"
)
const (
maxUnboundedRepeatCount = 15
// questNoChance is the likelihood that Z? will output nothing.
// The fraction should be reduced.
questNoChanceNumerator = 1
questNoChanceDenominator = 2
)
type regexpGenerator func(*strings.Builder, io.Reader) error
func (rg regexpGenerator) Password(r io.Reader) (string, error) {
var b strings.Builder
if err := rg(&b, r); err != nil {
return "", err
}
return b.String(), nil
}
// RegexpParser is a regular expressions parser that parses patterns into a
// Generator that generates passwords matching the parsed regexp. The zero-value is
// a usable parser.
type RegexpParser struct {
anyTab *unicode.RangeTable
specialCaptures map[string]SpecialCaptureFactory
}
// ParseRegexp is a shortcut for new(RegexpParser).Parse(pattern, flags).
func ParseRegexp(pattern string, flags syntax.Flags) (Generator, error) {
return new(RegexpParser).Parse(pattern, flags)
}
// SetAnyRangeTable sets the unicode.RangeTable used when generating any (.)
// characters or when restricting character classes ([a-z]) with a user provided
// one. By default a subset of ASCII is used. Calling SetAnyRangeTable(nil) will
// reset the RegexpParser back to the default.
//
// The regexp Generator is only deterministic if the same unicode.RangeTable is
// used. Be aware that the builtin unicode.X tables are subject to change as new
// versions of Unicode are released and are not suitable for deterministic use.
func (p *RegexpParser) SetAnyRangeTable(tab *unicode.RangeTable) {
p.anyTab = tab
}
// SetSpecialCapture adds a special capture factory to use for matching named
// captures. A regexp pattern such as "(?P<name>)" will invoke the factory and use
// the returned Generator instead of the contents of the capture. If name is "*",
// the factory will be used as a fallback if a named factory can't be found.
//
// If attempting to parse the inner contents of the capture, be aware that the
// regexp parser may have mangled them. For instance "(?P<name>1|2)" will become
// "(?P<name>[1-2])", "(?P<name>z|z)" will become "(?P<name>z)" and
// "(?i:(?P<name>z))" will become "(?P<name>(?i:Z))".
func (p *RegexpParser) SetSpecialCapture(name string, factory SpecialCaptureFactory) {
if p.specialCaptures == nil {
p.specialCaptures = make(map[string]SpecialCaptureFactory)
}
p.specialCaptures[name] = factory
}
// Parse parses the regexp pattern according to the flags and returns a Generator.
// It returns an error if the regexp is invalid. It uses regexp/syntax to parse the
// pattern.
//
// All regexp features supported by regexp/syntax are supported, though some may
// have no effect.
//
// It is an error to use named captures (?P<name>) except to refer to special
// capture factories added with SetSpecialCapture.
func (p *RegexpParser) Parse(pattern string, flags syntax.Flags) (Generator, error) {
// Note: The FoldCase, OneLine, DotNL and NonGreedy flags can be set or
// cleared within the pattern.
// The Literal flag is odd in that it can interact with FoldCase in ways
// that may be invalid. syntax.literalRegexp doesn't call syntax.minFoldRune
// so the resulting OpLiteral won't properly be the minimum folded runes.
// That means foldedLiteral will create an invalid character class.
if flags&(syntax.Literal|syntax.FoldCase) == syntax.Literal|syntax.FoldCase {
return nil, errors.New("passit: Literal flag is unsupported when used with FoldCase")
}
// If we're not going to generate newlines, we can set syntax.MatchNL in
// flags. This simplifies the parsed character classes and avoids needing to
// call anyCharNotNL. It does this without changing the output of the
// Generator.
if !p.hasAnyNL() {
flags |= syntax.MatchNL
}
r, err := syntax.Parse(pattern, flags)
if err != nil {
return nil, err
}
return p.compile(r)
}
func (p *RegexpParser) compile(r *syntax.Regexp) (regexpGenerator, error) {
// Strip un-named captures without recursing.
for r.Op == syntax.OpCapture && r.Name == "" {
r = r.Sub[0]
}
var (
gen regexpGenerator
err error
)
switch r.Op {
case syntax.OpEmptyMatch:
// This is handled below by onlyEmptyOutput.
case syntax.OpLiteral:
gen, err = p.literal(r)
case syntax.OpCharClass:
gen, err = p.charClass(r)
case syntax.OpAnyCharNotNL:
gen, err = p.anyCharNotNL(r)
case syntax.OpAnyChar:
gen, err = p.anyChar(r)
case syntax.OpBeginLine, syntax.OpEndLine,
syntax.OpBeginText, syntax.OpEndText,
syntax.OpWordBoundary, syntax.OpNoWordBoundary:
// This is handled below by onlyEmptyOutput.
case syntax.OpCapture:
// Un-named captures are handled above.
gen, err = p.namedCapture(r)
case syntax.OpStar:
gen, err = p.star(r)
case syntax.OpPlus:
gen, err = p.plus(r)
case syntax.OpQuest:
gen, err = p.quest(r)
case syntax.OpRepeat:
gen, err = p.repeat(r)
case syntax.OpConcat:
gen, err = p.concat(r)
case syntax.OpAlternate:
gen, err = p.alternate(r)
default:
err = fmt.Errorf("passit: invalid regexp %q, unhandled op %s", r, r.Op)
}
if err != nil {
return nil, err
}
// Check onlyEmptyOutput after we've compiled the syntax.Regexp to ensure we
// surface any errors.
if onlyEmptyOutput(r) {
return func(*strings.Builder, io.Reader) error {
return nil
}, nil
}
if gen == nil {
panic("passit: internal error: gen is nil after onlyEmptyOutput")
}
return gen, nil
}
func (p *RegexpParser) literal(sr *syntax.Regexp) (regexpGenerator, error) {
// FoldCase is the only flag relevant here.
if sr.Flags&syntax.FoldCase != 0 {
return p.foldedLiteral(sr)
}
return rawLiteral(sr.Rune), nil
}
func rawLiteral(runes []rune) regexpGenerator {
s := string(runes)
return func(b *strings.Builder, r io.Reader) error {
b.WriteString(s)
return nil
}
}
func (p *RegexpParser) foldedLiteral(sr *syntax.Regexp) (regexpGenerator, error) {
gens := make([]regexpGenerator, 0, len(sr.Rune))
litStart := -1
for i, c := range sr.Rune {
// SimpleFold(c) returns c if there are no equivalent runes.
if unicode.SimpleFold(c) == c {
if litStart < 0 {
litStart = i
}
continue
}
if litStart >= 0 {
gens = append(gens, rawLiteral(sr.Rune[litStart:i+1]))
litStart = -1
}
gen, err := p.foldedRune(c)
if err != nil {
return nil, err
}
gens = append(gens, gen)
}
if litStart >= 0 {
gens = append(gens, rawLiteral(sr.Rune[litStart:]))
}
return concatGenerators(gens), nil
}
func (p *RegexpParser) foldedRune(c rune) (regexpGenerator, error) {
// We generate a syntax.Regexp here and pass it to charClass rather than
// generating the unicode.RangeTable directly so that we get a nicer error
// message.
sr := &syntax.Regexp{Op: syntax.OpCharClass}
sr.Rune = append(sr.Rune0[:0], c, c)
for f := unicode.SimpleFold(c); f != c; f = unicode.SimpleFold(f) {
sr.Rune = append(sr.Rune, f, f)
}
// We don't need to sort sr.Rune as regexp/syntax ensures that the rune
// present in the literal is always the minimum rune. See
// regexp/syntax.minFoldRune.
return p.charClass(sr)
}
func (p *RegexpParser) charClass(sr *syntax.Regexp) (regexpGenerator, error) {
anyTab := p.anyRangeTable()
var tab unicode.RangeTable
for i := 0; i < len(sr.Rune); i += 2 {
addIntersectingRunes(&tab, sr.Rune[i], sr.Rune[i+1], anyTab)
}
setLatinOffset(&tab)
return charClassGenerator(sr, &tab)
}
func (p *RegexpParser) anyCharNotNL(sr *syntax.Regexp) (regexpGenerator, error) {
return charClassGenerator(sr, p.anyRangeTableNoNL())
}
func (p *RegexpParser) anyChar(sr *syntax.Regexp) (regexpGenerator, error) {
return charClassGenerator(sr, p.anyRangeTable())
}
func charClassGenerator(sr *syntax.Regexp, tab *unicode.RangeTable) (regexpGenerator, error) {
count := countRunesInTable(tab)
if count == 0 {
return nil, fmt.Errorf("passit: character class %s contains zero allowed runes", sr)
}
return func(b *strings.Builder, r io.Reader) error {
idx, err := readIntN(r, count)
b.WriteRune(getRuneInTable(tab, idx))
return err
}, nil
}
func (p *RegexpParser) namedCapture(sr *syntax.Regexp) (regexpGenerator, error) {
factory, ok := p.specialCaptures[sr.Name]
if !ok {
factory, ok = p.specialCaptures["*"]
}
if !ok {
return nil, errors.New("passit: named capture refers to unknown special capture factory")
}
// We pass in sr rather than sr.Sub[0] so that the factory can differentiate
// how it was called if it's present under multiple names.
gen, err := factory(sr)
if err != nil {
return nil, err
}
return func(b *strings.Builder, r io.Reader) error {
pass, err := gen.Password(r)
b.WriteString(pass)
return err
}, nil
}
func (p *RegexpParser) star(sr *syntax.Regexp) (regexpGenerator, error) {
// NonGreedy, which we ignore, is the only relevant flag here.
sr.Min, sr.Max = 0, -1
return p.repeat(sr)
}
func (p *RegexpParser) plus(sr *syntax.Regexp) (regexpGenerator, error) {
// NonGreedy, which we ignore, is the only relevant flag here.
sr.Min, sr.Max = 1, -1
return p.repeat(sr)
}
func (p *RegexpParser) quest(sr *syntax.Regexp) (regexpGenerator, error) {
// NonGreedy, which we ignore, is the only relevant flag here.
gen, err := p.compile(sr.Sub[0])
if err != nil {
return nil, err
}
return func(b *strings.Builder, r io.Reader) error {
n, err := readIntN(r, questNoChanceDenominator)
if err != nil {
return err
}
if n < questNoChanceNumerator {
return nil
}
return gen(b, r)
}, nil
}
func (p *RegexpParser) repeat(sr *syntax.Regexp) (regexpGenerator, error) {
// NonGreedy, which we ignore, is the only relevant flag here.
min := sr.Min
max := sr.Max
if max == -1 {
max = sr.Min + maxUnboundedRepeatCount
}
gen, err := p.compile(sr.Sub[0])
if err != nil {
return nil, err
}
// N can never overflow as syntax.Parse will return an error if min or max
// exceed 1000.
N := max - min + 1
return func(b *strings.Builder, r io.Reader) error {
n, err := readIntN(r, N)
if err != nil {
return err
}
for range min + n {
if err := gen(b, r); err != nil {
return err
}
}
return nil
}, nil
}
func (p *RegexpParser) concat(sr *syntax.Regexp) (regexpGenerator, error) {
gens := make([]regexpGenerator, 0, len(sr.Sub))
for _, r := range sr.Sub {
gen, err := p.compile(r)
if err != nil {
return nil, err
}
// Skip past empty generators. We do this after the call to compile
// to ensure we surface any errors.
if !onlyEmptyOutput(r) {
gens = append(gens, gen)
}
}
return concatGenerators(gens), nil
}
func concatGenerators(gens []regexpGenerator) regexpGenerator {
if len(gens) == 1 {
return gens[0]
}
return func(b *strings.Builder, r io.Reader) error {
for _, gen := range gens {
if err := gen(b, r); err != nil {
return err
}
}
return nil
}
}
func (p *RegexpParser) alternate(sr *syntax.Regexp) (regexpGenerator, error) {
gens := make([]regexpGenerator, len(sr.Sub))
for i, r := range sr.Sub {
gen, err := p.compile(r)
if err != nil {
return nil, err
}
// We don't skip empty generators here as they change the behaviour
// of the generator.
gens[i] = gen
}
return func(b *strings.Builder, r io.Reader) error {
gen, err := readSliceN(r, gens)
if err != nil {
return err
}
return gen(b, r)
}, nil
}
func (p *RegexpParser) hasAnyNL() bool {
// rangeTableASCII doesn't include \n so we only need to test this if
// SetAnyRangeTable was called.
return p.anyTab != nil && unicode.Is(p.anyTab, '\n')
}
var rangeTableASCII = &unicode.RangeTable{
R16: []unicode.Range16{
{Lo: 0x0020, Hi: 0x007e, Stride: 1},
},
LatinOffset: 1,
}
func (p *RegexpParser) anyRangeTable() *unicode.RangeTable {
if p.anyTab != nil {
return p.anyTab
}
return rangeTableASCII
}
func (p *RegexpParser) anyRangeTableNoNL() *unicode.RangeTable {
if !p.hasAnyNL() {
return p.anyRangeTable()
}
return removeNLFromRangeTable(p.anyRangeTable())
}
func onlyEmptyOutput(sr *syntax.Regexp) bool {
switch sr.Op {
case syntax.OpEmptyMatch:
return true
case syntax.OpCapture:
if sr.Name != "" {
return false
}
// fallout
case syntax.OpStar, syntax.OpPlus, syntax.OpQuest:
// fallout
case syntax.OpRepeat:
if sr.Min == 0 && sr.Max == 0 {
return true
}
// fallout
case syntax.OpConcat, syntax.OpAlternate:
// fallout
case syntax.OpBeginLine, syntax.OpEndLine,
syntax.OpBeginText, syntax.OpEndText,
syntax.OpWordBoundary, syntax.OpNoWordBoundary:
return true
default:
return false
}
for _, sub := range sr.Sub {
if !onlyEmptyOutput(sub) {
return false
}
}
return true
}
// SpecialCaptureFactory represents a special capture factory to be used with
// (*RegexpParser).SetSpecialCapture.
type SpecialCaptureFactory func(*syntax.Regexp) (Generator, error)
// SpecialCaptureBasic returns a special capture factory that doesn't accept any
// input and always returns the provided Generator.
func SpecialCaptureBasic(gen Generator) SpecialCaptureFactory {
return func(sr *syntax.Regexp) (Generator, error) {
if sr.Sub[0].Op == syntax.OpEmptyMatch {
return gen, nil
}
return nil, errors.New("passit: unsupported capture")
}
}
// SpecialCaptureWithRepeat returns a special capture factory that parses the
// capture value for a count to be used with Repeat(gen, sep, count). If the
// capture is empty, the Generator is returned directly.
func SpecialCaptureWithRepeat(gen Generator, sep string) SpecialCaptureFactory {
return func(sr *syntax.Regexp) (Generator, error) {
switch sr.Sub[0].Op {
case syntax.OpEmptyMatch:
return gen, nil
case syntax.OpLiteral:
count, err := strconv.ParseUint(string(sr.Sub[0].Rune), 10, bits.UintSize-1)
if err != nil {
return nil, fmt.Errorf("passit: failed to parse capture: %w", err)
}
return Repeat(gen, sep, int(count)), nil
default:
return nil, errors.New("passit: unsupported capture")
}
}
}