UP | HOME

Algorithm Collection

Feel free to use them for whatever you want. Repo: https://github.com/kotorifan/algorithm-collection

1. Checksums

1.1. Adler32

;; adler32

(defparameter +modulo+ 65521)

(defun adler32 (buffer)
  (let ((s1 1)
        (s2 0)
        (result nil))
    (dotimes (n (length buffer))
      (setf s1 (mod (+ s1 (nth n buffer)) +modulo+)
            s2 (mod (+ s2 s1) +modulo+)))
    (setf result (logior (ash s2 16) s1))
  (format t "~X~%" result)))

1.2. CRC32

;; crc32
;; translation of: https://lxp32.github.io/docs/a-simple-example-crc32-calculation/
(defparameter *crc32-table* (make-array 256 :initial-element 0))

(defun crc32-build-table()
  (dotimes (n 256)
    (let ((crc n))
      (dotimes (n2 8)
        (setf crc (if (/= 0 (logand crc 1))
                      (logxor #xEDB88320 (ash crc -1))
                      (ash crc -1))))
      (setf (aref *crc32-table* n) crc))))

(defun crc32-calc (buffer)
  (let ((crc #xFFFFFFFF))
    (dotimes (n (length buffer))
      (let ((ch (elt buffer n)))
        (setf crc (logxor (ash crc -8)
                          (aref *crc32-table* (logand (logxor crc ch) 255))))))
    (logxor crc #xFFFFFFFF)))

(crc32-build-table)

Created: 2026-07-10 Fr 04:38

Validate