-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
217 lines (176 loc) · 5.72 KB
/
data.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
package qrcode
import (
"fmt"
"math"
"qrcode/encode"
)
// Level of error correction
// Low - 7%
// Medium - 15%
// Quartile - 25%
// High - 30%
type ErrorCorrectionLevel int
const (
ErrorCorrectionLevelLow ErrorCorrectionLevel = iota
ErrorCorrectionLevelMedium
ErrorCorrectionLevelQuartile
ErrorCorrectionLevelHigh
)
var ErrContentTooLong = fmt.Errorf("content is too long")
// isVersionEnough checks if the given version can contain the data
func isVersionEnough(encodeBlocks []*encode.EncodeBlock, version int, dataSize int, ecl ErrorCorrectionLevel) (bool, error) {
prefixBits := 0
for _, block := range encodeBlocks {
lengthBits, err := block.GetLengthBits(version)
if err != nil {
return false, fmt.Errorf("failed to get length bits: %w", err)
}
prefixBits += lengthBits + block.GetModeBits(version)
}
size := int(math.Ceil(float64(dataSize+prefixBits) / 8.0))
var dataCodewords int
if version < 0 {
codewords := microCodewordsCount[-version]
errorCodewords := microErrorCorrectionCodeWords[-version][ecl]
if errorCodewords == 0 {
return false, fmt.Errorf("unsupported error correction level: %v", ecl)
}
dataCodewords = codewords - errorCodewords
} else {
dataCodewords = codewordsCount[version] - errorCorrectionCodeWords[version][ecl]
}
return size <= dataCodewords, nil
}
// calculateMinVersion returns the minimum version for the given content, encoding mode, and error correction level.
// Alghorithm: iterate over versions from 1 to 40 (from M1 to M4 for MicroQR) and return the first version that can contain the content.
func calculateMinVersion(encodeBlocks []*encode.EncodeBlock, ecl ErrorCorrectionLevel, microQR bool) (int, error) {
dataSize := 0
for _, block := range encodeBlocks {
blockSize, err := block.CalculateDataBitsCount()
if err != nil {
return 0, fmt.Errorf("failed to calculate data bits count: %w", err)
}
dataSize += blockSize
}
start, end, step := 1, 40, 1
if microQR {
start, end, step = -1, -4, -1
}
// For Normal QRs: 1 to 40
// For Micro QRs: -1 to -4
for version := start; version != end+step; version += step {
ok, _ := isVersionEnough(encodeBlocks, version, dataSize, ecl)
// if err != nil {
// return 0, fmt.Errorf("failed to check version: %w", err)
// }
if ok {
return version, nil
}
}
return 0, ErrContentTooLong
}
// rearrangeDataBlocks rearranges the data blocks according to the QR code specification.
// When the QR code is split into data blocks, the data stream should be rearranged.
func rearrangeDataBlocks(data []byte, version int, errorLevel ErrorCorrectionLevel) []byte {
var blocks []ecBlock
if version < 0 {
blocks = microErrorCorrectionBlocks[-version][errorLevel]
} else {
blocks = errorCorrectionBlocks[version][errorLevel]
}
var blocksData [][]byte
dataIdx := 0
for _, block := range blocks {
for i := 0; i < block.Blocks; i++ {
blocksData = append(blocksData, data[dataIdx:dataIdx+block.DataCodewords])
dataIdx += block.DataCodewords
}
}
var buf []byte
maxBlockSize := 0
for i := 0; i < len(blocksData); i++ {
if len(blocksData[i]) > maxBlockSize {
maxBlockSize = len(blocksData[i])
}
}
for i := 0; i < maxBlockSize; i++ {
for j := 0; j < len(blocksData); j++ {
if i < len(blocksData[j]) {
buf = append(buf, blocksData[j][i])
}
}
}
return buf
}
// fillTerminator fills the data with terminator and padding bits based on the QR code specification.
func fillTerminator(data []byte, remainedBits int, version int, errorLevel ErrorCorrectionLevel) []byte {
var availableCodewords int
terminatorBits := 4
// Micro QR Codes
if version < 0 {
availableCodewords = microCodewordsCount[-version] - microErrorCorrectionCodeWords[-version][errorLevel]
terminatorBits = -version*2 + 1
} else {
availableCodewords = codewordsCount[version] - errorCorrectionCodeWords[version][errorLevel]
}
if remainedBits < terminatorBits && len(data) < availableCodewords {
data = append(data, 0)
}
// TODO: refactor
if remainedBits == 0 && terminatorBits == 9 && len(data) < availableCodewords {
data = append(data, 0)
}
var terminator byte = 0b11101100
hasEmptyCodewords := false
for len(data) < availableCodewords {
hasEmptyCodewords = true
data = append(data, terminator)
if terminator == 0b11101100 {
terminator = 0b00010001
} else {
terminator = 0b11101100
}
}
if hasEmptyCodewords && (version == -1 || version == -3) {
data[len(data)-1] = 0
}
return data
}
// getBytesData returns the byte array for the given content, encoding mode, error correction level, and version.
func getBytesData(blocks []*encode.EncodeBlock, errorLevel ErrorCorrectionLevel, version int) ([]byte, error) {
allBits := 0
queue := make(chan encode.ValueBlock, 100)
result := make(chan []byte)
go encode.GenerateData(queue, result)
for _, block := range blocks {
blockBits, err := block.Encode(version, queue)
if err != nil {
return nil, fmt.Errorf("failed to encode data: %w", err)
}
allBits += blockBits
}
close(queue)
data := <-result
// add terminator
remainedBits := len(data)*8 - allBits
data = fillTerminator(data, remainedBits, version, errorLevel)
errorData := getEDCData(data, version, errorLevel)
data = rearrangeDataBlocks(data, version, errorLevel)
data = append(data, errorData...)
return data, nil
}
var (
// Number of codewords for Micro QR Code version
microCodewordsCount = [5]int{
0, // added for shift start index to 1
5, 10, 17, 24,
}
// Number of codewords for each version
codewordsCount = [41]int{
0, // added for shift start index to 1
26, 44, 70, 100, 134, 172, 196, 242, 292, 346,
404, 466, 532, 581, 655, 733, 815, 901, 991, 1085,
1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185,
2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706,
}
)