- collect
- `(setq ,a (mod32+ ,b (rol32 (mod32+ (mod32+ ,a (,op ,b ,c ,d))
- (mod32+ (aref ,block ,k)
- ,(aref *t* (1- i))))
- ,s)))
- into result
- finally
- (return `(progn ,@result))))
+ collect
+ `(setq ,a (mod32+ ,b (rol32 (mod32+ (mod32+ ,a (,op ,b ,c ,d))
+ (mod32+ (aref ,block ,k)
+ ,(aref *t* (1- i))))
+ ,s)))
+ into result
+ finally
+ (return `(progn ,@result))))
#+(and :cmu :little-endian)
(kernel:bit-bash-copy
buffer (+ (* vm:vector-data-offset vm:word-bits) (* offset vm:byte-bits))
block (* vm:vector-data-offset vm:word-bits)
(* 64 vm:byte-bits))
#+(and :sbcl :little-endian)
#+(and :cmu :little-endian)
(kernel:bit-bash-copy
buffer (+ (* vm:vector-data-offset vm:word-bits) (* offset vm:byte-bits))
block (* vm:vector-data-offset vm:word-bits)
(* 64 vm:byte-bits))
#+(and :sbcl :little-endian)
(defun fill-block-char (block buffer offset)
"Convert a complete 64 character input string segment starting from
offset into the given 16 word MD5 block."
(declare (type (integer 0 #.(- most-positive-fixnum 64)) offset)
(defun fill-block-char (block buffer offset)
"Convert a complete 64 character input string segment starting from
offset into the given 16 word MD5 block."
(declare (type (integer 0 #.(- most-positive-fixnum 64)) offset)
#+(and :cmu :little-endian)
(kernel:bit-bash-copy
buffer (+ (* vm:vector-data-offset vm:word-bits) (* offset vm:byte-bits))
block (* vm:vector-data-offset vm:word-bits)
(* 64 vm:byte-bits))
#+(and :sbcl :little-endian)
#+(and :cmu :little-endian)
(kernel:bit-bash-copy
buffer (+ (* vm:vector-data-offset vm:word-bits) (* offset vm:byte-bits))
block (* vm:vector-data-offset vm:word-bits)
(* 64 vm:byte-bits))
#+(and :sbcl :little-endian)
- for j of-type (integer 0 #.most-positive-fixnum)
- from offset to (+ offset 63) by 4
- do
- (setf (aref block i)
- (assemble-ub32 (char-code (schar buffer j))
- (char-code (schar buffer (+ j 1)))
- (char-code (schar buffer (+ j 2)))
- (char-code (schar buffer (+ j 3)))))))
+ for j of-type (integer 0 #.most-positive-fixnum)
+ from offset to (+ offset 63) by 4
+ do
+ (setf (aref block i)
+ (assemble-ub32 (char-code (schar buffer j))
+ (char-code (schar buffer (+ j 1)))
+ (char-code (schar buffer (+ j 2)))
+ (char-code (schar buffer (+ j 3)))))))
- (let ((var (gensym)))
- `(let ((,var ,reg))
- (declare (type ub32 ,var))
- (setf
- (aref result ,offset) (ldb (byte 8 0) ,var)
- (aref result ,(+ offset 1)) (ldb (byte 8 8) ,var)
- (aref result ,(+ offset 2)) (ldb (byte 8 16) ,var)
- (aref result ,(+ offset 3)) (ldb (byte 8 24) ,var))))))
+ (let ((var (gensym)))
+ `(let ((,var ,reg))
+ (declare (type ub32 ,var))
+ (setf
+ (aref result ,offset) (ldb (byte 8 0) ,var)
+ (aref result ,(+ offset 1)) (ldb (byte 8 8) ,var)
+ (aref result ,(+ offset 2)) (ldb (byte 8 16) ,var)
+ (aref result ,(+ offset 3)) (ldb (byte 8 24) ,var))))))
- (type (unsigned-byte 29) from-offset)
- (type (integer 0 63) count buffer-offset)
- (type (simple-array * (*)) from)
- (type (simple-array (unsigned-byte 8) (64)) buffer))
+ (type (unsigned-byte 29) from-offset)
+ (type (integer 0 63) count buffer-offset)
+ (type (simple-array * (*)) from)
+ (type (simple-array (unsigned-byte 8) (64)) buffer))
- for from-index of-type fixnum from from-offset
- below (+ from-offset count)
- do
- (setf (aref buffer buffer-index)
- (char-code (schar (the simple-string from) from-index)))))
+ for from-index of-type fixnum from from-offset
+ below (+ from-offset count)
+ do
+ (setf (aref buffer buffer-index)
+ (char-code (schar (the simple-string from) from-index)))))
- for from-index of-type fixnum from from-offset
- below (+ from-offset count)
- do
- (setf (aref buffer buffer-index)
- (aref (the (simple-array (unsigned-byte 8) (*)) from)
- from-index))))))
+ for from-index of-type fixnum from from-offset
+ below (+ from-offset count)
+ do
+ (setf (aref buffer buffer-index)
+ (aref (the (simple-array (unsigned-byte 8) (*)) from)
+ from-index))))))
(defun update-md5-state (state sequence &key (start 0) (end (length sequence)))
"Update the given md5-state from sequence, which is either a
simple-string or a simple-array with element-type (unsigned-byte 8),
bounded by start and end, which must be numeric bounding-indices."
(declare (type md5-state state)
(defun update-md5-state (state sequence &key (start 0) (end (length sequence)))
"Update the given md5-state from sequence, which is either a
simple-string or a simple-array with element-type (unsigned-byte 8),
bounded by start and end, which must be numeric bounding-indices."
(declare (type md5-state state)
- (type (simple-array * (*)) sequence)
- (type fixnum start end)
- (optimize (speed 3) #+(or cmu sbcl) (safety 0) (space 0) (debug 0))
- #+cmu
- (ext:optimize-interface (safety 1) (debug 1)))
+ (type (simple-array * (*)) sequence)
+ (type fixnum start end)
+ (optimize (speed 3) #+(or cmu sbcl) (safety 0) (space 0) (debug 0))
+ #+cmu
+ (ext:optimize-interface (safety 1) (debug 1)))
- (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))
+ (declare (type (integer 0 63) amount))
+ (copy-to-buffer sequence start amount buffer buffer-index)
+ (setq start (the fixnum (+ start amount)))
+ (let ((new-index (mod (+ buffer-index amount) 64)))
+ (when (zerop new-index)
+ (fill-block-ub8 block buffer 0)
+ (update-md5-block regs block))
+ (when (>= start end)
+ (setf (md5-state-buffer-index state) new-index)
+ (incf (md5-state-amount state) length)
+ (return-from update-md5-state state)))))
- (declare (type (simple-array (unsigned-byte 8) (*)) sequence))
- (loop for offset of-type (unsigned-byte 29) from start below end by 64
- until (< (- end offset) 64)
- do
- (fill-block-ub8 block sequence offset)
- (update-md5-block regs block)
- finally
- (let ((amount (- end offset)))
- (unless (zerop amount)
- (copy-to-buffer sequence offset amount buffer 0))
- (setf (md5-state-buffer-index state) amount)))))
+ (declare (type (simple-array (unsigned-byte 8) (*)) sequence))
+ (loop for offset of-type (unsigned-byte 29) from start below end by 64
+ until (< (- end offset) 64)
+ do
+ (fill-block-ub8 block sequence offset)
+ (update-md5-block regs block)
+ finally
+ (let ((amount (- end offset)))
+ (unless (zerop amount)
+ (copy-to-buffer sequence offset amount buffer 0))
+ (setf (md5-state-buffer-index state) amount)))))
- (declare (type simple-string sequence))
- (loop for offset of-type (unsigned-byte 29) from start below end by 64
- until (< (- end offset) 64)
- do
- (fill-block-char block sequence offset)
- (update-md5-block regs block)
- finally
- (let ((amount (- end offset)))
- (unless (zerop amount)
- (copy-to-buffer sequence offset amount buffer 0))
- (setf (md5-state-buffer-index state) amount))))))
+ (declare (type simple-string sequence))
+ (loop for offset of-type (unsigned-byte 29) from start below end by 64
+ until (< (- end offset) 64)
+ do
+ (fill-block-char block sequence offset)
+ (update-md5-block regs block)
+ finally
+ (let ((amount (- end offset)))
+ (unless (zerop amount)
+ (copy-to-buffer sequence offset amount buffer 0))
+ (setf (md5-state-buffer-index state) amount))))))
- (block (md5-state-block state))
- (buffer (md5-state-buffer state))
- (buffer-index (md5-state-buffer-index state))
- (total-length (* 8 (md5-state-amount state))))
- (declare (type md5-regs regs)
- (type (integer 0 63) buffer-index)
- (type (simple-array ub32 (16)) block)
- (type (simple-array (unsigned-byte 8) (*)) buffer))
- ;; Add mandatory bit 1 padding
- (setf (aref buffer buffer-index) #x80)
- ;; Fill with 0 bit padding
- (loop for index of-type (integer 0 64)
- from (1+ buffer-index) below 64
- do (setf (aref buffer index) #x00))
- (fill-block-ub8 block buffer 0)
- ;; Flush block first if length wouldn't fit
- (when (>= buffer-index 56)
- (update-md5-block regs block)
- ;; Create new fully 0 padded block
- (loop for index of-type (integer 0 16) from 0 below 16
- do (setf (aref block index) #x00000000)))
- ;; Add 64bit message bit length
- (setf (aref block 14) (ldb (byte 32 0) total-length))
- #-md5-small-length
- (setf (aref block 15) (ldb (byte 32 32) total-length))
- ;; Flush last block
- (update-md5-block regs block)
- ;; Done, remember digest for later calls
- (setf (md5-state-finalized-p state)
- (md5regs-digest regs)))))
+ (block (md5-state-block state))
+ (buffer (md5-state-buffer state))
+ (buffer-index (md5-state-buffer-index state))
+ (total-length (* 8 (md5-state-amount state))))
+ (declare (type md5-regs regs)
+ (type (integer 0 63) buffer-index)
+ (type (simple-array ub32 (16)) block)
+ (type (simple-array (unsigned-byte 8) (*)) buffer))
+ ;; Add mandatory bit 1 padding
+ (setf (aref buffer buffer-index) #x80)
+ ;; Fill with 0 bit padding
+ (loop for index of-type (integer 0 64)
+ from (1+ buffer-index) below 64
+ do (setf (aref buffer index) #x00))
+ (fill-block-ub8 block buffer 0)
+ ;; Flush block first if length wouldn't fit
+ (when (>= buffer-index 56)
+ (update-md5-block regs block)
+ ;; Create new fully 0 padded block
+ (loop for index of-type (integer 0 16) from 0 below 16
+ do (setf (aref block index) #x00000000)))
+ ;; Add 64bit message bit length
+ (setf (aref block 14) (ldb (byte 32 0) total-length))
+ #-md5-small-length
+ (setf (aref block 15) (ldb (byte 32 32) total-length))
+ ;; Flush last block
+ (update-md5-block regs block)
+ ;; Done, remember digest for later calls
+ (setf (md5-state-finalized-p state)
+ (md5regs-digest regs)))))
- (declare (optimize (speed 3) (space 0) (debug 0))
- (type vector sequence) (type fixnum start))
- (let ((state (make-md5-state)))
- (declare (type md5-state state))
- #+cmu
- (lisp::with-array-data ((data sequence) (real-start start) (real-end end))
- (update-md5-state state data :start real-start :end real-end))
- #+sbcl
- (sb-kernel:with-array-data ((data sequence) (real-start start) (real-end end))
- (update-md5-state state data :start real-start :end real-end))
- #-(or cmu sbcl)
- (let ((real-end (or end (length sequence))))
- (declare (type fixnum real-end))
- (update-md5-state state sequence :start start :end real-end))
- (finalize-md5-state state)))
+ (declare (optimize (speed 3) (safety 3) (space 0) (debug 1))
+ (type (vector (unsigned-byte 8)) sequence) (type fixnum start))
+ (locally
+ (declare (optimize (safety 1) (debug 0)))
+ (let ((state (make-md5-state)))
+ (declare (type md5-state state))
+ #+cmu
+ ;; respect the fill pointer
+ (let ((end (or end (length sequence))))
+ (lisp::with-array-data ((data sequence) (real-start start) (real-end end))
+ (declare (ignore real-end))
+ (update-md5-state state data :start real-start
+ :end (+ real-start (- end start)))))
+ #+sbcl
+ ;; respect the fill pointer
+ (let ((end (or end (length sequence))))
+ (sb-kernel:with-array-data ((data sequence)
+ (real-start start)
+ (real-end end)
+ :check-fill-pointer t)
+ (declare (ignore real-end))
+ (update-md5-state state data :start real-start
+ :end (+ real-start (- end start)))))
+ #-(or cmu sbcl)
+ (let ((real-end (or end (length sequence))))
+ (declare (type fixnum real-end))
+ (update-md5-state state sequence :start start :end real-end))
+ (finalize-md5-state state))))
- (declare (optimize (speed 3) (space 0) (debug 0))
- (type string string))
- (md5sum-sequence
- (sb-ext:string-to-octets string
- :external-format external-format
- :start start :end end)))
+ "Calculate the MD5 message-digest of the binary representation
+of STRING (as octets) in EXTERNAL-FORMAT. The boundaries START
+and END refer to character positions in the string, not to octets
+in the resulting binary representation."
+ (declare (optimize (speed 3) (safety 3) (space 0) (debug 1))
+ (type string string) (type fixnum start))
+ (locally
+ (declare (optimize (safety 1) (debug 0)))
+ (md5sum-sequence
+ (sb-ext:string-to-octets string
+ :external-format external-format
+ :start start :end end))))
- (declare (optimize (speed 3) (space 0) (debug 0)))
- (let ((state (make-md5-state)))
- (declare (type md5-state state))
- (cond
- ((equal (stream-element-type stream) '(unsigned-byte 8))
- (let ((buffer (make-array +buffer-size+
- :element-type '(unsigned-byte 8))))
- (declare (type (simple-array (unsigned-byte 8) (#.+buffer-size+))
- buffer))
- (loop for bytes of-type buffer-index = (read-sequence buffer stream)
- do (update-md5-state state buffer :end bytes)
- until (< bytes +buffer-size+)
- finally
- (return (finalize-md5-state state)))))
- ((equal (stream-element-type stream) 'character)
- (let ((buffer (make-string +buffer-size+)))
- (declare (type (simple-string #.+buffer-size+) buffer))
- (loop for bytes of-type buffer-index = (read-sequence buffer stream)
- do (update-md5-state state buffer :end bytes)
- until (< bytes +buffer-size+)
- finally
- (return (finalize-md5-state state)))))
- (t
- (error "Unsupported stream element-type ~S for stream ~S."
- (stream-element-type stream) stream)))))
+ (declare (optimize (speed 3) (safety 3) (space 0) (debug 1)))
+ (declare (type stream stream))
+ (locally
+ (declare (optimize (safety 1) (debug 0)))
+ (let ((state (make-md5-state)))
+ (declare (type md5-state state))
+ (cond
+ ((equal (stream-element-type stream) '(unsigned-byte 8))
+ (let ((buffer (make-array +buffer-size+
+ :element-type '(unsigned-byte 8))))
+ (declare (type (simple-array (unsigned-byte 8) (#.+buffer-size+))
+ buffer))
+ (loop for bytes of-type buffer-index = (read-sequence buffer stream)
+ do (update-md5-state state buffer :end bytes)
+ until (< bytes +buffer-size+)
+ finally
+ (return (finalize-md5-state state)))))
+ #+(or)
+ ((equal (stream-element-type stream) 'character)
+ (let ((buffer (make-string +buffer-size+)))
+ (declare (type (simple-string #.+buffer-size+) buffer))
+ (loop for bytes of-type buffer-index = (read-sequence buffer stream)
+ do (update-md5-state state buffer :end bytes)
+ until (< bytes +buffer-size+)
+ finally
+ (return (finalize-md5-state state)))))
+ (t
+ (error "Unsupported stream element-type ~S for stream ~S."
+ (stream-element-type stream) stream))))))
- "Calculate the MD5 message-digest of the file specified by pathname."
- (declare (optimize (speed 3) (space 0) (debug 0)))
- (with-open-file (stream pathname :element-type '(unsigned-byte 8))
- (md5sum-stream stream)))
+ "Calculate the MD5 message-digest of the file designated by
+pathname."
+ (declare (optimize (speed 3) (safety 3) (space 0) (debug 1)))
+ (locally
+ (declare (optimize (safety 1) (debug 0)))
+ (with-open-file (stream pathname :element-type '(unsigned-byte 8))
+ (md5sum-stream stream))))