-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencoder.go
More file actions
99 lines (88 loc) · 2.24 KB
/
encoder.go
File metadata and controls
99 lines (88 loc) · 2.24 KB
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
package decouplet
import (
"crypto/rand"
"errors"
"io"
"math/big"
)
var (
ErrorMatchNotFound = errors.New("match not found")
ErrorDecodeNotFound = errors.New("valid decode character not found")
ErrorKeyCastFailed = errors.New("failed to cast key")
ErrorDecodeGeneric = errors.New("decode error")
ErrorDecodeGroup = errors.New("decode groups missing locations")
ErrorInvalidKey = errors.New("invalid key")
ErrorBytesInvalidKeyLength = errors.New("invalid key length, must be between 32 and 64 bytes")
ErrorImageKeyTooSmall = errors.New("key needs to be larger than 300x300")
)
const (
stxByte byte = 0x02 // Start of Text
etxByte byte = 0x03 // End of Text
)
type Encoder interface {
Encode(io.Reader, io.Writer) error
Decode(io.Reader, io.Writer) error
Validate() error
}
func startEncode(w io.Writer) error {
_, err := w.Write([]byte{stxByte})
return err
}
func finishEncode(w io.Writer) error {
_, err := w.Write([]byte{etxByte})
return err
}
func readAndVerifyStart(r io.Reader) error {
start := make([]byte, 1)
if _, err := io.ReadFull(r, start); err != nil {
return err
}
if start[0] != stxByte {
return ErrorDecodeNotFound
}
return nil
}
func readAndCheckETX(r io.Reader, buffer []byte, recordSize int) (bool, error) {
n, err := io.ReadFull(r, buffer[:recordSize])
if err == io.EOF || err == io.ErrUnexpectedEOF {
// If we only got 1 byte and it's ETX, that's the end
if n == 1 && buffer[0] == etxByte {
return true, nil
}
if n == 0 {
return false, io.EOF
}
return false, ErrorDecodeNotFound
}
if err != nil {
return false, err
}
return false, nil
}
func getRandomInt(max int64) (int, error) {
bMax := big.NewInt(max)
n, err := rand.Int(rand.Reader, bMax)
if err != nil {
return 0, err
}
return int(n.Int64()), nil
}
func getRandomPair(bounds int64) (int, int, error) {
x1, err := getRandomInt(bounds)
if err != nil {
return 0, 0, err
}
x2, err := getRandomInt(bounds)
if err != nil {
return 0, 0, err
}
return x1, x2, nil
}
func checkMatch(b byte, x, y uint16) (bool, uint8) {
for supplement := 0; supplement < 256; supplement++ {
if b == byte((x-y+uint16(supplement)+256)%256) {
return true, uint8(supplement)
}
}
return false, 0
}