-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte_mapper.go
64 lines (52 loc) · 2.05 KB
/
byte_mapper.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
package streams
import "io"
// ByteMapper remaps all intercepted bytes based on the passed ByteMapperFunc. It should be safe
// to use either a statefull or idempotent function in this. However you should avoid reuse of a
// stateful ByteMapperFunc as correct behavior is difficult and error prone to implement.
type ByteMapper struct {
bmfn ByteMapperFunc
}
func NewByteMapper(bmfn ByteMapperFunc) Interceptor {
return &ByteMapper{
bmfn: bmfn,
}
}
func (bm *ByteMapper) InterceptWrite(w io.Writer, p []byte) (n int, err error) {
for i, b := range p {
p[i] = bm.bmfn(b)
}
return w.Write(p)
}
func (bm *ByteMapper) InterceptRead(r io.Reader, p []byte) (n int, err error) {
if n, err = r.Read(p); err != nil && err != io.EOF {
return
}
for i, b := range p[:n] {
p[i] = bm.bmfn(b)
}
return
}
// ByteMapperFunc is used to map a byte to another byte. This can be a stateful or stateless
// function. However you should avoid mutating or reuse of a stateful ByteMapperFunc as correct
// behvior is difficult and error prone to perfect.
type ByteMapperFunc func(byte) byte
// CompileByteMapperFunc prebuilds an array based on the passed ByteMapperFunc. Using this array to
// perform a fast lookup remap of bytes. However as this assumes that the passed ByteMapperFunc is
// idempotent or stateless in nature, it has undefined behvaior if this is not the case.
func CompileByteMapperFunc(byteMapperFunc ByteMapperFunc) ByteMapperFunc {
// This compiles this into a a very fast array lookup
var byteMap [256]byte
for i := range byteMap {
byteMap[i] = byteMapperFunc(byte(i))
}
return func(b byte) byte {
return byteMap[b]
}
}
// CompiledByteMapper prebuilds an array based on the output of the passed ByteMapperFunc, using this
// array as a fast lookup to remap all intercepted bytes. However as this assumes that the passed
// ByteMapperFunc is idempotent or stateless in nature, it has undefined behavr if this is not the
// case.
func CompiledByteMapper(byteMapperFunc ByteMapperFunc) Interceptor {
return NewByteMapper(CompileByteMapperFunc(byteMapperFunc))
}