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  * AsciiHex 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_AsciiHex 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         return bin2hex($data) . '>';
0046     }
0047 
0048     /**
0049      * Decode data
0050      *
0051      * @param string $data
0052      * @param array $params
0053      * @return string
0054      * @throws Zend_Pdf_Exception
0055      */
0056     public static function decode($data, $params = null)
0057     {
0058         $output  = '';
0059         $oddCode = true;
0060         $commentMode = false;
0061 
0062         for ($count = 0; $count < strlen($data)  &&  $data[$count] != '>'; $count++) {
0063             $charCode = ord($data[$count]);
0064 
0065             if ($commentMode) {
0066                 if ($charCode == 0x0A  || $charCode == 0x0D ) {
0067                     $commentMode = false;
0068                 }
0069 
0070                 continue;
0071             }
0072 
0073             switch ($charCode) {
0074                 //Skip white space
0075                 case 0x00: // null character
0076                     // fall through to next case
0077                 case 0x09: // Tab
0078                     // fall through to next case
0079                 case 0x0A: // Line feed
0080                     // fall through to next case
0081                 case 0x0C: // Form Feed
0082                     // fall through to next case
0083                 case 0x0D: // Carriage return
0084                     // fall through to next case
0085                 case 0x20: // Space
0086                     // Do nothing
0087                     break;
0088 
0089                 case 0x25: // '%'
0090                     // Switch to comment mode
0091                     $commentMode = true;
0092                     break;
0093 
0094                 default:
0095                     if ($charCode >= 0x30 /*'0'*/ && $charCode <= 0x39 /*'9'*/) {
0096                         $code = $charCode - 0x30;
0097                     } else if ($charCode >= 0x41 /*'A'*/ && $charCode <= 0x46 /*'F'*/) {
0098                         $code = $charCode - 0x37/*0x41 - 0x0A*/;
0099                     } else if ($charCode >= 0x61 /*'a'*/ && $charCode <= 0x66 /*'f'*/) {
0100                         $code = $charCode - 0x57/*0x61 - 0x0A*/;
0101                     } else {
0102                         // require_once 'Zend/Pdf/Exception.php';
0103                         throw new Zend_Pdf_Exception('Wrong character in a encoded stream');
0104                     }
0105 
0106                     if ($oddCode) {
0107                         // Odd pass. Store hex digit for next pass
0108                         // Scope of $hexCodeHigh variable is whole function
0109                         $hexCodeHigh = $code;
0110                     } else {
0111                         // Even pass.
0112                         // Add decoded character to the output
0113                         // ($hexCodeHigh is stored in previous pass)
0114                         $output .= chr($hexCodeHigh*16 + $code);
0115                     }
0116                     $oddCode = !$oddCode;
0117 
0118                     break;
0119             }
0120         }
0121 
0122         /* Check that stream is terminated by End Of Data marker */
0123         if ($data[$count] != '>') {
0124             // require_once 'Zend/Pdf/Exception.php';
0125             throw new Zend_Pdf_Exception('Wrong encoded stream End Of Data marker.');
0126         }
0127 
0128         /* Last '0' character is omitted */
0129         if (!$oddCode) {
0130             $output .= chr($hexCodeHigh*16);
0131         }
0132 
0133         return $output;
0134     }
0135 }