#include <sys/queue.h>
#include <sys/stat.h>
#include <assert.h>
#include <endian.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/md4.h>
#include "extern.h"
struct blkhash {
const struct blk *blk;
TAILQ_ENTRY(blkhash) entries;
};
TAILQ_HEAD(blkhashq, blkhash);
struct blktab {
struct blkhashq *q;
size_t qsz;
struct blkhash *blks;
};
#define BLKTAB_SZ 65536
struct blktab *
blkhash_alloc(void)
{
struct blktab *p;
if ((p = calloc(1, sizeof(struct blktab))) == NULL) {
ERR("calloc");
return NULL;
}
p->qsz = BLKTAB_SZ;
p->q = calloc(p->qsz, sizeof(struct blkhashq));
if (p->q == NULL) {
ERR("calloc");
free(p);
return NULL;
}
return p;
}
int
blkhash_set(struct blktab *p, const struct blkset *bset)
{
struct blkhash *blks;
size_t i, idx;
if (bset == NULL || bset->blksz == 0)
return 1;
for (i = 0; i < p->qsz; i++)
TAILQ_INIT(&p->q[i]);
blks = reallocarray(p->blks, bset->blksz, sizeof(struct blkhash));
if (blks == NULL) {
ERR("reallocarray");
free(p->blks);
p->blks = NULL;
return 0;
}
p->blks = blks;
for (i = 0; i < bset->blksz; i++) {
p->blks[i].blk = &bset->blks[i];
idx = bset->blks[i].chksum_short % p->qsz;
assert(idx < p->qsz);
TAILQ_INSERT_TAIL(&p->q[idx], &p->blks[i], entries);
}
return 1;
}
void
blkhash_free(struct blktab *p)
{
if (p == NULL)
return;
free(p->q);
free(p->blks);
free(p);
}
static const struct blk *
blk_find(struct sess *sess, struct blkstat *st,
const struct blkset *blks, const char *path, int recomp)
{
unsigned char md[MD4_DIGEST_LENGTH];
uint32_t fhash;
off_t remain, osz;
int have_md = 0;
char *map;
const struct blkhashq *q;
const struct blkhash *ent;
remain = st->mapsz - st->offs;
assert(remain);
osz = MINIMUM(remain, (off_t)blks->len);
if (!recomp) {
fhash = (st->s1 & 0xFFFF) | (st->s2 << 16);
} else {
fhash = hash_fast(st->map + st->offs, (size_t)osz);
st->s1 = fhash & 0xFFFF;
st->s2 = fhash >> 16;
}
if (st->hint < blks->blksz &&
fhash == blks->blks[st->hint].chksum_short &&
(size_t)osz == blks->blks[st->hint].len) {
hash_slow(st->map + st->offs, (size_t)osz, md, sess);
have_md = 1;
if (memcmp(md, blks->blks[st->hint].chksum_long, blks->csum) == 0) {
LOG4("%s: found matching hinted match: "
"position %jd, block %zu (position %jd, size %zu)",
path,
(intmax_t)st->offs, blks->blks[st->hint].idx,
(intmax_t)blks->blks[st->hint].offs,
blks->blks[st->hint].len);
return &blks->blks[st->hint];
}
}
q = &st->blktab->q[fhash % st->blktab->qsz];
TAILQ_FOREACH(ent, q, entries) {
if (fhash != ent->blk->chksum_short ||
(size_t)osz != ent->blk->len)
continue;
LOG4("%s: found matching fast match: "
"position %jd, block %zu (position %jd, size %zu)",
path, (intmax_t)st->offs, ent->blk->idx,
(intmax_t)ent->blk->offs, ent->blk->len);
if (have_md == 0) {
hash_slow(st->map + st->offs, (size_t)osz, md, sess);
have_md = 1;
}
if (memcmp(md, ent->blk->chksum_long, blks->csum))
continue;
LOG4("%s: sender verifies slow match", path);
return ent->blk;
}
map = st->map + st->offs;
st->s1 -= map[0];
st->s2 -= osz * map[0];
if (osz < remain) {
st->s1 += map[osz];
st->s2 += st->s1;
}
return NULL;
}
void
blk_match(struct sess *sess, const struct blkset *blks,
const char *path, struct blkstat *st)
{
off_t last, end = 0, sz;
int32_t tok;
size_t i;
const struct blk *blk;
if (st->mapsz && blks->blksz) {
end = st->mapsz + 1 - blks->blks[blks->blksz - 1].len;
}
last = st->offs;
for (i = 0; st->offs < end; st->offs++, i++) {
blk = blk_find(sess, st, blks, path, i == 0);
if (blk == NULL)
continue;
sz = st->offs - last;
st->dirty += sz;
st->total += sz;
LOG4("%s: flushing %jd B before %zu B block %zu",
path, (intmax_t)sz,
blk->len, blk->idx);
tok = -(blk->idx + 1);
hash_file_buf(&st->ctx, st->map + last, sz + blk->len);
st->curpos = last;
st->curlen = st->curpos + sz;
st->curtok = tok;
assert(st->curtok != 0);
st->curst = sz ? BLKSTAT_DATA : BLKSTAT_TOK;
st->total += blk->len;
st->offs += blk->len;
st->hint = blk->idx + 1;
return;
}
sz = st->mapsz - last;
LOG4("%s: flushing %s %jd B", path,
last == 0 ? "whole" : "remaining", (intmax_t)sz);
hash_file_buf(&st->ctx, st->map + last, sz);
st->total += sz;
st->dirty += sz;
st->curpos = last;
st->curlen = st->curpos + sz;
st->curtok = 0;
st->curst = sz ? BLKSTAT_DATA : BLKSTAT_TOK;
}
void
blk_recv_ack(char buf[20], const struct blkset *blocks, int32_t idx)
{
size_t pos = 0, sz;
sz = sizeof(int32_t) +
sizeof(int32_t) +
sizeof(int32_t) +
sizeof(int32_t) +
sizeof(int32_t);
assert(sz == 20);
io_buffer_int(buf, &pos, sz, idx);
io_buffer_int(buf, &pos, sz, blocks->blksz);
io_buffer_int(buf, &pos, sz, blocks->len);
io_buffer_int(buf, &pos, sz, blocks->csum);
io_buffer_int(buf, &pos, sz, blocks->rem);
assert(pos == sz);
}
struct blkset *
blk_recv(struct sess *sess, int fd, const char *path)
{
struct blkset *s;
int32_t i;
size_t j;
struct blk *b;
off_t offs = 0;
if ((s = calloc(1, sizeof(struct blkset))) == NULL) {
ERR("calloc");
return NULL;
}
if (!io_read_size(sess, fd, &s->blksz)) {
ERRX1("io_read_size");
goto out;
} else if (!io_read_size(sess, fd, &s->len)) {
ERRX1("io_read_size");
goto out;
} else if (!io_read_size(sess, fd, &s->csum)) {
ERRX1("io_read_int");
goto out;
} else if (!io_read_size(sess, fd, &s->rem)) {
ERRX1("io_read_int");
goto out;
} else if (s->rem && s->rem >= s->len) {
ERRX("block remainder is "
"greater than block size");
goto out;
}
LOG3("%s: read block prologue: %zu blocks of "
"%zu B, %zu B remainder, %zu B checksum", path,
s->blksz, s->len, s->rem, s->csum);
if (s->blksz) {
s->blks = calloc(s->blksz, sizeof(struct blk));
if (s->blks == NULL) {
ERR("calloc");
goto out;
}
}
for (j = 0; j < s->blksz; j++) {
b = &s->blks[j];
if (!io_read_int(sess, fd, &i)) {
ERRX1("io_read_int");
goto out;
}
b->chksum_short = i;
assert(s->csum <= sizeof(b->chksum_long));
if (!io_read_buf(sess,
fd, b->chksum_long, s->csum)) {
ERRX1("io_read_buf");
goto out;
}
b->offs = offs;
b->idx = j;
b->len = (j == (s->blksz - 1) && s->rem) ?
s->rem : s->len;
offs += b->len;
LOG4("%s: read block %zu, length %zu B",
path, b->idx, b->len);
}
s->size = offs;
LOG3("%s: read blocks: %zu blocks, %jd B total blocked data",
path, s->blksz, (intmax_t)s->size);
return s;
out:
free(s->blks);
free(s);
return NULL;
}
int
blk_send_ack(struct sess *sess, int fd, struct blkset *p)
{
char buf[16];
size_t pos = 0, sz;
sz = sizeof(int32_t) +
sizeof(int32_t) +
sizeof(int32_t) +
sizeof(int32_t);
assert(sz <= sizeof(buf));
if (!io_read_buf(sess, fd, buf, sz)) {
ERRX1("io_read_buf");
return 0;
}
if (!io_unbuffer_size(buf, &pos, sz, &p->blksz))
ERRX1("io_unbuffer_size");
else if (!io_unbuffer_size(buf, &pos, sz, &p->len))
ERRX1("io_unbuffer_size");
else if (!io_unbuffer_size(buf, &pos, sz, &p->csum))
ERRX1("io_unbuffer_size");
else if (!io_unbuffer_size(buf, &pos, sz, &p->rem))
ERRX1("io_unbuffer_size");
else if (p->len && p->rem >= p->len)
ERRX1("non-zero length is less than remainder");
else if (p->csum == 0 || p->csum > 16)
ERRX1("inappropriate checksum length");
else
return 1;
return 0;
}
int
blk_send(struct sess *sess, int fd, size_t idx,
const struct blkset *p, const char *path)
{
char *buf;
size_t i, pos = 0, sz;
int rc = 0;
sz = sizeof(int32_t) +
sizeof(int32_t) +
sizeof(int32_t) +
sizeof(int32_t) +
sizeof(int32_t) +
p->blksz *
(sizeof(int32_t) +
p->csum);
if ((buf = malloc(sz)) == NULL) {
ERR("malloc");
return 0;
}
io_buffer_int(buf, &pos, sz, idx);
io_buffer_int(buf, &pos, sz, p->blksz);
io_buffer_int(buf, &pos, sz, p->len);
io_buffer_int(buf, &pos, sz, p->csum);
io_buffer_int(buf, &pos, sz, p->rem);
for (i = 0; i < p->blksz; i++) {
io_buffer_int(buf, &pos, sz, p->blks[i].chksum_short);
io_buffer_buf(buf, &pos, sz, p->blks[i].chksum_long, p->csum);
}
assert(pos == sz);
if (!io_write_buf(sess, fd, buf, sz)) {
ERRX1("io_write_buf");
goto out;
}
LOG3("%s: sent block prologue: %zu blocks of %zu B, "
"%zu B remainder, %zu B checksum",
path, p->blksz, p->len, p->rem, p->csum);
rc = 1;
out:
free(buf);
return rc;
}