A library for reading, writing, and representing structures from binary storage https://shinmera.com/docs/binary-structures
  • Common Lisp 100%
Find a file
2025-11-11 10:36:35 +01:00
docs Doc update 2025-08-21 11:04:30 +02:00
binary-structures.asd Update linkage 2025-08-11 21:15:47 +02:00
documentation.lisp Document the io-timestamp 2025-06-04 11:59:44 +02:00
foreign-pointer.lisp Minor 2025-06-01 11:42:17 +02:00
LICENSE Update copyright 2023-07-03 19:01:23 +02:00
octet-vector.lisp Fix SB8 referencing on octet-vectors 2025-06-04 11:46:34 +02:00
package.lisp Add io type for timestamps 2025-06-04 10:46:04 +02:00
protocol.lisp handle conc-name option in slot accesses 2025-11-11 10:36:35 +01:00
README.mess Minor 2023-04-22 17:34:09 +02:00
standard-accessors.lisp Sigh 2025-06-04 11:14:38 +02:00
standard-types.lisp Change standard io-timestamp types 2025-06-04 11:06:11 +02:00
stream.lisp Fix SB8 referencing on streams 2025-06-04 11:48:20 +02:00
test.lisp Update copyright 2023-07-03 19:01:23 +02:00
toolkit.lisp Improve return type declaration 2025-05-30 11:35:35 +02:00

# About binary-structures
This is yet another library implementing a compiler to make parsing binary files or structures easier. It generates reader and writer functions to translate from streams, octet vectors, and memory pointers directly, and has an extensible protocol to allow implementing other storage backends as well.

## How To
The primary entry point is via ``define-io-structure``. It'll define the structure type, io-type, and the parsing functions for you in one go. Here's an example that shows off most capabilities:

:: common lisp
(define-io-structure rgb
  (r uint8)
  (g uint8)
  (b uint8))

(define-io-structure rgba
  (:include rgb)
  (a uint8))

(define-io-structure image
  "IMG"
  (version uint8)
  (width uint32)
  (height uint32)
  (pixel-type (case uint8
                (1 'uint8)
                (2 'rgb)
                (3 'rgba)))
  (data (vector (case (slot pixel-type)
                  (uint8 uint8)
                  (rgb rgb)
                  (rgba rgba))
                (* (slot width) (slot height)))))
::

From there you should have a ``read-image`` and ``write-image`` function, as well as the ``image`` structure and accessors to deal with the data.

## Standard Types
The following type names are defined and available in the ``org.shirakumo.binary-structures.types`` package:

- ``uint8``
- ``uint16``
- ``uint32``
- ``uint64``
- ``uint128``
- ``sint8``
- ``sint16``
- ``sint32``
- ``sint64``
- ``sint128``
- ``float16``
- ``float32``
- ``float64``
- ``float128``
- ``uint8-be``
- ``uint16-be``
- ``uint32-be``
- ``uint64-be``
- ``uint128-be``
- ``sint8-be``
- ``sint16-be``
- ``sint32-be``
- ``sint64-be``
- ``sint128-be``
- ``float16-be``
- ``float32-be``
- ``float64-be``
- ``float128-be``
- ``utf8-string``
- ``utf16-string``
- ``utf32-string``
- ``latin1-string``
- ``(string [SIZE] [ENCODING])``
  See ``io-string``
- ``(integer [SIZE] [SIGNEDNESS] [ORDER])``
  See ``io-integer``
- ``(float [SIZE])``
  See ``io-float``
- ``(vector ELEMENT-TYPE [ELEMENT-COUNT] [ELEMENT-OFFSET])``
  See ``io-vector``
- ``(case VALUE-TYPE CASE...)``
  See ``io-case``
- ``(typecase FORM CASE...)``
  See ``io-typecase``

## Limitations
This library is best suited towards files with a clear, fixed layout that grows sequentially from the beginning. Formats like ZIP that need to be scanned from the end cannot be decoded. There's also no way to restructure data after it has been parsed, so you may need additional wrapper functions or a secondary translation step to create a truly convenient view of the data. Finally, all data must be octet-aligned.