QRsplit.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /*
  3. * PHP QR Code encoder
  4. *
  5. * Input splitting classes
  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. * The following data / specifications are taken from
  14. * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
  15. * or
  16. * "Automatic identification and data capture techniques --
  17. * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 3 of the License, or any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library; if not, write to the Free Software
  31. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  32. */
  33. namespace PHPQRCode;
  34. use Exception;
  35. class QRsplit {
  36. public $dataStr = '';
  37. public $input;
  38. public $modeHint;
  39. //----------------------------------------------------------------------
  40. public function __construct($dataStr, $input, $modeHint)
  41. {
  42. $this->dataStr = $dataStr;
  43. $this->input = $input;
  44. $this->modeHint = $modeHint;
  45. }
  46. //----------------------------------------------------------------------
  47. public static function isdigitat($str, $pos)
  48. {
  49. if ($pos >= strlen($str))
  50. return false;
  51. return ((ord($str[$pos]) >= ord('0'))&&(ord($str[$pos]) <= ord('9')));
  52. }
  53. //----------------------------------------------------------------------
  54. public static function isalnumat($str, $pos)
  55. {
  56. if ($pos >= strlen($str))
  57. return false;
  58. return (QRinput::lookAnTable(ord($str[$pos])) >= 0);
  59. }
  60. //----------------------------------------------------------------------
  61. public function identifyMode($pos)
  62. {
  63. if ($pos >= strlen($this->dataStr))
  64. return Constants::QR_MODE_NUL;
  65. $c = $this->dataStr[$pos];
  66. if(self::isdigitat($this->dataStr, $pos)) {
  67. return Constants::QR_MODE_NUM;
  68. } else if(self::isalnumat($this->dataStr, $pos)) {
  69. return Constants::QR_MODE_AN;
  70. } else if($this->modeHint == Constants::QR_MODE_KANJI) {
  71. if ($pos+1 < strlen($this->dataStr))
  72. {
  73. $d = $this->dataStr[$pos+1];
  74. $word = (ord($c) << 8) | ord($d);
  75. if(($word >= 0x8140 && $word <= 0x9ffc) || ($word >= 0xe040 && $word <= 0xebbf)) {
  76. return Constants::QR_MODE_KANJI;
  77. }
  78. }
  79. }
  80. return Constants::QR_MODE_8;
  81. }
  82. //----------------------------------------------------------------------
  83. public function eatNum()
  84. {
  85. $ln = QRspec::lengthIndicator(Constants::QR_MODE_NUM, $this->input->getVersion());
  86. $p = 0;
  87. while(self::isdigitat($this->dataStr, $p)) {
  88. $p++;
  89. }
  90. $run = $p;
  91. $mode = $this->identifyMode($p);
  92. if($mode == Constants::QR_MODE_8) {
  93. $dif = QRinput::estimateBitsModeNum($run) + 4 + $ln
  94. + QRinput::estimateBitsMode8(1) // + 4 + l8
  95. - QRinput::estimateBitsMode8($run + 1); // - 4 - l8
  96. if($dif > 0) {
  97. return $this->eat8();
  98. }
  99. }
  100. if($mode == Constants::QR_MODE_AN) {
  101. $dif = QRinput::estimateBitsModeNum($run) + 4 + $ln
  102. + QRinput::estimateBitsModeAn(1) // + 4 + la
  103. - QRinput::estimateBitsModeAn($run + 1);// - 4 - la
  104. if($dif > 0) {
  105. return $this->eatAn();
  106. }
  107. }
  108. $ret = $this->input->append(Constants::QR_MODE_NUM, $run, str_split($this->dataStr));
  109. if($ret < 0)
  110. return -1;
  111. return $run;
  112. }
  113. //----------------------------------------------------------------------
  114. public function eatAn()
  115. {
  116. $la = QRspec::lengthIndicator(Constants::QR_MODE_AN, $this->input->getVersion());
  117. $ln = QRspec::lengthIndicator(Constants::QR_MODE_NUM, $this->input->getVersion());
  118. $p = 0;
  119. while(self::isalnumat($this->dataStr, $p)) {
  120. if(self::isdigitat($this->dataStr, $p)) {
  121. $q = $p;
  122. while(self::isdigitat($this->dataStr, $q)) {
  123. $q++;
  124. }
  125. $dif = QRinput::estimateBitsModeAn($p) // + 4 + la
  126. + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln
  127. - QRinput::estimateBitsModeAn($q); // - 4 - la
  128. if($dif < 0) {
  129. break;
  130. } else {
  131. $p = $q;
  132. }
  133. } else {
  134. $p++;
  135. }
  136. }
  137. $run = $p;
  138. if(!self::isalnumat($this->dataStr, $p)) {
  139. $dif = QRinput::estimateBitsModeAn($run) + 4 + $la
  140. + QRinput::estimateBitsMode8(1) // + 4 + l8
  141. - QRinput::estimateBitsMode8($run + 1); // - 4 - l8
  142. if($dif > 0) {
  143. return $this->eat8();
  144. }
  145. }
  146. $ret = $this->input->append(Constants::QR_MODE_AN, $run, str_split($this->dataStr));
  147. if($ret < 0)
  148. return -1;
  149. return $run;
  150. }
  151. //----------------------------------------------------------------------
  152. public function eatKanji()
  153. {
  154. $p = 0;
  155. while($this->identifyMode($p) == Constants::QR_MODE_KANJI) {
  156. $p += 2;
  157. }
  158. $ret = $this->input->append(Constants::QR_MODE_KANJI, $p, str_split($this->dataStr));
  159. if($ret < 0)
  160. return -1;
  161. return $ret;
  162. }
  163. //----------------------------------------------------------------------
  164. public function eat8()
  165. {
  166. $la = QRspec::lengthIndicator(Constants::QR_MODE_AN, $this->input->getVersion());
  167. $ln = QRspec::lengthIndicator(Constants::QR_MODE_NUM, $this->input->getVersion());
  168. $p = 1;
  169. $dataStrLen = strlen($this->dataStr);
  170. while($p < $dataStrLen) {
  171. $mode = $this->identifyMode($p);
  172. if($mode == Constants::QR_MODE_KANJI) {
  173. break;
  174. }
  175. if($mode == Constants::QR_MODE_NUM) {
  176. $q = $p;
  177. while(self::isdigitat($this->dataStr, $q)) {
  178. $q++;
  179. }
  180. $dif = QRinput::estimateBitsMode8($p) // + 4 + l8
  181. + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln
  182. - QRinput::estimateBitsMode8($q); // - 4 - l8
  183. if($dif < 0) {
  184. break;
  185. } else {
  186. $p = $q;
  187. }
  188. } else if($mode == Constants::QR_MODE_AN) {
  189. $q = $p;
  190. while(self::isalnumat($this->dataStr, $q)) {
  191. $q++;
  192. }
  193. $dif = QRinput::estimateBitsMode8($p) // + 4 + l8
  194. + QRinput::estimateBitsModeAn($q - $p) + 4 + $la
  195. - QRinput::estimateBitsMode8($q); // - 4 - l8
  196. if($dif < 0) {
  197. break;
  198. } else {
  199. $p = $q;
  200. }
  201. } else {
  202. $p++;
  203. }
  204. }
  205. $run = $p;
  206. $ret = $this->input->append(Constants::QR_MODE_8, $run, str_split($this->dataStr));
  207. if($ret < 0)
  208. return -1;
  209. return $run;
  210. }
  211. //----------------------------------------------------------------------
  212. public function splitString()
  213. {
  214. while (strlen($this->dataStr) > 0)
  215. {
  216. if($this->dataStr == '')
  217. return 0;
  218. $mode = $this->identifyMode(0);
  219. switch ($mode) {
  220. case Constants::QR_MODE_NUM: $length = $this->eatNum(); break;
  221. case Constants::QR_MODE_AN: $length = $this->eatAn(); break;
  222. case Constants::QR_MODE_KANJI:
  223. if ($hint == Constants::QR_MODE_KANJI)
  224. $length = $this->eatKanji();
  225. else $length = $this->eat8();
  226. break;
  227. default: $length = $this->eat8(); break;
  228. }
  229. if($length == 0) return 0;
  230. if($length < 0) return -1;
  231. $this->dataStr = substr($this->dataStr, $length);
  232. }
  233. }
  234. //----------------------------------------------------------------------
  235. public function toUpper()
  236. {
  237. $stringLen = strlen($this->dataStr);
  238. $p = 0;
  239. while ($p<$stringLen) {
  240. $mode = self::identifyMode(substr($this->dataStr, $p), $this->modeHint);
  241. if($mode == Constants::QR_MODE_KANJI) {
  242. $p += 2;
  243. } else {
  244. if (ord($this->dataStr[$p]) >= ord('a') && ord($this->dataStr[$p]) <= ord('z')) {
  245. $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
  246. }
  247. $p++;
  248. }
  249. }
  250. return $this->dataStr;
  251. }
  252. //----------------------------------------------------------------------
  253. public static function splitStringToQRinput($string, QRinput $input, $modeHint, $casesensitive = true)
  254. {
  255. if(is_null($string) || $string == '\0' || $string == '') {
  256. throw new Exception('empty string!!!');
  257. }
  258. $split = new QRsplit($string, $input, $modeHint);
  259. if(!$casesensitive)
  260. $split->toUpper();
  261. return $split->splitString();
  262. }
  263. }