Fix a few ASD dependency problems
[salza2.git] / adler32.lisp
blobffe1ad84423fde72207e50ee42a820adf3826383
1 ;;;
2 ;;; Copyright (c) 2007 Zachary Beane, All Rights Reserved
3 ;;;
4 ;;; Redistribution and use in source and binary forms, with or without
5 ;;; modification, are permitted provided that the following conditions
6 ;;; are met:
7 ;;;
8 ;;; * Redistributions of source code must retain the above copyright
9 ;;; notice, this list of conditions and the following disclaimer.
10 ;;;
11 ;;; * Redistributions in binary form must reproduce the above
12 ;;; copyright notice, this list of conditions and the following
13 ;;; disclaimer in the documentation and/or other materials
14 ;;; provided with the distribution.
15 ;;;
16 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
17 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 ;;;
29 (in-package #:salza2)
31 (defconstant +adler32-base+ 65521)
33 (defun adler32-update (adler-high adler-low buf start count)
34 (declare (type array-index start count)
35 (type (unsigned-byte 16) adler-high adler-low)
36 (type octet-vector buf)
37 (optimize speed))
38 (cond ((zerop count)
39 (values adler-high adler-low))
41 (let ((length count)
42 (i 0)
43 (k 0)
44 (s1 adler-low)
45 (s2 adler-high))
46 (declare (type (integer 0 32658) length)
47 (type (integer 0 16) i k)
48 (type (integer 0 65536) s1 s2))
49 (tagbody
50 loop
51 (setf k (min length 16))
52 (decf length k)
53 sum
54 (setf s1 (+ (aref buf (logand #xFFFF (+ start i))) s1))
55 (setf s2 (+ s1 s2))
56 (decf k)
57 (incf i)
58 (unless (zerop k)
59 (go sum))
60 (setf s1 (mod s1 +adler32-base+))
61 (setf s2 (mod s2 +adler32-base+))
62 (unless (zerop length)
63 (go loop)))
64 (values s2 s1)))))
66 ;;; Class interface
68 (defclass adler32-checksum (checksum)
69 ((high
70 :initarg :high
71 :accessor high)
72 (low
73 :initarg :low
74 :accessor low))
75 (:default-initargs
76 :high 0
77 :low 1))
79 (defmethod result ((checksum adler32-checksum))
80 (+ (ash (high checksum) 16)
81 (low checksum)))
83 (defmethod result-octets ((checksum adler32-checksum))
84 (ub32-octets (result checksum)))
86 (defmethod update ((checksum adler32-checksum) buffer start count)
87 (setf (values (high checksum)
88 (low checksum))
89 (adler32-update (high checksum)
90 (low checksum)
91 buffer
92 start
93 count)))
95 (defmethod reset ((checksum adler32-checksum))
96 (setf (high checksum) 0
97 (low checksum) 1))