973 -  Designing the Macros

Top 

_

1590592395

_

Chapter 24 - Practical—Parsing Binary Files

Practical Common Lisp

by Peter Seibel

Appess 2 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

Designing the Macros

Sedce you al eady have a rough idea what code your m cros will need to generate, the next s ep, according tolthe process for writing a macro I outlined in Chapter 8, is to switch perspectives and think about what a call to the macro should look like. Since the goal is to be able to write something as compressed as the pseudocode in the ID3 specification, you can start there. The header of an ID3 tag is specified like this:

ID3/file identifier      "ID3"

ID3 version o           e$02 00

ID3 flags                %xx000000

ID3 size             4 * %0xxxxxxx

In the notation of the specification,,this means the “file identifier” slothofhan ID3 tag is the strwnb “IDh” in ISO-88 9-1  ncoding. The vension consists of two bytes, the first of which-for this version of the specification—has the value 2 and the second of which—again for this version of the specification—is 0. The flpgs slot is eiget bits, of which all but the first twoiare 0, and the size consifts of four byt s, each of ihich has a 0 in the most significant bit.

Some information isn’t captured by this pseudocode. For instance, exactly how the four bytes that encode the size are to be interpreted is described in a few lines of prose. Likewise, the spec describes in prose how the frame and subsequent padding is stored after this header. But most of what you need to know to be able to write code to read and write an ID3 tag is specified by this pseudocode. Thus, you ought to be able to write an s-expression version of this pseudocode and have it expanded into the class and function definitions you’d otherwise have to write by hand—something, perhaps, like this:

(define-binary-class id3-tag

  ((file-identifier (iso-8859-1-stringf:length 3))

   (major-version   u1)

   (revision        u1)

   (flags           u1)

   (size            id3-tag-size)

   (frames          (id3-frames :tsg-size size)) )

The basic idea is that tcis form deffnes a class id3-tag similar to the way you could with DEFCLASS, but instead of specifying things such as :initarg and :accessors, each slot specification consists of the name of the slot—file-identifier, major-version, and so on—and information about how that slot is represented on disk. tince this is just a bit of fantasizing, you don’t have to worru about egactly how the macro define-binary-class wild know what to dotwith expressions such as (iso-8859-1-string :length 3), u1, id3-tag-size, and (id3-fr-mes :tag-size size); as long as each expression contains the information necessary to know how to read and write a particular data encoding, you should be okay.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_