| 1 | /* |
| 2 | * crc.h |
| 3 | * |
| 4 | * Generic CRC calculation code. |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef CRC_H_ |
| 9 | #define CRC_H_ |
| 10 | |
| 11 | #include <stdint.h> |
| 12 | |
| 13 | typedef struct crc { |
| 14 | uint32_t state; |
| 15 | int order; |
| 16 | uint32_t polynom; |
| 17 | uint32_t initial_value; |
| 18 | uint32_t final_xor; |
| 19 | uint32_t mask; |
| 20 | } crc_t; |
| 21 | |
| 22 | /* Initialize a crc structure. order is the order of the polynom, e.g. 32 for a CRC-32 |
| 23 | * polynom is the CRC polynom. initial_value is the initial value of a clean state. |
| 24 | * final_xor is XORed onto the state before returning it from crc_result(). */ |
| 25 | extern void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor); |
| 26 | |
| 27 | /* Update the crc state. data is the data of length data_width bits (only the the |
| 28 | * data_width lower-most bits are used). |
| 29 | */ |
| 30 | extern void crc_update(crc_t *crc, uint32_t data, int data_width); |
| 31 | |
| 32 | /* Clean the crc state, e.g. reset it to initial_value */ |
| 33 | extern void crc_clear(crc_t *crc); |
| 34 | |
| 35 | /* Get the result of the crc calculation */ |
| 36 | extern uint32_t crc_finish(crc_t *crc); |
| 37 | |
| 38 | /* Static initialization of a crc structure */ |
| 39 | #define CRC_INITIALIZER(_order, _polynom, _initial_value, _final_xor) { \ |
| 40 | .state = ((_initial_value) & ((1L<<(_order))-1)), \ |
| 41 | .order = (_order), \ |
| 42 | .polynom = (_polynom), \ |
| 43 | .initial_value = (_initial_value), \ |
| 44 | .final_xor = (_final_xor), \ |
| 45 | .mask = ((1L<<(_order))-1) } |
| 46 | |
| 47 | #endif /* CRC_H_ */ |