Esempio n. 1
0
 /**
  * @brief Apply the git delta $delta to the byte sequence $base.
  *
  * @param $delta (string) the delta to apply
  * @param $base (string) the sequence to patch
  * @returns (string) the patched byte sequence
  */
 protected function applyDelta($delta, $base)
 {
     $pos = 0;
     $baseSize = Binary::gitVarInt($delta, $pos);
     $resultSize = Binary::gitVarInt($delta, $pos);
     $result = '';
     while ($pos < strlen($delta)) {
         $opcode = ord($delta[$pos++]);
         if ($opcode & 0x80) {
             /* copy a part of $base */
             $off = 0;
             if ($opcode & 0x1) {
                 $off = ord($delta[$pos++]);
             }
             if ($opcode & 0x2) {
                 $off |= ord($delta[$pos++]) << 8;
             }
             if ($opcode & 0x4) {
                 $off |= ord($delta[$pos++]) << 16;
             }
             if ($opcode & 0x8) {
                 $off |= ord($delta[$pos++]) << 24;
             }
             $len = 0;
             if ($opcode & 0x10) {
                 $len = ord($delta[$pos++]);
             }
             if ($opcode & 0x20) {
                 $len |= ord($delta[$pos++]) << 8;
             }
             if ($opcode & 0x40) {
                 $len |= ord($delta[$pos++]) << 16;
             }
             if ($len == 0) {
                 $len = 0x10000;
             }
             $result .= substr($base, $off, $len);
         } else {
             /* take the next $opcode bytes as they are */
             $result .= substr($delta, $pos, $opcode);
             $pos += $opcode;
         }
     }
     return $result;
 }