forked from empijei/wapty
/
b16.go
154 lines (129 loc) · 2.83 KB
/
b16.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
package decode
import (
"bytes"
"encoding/hex"
)
const b16Alphabet = "0123456789abcdefABCDEF"
const b16name = "b16"
// Base16 takes a decoder and an input string
type Base16 struct {
dec *decoder
input string
}
// NewB16CodecC state machine to smartly decode a string with invalid chars
// nolint: gocyclo
func NewB16CodecC(in string) CodecC {
const (
itemInvalid itemType = iota
itemAlphabet
)
// emit should write into output what was read up until this point
// and move l.start to l.pos
emit := func(d *decoder, t itemType) {
token := d.input[d.start:d.pos]
var decodefunc func(string) []byte
switch t {
case itemAlphabet:
decodefunc = func(in string) []byte {
if len(in) < 2 {
return []byte(genInvalid(len(in)))
}
odd := false
if len(in)%2 != 0 {
in = in[:len(in)-1]
odd = true
}
buf, err := hex.DecodeString(in)
if err != nil {
return []byte(err.Error())
}
if odd {
buf = append(buf, []byte(genInvalid(1))...)
}
return buf
}
case itemInvalid:
decodefunc = func(in string) []byte {
return []byte(genInvalid(len(in)))
}
}
d.out.Write(decodefunc(token))
d.start = d.pos
}
var (
startState stateFn
invalidState stateFn
alphabetState stateFn
)
startState = func(d *decoder) stateFn {
switch n := d.peek(); {
case bytes.ContainsRune([]byte(b16Alphabet), n):
return alphabetState
case n == eof:
return nil
default:
return invalidState
}
}
invalidState = func(d *decoder) stateFn {
for {
switch n := d.next(); {
case bytes.ContainsRune([]byte(b16Alphabet), n):
d.backup()
emit(d, itemInvalid)
return alphabetState
case n == eof:
emit(d, itemInvalid)
return nil
}
}
}
alphabetState = func(d *decoder) stateFn {
for {
switch n := d.next(); {
case bytes.ContainsRune([]byte(b16Alphabet), n):
d.acceptRun(b16Alphabet)
continue
case n == eof:
emit(d, itemAlphabet)
return nil
default:
d.backup()
emit(d, itemAlphabet)
return invalidState
}
}
}
return &Base16{
dec: newDecoder(in, startState),
input: in,
}
}
// Name returns the name of the codec
func (b *Base16) Name() string {
return b16name
}
// Decode a valid b16 string
func (b *Base16) Decode() (output string) {
return string(b.dec.decode())
}
// Encode a string into b16
func (b *Base16) Encode() (output string) {
return hex.EncodeToString([]byte(b.input))
}
// Check returns the percentage of valid b16 characters in the input string
func (b *Base16) Check() (acceptability float64) {
var c int
var tot int
for _, r := range b.input {
tot++
if bytes.ContainsRune([]byte(b16Alphabet), r) {
c++
}
}
//Heuristic to consider uneven strings as less likely to be valid base16
if delta := tot % 2; delta != 0 {
tot += delta
}
return float64(c) / float64(tot)
}