forked from keybase/client
/
base64_finder.go
120 lines (102 loc) · 2.16 KB
/
base64_finder.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"encoding/base64"
"regexp"
"strings"
)
type Base64Finder struct {
input string
rxx *regexp.Regexp
lines []string
base64blocks []string
}
func NewBase64Finder(i string) *Base64Finder {
rxx := regexp.MustCompile(`^\s*(([a-zA-Z0-9/+_-]+)(={0,3}))\s*$`)
return &Base64Finder{input: i, rxx: rxx}
}
func (s *Base64Finder) split() {
s.lines = strings.Split(s.input, "\n")
}
func (s *Base64Finder) findAll() {
i := 0
for i >= 0 {
var msg string
msg, i = s.findOne(i)
if len(msg) > 0 {
s.base64blocks = append(s.base64blocks, msg)
}
}
}
func (s *Base64Finder) search(searchFor []byte, url bool) bool {
var encoding *base64.Encoding
if url {
encoding = base64.URLEncoding
} else {
encoding = base64.StdEncoding
}
i := 0
for i >= 0 {
var msg string
msg, i = s.findOne(i)
if len(msg) > 0 {
buf, err := encoding.DecodeString(msg)
if err == nil && FastByteArrayEq(searchFor, buf) {
return true
}
}
}
return false
}
func (s *Base64Finder) findOne(i int) (string, int) {
var parts []string
l := len(s.lines)
state := 0
for ; i < l; i++ {
line := s.lines[i]
match := s.rxx.FindStringSubmatch(line)
if match != nil {
state = 1
parts = append(parts, match[1])
if len(match[3]) > 0 {
// A terminal "=" character means jump on out
i++
break
}
} else if state == 1 {
break
} else {
// wait until next time
}
}
if i == l {
i = -1
}
ret := strings.Join(parts, "")
return ret, i
}
func (s *Base64Finder) Run() []string {
s.split()
s.findAll()
return s.base64blocks
}
func FindBase64Blocks(s string) []string {
eng := NewBase64Finder(s)
return eng.Run()
}
func FindFirstBase64Block(s string) string {
v := FindBase64Blocks(s)
if len(v) > 0 {
return v[0]
}
return ""
}
func FindBase64Snippets(s string) []string {
return regexp.MustCompile(`(([a-zA-Z0-9/+_-]+)(={0,3}))`).FindAllString(s, -1)
}
func FindBase64Block(s string, pattern []byte, url bool) bool {
eng := NewBase64Finder(s)
eng.split()
return eng.search(pattern, url)
}