QRinput.php 13.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
<?php
/*
* PHP QR Code encoder
*
* Input encoding class
*
* Based on libqrencode C library distributed under LGPL 2.1
* Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
namespace tinymeng\code\Gateways\qrcode;

define('STRUCTURE_HEADER_BITS',  20);
define('MAX_STRUCTURED_SYMBOLS', 16);

class QRinput {

    public $items;
    
    private $version;
    private $level;
    
    //----------------------------------------------------------------------
    public function __construct($version = 0, $level = QR_ECLEVEL_L)
    {
        if ($version < 0 || $version > QRSPEC_VERSION_MAX || $level > QR_ECLEVEL_H) {
            throw new Exception('Invalid version no');
            return NULL;
        }
        
        $this->version = $version;
        $this->level = $level;
    }
    
    //----------------------------------------------------------------------
    public function getVersion()
    {
        return $this->version;
    }
    
    //----------------------------------------------------------------------
    public function setVersion($version)
    {
        if($version < 0 || $version > QRSPEC_VERSION_MAX) {
            throw new Exception('Invalid version no');
            return -1;
        }

        $this->version = $version;

        return 0;
    }
    
    //----------------------------------------------------------------------
    public function getErrorCorrectionLevel()
    {
        return $this->level;
    }

    //----------------------------------------------------------------------
    public function setErrorCorrectionLevel($level)
    {
        if($level > QR_ECLEVEL_H) {
            throw new Exception('Invalid ECLEVEL');
            return -1;
        }

        $this->level = $level;

        return 0;
    }
    
    //----------------------------------------------------------------------
    public function appendEntry(QRinputItem $entry)
    {
        $this->items[] = $entry;
    }
    
    //----------------------------------------------------------------------
    public function append($mode, $size, $data)
    {
        try {
            $entry = new QRinputItem($mode, $size, $data);
            $this->items[] = $entry;
            return 0;
        } catch (Exception $e) {
            return -1;
        }
    }
    
    //----------------------------------------------------------------------
    
    public function insertStructuredAppendHeader($size, $index, $parity)
    {
        if( $size > MAX_STRUCTURED_SYMBOLS ) {
            throw new Exception('insertStructuredAppendHeader wrong size');
        }
        
        if( $index <= 0 || $index > MAX_STRUCTURED_SYMBOLS ) {
            throw new Exception('insertStructuredAppendHeader wrong index');
        }

        $buf = array($size, $index, $parity);
        
        try {
            $entry = new QRinputItem(QR_MODE_STRUCTURE, 3, buf);
            array_unshift($this->items, $entry);
            return 0;
        } catch (Exception $e) {
            return -1;
        }
    }

    //----------------------------------------------------------------------
    public function calcParity()
    {
        $parity = 0;
        
        foreach($this->items as $item) {
            if($item->mode != QR_MODE_STRUCTURE) {
                for($i=$item->size-1; $i>=0; $i--) {
                    $parity ^= $item->data[$i];
                }
            }
        }

        return $parity;
    }
    
    //----------------------------------------------------------------------
    public static function checkModeNum($size, $data)
    {
        for($i=0; $i<$size; $i++) {
            if((ord($data[$i]) < ord('0')) || (ord($data[$i]) > ord('9'))){
                return false;
            }
        }

        return true;
    }

    //----------------------------------------------------------------------
    public static function estimateBitsModeNum($size)
    {
        $w = (int)$size / 3;
        $bits = $w * 10;
        
        switch($size - $w * 3) {
            case 1:
                $bits += 4;
                break;
            case 2:
                $bits += 7;
                break;
            default:
                break;
        }

        return $bits;
    }
    
    //----------------------------------------------------------------------
    public static $anTable = array(
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
         0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 44, -1, -1, -1, -1, -1,
        -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
        25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
    );
    
    //----------------------------------------------------------------------
    public static function lookAnTable($c)
    {
        return (($c > 127)?-1:self::$anTable[$c]);
    }
    
    //----------------------------------------------------------------------
    public static function checkModeAn($size, $data)
    {
        for($i=0; $i<$size; $i++) {
            if (self::lookAnTable(ord($data[$i])) == -1) {
                return false;
            }
        }

        return true;
    }
    
    //----------------------------------------------------------------------
    public static function estimateBitsModeAn($size)
    {
        $w = (int)($size / 2);
        $bits = $w * 11;
        
        if($size & 1) {
            $bits += 6;
        }

        return $bits;
    }

    //----------------------------------------------------------------------
    public static function estimateBitsMode8($size)
    {
        return $size * 8;
    }
    
    //----------------------------------------------------------------------
    public function estimateBitsModeKanji($size)
    {
        return (int)(($size / 2) * 13);
    }
    
    //----------------------------------------------------------------------
    public static function checkModeKanji($size, $data)
    {
        if($size & 1)
            return false;

        for($i=0; $i<$size; $i+=2) {
            $val = (ord($data[$i]) << 8) | ord($data[$i+1]);
            if( $val < 0x8140 
            || ($val > 0x9ffc && $val < 0xe040) 
            || $val > 0xebbf) {
                return false;
            }
        }

        return true;
    }

    /***********************************************************************
     * Validation
     **********************************************************************/

    public static function check($mode, $size, $data)
    {
        if($size <= 0) 
            return false;

        switch($mode) {
            case QR_MODE_NUM:       return self::checkModeNum($size, $data);   break;
            case QR_MODE_AN:        return self::checkModeAn($size, $data);    break;
            case QR_MODE_KANJI:     return self::checkModeKanji($size, $data); break;
            case QR_MODE_8:         return true; break;
            case QR_MODE_STRUCTURE: return true; break;
            
            default:
                break;
        }

        return false;
    }
    
    
    //----------------------------------------------------------------------
    public function estimateBitStreamSize($version)
    {
        $bits = 0;

        foreach($this->items as $item) {
            $bits += $item->estimateBitStreamSizeOfEntry($version);
        }

        return $bits;
    }
    
    //----------------------------------------------------------------------
    public function estimateVersion()
    {
        $version = 0;
        $prev = 0;
        do {
            $prev = $version;
            $bits = $this->estimateBitStreamSize($prev);
            $version = QRspec::getMinimumVersion((int)(($bits + 7) / 8), $this->level);
            if ($version < 0) {
                return -1;
            }
        } while ($version > $prev);

        return $version;
    }
    
    //----------------------------------------------------------------------
    public static function lengthOfCode($mode, $version, $bits)
    {
        $payload = $bits - 4 - QRspec::lengthIndicator($mode, $version);
        switch($mode) {
            case QR_MODE_NUM:
                $chunks = (int)($payload / 10);
                $remain = $payload - $chunks * 10;
                $size = $chunks * 3;
                if($remain >= 7) {
                    $size += 2;
                } else if($remain >= 4) {
                    $size += 1;
                }
                break;
            case QR_MODE_AN:
                $chunks = (int)($payload / 11);
                $remain = $payload - $chunks * 11;
                $size = $chunks * 2;
                if($remain >= 6) 
                    $size++;
                break;
            case QR_MODE_8:
                $size = (int)($payload / 8);
                break;
            case QR_MODE_KANJI:
                $size = (int)(($payload / 13) * 2);
                break;
            case QR_MODE_STRUCTURE:
                $size = (int)($payload / 8);
                break;
            default:
                $size = 0;
                break;
        }
        
        $maxsize = QRspec::maximumWords($mode, $version);
        if($size < 0) $size = 0;
        if($size > $maxsize) $size = $maxsize;

        return $size;
    }
    
    //----------------------------------------------------------------------
    public function createBitStream()
    {
        $total = 0;

        foreach($this->items as $item) {
            $bits = $item->encodeBitStream($this->version);
            
            if($bits < 0) 
                return -1;
                
            $total += $bits;
        }

        return $total;
    }
    
    //----------------------------------------------------------------------
    public function convertData()
    {
        $ver = $this->estimateVersion();
        if($ver > $this->getVersion()) {
            $this->setVersion($ver);
        }

        for(;;) {
            $bits = $this->createBitStream();
            
            if($bits < 0) 
                return -1;
                
            $ver = QRspec::getMinimumVersion((int)(($bits + 7) / 8), $this->level);
            if($ver < 0) {
                throw new Exception('WRONG VERSION');
                return -1;
            } else if($ver > $this->getVersion()) {
                $this->setVersion($ver);
            } else {
                break;
            }
        }

        return 0;
    }
    
    //----------------------------------------------------------------------
    public function appendPaddingBit(&$bstream)
    {
        $bits = $bstream->size();
        $maxwords = QRspec::getDataLength($this->version, $this->level);
        $maxbits = $maxwords * 8;

        if ($maxbits == $bits) {
            return 0;
        }

        if ($maxbits - $bits < 5) {
            return $bstream->appendNum($maxbits - $bits, 0);
        }

        $bits += 4;
        $words = (int)(($bits + 7) / 8);

        $padding = new QRbitstream();
        $ret = $padding->appendNum($words * 8 - $bits + 4, 0);
        
        if($ret < 0) 
            return $ret;

        $padlen = $maxwords - $words;
        
        if($padlen > 0) {
            
            $padbuf = array();
            for($i=0; $i<$padlen; $i++) {
                $padbuf[$i] = ($i&1)?0x11:0xec;
            }
            
            $ret = $padding->appendBytes($padlen, $padbuf);
            
            if($ret < 0)
                return $ret;
            
        }

        $ret = $bstream->append($padding);
        
        return $ret;
    }

    //----------------------------------------------------------------------
    public function mergeBitStream()
    {
        if($this->convertData() < 0) {
            return null;
        }

        $bstream = new QRbitstream();
        
        foreach($this->items as $item) {
            $ret = $bstream->append($item->bstream);
            if($ret < 0) {
                return null;
            }
        }

        return $bstream;
    }

    //----------------------------------------------------------------------
    public function getBitStream()
    {

        $bstream = $this->mergeBitStream();
        
        if($bstream == null) {
            return null;
        }
        
        $ret = $this->appendPaddingBit($bstream);
        if($ret < 0) {
            return null;
        }

        return $bstream;
    }
    
    //----------------------------------------------------------------------
    public function getByteStream()
    {
        $bstream = $this->getBitStream();
        if($bstream == null) {
            return null;
        }
        
        return $bstream->toByte();
    }
}