Skip to content
Binary data library based on code from chapter 24 of Practical Common Lisp
Branch: master
Clone or download
Pull request Compare This branch is 43 commits ahead of gigamonkey:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
COPYING
Makefile
README.org
binary-data.lisp
binary-io.asd
common-datatypes.lisp
packages.lisp
tests.lisp

README.org

binary-io

This library should be a drop in replacement for monkeylib-binary-data described in Peter Seibel’s excellent book: you should start here.

It also contains the following optionals enhancements.

definition/access of octet size of objects

When defining a new binary-type, in addition to :reader and :writer definition, you can set a :size to calculate the octet size of this new type. You can then access this size with type-size

Here is an example from common-datatypes.lisp (that comes with binary-io ;-)

;;; Unsigned integers
(define-binary-type unsigned-integer (bits)
  (:reader (fd)
	   (assert (equal (stream-element-type fd) '(unsigned-byte 8)))
	   (let ((byte-indexes (byte-indexes bits *endianness*))
		 (value 0))
	     (dolist (i byte-indexes value)
	       (setf (ldb (byte 8 i) value) (read-byte fd)))))
  (:writer (fd value)
	   (assert (equal (stream-element-type fd) '(unsigned-byte 8)))
	   (let ((byte-indexes (byte-indexes bits *endianness*)))
	     (dolist (i byte-indexes)
	       (write-byte (ldb (byte 8 i) value) fd))))
  (:size () (ceiling bits 8)))

(define-binary-type u2 () (unsigned-integer :bits 16))

(type-size 'u2) ;; -> 2

type-size method also works for binary class.

(define-binary-class test-size ()
  ((a u2)
   (b u2)))

(type-size 'test-size) ;; -> 4

optional initform for slots

You can precise an optional initform for a slot as a third value in the slot spec of a binary class definition:

(define-binary-class foo-header ()
  ((tag (8bit-string :length 4 :terminator #\Nul) "FOO")
   (counter u2 0)))

(tag (make-instance 'foo-header)) ;; -> "FOO"
You can’t perform that action at this time.