Sep 13
A Better Binary File Generator DSL in Ruby
In Creating a Binary File Using a Ruby DSL I did a very small example of using a ruby DSL to generate complex binary files without having to use C or a hexeditor. I’ve beefed it up significantly since then so here is the updated version of h2b.irb.
Now I can write something like this:
#write ascii header ascii "File header V1.0" byte 0 #stream data data = block{ hex "01 02 03 04 05 06 07 08 09 0A" hex "AA BB CC DD EE FF AB AC AD AE" } frame_data = block{ #create frames each_chunk(data, 2){ |chunk| hex "HEADER" binary chunk } } #shift right by 3 bits #and fill to byte boundaries #with 0's shift_right(3, 0){ #original binary(frame_data) #reversed/inverted version reverse_bits{ invert{ binary(frame_data) } } }
And get this with hexdump
00000000 46 69 6c 65 20 68 65 61 64 65 72 20 56 31 2e 30 |File header V1.0| 00000010 00 03 d5 bd 60 20 43 d5 bd 60 60 83 d5 bd 60 a0 |....` C..``...`.| 00000020 c3 d5 bd 60 e1 03 d5 bd 61 21 43 d5 bd 75 57 63 |...`....a!C..uWc| 00000030 d5 bd 79 9b a3 d5 bd 7d df e3 d5 bd 75 75 83 d5 |..y....}....uu..| 00000040 bd 75 b5 d1 49 45 09 50 f9 45 45 09 50 e0 11 05 |.u..IE.P.EE.P...| 00000050 09 50 e8 99 85 09 50 e4 55 45 09 50 f5 ed e5 09 |.P....P.UE.P....| 00000060 50 fd e3 e5 09 50 f3 eb e5 09 50 fb e7 e5 09 50 |P....P....P....P| 00000070 f7 ef e5 09 50 e0 |....P.|
Michael Smit is a software engineer in Maryland who currently develops software for spacecraft, spacecraft simulators, and spacecraft control systems.The Author
No Comments
Leave a comment