FormatInformation.php 5.7 KB

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