]> git.ipfire.org Git - thirdparty/git.git/blame - patch-delta.c
Fix packed_delta_info() that was broken by the delta header packing change
[thirdparty/git.git] / patch-delta.c
CommitLineData
a310d434
NP
1/*
2 * patch-delta.c:
3 * recreate a buffer from a source and the delta produced by diff-delta.c
4 *
5 * (C) 2005 Nicolas Pitre <nico@cam.org>
6 *
7 * This code is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <stdlib.h>
13#include <string.h>
14#include "delta.h"
15
16void *patch_delta(void *src_buf, unsigned long src_size,
17 void *delta_buf, unsigned long delta_size,
18 unsigned long *dst_size)
19{
20 const unsigned char *data, *top;
21 unsigned char *dst_buf, *out, cmd;
22 unsigned long size;
23 int i;
24
69a2d426
NP
25 /* the smallest delta size possible is 4 bytes */
26 if (delta_size < 4)
a310d434
NP
27 return NULL;
28
29 data = delta_buf;
30 top = delta_buf + delta_size;
31
32 /* make sure the orig file size matches what we expect */
a310d434 33 cmd = *data++;
69a2d426
NP
34 size = cmd & ~0x80;
35 i = 7;
36 while (cmd & 0x80) {
37 cmd = *data++;
38 size |= (cmd & ~0x80) << i;
39 i += 7;
a310d434
NP
40 }
41 if (size != src_size)
42 return NULL;
43
44 /* now the result size */
a310d434 45 cmd = *data++;
69a2d426
NP
46 size = cmd & ~0x80;
47 i = 7;
48 while (cmd & 0x80) {
49 cmd = *data++;
50 size |= (cmd & ~0x80) << i;
51 i += 7;
a310d434
NP
52 }
53 dst_buf = malloc(size);
54 if (!dst_buf)
55 return NULL;
56
57 out = dst_buf;
58 while (data < top) {
59 cmd = *data++;
60 if (cmd & 0x80) {
61 unsigned long cp_off = 0, cp_size = 0;
62 const unsigned char *buf;
63 if (cmd & 0x01) cp_off = *data++;
64 if (cmd & 0x02) cp_off |= (*data++ << 8);
65 if (cmd & 0x04) cp_off |= (*data++ << 16);
66 if (cmd & 0x08) cp_off |= (*data++ << 24);
67 if (cmd & 0x10) cp_size = *data++;
68 if (cmd & 0x20) cp_size |= (*data++ << 8);
69 if (cp_size == 0) cp_size = 0x10000;
70 buf = (cmd & 0x40) ? dst_buf : src_buf;
71 memcpy(out, buf + cp_off, cp_size);
72 out += cp_size;
73 } else {
74 memcpy(out, data, cmd);
75 out += cmd;
76 data += cmd;
77 }
78 }
79
80 /* sanity check */
81 if (data != top || out - dst_buf != size) {
82 free(dst_buf);
83 return NULL;
84 }
85
86 *dst_size = size;
87 return dst_buf;
88}