2 #include <ccan/crc/crc.h>
7 void crc_of_blocks(const void *data, size_t len, unsigned int block_size,
8 unsigned int crcbits, uint32_t crc[])
11 const uint8_t *buf = data;
12 uint32_t crcmask = crcbits < 32 ? (1 << crcbits) - 1 : 0xFFFFFFFF;
14 for (i = 0; len >= block_size; i++) {
15 crc[i] = (crc32c(0, buf, block_size) & crcmask);
20 crc[i] = (crc32c(0, buf, len) & crcmask);
27 /* Saved old buffer bytes (block_size bytes). */
29 size_t buffer_start, buffer_end;
31 /* Progress so far. */
37 /* Final block is special (if a different size) */
42 uint32_t uncrc_tab[256];
44 /* This doesn't count the last CRC. */
45 unsigned int num_crcs;
49 /* Calculate the how the crc changes when we take a give char out of the
51 static void init_uncrc_tab(uint32_t uncrc_tab[], unsigned int wsize)
55 uint8_t buffer[wsize];
57 /* Calculate crc(buffer+1, wsize-1) once, since it doesn't change. */
58 memset(buffer, 0, wsize);
59 part_crc = crc32c(0, buffer+1, wsize-1);
61 for (i = 0; i < 256; i++) {
63 uncrc_tab[i] = (crc32c(0, buffer, wsize) ^ part_crc);
67 struct crc_context *crc_context_new(size_t block_size, unsigned crcbits,
68 const uint32_t crc[], unsigned num_crcs,
71 struct crc_context *ctx;
74 assert(block_size > 0);
75 assert(tail_size < block_size);
77 ctx = malloc(sizeof(*ctx) + sizeof(crc[0])*num_crcs);
79 ctx->block_size = block_size;
80 ctx->tail_size = tail_size;
82 ctx->tail_crc = crc[--num_crcs];
84 /* Technically, 1 << 32 is undefined. */
86 ctx->crcmask = 0xFFFFFFFF;
88 ctx->crcmask = (1 << crcbits)-1;
89 ctx->num_crcs = num_crcs;
90 memcpy(ctx->crc, crc, sizeof(crc[0])*num_crcs);
92 ctx->buffer_start = 0;
94 ctx->literal_bytes = 0;
97 init_uncrc_tab(ctx->uncrc_tab, block_size);
98 ctx->buffer = malloc(block_size);
107 /* Return -1 or index into matching crc. */
108 static int crc_matches(const struct crc_context *ctx)
112 if (ctx->literal_bytes < ctx->block_size)
115 for (i = 0; i < ctx->num_crcs; i++)
116 if ((ctx->running_crc & ctx->crcmask) == ctx->crc[i])
121 static bool tail_matches(const struct crc_context *ctx)
123 if (ctx->literal_bytes != ctx->tail_size)
126 return (ctx->running_crc & ctx->crcmask) == ctx->tail_crc;
129 static uint32_t crc_add_byte(uint32_t crc, uint8_t newbyte)
131 return crc32c(crc, &newbyte, 1);
134 static uint32_t crc_remove_byte(uint32_t crc, uint8_t oldbyte,
135 const uint32_t uncrc_tab[])
137 return crc ^ uncrc_tab[oldbyte];
140 static uint32_t crc_roll(uint32_t crc, uint8_t oldbyte, uint8_t newbyte,
141 const uint32_t uncrc_tab[])
143 return crc_add_byte(crc_remove_byte(crc, oldbyte, uncrc_tab), newbyte);
146 static size_t buffer_size(const struct crc_context *ctx)
148 return ctx->buffer_end - ctx->buffer_start;
151 size_t crc_read_block(struct crc_context *ctx, long *result,
152 const void *buf, size_t buflen)
154 size_t consumed = 0, len;
156 const uint8_t *old, *p = buf;
158 /* Simple optimization, if we found a match last time. */
159 if (ctx->have_match >= 0) {
160 crcmatch = ctx->have_match;
164 /* old is the trailing edge of the checksum window. */
165 if (buffer_size(ctx) >= ctx->block_size)
166 old = ctx->buffer + ctx->buffer_start;
170 while (ctx->literal_bytes < ctx->block_size
171 || (crcmatch = crc_matches(ctx)) < 0) {
172 if (consumed == buflen)
175 /* Increment these now in case we hit goto: below. */
176 ctx->literal_bytes++;
182 ctx->running_crc = crc_roll(ctx->running_crc,
186 /* End of stored buffer? Start on data they gave us. */
187 if (old == ctx->buffer + ctx->buffer_end)
190 ctx->running_crc = crc_add_byte(ctx->running_crc, *p);
191 if (p == (uint8_t *)buf + ctx->block_size - 1)
193 /* We don't roll this csum, we only look for it after
194 * a block match. It's simpler and faster. */
195 if (tail_matches(ctx)) {
196 crcmatch = ctx->num_crcs;
204 /* We have a match! */
205 if (ctx->literal_bytes > ctx->block_size) {
206 /* Output literal first. */
207 *result = ctx->literal_bytes - ctx->block_size;
208 ctx->literal_bytes = ctx->block_size;
209 /* Remember for next time! */
210 ctx->have_match = crcmatch;
213 *result = -crcmatch-1;
214 if (crcmatch == ctx->num_crcs)
215 assert(ctx->literal_bytes == ctx->tail_size);
217 assert(ctx->literal_bytes == ctx->block_size);
218 ctx->literal_bytes = 0;
219 ctx->have_match = -1;
220 ctx->running_crc = 0;
221 /* Nothing more in the buffer. */
222 ctx->buffer_start = ctx->buffer_end = 0;
225 /* Output literal if it's more than 1 block ago. */
226 if (ctx->literal_bytes > ctx->block_size) {
227 *result = ctx->literal_bytes - ctx->block_size;
228 ctx->literal_bytes -= *result;
229 /* Advance buffer. */
230 if (*result >= buffer_size(ctx))
231 ctx->buffer_start = ctx->buffer_end = 0;
233 ctx->buffer_start += *result;
237 /* Now save any literal bytes we'll need in future. */
238 len = ctx->literal_bytes - buffer_size(ctx);
240 /* Move down old data if we don't have room. */
241 if (ctx->buffer_end + len > ctx->block_size) {
242 memmove(ctx->buffer, ctx->buffer + ctx->buffer_start,
244 ctx->buffer_end -= ctx->buffer_start;
245 ctx->buffer_start = 0;
248 /* Copy len bytes from tail of buffer. */
249 memcpy(ctx->buffer + ctx->buffer_end, buf + buflen - len, len);
250 ctx->buffer_end += len;
251 assert(buffer_size(ctx) <= ctx->block_size);
256 long crc_read_flush(struct crc_context *ctx)
260 /* We might have ended right on a matched block. */
261 if (ctx->have_match != -1) {
262 ctx->literal_bytes -= ctx->block_size;
263 assert(ctx->literal_bytes == 0);
264 ret = -ctx->have_match-1;
265 ctx->have_match = -1;
266 ctx->running_crc = 0;
267 /* Nothing more in the buffer. */
268 ctx->buffer_start = ctx->buffer_end;
272 /* The rest is just a literal. */
273 ret = buffer_size(ctx);
274 assert(ctx->literal_bytes == ret);
275 ctx->buffer_start = ctx->buffer_end = 0;
276 ctx->literal_bytes = 0;
281 * crc_context_free - free a context returned from crc_context_new.
282 * @ctx: the context returned from crc_context_new, or NULL.
284 void crc_context_free(struct crc_context *ctx)