diff-delta: big code simplification
This is much smaller and hopefully clearer code now. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>maint
parent
fe474b588b
commit
8e1454b5ad
217
diff-delta.c
217
diff-delta.c
|
@ -19,8 +19,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <zlib.h>
|
||||||
#include "delta.h"
|
#include "delta.h"
|
||||||
#include "zlib.h"
|
|
||||||
|
|
||||||
|
|
||||||
/* block size: min = 16, max = 64k, power of 2 */
|
/* block size: min = 16, max = 64k, power of 2 */
|
||||||
|
@ -29,123 +30,54 @@
|
||||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||||
|
|
||||||
#define GR_PRIME 0x9e370001
|
#define GR_PRIME 0x9e370001
|
||||||
#define HASH(v, b) (((unsigned int)(v) * GR_PRIME) >> (32 - (b)))
|
#define HASH(v, shift) (((unsigned int)(v) * GR_PRIME) >> (shift))
|
||||||
|
|
||||||
static unsigned int hashbits(unsigned int size)
|
struct index {
|
||||||
{
|
|
||||||
unsigned int val = 1, bits = 0;
|
|
||||||
while (val < size && bits < 32) {
|
|
||||||
val <<= 1;
|
|
||||||
bits++;
|
|
||||||
}
|
|
||||||
return bits ? bits: 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct s_chanode {
|
|
||||||
struct s_chanode *next;
|
|
||||||
int icurr;
|
|
||||||
} chanode_t;
|
|
||||||
|
|
||||||
typedef struct s_chastore {
|
|
||||||
int isize, nsize;
|
|
||||||
chanode_t *ancur;
|
|
||||||
} chastore_t;
|
|
||||||
|
|
||||||
static void cha_init(chastore_t *cha, int isize, int icount)
|
|
||||||
{
|
|
||||||
cha->isize = isize;
|
|
||||||
cha->nsize = icount * isize;
|
|
||||||
cha->ancur = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *cha_alloc(chastore_t *cha)
|
|
||||||
{
|
|
||||||
chanode_t *ancur;
|
|
||||||
void *data;
|
|
||||||
|
|
||||||
ancur = cha->ancur;
|
|
||||||
if (!ancur || ancur->icurr == cha->nsize) {
|
|
||||||
ancur = malloc(sizeof(chanode_t) + cha->nsize);
|
|
||||||
if (!ancur)
|
|
||||||
return NULL;
|
|
||||||
ancur->icurr = 0;
|
|
||||||
ancur->next = cha->ancur;
|
|
||||||
cha->ancur = ancur;
|
|
||||||
}
|
|
||||||
|
|
||||||
data = (void *)ancur + sizeof(chanode_t) + ancur->icurr;
|
|
||||||
ancur->icurr += cha->isize;
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void cha_free(chastore_t *cha)
|
|
||||||
{
|
|
||||||
chanode_t *cur = cha->ancur;
|
|
||||||
while (cur) {
|
|
||||||
chanode_t *tmp = cur;
|
|
||||||
cur = cur->next;
|
|
||||||
free(tmp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct s_bdrecord {
|
|
||||||
struct s_bdrecord *next;
|
|
||||||
unsigned int fp;
|
|
||||||
const unsigned char *ptr;
|
const unsigned char *ptr;
|
||||||
} bdrecord_t;
|
unsigned int val;
|
||||||
|
struct index *next;
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct s_bdfile {
|
static struct index ** delta_index(const unsigned char *buf,
|
||||||
chastore_t cha;
|
unsigned long bufsize,
|
||||||
unsigned int fphbits;
|
unsigned int *hash_shift)
|
||||||
bdrecord_t **fphash;
|
|
||||||
} bdfile_t;
|
|
||||||
|
|
||||||
static int delta_prepare(const unsigned char *buf, int bufsize, bdfile_t *bdf)
|
|
||||||
{
|
{
|
||||||
unsigned int fphbits;
|
unsigned int hsize, hshift, entries, blksize, i;
|
||||||
int i, hsize;
|
const unsigned char *data;
|
||||||
const unsigned char *data, *top;
|
struct index *entry, **hash;
|
||||||
bdrecord_t *brec;
|
void *mem;
|
||||||
bdrecord_t **fphash;
|
|
||||||
|
|
||||||
fphbits = hashbits(bufsize / BLK_SIZE + 1);
|
/* determine index hash size */
|
||||||
hsize = 1 << fphbits;
|
entries = (bufsize + BLK_SIZE - 1) / BLK_SIZE;
|
||||||
fphash = malloc(hsize * sizeof(bdrecord_t *));
|
hsize = entries / 4;
|
||||||
if (!fphash)
|
for (i = 4; (1 << i) < hsize && i < 16; i++);
|
||||||
return -1;
|
hsize = 1 << i;
|
||||||
for (i = 0; i < hsize; i++)
|
hshift = 32 - i;
|
||||||
fphash[i] = NULL;
|
*hash_shift = hshift;
|
||||||
cha_init(&bdf->cha, sizeof(bdrecord_t), hsize / 4 + 1);
|
|
||||||
|
|
||||||
top = buf + bufsize;
|
/* allocate lookup index */
|
||||||
data = buf + (bufsize / BLK_SIZE) * BLK_SIZE;
|
mem = malloc(hsize * sizeof(*hash) + entries * sizeof(*entry));
|
||||||
if (data == top)
|
if (!mem)
|
||||||
|
return NULL;
|
||||||
|
hash = mem;
|
||||||
|
entry = mem + hsize * sizeof(*hash);
|
||||||
|
memset(hash, 0, hsize * sizeof(*hash));
|
||||||
|
|
||||||
|
/* then populate it */
|
||||||
|
data = buf + entries * BLK_SIZE - BLK_SIZE;
|
||||||
|
blksize = bufsize - (data - buf);
|
||||||
|
while (data >= buf) {
|
||||||
|
unsigned int val = adler32(0, data, blksize);
|
||||||
|
i = HASH(val, hshift);
|
||||||
|
entry->ptr = data;
|
||||||
|
entry->val = val;
|
||||||
|
entry->next = hash[i];
|
||||||
|
hash[i] = entry++;
|
||||||
|
blksize = BLK_SIZE;
|
||||||
data -= BLK_SIZE;
|
data -= BLK_SIZE;
|
||||||
|
|
||||||
for ( ; data >= buf; data -= BLK_SIZE) {
|
|
||||||
brec = cha_alloc(&bdf->cha);
|
|
||||||
if (!brec) {
|
|
||||||
cha_free(&bdf->cha);
|
|
||||||
free(fphash);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
brec->fp = adler32(0, data, MIN(BLK_SIZE, top - data));
|
|
||||||
brec->ptr = data;
|
|
||||||
i = HASH(brec->fp, fphbits);
|
|
||||||
brec->next = fphash[i];
|
|
||||||
fphash[i] = brec;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bdf->fphbits = fphbits;
|
return hash;
|
||||||
bdf->fphash = fphash;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void delta_cleanup(bdfile_t *bdf)
|
|
||||||
{
|
|
||||||
free(bdf->fphash);
|
|
||||||
cha_free(&bdf->cha);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* provide the size of the copy opcode given the block offset and size */
|
/* provide the size of the copy opcode given the block offset and size */
|
||||||
|
@ -161,14 +93,15 @@ void *diff_delta(void *from_buf, unsigned long from_size,
|
||||||
unsigned long *delta_size,
|
unsigned long *delta_size,
|
||||||
unsigned long max_size)
|
unsigned long max_size)
|
||||||
{
|
{
|
||||||
unsigned int i, outpos, outsize, inscnt, csize, msize, moff;
|
unsigned int i, outpos, outsize, inscnt, hash_shift;
|
||||||
unsigned int fp;
|
const unsigned char *ref_data, *ref_top, *data, *top;
|
||||||
const unsigned char *ref_data, *ref_top, *data, *top, *ptr1, *ptr2;
|
unsigned char *out;
|
||||||
unsigned char *out, *orig;
|
struct index *entry, **hash;
|
||||||
bdrecord_t *brec;
|
|
||||||
bdfile_t bdf;
|
|
||||||
|
|
||||||
if (!from_size || !to_size || delta_prepare(from_buf, from_size, &bdf))
|
if (!from_size || !to_size)
|
||||||
|
return NULL;
|
||||||
|
hash = delta_index(from_buf, from_size, &hash_shift);
|
||||||
|
if (!hash)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
outpos = 0;
|
outpos = 0;
|
||||||
|
@ -177,7 +110,7 @@ void *diff_delta(void *from_buf, unsigned long from_size,
|
||||||
outsize = max_size + MAX_OP_SIZE + 1;
|
outsize = max_size + MAX_OP_SIZE + 1;
|
||||||
out = malloc(outsize);
|
out = malloc(outsize);
|
||||||
if (!out) {
|
if (!out) {
|
||||||
delta_cleanup(&bdf);
|
free(hash);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,31 +138,35 @@ void *diff_delta(void *from_buf, unsigned long from_size,
|
||||||
}
|
}
|
||||||
|
|
||||||
inscnt = 0;
|
inscnt = 0;
|
||||||
moff = 0;
|
|
||||||
while (data < top) {
|
|
||||||
msize = 0;
|
|
||||||
fp = adler32(0, data, MIN(top - data, BLK_SIZE));
|
|
||||||
i = HASH(fp, bdf.fphbits);
|
|
||||||
for (brec = bdf.fphash[i]; brec; brec = brec->next) {
|
|
||||||
if (brec->fp == fp) {
|
|
||||||
csize = ref_top - brec->ptr;
|
|
||||||
if (csize > top - data)
|
|
||||||
csize = top - data;
|
|
||||||
for (ptr1 = brec->ptr, ptr2 = data;
|
|
||||||
csize && *ptr1 == *ptr2;
|
|
||||||
csize--, ptr1++, ptr2++);
|
|
||||||
|
|
||||||
csize = ptr1 - brec->ptr;
|
while (data < top) {
|
||||||
if (csize > msize) {
|
unsigned int moff = 0, msize = 0;
|
||||||
moff = brec->ptr - ref_data;
|
unsigned int blksize = MIN(top - data, BLK_SIZE);
|
||||||
msize = csize;
|
unsigned int val = adler32(0, data, blksize);
|
||||||
|
i = HASH(val, hash_shift);
|
||||||
|
for (entry = hash[i]; entry; entry = entry->next) {
|
||||||
|
const unsigned char *ref = entry->ptr;
|
||||||
|
const unsigned char *src = data;
|
||||||
|
unsigned int ref_size = ref_top - ref;
|
||||||
|
if (entry->val != val)
|
||||||
|
continue;
|
||||||
|
if (ref_size > top - src)
|
||||||
|
ref_size = top - src;
|
||||||
|
while (ref_size && *src++ == *ref) {
|
||||||
|
ref++;
|
||||||
|
ref_size--;
|
||||||
|
}
|
||||||
|
ref_size = ref - entry->ptr;
|
||||||
|
if (ref_size > msize) {
|
||||||
|
/* this is our best match so far */
|
||||||
|
moff = entry->ptr - ref_data;
|
||||||
|
msize = ref_size;
|
||||||
if (msize >= 0x10000) {
|
if (msize >= 0x10000) {
|
||||||
msize = 0x10000;
|
msize = 0x10000;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!msize || msize < COPYOP_SIZE(moff, msize)) {
|
if (!msize || msize < COPYOP_SIZE(moff, msize)) {
|
||||||
if (!inscnt)
|
if (!inscnt)
|
||||||
|
@ -241,13 +178,15 @@ void *diff_delta(void *from_buf, unsigned long from_size,
|
||||||
inscnt = 0;
|
inscnt = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
unsigned char *op;
|
||||||
|
|
||||||
if (inscnt) {
|
if (inscnt) {
|
||||||
out[outpos - inscnt - 1] = inscnt;
|
out[outpos - inscnt - 1] = inscnt;
|
||||||
inscnt = 0;
|
inscnt = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
data += msize;
|
data += msize;
|
||||||
orig = out + outpos++;
|
op = out + outpos++;
|
||||||
i = 0x80;
|
i = 0x80;
|
||||||
|
|
||||||
if (moff & 0xff) { out[outpos++] = moff; i |= 0x01; }
|
if (moff & 0xff) { out[outpos++] = moff; i |= 0x01; }
|
||||||
|
@ -262,7 +201,7 @@ void *diff_delta(void *from_buf, unsigned long from_size,
|
||||||
msize >>= 8;
|
msize >>= 8;
|
||||||
if (msize & 0xff) { out[outpos++] = msize; i |= 0x20; }
|
if (msize & 0xff) { out[outpos++] = msize; i |= 0x20; }
|
||||||
|
|
||||||
*orig = i;
|
*op = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (outpos >= outsize - MAX_OP_SIZE) {
|
if (outpos >= outsize - MAX_OP_SIZE) {
|
||||||
|
@ -276,7 +215,7 @@ void *diff_delta(void *from_buf, unsigned long from_size,
|
||||||
out = realloc(out, outsize);
|
out = realloc(out, outsize);
|
||||||
if (!out) {
|
if (!out) {
|
||||||
free(tmp);
|
free(tmp);
|
||||||
delta_cleanup(&bdf);
|
free(hash);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -285,7 +224,7 @@ void *diff_delta(void *from_buf, unsigned long from_size,
|
||||||
if (inscnt)
|
if (inscnt)
|
||||||
out[outpos - inscnt - 1] = inscnt;
|
out[outpos - inscnt - 1] = inscnt;
|
||||||
|
|
||||||
delta_cleanup(&bdf);
|
free(hash);
|
||||||
*delta_size = outpos;
|
*delta_size = outpos;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue