data_unpack.rb (739B)
1 #! /usr/bin/env ruby 2 3 require 'bindata' 4 5 class GarbageString < BinData::Primitive 6 mandatory_parameter :length 7 string :data, length: :length 8 9 def get; data.gsub(/\0.*/, ''); end 10 def set v; self.data = v; end 11 end 12 13 class Header < BinData::Record 14 endian :little 15 garbage_string :hdr, length: 17 16 array :files, read_until: :eof do 17 garbage_string :name, length: 16 18 uint32 :offs 19 end 20 end 21 22 fail "Usage: #{$0} HED DAT out_dir" unless ARGV.size == 3 23 24 h = File.open(ARGV[0], 'r') { |f| Header.read f } 25 fail unless h.files[0].offs == 0 26 fail unless h.files[-1].name == 'end' 27 28 File.open ARGV[1], 'r' do |data| 29 h.files.each_cons 2 do |t,n| 30 out = File.join ARGV[2], t.name 31 File.write out, data.read(n.offs-t.offs) 32 end 33 end