]> mj.ucw.cz Git - libucw.git/blob - ucw/crc.h
0e3d0137c20858fe775cec9afe30116e850dd107
[libucw.git] / ucw / crc.h
1 /*
2  *      CRC32 (Castagnoli 1993)
3  *
4  *      Based on Michael E. Kounavis and Frank L. Berry: A Systematic Approach
5  *      to Building High Performance Software-based CRC Generators
6  *      (Proceedings of the 10th IEEE Symposium on Computers and Communications 2005)
7  *
8  *      Includes code from http://sourceforge.net/projects/slicing-by-8/,
9  *      which carried the following copyright notice:
10  *
11  *      Copyright (c) 2004-2006 Intel Corporation - All Rights Reserved
12  *
13  *      This software program is licensed subject to the BSD License,
14  *      available at http://www.opensource.org/licenses/bsd-license.html
15  *
16  *      Adapted for LibUCW by Martin Mares <mj@ucw.cz> in 2012.
17  */
18
19 /*
20  * Internal CRC calculator context.
21  * You should use it just as an opaque handle only.
22  */
23 typedef struct crc32_context {
24   u32 state;
25   void (*update_func)(struct crc32_context *ctx, const byte *buf, uns len);
26 } crc32_context;
27
28 void crc32_init(crc32_context *ctx, uns crc_mode);
29
30 enum crc_mode {
31   CRC_MODE_DEFAULT,
32   CRC_MODE_SMALL,
33   CRC_MODE_BIG,
34   CRC_MODE_MAX,
35 };
36
37 static inline void
38 crc32_update(crc32_context *ctx, const byte *buf, uns len)
39 {
40   ctx->update_func(ctx, buf, len);
41 }
42
43 static inline u32
44 crc32_final(crc32_context *ctx)
45 {
46   return ctx->state ^ 0xffffffff;
47 }