How to start reading with offset in binary data?

I have a C-like structure:

SomeStruct << BinData::Record
endian :little

uint32 :offsetOfName
uint32 :offsetOfLastname
#...
uint32 :lenVars
struct :Person, :length => :lenVars
    string :name
    string :lname
    #...
end

I have many offsets and lengths up to :Person. All offsets and lengths describe the data in the structure :Person.

How can I start reading data at a given offset, for a given length or until the next offset?

+5
source share
3 answers

Look for offset 1234, and then read 32 bytes in String s:

open 'some-binary-file', 'r' do |f|
  f.seek 1234
  s = f.read 32
  # tho in your case, something like:
  o = aBinData_object.read f
  p s
end

: , BinData , , , - , , , , .

, , someBinDataObject.read(f), .

+3

BinData , : check_offset : adjust_offset. , bindata/offset.rb

.

class SomeStruct < BinData::Record
  endian :little

  uint32 :offsetOfName
  uint32 :offsetOfLastname
  #...
  uint32 :lenVars

  struct :person do
    string :name,  :adjust_offset => :offsetOfName,
                   :read_length => lambda { offsetOfLastName - offsetOfName }
    string :lname, :adjust_offset => :offsetOfLastName,
                   :read_length => ...
    #...
  end
end
+2

BinData: http://bindata.rubyforge.org/#nested_records

BinData, , , , , , :

class SomeStruct < BinData::Record
...

:

  struct :person do
  ...

In addition, it seems that you indicate your internal structure: man - length. I assume the length is not applicable here.

+1
source

All Articles