[PATCH] assorted delta code cleanup · wolfpython/git@dcde55b · GitHub
Skip to content

Commit dcde55b

Browse files
Nicolas PitreLinus Torvalds
authored andcommitted
[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>
1 parent e5e3e0f commit dcde55b

6 files changed

Lines changed: 35 additions & 58 deletions

File tree

count-delta.c

Lines changed: 4 additions & 19 deletions

delta.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,26 @@ extern void *patch_delta(void *src_buf, unsigned long src_size,
99
void *delta_buf, unsigned long delta_size,
1010
unsigned long *dst_size);
1111

12+
/* the smallest possible delta size is 4 bytes */
13+
#define DELTA_SIZE_MIN 4
14+
15+
/*
16+
* This must be called twice on the delta data buffer, first to get the
17+
* expected reference buffer size, and again to get the result buffer size.
18+
*/
19+
static inline unsigned long get_delta_hdr_size(const unsigned char **datap)
20+
{
21+
const unsigned char *data = *datap;
22+
unsigned char cmd = *data++;
23+
unsigned long size = cmd & ~0x80;
24+
int i = 7;
25+
while (cmd & 0x80) {
26+
cmd = *data++;
27+
size |= (cmd & ~0x80) << i;
28+
i += 7;
29+
}
30+
*datap = data;
31+
return size;
32+
}
33+
1234
#endif

diff-delta.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,13 @@ void *diff_delta(void *from_buf, unsigned long from_size,
306306
*orig = i;
307307
}
308308

309-
/* next time around the largest possible output is 1 + 4 + 3 */
310309
if (max_size && outpos > max_size) {
311310
free(out);
312311
delta_cleanup(&bdf);
313312
return NULL;
314313
}
314+
315+
/* next time around the largest possible output is 1 + 4 + 3 */
315316
if (outpos > outsize - 8) {
316317
void *tmp = out;
317318
outsize = outsize * 3 / 2;

pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void *delta_against(void *buf, unsigned long size, struct object_entry *e
3434
if (!otherbuf)
3535
die("unable to read %s", sha1_to_hex(entry->delta->sha1));
3636
delta_buf = diff_delta(otherbuf, othersize,
37-
buf, size, &delta_size, ~0UL);
37+
buf, size, &delta_size, 0);
3838
if (!delta_buf || delta_size != entry->delta_size)
3939
die("delta size changed");
4040
free(buf);

patch-delta.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,20 @@ void *patch_delta(void *src_buf, unsigned long src_size,
2020
const unsigned char *data, *top;
2121
unsigned char *dst_buf, *out, cmd;
2222
unsigned long size;
23-
int i;
2423

25-
/* the smallest delta size possible is 4 bytes */
26-
if (delta_size < 4)
24+
if (delta_size < DELTA_SIZE_MIN)
2725
return NULL;
2826

2927
data = delta_buf;
3028
top = delta_buf + delta_size;
3129

3230
/* make sure the orig file size matches what we expect */
33-
cmd = *data++;
34-
size = cmd & ~0x80;
35-
i = 7;
36-
while (cmd & 0x80) {
37-
cmd = *data++;
38-
size |= (cmd & ~0x80) << i;
39-
i += 7;
40-
}
31+
size = get_delta_hdr_size(&data);
4132
if (size != src_size)
4233
return NULL;
4334

4435
/* now the result size */
45-
cmd = *data++;
46-
size = cmd & ~0x80;
47-
i = 7;
48-
while (cmd & 0x80) {
49-
cmd = *data++;
50-
size |= (cmd & ~0x80) << i;
51-
i += 7;
52-
}
36+
size = get_delta_hdr_size(&data);
5337
dst_buf = malloc(size);
5438
if (!dst_buf)
5539
return NULL;

sha1_file.c

Lines changed: 3 additions & 18 deletions

0 commit comments

Comments
 (0)