qrsplit.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. class QRsplit {
  34. public $dataStr = '';
  35. public $input;
  36. public $modeHint;
  37. //----------------------------------------------------------------------
  38. public function __construct($dataStr, $input, $modeHint)
  39. {
  40. $this->dataStr = $dataStr;
  41. $this->input = $input;
  42. $this->modeHint = $modeHint;
  43. }
  44. //----------------------------------------------------------------------
  45. public static function isdigitat($str, $pos)
  46. {
  47. if ($pos >= strlen($str))
  48. return false;
  49. return ((ord($str[$pos]) >= ord('0'))&&(ord($str[$pos]) <= ord('9')));
  50. }
  51. //----------------------------------------------------------------------
  52. public static function isalnumat($str, $pos)
  53. {
  54. if ($pos >= strlen($str))
  55. return false;
  56. return (QRinput::lookAnTable(ord($str[$pos])) >= 0);
  57. }
  58. //----------------------------------------------------------------------
  59. public function identifyMode($pos)
  60. {
  61. if ($pos >= strlen($this->dataStr))
  62. return QR_MODE_NUL;
  63. $c = $this->dataStr[$pos];
  64. if(self::isdigitat($this->dataStr, $pos)) {
  65. return QR_MODE_NUM;
  66. } else if(self::isalnumat($this->dataStr, $pos)) {
  67. return QR_MODE_AN;
  68. } else if($this->modeHint == QR_MODE_KANJI) {
  69. if ($pos+1 < strlen($this->dataStr))
  70. {
  71. $d = $this->dataStr[$pos+1];
  72. $word = (ord($c) << 8) | ord($d);
  73. if(($word >= 0x8140 && $word <= 0x9ffc) || ($word >= 0xe040 && $word <= 0xebbf)) {
  74. return QR_MODE_KANJI;
  75. }
  76. }
  77. }
  78. return QR_MODE_8;
  79. }
  80. //----------------------------------------------------------------------
  81. public function eatNum()
  82. {
  83. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  84. $p = 0;
  85. while(self::isdigitat($this->dataStr, $p)) {
  86. $p++;
  87. }
  88. $run = $p;
  89. $mode = $this->identifyMode($p);
  90. if($mode == QR_MODE_8) {
  91. $dif = QRinput::estimateBitsModeNum($run) + 4 + $ln
  92. + QRinput::estimateBitsMode8(1) // + 4 + l8
  93. - QRinput::estimateBitsMode8($run + 1); // - 4 - l8
  94. if($dif > 0) {
  95. return $this->eat8();
  96. }
  97. }
  98. if($mode == QR_MODE_AN) {
  99. $dif = QRinput::estimateBitsModeNum($run) + 4 + $ln
  100. + QRinput::estimateBitsModeAn(1) // + 4 + la
  101. - QRinput::estimateBitsModeAn($run + 1);// - 4 - la
  102. if($dif > 0) {
  103. return $this->eatAn();
  104. }
  105. }
  106. $ret = $this->input->append(QR_MODE_NUM, $run, str_split($this->dataStr));
  107. if($ret < 0)
  108. return -1;
  109. return $run;
  110. }
  111. //----------------------------------------------------------------------
  112. public function eatAn()
  113. {
  114. $la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion());
  115. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  116. $p = 0;
  117. while(self::isalnumat($this->dataStr, $p)) {
  118. if(self::isdigitat($this->dataStr, $p)) {
  119. $q = $p;
  120. while(self::isdigitat($this->dataStr, $q)) {
  121. $q++;
  122. }
  123. $dif = QRinput::estimateBitsModeAn($p) // + 4 + la
  124. + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln
  125. - QRinput::estimateBitsModeAn($q); // - 4 - la
  126. if($dif < 0) {
  127. break;
  128. } else {
  129. $p = $q;
  130. }
  131. } else {
  132. $p++;
  133. }
  134. }
  135. $run = $p;
  136. if(!self::isalnumat($this->dataStr, $p)) {
  137. $dif = QRinput::estimateBitsModeAn($run) + 4 + $la
  138. + QRinput::estimateBitsMode8(1) // + 4 + l8
  139. - QRinput::estimateBitsMode8($run + 1); // - 4 - l8
  140. if($dif > 0) {
  141. return $this->eat8();
  142. }
  143. }
  144. $ret = $this->input->append(QR_MODE_AN, $run, str_split($this->dataStr));
  145. if($ret < 0)
  146. return -1;
  147. return $run;
  148. }
  149. //----------------------------------------------------------------------
  150. public function eatKanji()
  151. {
  152. $p = 0;
  153. while($this->identifyMode($p) == QR_MODE_KANJI) {
  154. $p += 2;
  155. }
  156. $ret = $this->input->append(QR_MODE_KANJI, $p, str_split($this->dataStr));
  157. if($ret < 0)
  158. return -1;
  159. return $run;
  160. }
  161. //----------------------------------------------------------------------
  162. public function eat8()
  163. {
  164. $la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion());
  165. $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
  166. $p = 1;
  167. $dataStrLen = strlen($this->dataStr);
  168. while($p < $dataStrLen) {
  169. $mode = $this->identifyMode($p);
  170. if($mode == QR_MODE_KANJI) {
  171. break;
  172. }
  173. if($mode == QR_MODE_NUM) {
  174. $q = $p;
  175. while(self::isdigitat($this->dataStr, $q)) {
  176. $q++;
  177. }
  178. $dif = QRinput::estimateBitsMode8($p) // + 4 + l8
  179. + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln
  180. - QRinput::estimateBitsMode8($q); // - 4 - l8
  181. if($dif < 0) {
  182. break;
  183. } else {
  184. $p = $q;
  185. }
  186. } else if($mode == QR_MODE_AN) {
  187. $q = $p;
  188. while(self::isalnumat($this->dataStr, $q)) {
  189. $q++;
  190. }
  191. $dif = QRinput::estimateBitsMode8($p) // + 4 + l8
  192. + QRinput::estimateBitsModeAn($q - $p) + 4 + $la
  193. - QRinput::estimateBitsMode8($q); // - 4 - l8
  194. if($dif < 0) {
  195. break;
  196. } else {
  197. $p = $q;
  198. }
  199. } else {
  200. $p++;
  201. }
  202. }
  203. $run = $p;
  204. $ret = $this->input->append(QR_MODE_8, $run, str_split($this->dataStr));
  205. if($ret < 0)
  206. return -1;
  207. return $run;
  208. }
  209. //----------------------------------------------------------------------
  210. public function splitString()
  211. {
  212. while (strlen($this->dataStr) > 0)
  213. {
  214. if($this->dataStr == '')
  215. return 0;
  216. $mode = $this->identifyMode(0);
  217. switch ($mode) {
  218. case QR_MODE_NUM: $length = $this->eatNum(); break;
  219. case QR_MODE_AN: $length = $this->eatAn(); break;
  220. case QR_MODE_KANJI:
  221. if ($hint == QR_MODE_KANJI)
  222. $length = $this->eatKanji();
  223. else $length = $this->eat8();
  224. break;
  225. default: $length = $this->eat8(); break;
  226. }
  227. if($length == 0) return 0;
  228. if($length < 0) return -1;
  229. $this->dataStr = substr($this->dataStr, $length);
  230. }
  231. }
  232. //----------------------------------------------------------------------
  233. public function toUpper()
  234. {
  235. $stringLen = strlen($this->dataStr);
  236. $p = 0;
  237. while ($p<$stringLen) {
  238. $mode = self::identifyMode(substr($this->dataStr, $p), $this->modeHint);
  239. if($mode == QR_MODE_KANJI) {
  240. $p += 2;
  241. } else {
  242. if (ord($this->dataStr[$p]) >= ord('a') && ord($this->dataStr[$p]) <= ord('z')) {
  243. $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
  244. }
  245. $p++;
  246. }
  247. }
  248. return $this->dataStr;
  249. }
  250. //----------------------------------------------------------------------
  251. public static function splitStringToQRinput($string, QRinput $input, $modeHint, $casesensitive = true)
  252. {
  253. if(is_null($string) || $string == '\0' || $string == '') {
  254. throw new Exception('empty string!!!');
  255. }
  256. $split = new QRsplit($string, $input, $modeHint);
  257. if(!$casesensitive)
  258. $split->toUpper();
  259. return $split->splitString();
  260. }
  261. }