Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  /**
   3   * This file is part of FPDI
   4   *
   5   * @package   setasign\Fpdi
   6   * @copyright Copyright (c) 2019 Setasign - Jan Slabon (https://www.setasign.com)
   7   * @license   http://opensource.org/licenses/mit-license The MIT License
   8   */
   9  
  10  namespace setasign\Fpdi\PdfParser\Filter;
  11  
  12  /**
  13   * Class for handling ASCII base-85 encoded data
  14   *
  15   * @package setasign\Fpdi\PdfParser\Filter
  16   */
  17  class Ascii85 implements FilterInterface
  18  {
  19      /**
  20       * Decode ASCII85 encoded string.
  21       *
  22       * @param string $data The input string
  23       * @return string
  24       * @throws Ascii85Exception
  25       */
  26      public function decode($data)
  27      {
  28          $out = '';
  29          $state = 0;
  30          $chn = null;
  31  
  32          $l = \strlen($data);
  33  
  34          /** @noinspection ForeachInvariantsInspection */
  35          for ($k = 0; $k < $l; ++$k) {
  36              $ch = \ord($data[$k]) & 0xff;
  37  
  38              //Start <~
  39              if ($k === 0 && $ch === 60 && isset($data[$k + 1]) && (\ord($data[$k + 1]) & 0xFF) === 126) {
  40                  $k++;
  41                  continue;
  42              }
  43              //End ~>
  44              if ($ch === 126 && isset($data[$k + 1]) && (\ord($data[$k + 1]) & 0xFF) === 62) {
  45                  break;
  46              }
  47              if (\preg_match('/^\s$/', \chr($ch))) {
  48                  continue;
  49              }
  50              if ($ch === 122 /* z */ && $state === 0) {
  51                  $out .= \chr(0) . \chr(0) . \chr(0) . \chr(0);
  52                  continue;
  53              }
  54              if ($ch < 33 /* ! */ || $ch > 117 /* u */) {
  55                  throw new Ascii85Exception(
  56                      'Illegal character found while ASCII85 decode.',
  57                      Ascii85Exception::ILLEGAL_CHAR_FOUND
  58                  );
  59              }
  60  
  61              $chn[$state] = $ch - 33;/* ! */
  62              $state++;
  63  
  64              if ($state === 5) {
  65                  $state = 0;
  66                  $r = 0;
  67                  for ($j = 0; $j < 5; ++$j) {
  68                      /** @noinspection UnnecessaryCastingInspection */
  69                      $r = (int)($r * 85 + $chn[$j]);
  70                  }
  71  
  72                  $out .= \chr($r >> 24)
  73                      . \chr($r >> 16)
  74                      . \chr($r >> 8)
  75                      . \chr($r);
  76              }
  77          }
  78  
  79          if ($state === 1) {
  80              throw new Ascii85Exception(
  81                  'Illegal length while ASCII85 decode.',
  82                  Ascii85Exception::ILLEGAL_LENGTH
  83              );
  84          }
  85  
  86          if ($state === 2) {
  87              $r = $chn[0] * 85 * 85 * 85 * 85 + ($chn[1] + 1) * 85 * 85 * 85;
  88              $out .= \chr($r >> 24);
  89  
  90          } elseif ($state === 3) {
  91              $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + ($chn[2] + 1) * 85 * 85;
  92              $out .= \chr($r >> 24);
  93              $out .= \chr($r >> 16);
  94  
  95          } elseif ($state === 4) {
  96              $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + $chn[2] * 85 * 85 + ($chn[3] + 1) * 85;
  97              $out .= \chr($r >> 24);
  98              $out .= \chr($r >> 16);
  99              $out .= \chr($r >> 8);
 100          }
 101  
 102          return $out;
 103      }
 104  }