🌐 AI搜索 & 代理 主页
blob: 058a2aadb1475801ea7573837867fa79bf1766c1 [file] [log] [blame]
Junio C Hamano85976972005-05-24 19:09:321/*
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>
Nicolas Pitredcde55b2005-06-29 06:49:569#include "delta.h"
Junio C Hamano85976972005-05-24 19:09:3210#include "count-delta.h"
11
Junio C Hamano85976972005-05-24 19:09:3212/*
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
Junio C Hamano355e76a2005-06-03 08:36:0315 * the number of literal data bytes that are inserted.
16 *
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
Nicolas Pitree7ad4a92006-02-10 15:20:4019 * is the extent of damage.
Junio C Hamano85976972005-05-24 19:09:3220 */
Junio C Hamano355e76a2005-06-03 08:36:0321int count_delta(void *delta_buf, unsigned long delta_size,
22 unsigned long *src_copied, unsigned long *literal_added)
Junio C Hamano85976972005-05-24 19:09:3223{
24 unsigned long copied_from_source, added_literal;
25 const unsigned char *data, *top;
26 unsigned char cmd;
27 unsigned long src_size, dst_size, out;
28
Nicolas Pitredcde55b2005-06-29 06:49:5629 if (delta_size < DELTA_SIZE_MIN)
Junio C Hamano355e76a2005-06-03 08:36:0330 return -1;
Junio C Hamano85976972005-05-24 19:09:3231
32 data = delta_buf;
33 top = delta_buf + delta_size;
34
Nicolas Pitredcde55b2005-06-29 06:49:5635 src_size = get_delta_hdr_size(&data);
36 dst_size = get_delta_hdr_size(&data);
Junio C Hamano85976972005-05-24 19:09:3237
38 added_literal = copied_from_source = out = 0;
39 while (data < top) {
40 cmd = *data++;
41 if (cmd & 0x80) {
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);
Junio C Hamano91c76742006-02-10 01:15:5949 if (cmd & 0x40) cp_size |= (*data++ << 16);
Junio C Hamano85976972005-05-24 19:09:3250 if (cp_size == 0) cp_size = 0x10000;
51
Junio C Hamano91c76742006-02-10 01:15:5952 copied_from_source += cp_size;
Junio C Hamano85976972005-05-24 19:09:3253 out += cp_size;
54 } else {
55 /* write literal into dst */
56 added_literal += cmd;
57 out += cmd;
58 data += cmd;
59 }
60 }
61
62 /* sanity check */
63 if (data != top || out != dst_size)
Junio C Hamano355e76a2005-06-03 08:36:0364 return -1;
Junio C Hamano85976972005-05-24 19:09:3265
66 /* delete size is what was _not_ copied from source.
67 * edit size is that and literal additions.
68 */
Junio C Hamano355e76a2005-06-03 08:36:0369 *src_copied = copied_from_source;
70 *literal_added = added_literal;
71 return 0;
Junio C Hamano85976972005-05-24 19:09:3272}