Skip to content

Commit

Permalink
Read chunks, identify their types and check crc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramarren committed Apr 8, 2008
1 parent a38ee9b commit 8ffd462
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion basic-chunks.lisp
Expand Up @@ -12,4 +12,32 @@
(cond
((every #'eql *png-header* header)
(read-png-chunks png-stream))
(t (error "Not PNG file.")))))
(t (error "Not PNG file.")))))

(defun big-endian-vector-to-integer (byte-vector)
(check-type byte-vector (vector (unsigned-byte 8)))
(iter (for i from (1- (length byte-vector)) downto 0)
(for j from 0)
(summing (ash (aref byte-vector j) i))))


(defun read-png-chunks (png-stream)
(let ((length-field (make-array 4 :element-type '(unsigned-byte 8)))
(type-field (make-array 4 :element-type '(unsigned-byte 8)))
(crc-field (make-array 4 :element-type '(unsigned-byte 8))))
(iter
(for read-status next (read-sequence length-field png-stream))
(for type-status next (read-sequence type-field png-stream))
(until (zerop read-status))
(assert (eql read-status 4))
(assert (eql type-status 4))
(let ((chunk-length (big-endian-vector-to-integer length-field))
(type-string (map 'string #'code-char type-field)))
(let ((chunk-data (make-array chunk-length)))
(let ((data-status (read-sequence chunk-data png-stream)))
(assert (eql data-status chunk-length))
(let ((crc-status (read-sequence crc-field png-stream)))
(assert (eql crc-status 4))
(let ((read-crc (big-endian-vector-to-integer crc-field))
(computed-crc (updated-crc (crc type-field) chunk-data)))
(collecting (list type-string chunk-data (eql read-crc computed-crc) read-crc computed-crc))))))))))

0 comments on commit 8ffd462

Please sign in to comment.