2

I'm trying to read a binary file that has chunks of variable size. The size of each chunk is located in a fix position at the beginning of each chunk.

The file is composed like this:

  • Main header = first 20 bytes of the file
  • After Main header come all the chunks.
  • Each chunk has a header of 16 bytes
  • The 4 bytes after chunk header represent the size of the chunk.

I currently have the code below that extracts main header and chunk header, size and data for first chunk, but I'm quite of novice, and I'm stuck in how to repeat this process for all the chunks.

May somebody help with my case please.

FILENAME="file.bin"

open(FILENAME, "rb") do |z|
    mainheader = z.read(20).unpack('H*')  

    puts mainheader

    puts "############### Chunk No. 1 ######################"

    chunkheader = z.read(16)                                 
    chunksize = z.read(4).unpack('H*')[0].hex
    data = z.read(chunksize).unpack('H*')

    puts chunkheader.unpack('H*')
    puts chunksize
    puts data                    
end

1 Answer 1

1

Just surround:

puts "############### Chunk No. 1 ######################"

chunkheader = z.read(16)                                 
chunksize = z.read(4).unpack('H*')[0].hex
data = z.read(chunksize).unpack('H*')

puts chunkheader.unpack('H*')
puts chunksize
puts data 

with loop:

while chunkheader = z.read(16) do
  puts "############### Chunk ######################"
  chunksize = z.read(4).unpack('H*')[0].hex
  data = z.read(chunksize).unpack('H*')

  puts chunkheader.unpack('H*')
  puts chunksize
  puts data 
end

the loop above will be terminated as there is no more data in the file remained. Please note, that the snipped above is in general error-prone, since it expects the file to be not corrupted and will fail if last chunk header reports erroneous amount of bytes.

But in your case it seems to be ok.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi mudasobwa, thanks so much for your help and answer. It looks the modification is small, but when someone don't know gets stuck like me jeje. It seems to work fine and if the file has less bytes than the last chunk size says, no error happens, only prints the number of bytes available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.