count-delta.con commit git-rev-parse: parse ".." before simple SHA1's (800644c)
   1/*
   2 * Copyright (C) 2005 Junio C Hamano
   3 * The delta-parsing part is almost straight copy of patch-delta.c
   4 * which is (C) 2005 Nicolas Pitre <nico@cam.org>.
   5 */
   6#include <stdlib.h>
   7#include <string.h>
   8#include <limits.h>
   9#include "count-delta.h"
  10
  11static unsigned long get_hdr_size(const unsigned char **datap)
  12{
  13        const unsigned char *data = *datap;
  14        unsigned long size;
  15        unsigned char cmd;
  16        int i;
  17        size = i = 0;
  18        cmd = *data++;
  19        while (cmd) {
  20                if (cmd & 1)
  21                        size |= *data++ << i;
  22                i += 8;
  23                cmd >>= 1;
  24        }
  25        *datap = data;
  26        return size;
  27}
  28
  29/*
  30 * NOTE.  We do not _interpret_ delta fully.  As an approximation, we
  31 * just count the number of bytes that are copied from the source, and
  32 * the number of literal data bytes that are inserted.
  33 *
  34 * Number of bytes that are _not_ copied from the source is deletion,
  35 * and number of inserted literal bytes are addition, so sum of them
  36 * is the extent of damage.  xdelta can express an edit that copies
  37 * data inside of the destination which originally came from the
  38 * source.  We do not count that in the following routine, so we are
  39 * undercounting the source material that remains in the final output
  40 * that way.
  41 */
  42int count_delta(void *delta_buf, unsigned long delta_size,
  43                unsigned long *src_copied, unsigned long *literal_added)
  44{
  45        unsigned long copied_from_source, added_literal;
  46        const unsigned char *data, *top;
  47        unsigned char cmd;
  48        unsigned long src_size, dst_size, out;
  49
  50        /* the smallest delta size possible is 6 bytes */
  51        if (delta_size < 6)
  52                return -1;
  53
  54        data = delta_buf;
  55        top = delta_buf + delta_size;
  56
  57        src_size = get_hdr_size(&data);
  58        dst_size = get_hdr_size(&data);
  59
  60        added_literal = copied_from_source = out = 0;
  61        while (data < top) {
  62                cmd = *data++;
  63                if (cmd & 0x80) {
  64                        unsigned long cp_off = 0, cp_size = 0;
  65                        if (cmd & 0x01) cp_off = *data++;
  66                        if (cmd & 0x02) cp_off |= (*data++ << 8);
  67                        if (cmd & 0x04) cp_off |= (*data++ << 16);
  68                        if (cmd & 0x08) cp_off |= (*data++ << 24);
  69                        if (cmd & 0x10) cp_size = *data++;
  70                        if (cmd & 0x20) cp_size |= (*data++ << 8);
  71                        if (cp_size == 0) cp_size = 0x10000;
  72
  73                        if (cmd & 0x40)
  74                                /* copy from dst */
  75                                ;
  76                        else
  77                                copied_from_source += cp_size;
  78                        out += cp_size;
  79                } else {
  80                        /* write literal into dst */
  81                        added_literal += cmd;
  82                        out += cmd;
  83                        data += cmd;
  84                }
  85        }
  86
  87        /* sanity check */
  88        if (data != top || out != dst_size)
  89                return -1;
  90
  91        /* delete size is what was _not_ copied from source.
  92         * edit size is that and literal additions.
  93         */
  94        *src_copied = copied_from_source;
  95        *literal_added = added_literal;
  96        return 0;
  97}