PageRenderTime 51ms CodeModel.GetById 45ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/zend/Zend/Pdf/Filter/AsciiHex.php

http://zoop.googlecode.com/
PHP | 135 lines | 62 code | 15 blank | 58 comment | 23 complexity | efe58f1d76a0795be6c0beacad8bcd66 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Pdf
 17 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 19 * @version    $Id: AsciiHex.php 20096 2010-01-06 02:05:09Z bkarwin $
 20 */
 21
 22
 23/** Zend_Pdf_Filter_Interface */
 24require_once 'Zend/Pdf/Filter/Interface.php';
 25
 26/**
 27 * AsciiHex stream filter
 28 *
 29 * @package    Zend_Pdf
 30 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 31 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 32 */
 33class Zend_Pdf_Filter_AsciiHex implements Zend_Pdf_Filter_Interface
 34{
 35    /**
 36     * Encode data
 37     *
 38     * @param string $data
 39     * @param array $params
 40     * @return string
 41     * @throws Zend_Pdf_Exception
 42     */
 43    public static function encode($data, $params = null)
 44    {
 45        return bin2hex($data) . '>';
 46    }
 47
 48    /**
 49     * Decode data
 50     *
 51     * @param string $data
 52     * @param array $params
 53     * @return string
 54     * @throws Zend_Pdf_Exception
 55     */
 56    public static function decode($data, $params = null)
 57    {
 58        $output  = '';
 59        $oddCode = true;
 60        $commentMode = false;
 61
 62        for ($count = 0; $count < strlen($data)  &&  $data[$count] != '>'; $count++) {
 63            $charCode = ord($data[$count]);
 64
 65            if ($commentMode) {
 66                if ($charCode == 0x0A  || $charCode == 0x0D ) {
 67                    $commentMode = false;
 68                }
 69
 70                continue;
 71            }
 72
 73            switch ($charCode) {
 74                //Skip white space
 75                case 0x00: // null character
 76                    // fall through to next case
 77                case 0x09: // Tab
 78                    // fall through to next case
 79                case 0x0A: // Line feed
 80                    // fall through to next case
 81                case 0x0C: // Form Feed
 82                    // fall through to next case
 83                case 0x0D: // Carriage return
 84                    // fall through to next case
 85                case 0x20: // Space
 86                    // Do nothing
 87                    break;
 88
 89                case 0x25: // '%'
 90                    // Switch to comment mode
 91                    $commentMode = true;
 92                    break;
 93
 94                default:
 95                    if ($charCode >= 0x30 /*'0'*/ && $charCode <= 0x39 /*'9'*/) {
 96                        $code = $charCode - 0x30;
 97                    } else if ($charCode >= 0x41 /*'A'*/ && $charCode <= 0x46 /*'F'*/) {
 98                        $code = $charCode - 0x37/*0x41 - 0x0A*/;
 99                    } else if ($charCode >= 0x61 /*'a'*/ && $charCode <= 0x66 /*'f'*/) {
100                        $code = $charCode - 0x57/*0x61 - 0x0A*/;
101                    } else {
102                        require_once 'Zend/Pdf/Exception.php';
103                        throw new Zend_Pdf_Exception('Wrong character in a encoded stream');
104                    }
105
106                    if ($oddCode) {
107                        // Odd pass. Store hex digit for next pass
108                        // Scope of $hexCodeHigh variable is whole function
109                        $hexCodeHigh = $code;
110                    } else {
111                        // Even pass.
112                        // Add decoded character to the output
113                        // ($hexCodeHigh is stored in previous pass)
114                        $output .= chr($hexCodeHigh*16 + $code);
115                    }
116                    $oddCode = !$oddCode;
117
118                    break;
119            }
120        }
121
122        /* Check that stream is terminated by End Of Data marker */
123        if ($data[$count] != '>') {
124            require_once 'Zend/Pdf/Exception.php';
125            throw new Zend_Pdf_Exception('Wrong encoded stream End Of Data marker.');
126        }
127
128        /* Last '0' character is omitted */
129        if (!$oddCode) {
130            $output .= chr($hexCodeHigh*16);
131        }
132
133        return $output;
134    }
135}