X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fcrcsync%2Fcrcsync.c;h=6c251f1ff967d1c1a49a9a42592f50926e1223c0;hp=cb768806f6e03e829293f624ea06db540fe02e32;hb=3404ba2a1b34e766628d5b9febf401011aa6753f;hpb=94ff9fc87d90173145772a41f79987988f9f0972 diff --git a/ccan/crcsync/crcsync.c b/ccan/crcsync/crcsync.c index cb768806..6c251f1f 100644 --- a/ccan/crcsync/crcsync.c +++ b/ccan/crcsync/crcsync.c @@ -170,10 +170,6 @@ size_t crc_read_block(struct crc_context *ctx, long *result, /* Make sure we have a copy of the last block_size bytes. * First, copy down the old data. */ if (buffer_size(ctx)) { - memmove(ctx->buffer, ctx->buffer + ctx->buffer_start, - buffer_size(ctx)); - ctx->buffer_end -= ctx->buffer_start; - ctx->buffer_start = 0; } if (crcmatch >= 0) { @@ -202,6 +198,14 @@ size_t crc_read_block(struct crc_context *ctx, long *result, /* Now save any literal bytes we'll need in future. */ len = ctx->literal_bytes - buffer_size(ctx); + + /* Move down old data if we don't have room. */ + if (ctx->buffer_end + len > ctx->block_size) { + memmove(ctx->buffer, ctx->buffer + ctx->buffer_start, + buffer_size(ctx)); + ctx->buffer_end -= ctx->buffer_start; + ctx->buffer_start = 0; + } memcpy(ctx->buffer + ctx->buffer_end, buf, len); ctx->buffer_end += len; assert(buffer_size(ctx) <= ctx->block_size);