blob: b5c8594db60dd19fbe18b95ea79439c7bad3fa40 [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;
Jeff King9514b0b2018-08-30 03:12:52 -040043#define PARSE_CP_PARAM(bit, var, shift) do { \
44 if (cmd & (bit)) { \
45 if (data >= top) \
46 goto bad_length; \
47 var |= ((unsigned) *data++ << (shift)); \
48 } } while (0)
49 PARSE_CP_PARAM(0x01, cp_off, 0);
50 PARSE_CP_PARAM(0x02, cp_off, 8);
51 PARSE_CP_PARAM(0x04, cp_off, 16);
52 PARSE_CP_PARAM(0x08, cp_off, 24);
53 PARSE_CP_PARAM(0x10, cp_size, 0);
54 PARSE_CP_PARAM(0x20, cp_size, 8);
55 PARSE_CP_PARAM(0x40, cp_size, 16);
56#undef PARSE_CP_PARAM
Nicolas Pitrea310d432005-05-19 10:27:14 -040057 if (cp_size == 0) cp_size = 0x10000;
Jonathan Nieder1368f652010-10-10 21:59:26 -050058 if (unsigned_add_overflows(cp_off, cp_size) ||
Nicolas Pitre89608442006-04-07 15:26:10 -040059 cp_off + cp_size > src_size ||
60 cp_size > size)
Jann Hornfa72f902018-08-30 03:10:26 -040061 goto bad_length;
Florian Forster1d7f1712006-06-18 17:18:09 +020062 memcpy(out, (char *) src_buf + cp_off, cp_size);
Nicolas Pitrea310d432005-05-19 10:27:14 -040063 out += cp_size;
Nicolas Pitre89608442006-04-07 15:26:10 -040064 size -= cp_size;
65 } else if (cmd) {
Jann Horn21870ef2018-08-30 03:09:45 -040066 if (cmd > size || cmd > top - data)
Jann Hornfa72f902018-08-30 03:10:26 -040067 goto bad_length;
Nicolas Pitrea310d432005-05-19 10:27:14 -040068 memcpy(out, data, cmd);
69 out += cmd;
70 data += cmd;
Nicolas Pitre89608442006-04-07 15:26:10 -040071 size -= cmd;
72 } else {
73 /*
74 * cmd == 0 is reserved for future encoding
75 * extensions. In the mean time we must fail when
76 * encountering them (might be data corruption).
77 */
Nicolas Pitre57b73152006-12-18 16:06:50 -050078 error("unexpected delta opcode 0");
Nicolas Pitre89608442006-04-07 15:26:10 -040079 goto bad;
Nicolas Pitrea310d432005-05-19 10:27:14 -040080 }
81 }
82
83 /* sanity check */
Nicolas Pitre89608442006-04-07 15:26:10 -040084 if (data != top || size != 0) {
Jann Hornfa72f902018-08-30 03:10:26 -040085 bad_length:
Nicolas Pitre57b73152006-12-18 16:06:50 -050086 error("delta replay has gone wild");
Nicolas Pitre89608442006-04-07 15:26:10 -040087 bad:
Nicolas Pitrea310d432005-05-19 10:27:14 -040088 free(dst_buf);
89 return NULL;
90 }
91
Nicolas Pitre89608442006-04-07 15:26:10 -040092 *dst_size = out - dst_buf;
Nicolas Pitrea310d432005-05-19 10:27:14 -040093 return dst_buf;
94}