= binstruct

http://metafuzz.rubyforge.org/binstruct

== DESCRIPTION:

BinStruct is a small class for defining structures that can be used to parse /
manipulate binary data. It is similar in concept to BitStruct, but supports
some things that are difficult to do with a pure pack / unpack approach. It is
mainly designed to work with the Metafuzz fuzzing tools, but would possibly be
useful to anyone working with binary data.

== FEATURES/PROBLEMS:

* Parsing is done 'as you go' which means you can refer to previous fields or
values when making parser decisions (variable length fields etc)
* Supports byte-swapped multi-byte bitfields for little endian file formats and
substructs for nested parsing
* Full Ruby inside the class definition, with a little bit of syntactic sugar
for field type definitions
* All field data remains in the original binary internally, but get / setters
are available for signed, unsigned, string, octets, hexstring and bitstring
and can be easily extended.
* Key instance methods are get / setters for the fields (defined when the
subclass is constructed), directly access the raw field object with #[:afield]
#each {|item| (item may be a substruct), #deep_each {|field| (for nested
structs), #flatten and of course #to_s to re-pack as a string.
* Because this class was designed for use with a fuzzer it does not do very
stringent checking of alignment, length matching etc, and raw contents are
available where possible. This is either a bug or a feature, depending on
your point of view.

== SYNOPSIS:

This example is from some Word binary file work...

class WordDgg < BinStruct
parse{ |bitbuf|
endian :little
# These two bytes will be swapped before they are carved up, and
# swapped back whenever to_s is called. The bitfield itself cannot
# be directly accessed, just the fields inside.
bitfield(bitbuf, 16) do |buf|
unsigned buf, :recInstance, 12, "Object Identifier"
unsigned buf, :recVer, 4, "Object version, 0xF for container"
end
# Normal field definitions take effect immediately, and consume
# the portion of the bit buffer they used.
unsigned bitbuf, :recType, 16, "RecType"
unsigned bitbuf, :recLen, 32, "Content length"
# Context sensitive parsing...
if self.recVer==0xF
substruct_name=:substruct0
# Manually manipulate the buffer parameter, if you like.
mychunk=[bitbuf.slice!(0,self.recLen*8)].pack('B*')
while mychunk.length > 0
# Nested parsing, here...
substruct(mychunk, substruct_name, mychunk.length, WordDgg)
substruct_name=substruct_name.succ
end
else
string bitbuf, :contents, self.recLen*8, "Contents"
end
# groups link sets of fields, using a given name. The groups are used
# by Metafuzz to create cartestian product output of the fuzz items
# for all fields in the group.
if self[:contents]
group :tv, :recType, :contents
else
group :tl, :recType, :recLen
end
}
end

== REQUIREMENTS:

None.

== INSTALL:

Just install the gem.

== LICENSE:

(The MIT License)

Copyright (c) 2006 Ben Nagy

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.