PageRenderTime 71ms CodeModel.GetById 21ms RepoModel.GetById 5ms app.codeStats 0ms

/lib/phpqrcode/qrsplit.php

https://github.com/iarna/Tiny-Tiny-RSS
PHP | 311 lines | 219 code | 50 blank | 42 comment | 65 complexity | f5160b012d1f6993e50e25a38275510d MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0, LGPL-3.0
  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 $ret;
  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 ($this->modeHint == 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));
  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. }