File indexing completed on 2025-01-19 05:21:21
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Pdf 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 0023 /** Zend_Pdf_Filter_Interface */ 0024 // require_once 'Zend/Pdf/Filter/Interface.php'; 0025 0026 /** 0027 * ASCII85 stream filter 0028 * 0029 * @package Zend_Pdf 0030 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0031 * @license http://framework.zend.com/license/new-bsd New BSD License 0032 */ 0033 class Zend_Pdf_Filter_Ascii85 implements Zend_Pdf_Filter_Interface 0034 { 0035 /** 0036 * Encode data 0037 * 0038 * @param string $data 0039 * @param array $params 0040 * @return string 0041 * @throws Zend_Pdf_Exception 0042 */ 0043 public static function encode($data, $params = null) 0044 { 0045 $output = ''; 0046 $dataLength = strlen($data); 0047 0048 for ($i = 0; $i < $dataLength; $i += 4) { 0049 //convert the 4 characters into a 32-bit number 0050 $chunk = substr($data, $i, 4); 0051 0052 if (strlen($chunk) < 4) { 0053 //partial chunk 0054 break; 0055 } 0056 0057 $b = unpack("N", $chunk); 0058 $b = $b[1]; 0059 0060 //special char for all 4 bytes = 0 0061 if ($b == 0) { 0062 $output .= 'z'; 0063 continue; 0064 } 0065 0066 //encode into 5 bytes 0067 for ($j = 4; $j >= 0; $j--) { 0068 $foo = (int) (($b / pow(85,$j)) + 33); 0069 $b %= pow(85,$j); 0070 $output .= chr($foo); 0071 } 0072 } 0073 0074 //encode partial chunk 0075 if ($i < $dataLength) { 0076 $n = $dataLength - $i; 0077 $chunk = substr($data, -$n); 0078 0079 //0 pad the rest 0080 for ($j = $n;$j < 4;$j++) { 0081 $chunk .= "\0"; 0082 } 0083 0084 $b = unpack("N", $chunk); 0085 $b = $b[1]; 0086 0087 //encode just $n + 1 0088 for ($j = 4; $j >= (4 - $n); $j--) { 0089 $foo = (int) (($b / pow(85,$j)) + 33); 0090 $b %= pow(85,$j); 0091 $output .= chr($foo); 0092 } 0093 } 0094 0095 //EOD 0096 $output .= '~>'; 0097 0098 //make sure lines are split 0099 $output = chunk_split($output, 76, "\n"); 0100 0101 //get rid of new line at the end 0102 $output = substr($output, 0, -1); 0103 return $output; 0104 } 0105 0106 /** 0107 * Decode data 0108 * 0109 * @param string $data 0110 * @param array $params 0111 * @return string 0112 * @throws Zend_Pdf_Exception 0113 */ 0114 public static function decode($data, $params = null) 0115 { 0116 $output = ''; 0117 0118 //get rid of the whitespaces 0119 $whiteSpace = array("\x00", "\x09", "\x0A", "\x0C", "\x0D", "\x20"); 0120 $data = str_replace($whiteSpace, '', $data); 0121 0122 if (substr($data, -2) != '~>') { 0123 // require_once 'Zend/Pdf/Exception.php'; 0124 throw new Zend_Pdf_Exception('Invalid EOF marker'); 0125 return ''; 0126 } 0127 0128 $data = substr($data, 0, (strlen($data) - 2)); 0129 $dataLength = strlen($data); 0130 0131 for ($i = 0; $i < $dataLength; $i += 5) { 0132 $b = 0; 0133 0134 if (substr($data, $i, 1) == "z") { 0135 $i -= 4; 0136 $output .= pack("N", 0); 0137 continue; 0138 } 0139 0140 $c = substr($data, $i, 5); 0141 0142 if(strlen($c) < 5) { 0143 //partial chunk 0144 break; 0145 } 0146 0147 $c = unpack('C5', $c); 0148 $value = 0; 0149 0150 for ($j = 1; $j <= 5; $j++) { 0151 $value += (($c[$j] - 33) * pow(85, (5 - $j))); 0152 } 0153 0154 $output .= pack("N", $value); 0155 } 0156 0157 //decode partial 0158 if ($i < $dataLength) { 0159 $value = 0; 0160 $chunk = substr($data, $i); 0161 $partialLength = strlen($chunk); 0162 0163 //pad the rest of the chunk with u's 0164 //until the lenght of the chunk is 5 0165 for ($j = 0; $j < (5 - $partialLength); $j++) { 0166 $chunk .= 'u'; 0167 } 0168 0169 $c = unpack('C5', $chunk); 0170 0171 for ($j = 1; $j <= 5; $j++) { 0172 $value += (($c[$j] - 33) * pow(85, (5 - $j))); 0173 } 0174 0175 $foo = pack("N", $value); 0176 $output .= substr($foo, 0, ($partialLength - 1)); 0177 } 0178 0179 return $output; 0180 } 0181 }