source-class-Com.Tecnick.Pdf.Filter.Type.AsciiEightFive

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1: <?php
  2: /**
  3:  * AsciiEightFive.php
  4:  *
  5:  * @since       2011-05-23
  6:  * @category    Library
  7:  * @package     PdfFilter
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-pdf-filter
 12:  *
 13:  * This file is part of tc-lib-pdf-filter software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Pdf\Filter\Type;
 17: 
 18: use \Com\Tecnick\Pdf\Filter\Exception as PPException;
 19: 
 20: /**
 21:  * Com\Tecnick\Pdf\Filter\Type\AsciiEightFive
 22:  *
 23:  * ASCII85
 24:  * Decodes data encoded in an ASCII base-85 representation, reproducing the original binary data.
 25:  *
 26:  * @since       2011-05-23
 27:  * @category    Library
 28:  * @package     PdfFilter
 29:  * @author      Nicola Asuni <info@tecnick.com>
 30:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 31:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 32:  * @link        https://github.com/tecnickcom/tc-lib-pdf-filter
 33:  */
 34: class AsciiEightFive
 35: {
 36:     /**
 37:      * Decode the data
 38:      *
 39:      * @param string $data Data to decode.
 40:      *
 41:      * @return string Decoded data string.
 42:      */
 43:     public function decode($data)
 44:     {
 45:         // initialize string to return
 46:         $decoded = '';
 47:         // all white-space characters shall be ignored
 48:         $data = preg_replace('/[\s]/', '', $data);
 49:         // remove start sequence 2-character sequence <~ (3Ch)(7Eh)
 50:         if (strpos($data, '<~') !== false) {
 51:             // remove EOD and extra data (if any)
 52:             $data = substr($data, 2);
 53:         }
 54:         // check for EOD: 2-character sequence ~> (7Eh)(3Eh)
 55:         $eod = strpos($data, '~>');
 56:         if ($eod !== false) {
 57:             // remove EOD and extra data (if any)
 58:             $data = substr($data, 0, $eod);
 59:         }
 60:         // data length
 61:         $data_length = strlen($data);
 62:         // check for invalid characters
 63:         if (preg_match('/[^\x21-\x75,\x74]/', $data) > 0) {
 64:             throw new PPException('invalid code');
 65:         }
 66:         // z sequence
 67:         $zseq = chr(0).chr(0).chr(0).chr(0);
 68:         // position inside a group of 4 bytes (0-3)
 69:         $group_pos = 0;
 70:         $tuple = 0;
 71:         $pow85 = array((85*85*85*85), (85*85*85), (85*85), 85, 1);
 72:         // for each byte
 73:         for ($i = 0; $i < $data_length; ++$i) {
 74:             // get char value
 75:             $char = ord($data[$i]);
 76:             if ($char == 122) { // 'z'
 77:                 if ($group_pos == 0) {
 78:                     $decoded .= $zseq;
 79:                 } else {
 80:                     throw new PPException('invalid code');
 81:                 }
 82:             } else {
 83:                 // the value represented by a group of 5 characters should never be greater than 2^32 - 1
 84:                 $tuple += (($char - 33) * $pow85[$group_pos]);
 85:                 if ($group_pos == 4) {
 86:                     $decoded .= chr($tuple >> 24).chr($tuple >> 16).chr($tuple >> 8).chr($tuple);
 87:                     $tuple = 0;
 88:                     $group_pos = 0;
 89:                 } else {
 90:                     ++$group_pos;
 91:                 }
 92:             }
 93:         }
 94:         if ($group_pos > 1) {
 95:             $tuple += $pow85[($group_pos - 1)];
 96:         }
 97:         $decoded .= $this->getLastTuple($group_pos, $tuple);
 98:         return $decoded;
 99:     }
100: 
101:     /**
102:      * Get last tuple
103:      *
104:      * @param int $group_pos
105:      * @param int $tuple
106:      *
107:      * @return string Decoded data string.
108:      */
109:     protected function getLastTuple($group_pos, $tuple)
110:     {
111:         // last tuple (if any)
112:         switch ($group_pos) {
113:             case 4:
114:                 return chr($tuple >> 24).chr($tuple >> 16).chr($tuple >> 8);
115:             case 3:
116:                 return chr($tuple >> 24).chr($tuple >> 16);
117:             case 2:
118:                 return chr($tuple >> 24);
119:             case 1:
120:                 throw new PPException('invalid code');
121:         }
122:         return '';
123:     }
124: }
125: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy