blob: ed9db81fa82c812c9ffa07f5a40540dbb15da0d3 [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 *
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
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);
Nicolas Pitre57b73152006-12-18 16:06:50 -050036 dst_buf = xmalloc(size + 1);
Sergey Vlasovce726ec2005-09-04 13:43:16 +040037 dst_buf[size] = 0;
Nicolas Pitrea310d432005-05-19 10:27:14 -040038
39 out = dst_buf;
40 while (data < top) {
41 cmd = *data++;
42 if (cmd & 0x80) {
43 unsigned long cp_off = 0, cp_size = 0;
Nicolas Pitrea310d432005-05-19 10:27:14 -040044 if (cmd & 0x01) cp_off = *data++;
45 if (cmd & 0x02) cp_off |= (*data++ << 8);
46 if (cmd & 0x04) cp_off |= (*data++ << 16);
47 if (cmd & 0x08) cp_off |= (*data++ << 24);
48 if (cmd & 0x10) cp_size = *data++;
49 if (cmd & 0x20) cp_size |= (*data++ << 8);
Nicolas Pitred60fc1c2006-02-09 17:50:04 -050050 if (cmd & 0x40) cp_size |= (*data++ << 16);
Nicolas Pitrea310d432005-05-19 10:27:14 -040051 if (cp_size == 0) cp_size = 0x10000;
Nicolas Pitre89608442006-04-07 15:26:10 -040052 if (cp_off + cp_size < cp_size ||
53 cp_off + cp_size > src_size ||
54 cp_size > size)
Nicolas Pitre57b73152006-12-18 16:06:50 -050055 break;
Florian Forster1d7f1712006-06-18 17:18:09 +020056 memcpy(out, (char *) src_buf + cp_off, cp_size);
Nicolas Pitrea310d432005-05-19 10:27:14 -040057 out += cp_size;
Nicolas Pitre89608442006-04-07 15:26:10 -040058 size -= cp_size;
59 } else if (cmd) {
60 if (cmd > size)
Nicolas Pitre57b73152006-12-18 16:06:50 -050061 break;
Nicolas Pitrea310d432005-05-19 10:27:14 -040062 memcpy(out, data, cmd);
63 out += cmd;
64 data += cmd;
Nicolas Pitre89608442006-04-07 15:26:10 -040065 size -= cmd;
66 } else {
67 /*
68 * cmd == 0 is reserved for future encoding
69 * extensions. In the mean time we must fail when
70 * encountering them (might be data corruption).
71 */
Nicolas Pitre57b73152006-12-18 16:06:50 -050072 error("unexpected delta opcode 0");
Nicolas Pitre89608442006-04-07 15:26:10 -040073 goto bad;
Nicolas Pitrea310d432005-05-19 10:27:14 -040074 }
75 }
76
77 /* sanity check */
Nicolas Pitre89608442006-04-07 15:26:10 -040078 if (data != top || size != 0) {
Nicolas Pitre57b73152006-12-18 16:06:50 -050079 error("delta replay has gone wild");
Nicolas Pitre89608442006-04-07 15:26:10 -040080 bad:
Nicolas Pitrea310d432005-05-19 10:27:14 -040081 free(dst_buf);
82 return NULL;
83 }
84
Nicolas Pitre89608442006-04-07 15:26:10 -040085 *dst_size = out - dst_buf;
Nicolas Pitrea310d432005-05-19 10:27:14 -040086 return dst_buf;
87}