QRDataAbstract.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. * the options instance
  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. return $this;
  79. }
  80. /**
  81. * @inheritDoc
  82. */
  83. public function initMatrix(int $maskPattern, ?bool $test = null):QRMatrix{
  84. return (new QRMatrix($this->version, $this->options->eccLevel))
  85. ->init($maskPattern, $test)
  86. ->mapData($this->maskECC(), $maskPattern)
  87. ;
  88. }
  89. /**
  90. * returns the length bits for the version breakpoints 1-9, 10-26 and 27-40
  91. *
  92. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  93. * @codeCoverageIgnore
  94. */
  95. protected function getLengthBits():int{
  96. foreach([9, 26, 40] as $key => $breakpoint){
  97. if($this->version <= $breakpoint){
  98. return $this->lengthBits[$key];
  99. }
  100. }
  101. throw new QRCodeDataException(sprintf('invalid version number: %d', $this->version));
  102. }
  103. /**
  104. * returns the byte count of the $data string
  105. */
  106. protected function getLength(string $data):int{
  107. return strlen($data);
  108. }
  109. /**
  110. * returns the minimum version number for the given string
  111. *
  112. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  113. */
  114. protected function getMinimumVersion():int{
  115. $maxlength = 0;
  116. // guess the version number within the given range
  117. $dataMode = QRCode::DATA_MODES[$this->datamode];
  118. $eccMode = QRCode::ECC_MODES[$this->options->eccLevel];
  119. foreach(range($this->options->versionMin, $this->options->versionMax) as $version){
  120. $maxlength = $this::MAX_LENGTH[$version][$dataMode][$eccMode];
  121. if($this->strlen <= $maxlength){
  122. return $version;
  123. }
  124. }
  125. throw new QRCodeDataException(sprintf('data exceeds %d characters', $maxlength));
  126. }
  127. /**
  128. * writes the actual data string to the BitBuffer
  129. *
  130. * @see \chillerlan\QRCode\Data\QRDataAbstract::writeBitBuffer()
  131. */
  132. abstract protected function write(string $data):void;
  133. /**
  134. * creates a BitBuffer and writes the string data to it
  135. *
  136. * @throws \chillerlan\QRCode\QRCodeException on data overflow
  137. */
  138. protected function writeBitBuffer(string $data):void{
  139. $this->bitBuffer = new BitBuffer;
  140. $MAX_BITS = $this::MAX_BITS[$this->version][QRCode::ECC_MODES[$this->options->eccLevel]];
  141. $this->bitBuffer
  142. ->put($this->datamode, 4)
  143. ->put($this->strlen, $this->getLengthBits())
  144. ;
  145. $this->write($data);
  146. // overflow, likely caused due to invalid version setting
  147. if($this->bitBuffer->getLength() > $MAX_BITS){
  148. throw new QRCodeDataException(sprintf('code length overflow. (%d > %d bit)', $this->bitBuffer->getLength(), $MAX_BITS));
  149. }
  150. // add terminator (ISO/IEC 18004:2000 Table 2)
  151. if($this->bitBuffer->getLength() + 4 <= $MAX_BITS){
  152. $this->bitBuffer->put(0, 4);
  153. }
  154. // padding
  155. while($this->bitBuffer->getLength() % 8 !== 0){
  156. $this->bitBuffer->putBit(false);
  157. }
  158. // padding
  159. while(true){
  160. if($this->bitBuffer->getLength() >= $MAX_BITS){
  161. break;
  162. }
  163. $this->bitBuffer->put(0xEC, 8);
  164. if($this->bitBuffer->getLength() >= $MAX_BITS){
  165. break;
  166. }
  167. $this->bitBuffer->put(0x11, 8);
  168. }
  169. }
  170. /**
  171. * ECC masking
  172. *
  173. * ISO/IEC 18004:2000 Section 8.5 ff
  174. *
  175. * @see 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. $bitBuffer = $this->bitBuffer->getBuffer();
  191. foreach($rsBlocks as $key => $block){
  192. [$rsBlockTotal, $dcCount] = $block;
  193. $ecCount = $rsBlockTotal - $dcCount;
  194. $maxDcCount = max($maxDcCount, $dcCount);
  195. $maxEcCount = max($maxEcCount, $ecCount);
  196. $this->dcdata[$key] = array_fill(0, $dcCount, null);
  197. foreach($this->dcdata[$key] as $a => $_z){
  198. $this->dcdata[$key][$a] = 0xff & $bitBuffer[$a + $offset];
  199. }
  200. [$num, $add] = $this->poly($key, $ecCount);
  201. foreach($this->ecdata[$key] as $c => $_){
  202. $modIndex = $c + $add;
  203. $this->ecdata[$key][$c] = $modIndex >= 0 ? $num[$modIndex] : 0;
  204. }
  205. $offset += $dcCount;
  206. $totalCodeCount += $rsBlockTotal;
  207. }
  208. $data = array_fill(0, $totalCodeCount, null);
  209. $index = 0;
  210. $mask = function(array $arr, int $count) use (&$data, &$index, $rsCount):void{
  211. for($x = 0; $x < $count; $x++){
  212. for($y = 0; $y < $rsCount; $y++){
  213. if($x < count($arr[$y])){
  214. $data[$index] = $arr[$y][$x];
  215. $index++;
  216. }
  217. }
  218. }
  219. };
  220. $mask($this->dcdata, $maxDcCount);
  221. $mask($this->ecdata, $maxEcCount);
  222. return $data;
  223. }
  224. /**
  225. * helper method for the polynomial operations
  226. */
  227. protected function poly(int $key, int $count):array{
  228. $rsPoly = new Polynomial;
  229. $modPoly = new Polynomial;
  230. for($i = 0; $i < $count; $i++){
  231. $modPoly->setNum([1, $modPoly->gexp($i)]);
  232. $rsPoly->multiply($modPoly->getNum());
  233. }
  234. $rsPolyCount = count($rsPoly->getNum());
  235. $modPoly
  236. ->setNum($this->dcdata[$key], $rsPolyCount - 1)
  237. ->mod($rsPoly->getNum())
  238. ;
  239. $this->ecdata[$key] = array_fill(0, $rsPolyCount - 1, null);
  240. $num = $modPoly->getNum();
  241. return [
  242. $num,
  243. count($num) - count($this->ecdata[$key]),
  244. ];
  245. }
  246. }