Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/qrcode/base.py: 82%
62 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from qrcode import constants
3EXP_TABLE = list(range(256))
5LOG_TABLE = list(range(256))
7for i in range(8):
8 EXP_TABLE[i] = 1 << i
10for i in range(8, 256):
11 EXP_TABLE[i] = (
12 EXP_TABLE[i - 4] ^ EXP_TABLE[i - 5] ^ EXP_TABLE[i - 6] ^
13 EXP_TABLE[i - 8])
15for i in range(255):
16 LOG_TABLE[EXP_TABLE[i]] = i
18RS_BLOCK_OFFSET = {
19 constants.ERROR_CORRECT_L: 0,
20 constants.ERROR_CORRECT_M: 1,
21 constants.ERROR_CORRECT_Q: 2,
22 constants.ERROR_CORRECT_H: 3,
23}
25RS_BLOCK_TABLE = (
27 # L
28 # M
29 # Q
30 # H
32 # 1
33 (1, 26, 19),
34 (1, 26, 16),
35 (1, 26, 13),
36 (1, 26, 9),
38 # 2
39 (1, 44, 34),
40 (1, 44, 28),
41 (1, 44, 22),
42 (1, 44, 16),
44 # 3
45 (1, 70, 55),
46 (1, 70, 44),
47 (2, 35, 17),
48 (2, 35, 13),
50 # 4
51 (1, 100, 80),
52 (2, 50, 32),
53 (2, 50, 24),
54 (4, 25, 9),
56 # 5
57 (1, 134, 108),
58 (2, 67, 43),
59 (2, 33, 15, 2, 34, 16),
60 (2, 33, 11, 2, 34, 12),
62 # 6
63 (2, 86, 68),
64 (4, 43, 27),
65 (4, 43, 19),
66 (4, 43, 15),
68 # 7
69 (2, 98, 78),
70 (4, 49, 31),
71 (2, 32, 14, 4, 33, 15),
72 (4, 39, 13, 1, 40, 14),
74 # 8
75 (2, 121, 97),
76 (2, 60, 38, 2, 61, 39),
77 (4, 40, 18, 2, 41, 19),
78 (4, 40, 14, 2, 41, 15),
80 # 9
81 (2, 146, 116),
82 (3, 58, 36, 2, 59, 37),
83 (4, 36, 16, 4, 37, 17),
84 (4, 36, 12, 4, 37, 13),
86 # 10
87 (2, 86, 68, 2, 87, 69),
88 (4, 69, 43, 1, 70, 44),
89 (6, 43, 19, 2, 44, 20),
90 (6, 43, 15, 2, 44, 16),
92 # 11
93 (4, 101, 81),
94 (1, 80, 50, 4, 81, 51),
95 (4, 50, 22, 4, 51, 23),
96 (3, 36, 12, 8, 37, 13),
98 # 12
99 (2, 116, 92, 2, 117, 93),
100 (6, 58, 36, 2, 59, 37),
101 (4, 46, 20, 6, 47, 21),
102 (7, 42, 14, 4, 43, 15),
104 # 13
105 (4, 133, 107),
106 (8, 59, 37, 1, 60, 38),
107 (8, 44, 20, 4, 45, 21),
108 (12, 33, 11, 4, 34, 12),
110 # 14
111 (3, 145, 115, 1, 146, 116),
112 (4, 64, 40, 5, 65, 41),
113 (11, 36, 16, 5, 37, 17),
114 (11, 36, 12, 5, 37, 13),
116 # 15
117 (5, 109, 87, 1, 110, 88),
118 (5, 65, 41, 5, 66, 42),
119 (5, 54, 24, 7, 55, 25),
120 (11, 36, 12, 7, 37, 13),
122 # 16
123 (5, 122, 98, 1, 123, 99),
124 (7, 73, 45, 3, 74, 46),
125 (15, 43, 19, 2, 44, 20),
126 (3, 45, 15, 13, 46, 16),
128 # 17
129 (1, 135, 107, 5, 136, 108),
130 (10, 74, 46, 1, 75, 47),
131 (1, 50, 22, 15, 51, 23),
132 (2, 42, 14, 17, 43, 15),
134 # 18
135 (5, 150, 120, 1, 151, 121),
136 (9, 69, 43, 4, 70, 44),
137 (17, 50, 22, 1, 51, 23),
138 (2, 42, 14, 19, 43, 15),
140 # 19
141 (3, 141, 113, 4, 142, 114),
142 (3, 70, 44, 11, 71, 45),
143 (17, 47, 21, 4, 48, 22),
144 (9, 39, 13, 16, 40, 14),
146 # 20
147 (3, 135, 107, 5, 136, 108),
148 (3, 67, 41, 13, 68, 42),
149 (15, 54, 24, 5, 55, 25),
150 (15, 43, 15, 10, 44, 16),
152 # 21
153 (4, 144, 116, 4, 145, 117),
154 (17, 68, 42),
155 (17, 50, 22, 6, 51, 23),
156 (19, 46, 16, 6, 47, 17),
158 # 22
159 (2, 139, 111, 7, 140, 112),
160 (17, 74, 46),
161 (7, 54, 24, 16, 55, 25),
162 (34, 37, 13),
164 # 23
165 (4, 151, 121, 5, 152, 122),
166 (4, 75, 47, 14, 76, 48),
167 (11, 54, 24, 14, 55, 25),
168 (16, 45, 15, 14, 46, 16),
170 # 24
171 (6, 147, 117, 4, 148, 118),
172 (6, 73, 45, 14, 74, 46),
173 (11, 54, 24, 16, 55, 25),
174 (30, 46, 16, 2, 47, 17),
176 # 25
177 (8, 132, 106, 4, 133, 107),
178 (8, 75, 47, 13, 76, 48),
179 (7, 54, 24, 22, 55, 25),
180 (22, 45, 15, 13, 46, 16),
182 # 26
183 (10, 142, 114, 2, 143, 115),
184 (19, 74, 46, 4, 75, 47),
185 (28, 50, 22, 6, 51, 23),
186 (33, 46, 16, 4, 47, 17),
188 # 27
189 (8, 152, 122, 4, 153, 123),
190 (22, 73, 45, 3, 74, 46),
191 (8, 53, 23, 26, 54, 24),
192 (12, 45, 15, 28, 46, 16),
194 # 28
195 (3, 147, 117, 10, 148, 118),
196 (3, 73, 45, 23, 74, 46),
197 (4, 54, 24, 31, 55, 25),
198 (11, 45, 15, 31, 46, 16),
200 # 29
201 (7, 146, 116, 7, 147, 117),
202 (21, 73, 45, 7, 74, 46),
203 (1, 53, 23, 37, 54, 24),
204 (19, 45, 15, 26, 46, 16),
206 # 30
207 (5, 145, 115, 10, 146, 116),
208 (19, 75, 47, 10, 76, 48),
209 (15, 54, 24, 25, 55, 25),
210 (23, 45, 15, 25, 46, 16),
212 # 31
213 (13, 145, 115, 3, 146, 116),
214 (2, 74, 46, 29, 75, 47),
215 (42, 54, 24, 1, 55, 25),
216 (23, 45, 15, 28, 46, 16),
218 # 32
219 (17, 145, 115),
220 (10, 74, 46, 23, 75, 47),
221 (10, 54, 24, 35, 55, 25),
222 (19, 45, 15, 35, 46, 16),
224 # 33
225 (17, 145, 115, 1, 146, 116),
226 (14, 74, 46, 21, 75, 47),
227 (29, 54, 24, 19, 55, 25),
228 (11, 45, 15, 46, 46, 16),
230 # 34
231 (13, 145, 115, 6, 146, 116),
232 (14, 74, 46, 23, 75, 47),
233 (44, 54, 24, 7, 55, 25),
234 (59, 46, 16, 1, 47, 17),
236 # 35
237 (12, 151, 121, 7, 152, 122),
238 (12, 75, 47, 26, 76, 48),
239 (39, 54, 24, 14, 55, 25),
240 (22, 45, 15, 41, 46, 16),
242 # 36
243 (6, 151, 121, 14, 152, 122),
244 (6, 75, 47, 34, 76, 48),
245 (46, 54, 24, 10, 55, 25),
246 (2, 45, 15, 64, 46, 16),
248 # 37
249 (17, 152, 122, 4, 153, 123),
250 (29, 74, 46, 14, 75, 47),
251 (49, 54, 24, 10, 55, 25),
252 (24, 45, 15, 46, 46, 16),
254 # 38
255 (4, 152, 122, 18, 153, 123),
256 (13, 74, 46, 32, 75, 47),
257 (48, 54, 24, 14, 55, 25),
258 (42, 45, 15, 32, 46, 16),
260 # 39
261 (20, 147, 117, 4, 148, 118),
262 (40, 75, 47, 7, 76, 48),
263 (43, 54, 24, 22, 55, 25),
264 (10, 45, 15, 67, 46, 16),
266 # 40
267 (19, 148, 118, 6, 149, 119),
268 (18, 75, 47, 31, 76, 48),
269 (34, 54, 24, 34, 55, 25),
270 (20, 45, 15, 61, 46, 16)
272)
275def glog(n):
276 if n < 1: # pragma: no cover 276 ↛ 277line 276 didn't jump to line 277, because the condition on line 276 was never true
277 raise ValueError(f"glog({n})")
278 return LOG_TABLE[n]
281def gexp(n):
282 return EXP_TABLE[n % 255]
285class Polynomial:
287 def __init__(self, num, shift):
288 if not num: # pragma: no cover 288 ↛ 289line 288 didn't jump to line 289, because the condition on line 288 was never true
289 raise Exception(f"{len(num)}/{shift}")
291 for offset in range(len(num)): 291 ↛ 295line 291 didn't jump to line 295, because the loop on line 291 didn't complete
292 if num[offset] != 0:
293 break
294 else:
295 offset += 1
297 self.num = num[offset:] + [0] * shift
299 def __getitem__(self, index):
300 return self.num[index]
302 def __iter__(self):
303 return iter(self.num)
305 def __len__(self):
306 return len(self.num)
308 def __mul__(self, other):
309 num = [0] * (len(self) + len(other) - 1)
311 for i, item in enumerate(self):
312 for j, other_item in enumerate(other):
313 num[i + j] ^= gexp(glog(item) + glog(other_item))
315 return Polynomial(num, 0)
317 def __mod__(self, other):
318 difference = len(self) - len(other)
319 if difference < 0:
320 return self
322 ratio = glog(self[0]) - glog(other[0])
324 num = [
325 item ^ gexp(glog(other_item) + ratio)
326 for item, other_item in zip(self, other)]
327 if difference:
328 num.extend(self[-difference:])
330 # recursive call
331 return Polynomial(num, 0) % other
334class RSBlock:
336 def __init__(self, total_count, data_count):
337 self.total_count = total_count
338 self.data_count = data_count
341def rs_blocks(version, error_correction):
342 if error_correction not in RS_BLOCK_OFFSET: # pragma: no cover 342 ↛ 343line 342 didn't jump to line 343, because the condition on line 342 was never true
343 raise Exception(
344 "bad rs block @ version: %s / error_correction: %s" %
345 (version, error_correction))
346 offset = RS_BLOCK_OFFSET[error_correction]
347 rs_block = RS_BLOCK_TABLE[(version - 1) * 4 + offset]
349 blocks = []
351 for i in range(0, len(rs_block), 3):
352 count, total_count, data_count = rs_block[i:i + 3]
353 for _ in range(count):
354 blocks.append(RSBlock(total_count, data_count))
356 return blocks