<?php

class InputBitStream {
          private 
$_data;
          private 
$_byte_offset;
          private 
$_bit_offset;
      function 
__construct($data) {
              
$this->_data $data;
          
$this->_byte_offset 0;
          
$this->_bit_offset  0;
      }
      function 
getBits($bit_width) {
        
$value 0;
        for(
$i=0$i$bit_width$i++) {
          
$value <<= 1;
          
$value |= $this->getBit();
        }
        return 
$value;
      }
      function 
getBit() {
              
$shift_width $this->_bit_offset;
          
$bit = (ord($this->_data{$this->_byte_offset}) >> $shift_width) & 0x1;
          
$this->_bit_offset ++;
          if (
$this->_bit_offset == 8) {
            
$this->_byte_offset ++;
            
$this->_bit_offset 0;
          }
          return 
$bit;
      }
      function 
getBytesLE($bytes) {
              
$this->align();
          
$value 0;
          for (
$i=$i<$bytes $i++) {
            
$value >>= 8;
            
$value |= $this->getByte() << (* ($bytes 1));
          }
          return 
$value;
      }
      function 
getByte() {
              
$this->align();
              
$byte ord($this->_data{$this->_byte_offset});
          
$this->_byte_offset ++;
          return 
$byte;
      }
      function 
getStrings($len null) {
              
$this->align();
          if (
is_null($len)) {
                  
$len strlen($this->_data) - $this->_byte_offset;
          }
          
$str substr($this->_data$this->_byte_offset$len);
          
$this->_byte_offset += $len;
          return 
$str;
      }
      function 
align() {
              if (
$this->_bit_offset 0) {
                  
$this->_byte_offset ++;
              
$this->_bit_offset 0;
          }
      }
      function 
getByteOffset() {
              return 
$this->_byte_offset;
      }
      function 
setByteOffset($offset) {
              return 
$this->_byte_offset $offset;
      }
}

class 
OutputBitStream {
        private 
$_data;
    private 
$_byte_offset;
    private 
$_bit_offset;
    function 
__construct() {
            
$this->_data null;
        
$this->_byte_offset 0;
        
$this->_bit_offset  0;
    }
    function 
putBits($bits$bit_width) {
      return ;
    }
    function 
putBit($bit) {
      return ;
    }
    function 
putBytesLE($value$bytes) {
      return ;
    }
    function 
putByte($byte) {
      return ;
    }
    function 
flush() {
      return ;
    }
    function 
getByteOffset() {
      return 
$this->_byte_offset;
    }
}