-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.go
234 lines (200 loc) · 3.9 KB
/
encoding.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package polishedqr
import (
"bytes"
"regexp"
"strconv"
)
type CharacterSet int
const (
Numeric = iota
Alphanumeric
Bytes
// Kanji not currently supported
)
var numericRE = regexp.MustCompile(`\d+`)
var alphanumericRE = regexp.MustCompile(`[\dA-Z\ $%*+\-.\/:]+`)
func AutodetectCharacterSet(data []byte) CharacterSet {
// Feels hacky, but the most sane way to do this
if bytes.Equal(numericRE.Find(data), data) {
return Numeric
} else if bytes.Equal(alphanumericRE.Find(data), data) {
return Alphanumeric
} else {
return Bytes
}
}
type Bits []uint8
func CharacterCountBitCapacity(mode CharacterSet, version int) (bitCapacity int) {
// Different versions and encodings have different numbers of bits
// in the character count
if version < 10 {
switch mode {
case Numeric:
bitCapacity = 10
case Alphanumeric:
bitCapacity = 9
case Bytes:
bitCapacity = 8
}
} else if version < 27 {
switch mode {
case Numeric:
bitCapacity = 12
case Alphanumeric:
bitCapacity = 11
case Bytes:
bitCapacity = 16
}
} else {
switch mode {
case Numeric:
bitCapacity = 14
case Alphanumeric:
bitCapacity = 13
case Bytes:
bitCapacity = 16
}
}
return
}
func CharacterCount(count int, mode CharacterSet, version int) Bits {
bitCapacity := CharacterCountBitCapacity(mode, version)
var out Bits
for i := bitCapacity - 1; i >= 0; i-- {
// cursed masking
out = append(out, uint8((count&int(1<<i))>>i))
}
return out
}
func ConvertToNumeric(data []byte, version int) Bits {
var b Bits
// Add mode indicator
b = append(b, Bits{0, 0, 0, 1}...)
// Add character count
b = append(b, CharacterCount(len(data), Numeric, version)...)
// Divide into groups of three digits and convert to bits
for i := 0; i < len(data); i += 3 {
// Group digits
var g []byte
if len(data)-i < 3 {
g = data[i:]
} else {
g = data[i : i+3]
}
// Convert group
gi, err := strconv.Atoi(string(g))
if err != nil {
panic(err)
}
// Small groups are encoded in less bits
bc := 10 - 1
if len(g) == 2 {
bc = 7 - 1
} else if len(g) == 1 {
bc = 4 - 1
}
// Add bits
for i := bc; i >= 0; i-- {
b = append(b, uint8((gi&int(1<<i))>>i))
}
}
return b
}
func ConvertToAlphanumeric(data []byte, version int) Bits {
var b Bits
// Add mode indicator
b = append(b, Bits{0, 0, 1, 0}...)
// Add character count
b = append(b, CharacterCount(len(data), Alphanumeric, version)...)
// Divide into groups of three digits and convert to bits
for i := 0; i < len(data); i += 2 {
// Group digits
var g []byte
if len(data)-i < 2 {
g = data[i:]
} else {
g = data[i : i+2]
}
// Convert group
var gi int
if len(g) > 1 {
gi = 45 * alphanumericTable[g[0]]
gi += alphanumericTable[g[1]]
} else {
gi = alphanumericTable[g[0]]
}
// Small groups are encoded in less bits
bc := 11 - 1
if len(g) == 1 {
bc = 6 - 1
}
// Add bits
for i := bc; i >= 0; i-- {
b = append(b, uint8((gi&int(1<<i))>>i))
}
}
return b
}
func ConvertToBytes(data []byte, version int) Bits {
var b Bits
// Add mode indicator
b = append(b, Bits{0, 1, 0, 0}...)
// Add character count
b = append(b, CharacterCount(len(data), Bytes, version)...)
// Convert each byte into bits
for _, v := range data {
for i := 7; i >= 0; i-- {
b = append(b, uint8((v&(1<<i)))>>i)
}
}
return b
}
// I love manually encoding tables into code by hand
var alphanumericTable = map[byte]int{
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'A': 10,
'B': 11,
'C': 12,
'D': 13,
'E': 14,
'F': 15,
'G': 16,
'H': 17,
'I': 18,
'J': 19,
'K': 20,
'L': 21,
'M': 22,
'N': 23,
'O': 24,
'P': 25,
'Q': 26,
'R': 27,
'S': 28,
'T': 29,
'U': 30,
'V': 31,
'W': 32,
'X': 33,
'Y': 34,
'Z': 35,
' ': 36,
'$': 37,
'%': 38,
'*': 39,
'+': 40,
'-': 41,
'.': 42,
'/': 43,
':': 44,
}
var alphanumericTableReverse = reverseMap(alphanumericTable)