Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Buffer
- {
- public $data = ""; // buffer data
- public $seek = 0; // current position
- // initialize buffer
- public function __construct($data = '', $seek = 0)
- {
- $this->data = $data;
- $this->seek = $seek;
- }
- public function read(&$fp)
- {
- $this->seek = 0;
- $this->data = '';
- while($str = fread($fp, 1024))
- {
- $this->data .= $str;
- }
- return $this->data;
- }
- public function write(&$fp)
- {
- $write = fwrite($fp, $this->data);
- $this->data = '';
- $this->seek = 0;
- return $write;
- }
- public function get()
- {
- return $this->data[$this->seek++];
- }
- public function put($n)
- {
- $this->data .= chr($n);
- return $this->seek = strlen($this->data) - 1;
- }
- // read a string
- public function getstring($seek = -1)
- {
- $this->seek = $seek < 0 ? $this->seek : $seek;
- $str = '';
- for(;$this->seek<strlen($this->data);)
- {
- $chr = $this->get();
- if(ord($chr) == 0) break;
- $str .= $chr;
- }
- return $str;
- }
- public function getbin($len = NULL)
- {
- $out = substr($this->data, $this->seek, $len);
- $this->seek += $len;
- return $out;
- }
- // get an integer
- public function getint($seek = -1)
- {
- $this->seek = $seek < 0 ? $this->seek : $seek;
- if(!$this->remaining()) return false;
- $c = ord($this->get());
- if($c == 128)
- {
- $n = ord($this->get());
- $n |= ord($this->get())<<8;
- return $n > ((1<<15)-1) ? $n - 1<<16 : $n;
- }
- if($c == 129)
- {
- $n = ord($this->get());
- $n |= (ord($this->get()))<<8;
- $n |= (ord($this->get()))<<16;
- $n |= (ord($this->get()))<<24;
- return $n > ((1<<31)-1) ? $n - 1<<32 : $n;
- }
- return $c > ((1<<7)-1) ? $c - (1<<8) : $c;
- }
- public function getuint($seek = -1)
- {
- $this->seek = $seek < 0 ? $this->seek : $seek;
- if(!$this->remaining()) return false;
- $c = ord($this->get());
- if($c == 128)
- {
- $n = ord($this->get());
- $n |= ord($this->get())<<8;
- return $n;
- }
- if($c == 129)
- {
- $n = ord($this->get());
- $n |= (ord($this->get()))<<8;
- $n |= (ord($this->get()))<<16;
- $n |= (ord($this->get()))<<24;
- return $n;
- }
- return $c;
- }
- public function getbyte($seek = -1)
- {
- $this->seek = $seek < 0 ? $this->seek : $seek;
- if(!$this->remaining()) return false;
- $c = ord($this->get());
- return $c;
- }
- // send a string
- public function putstring($str)
- {
- for($i = 0; $i<strlen($str); $i++)
- {
- $this->data .= $str{$i};
- }
- $this->put(0);
- }
- public function putbin($bin)
- {
- $this->data .= $bin;
- $this->seek = strlen($this->data) - 1;
- }
- // send an integer
- public function putint($n)
- {
- if($n<128 && $n>-127) $this->put($n);
- else if($n<0x8000 && $n>=-0x8000) { $this->put(0x80); $this->put($n); $this->put($n>>8); }
- else { $this->put(0x81); $this->put($n); $this->put($n>>8); $this->put($n>>16); $this->put($n>>24); }
- }
- public function remaining()
- {
- return max(0, strlen($this->data) - $this->seek);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement