Parsing Binary Data

Parsing Binary Data

A lighting talk about parsing binary data in ruby with bindata gem.

B5b39c8f21b5bb1ab97852ed32c888ab?s=128

Jan Krutisch

June 29, 2012
Tweet

Transcript

  1. Parsing and Writing Binary Data in Ruby Jan Krutisch @halfbyte

    jan.krutisch@mindmatters.de
  2. señor developer

  3. None
  4. Protracker "mod" files

  5. Binary Format

  6. None
  7. ‣ Instrument Metadata ‣ Sample Data ‣ Pattern Data ‣

    Sequence Data Mod File Contents
  8. ‣ Instrument Metadata ‣ Sample Data ‣ Pattern Data ‣

    Sequence Data Mod File Contents Array of Structs (=Array of Hashes)
  9. ‣ Instrument Metadata ‣ Sample Data ‣ Pattern Data ‣

    Sequence Data Mod File Contents Array of Structs (=Array of Hashes) Array of Blobs
  10. ‣ Instrument Metadata ‣ Sample Data ‣ Pattern Data ‣

    Sequence Data Mod File Contents Array of Structs (=Array of Hashes) Array of Blobs Arrays of Arrays of Arrays of Structs
  11. ‣ Instrument Metadata ‣ Sample Data ‣ Pattern Data ‣

    Sequence Data Mod File Contents Array of Structs (=Array of Hashes) Array of Blobs Arrays of Arrays of Arrays of Structs Array of Integers
  12. Simple Approach

  13. Serializer / Deserializer

  14. Different Datatypes

  15. Protracker Datatypes

  16. Protracker Datatypes ‣ 8Bit signed binary blobs

  17. Protracker Datatypes ‣ 8Bit signed binary blobs ‣ UInt8

  18. Protracker Datatypes ‣ 8Bit signed binary blobs ‣ UInt8 ‣

    UInt16 (Endianness?)
  19. Protracker Datatypes ‣ 8Bit signed binary blobs ‣ UInt8 ‣

    UInt16 (Endianness?) ‣ \0-Terminated/Padded Strings
  20. Protracker Datatypes ‣ 8Bit signed binary blobs ‣ UInt8 ‣

    UInt16 (Endianness?) ‣ \0-Terminated/Padded Strings ‣ Int4 (Yes, that‘s a signed halfbyte)
  21. And this...

  22. None
  23. Procedural Mess

  24. Declarative

  25. gem "bindata"

  26. class ProtrackerModule < BinData::Record endian :big string :name, :length =>

    20, :trim_padding => true array :samples, :type => Sample, :length => 31 uint8 :pattern_table_length uint8 :unused array :pattern_table, :type => :uint8, :initial_length => 128 [...]
  27. class ProtrackerModule < BinData::Record endian :big string :name, :length =>

    20, :trim_padding => true array :samples, :type => Sample, :length => 31 uint8 :pattern_table_length uint8 :unused array :pattern_table, :type => :uint8, :initial_length => 128 [...]
  28. class ProtrackerModule < BinData::Record endian :big string :name, :length =>

    20, :trim_padding => true array :samples, :type => Sample, :length => 31 uint8 :pattern_table_length uint8 :unused array :pattern_table, :type => :uint8, :initial_length => 128 [...]
  29. class ProtrackerModule < BinData::Record endian :big string :name, :length =>

    20, :trim_padding => true array :samples, :type => Sample, :length => 31 uint8 :pattern_table_length uint8 :unused array :pattern_table, :type => :uint8, :initial_length => 128 [...]
  30. class ProtrackerModule < BinData::Record endian :big string :name, :length =>

    20, :trim_padding => true array :samples, :type => Sample, :length => 31 uint8 :pattern_table_length uint8 :unused array :pattern_table, :type => :uint8, :initial_length => 128 [...]
  31. array :patterns, :type => Pattern, :initial_length => lambda { pattern_table.max

    + 1 }
  32. None
  33. Demo

  34. señor developer http://senordevelopershop.spreadshirt.de/ (europe) http://senordeveloper.spreadshirt.com/ (intl)