#include <stdlib.h>
#include "delta.h"
+#include "zlib.h"
/* block size: min = 16, max = 64k, power of 2 */
#define GR_PRIME 0x9e370001
#define HASH(v, b) (((unsigned int)(v) * GR_PRIME) >> (32 - (b)))
-/* largest prime smaller than 65536 */
-#define BASE 65521
-
-/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
-#define NMAX 5552
-
-#define DO1(buf, i) { s1 += buf[i]; s2 += s1; }
-#define DO2(buf, i) DO1(buf, i); DO1(buf, i + 1);
-#define DO4(buf, i) DO2(buf, i); DO2(buf, i + 2);
-#define DO8(buf, i) DO4(buf, i); DO4(buf, i + 4);
-#define DO16(buf) DO8(buf, 0); DO8(buf, 8);
-
-static unsigned int adler32(unsigned int adler, const unsigned char *buf, int len)
-{
- int k;
- unsigned int s1 = adler & 0xffff;
- unsigned int s2 = adler >> 16;
-
- while (len > 0) {
- k = MIN(len, NMAX);
- len -= k;
- while (k >= 16) {
- DO16(buf);
- buf += 16;
- k -= 16;
- }
- if (k != 0)
- do {
- s1 += *buf++;
- s2 += s1;
- } while (--k);
- s1 %= BASE;
- s2 %= BASE;
- }
-
- return (s2 << 16) | s1;
-}
-
static unsigned int hashbits(unsigned int size)
{
unsigned int val = 1, bits = 0;
} chanode_t;
typedef struct s_chastore {
- chanode_t *head, *tail;
int isize, nsize;
chanode_t *ancur;
- chanode_t *sncur;
- int scurr;
} chastore_t;
static void cha_init(chastore_t *cha, int isize, int icount)
{
- cha->head = cha->tail = NULL;
cha->isize = isize;
cha->nsize = icount * isize;
- cha->ancur = cha->sncur = NULL;
- cha->scurr = 0;
+ cha->ancur = NULL;
}
static void *cha_alloc(chastore_t *cha)
if (!ancur)
return NULL;
ancur->icurr = 0;
- ancur->next = NULL;
- if (cha->tail)
- cha->tail->next = ancur;
- if (!cha->head)
- cha->head = ancur;
- cha->tail = ancur;
+ ancur->next = cha->ancur;
cha->ancur = ancur;
}
static void cha_free(chastore_t *cha)
{
- chanode_t *cur = cha->head;
+ chanode_t *cur = cha->ancur;
while (cur) {
chanode_t *tmp = cur;
cur = cur->next;
} bdrecord_t;
typedef struct s_bdfile {
- const unsigned char *data, *top;
chastore_t cha;
unsigned int fphbits;
bdrecord_t **fphash;
{
unsigned int fphbits;
int i, hsize;
- const unsigned char *base, *data, *top;
+ const unsigned char *data, *top;
bdrecord_t *brec;
bdrecord_t **fphash;
fphash[i] = NULL;
cha_init(&bdf->cha, sizeof(bdrecord_t), hsize / 4 + 1);
- bdf->data = data = base = buf;
- bdf->top = top = buf + bufsize;
- data += (bufsize / BLK_SIZE) * BLK_SIZE;
+ top = buf + bufsize;
+ data = buf + (bufsize / BLK_SIZE) * BLK_SIZE;
if (data == top)
data -= BLK_SIZE;
- for ( ; data >= base; data -= BLK_SIZE) {
+ for ( ; data >= buf; data -= BLK_SIZE) {
brec = cha_alloc(&bdf->cha);
if (!brec) {
cha_free(&bdf->cha);
{
int i, outpos, outsize, inscnt, csize, msize, moff;
unsigned int fp;
- const unsigned char *data, *top, *ptr1, *ptr2;
+ const unsigned char *ref_data, *ref_top, *data, *top, *ptr1, *ptr2;
unsigned char *out, *orig;
bdrecord_t *brec;
bdfile_t bdf;
- if (delta_prepare(from_buf, from_size, &bdf))
+ if (!from_size || !to_size || delta_prepare(from_buf, from_size, &bdf))
return NULL;
outpos = 0;
return NULL;
}
+ ref_data = from_buf;
+ ref_top = from_buf + from_size;
data = to_buf;
top = to_buf + to_size;
i = HASH(fp, bdf.fphbits);
for (brec = bdf.fphash[i]; brec; brec = brec->next) {
if (brec->fp == fp) {
- csize = bdf.top - brec->ptr;
+ csize = ref_top - brec->ptr;
if (csize > top - data)
csize = top - data;
for (ptr1 = brec->ptr, ptr2 = data;
csize = ptr1 - brec->ptr;
if (csize > msize) {
- moff = brec->ptr - bdf.data;
+ moff = brec->ptr - ref_data;
msize = csize;
if (msize >= 0x10000) {
msize = 0x10000;