-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
161 lines (130 loc) · 2.92 KB
/
util.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
155
156
157
158
159
160
161
package ar8t
import (
"bytes"
)
// I don't like the file's name, we'll see about it
type QRLocation struct {
TopLeft, TopRight, BottomLeft Point
ModuleSize float64 //in pixels
Version uint32 //1 .. 40
}
type QRData struct {
/// QR Pixel Data in side x side pixels, stored in row major order. Using the provided index will convert into 1's and 0's.
Data []byte
/// Version of the QR Code, 1 being the smallest, 40 the largest
Version uint32
/// Side in pixels of the QR square
Side uint32
}
func (d QRData) Index(x, y uint32) byte {
index := y*d.Side + x
if int(index) >= len(d.Data) {
return 1
}
switch d.Data[index] {
case 0:
return 1
default:
return 0
}
}
type ECLevel int
const (
ECLevelLow ECLevel = iota
ECLevelMedium
ECLevelQuartile
ECLevelHigh
)
type Chomp struct {
bytes *bytes.Reader
bitsLeft uint32
currentByte byte
currentByteValid bool
bitsLeftInByte uint8
}
// NewChomp creates a chomper dependant on bytes
func NewChomp(b []byte) *Chomp {
ch := &Chomp{}
ch.bitsLeft = uint32(len(b) * 8)
ch.bytes = bytes.NewReader(b)
var err error
ch.currentByte, err = ch.bytes.ReadByte()
// otherwise it's EOF
if err == nil {
ch.bitsLeftInByte = 8
ch.currentByteValid = true
}
return ch
}
func (ch *Chomp) Chomp(nBits uint8) (result byte, ok bool) {
bitCount := nBits
if bitCount > 8 && bitCount < 1 ||
uint32(bitCount) > ch.bitsLeft {
return
}
switch {
case bitCount < ch.bitsLeftInByte:
return ch.nibble(nBits)
case bitCount == ch.bitsLeftInByte:
if ch.currentByteValid {
result = ch.currentByte >> (8 - ch.bitsLeftInByte)
}
ch.bitsLeft -= uint32(ch.bitsLeftInByte)
ch.bitsLeftInByte = 0
ch.currentByteValid = false
var err error
ch.currentByte, err = ch.bytes.ReadByte()
if err == nil {
ch.bitsLeftInByte = 8
ch.currentByteValid = true
}
ok = true
return
default:
bitsToGo := bitCount - ch.bitsLeftInByte
if ch.currentByteValid {
result = ch.currentByte >> (8 - ch.bitsLeftInByte) << bitsToGo
}
ch.bitsLeft -= uint32(ch.bitsLeftInByte)
var err error
ch.currentByte, err = ch.bytes.ReadByte()
if err != nil {
ch.currentByteValid = false
ch.bitsLeftInByte = 0
return
}
ch.bitsLeftInByte = 8
nibble, _ := ch.nibble(bitsToGo)
return result + nibble, true
}
}
func (ch *Chomp) ChompUint16(nBits uint8) (uint16, bool) {
var (
result uint16
initialResult byte
ok bool
)
for nBits > 8 {
initialResult, ok = ch.Chomp(8)
if !ok {
return 0, false
}
result = uint16(initialResult) << (nBits - 8)
nBits -= 8
}
initialResult, ok = ch.Chomp(nBits)
if !ok {
return 0, false
}
return result + uint16(initialResult), true
}
func (ch *Chomp) nibble(nBits uint8) (result byte, ok bool) {
if !ch.currentByteValid {
return
}
result = ch.currentByte >> (8 - nBits)
ch.currentByte <<= nBits
ch.bitsLeftInByte -= nBits
ok = true
return
}