[PATCH] assorted delta code cleanup
This is a wrap-up patch including all the cleanups I've done to the delta code and its usage. The most important change is the factorization of the delta header handling code. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
This commit is contained in:
parent
e5e3e0f500
commit
dcde55bc58
@ -6,23 +6,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include "delta.h"
|
||||||
#include "count-delta.h"
|
#include "count-delta.h"
|
||||||
|
|
||||||
static unsigned long get_hdr_size(const unsigned char **datap)
|
|
||||||
{
|
|
||||||
const unsigned char *data = *datap;
|
|
||||||
unsigned char cmd = *data++;
|
|
||||||
unsigned long size = cmd & ~0x80;
|
|
||||||
int i = 7;
|
|
||||||
while (cmd & 0x80) {
|
|
||||||
cmd = *data++;
|
|
||||||
size |= (cmd & ~0x80) << i;
|
|
||||||
i += 7;
|
|
||||||
}
|
|
||||||
*datap = data;
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* NOTE. We do not _interpret_ delta fully. As an approximation, we
|
* NOTE. We do not _interpret_ delta fully. As an approximation, we
|
||||||
* just count the number of bytes that are copied from the source, and
|
* just count the number of bytes that are copied from the source, and
|
||||||
@ -44,15 +30,14 @@ int count_delta(void *delta_buf, unsigned long delta_size,
|
|||||||
unsigned char cmd;
|
unsigned char cmd;
|
||||||
unsigned long src_size, dst_size, out;
|
unsigned long src_size, dst_size, out;
|
||||||
|
|
||||||
/* the smallest delta size possible is 4 bytes */
|
if (delta_size < DELTA_SIZE_MIN)
|
||||||
if (delta_size < 4)
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
data = delta_buf;
|
data = delta_buf;
|
||||||
top = delta_buf + delta_size;
|
top = delta_buf + delta_size;
|
||||||
|
|
||||||
src_size = get_hdr_size(&data);
|
src_size = get_delta_hdr_size(&data);
|
||||||
dst_size = get_hdr_size(&data);
|
dst_size = get_delta_hdr_size(&data);
|
||||||
|
|
||||||
added_literal = copied_from_source = out = 0;
|
added_literal = copied_from_source = out = 0;
|
||||||
while (data < top) {
|
while (data < top) {
|
||||||
|
22
delta.h
22
delta.h
@ -9,4 +9,26 @@ extern void *patch_delta(void *src_buf, unsigned long src_size,
|
|||||||
void *delta_buf, unsigned long delta_size,
|
void *delta_buf, unsigned long delta_size,
|
||||||
unsigned long *dst_size);
|
unsigned long *dst_size);
|
||||||
|
|
||||||
|
/* the smallest possible delta size is 4 bytes */
|
||||||
|
#define DELTA_SIZE_MIN 4
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This must be called twice on the delta data buffer, first to get the
|
||||||
|
* expected reference buffer size, and again to get the result buffer size.
|
||||||
|
*/
|
||||||
|
static inline unsigned long get_delta_hdr_size(const unsigned char **datap)
|
||||||
|
{
|
||||||
|
const unsigned char *data = *datap;
|
||||||
|
unsigned char cmd = *data++;
|
||||||
|
unsigned long size = cmd & ~0x80;
|
||||||
|
int i = 7;
|
||||||
|
while (cmd & 0x80) {
|
||||||
|
cmd = *data++;
|
||||||
|
size |= (cmd & ~0x80) << i;
|
||||||
|
i += 7;
|
||||||
|
}
|
||||||
|
*datap = data;
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -306,12 +306,13 @@ void *diff_delta(void *from_buf, unsigned long from_size,
|
|||||||
*orig = i;
|
*orig = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* next time around the largest possible output is 1 + 4 + 3 */
|
|
||||||
if (max_size && outpos > max_size) {
|
if (max_size && outpos > max_size) {
|
||||||
free(out);
|
free(out);
|
||||||
delta_cleanup(&bdf);
|
delta_cleanup(&bdf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* next time around the largest possible output is 1 + 4 + 3 */
|
||||||
if (outpos > outsize - 8) {
|
if (outpos > outsize - 8) {
|
||||||
void *tmp = out;
|
void *tmp = out;
|
||||||
outsize = outsize * 3 / 2;
|
outsize = outsize * 3 / 2;
|
||||||
|
@ -34,7 +34,7 @@ static void *delta_against(void *buf, unsigned long size, struct object_entry *e
|
|||||||
if (!otherbuf)
|
if (!otherbuf)
|
||||||
die("unable to read %s", sha1_to_hex(entry->delta->sha1));
|
die("unable to read %s", sha1_to_hex(entry->delta->sha1));
|
||||||
delta_buf = diff_delta(otherbuf, othersize,
|
delta_buf = diff_delta(otherbuf, othersize,
|
||||||
buf, size, &delta_size, ~0UL);
|
buf, size, &delta_size, 0);
|
||||||
if (!delta_buf || delta_size != entry->delta_size)
|
if (!delta_buf || delta_size != entry->delta_size)
|
||||||
die("delta size changed");
|
die("delta size changed");
|
||||||
free(buf);
|
free(buf);
|
||||||
|
@ -20,36 +20,20 @@ void *patch_delta(void *src_buf, unsigned long src_size,
|
|||||||
const unsigned char *data, *top;
|
const unsigned char *data, *top;
|
||||||
unsigned char *dst_buf, *out, cmd;
|
unsigned char *dst_buf, *out, cmd;
|
||||||
unsigned long size;
|
unsigned long size;
|
||||||
int i;
|
|
||||||
|
|
||||||
/* the smallest delta size possible is 4 bytes */
|
if (delta_size < DELTA_SIZE_MIN)
|
||||||
if (delta_size < 4)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
data = delta_buf;
|
data = delta_buf;
|
||||||
top = delta_buf + delta_size;
|
top = delta_buf + delta_size;
|
||||||
|
|
||||||
/* make sure the orig file size matches what we expect */
|
/* make sure the orig file size matches what we expect */
|
||||||
cmd = *data++;
|
size = get_delta_hdr_size(&data);
|
||||||
size = cmd & ~0x80;
|
|
||||||
i = 7;
|
|
||||||
while (cmd & 0x80) {
|
|
||||||
cmd = *data++;
|
|
||||||
size |= (cmd & ~0x80) << i;
|
|
||||||
i += 7;
|
|
||||||
}
|
|
||||||
if (size != src_size)
|
if (size != src_size)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* now the result size */
|
/* now the result size */
|
||||||
cmd = *data++;
|
size = get_delta_hdr_size(&data);
|
||||||
size = cmd & ~0x80;
|
|
||||||
i = 7;
|
|
||||||
while (cmd & 0x80) {
|
|
||||||
cmd = *data++;
|
|
||||||
size |= (cmd & ~0x80) << i;
|
|
||||||
i += 7;
|
|
||||||
}
|
|
||||||
dst_buf = malloc(size);
|
dst_buf = malloc(size);
|
||||||
if (!dst_buf)
|
if (!dst_buf)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
21
sha1_file.c
21
sha1_file.c
@ -592,22 +592,6 @@ void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned l
|
|||||||
return unpack_sha1_rest(&stream, hdr, *size);
|
return unpack_sha1_rest(&stream, hdr, *size);
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned long parse_delta_size(unsigned char **p)
|
|
||||||
{
|
|
||||||
unsigned char c;
|
|
||||||
unsigned long size = 0;
|
|
||||||
unsigned shift = 0;
|
|
||||||
unsigned char *data = *p;
|
|
||||||
|
|
||||||
do {
|
|
||||||
c = *data++;
|
|
||||||
size += (c & 0x7f) << shift;
|
|
||||||
shift += 7;
|
|
||||||
} while (c & 0x80);
|
|
||||||
*p = data;
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int packed_delta_info(unsigned char *base_sha1,
|
static int packed_delta_info(unsigned char *base_sha1,
|
||||||
unsigned long delta_size,
|
unsigned long delta_size,
|
||||||
unsigned long left,
|
unsigned long left,
|
||||||
@ -645,11 +629,12 @@ static int packed_delta_info(unsigned char *base_sha1,
|
|||||||
* the result size. Verify the base size while we are at it.
|
* the result size. Verify the base size while we are at it.
|
||||||
*/
|
*/
|
||||||
data = delta_head;
|
data = delta_head;
|
||||||
verify_base_size = parse_delta_size(&data);
|
verify_base_size = get_delta_hdr_size(&data);
|
||||||
result_size = parse_delta_size(&data);
|
|
||||||
if (verify_base_size != base_size)
|
if (verify_base_size != base_size)
|
||||||
die("delta base size mismatch");
|
die("delta base size mismatch");
|
||||||
|
|
||||||
|
/* Read the result size */
|
||||||
|
result_size = get_delta_hdr_size(&data);
|
||||||
*sizep = result_size;
|
*sizep = result_size;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user