blob: e3a1d425ee2e333bce0f87b1e50fc112a8ec51a3 [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
12#include <stdlib.h>
13#include <string.h>
14#include "delta.h"
15
Nicolas Pitre08abe662006-04-24 23:07:47 -040016void *patch_delta(const void *src_buf, unsigned long src_size,
17 const void *delta_buf, unsigned long delta_size,
Nicolas Pitrea310d432005-05-19 10:27:14 -040018 unsigned long *dst_size)
19{
20 const unsigned char *data, *top;
21 unsigned char *dst_buf, *out, cmd;
22 unsigned long size;
Nicolas Pitrea310d432005-05-19 10:27:14 -040023
Nicolas Pitredcde55b2005-06-29 02:49:56 -040024 if (delta_size < DELTA_SIZE_MIN)
Nicolas Pitrea310d432005-05-19 10:27:14 -040025 return NULL;
26
27 data = delta_buf;
Florian Forster1d7f1712006-06-18 17:18:09 +020028 top = (const unsigned char *) delta_buf + delta_size;
Nicolas Pitrea310d432005-05-19 10:27:14 -040029
30 /* make sure the orig file size matches what we expect */
Nicolas Pitre89608442006-04-07 15:26:10 -040031 size = get_delta_hdr_size(&data, top);
Nicolas Pitrea310d432005-05-19 10:27:14 -040032 if (size != src_size)
33 return NULL;
34
35 /* now the result size */
Nicolas Pitre89608442006-04-07 15:26:10 -040036 size = get_delta_hdr_size(&data, top);
Sergey Vlasovce726ec2005-09-04 13:43:16 +040037 dst_buf = malloc(size + 1);
Nicolas Pitrea310d432005-05-19 10:27:14 -040038 if (!dst_buf)
39 return NULL;
Sergey Vlasovce726ec2005-09-04 13:43:16 +040040 dst_buf[size] = 0;
Nicolas Pitrea310d432005-05-19 10:27:14 -040041
42 out = dst_buf;
43 while (data < top) {
44 cmd = *data++;
45 if (cmd & 0x80) {
46 unsigned long cp_off = 0, cp_size = 0;
Nicolas Pitrea310d432005-05-19 10:27:14 -040047 if (cmd & 0x01) cp_off = *data++;
48 if (cmd & 0x02) cp_off |= (*data++ << 8);
49 if (cmd & 0x04) cp_off |= (*data++ << 16);
50 if (cmd & 0x08) cp_off |= (*data++ << 24);
51 if (cmd & 0x10) cp_size = *data++;
52 if (cmd & 0x20) cp_size |= (*data++ << 8);
Nicolas Pitred60fc1c2006-02-09 17:50:04 -050053 if (cmd & 0x40) cp_size |= (*data++ << 16);
Nicolas Pitrea310d432005-05-19 10:27:14 -040054 if (cp_size == 0) cp_size = 0x10000;
Nicolas Pitre89608442006-04-07 15:26:10 -040055 if (cp_off + cp_size < cp_size ||
56 cp_off + cp_size > src_size ||
57 cp_size > size)
58 goto bad;
Florian Forster1d7f1712006-06-18 17:18:09 +020059 memcpy(out, (char *) src_buf + cp_off, cp_size);
Nicolas Pitrea310d432005-05-19 10:27:14 -040060 out += cp_size;
Nicolas Pitre89608442006-04-07 15:26:10 -040061 size -= cp_size;
62 } else if (cmd) {
63 if (cmd > size)
64 goto bad;
Nicolas Pitrea310d432005-05-19 10:27:14 -040065 memcpy(out, data, cmd);
66 out += cmd;
67 data += cmd;
Nicolas Pitre89608442006-04-07 15:26:10 -040068 size -= cmd;
69 } else {
70 /*
71 * cmd == 0 is reserved for future encoding
72 * extensions. In the mean time we must fail when
73 * encountering them (might be data corruption).
74 */
75 goto bad;
Nicolas Pitrea310d432005-05-19 10:27:14 -040076 }
77 }
78
79 /* sanity check */
Nicolas Pitre89608442006-04-07 15:26:10 -040080 if (data != top || size != 0) {
81 bad:
Nicolas Pitrea310d432005-05-19 10:27:14 -040082 free(dst_buf);
83 return NULL;
84 }
85
Nicolas Pitre89608442006-04-07 15:26:10 -040086 *dst_size = out - dst_buf;
Nicolas Pitrea310d432005-05-19 10:27:14 -040087 return dst_buf;
88}