Debian Codegraph @

src/Common/FormatInformation.php

raw
1 <?php
2 /**
3 * BaconQrCode
4 *
5 * @link http://github.com/Bacon/BaconQrCode For the canonical source repository
6 * @copyright 2013 Ben 'DASPRiD' Scholzen
7 * @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8 */
9
10 namespace BaconQrCode\Common;
11
12 /**
13 * Encapsulates a QR Code's format information, including the data mask used and error correction level.
14 */
15 class FormatInformation
16 {
17 /**
18 * Mask for format information.
19 */
20 private const FORMAT_INFO_MASK_QR = 0x5412;
21
22 /**
23 * Lookup table for decoding format information.
24 *
25 * See ISO 18004:2006, Annex C, Table C.1
26 */
27 private const FORMAT_INFO_DECODE_LOOKUP = [
28 [0x5412, 0x00],
29 [0x5125, 0x01],
30 [0x5e7c, 0x02],
31 [0x5b4b, 0x03],
32 [0x45f9, 0x04],
33 [0x40ce, 0x05],
34 [0x4f97, 0x06],
35 [0x4aa0, 0x07],
36 [0x77c4, 0x08],
37 [0x72f3, 0x09],
38 [0x7daa, 0x0a],
39 [0x789d, 0x0b],
40 [0x662f, 0x0c],
41 [0x6318, 0x0d],
42 [0x6c41, 0x0e],
43 [0x6976, 0x0f],
44 [0x1689, 0x10],
45 [0x13be, 0x11],
46 [0x1ce7, 0x12],
47 [0x19d0, 0x13],
48 [0x0762, 0x14],
49 [0x0255, 0x15],
50 [0x0d0c, 0x16],
51 [0x083b, 0x17],
52 [0x355f, 0x18],
53 [0x3068, 0x19],
54 [0x3f31, 0x1a],
55 [0x3a06, 0x1b],
56 [0x24b4, 0x1c],
57 [0x2183, 0x1d],
58 [0x2eda, 0x1e],
59 [0x2bed, 0x1f],
60 ];
61
62 /**
63 * Offset i holds the number of 1 bits in the binary representation of i.
64 *
65 * @var int[]
66 */
67 private const BITS_SET_IN_HALF_BYTE = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4];
68
69 /**
70 * Error correction level.
71 */
72 private ErrorCorrectionLevel $ecLevel;
73
74 private int $dataMask;
75
76 protected function __construct(int $formatInfo)
77 {
78 $this->ecLevel = ErrorCorrectionLevel::forBits(($formatInfo >> 3) & 0x3);
79 $this->dataMask = $formatInfo & 0x7;
80 }
81
82 /**
83 * Checks how many bits are different between two integers.
84 */
85 public static function numBitsDiffering(int $a, int $b) : int
86 {
87 $a ^= $b;
88
89 return (
90 self::BITS_SET_IN_HALF_BYTE[$a & 0xf]
91 + self::BITS_SET_IN_HALF_BYTE[(BitUtils::unsignedRightShift($a, 4) & 0xf)]
92 + self::BITS_SET_IN_HALF_BYTE[(BitUtils::unsignedRightShift($a, 8) & 0xf)]
93 + self::BITS_SET_IN_HALF_BYTE[(BitUtils::unsignedRightShift($a, 12) & 0xf)]
94 + self::BITS_SET_IN_HALF_BYTE[(BitUtils::unsignedRightShift($a, 16) & 0xf)]
95 + self::BITS_SET_IN_HALF_BYTE[(BitUtils::unsignedRightShift($a, 20) & 0xf)]
96 + self::BITS_SET_IN_HALF_BYTE[(BitUtils::unsignedRightShift($a, 24) & 0xf)]
97 + self::BITS_SET_IN_HALF_BYTE[(BitUtils::unsignedRightShift($a, 28) & 0xf)]
98 );
99 }
100
101 /**
102 * Decodes format information.
103 */
104 public static function decodeFormatInformation(int $maskedFormatInfo1, int $maskedFormatInfo2) : ?self
105 {
106 $formatInfo = self::doDecodeFormatInformation($maskedFormatInfo1, $maskedFormatInfo2);
107
108 if (null !== $formatInfo) {
109 return $formatInfo;
110 }
111
112 // Should return null, but, some QR codes apparently do not mask this info. Try again by actually masking the
113 // pattern first.
114 return self::doDecodeFormatInformation(
115 $maskedFormatInfo1 ^ self::FORMAT_INFO_MASK_QR,
116 $maskedFormatInfo2 ^ self::FORMAT_INFO_MASK_QR
117 );
118 }
119
120 /**
121 * Internal method for decoding format information.
122 */
123 private static function doDecodeFormatInformation(int $maskedFormatInfo1, int $maskedFormatInfo2) : ?self
124 {
125 $bestDifference = PHP_INT_MAX;
126 $bestFormatInfo = 0;
127
128 foreach (self::FORMAT_INFO_DECODE_LOOKUP as $decodeInfo) {
129 $targetInfo = $decodeInfo[0];
130
131 if ($targetInfo === $maskedFormatInfo1 || $targetInfo === $maskedFormatInfo2) {
132 // Found an exact match
133 return new self($decodeInfo[1]);
134 }
135
136 $bitsDifference = self::numBitsDiffering($maskedFormatInfo1, $targetInfo);
137
138 if ($bitsDifference < $bestDifference) {
139 $bestFormatInfo = $decodeInfo[1];
140 $bestDifference = $bitsDifference;
141 }
142
143 if ($maskedFormatInfo1 !== $maskedFormatInfo2) {
144 // Also try the other option
145 $bitsDifference = self::numBitsDiffering($maskedFormatInfo2, $targetInfo);
146
147 if ($bitsDifference < $bestDifference) {
148 $bestFormatInfo = $decodeInfo[1];
149 $bestDifference = $bitsDifference;
150 }
151 }
152 }
153
154 // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits differing means we found a match.
155 if ($bestDifference <= 3) {
156 return new self($bestFormatInfo);
157 }
158
159 return null;
160 }
161
162 /**
163 * Returns the error correction level.
164 */
165 public function getErrorCorrectionLevel() : ErrorCorrectionLevel
166 {
167 return $this->ecLevel;
168 }
169
170 /**
171 * Returns the data mask.
172 */
173 public function getDataMask() : int
174 {
175 return $this->dataMask;
176 }
177
178 /**
179 * Hashes the code of the EC level.
180 */
181 public function hashCode() : int
182 {
183 return ($this->ecLevel->getBits() << 3) | $this->dataMask;
184 }
185
186 /**
187 * Verifies if this instance equals another one.
188 */
189 public function equals(self $other) : bool
190 {
191 return (
192 $this->ecLevel === $other->ecLevel
193 && $this->dataMask === $other->dataMask
194 );
195 }
196 }