majiameng /
QrCode-php
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * PHP QR Code encoder |
||
| 4 | * |
||
| 5 | * Masking |
||
| 6 | * |
||
| 7 | * Based on libqrencode C library distributed under LGPL 2.1 |
||
| 8 | * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <[email protected]> |
||
| 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 tinymeng\code\Gateways\qrcode; |
||
| 28 | |||
| 29 | define('N1', 3); |
||
| 30 | define('N2', 3); |
||
| 31 | define('N3', 40); |
||
| 32 | define('N4', 10); |
||
| 33 | |||
| 34 | class QRmask { |
||
| 35 | |||
| 36 | public $runLength = array(); |
||
| 37 | |||
| 38 | //---------------------------------------------------------------------- |
||
| 39 | public function __construct() |
||
| 40 | { |
||
| 41 | $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0); |
||
| 42 | } |
||
| 43 | |||
| 44 | //---------------------------------------------------------------------- |
||
| 45 | public function writeFormatInformation($width, &$frame, $mask, $level) |
||
| 46 | { |
||
| 47 | $blacks = 0; |
||
| 48 | $format = QRspec::getFormatInfo($mask, $level); |
||
| 49 | |||
| 50 | for($i=0; $i<8; $i++) { |
||
| 51 | if($format & 1) { |
||
| 52 | $blacks += 2; |
||
| 53 | $v = 0x85; |
||
| 54 | } else { |
||
| 55 | $v = 0x84; |
||
| 56 | } |
||
| 57 | |||
| 58 | $frame[8][$width - 1 - $i] = chr($v); |
||
| 59 | if($i < 6) { |
||
| 60 | $frame[$i][8] = chr($v); |
||
| 61 | } else { |
||
| 62 | $frame[$i + 1][8] = chr($v); |
||
| 63 | } |
||
| 64 | $format = $format >> 1; |
||
| 65 | } |
||
| 66 | |||
| 67 | for($i=0; $i<7; $i++) { |
||
| 68 | if($format & 1) { |
||
| 69 | $blacks += 2; |
||
| 70 | $v = 0x85; |
||
| 71 | } else { |
||
| 72 | $v = 0x84; |
||
| 73 | } |
||
| 74 | |||
| 75 | $frame[$width - 7 + $i][8] = chr($v); |
||
| 76 | if($i == 0) { |
||
| 77 | $frame[8][7] = chr($v); |
||
| 78 | } else { |
||
| 79 | $frame[8][6 - $i] = chr($v); |
||
| 80 | } |
||
| 81 | |||
| 82 | $format = $format >> 1; |
||
| 83 | } |
||
| 84 | |||
| 85 | return $blacks; |
||
| 86 | } |
||
| 87 | |||
| 88 | //---------------------------------------------------------------------- |
||
| 89 | public function mask0($x, $y) { return ($x+$y)&1; } |
||
| 90 | public function mask1($x, $y) { return ($y&1); } |
||
| 91 | public function mask2($x, $y) { return ($x%3); } |
||
| 92 | public function mask3($x, $y) { return ($x+$y)%3; } |
||
| 93 | public function mask4($x, $y) { return (((int)($y/2))+((int)($x/3)))&1; } |
||
| 94 | public function mask5($x, $y) { return (($x*$y)&1)+($x*$y)%3; } |
||
| 95 | public function mask6($x, $y) { return ((($x*$y)&1)+($x*$y)%3)&1; } |
||
| 96 | public function mask7($x, $y) { return ((($x*$y)%3)+(($x+$y)&1))&1; } |
||
| 97 | |||
| 98 | //---------------------------------------------------------------------- |
||
| 99 | private function generateMaskNo($maskNo, $width, $frame) |
||
| 100 | { |
||
| 101 | $bitMask = array_fill(0, $width, array_fill(0, $width, 0)); |
||
| 102 | |||
| 103 | for($y=0; $y<$width; $y++) { |
||
| 104 | for($x=0; $x<$width; $x++) { |
||
| 105 | if(ord($frame[$y][$x]) & 0x80) { |
||
| 106 | $bitMask[$y][$x] = 0; |
||
| 107 | } else { |
||
| 108 | $maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y); |
||
| 109 | $bitMask[$y][$x] = ($maskFunc == 0)?1:0; |
||
| 110 | } |
||
| 111 | |||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return $bitMask; |
||
| 116 | } |
||
| 117 | |||
| 118 | //---------------------------------------------------------------------- |
||
| 119 | public static function serial($bitFrame) |
||
| 120 | { |
||
| 121 | $codeArr = array(); |
||
| 122 | |||
| 123 | foreach ($bitFrame as $line) |
||
| 124 | $codeArr[] = join('', $line); |
||
| 125 | |||
| 126 | return gzcompress(join("\n", $codeArr), 9); |
||
| 127 | } |
||
| 128 | |||
| 129 | //---------------------------------------------------------------------- |
||
| 130 | public static function unserial($code) |
||
| 131 | { |
||
| 132 | $codeArr = array(); |
||
| 133 | |||
| 134 | $codeLines = explode("\n", gzuncompress($code)); |
||
| 135 | foreach ($codeLines as $line) |
||
| 136 | $codeArr[] = str_split($line); |
||
| 137 | |||
| 138 | return $codeArr; |
||
| 139 | } |
||
| 140 | |||
| 141 | //---------------------------------------------------------------------- |
||
| 142 | public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false) |
||
| 143 | { |
||
| 144 | $b = 0; |
||
| 145 | $bitMask = array(); |
||
| 146 | |||
| 147 | $fileName = QR_CACHE_DIR.'mask_'.$maskNo.DIRECTORY_SEPARATOR.'mask_'.$width.'_'.$maskNo.'.dat'; |
||
| 148 | |||
| 149 | if (QR_CACHEABLE) { |
||
| 150 | if (file_exists($fileName)) { |
||
| 151 | $bitMask = self::unserial(file_get_contents($fileName)); |
||
| 152 | } else { |
||
| 153 | $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d); |
||
|
0 ignored issues
–
show
|
|||
| 154 | if (!file_exists(QR_CACHE_DIR.'mask_'.$maskNo)) |
||
| 155 | mkdir(QR_CACHE_DIR.'mask_'.$maskNo); |
||
| 156 | file_put_contents($fileName, self::serial($bitMask)); |
||
| 157 | } |
||
| 158 | } else { |
||
| 159 | $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d); |
||
| 160 | } |
||
| 161 | |||
| 162 | if ($maskGenOnly) |
||
| 163 | return; |
||
| 164 | |||
| 165 | $d = $s; |
||
| 166 | |||
| 167 | for($y=0; $y<$width; $y++) { |
||
| 168 | for($x=0; $x<$width; $x++) { |
||
| 169 | if($bitMask[$y][$x] == 1) { |
||
| 170 | $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int)$bitMask[$y][$x]); |
||
| 171 | } |
||
| 172 | $b += (int)(ord($d[$y][$x]) & 1); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | return $b; |
||
| 177 | } |
||
| 178 | |||
| 179 | //---------------------------------------------------------------------- |
||
| 180 | public function makeMask($width, $frame, $maskNo, $level) |
||
| 181 | { |
||
| 182 | $masked = array_fill(0, $width, str_repeat("\0", $width)); |
||
| 183 | $this->makeMaskNo($maskNo, $width, $frame, $masked); |
||
| 184 | $this->writeFormatInformation($width, $masked, $maskNo, $level); |
||
| 185 | |||
| 186 | return $masked; |
||
| 187 | } |
||
| 188 | |||
| 189 | //---------------------------------------------------------------------- |
||
| 190 | public function calcN1N3($length) |
||
| 191 | { |
||
| 192 | $demerit = 0; |
||
| 193 | |||
| 194 | for($i=0; $i<$length; $i++) { |
||
| 195 | |||
| 196 | if($this->runLength[$i] >= 5) { |
||
| 197 | $demerit += (N1 + ($this->runLength[$i] - 5)); |
||
| 198 | } |
||
| 199 | if($i & 1) { |
||
| 200 | if(($i >= 3) && ($i < ($length-2)) && ($this->runLength[$i] % 3 == 0)) { |
||
| 201 | $fact = (int)($this->runLength[$i] / 3); |
||
| 202 | if(($this->runLength[$i-2] == $fact) && |
||
| 203 | ($this->runLength[$i-1] == $fact) && |
||
| 204 | ($this->runLength[$i+1] == $fact) && |
||
| 205 | ($this->runLength[$i+2] == $fact)) { |
||
| 206 | if(($this->runLength[$i-3] < 0) || ($this->runLength[$i-3] >= (4 * $fact))) { |
||
| 207 | $demerit += N3; |
||
| 208 | } else if((($i+3) >= $length) || ($this->runLength[$i+3] >= (4 * $fact))) { |
||
| 209 | $demerit += N3; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | return $demerit; |
||
| 216 | } |
||
| 217 | |||
| 218 | //---------------------------------------------------------------------- |
||
| 219 | public function evaluateSymbol($width, $frame) |
||
| 220 | { |
||
| 221 | $head = 0; |
||
| 222 | $demerit = 0; |
||
| 223 | |||
| 224 | for($y=0; $y<$width; $y++) { |
||
| 225 | $head = 0; |
||
| 226 | $this->runLength[0] = 1; |
||
| 227 | |||
| 228 | $frameY = $frame[$y]; |
||
| 229 | |||
| 230 | if ($y>0) |
||
| 231 | $frameYM = $frame[$y-1]; |
||
| 232 | |||
| 233 | for($x=0; $x<$width; $x++) { |
||
| 234 | if(($x > 0) && ($y > 0)) { |
||
| 235 | $b22 = ord($frameY[$x]) & ord($frameY[$x-1]) & ord($frameYM[$x]) & ord($frameYM[$x-1]); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 236 | $w22 = ord($frameY[$x]) | ord($frameY[$x-1]) | ord($frameYM[$x]) | ord($frameYM[$x-1]); |
||
| 237 | |||
| 238 | if(($b22 | ($w22 ^ 1))&1) { |
||
| 239 | $demerit += N2; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | if(($x == 0) && (ord($frameY[$x]) & 1)) { |
||
| 243 | $this->runLength[0] = -1; |
||
| 244 | $head = 1; |
||
| 245 | $this->runLength[$head] = 1; |
||
| 246 | } else if($x > 0) { |
||
| 247 | if((ord($frameY[$x]) ^ ord($frameY[$x-1])) & 1) { |
||
| 248 | $head++; |
||
| 249 | $this->runLength[$head] = 1; |
||
| 250 | } else { |
||
| 251 | $this->runLength[$head]++; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | $demerit += $this->calcN1N3($head+1); |
||
| 257 | } |
||
| 258 | |||
| 259 | for($x=0; $x<$width; $x++) { |
||
| 260 | $head = 0; |
||
| 261 | $this->runLength[0] = 1; |
||
| 262 | |||
| 263 | for($y=0; $y<$width; $y++) { |
||
| 264 | if($y == 0 && (ord($frame[$y][$x]) & 1)) { |
||
| 265 | $this->runLength[0] = -1; |
||
| 266 | $head = 1; |
||
| 267 | $this->runLength[$head] = 1; |
||
| 268 | } else if($y > 0) { |
||
| 269 | if((ord($frame[$y][$x]) ^ ord($frame[$y-1][$x])) & 1) { |
||
| 270 | $head++; |
||
| 271 | $this->runLength[$head] = 1; |
||
| 272 | } else { |
||
| 273 | $this->runLength[$head]++; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | $demerit += $this->calcN1N3($head+1); |
||
| 279 | } |
||
| 280 | |||
| 281 | return $demerit; |
||
| 282 | } |
||
| 283 | |||
| 284 | |||
| 285 | //---------------------------------------------------------------------- |
||
| 286 | public function mask($width, $frame, $level) |
||
| 287 | { |
||
| 288 | $minDemerit = PHP_INT_MAX; |
||
| 289 | $bestMaskNum = 0; |
||
|
0 ignored issues
–
show
|
|||
| 290 | $bestMask = array(); |
||
|
0 ignored issues
–
show
|
|||
| 291 | |||
| 292 | $checked_masks = array(0,1,2,3,4,5,6,7); |
||
| 293 | |||
| 294 | if (QR_FIND_FROM_RANDOM !== false) { |
||
|
0 ignored issues
–
show
|
|||
| 295 | |||
| 296 | $howManuOut = 8-(QR_FIND_FROM_RANDOM % 9); |
||
| 297 | for ($i = 0; $i < $howManuOut; $i++) { |
||
| 298 | $remPos = rand (0, count($checked_masks)-1); |
||
| 299 | unset($checked_masks[$remPos]); |
||
| 300 | $checked_masks = array_values($checked_masks); |
||
| 301 | } |
||
| 302 | |||
| 303 | } |
||
| 304 | |||
| 305 | $bestMask = $frame; |
||
| 306 | |||
| 307 | foreach($checked_masks as $i) { |
||
| 308 | $mask = array_fill(0, $width, str_repeat("\0", $width)); |
||
| 309 | |||
| 310 | $demerit = 0; |
||
|
0 ignored issues
–
show
|
|||
| 311 | $blacks = 0; |
||
|
0 ignored issues
–
show
|
|||
| 312 | $blacks = $this->makeMaskNo($i, $width, $frame, $mask); |
||
| 313 | $blacks += $this->writeFormatInformation($width, $mask, $i, $level); |
||
| 314 | $blacks = (int)(100 * $blacks / ($width * $width)); |
||
| 315 | $demerit = (int)((int)(abs($blacks - 50) / 5) * N4); |
||
| 316 | $demerit += $this->evaluateSymbol($width, $mask); |
||
| 317 | |||
| 318 | if($demerit < $minDemerit) { |
||
| 319 | $minDemerit = $demerit; |
||
| 320 | $bestMask = $mask; |
||
| 321 | $bestMaskNum = $i; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | return $bestMask; |
||
| 326 | } |
||
| 327 | |||
| 328 | //---------------------------------------------------------------------- |
||
| 329 | } |
||
| 330 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.