Subject: Re: hetrogenous binary streams
From: Erik Naggum <erik@naggum.net>
Date: 15 Dec 2000 02:50:09 +0000
Newsgroups: comp.lang.lisp
Message-ID: <3185837409763528@naggum.net>

* David Morse
| I have a binary stream with entries that can be ints, doubles, unsigned
| longs, chars, whatever.

  Do you know beforehand exactly what bit patterns the stream will
  contain, or do you have special encoding of the data types that lets
  you read them independent of high-level type expectations?

| I'm trying to read them from lisp, but all I see is read-byte and
| read-char, that don't do what I want at all.

  The Lisp reader is built from read-char, and it can build any object.
  You can write a reader for your own format that can build any object,
  too, using read-byte if the stream is binary.  This is not always
  trivial, but it can certainly be done.

| I think I want read-double, read-int, etc.  But there don't seem to be
| such functions.

  No, because binary formats are not defined by the language.  Such
  formats are either much more complex than textual representation or
  they are as machine-dependent as a raw memory dump, which they much
  too often actually are.  You may be able to dump data to be loaded
  back into the same build of the same program on the same machine with
  the same version compiler and same version libraries and everything
  else being the same, but otherwise, it is highly risky business to
  load binary data.  Therefore, such complexification as you find in
  ASN.1 BER and tagged data formats from .fasl (fast load) files in Lisp
  systems are necessary.

| Okay, so how do I do it?  :)

  You don't write binary data to begin with.  :)

  If you really have no choice about the data you're reading, you need
  to interpret the bit patterns yourself and build Lisp objects from
  them.  This is not as impossible as it sounds, but some Lisps have
  functions that help you do this.  E.g., Allegro CL 6.0 has functions
  that both hack up floating-point types into several shorts (16-bit
  integers) and reassemble them, for use in binary data formats.  Do
  (apropos :shorts) to find them.  You build integers from bytes the
  easy way, but you may also specify bytes that are bigger than the
  trivial 8 bits.  A Lisp "byte" is as large as you want it.  Specify
  the :element-type when you open the stream.

#:Erik
-- 
  The United States of America, soon a Bush league world power.  Yeee-haw!