QRDataAbstract.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * Class QRDataAbstract
  4. *
  5. * @filesource QRDataAbstract.php
  6. * @created 25.11.2015
  7. * @package chillerlan\QRCode\Data
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2015 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCode\Data;
  13. use chillerlan\QRCode\QRCode;
  14. use chillerlan\QRCode\Helpers\{BitBuffer, Polynomial};
  15. use chillerlan\Settings\SettingsContainerInterface;
  16. use function array_fill, array_merge, count, max, mb_convert_encoding, mb_detect_encoding, range, sprintf, strlen;
  17. /**
  18. * Processes the binary data and maps it on a matrix which is then being returned
  19. */
  20. abstract class QRDataAbstract implements QRDataInterface{
  21. /**
  22. * the string byte count
  23. */
  24. protected ?int $strlen = null;
  25. /**
  26. * the current data mode: Num, Alphanum, Kanji, Byte
  27. */
  28. protected int $datamode;
  29. /**
  30. * mode length bits for the version breakpoints 1-9, 10-26 and 27-40
  31. *
  32. * ISO/IEC 18004:2000 Table 3 - Number of bits in Character Count Indicator
  33. */
  34. protected array $lengthBits = [0, 0, 0];
  35. /**
  36. * current QR Code version
  37. */
  38. protected int $version;
  39. /**
  40. * the raw data that's being passed to QRMatrix::mapData()
  41. */
  42. protected array $matrixdata;
  43. /**
  44. * ECC temp data
  45. */
  46. protected array $ecdata;
  47. /**
  48. * ECC temp data
  49. */
  50. protected array $dcdata;
  51. /**
  52. * @var \chillerlan\Settings\SettingsContainerInterface|\chillerlan\QRCode\QROptions
  53. */
  54. protected SettingsContainerInterface $options;
  55. /**
  56. * a BitBuffer instance
  57. */
  58. protected BitBuffer $bitBuffer;
  59. /**
  60. * QRDataInterface constructor.
  61. */
  62. public function __construct(SettingsContainerInterface $options, string $data = null){
  63. $this->options = $options;
  64. if($data !== null){
  65. $this->setData($data);
  66. }
  67. }
  68. /**
  69. * @inheritDoc
  70. */
  71. public function setData(string $data):QRDataInterface{
  72. if($this->datamode === QRCode::DATA_KANJI){
  73. $data = mb_convert_encoding($data, 'SJIS', mb_detect_encoding($data));
  74. }
  75. $this->strlen = $this->getLength($data);
  76. $this->version = $this->options->version === QRCode::VERSION_AUTO
  77. ? $this->getMinimumVersion()
  78. : $this->options->version;
  79. $this->writeBitBuffer($data);
  80. $this->matrixdata = $this->maskECC();
  81. return $this;
  82. }
  83. /**
  84. * @inheritDoc
  85. */
  86. public function initMatrix(int $maskPattern, bool $test = null):QRMatrix{
  87. return (new QRMatrix($this->version, $this->options->eccLevel))
  88. ->init($maskPattern, $test)
  89. ->mapData($this->matrixdata, $maskPattern)
  90. ;
  91. }
  92. /**
  93. * returns the length bits for the version breakpoints 1-9, 10-26 and 27-40
  94. *
  95. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  96. * @codeCoverageIgnore
  97. */
  98. protected function getLengthBits():int{
  99. foreach([9, 26, 40] as $key => $breakpoint){
  100. if($this->version <= $breakpoint){
  101. return $this->lengthBits[$key];
  102. }
  103. }
  104. throw new QRCodeDataException(sprintf('invalid version number: %d', $this->version));
  105. }
  106. /**
  107. * returns the byte count of the $data string
  108. */
  109. protected function getLength(string $data):int{
  110. return strlen($data);
  111. }
  112. /**
  113. * returns the minimum version number for the given string
  114. *
  115. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  116. */
  117. protected function getMinimumVersion():int{
  118. $maxlength = 0;
  119. // guess the version number within the given range
  120. $dataMode = QRCode::DATA_MODES[$this->datamode];
  121. $eccMode = QRCode::ECC_MODES[$this->options->eccLevel];
  122. foreach(range($this->options->versionMin, $this->options->versionMax) as $version){
  123. $maxlength = $this::MAX_LENGTH[$version][$dataMode][$eccMode];
  124. if($this->strlen <= $maxlength){
  125. return $version;
  126. }
  127. }
  128. throw new QRCodeDataException(sprintf('data exceeds %d characters', $maxlength));
  129. }
  130. /**
  131. * writes the actual data string to the BitBuffer
  132. *
  133. * @see \chillerlan\QRCode\Data\QRDataAbstract::writeBitBuffer()
  134. */
  135. abstract protected function write(string $data):void;
  136. /**
  137. * creates a BitBuffer and writes the string data to it
  138. *
  139. * @throws \chillerlan\QRCode\QRCodeException on data overflow
  140. */
  141. protected function writeBitBuffer(string $data):void{
  142. $this->bitBuffer = new BitBuffer;
  143. $MAX_BITS = $this::MAX_BITS[$this->version][QRCode::ECC_MODES[$this->options->eccLevel]];
  144. $this->bitBuffer
  145. ->clear()
  146. ->put($this->datamode, 4)
  147. ->put($this->strlen, $this->getLengthBits())
  148. ;
  149. $this->write($data);
  150. // overflow, likely caused due to invalid version setting
  151. if($this->bitBuffer->getLength() > $MAX_BITS){
  152. throw new QRCodeDataException(sprintf('code length overflow. (%d > %d bit)', $this->bitBuffer->getLength(), $MAX_BITS));
  153. }
  154. // add terminator (ISO/IEC 18004:2000 Table 2)
  155. if($this->bitBuffer->getLength() + 4 <= $MAX_BITS){
  156. $this->bitBuffer->put(0, 4);
  157. }
  158. // padding
  159. while($this->bitBuffer->getLength() % 8 !== 0){
  160. $this->bitBuffer->putBit(false);
  161. }
  162. // padding
  163. while(true){
  164. if($this->bitBuffer->getLength() >= $MAX_BITS){
  165. break;
  166. }
  167. $this->bitBuffer->put(0xEC, 8);
  168. if($this->bitBuffer->getLength() >= $MAX_BITS){
  169. break;
  170. }
  171. $this->bitBuffer->put(0x11, 8);
  172. }
  173. }
  174. /**
  175. * ECC masking
  176. *
  177. * ISO/IEC 18004:2000 Section 8.5 ff
  178. *
  179. * @see http://www.thonky.com/qr-code-tutorial/error-correction-coding
  180. */
  181. protected function maskECC():array{
  182. [$l1, $l2, $b1, $b2] = $this::RSBLOCKS[$this->version][QRCode::ECC_MODES[$this->options->eccLevel]];
  183. $rsBlocks = array_fill(0, $l1, [$b1, $b2]);
  184. $rsCount = $l1 + $l2;
  185. $this->ecdata = array_fill(0, $rsCount, []);
  186. $this->dcdata = $this->ecdata;
  187. if($l2 > 0){
  188. $rsBlocks = array_merge($rsBlocks, array_fill(0, $l2, [$b1 + 1, $b2 + 1]));
  189. }
  190. $totalCodeCount = 0;
  191. $maxDcCount = 0;
  192. $maxEcCount = 0;
  193. $offset = 0;
  194. $bitBuffer = $this->bitBuffer->getBuffer();
  195. foreach($rsBlocks as $key => $block){
  196. [$rsBlockTotal, $dcCount] = $block;
  197. $ecCount = $rsBlockTotal - $dcCount;
  198. $maxDcCount = max($maxDcCount, $dcCount);
  199. $maxEcCount = max($maxEcCount, $ecCount);
  200. $this->dcdata[$key] = array_fill(0, $dcCount, null);
  201. foreach($this->dcdata[$key] as $a => $_z){
  202. $this->dcdata[$key][$a] = 0xff & $bitBuffer[$a + $offset];
  203. }
  204. [$num, $add] = $this->poly($key, $ecCount);
  205. foreach($this->ecdata[$key] as $c => $_){
  206. $modIndex = $c + $add;
  207. $this->ecdata[$key][$c] = $modIndex >= 0 ? $num[$modIndex] : 0;
  208. }
  209. $offset += $dcCount;
  210. $totalCodeCount += $rsBlockTotal;
  211. }
  212. $data = array_fill(0, $totalCodeCount, null);
  213. $index = 0;
  214. $mask = function(array $arr, int $count) use (&$data, &$index, $rsCount):void{
  215. for($x = 0; $x < $count; $x++){
  216. for($y = 0; $y < $rsCount; $y++){
  217. if($x < count($arr[$y])){
  218. $data[$index] = $arr[$y][$x];
  219. $index++;
  220. }
  221. }
  222. }
  223. };
  224. $mask($this->dcdata, $maxDcCount);
  225. $mask($this->ecdata, $maxEcCount);
  226. return $data;
  227. }
  228. /**
  229. * helper method for the polynomial operations
  230. */
  231. protected function poly(int $key, int $count):array{
  232. $rsPoly = new Polynomial;
  233. $modPoly = new Polynomial;
  234. for($i = 0; $i < $count; $i++){
  235. $modPoly->setNum([1, $modPoly->gexp($i)]);
  236. $rsPoly->multiply($modPoly->getNum());
  237. }
  238. $rsPolyCount = count($rsPoly->getNum());
  239. $modPoly
  240. ->setNum($this->dcdata[$key], $rsPolyCount - 1)
  241. ->mod($rsPoly->getNum())
  242. ;
  243. $this->ecdata[$key] = array_fill(0, $rsPolyCount - 1, null);
  244. $num = $modPoly->getNum();
  245. return [
  246. $num,
  247. count($num) - count($this->ecdata[$key]),
  248. ];
  249. }
  250. }