I have a C-like struct like this:
SomeStruct << BinData::Record
endian :little
uint32 :offsetOfName
uint32 :offsetOfLastname
#...
uint32 :lenVars
struct :Person, :length => :lenVars
string :name
string :lname
#...
end
I have a bunch of offsets and lengths before :Person
. All the offsets and lengths describe the data within the :Person
struct.
How can I start reading data at a specified offset, for the given length, or until the next offset?
Seek to offset 1234 and then read 32 bytes into 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
Update: It looks like BinData understands records that encode the lengths of their own fields, but I doubt if there is any way to make it seek for you, unless you are willing to essentially encode dummy fields the size of the seeked-over space, and then ignore forever the data that it's skipping.
I suspect that a good solution will involve an explicit seek and then someBinDataObject.read(f)
to get the record.
BinData has two options related to offsets -:check_offset and:adjust_offset. These are not documented in the manual, but are documented in bindata/offset.rb
Here's how they'd work into your example.
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
Docs for BinData: http://bindata.rubyforge.org/#nested_records
Not familiar with BinData so maybe I'm off base here but the examples seem to define a class for the outermost structure:
class SomeStruct < BinData::Record
...
Then it talks about nesting anonymous structs within that class:
struct :person do
...
Also, looks like you're giving your inner struct:person a length. My guess is that length doesn't apply here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.