2 changed files with 29 additions and 0 deletions
@ -0,0 +1,28 @@ |
|||
(c-import |
|||
"<assert.h>" |
|||
&with-decls "<stdint.h>" "<stddef.h>") |
|||
|
|||
;; From http://home.thep.lu.se/~bjorn/crc/ (public domain) |
|||
(defun-local hash-crc32-for-byte (r uint32_t &return uint32_t) |
|||
(each-in-range 8 j |
|||
(set r (bit-xor |
|||
(? (bit-and r 1) 0 |
|||
(type-cast 0xEDB88320L uint32_t)) |
|||
(bit->> r 1)))) |
|||
(return (bit-xor r (type-cast 0xFF000000L uint32_t)))) |
|||
|
|||
(var s-crc32-table ([] 0x100 uint32_t)) |
|||
(defun hash-crc32-initialize () |
|||
(each-in-range 0x100 i |
|||
(set (at i s-crc32-table) (hash-crc32-for-byte i)))) |
|||
|
|||
(defun hash-crc32 (data (* (const void)) n-bytes size_t crc-out (* uint32_t)) |
|||
(unless (deref s-crc32-table) |
|||
(assert (and s-crc32-table "Need to call crc32-initialize first"))) |
|||
(each-in-range n-bytes i |
|||
(set (deref crc-out) |
|||
(bit-xor |
|||
(at (bit-xor (type-cast (deref crc-out) uint8_t) |
|||
(at i (type-cast data (* uint8_t)))) |
|||
s-crc32-table) |
|||
(bit->> (deref crc-out) 8))))) |
Loading…
Reference in new issue