New test and fixes to crcsync.
[ccan] / ccan / crcsync / crcsync.c
index 87f513ea90cc74576b2f5854ccd68431f8ed5e65..2301bc103c1fce4952dbb14cd88057416d62339c 100644 (file)
@@ -192,7 +192,11 @@ size_t crc_read_block(struct crc_context *ctx, long *result,
                if (ctx->literal_bytes > ctx->block_size) {
                        *result = ctx->literal_bytes - ctx->block_size;
                        ctx->literal_bytes -= *result;
-                       ctx->buffer_start += *result;
+                       /* Advance buffer. */
+                       if (*result >= buffer_size(ctx))
+                               ctx->buffer_start = ctx->buffer_end = 0;
+                       else
+                               ctx->buffer_start += *result;
                } else
                        *result = 0;
 
@@ -206,7 +210,9 @@ size_t crc_read_block(struct crc_context *ctx, long *result,
                        ctx->buffer_end -= ctx->buffer_start;
                        ctx->buffer_start = 0;
                }
-               memcpy(ctx->buffer + ctx->buffer_end, buf, len);
+
+               /* Copy len bytes from tail of buffer. */
+               memcpy(ctx->buffer + ctx->buffer_end, buf + buflen - len, len);
                ctx->buffer_end += len;
                assert(buffer_size(ctx) <= ctx->block_size);
        }