/**
  * @return int
  */
 public function encodeModeStructure()
 {
     try {
         $bs = new BitStream();
         $bs->appendNum(4, 0x3);
         $bs->appendNum(4, ord($this->data[1]) - 1);
         $bs->appendNum(4, ord($this->data[0]) - 1);
         $bs->appendNum(8, ord($this->data[2]));
         $this->bStream = $bs;
     } catch (\Exception $e) {
         return -1;
     }
     return 0;
 }
Esempio n. 2
0
 /**
  * @param BitStream $bStream
  *
  * @return int
  */
 public function appendPaddingBit(BitStream &$bStream)
 {
     $bits = $bStream->size();
     $maxWords = Specifications::getDataLength($this->version, $this->level);
     $maxBits = $maxWords * 8;
     if ($maxBits == $bits) {
         return 0;
     }
     if ($maxBits - $bits < 5) {
         return $bStream->appendNum($maxBits - $bits, 0);
     }
     $bits += 4;
     $words = (int) (($bits + 7) / 8);
     $padding = new BitStream();
     $ret = $padding->appendNum($words * 8 - $bits + 4, 0);
     if ($ret < 0) {
         return $ret;
     }
     $padLen = $maxWords - $words;
     if ($padLen > 0) {
         $padBuf = [];
         for ($i = 0; $i < $padLen; $i++) {
             $padBuf[$i] = $i & 1 ? 0x11 : 0xec;
         }
         $ret = $padding->appendBytes($padLen, $padBuf);
         if ($ret < 0) {
             return $ret;
         }
     }
     $ret = $bStream->append($padding);
     return $ret;
 }