示例#1
0
文件: git.class.php 项目: Bombe/glip
 /**
  * @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;
     $base_size = Binary::git_varint($delta, &$pos);
     $result_size = Binary::git_varint($delta, &$pos);
     $r = '';
     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;
             }
             $r .= substr($base, $off, $len);
         } else {
             /* take the next $opcode bytes as they are */
             $r .= substr($delta, $pos, $opcode);
             $pos += $opcode;
         }
     }
     return $r;
 }