;;;; cmucl-help mailing-list hosted at cons.org, in November 2001 and
;;;; has been placed into the public domain.
;;;;
-;;;; $Id: md5.lisp,v 1.4 2003/05/06 04:57:43 kevin Exp $
+;;;; $Id$
;;;;
;;;; While the implementation should work on all conforming Common
;;;; Lisp implementations, it has only been optimized for CMU CL,
(declare (type md5-regs regs)
(type (simple-array ub32 (16)) block)
(optimize (speed 3) (safety 0) (space 0) (debug 0)))
- (let ((a (md5-regs-a regs)) (b (md5-regs-b regs))
- (c (md5-regs-c regs)) (d (md5-regs-d regs)))
- (declare (type ub32 a b c d))
+ (let ((A (md5-regs-a regs)) (B (md5-regs-b regs))
+ (C (md5-regs-c regs)) (D (md5-regs-d regs)))
+ (declare (type ub32 A B C D))
;; Round 1
(with-md5-round (f block)
(A B C D 0 7 1)(D A B C 1 12 2)(C D A B 2 17 3)(B C D A 3 22 4)
(A B C D 8 6 57)(D A B C 15 10 58)(C D A B 6 15 59)(B C D A 13 21 60)
(A B C D 4 6 61)(D A B C 11 10 62)(C D A B 2 15 63)(B C D A 9 21 64))
;; Update and return
- (setf (md5-regs-a regs) (mod32+ (md5-regs-a regs) a)
- (md5-regs-b regs) (mod32+ (md5-regs-b regs) b)
- (md5-regs-c regs) (mod32+ (md5-regs-c regs) c)
- (md5-regs-d regs) (mod32+ (md5-regs-d regs) d))
+ (setf (md5-regs-a regs) (mod32+ (md5-regs-a regs) A)
+ (md5-regs-b regs) (mod32+ (md5-regs-b regs) B)
+ (md5-regs-c regs) (mod32+ (md5-regs-c regs) C)
+ (md5-regs-d regs) (mod32+ (md5-regs-d regs) D))
regs))
;;; Section 3.4: Converting 8bit-vectors into 16-Word Blocks
(ext:optimize-interface (safety 1) (debug 1)))
(let ((regs (md5-state-regs state))
(block (md5-state-block state))
- (buffer (md5-state-buffer state))
- (buffer-index (md5-state-buffer-index state))
- (length (- end start)))
- (declare (type md5-regs regs) (type fixnum length)
- (type (integer 0 63) buffer-index)
+ (buffer (md5-state-buffer state)))
+ (declare (type md5-regs regs)
(type (simple-array (unsigned-byte 32) (16)) block)
(type (simple-array (unsigned-byte 8) (64)) buffer))
;; Handle old rest
- (unless (zerop buffer-index)
- (let ((amount (min (- 64 buffer-index) length)))
- (declare (type (integer 0 63) amount))
- (copy-to-buffer sequence start amount buffer buffer-index)
- (setq start (the fixnum (+ start amount)))
- (when (>= start end)
- (setf (md5-state-buffer-index state) (+ buffer-index amount))
- (return-from update-md5-state state)))
- (fill-block-ub8 block buffer 0)
- (update-md5-block regs block))
+ (unless (zerop (md5-state-buffer-index state))
+ (let* ((buffer-index (md5-state-buffer-index state))
+ (remainder (- 64 buffer-index))
+ (length (- end start))
+ (amount (min remainder length)))
+ (declare (type (integer 0 63) buffer-index remainder amount)
+ (type fixnum length))
+ (copy-to-buffer sequence start amount buffer buffer-index)
+ (setf (md5-state-amount state)
+ #-md5-small-length (+ (md5-state-amount state) amount)
+ #+md5-small-length (the (unsigned-byte 29)
+ (+ (md5-state-amount state) amount)))
+ (setq start (the fixnum (+ start amount)))
+ (if (< length remainder)
+ (setf (md5-state-buffer-index state)
+ (the (integer 0 63) (+ buffer-index amount)))
+ (progn
+ (fill-block-ub8 block buffer 0)
+ (update-md5-block regs block)
+ (setf (md5-state-buffer-index state) 0)))))
+ ;; Leave when nothing to do
+ (when (>= start end)
+ (return-from update-md5-state state))
;; Handle main-part and new-rest
(etypecase sequence
((simple-array (unsigned-byte 8) (*))
(copy-to-buffer sequence offset amount buffer 0))
(setf (md5-state-buffer-index state) amount))))))
(setf (md5-state-amount state)
- #-md5-small-length (+ (md5-state-amount state) length)
+ #-md5-small-length (+ (md5-state-amount state)
+ (the fixnum (- end start)))
#+md5-small-length (the (unsigned-byte 29)
- (+ (md5-state-amount state) length)))
+ (+ (md5-state-amount state)
+ (the fixnum (- end start)))))
state))
(defun finalize-md5-state (state)
(update-md5-state state sequence :start start :end real-end))
(finalize-md5-state state)))
-(defconstant +buffer-size+ (* 128 1024)
- "Size of internal buffer to use for md5sum-stream and md5sum-file
-operations. This should be a multiple of 64, the MD5 block size.")
+(eval-when (:compile-toplevel :load-toplevel :execute)
+ (defconstant +buffer-size+ (* 128 1024)
+ "Size of internal buffer to use for md5sum-stream and md5sum-file
+operations. This should be a multiple of 64, the MD5 block size."))
(deftype buffer-index () `(integer 0 ,+buffer-size+))