hiphop-php /hphp/system/php/stream/default-filters.php

Language PHP Lines 144
MD5 Hash 2f2df590f884d63ddd60a78f8b242a70 Estimated Cost $3,095 (why?)
Repository git://github.com/facebook/hiphop-php.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php

namespace __SystemLib {
  function register_default_stream_filters() {
    \stream_filter_register('zlib.deflate', '__SystemLib\DeflateStreamFilter');
    \stream_filter_register('zlib.inflate', '__SystemLib\InflateStreamFilter');
    \stream_filter_register(
      'string.rot13',
      '__SystemLib\StringRot13StreamFilter'
    );
    \stream_filter_Register(
      'string.toupper',
      '__SystemLib\StringToUpperStreamFilter'
    );
    \stream_filter_Register(
      'string.tolower',
      '__SystemLib\StringToLowerStreamFilter'
    );
    \stream_filter_Register(
      'convert.iconv.*',
      '__SystemLib\ConvertIconvFilter'
    );
  }

  class ConvertIconvFilter extends \php_user_filter {
    private $fromEncoding;
    private $toEncoding;

    public function onCreate(): bool {
      /* strip out prefix "convert.iconv." */
      $filter = substr($this->filtername, 14);
      if (false === strpos($filter, '/')) {
        return false;
      }
      $encodingPair = explode('/', $filter, 2);
      $this->fromEncoding = strtolower($encodingPair[0]);
      $this->toEncoding = strtolower($encodingPair[1]);
      return true;
    }

    public function filter($in, $out, &$consumed, $closing): int {
      while ($bucket = stream_bucket_make_writeable($in)) {
        $convertedData = iconv(
          $this->fromEncoding,
          $this->toEncoding,
          $bucket->data
        );
        stream_bucket_append(
          $out,
          stream_bucket_new($this->stream, $convertedData)
        );
      }
      return \PSFS_PASS_ON;
    }
  }

  class DeflateStreamFilter extends \php_user_filter {
    private $level = -1; // zlib default
    private $buffer = '';

    public function filter($in, $out, &$consumed, $closing): int {
      while ($bucket = stream_bucket_make_writeable($in)) {
        $this->buffer .= $bucket->data;
      }

      if ($closing) {
        if (is_int($this->params)) {
          $this->level = $this->params;
        }
        if (is_array($this->params) && isset($this->params['level'])) {
          $this->level = $this->params['level'];
        }
        stream_bucket_append(
          $out,
          stream_bucket_new(
            $this->stream,
            gzdeflate($this->buffer, $this->level)
          )
        );
      }

      return \PSFS_PASS_ON;
    }
  }

  class InflateStreamFilter extends \php_user_filter {
    public function onCreate(): bool {
      $this->impl = new \__SystemLib\ChunkedInflator();
      return true;
    }

    public function filter($in, $out, &$consumed, $closing): int {
      while ($bucket = stream_bucket_make_writeable($in)) {
        if ($this->impl->eof()) {
          return \PSFS_ERR_FATAL;
        }
        $this_chunk = $this->impl->inflateChunk($bucket->data);
        stream_bucket_append(
          $out,
          stream_bucket_new($this->stream, $this_chunk)
        );
      }

      return \PSFS_PASS_ON;
    }
  }

  class StringToUpperStreamFilter extends \php_user_filter {
    public function filter($in, $out, &$consumed, $closing): int {
      while ($bucket = stream_bucket_make_writeable($in)) {
        stream_bucket_append(
          $out,
          stream_bucket_new($this->stream, strtoupper($bucket->data))
        );
      }
      return \PSFS_PASS_ON;
    }
  }

  class StringToLowerStreamFilter extends \php_user_filter {
    public function filter($in, $out, &$consumed, $closing): int {
      while ($bucket = stream_bucket_make_writeable($in)) {
        stream_bucket_append(
          $out,
          stream_bucket_new($this->stream, strtolower($bucket->data))
        );
      }
      return \PSFS_PASS_ON;
    }
  }

  class StringRot13StreamFilter extends \php_user_filter {
    public function filter($in, $out, &$consumed, $closing): int {
      while ($bucket = stream_bucket_make_writeable($in)) {
        stream_bucket_append(
          $out,
          stream_bucket_new($this->stream, str_rot13($bucket->data))
        );
      }
      return \PSFS_PASS_ON;
    }
  }
}
Back to Top