RAA - bit-struct/0.13

bit-struct / 0.13

Short description: Class for packed binary data stored in ruby Strings.
Category: Library/Datastructure
Status: beta
Created: 2005-10-09 23:45:14 GMT
Last update: 2009-07-19 19:30:22 GMT
Owner: Joel VanderWerf (Projects of this owner)
Homepage: http://bit-struct.rubyforge.org/
Download: http://rubyforge.org/projects/bit-struct
License: Ruby's
Dependency:
None
Description:

Bit-struct is now a gem on rubyforge!

Use ‘gem install bit-struct’ to install.

BitStruct

Class for packed binary data stored in ruby Strings. BitStruct accessors, generated from user declared fields, use pack/unpack to treat substrings as fields with a specified portable format.

Field types include

  • signed and unsigned integer (1..16 bits, or 24, 32, 40, 48… bits)
  • numeric fields (signed, unsigned, float) can be designated as any of the following endians: little, big, native, network (default)
  • fixed point, with arbitrary scale factor
  • fixed length character array
  • null-terminated character array for printable text
  • octets (hex and decimal representation options; useful for IP and MAC addrs)
  • float
  • nested BitStruct
  • free-form "rest" field (e.g., for the variable-size payload of a packet)

Installation

  ruby install.rb config
  ruby install.rb setup
  ruby install.rb install

Uses

BitStruct is useful for defining packets used in network protocols. This is especially useful for raw IP—see examples/ping-recv.rb. All multibyte numeric fields are stored in network order (though this could be different for user-defined field classes).

BitStruct is most efficient when your data is primarily treated as a binary string, and only secondarily treated as a data structure. (For instance, you are routing packets from one socket to another, possibly looking at one or two fields as it passes through or munging some headers.) If accessor operations are a bottleneck, a better approach is to define a class that wraps an array and uses pack/unpack when the object needs to behave like a binary string.

Recent Changes:

bit-struct 0.10

  • Fixed a bug when calling #to_yaml on a BitStruct with a pad field. Thanks to Jay Reitz for reporting it and providing the solution.
  • Added BitStruct.default_options. Particularly useful for default endian settings—see examples/native.rb.
  • Fixed a bug that prevented warning about field name conflicts with existing methods when the name is given as a symbol.
  • Fixed point fields may now have fractional divisors (like :fixed => 0.001).

bit-struct 0.9

  • Added examples/field-ripper.rb.
  • Added BitStruct#field_by_name.
  • Added more warnings about nested field accessors returning a copy of the nested data.
  • Added "pad" fields. See documentation in pad-field.rb and examples/pad.rb.
  • The #initial_value method now yields the value to a block, if given.
  • A BitStruct class is only closed when an instance is created. (After this point no fields can be added.) Formerly, a class was closed when any method requested the total length of the structure. This change makes it easier to add groups of fields modularly, each with their own initial_value block.
  • Added examples/modular-def.rb to explain how to factor a BitStruct definition into modules.
Versions: [0.13 (2009-07-19)] [0.12 (2009-07-19)] [0.10 (2007-01-15)] [0.9 (2006-09-20)] [0.8 (2006-05-27)] [0.7 (2006-05-21)] [0.6 (2006-05-01)] [0.5 (2006-04-01)] [0.3 (2006-01-17)] [0.2 (2005-10-09)]

Edit this project (for project owner)

back to RAA top