Browse Source

Add simple CRC32 functions

master
Macoy Madson 5 months ago
parent
commit
6ffd21713e
  1. 1
      ReadMe.org
  2. 28
      src/Hash.cake

1
ReadMe.org

@ -106,6 +106,7 @@ Here are the known compatibility results, where blank means untested/unknown:
| FileDialog.cake | Yes | Yes | Yes | |
| FileSystem.cake | Yes | Yes | Yes | |
| FreeType.cake | Yes | | | |
| Hash.cake | Yes | Yes | Yes | Yes |
| Image.cake | Yes | Yes | Yes | |
| ImGui.cake | Yes | No[1] | Yes | |
| ImGuiSDLOpenGL.cpp | Yes | No[1] | Yes | |

28
src/Hash.cake

@ -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…
Cancel
Save