Coder Social home page Coder Social logo

mgi / binary-io Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gigamonkey/monkeylib-binary-data

2.0 2.0 0.0 71 KB

Binary data library based on code from chapter 24 of Practical Common Lisp

Home Page: http://gigamonkeys.com/book/practical-parsing-binary-files.html

License: BSD 3-Clause "New" or "Revised" License

Common Lisp 99.70% Makefile 0.30%

binary-io's Introduction

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"

a word of warning

binary-io is useful if you have some “complex” data structures that will be easily mapped by some define-binary-class. But if you have binary data that are mostly arrays of the same type, you’d better use read-sequence directly (with the correct element-type on the stream).

I have made some measurement on SBCL and a (binary-io:read-value :vector) is about 3 times slower than an equivalent (read-sequence).

binary-io's People

Contributors

gigamonkey avatar mgi avatar slyrus avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.