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>.
10 #include "count-delta.h"
13 * NOTE. We do not _interpret_ delta fully. As an approximation, we
14 * just count the number of bytes that are copied from the source, and
15 * the number of literal data bytes that are inserted.
17 * Number of bytes that are _not_ copied from the source is deletion,
18 * and number of inserted literal bytes are addition, so sum of them
19 * is the extent of damage.
21 int count_delta(void *delta_buf
, unsigned long delta_size
,
22 unsigned long *src_copied
, unsigned long *literal_added
)
24 unsigned long copied_from_source
, added_literal
;
25 const unsigned char *data
, *top
;
27 unsigned long src_size
, dst_size
, out
;
29 if (delta_size
< DELTA_SIZE_MIN
)
33 top
= delta_buf
+ delta_size
;
35 src_size
= get_delta_hdr_size(&data
);
36 dst_size
= get_delta_hdr_size(&data
);
38 added_literal
= copied_from_source
= out
= 0;
42 unsigned long cp_off
= 0, cp_size
= 0;
43 if (cmd
& 0x01) cp_off
= *data
++;
44 if (cmd
& 0x02) cp_off
|= (*data
++ << 8);
45 if (cmd
& 0x04) cp_off
|= (*data
++ << 16);
46 if (cmd
& 0x08) cp_off
|= (*data
++ << 24);
47 if (cmd
& 0x10) cp_size
= *data
++;
48 if (cmd
& 0x20) cp_size
|= (*data
++ << 8);
49 if (cmd
& 0x40) cp_size
|= (*data
++ << 16);
50 if (cp_size
== 0) cp_size
= 0x10000;
52 copied_from_source
+= cp_size
;
55 /* write literal into dst */
63 if (data
!= top
|| out
!= dst_size
)
66 /* delete size is what was _not_ copied from source.
67 * edit size is that and literal additions.
69 *src_copied
= copied_from_source
;
70 *literal_added
= added_literal
;