OSDN Git Service

client 0.5.2 release
[unagi/old-svn-converted.git] / client / tag / 0.5.2 / crc32.c
1 /*
2 GZIP file format specification version 4.3
3 */
4 #include "type.h"
5 #include "crc32.h"
6 #include "crctable.h"
7 /*
8    Update a running crc with the bytes buf[0..len-1] and return
9  the updated crc. The crc should be initialized to zero. Pre- and
10  post-conditioning (one's complement) is performed within this
11  function so it shouldn't be done by the caller. Usage example:
12
13    unsigned long crc = 0L;
14
15    while (read_buffer(buffer, length) != EOF) {
16      crc = update_crc(crc, buffer, length);
17    }
18    if (crc != original_crc) error();
19 */
20 static uint32_t update_crc(uint32_t crc, const uint8_t *buf, int len)
21 {
22         uint32_t c = crc ^ 0xffffffffUL;
23         int n;
24
25         for (n = 0; n < len; n++) {
26                 c = CRCTABLE[(c ^ buf[n]) & 0xff] ^ (c >> 8);
27         }
28         return c ^ 0xffffffffUL;
29 }
30
31 /* Return the CRC of the bytes buf[0..len-1]. */
32 //uint32_t crc(uint8_t *buf, int len) //ÊÑ¿ô̾¤È¤«¤Ö¤ë¤Î¤Ç¤«¤¨¤ë
33 uint32_t crc32_get(const uint8_t *buf, int len)
34 {
35         return update_crc(0UL, buf, len);
36 }