Advertisement
lucasgautheron

Untitled

Aug 24th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. <?php
  2. class Buffer
  3. {
  4.     public $data = ""; //  buffer data
  5.     public $seek = 0; // current position
  6.    
  7.     // initialize buffer
  8.     public function __construct($data = '', $seek = 0)
  9.     {
  10.         $this->data = $data;
  11.         $this->seek = $seek;
  12.     }
  13.    
  14.     public function read(&$fp)
  15.     {
  16.         $this->seek = 0;
  17.         $this->data = '';
  18.         while($str = fread($fp, 1024))
  19.         {
  20.             $this->data .= $str;
  21.         }
  22.         return $this->data;
  23.     }
  24.    
  25.     public function write(&$fp)
  26.     {
  27.         $write = fwrite($fp, $this->data);
  28.         $this->data = '';
  29.         $this->seek = 0;
  30.         return $write;
  31.     }
  32.    
  33.     public function get()
  34.     {
  35.         return $this->data[$this->seek++];
  36.     }
  37.    
  38.     public function put($n)
  39.     {
  40.         $this->data .= chr($n);
  41.         return $this->seek = strlen($this->data) - 1;
  42.     }
  43.    
  44.     // read a string
  45.     public function getstring($seek = -1)
  46.     {
  47.         $this->seek = $seek < 0 ? $this->seek : $seek;
  48.         $str = '';
  49.         for(;$this->seek<strlen($this->data);)
  50.         {
  51.             $chr = $this->get();
  52.             if(ord($chr) == 0) break;
  53.             $str .= $chr;
  54.         }
  55.         return $str;
  56.     }
  57.    
  58.     public function getbin($len = NULL)
  59.     {
  60.         $out = substr($this->data, $this->seek, $len);
  61.         $this->seek += $len;
  62.         return $out;
  63.     }
  64.    
  65.     // get an integer
  66.     public function getint($seek = -1)
  67.     {
  68.         $this->seek = $seek < 0 ? $this->seek : $seek;
  69.         if(!$this->remaining()) return false;
  70.         $c = ord($this->get());
  71.         if($c == 128)
  72.         {
  73.             $n = ord($this->get());
  74.             $n |= ord($this->get())<<8;
  75.             return $n > ((1<<15)-1) ? $n - 1<<16 : $n;
  76.         }
  77.         if($c == 129)
  78.         {
  79.             $n = ord($this->get());
  80.             $n |= (ord($this->get()))<<8;
  81.             $n |= (ord($this->get()))<<16;
  82.             $n |= (ord($this->get()))<<24;
  83.             return  $n > ((1<<31)-1) ? $n - 1<<32 : $n;
  84.         }
  85.         return $c > ((1<<7)-1) ? $c - (1<<8) : $c;
  86.     }
  87.    
  88.     public function getuint($seek = -1)
  89.     {
  90.         $this->seek = $seek < 0 ? $this->seek : $seek;
  91.         if(!$this->remaining()) return false;
  92.         $c = ord($this->get());
  93.         if($c == 128)
  94.         {
  95.             $n = ord($this->get());
  96.             $n |= ord($this->get())<<8;
  97.             return $n;
  98.         }
  99.         if($c == 129)
  100.         {
  101.             $n = ord($this->get());
  102.             $n |= (ord($this->get()))<<8;
  103.             $n |= (ord($this->get()))<<16;
  104.             $n |= (ord($this->get()))<<24;
  105.             return $n;
  106.         }
  107.         return $c;
  108.     }
  109.    
  110.     public function getbyte($seek = -1)
  111.     {
  112.         $this->seek = $seek < 0 ? $this->seek : $seek;
  113.         if(!$this->remaining()) return false;
  114.         $c = ord($this->get());
  115.         return $c;
  116.     }
  117.    
  118.     // send a string
  119.     public function putstring($str)
  120.     {
  121.         for($i = 0; $i<strlen($str); $i++)
  122.         {
  123.             $this->data .= $str{$i};
  124.         }
  125.         $this->put(0);
  126.     }
  127.    
  128.     public function putbin($bin)
  129.     {
  130.         $this->data .= $bin;
  131.         $this->seek = strlen($this->data) - 1;
  132.     }
  133.    
  134.     // send an integer
  135.     public function putint($n)
  136.     {
  137.         if($n<128 && $n>-127) $this->put($n);
  138.         else if($n<0x8000 && $n>=-0x8000) { $this->put(0x80); $this->put($n); $this->put($n>>8); }
  139.         else { $this->put(0x81); $this->put($n); $this->put($n>>8); $this->put($n>>16); $this->put($n>>24); }
  140.     }
  141.  
  142.     public function remaining()
  143.     {
  144.         return max(0, strlen($this->data) - $this->seek);
  145.     }
  146. }
  147. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement