count-delta.con commit Make git pack files use little-endian size encoding (01247d8)
   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 char cmd = *data++;
  15        unsigned long size = cmd & ~0x80;
  16        int i = 7;
  17        while (cmd & 0x80) {
  18                cmd = *data++;
  19                size |= (cmd & ~0x80) << i;
  20                i += 7;
  21        }
  22        *datap = data;
  23        return size;
  24}
  25
  26/*
  27 * NOTE.  We do not _interpret_ delta fully.  As an approximation, we
  28 * just count the number of bytes that are copied from the source, and
  29 * the number of literal data bytes that are inserted.
  30 *
  31 * Number of bytes that are _not_ copied from the source is deletion,
  32 * and number of inserted literal bytes are addition, so sum of them
  33 * is the extent of damage.  xdelta can express an edit that copies
  34 * data inside of the destination which originally came from the
  35 * source.  We do not count that in the following routine, so we are
  36 * undercounting the source material that remains in the final output
  37 * that way.
  38 */
  39int count_delta(void *delta_buf, unsigned long delta_size,
  40                unsigned long *src_copied, unsigned long *literal_added)
  41{
  42        unsigned long copied_from_source, added_literal;
  43        const unsigned char *data, *top;
  44        unsigned char cmd;
  45        unsigned long src_size, dst_size, out;
  46
  47        /* the smallest delta size possible is 4 bytes */
  48        if (delta_size < 4)
  49                return -1;
  50
  51        data = delta_buf;
  52        top = delta_buf + delta_size;
  53
  54        src_size = get_hdr_size(&data);
  55        dst_size = get_hdr_size(&data);
  56
  57        added_literal = copied_from_source = out = 0;
  58        while (data < top) {
  59                cmd = *data++;
  60                if (cmd & 0x80) {
  61                        unsigned long cp_off = 0, cp_size = 0;
  62                        if (cmd & 0x01) cp_off = *data++;
  63                        if (cmd & 0x02) cp_off |= (*data++ << 8);
  64                        if (cmd & 0x04) cp_off |= (*data++ << 16);
  65                        if (cmd & 0x08) cp_off |= (*data++ << 24);
  66                        if (cmd & 0x10) cp_size = *data++;
  67                        if (cmd & 0x20) cp_size |= (*data++ << 8);
  68                        if (cp_size == 0) cp_size = 0x10000;
  69
  70                        if (cmd & 0x40)
  71                                /* copy from dst */
  72                                ;
  73                        else
  74                                copied_from_source += cp_size;
  75                        out += cp_size;
  76                } else {
  77                        /* write literal into dst */
  78                        added_literal += cmd;
  79                        out += cmd;
  80                        data += cmd;
  81                }
  82        }
  83
  84        /* sanity check */
  85        if (data != top || out != dst_size)
  86                return -1;
  87
  88        /* delete size is what was _not_ copied from source.
  89         * edit size is that and literal additions.
  90         */
  91        *src_copied = copied_from_source;
  92        *literal_added = added_literal;
  93        return 0;
  94}