You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
17 KiB

4 years ago
  1. import re
  2. import math
  3. import six
  4. from six.moves import xrange
  5. from qrcode import base, exceptions, LUT
  6. # QR encoding modes.
  7. MODE_NUMBER = 1 << 0
  8. MODE_ALPHA_NUM = 1 << 1
  9. MODE_8BIT_BYTE = 1 << 2
  10. MODE_KANJI = 1 << 3
  11. # Encoding mode sizes.
  12. MODE_SIZE_SMALL = {
  13. MODE_NUMBER: 10,
  14. MODE_ALPHA_NUM: 9,
  15. MODE_8BIT_BYTE: 8,
  16. MODE_KANJI: 8,
  17. }
  18. MODE_SIZE_MEDIUM = {
  19. MODE_NUMBER: 12,
  20. MODE_ALPHA_NUM: 11,
  21. MODE_8BIT_BYTE: 16,
  22. MODE_KANJI: 10,
  23. }
  24. MODE_SIZE_LARGE = {
  25. MODE_NUMBER: 14,
  26. MODE_ALPHA_NUM: 13,
  27. MODE_8BIT_BYTE: 16,
  28. MODE_KANJI: 12,
  29. }
  30. ALPHA_NUM = six.b('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:')
  31. RE_ALPHA_NUM = re.compile(six.b('^[') + re.escape(ALPHA_NUM) + six.b(']*\Z'))
  32. # The number of bits for numeric delimited data lengths.
  33. NUMBER_LENGTH = {3: 10, 2: 7, 1: 4}
  34. PATTERN_POSITION_TABLE = [
  35. [],
  36. [6, 18],
  37. [6, 22],
  38. [6, 26],
  39. [6, 30],
  40. [6, 34],
  41. [6, 22, 38],
  42. [6, 24, 42],
  43. [6, 26, 46],
  44. [6, 28, 50],
  45. [6, 30, 54],
  46. [6, 32, 58],
  47. [6, 34, 62],
  48. [6, 26, 46, 66],
  49. [6, 26, 48, 70],
  50. [6, 26, 50, 74],
  51. [6, 30, 54, 78],
  52. [6, 30, 56, 82],
  53. [6, 30, 58, 86],
  54. [6, 34, 62, 90],
  55. [6, 28, 50, 72, 94],
  56. [6, 26, 50, 74, 98],
  57. [6, 30, 54, 78, 102],
  58. [6, 28, 54, 80, 106],
  59. [6, 32, 58, 84, 110],
  60. [6, 30, 58, 86, 114],
  61. [6, 34, 62, 90, 118],
  62. [6, 26, 50, 74, 98, 122],
  63. [6, 30, 54, 78, 102, 126],
  64. [6, 26, 52, 78, 104, 130],
  65. [6, 30, 56, 82, 108, 134],
  66. [6, 34, 60, 86, 112, 138],
  67. [6, 30, 58, 86, 114, 142],
  68. [6, 34, 62, 90, 118, 146],
  69. [6, 30, 54, 78, 102, 126, 150],
  70. [6, 24, 50, 76, 102, 128, 154],
  71. [6, 28, 54, 80, 106, 132, 158],
  72. [6, 32, 58, 84, 110, 136, 162],
  73. [6, 26, 54, 82, 110, 138, 166],
  74. [6, 30, 58, 86, 114, 142, 170]
  75. ]
  76. G15 = (
  77. (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) |
  78. (1 << 0))
  79. G18 = (
  80. (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) |
  81. (1 << 2) | (1 << 0))
  82. G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1)
  83. PAD0 = 0xEC
  84. PAD1 = 0x11
  85. # Precompute bit count limits, indexed by error correction level and code size
  86. _data_count = lambda block: block.data_count
  87. BIT_LIMIT_TABLE = [
  88. [0] + [8*sum(map(_data_count, base.rs_blocks(version, error_correction)))
  89. for version in xrange(1, 41)]
  90. for error_correction in xrange(4)
  91. ]
  92. def BCH_type_info(data):
  93. d = data << 10
  94. while BCH_digit(d) - BCH_digit(G15) >= 0:
  95. d ^= (G15 << (BCH_digit(d) - BCH_digit(G15)))
  96. return ((data << 10) | d) ^ G15_MASK
  97. def BCH_type_number(data):
  98. d = data << 12
  99. while BCH_digit(d) - BCH_digit(G18) >= 0:
  100. d ^= (G18 << (BCH_digit(d) - BCH_digit(G18)))
  101. return (data << 12) | d
  102. def BCH_digit(data):
  103. digit = 0
  104. while data != 0:
  105. digit += 1
  106. data >>= 1
  107. return digit
  108. def pattern_position(version):
  109. return PATTERN_POSITION_TABLE[version - 1]
  110. def mask_func(pattern):
  111. """
  112. Return the mask function for the given mask pattern.
  113. """
  114. if pattern == 0: # 000
  115. return lambda i, j: (i + j) % 2 == 0
  116. if pattern == 1: # 001
  117. return lambda i, j: i % 2 == 0
  118. if pattern == 2: # 010
  119. return lambda i, j: j % 3 == 0
  120. if pattern == 3: # 011
  121. return lambda i, j: (i + j) % 3 == 0
  122. if pattern == 4: # 100
  123. return lambda i, j: (math.floor(i / 2) + math.floor(j / 3)) % 2 == 0
  124. if pattern == 5: # 101
  125. return lambda i, j: (i * j) % 2 + (i * j) % 3 == 0
  126. if pattern == 6: # 110
  127. return lambda i, j: ((i * j) % 2 + (i * j) % 3) % 2 == 0
  128. if pattern == 7: # 111
  129. return lambda i, j: ((i * j) % 3 + (i + j) % 2) % 2 == 0
  130. raise TypeError("Bad mask pattern: " + pattern) # pragma: no cover
  131. def mode_sizes_for_version(version):
  132. if version < 10:
  133. return MODE_SIZE_SMALL
  134. elif version < 27:
  135. return MODE_SIZE_MEDIUM
  136. else:
  137. return MODE_SIZE_LARGE
  138. def length_in_bits(mode, version):
  139. if mode not in (
  140. MODE_NUMBER, MODE_ALPHA_NUM, MODE_8BIT_BYTE, MODE_KANJI):
  141. raise TypeError("Invalid mode (%s)" % mode) # pragma: no cover
  142. if version < 1 or version > 40: # pragma: no cover
  143. raise ValueError(
  144. "Invalid version (was %s, expected 1 to 40)" % version)
  145. return mode_sizes_for_version(version)[mode]
  146. def lost_point(modules):
  147. modules_count = len(modules)
  148. lost_point = 0
  149. lost_point = _lost_point_level1(modules, modules_count)
  150. lost_point += _lost_point_level2(modules, modules_count)
  151. lost_point += _lost_point_level3(modules, modules_count)
  152. lost_point += _lost_point_level4(modules, modules_count)
  153. return lost_point
  154. def _lost_point_level1(modules, modules_count):
  155. lost_point = 0
  156. modules_range = xrange(modules_count)
  157. container = [0] * (modules_count + 1)
  158. for row in modules_range:
  159. this_row = modules[row]
  160. previous_color = this_row[0]
  161. length = 0
  162. for col in modules_range:
  163. if this_row[col] == previous_color:
  164. length += 1
  165. else:
  166. if length >= 5:
  167. container[length] += 1
  168. length = 1
  169. previous_color = this_row[col]
  170. if length >= 5:
  171. container[length] += 1
  172. for col in modules_range:
  173. previous_color = modules[0][col]
  174. length = 0
  175. for row in modules_range:
  176. if modules[row][col] == previous_color:
  177. length += 1
  178. else:
  179. if length >= 5:
  180. container[length] += 1
  181. length = 1
  182. previous_color = modules[row][col]
  183. if length >= 5:
  184. container[length] += 1
  185. lost_point += sum(container[each_length] * (each_length - 2)
  186. for each_length in xrange(5, modules_count + 1))
  187. return lost_point
  188. def _lost_point_level2(modules, modules_count):
  189. lost_point = 0
  190. modules_range = xrange(modules_count - 1)
  191. for row in modules_range:
  192. this_row = modules[row]
  193. next_row = modules[row + 1]
  194. # use iter() and next() to skip next four-block. e.g.
  195. # d a f if top-right a != b botton-right,
  196. # c b e then both abcd and abef won't lost any point.
  197. modules_range_iter = iter(modules_range)
  198. for col in modules_range_iter:
  199. top_right = this_row[col + 1]
  200. if top_right != next_row[col + 1]:
  201. # reduce 33.3% of runtime via next().
  202. # None: raise nothing if there is no next item.
  203. next(modules_range_iter, None)
  204. elif top_right != this_row[col]:
  205. continue
  206. elif top_right != next_row[col]:
  207. continue
  208. else:
  209. lost_point += 3
  210. return lost_point
  211. def _lost_point_level3(modules, modules_count):
  212. # 1 : 1 : 3 : 1 : 1 ratio (dark:light:dark:light:dark) pattern in
  213. # row/column, preceded or followed by light area 4 modules wide. From ISOIEC.
  214. # pattern1: 10111010000
  215. # pattern2: 00001011101
  216. modules_range = xrange(modules_count)
  217. modules_range_short = xrange(modules_count-10)
  218. lost_point = 0
  219. for row in modules_range:
  220. this_row = modules[row]
  221. modules_range_short_iter = iter(modules_range_short)
  222. col = 0
  223. for col in modules_range_short_iter:
  224. if (
  225. not this_row[col + 1]
  226. and this_row[col + 4]
  227. and not this_row[col + 5]
  228. and this_row[col + 6]
  229. and not this_row[col + 9]
  230. and (
  231. this_row[col + 0]
  232. and this_row[col + 2]
  233. and this_row[col + 3]
  234. and not this_row[col + 7]
  235. and not this_row[col + 8]
  236. and not this_row[col + 10]
  237. or
  238. not this_row[col + 0]
  239. and not this_row[col + 2]
  240. and not this_row[col + 3]
  241. and this_row[col + 7]
  242. and this_row[col + 8]
  243. and this_row[col + 10]
  244. )
  245. ):
  246. lost_point += 40
  247. # horspool algorithm.
  248. # if this_row[col + 10] == True, pattern1 shift 4, pattern2 shift 2. So min=2.
  249. # if this_row[col + 10] == False, pattern1 shift 1, pattern2 shift 1. So min=1.
  250. if this_row[col + 10]:
  251. next(modules_range_short_iter, None)
  252. for col in modules_range:
  253. modules_range_short_iter = iter(modules_range_short)
  254. row = 0
  255. for row in modules_range_short_iter:
  256. if (
  257. not modules[row + 1][col]
  258. and modules[row + 4][col]
  259. and not modules[row + 5][col]
  260. and modules[row + 6][col]
  261. and not modules[row + 9][col]
  262. and (
  263. modules[row + 0][col]
  264. and modules[row + 2][col]
  265. and modules[row + 3][col]
  266. and not modules[row + 7][col]
  267. and not modules[row + 8][col]
  268. and not modules[row + 10][col]
  269. or
  270. not modules[row + 0][col]
  271. and not modules[row + 2][col]
  272. and not modules[row + 3][col]
  273. and modules[row + 7][col]
  274. and modules[row + 8][col]
  275. and modules[row + 10][col]
  276. )
  277. ):
  278. lost_point += 40
  279. if modules[row + 10][col]:
  280. next(modules_range_short_iter, None)
  281. return lost_point
  282. def _lost_point_level4(modules, modules_count):
  283. dark_count = sum(map(sum, modules))
  284. percent = float(dark_count) / (modules_count**2)
  285. # Every 5% departure from 50%, rating++
  286. rating = int(abs(percent * 100 - 50) / 5)
  287. return rating * 10
  288. def optimal_data_chunks(data, minimum=4):
  289. """
  290. An iterator returning QRData chunks optimized to the data content.
  291. :param minimum: The minimum number of bytes in a row to split as a chunk.
  292. """
  293. data = to_bytestring(data)
  294. re_repeat = (
  295. six.b('{') + six.text_type(minimum).encode('ascii') + six.b(',}'))
  296. num_pattern = re.compile(six.b('\d') + re_repeat)
  297. num_bits = _optimal_split(data, num_pattern)
  298. alpha_pattern = re.compile(
  299. six.b('[') + re.escape(ALPHA_NUM) + six.b(']') + re_repeat)
  300. for is_num, chunk in num_bits:
  301. if is_num:
  302. yield QRData(chunk, mode=MODE_NUMBER, check_data=False)
  303. else:
  304. for is_alpha, sub_chunk in _optimal_split(chunk, alpha_pattern):
  305. if is_alpha:
  306. mode = MODE_ALPHA_NUM
  307. else:
  308. mode = MODE_8BIT_BYTE
  309. yield QRData(sub_chunk, mode=mode, check_data=False)
  310. def _optimal_split(data, pattern):
  311. while data:
  312. match = re.search(pattern, data)
  313. if not match:
  314. break
  315. start, end = match.start(), match.end()
  316. if start:
  317. yield False, data[:start]
  318. yield True, data[start:end]
  319. data = data[end:]
  320. if data:
  321. yield False, data
  322. def to_bytestring(data):
  323. """
  324. Convert data to a (utf-8 encoded) byte-string if it isn't a byte-string
  325. already.
  326. """
  327. if not isinstance(data, six.binary_type):
  328. data = six.text_type(data).encode('utf-8')
  329. return data
  330. def optimal_mode(data):
  331. """
  332. Calculate the optimal mode for this chunk of data.
  333. """
  334. if data.isdigit():
  335. return MODE_NUMBER
  336. if RE_ALPHA_NUM.match(data):
  337. return MODE_ALPHA_NUM
  338. return MODE_8BIT_BYTE
  339. class QRData:
  340. """
  341. Data held in a QR compatible format.
  342. Doesn't currently handle KANJI.
  343. """
  344. def __init__(self, data, mode=None, check_data=True):
  345. """
  346. If ``mode`` isn't provided, the most compact QR data type possible is
  347. chosen.
  348. """
  349. if check_data:
  350. data = to_bytestring(data)
  351. if mode is None:
  352. self.mode = optimal_mode(data)
  353. else:
  354. self.mode = mode
  355. if mode not in (MODE_NUMBER, MODE_ALPHA_NUM, MODE_8BIT_BYTE):
  356. raise TypeError("Invalid mode (%s)" % mode) # pragma: no cover
  357. if check_data and mode < optimal_mode(data): # pragma: no cover
  358. raise ValueError(
  359. "Provided data can not be represented in mode "
  360. "{0}".format(mode))
  361. self.data = data
  362. def __len__(self):
  363. return len(self.data)
  364. def write(self, buffer):
  365. if self.mode == MODE_NUMBER:
  366. for i in xrange(0, len(self.data), 3):
  367. chars = self.data[i:i + 3]
  368. bit_length = NUMBER_LENGTH[len(chars)]
  369. buffer.put(int(chars), bit_length)
  370. elif self.mode == MODE_ALPHA_NUM:
  371. for i in xrange(0, len(self.data), 2):
  372. chars = self.data[i:i + 2]
  373. if len(chars) > 1:
  374. buffer.put(
  375. ALPHA_NUM.find(chars[0]) * 45 +
  376. ALPHA_NUM.find(chars[1]), 11)
  377. else:
  378. buffer.put(ALPHA_NUM.find(chars), 6)
  379. else:
  380. if six.PY3:
  381. # Iterating a bytestring in Python 3 returns an integer,
  382. # no need to ord().
  383. data = self.data
  384. else:
  385. data = [ord(c) for c in self.data]
  386. for c in data:
  387. buffer.put(c, 8)
  388. def __repr__(self):
  389. return repr(self.data)
  390. class BitBuffer:
  391. def __init__(self):
  392. self.buffer = []
  393. self.length = 0
  394. def __repr__(self):
  395. return ".".join([str(n) for n in self.buffer])
  396. def get(self, index):
  397. buf_index = math.floor(index / 8)
  398. return ((self.buffer[buf_index] >> (7 - index % 8)) & 1) == 1
  399. def put(self, num, length):
  400. for i in range(length):
  401. self.put_bit(((num >> (length - i - 1)) & 1) == 1)
  402. def __len__(self):
  403. return self.length
  404. def put_bit(self, bit):
  405. buf_index = self.length // 8
  406. if len(self.buffer) <= buf_index:
  407. self.buffer.append(0)
  408. if bit:
  409. self.buffer[buf_index] |= (0x80 >> (self.length % 8))
  410. self.length += 1
  411. def create_bytes(buffer, rs_blocks):
  412. offset = 0
  413. maxDcCount = 0
  414. maxEcCount = 0
  415. dcdata = [0] * len(rs_blocks)
  416. ecdata = [0] * len(rs_blocks)
  417. for r in range(len(rs_blocks)):
  418. dcCount = rs_blocks[r].data_count
  419. ecCount = rs_blocks[r].total_count - dcCount
  420. maxDcCount = max(maxDcCount, dcCount)
  421. maxEcCount = max(maxEcCount, ecCount)
  422. dcdata[r] = [0] * dcCount
  423. for i in range(len(dcdata[r])):
  424. dcdata[r][i] = 0xff & buffer.buffer[i + offset]
  425. offset += dcCount
  426. # Get error correction polynomial.
  427. if ecCount in LUT.rsPoly_LUT:
  428. rsPoly = base.Polynomial(LUT.rsPoly_LUT[ecCount], 0)
  429. else:
  430. rsPoly = base.Polynomial([1], 0)
  431. for i in range(ecCount):
  432. rsPoly = rsPoly * base.Polynomial([1, base.gexp(i)], 0)
  433. rawPoly = base.Polynomial(dcdata[r], len(rsPoly) - 1)
  434. modPoly = rawPoly % rsPoly
  435. ecdata[r] = [0] * (len(rsPoly) - 1)
  436. for i in range(len(ecdata[r])):
  437. modIndex = i + len(modPoly) - len(ecdata[r])
  438. if (modIndex >= 0):
  439. ecdata[r][i] = modPoly[modIndex]
  440. else:
  441. ecdata[r][i] = 0
  442. totalCodeCount = 0
  443. for rs_block in rs_blocks:
  444. totalCodeCount += rs_block.total_count
  445. data = [None] * totalCodeCount
  446. index = 0
  447. for i in range(maxDcCount):
  448. for r in range(len(rs_blocks)):
  449. if i < len(dcdata[r]):
  450. data[index] = dcdata[r][i]
  451. index += 1
  452. for i in range(maxEcCount):
  453. for r in range(len(rs_blocks)):
  454. if i < len(ecdata[r]):
  455. data[index] = ecdata[r][i]
  456. index += 1
  457. return data
  458. def create_data(version, error_correction, data_list):
  459. buffer = BitBuffer()
  460. for data in data_list:
  461. buffer.put(data.mode, 4)
  462. buffer.put(len(data), length_in_bits(data.mode, version))
  463. data.write(buffer)
  464. # Calculate the maximum number of bits for the given version.
  465. rs_blocks = base.rs_blocks(version, error_correction)
  466. bit_limit = 0
  467. for block in rs_blocks:
  468. bit_limit += block.data_count * 8
  469. if len(buffer) > bit_limit:
  470. raise exceptions.DataOverflowError(
  471. "Code length overflow. Data size (%s) > size available (%s)" %
  472. (len(buffer), bit_limit))
  473. # Terminate the bits (add up to four 0s).
  474. for i in range(min(bit_limit - len(buffer), 4)):
  475. buffer.put_bit(False)
  476. # Delimit the string into 8-bit words, padding with 0s if necessary.
  477. delimit = len(buffer) % 8
  478. if delimit:
  479. for i in range(8 - delimit):
  480. buffer.put_bit(False)
  481. # Add special alternating padding bitstrings until buffer is full.
  482. bytes_to_fill = (bit_limit - len(buffer)) // 8
  483. for i in range(bytes_to_fill):
  484. if i % 2 == 0:
  485. buffer.put(PAD0, 8)
  486. else:
  487. buffer.put(PAD1, 8)
  488. return create_bytes(buffer, rs_blocks)