PageRenderTime 59ms CodeModel.GetById 54ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/library/html2pdf/fpdi/filters/FilterASCII85.php

https://bitbucket.org/DenizYldrm/openemr
PHP | 101 lines | 71 code | 13 blank | 17 comment | 28 complexity | af0a3a7c79322bd04c29f37d5ad7015f MD5 | raw file
  1<?php
  2//
  3//  FPDI - Version 1.4.2
  4//
  5//    Copyright 2004-2011 Setasign - Jan Slabon
  6//
  7//  Licensed under the Apache License, Version 2.0 (the "License");
  8//  you may not use this file except in compliance with the License.
  9//  You may obtain a copy of the License at
 10//
 11//      http://www.apache.org/licenses/LICENSE-2.0
 12//
 13//  Unless required by applicable law or agreed to in writing, software
 14//  distributed under the License is distributed on an "AS IS" BASIS,
 15//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16//  See the License for the specific language governing permissions and
 17//  limitations under the License.
 18//
 19
 20if (!defined('ORD_z'))
 21	define('ORD_z',ord('z'));
 22if (!defined('ORD_exclmark'))
 23	define('ORD_exclmark', ord('!'));
 24if (!defined('ORD_u'))	
 25	define('ORD_u', ord('u'));
 26if (!defined('ORD_tilde'))
 27	define('ORD_tilde', ord('~'));
 28
 29if (!class_exists('FilterASCII85', false)) {
 30
 31    class FilterASCII85 {
 32        
 33        function error($msg) {
 34            die($msg);
 35        }
 36        
 37        function decode($in) {
 38            $out = '';
 39            $state = 0;
 40            $chn = null;
 41            
 42            $l = strlen($in);
 43            
 44            for ($k = 0; $k < $l; ++$k) {
 45                $ch = ord($in[$k]) & 0xff;
 46                
 47                if ($ch == ORD_tilde) {
 48                    break;
 49                }
 50                if (preg_match('/^\s$/',chr($ch))) {
 51                    continue;
 52                }
 53                if ($ch == ORD_z && $state == 0) {
 54                    $out .= chr(0) . chr(0) . chr(0) . chr(0);
 55                    continue;
 56                }
 57                if ($ch < ORD_exclmark || $ch > ORD_u) {
 58                    return $this->error('Illegal character in ASCII85Decode.');
 59                }
 60                
 61                $chn[$state++] = $ch - ORD_exclmark;
 62                
 63                if ($state == 5) {
 64                    $state = 0;
 65                    $r = 0;
 66                    for ($j = 0; $j < 5; ++$j)
 67                        $r = $r * 85 + $chn[$j];
 68                    $out .= chr($r >> 24);
 69                    $out .= chr($r >> 16);
 70                    $out .= chr($r >> 8);
 71                    $out .= chr($r);
 72                }
 73            }
 74            $r = 0;
 75            
 76            if ($state == 1)
 77                return $this->error('Illegal length in ASCII85Decode.');
 78            if ($state == 2) {
 79                $r = $chn[0] * 85 * 85 * 85 * 85 + ($chn[1]+1) * 85 * 85 * 85;
 80                $out .= chr($r >> 24);
 81            }
 82            else if ($state == 3) {
 83                $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85  + ($chn[2]+1) * 85 * 85;
 84                $out .= chr($r >> 24);
 85                $out .= chr($r >> 16);
 86            }
 87            else if ($state == 4) {
 88                $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85  + $chn[2] * 85 * 85  + ($chn[3]+1) * 85 ;
 89                $out .= chr($r >> 24);
 90                $out .= chr($r >> 16);
 91                $out .= chr($r >> 8);
 92            }
 93    
 94            return $out;
 95        }
 96        
 97        function encode($in) {
 98            return $this->error("ASCII85 encoding not implemented.");
 99        }
100    }
101}