3 * recreate a buffer from a source and the delta produced by diff-delta.c
5 * (C) 2005 Nicolas Pitre <nico@cam.org>
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.
16 void *patch_delta(void *src_buf, unsigned long src_size,
17 void *delta_buf, unsigned long delta_size,
18 unsigned long *dst_size)
20 const unsigned char *data, *top;
21 unsigned char *dst_buf, *out, cmd;
25 /* the smallest delta size possible is 4 bytes */
30 top = delta_buf + delta_size;
32 /* make sure the orig file size matches what we expect */
38 size |= (cmd & ~0x80) << i;
44 /* now the result size */
50 size |= (cmd & ~0x80) << i;
53 dst_buf = malloc(size);
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);
74 memcpy(out, data, cmd);
81 if (data != top || out - dst_buf != size) {