QRDataAbstract.php 7.7 KB

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