openwrt-keyring: update to Git HEAD
[openwrt/.git] / package / system / fwtool / src / crc32.h
1 /*
2  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
3  *
4  * Based on busybox code:
5  *   CRC32 table fill function
6  *   Copyright (C) 2006 by Rob Sullivan <cogito.ergo.cogito@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 #ifndef __BB_CRC32_H
18 #define __BB_CRC32_H
19
20 static inline void
21 crc32_filltable(uint32_t *crc_table)
22 {
23         uint32_t polynomial = 0xedb88320;
24         uint32_t c;
25         int i, j;
26
27         for (i = 0; i < 256; i++) {
28                 c = i;
29                 for (j = 8; j; j--)
30                         c = (c&1) ? ((c >> 1) ^ polynomial) : (c >> 1);
31
32                 *crc_table++ = c;
33         }
34 }
35
36 static inline uint32_t
37 crc32_block(uint32_t val, const void *buf, unsigned len, uint32_t *crc_table)
38 {
39         const void *end = (uint8_t*)buf + len;
40
41         while (buf != end) {
42                 val = crc_table[(uint8_t)val ^ *(uint8_t*)buf] ^ (val >> 8);
43                 buf = (uint8_t*)buf + 1;
44         }
45         return val;
46 }
47
48 #endif