/
base64.go
134 lines (111 loc) · 3.39 KB
/
base64.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
package process
import (
"context"
"encoding/base64"
"github.com/brexhq/substation/condition"
"github.com/brexhq/substation/internal/errors"
)
// Base64InvalidDirection is used when an invalid direction setting is given to the processor
const Base64InvalidDirection = errors.Error("Base64InvalidDirection")
// Base64InvalidAlphabet is used when an invalid alphabet setting is given to the processor
const Base64InvalidAlphabet = errors.Error("Base64InvalidAlphabet")
/*
Base64Options contain custom options settings for this processor.
Direction: the direction of the encoding, either to (encode) or from (decode) base64.
Alphabet (optional): the base64 alphabet to use, either std (https://www.rfc-editor.org/rfc/rfc4648.html#section-4) or url (https://www.rfc-editor.org/rfc/rfc4648.html#section-5); defaults to std.
*/
type Base64Options struct {
Direction string `mapstructure:"direction"`
Alphabet string `mapstructure:"alphabet"`
}
// Base64 implements the Byter and Channeler interfaces and converts bytes to and from Base64. More information is available in the README.
type Base64 struct {
Condition condition.OperatorConfig `mapstructure:"condition"`
Options Base64Options `mapstructure:"options"`
}
// Channel processes a data channel of bytes with this processor. Conditions can be optionally applied on the channel data to enable processing.
func (p Base64) Channel(ctx context.Context, ch <-chan []byte) (<-chan []byte, error) {
var array [][]byte
op, err := condition.OperatorFactory(p.Condition)
if err != nil {
return nil, err
}
for data := range ch {
ok, err := op.Operate(data)
if err != nil {
return nil, err
}
if !ok {
array = append(array, data)
continue
}
processed, err := p.Byte(ctx, data)
if err != nil {
return nil, err
}
array = append(array, processed)
}
output := make(chan []byte, len(array))
for _, x := range array {
output <- x
}
close(output)
return output, nil
}
// Byte processes a byte slice with this processor.
func (p Base64) Byte(ctx context.Context, data []byte) ([]byte, error) {
if p.Options.Alphabet == "" {
p.Options.Alphabet = "std"
}
if p.Options.Direction == "from" {
res, err := fromBase64(data, p.Options.Alphabet)
if err != nil {
return nil, err
}
return res, nil
} else if p.Options.Direction == "to" {
res, err := toBase64(data, p.Options.Alphabet)
if err != nil {
return nil, err
}
return res, nil
} else {
return nil, Base64InvalidDirection
}
}
func fromBase64(data []byte, alphabet string) ([]byte, error) {
len := len(string(data))
switch s := alphabet; s {
case "std":
res := make([]byte, base64.StdEncoding.DecodedLen(len))
n, err := base64.StdEncoding.Decode(res, data)
if err != nil {
return nil, err
}
return res[:n], nil
case "url":
res := make([]byte, base64.URLEncoding.DecodedLen(len))
n, err := base64.URLEncoding.Decode(res, data)
if err != nil {
return nil, err
}
return res[:n], nil
default:
return nil, Base64InvalidAlphabet
}
}
func toBase64(data []byte, alphabet string) ([]byte, error) {
len := len(data)
switch s := alphabet; s {
case "std":
res := make([]byte, base64.StdEncoding.EncodedLen(len))
base64.StdEncoding.Encode(res, data)
return res, nil
case "url":
res := make([]byte, base64.URLEncoding.EncodedLen(len))
base64.URLEncoding.Encode(res, data)
return res, nil
default:
return nil, Base64InvalidAlphabet
}
}