]> git.saurik.com Git - apple/security.git/blob - OSX/authd/crc.h
Security-59754.41.1.tar.gz
[apple/security.git] / OSX / authd / crc.h
1 /* Copyright (c) 2012 Apple Inc. All Rights Reserved. */
2
3 #ifndef _SECURITY_AUTH_CRC_H_
4 #define _SECURITY_AUTH_CRC_H_
5
6 #if defined(__cplusplus)
7 extern "C" {
8 #endif
9
10 extern const uint64_t _crc_table64[256];
11 extern const uint64_t xorout;
12
13 AUTH_INLINE uint64_t
14 crc64_init()
15 {
16 return xorout;
17 }
18
19 AUTH_INLINE uint64_t
20 crc64_final(uint64_t crc)
21 {
22 return crc ^ xorout;
23 }
24
25 AUTH_INLINE AUTH_NONNULL_ALL uint64_t
26 crc64_update(uint64_t crc, const void *buf, uint64_t len)
27 {
28 const unsigned char * ptr = (const unsigned char *) buf;
29
30 while (len-- > 0) {
31 crc = _crc_table64[((crc >> 56) ^ *(ptr++)) & 0xff] ^ (crc << 8);
32 }
33
34 return crc;
35 }
36
37 AUTH_INLINE uint64_t
38 crc64(const void *buf, uint64_t len)
39 {
40 uint64_t crc = crc64_init();
41
42 crc = crc64_update(crc, buf, len);
43
44 crc = crc64_final(crc);
45
46 return crc;
47 }
48
49 #if defined(__cplusplus)
50 }
51 #endif
52
53 #endif /* !_SECURITY_AUTH_CRC_H_ */