Nicolas Pitre | a310d43 | 2005-05-19 10:27:14 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | void *patch_delta(void *src_buf, unsigned long src_size, |
| 17 | void *delta_buf, unsigned long delta_size, |
| 18 | unsigned long *dst_size) |
| 19 | { |
| 20 | const unsigned char *data, *top; |
| 21 | unsigned char *dst_buf, *out, cmd; |
| 22 | unsigned long size; |
Nicolas Pitre | a310d43 | 2005-05-19 10:27:14 -0400 | [diff] [blame] | 23 | |
Nicolas Pitre | dcde55b | 2005-06-29 02:49:56 -0400 | [diff] [blame] | 24 | if (delta_size < DELTA_SIZE_MIN) |
Nicolas Pitre | a310d43 | 2005-05-19 10:27:14 -0400 | [diff] [blame] | 25 | return NULL; |
| 26 | |
| 27 | data = delta_buf; |
| 28 | top = delta_buf + delta_size; |
| 29 | |
| 30 | /* make sure the orig file size matches what we expect */ |
Nicolas Pitre | dcde55b | 2005-06-29 02:49:56 -0400 | [diff] [blame] | 31 | size = get_delta_hdr_size(&data); |
Nicolas Pitre | a310d43 | 2005-05-19 10:27:14 -0400 | [diff] [blame] | 32 | if (size != src_size) |
| 33 | return NULL; |
| 34 | |
| 35 | /* now the result size */ |
Nicolas Pitre | dcde55b | 2005-06-29 02:49:56 -0400 | [diff] [blame] | 36 | size = get_delta_hdr_size(&data); |
Sergey Vlasov | ce726ec | 2005-09-04 13:43:16 +0400 | [diff] [blame] | 37 | dst_buf = malloc(size + 1); |
Nicolas Pitre | a310d43 | 2005-05-19 10:27:14 -0400 | [diff] [blame] | 38 | if (!dst_buf) |
| 39 | return NULL; |
Sergey Vlasov | ce726ec | 2005-09-04 13:43:16 +0400 | [diff] [blame] | 40 | dst_buf[size] = 0; |
Nicolas Pitre | a310d43 | 2005-05-19 10:27:14 -0400 | [diff] [blame] | 41 | |
| 42 | out = dst_buf; |
| 43 | while (data < top) { |
| 44 | cmd = *data++; |
| 45 | if (cmd & 0x80) { |
| 46 | unsigned long cp_off = 0, cp_size = 0; |
| 47 | const unsigned char *buf; |
| 48 | if (cmd & 0x01) cp_off = *data++; |
| 49 | if (cmd & 0x02) cp_off |= (*data++ << 8); |
| 50 | if (cmd & 0x04) cp_off |= (*data++ << 16); |
| 51 | if (cmd & 0x08) cp_off |= (*data++ << 24); |
| 52 | if (cmd & 0x10) cp_size = *data++; |
| 53 | if (cmd & 0x20) cp_size |= (*data++ << 8); |
| 54 | if (cp_size == 0) cp_size = 0x10000; |
| 55 | buf = (cmd & 0x40) ? dst_buf : src_buf; |
| 56 | memcpy(out, buf + cp_off, cp_size); |
| 57 | out += cp_size; |
| 58 | } else { |
| 59 | memcpy(out, data, cmd); |
| 60 | out += cmd; |
| 61 | data += cmd; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /* sanity check */ |
| 66 | if (data != top || out - dst_buf != size) { |
| 67 | free(dst_buf); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | *dst_size = size; |
| 72 | return dst_buf; |
| 73 | } |