QRDataAbstract.php 7.2 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. * 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. * ECC temp data
  41. */
  42. protected array $ecdata;
  43. /**
  44. * ECC temp data
  45. */
  46. protected array $dcdata;
  47. /**
  48. * @var \chillerlan\Settings\SettingsContainerInterface|\chillerlan\QRCode\QROptions
  49. */
  50. protected SettingsContainerInterface $options;
  51. /**
  52. * a BitBuffer instance
  53. */
  54. protected BitBuffer $bitBuffer;
  55. /**
  56. * QRDataInterface constructor.
  57. */
  58. public function __construct(SettingsContainerInterface $options, string $data = null){
  59. $this->options = $options;
  60. if($data !== null){
  61. $this->setData($data);
  62. }
  63. }
  64. /**
  65. * @inheritDoc
  66. */
  67. public function setData(string $data):QRDataInterface{
  68. if($this->datamode === QRCode::DATA_KANJI){
  69. $data = mb_convert_encoding($data, 'SJIS', mb_detect_encoding($data));
  70. }
  71. $this->strlen = $this->getLength($data);
  72. $this->version = $this->options->version === QRCode::VERSION_AUTO
  73. ? $this->getMinimumVersion()
  74. : $this->options->version;
  75. $this->writeBitBuffer($data);
  76. return $this;
  77. }
  78. /**
  79. * @inheritDoc
  80. */
  81. public function initMatrix(int $maskPattern, bool $test = null):QRMatrix{
  82. return (new QRMatrix($this->version, $this->options->eccLevel))
  83. ->init($maskPattern, $test)
  84. ->mapData($this->maskECC(), $maskPattern)
  85. ;
  86. }
  87. /**
  88. * returns the length bits for the version breakpoints 1-9, 10-26 and 27-40
  89. *
  90. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  91. * @codeCoverageIgnore
  92. */
  93. protected function getLengthBits():int{
  94. foreach([9, 26, 40] as $key => $breakpoint){
  95. if($this->version <= $breakpoint){
  96. return $this->lengthBits[$key];
  97. }
  98. }
  99. throw new QRCodeDataException(sprintf('invalid version number: %d', $this->version));
  100. }
  101. /**
  102. * returns the byte count of the $data string
  103. */
  104. protected function getLength(string $data):int{
  105. return strlen($data);
  106. }
  107. /**
  108. * returns the minimum version number for the given string
  109. *
  110. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  111. */
  112. protected function getMinimumVersion():int{
  113. $maxlength = 0;
  114. // guess the version number within the given range
  115. $dataMode = QRCode::DATA_MODES[$this->datamode];
  116. $eccMode = QRCode::ECC_MODES[$this->options->eccLevel];
  117. foreach(range($this->options->versionMin, $this->options->versionMax) as $version){
  118. $maxlength = $this::MAX_LENGTH[$version][$dataMode][$eccMode];
  119. if($this->strlen <= $maxlength){
  120. return $version;
  121. }
  122. }
  123. throw new QRCodeDataException(sprintf('data exceeds %d characters', $maxlength));
  124. }
  125. /**
  126. * writes the actual data string to the BitBuffer
  127. *
  128. * @see \chillerlan\QRCode\Data\QRDataAbstract::writeBitBuffer()
  129. */
  130. abstract protected function write(string $data):void;
  131. /**
  132. * creates a BitBuffer and writes the string data to it
  133. *
  134. * @throws \chillerlan\QRCode\QRCodeException on data overflow
  135. */
  136. protected function writeBitBuffer(string $data):void{
  137. $this->bitBuffer = new BitBuffer;
  138. $MAX_BITS = $this::MAX_BITS[$this->version][QRCode::ECC_MODES[$this->options->eccLevel]];
  139. $this->bitBuffer
  140. ->clear()
  141. ->put($this->datamode, 4)
  142. ->put($this->strlen, $this->getLengthBits())
  143. ;
  144. $this->write($data);
  145. // overflow, likely caused due to invalid version setting
  146. if($this->bitBuffer->getLength() > $MAX_BITS){
  147. throw new QRCodeDataException(sprintf('code length overflow. (%d > %d bit)', $this->bitBuffer->getLength(), $MAX_BITS));
  148. }
  149. // add terminator (ISO/IEC 18004:2000 Table 2)
  150. if($this->bitBuffer->getLength() + 4 <= $MAX_BITS){
  151. $this->bitBuffer->put(0, 4);
  152. }
  153. // padding
  154. while($this->bitBuffer->getLength() % 8 !== 0){
  155. $this->bitBuffer->putBit(false);
  156. }
  157. // padding
  158. while(true){
  159. if($this->bitBuffer->getLength() >= $MAX_BITS){
  160. break;
  161. }
  162. $this->bitBuffer->put(0xEC, 8);
  163. if($this->bitBuffer->getLength() >= $MAX_BITS){
  164. break;
  165. }
  166. $this->bitBuffer->put(0x11, 8);
  167. }
  168. }
  169. /**
  170. * ECC masking
  171. *
  172. * ISO/IEC 18004:2000 Section 8.5 ff
  173. *
  174. * @see http://www.thonky.com/qr-code-tutorial/error-correction-coding
  175. */
  176. protected function maskECC():array{
  177. [$l1, $l2, $b1, $b2] = $this::RSBLOCKS[$this->version][QRCode::ECC_MODES[$this->options->eccLevel]];
  178. $rsBlocks = array_fill(0, $l1, [$b1, $b2]);
  179. $rsCount = $l1 + $l2;
  180. $this->ecdata = array_fill(0, $rsCount, []);
  181. $this->dcdata = $this->ecdata;
  182. if($l2 > 0){
  183. $rsBlocks = array_merge($rsBlocks, array_fill(0, $l2, [$b1 + 1, $b2 + 1]));
  184. }
  185. $totalCodeCount = 0;
  186. $maxDcCount = 0;
  187. $maxEcCount = 0;
  188. $offset = 0;
  189. $bitBuffer = $this->bitBuffer->getBuffer();
  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 & $bitBuffer[$a + $offset];
  198. }
  199. [$num, $add] = $this->poly($key, $ecCount);
  200. foreach($this->ecdata[$key] as $c => $_){
  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):void{
  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. * helper method for the polynomial operations
  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. }