PageRenderTime 43ms CodeModel.GetById 29ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://zoop.googlecode.com/
PHP | 181 lines | 91 code | 32 blank | 58 comment | 18 complexity | 1f16d049fa2b4df656813b9e493b236e 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: Ascii85.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 * ASCII85 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_Ascii85 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        $output = '';
 46        $dataLength = strlen($data);
 47
 48        for ($i = 0; $i < $dataLength; $i += 4) {
 49            //convert the 4 characters into a 32-bit number
 50            $chunk = substr($data, $i, 4);
 51
 52            if (strlen($chunk) < 4) {
 53                //partial chunk
 54                break;
 55            }
 56
 57            $b = unpack("N", $chunk);
 58            $b = $b[1];
 59
 60            //special char for all 4 bytes = 0
 61            if ($b == 0) {
 62                $output .= 'z';
 63                continue;
 64            }
 65
 66            //encode into 5 bytes
 67            for ($j = 4; $j >= 0; $j--) {
 68                $foo = (int) (($b / pow(85,$j)) + 33);
 69                $b %= pow(85,$j);
 70                $output .= chr($foo);
 71            }
 72        }
 73
 74        //encode partial chunk
 75        if ($i < $dataLength) {
 76            $n = $dataLength - $i;
 77            $chunk = substr($data, -$n);
 78
 79            //0 pad the rest
 80            for ($j = $n;$j < 4;$j++) {
 81                $chunk .= chr(0);
 82            }
 83
 84            $b = unpack("N", $chunk);
 85            $b = $b[1];
 86
 87            //encode just $n + 1
 88            for ($j = 4; $j >= (4 - $n); $j--) {
 89                $foo = (int) (($b / pow(85,$j)) + 33);
 90                $b %= pow(85,$j);
 91                $output .= chr($foo);
 92            }
 93        }
 94
 95        //EOD
 96        $output .= '~>';
 97
 98        //make sure lines are split
 99        $output = chunk_split($output, 76, "\n");
100
101        //get rid of new line at the end
102        $output = substr($output, 0, -1);
103        return $output;
104    }
105
106    /**
107     * Decode data
108     *
109     * @param string $data
110     * @param array $params
111     * @return string
112     * @throws Zend_Pdf_Exception
113     */
114    public static function decode($data, $params = null)
115    {
116        $output = '';
117
118        //get rid of the whitespaces
119        $whiteSpace = array("\x00", "\x09", "\x0A", "\x0C", "\x0D", "\x20");
120        $data = str_replace($whiteSpace, '', $data);
121
122        if (substr($data, -2) != '~>') {
123            require_once 'Zend/Pdf/Exception.php';
124            throw new Zend_Pdf_Exception('Invalid EOF marker');
125            return '';
126        }
127
128        $data = substr($data, 0, (strlen($data) - 2));
129        $dataLength = strlen($data);
130
131        for ($i = 0; $i < $dataLength; $i += 5) {
132            $b = 0;
133
134            if (substr($data, $i, 1) == "z") {
135                $i -= 4;
136                $output .= pack("N", 0);
137                next;
138            }
139
140            $c = substr($data, $i, 5);
141
142            if(strlen($c) < 5) {
143                //partial chunk
144                break;
145            }
146
147            $c = unpack('C5', $c);
148            $value = 0;
149
150            for ($j = 1; $j <= 5; $j++) {
151                $value += (($c[$j] - 33) * pow(85, (5 - $j)));
152            }
153
154            $output .= pack("N", $value);
155        }
156
157        //decode partial
158        if ($i < $dataLength) {
159            $value = 0;
160            $chunk = substr($data, $i);
161            $partialLength = strlen($chunk);
162
163            //pad the rest of the chunk with u's
164            //until the lenght of the chunk is 5
165            for ($j = 0; $j < (5 - $partialLength); $j++) {
166                $chunk .= 'u';
167            }
168
169            $c = unpack('C5', $chunk);
170
171            for ($j = 1; $j <= 5; $j++) {
172                $value += (($c[$j] - 33) * pow(85, (5 - $j)));
173            }
174
175            $foo = pack("N", $value);
176            $output .= substr($foo, 0, ($partialLength - 1));
177        }
178
179        return $output;
180    }
181}