QRinput.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. /*
  3. * PHP QR Code encoder
  4. *
  5. * Input encoding class
  6. *
  7. * Based on libqrencode C library distributed under LGPL 2.1
  8. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  9. *
  10. * PHP QR Code is distributed under LGPL 3
  11. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 3 of the License, or any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. namespace PHPQRCode;
  28. use Exception;
  29. class QRinput {
  30. public $items;
  31. private $version;
  32. private $level;
  33. //----------------------------------------------------------------------
  34. public function __construct($version = 0, $level = Constants::QR_ECLEVEL_L)
  35. {
  36. if ($version < 0 || $version > Constants::QRSPEC_VERSION_MAX || $level > Constants::QR_ECLEVEL_H) {
  37. throw new Exception('Invalid version no');
  38. return NULL;
  39. }
  40. $this->version = $version;
  41. $this->level = $level;
  42. }
  43. //----------------------------------------------------------------------
  44. public function getVersion()
  45. {
  46. return $this->version;
  47. }
  48. //----------------------------------------------------------------------
  49. public function setVersion($version)
  50. {
  51. if($version < 0 || $version > Constants::QRSPEC_VERSION_MAX) {
  52. throw new Exception('Invalid version no');
  53. return -1;
  54. }
  55. $this->version = $version;
  56. return 0;
  57. }
  58. //----------------------------------------------------------------------
  59. public function getErrorCorrectionLevel()
  60. {
  61. return $this->level;
  62. }
  63. //----------------------------------------------------------------------
  64. public function setErrorCorrectionLevel($level)
  65. {
  66. if($level > Constants::QR_ECLEVEL_H) {
  67. throw new Exception('Invalid ECLEVEL');
  68. return -1;
  69. }
  70. $this->level = $level;
  71. return 0;
  72. }
  73. //----------------------------------------------------------------------
  74. public function appendEntry(QRinputItem $entry)
  75. {
  76. $this->items[] = $entry;
  77. }
  78. //----------------------------------------------------------------------
  79. public function append($mode, $size, $data)
  80. {
  81. try {
  82. $entry = new QRinputItem($mode, $size, $data);
  83. $this->items[] = $entry;
  84. return 0;
  85. } catch (Exception $e) {
  86. return -1;
  87. }
  88. }
  89. //----------------------------------------------------------------------
  90. public function insertStructuredAppendHeader($size, $index, $parity)
  91. {
  92. if( $size > Constants::MAX_STRUCTURED_SYMBOLS ) {
  93. throw new Exception('insertStructuredAppendHeader wrong size');
  94. }
  95. if( $index <= 0 || $index > Constants::MAX_STRUCTURED_SYMBOLS ) {
  96. throw new Exception('insertStructuredAppendHeader wrong index');
  97. }
  98. $buf = array($size, $index, $parity);
  99. try {
  100. $entry = new QRinputItem(Constants::QR_MODE_STRUCTURE, 3, buf);
  101. array_unshift($this->items, $entry);
  102. return 0;
  103. } catch (Exception $e) {
  104. return -1;
  105. }
  106. }
  107. //----------------------------------------------------------------------
  108. public function calcParity()
  109. {
  110. $parity = 0;
  111. foreach($this->items as $item) {
  112. if($item->mode != Constants::QR_MODE_STRUCTURE) {
  113. for($i=$item->size-1; $i>=0; $i--) {
  114. $parity ^= $item->data[$i];
  115. }
  116. }
  117. }
  118. return $parity;
  119. }
  120. //----------------------------------------------------------------------
  121. public static function checkModeNum($size, $data)
  122. {
  123. for($i=0; $i<$size; $i++) {
  124. if((ord($data[$i]) < ord('0')) || (ord($data[$i]) > ord('9'))){
  125. return false;
  126. }
  127. }
  128. return true;
  129. }
  130. //----------------------------------------------------------------------
  131. public static function estimateBitsModeNum($size)
  132. {
  133. $w = (int)$size / 3;
  134. $bits = $w * 10;
  135. switch($size - $w * 3) {
  136. case 1:
  137. $bits += 4;
  138. break;
  139. case 2:
  140. $bits += 7;
  141. break;
  142. default:
  143. break;
  144. }
  145. return $bits;
  146. }
  147. //----------------------------------------------------------------------
  148. public static $anTable = array(
  149. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  150. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  151. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
  152. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
  153. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  154. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
  155. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  156. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  157. );
  158. //----------------------------------------------------------------------
  159. public static function lookAnTable($c)
  160. {
  161. return (($c > 127)?-1:self::$anTable[$c]);
  162. }
  163. //----------------------------------------------------------------------
  164. public static function checkModeAn($size, $data)
  165. {
  166. for($i=0; $i<$size; $i++) {
  167. if (self::lookAnTable(ord($data[$i])) == -1) {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. //----------------------------------------------------------------------
  174. public static function estimateBitsModeAn($size)
  175. {
  176. $w = (int)($size / 2);
  177. $bits = $w * 11;
  178. if($size & 1) {
  179. $bits += 6;
  180. }
  181. return $bits;
  182. }
  183. //----------------------------------------------------------------------
  184. public static function estimateBitsMode8($size)
  185. {
  186. return $size * 8;
  187. }
  188. //----------------------------------------------------------------------
  189. public function estimateBitsModeKanji($size)
  190. {
  191. return (int)(($size / 2) * 13);
  192. }
  193. //----------------------------------------------------------------------
  194. public static function checkModeKanji($size, $data)
  195. {
  196. if($size & 1)
  197. return false;
  198. for($i=0; $i<$size; $i+=2) {
  199. $val = (ord($data[$i]) << 8) | ord($data[$i+1]);
  200. if( $val < 0x8140
  201. || ($val > 0x9ffc && $val < 0xe040)
  202. || $val > 0xebbf) {
  203. return false;
  204. }
  205. }
  206. return true;
  207. }
  208. /***********************************************************************
  209. * Validation
  210. **********************************************************************/
  211. public static function check($mode, $size, $data)
  212. {
  213. if($size <= 0)
  214. return false;
  215. switch($mode) {
  216. case Constants::QR_MODE_NUM: return self::checkModeNum($size, $data); break;
  217. case Constants::QR_MODE_AN: return self::checkModeAn($size, $data); break;
  218. case Constants::QR_MODE_KANJI: return self::checkModeKanji($size, $data); break;
  219. case Constants::QR_MODE_8: return true; break;
  220. case Constants::QR_MODE_STRUCTURE: return true; break;
  221. default:
  222. break;
  223. }
  224. return false;
  225. }
  226. //----------------------------------------------------------------------
  227. public function estimateBitStreamSize($version)
  228. {
  229. $bits = 0;
  230. foreach($this->items as $item) {
  231. $bits += $item->estimateBitStreamSizeOfEntry($version);
  232. }
  233. return $bits;
  234. }
  235. //----------------------------------------------------------------------
  236. public function estimateVersion()
  237. {
  238. $version = 0;
  239. $prev = 0;
  240. do {
  241. $prev = $version;
  242. $bits = $this->estimateBitStreamSize($prev);
  243. $version = QRspec::getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  244. if ($version < 0) {
  245. return -1;
  246. }
  247. } while ($version > $prev);
  248. return $version;
  249. }
  250. //----------------------------------------------------------------------
  251. public static function lengthOfCode($mode, $version, $bits)
  252. {
  253. $payload = $bits - 4 - QRspec::lengthIndicator($mode, $version);
  254. switch($mode) {
  255. case Constants::QR_MODE_NUM:
  256. $chunks = (int)($payload / 10);
  257. $remain = $payload - $chunks * 10;
  258. $size = $chunks * 3;
  259. if($remain >= 7) {
  260. $size += 2;
  261. } else if($remain >= 4) {
  262. $size += 1;
  263. }
  264. break;
  265. case Constants::QR_MODE_AN:
  266. $chunks = (int)($payload / 11);
  267. $remain = $payload - $chunks * 11;
  268. $size = $chunks * 2;
  269. if($remain >= 6)
  270. $size++;
  271. break;
  272. case Constants::QR_MODE_8:
  273. $size = (int)($payload / 8);
  274. break;
  275. case Constants::QR_MODE_KANJI:
  276. $size = (int)(($payload / 13) * 2);
  277. break;
  278. case Constants::QR_MODE_STRUCTURE:
  279. $size = (int)($payload / 8);
  280. break;
  281. default:
  282. $size = 0;
  283. break;
  284. }
  285. $maxsize = QRspec::maximumWords($mode, $version);
  286. if($size < 0) $size = 0;
  287. if($size > $maxsize) $size = $maxsize;
  288. return $size;
  289. }
  290. //----------------------------------------------------------------------
  291. public function createBitStream()
  292. {
  293. $total = 0;
  294. foreach($this->items as $item) {
  295. $bits = $item->encodeBitStream($this->version);
  296. if($bits < 0)
  297. return -1;
  298. $total += $bits;
  299. }
  300. return $total;
  301. }
  302. //----------------------------------------------------------------------
  303. public function convertData()
  304. {
  305. $ver = $this->estimateVersion();
  306. if($ver > $this->getVersion()) {
  307. $this->setVersion($ver);
  308. }
  309. for(;;) {
  310. $bits = $this->createBitStream();
  311. if($bits < 0)
  312. return -1;
  313. $ver = QRspec::getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  314. if($ver < 0) {
  315. throw new Exception('WRONG VERSION');
  316. return -1;
  317. } else if($ver > $this->getVersion()) {
  318. $this->setVersion($ver);
  319. } else {
  320. break;
  321. }
  322. }
  323. return 0;
  324. }
  325. //----------------------------------------------------------------------
  326. public function appendPaddingBit(&$bstream)
  327. {
  328. $bits = $bstream->size();
  329. $maxwords = QRspec::getDataLength($this->version, $this->level);
  330. $maxbits = $maxwords * 8;
  331. if ($maxbits == $bits) {
  332. return 0;
  333. }
  334. if ($maxbits - $bits < 5) {
  335. return $bstream->appendNum($maxbits - $bits, 0);
  336. }
  337. $bits += 4;
  338. $words = (int)(($bits + 7) / 8);
  339. $padding = new QRbitstream();
  340. $ret = $padding->appendNum($words * 8 - $bits + 4, 0);
  341. if($ret < 0)
  342. return $ret;
  343. $padlen = $maxwords - $words;
  344. if($padlen > 0) {
  345. $padbuf = array();
  346. for($i=0; $i<$padlen; $i++) {
  347. $padbuf[$i] = ($i&1)?0x11:0xec;
  348. }
  349. $ret = $padding->appendBytes($padlen, $padbuf);
  350. if($ret < 0)
  351. return $ret;
  352. }
  353. $ret = $bstream->append($padding);
  354. return $ret;
  355. }
  356. //----------------------------------------------------------------------
  357. public function mergeBitStream()
  358. {
  359. if($this->convertData() < 0) {
  360. return null;
  361. }
  362. $bstream = new QRbitstream();
  363. foreach($this->items as $item) {
  364. $ret = $bstream->append($item->bstream);
  365. if($ret < 0) {
  366. return null;
  367. }
  368. }
  369. return $bstream;
  370. }
  371. //----------------------------------------------------------------------
  372. public function getBitStream()
  373. {
  374. $bstream = $this->mergeBitStream();
  375. if($bstream == null) {
  376. return null;
  377. }
  378. $ret = $this->appendPaddingBit($bstream);
  379. if($ret < 0) {
  380. return null;
  381. }
  382. return $bstream;
  383. }
  384. //----------------------------------------------------------------------
  385. public function getByteStream()
  386. {
  387. $bstream = $this->getBitStream();
  388. if($bstream == null) {
  389. return null;
  390. }
  391. return $bstream->toByte();
  392. }
  393. }