QRDataAbstract.php 7.1 KB

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