blob: 56e0a5ede22c9396fc897bf1d3444dce92d8916f [file] [log] [blame]
Nicolas Pitrea310d432005-05-19 10:27:14 -04001/*
2 * patch-delta.c:
3 * recreate a buffer from a source and the delta produced by diff-delta.c
4 *
Nicolas Pitre03aa8ff2009-09-14 02:41:16 -04005 * (C) 2005 Nicolas Pitre <nico@fluxnic.net>
Nicolas Pitrea310d432005-05-19 10:27:14 -04006 *
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
Nicolas Pitre57b73152006-12-18 16:06:50 -050012#include "git-compat-util.h"
Nicolas Pitrea310d432005-05-19 10:27:14 -040013#include "delta.h"
14
Nicolas Pitre08abe662006-04-24 23:07:47 -040015void *patch_delta(const void *src_buf, unsigned long src_size,
16 const void *delta_buf, unsigned long delta_size,
Nicolas Pitrea310d432005-05-19 10:27:14 -040017 unsigned long *dst_size)
18{
19 const unsigned char *data, *top;
20 unsigned char *dst_buf, *out, cmd;
21 unsigned long size;
Nicolas Pitrea310d432005-05-19 10:27:14 -040022
Nicolas Pitredcde55b2005-06-29 02:49:56 -040023 if (delta_size < DELTA_SIZE_MIN)
Nicolas Pitrea310d432005-05-19 10:27:14 -040024 return NULL;
25
26 data = delta_buf;
Florian Forster1d7f1712006-06-18 17:18:09 +020027 top = (const unsigned char *) delta_buf + delta_size;
Nicolas Pitrea310d432005-05-19 10:27:14 -040028
29 /* make sure the orig file size matches what we expect */
Nicolas Pitre89608442006-04-07 15:26:10 -040030 size = get_delta_hdr_size(&data, top);
Nicolas Pitrea310d432005-05-19 10:27:14 -040031 if (size != src_size)
32 return NULL;
33
34 /* now the result size */
Nicolas Pitre89608442006-04-07 15:26:10 -040035 size = get_delta_hdr_size(&data, top);
Ilari Liusvaara222083a2010-01-26 20:24:13 +020036 dst_buf = xmallocz(size);
Nicolas Pitrea310d432005-05-19 10:27:14 -040037
38 out = dst_buf;
39 while (data < top) {
40 cmd = *data++;
41 if (cmd & 0x80) {
42 unsigned long cp_off = 0, cp_size = 0;
Nicolas Pitrea310d432005-05-19 10:27:14 -040043 if (cmd & 0x01) cp_off = *data++;
44 if (cmd & 0x02) cp_off |= (*data++ << 8);
45 if (cmd & 0x04) cp_off |= (*data++ << 16);
Linus Torvalds48fb7de2009-06-17 17:22:27 -070046 if (cmd & 0x08) cp_off |= ((unsigned) *data++ << 24);
Nicolas Pitrea310d432005-05-19 10:27:14 -040047 if (cmd & 0x10) cp_size = *data++;
48 if (cmd & 0x20) cp_size |= (*data++ << 8);
Nicolas Pitred60fc1c2006-02-09 17:50:04 -050049 if (cmd & 0x40) cp_size |= (*data++ << 16);
Nicolas Pitrea310d432005-05-19 10:27:14 -040050 if (cp_size == 0) cp_size = 0x10000;
Jonathan Nieder1368f652010-10-10 21:59:26 -050051 if (unsigned_add_overflows(cp_off, cp_size) ||
Nicolas Pitre89608442006-04-07 15:26:10 -040052 cp_off + cp_size > src_size ||
53 cp_size > size)
Nicolas Pitre57b73152006-12-18 16:06:50 -050054 break;
Florian Forster1d7f1712006-06-18 17:18:09 +020055 memcpy(out, (char *) src_buf + cp_off, cp_size);
Nicolas Pitrea310d432005-05-19 10:27:14 -040056 out += cp_size;
Nicolas Pitre89608442006-04-07 15:26:10 -040057 size -= cp_size;
58 } else if (cmd) {
59 if (cmd > size)
Nicolas Pitre57b73152006-12-18 16:06:50 -050060 break;
Nicolas Pitrea310d432005-05-19 10:27:14 -040061 memcpy(out, data, cmd);
62 out += cmd;
63 data += cmd;
Nicolas Pitre89608442006-04-07 15:26:10 -040064 size -= cmd;
65 } else {
66 /*
67 * cmd == 0 is reserved for future encoding
68 * extensions. In the mean time we must fail when
69 * encountering them (might be data corruption).
70 */
Nicolas Pitre57b73152006-12-18 16:06:50 -050071 error("unexpected delta opcode 0");
Nicolas Pitre89608442006-04-07 15:26:10 -040072 goto bad;
Nicolas Pitrea310d432005-05-19 10:27:14 -040073 }
74 }
75
76 /* sanity check */
Nicolas Pitre89608442006-04-07 15:26:10 -040077 if (data != top || size != 0) {
Nicolas Pitre57b73152006-12-18 16:06:50 -050078 error("delta replay has gone wild");
Nicolas Pitre89608442006-04-07 15:26:10 -040079 bad:
Nicolas Pitrea310d432005-05-19 10:27:14 -040080 free(dst_buf);
81 return NULL;
82 }
83
Nicolas Pitre89608442006-04-07 15:26:10 -040084 *dst_size = out - dst_buf;
Nicolas Pitrea310d432005-05-19 10:27:14 -040085 return dst_buf;
86}