bindata 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bindata might be problematic. Click here for more details.

data/manual.md ADDED
@@ -0,0 +1,1187 @@
1
+ Title: BinData Reference Manual
2
+
3
+ {:ruby: lang=ruby html_use_syntax=true}
4
+
5
+ # BinData
6
+
7
+ A declarative way to read and write structured binary data.
8
+
9
+ ## What is it for?
10
+
11
+ Do you ever find yourself writing code like this?
12
+
13
+ io = File.open(...)
14
+ len = io.read(2).unpack("v")[0]
15
+ name = io.read(len)
16
+ width, height = io.read(8).unpack("VV")
17
+ puts "Rectangle #{name} is #{width} x #{height}"
18
+ {:ruby}
19
+
20
+ It's ugly, violates DRY and feels like you're writing Perl, not Ruby.
21
+
22
+ There is a better way.
23
+
24
+ class Rectangle < BinData::Record
25
+ endian :little
26
+ uint16 :len
27
+ string :name, :read_length => :len
28
+ uint32 :width
29
+ uint32 :height
30
+ end
31
+
32
+ io = File.open(...)
33
+ r = Rectangle.read(io)
34
+ puts "Rectangle #{r.name} is #{r.width} x #{r.height}"
35
+ {:ruby}
36
+
37
+ BinData makes it easy to specify the structure of the data you are
38
+ manipulating.
39
+
40
+ Read on for the tutorial, or go straight to the
41
+ [download](http://rubyforge.org/frs/?group_id=3252) page.
42
+
43
+ ## License
44
+
45
+ BinData is released under the same license as Ruby.
46
+
47
+ Copyright &copy; 2007 - 2010 [Dion Mendel](mailto:dion@lostrealm.com)
48
+
49
+ ---------------------------------------------------------------------------
50
+
51
+ # Installation
52
+
53
+ You can install BinData via rubygems.
54
+
55
+ gem install bindata
56
+
57
+ Alternatively, visit the
58
+ [download](http://rubyforge.org/frs/?group_id=3252) page and download
59
+ BinData as a tar file.
60
+
61
+ ---------------------------------------------------------------------------
62
+
63
+ # Overview
64
+
65
+ BinData declarations are easy to read. Here's an example.
66
+
67
+ class MyFancyFormat < BinData::Record
68
+ stringz :comment
69
+ uint8 :num_ints, :check_value => lambda { value.even? }
70
+ array :some_ints, :type => :int32be, :initial_length => :num_ints
71
+ end
72
+ {:ruby}
73
+
74
+ This fancy format describes the following collection of data:
75
+
76
+ 1. A zero terminated string
77
+ 2. An unsigned 8bit integer which must by even
78
+ 3. A sequence of unsigned 32bit integers in big endian form, the total
79
+ number of which is determined by the value of the 8bit integer.
80
+
81
+ The BinData declaration matches the English description closely.
82
+ Compare the above declaration with the equivalent `#unpack` code to read
83
+ such a data record.
84
+
85
+ def read_fancy_format(io)
86
+ comment, num_ints, rest = io.read.unpack("Z*Ca*")
87
+ raise ArgumentError, "ints must be even" unless num_ints.even?
88
+ some_ints = rest.unpack("N#{num_ints}")
89
+ {:comment => comment, :num_ints => num_ints, :some_ints => *some_ints}
90
+ end
91
+ {:ruby}
92
+
93
+ The BinData declaration clearly shows the structure of the record. The
94
+ `#unpack` code makes this structure opaque.
95
+
96
+ The general usage of BinData is to declare a structured collection of
97
+ data as a user defined record. This record can be instantiated, read,
98
+ written and manipulated without the user having to be concerned with the
99
+ underlying binary representation of the data.
100
+
101
+ ---------------------------------------------------------------------------
102
+
103
+ # Common Operations
104
+
105
+ There are operations common to all BinData types, including user defined
106
+ ones. These are summarised here.
107
+
108
+ ## Reading and writing
109
+
110
+ `::read(io)`
111
+
112
+ : Creates a BinData object and reads its value from the given string
113
+ or `IO`. The newly created object is returned.
114
+
115
+ str = BinData::Stringz::read("string1\0string2")
116
+ str.snapshot #=> "string1"
117
+ {:ruby}
118
+
119
+ `#read(io)`
120
+
121
+ : Reads and assigns binary data read from `io`.
122
+
123
+ obj = BinData::Uint16be.new
124
+ obj.read("\022\064")
125
+ obj.value #=> 4660
126
+ {:ruby}
127
+
128
+ `#write(io)`
129
+
130
+ : Writes the binary representation of the object to `io`.
131
+
132
+ File.open("...", "wb") do |io|
133
+ obj = BinData::Uint64be.new
134
+ obj.value = 568290145640170
135
+ obj.write(io)
136
+ end
137
+ {:ruby}
138
+
139
+ `#to_binary_s`
140
+
141
+ : Returns the binary representation of this object as a string.
142
+
143
+ obj = BinData::Uint16be.new
144
+ obj.assign(4660)
145
+ obj.to_binary_s #=> "\022\064"
146
+ {:ruby}
147
+
148
+ ## Manipulating
149
+
150
+ `#assign(value)`
151
+
152
+ : Assigns the given value to this object. `value` can be of the same
153
+ format as produced by `#snapshot`, or it can be a compatible data
154
+ object.
155
+
156
+ arr = BinData::Array.new(:type => :uint8)
157
+ arr.assign([1, 2, 3, 4])
158
+ arr.snapshot #=> [1, 2, 3, 4]
159
+ {:ruby}
160
+
161
+ `#clear`
162
+
163
+ : Resets this object to its initial state.
164
+
165
+ obj = BinData::Int32be.new(:initial_value => 42)
166
+ obj.assign(50)
167
+ obj.clear
168
+ obj.value #=> 42
169
+ {:ruby}
170
+
171
+ `#clear?`
172
+
173
+ : Returns whether this object is in its initial state.
174
+
175
+ arr = BinData::Array.new(:type => :uint16be, :initial_length => 5)
176
+ arr[3] = 42
177
+ arr.clear? #=> false
178
+
179
+ arr[3].clear
180
+ arr.clear? #=> true
181
+ {:ruby}
182
+
183
+ ## Inspecting
184
+
185
+ `#num_bytes`
186
+
187
+ : Returns the number of bytes required for the binary representation
188
+ of this object.
189
+
190
+ arr = BinData::Array.new(:type => :uint16be, :initial_length => 5)
191
+ arr[0].num_bytes #=> 2
192
+ arr.num_bytes #=> 10
193
+ {:ruby}
194
+
195
+ `#snapshot`
196
+
197
+ : Returns the value of this object as primitive Ruby objects
198
+ (numerics, strings, arrays and hashs). The output of `#snapshot`
199
+ may be useful for serialization or as a reduced memory usage
200
+ representation.
201
+
202
+ obj = BinData::Uint8.new
203
+ obj.assign(3)
204
+ obj + 3 #=> 6
205
+
206
+ obj.snapshot #=> 3
207
+ obj.snapshot.class #=> Fixnum
208
+ {:ruby}
209
+
210
+ `#offset`
211
+
212
+ : Returns the offset of this object with respect to the most distant
213
+ ancestor structure it is contained within. This is most likely to
214
+ be used with arrays and records.
215
+
216
+ class Tuple < BinData::Record
217
+ int8 :a
218
+ int8 :b
219
+ end
220
+
221
+ arr = BinData::Array.new(:type => :tuple, :initial_length => 3)
222
+ arr[2].b.offset #=> 5
223
+ {:ruby}
224
+
225
+ `#rel_offset`
226
+
227
+ : Returns the offset of this object with respect to the parent
228
+ structure it is contained within. Compare this to `#offset`.
229
+
230
+ class Tuple < BinData::Record
231
+ int8 :a
232
+ int8 :b
233
+ end
234
+
235
+ arr = BinData::Array.new(:type => :tuple, :initial_length => 3)
236
+ arr[2].b.rel_offset #=> 1
237
+ {:ruby}
238
+
239
+ `#inspect`
240
+
241
+ : Returns a human readable representation of this object. This is a
242
+ shortcut to #snapshot.inspect.
243
+
244
+ ---------------------------------------------------------------------------
245
+
246
+ # Records
247
+
248
+ The general format of a BinData record declaration is a class containing
249
+ one or more fields.
250
+
251
+ class MyName < BinData::Record
252
+ type field_name, :param1 => "foo", :param2 => bar, ...
253
+ ...
254
+ end
255
+ {:ruby}
256
+
257
+ `type`
258
+ : is the name of a supplied type (e.g. `uint32be`, `string`, `array`)
259
+ or a user defined type. For user defined types, the class name is
260
+ converted from `CamelCase` to lowercased `underscore_style`.
261
+
262
+ `field_name`
263
+ : is the name by which you can access the field. Use either a
264
+ `String` or a `Symbol`. If name is nil or the empty string, then
265
+ this particular field is anonymous. An anonymous field is still
266
+ read and written, but will not appear in `#snapshot`.
267
+
268
+ Each field may have optional *parameters* for how to process the data.
269
+ The parameters are passed as a `Hash` with `Symbols` for keys.
270
+ Parameters are designed to be lazily evaluated, possibly multiple times.
271
+ This means that any parameter value must not have side effects.
272
+
273
+ Here are some examples of legal values for parameters.
274
+
275
+ * `:param => 5`
276
+ * `:param => lambda { 5 + 2 }`
277
+ * `:param => lambda { foo + 2 }`
278
+ * `:param => :foo`
279
+
280
+ The simplest case is when the value is a literal value, such as `5`.
281
+
282
+ If the value is not a literal, it is expected to be a lambda. The
283
+ lambda will be evaluated in the context of the parent, in this case the
284
+ parent is an instance of `MyName`.
285
+
286
+ If the value is a symbol, it is taken as syntactic sugar for a lambda
287
+ containing the value of the symbol.
288
+ e.g `:param => :foo` is `:param => lambda { foo }`
289
+
290
+ ## Specifying default endian
291
+
292
+ The endianess of numeric types must be explicitly defined so that the
293
+ code produced is independent of architecture. However, explicitly
294
+ specifying the endian for each numeric field can result in a bloated
295
+ declaration that can be difficult to read.
296
+
297
+ class A < BinData::Record
298
+ int16be :a
299
+ int32be :b
300
+ int16le :c # <-- Note little endian!
301
+ int32be :d
302
+ float_be :e
303
+ array :f, :type => :uint32be
304
+ end
305
+ {:ruby}
306
+
307
+ The `endian` keyword can be used to set the default endian. This makes
308
+ the declaration easier to read. Any numeric field that doesn't use the
309
+ default endian can explicitly override it.
310
+
311
+ class A < BinData::Record
312
+ endian :big
313
+
314
+ int16 :a
315
+ int32 :b
316
+ int16le :c # <-- Note how this little endian now stands out
317
+ int32 :d
318
+ float :e
319
+ array :f, :type => :uint32
320
+ end
321
+ {:ruby}
322
+
323
+ The increase in clarity can be seen with the above example. The
324
+ `endian` keyword will cascade to nested types, as illustrated with the
325
+ array in the above example.
326
+
327
+ ## Optional fields
328
+
329
+ A record may contain optional fields. The optional state of a field is
330
+ decided by the `:onlyif` parameter. If the value of this parameter is
331
+ `false`, then the field will be as if it didn't exist in the record.
332
+
333
+ class RecordWithOptionalField < BinData::Record
334
+ ...
335
+ uint8 :comment_flag
336
+ string :comment, :length => 20, :onlyif => :has_comment?
337
+
338
+ def has_comment?
339
+ comment_flag.nonzero?
340
+ end
341
+ end
342
+ {:ruby}
343
+
344
+ In the above example, the `comment` field is only included in the record
345
+ if the value of the `comment_flag` field is non zero.
346
+
347
+ ## Handling dependencies between fields
348
+
349
+ A common occurence in binary file formats is one field depending upon
350
+ the value of another. e.g. A string preceded by its length.
351
+
352
+ As an example, let's assume a Pascal style string where the byte
353
+ preceding the string contains the string's length.
354
+
355
+ # reading
356
+ io = File.open(...)
357
+ len = io.getc
358
+ str = io.read(len)
359
+ puts "string is " + str
360
+
361
+ # writing
362
+ io = File.open(...)
363
+ str = "this is a string"
364
+ io.putc(str.length)
365
+ io.write(str)
366
+ {:ruby}
367
+
368
+ Here's how we'd implement the same example with BinData.
369
+
370
+ class PascalString < BinData::Record
371
+ uint8 :len, :value => lambda { data.length }
372
+ string :data, :read_length => :len
373
+ end
374
+
375
+ # reading
376
+ io = File.open(...)
377
+ ps = PascalString.new
378
+ ps.read(io)
379
+ puts "string is " + ps.data
380
+
381
+ # writing
382
+ io = File.open(...)
383
+ ps = PascalString.new
384
+ ps.data = "this is a string"
385
+ ps.write(io)
386
+ {:ruby}
387
+
388
+ This syntax needs explaining. Let's simplify by examining reading and
389
+ writing separately.
390
+
391
+ class PascalStringReader < BinData::Record
392
+ uint8 :len
393
+ string :data, :read_length => :len
394
+ end
395
+ {:ruby}
396
+
397
+ This states that when reading the string, the initial length of the
398
+ string (and hence the number of bytes to read) is determined by the
399
+ value of the `len` field.
400
+
401
+ Note that `:read_length => :len` is syntactic sugar for
402
+ `:read_length => lambda { len }`, as described previously.
403
+
404
+ class PascalStringWriter < BinData::Record
405
+ uint8 :len, :value => lambda { data.length }
406
+ string :data
407
+ end
408
+ {:ruby}
409
+
410
+ This states that the value of `len` is always equal to the length of
411
+ `data`. `len` may not be manually modified.
412
+
413
+ Combining these two definitions gives the definition for `PascalString`
414
+ as previously defined.
415
+
416
+ It is important to note with dependencies, that a field can only depend
417
+ on one before it. You can't have a string which has the characters
418
+ first and the length afterwards.
419
+
420
+ ---------------------------------------------------------------------------
421
+
422
+ # Primitive Types
423
+
424
+ BinData provides support for the most commonly used primitive types that
425
+ are used when working with binary data. Namely:
426
+
427
+ * fixed size strings
428
+ * zero terminated strings
429
+ * byte based integers - signed or unsigned, big or little endian and
430
+ of any size
431
+ * bit based integers - unsigned big or little endian integers of any
432
+ size
433
+ * floating point numbers - single or double precision floats in either
434
+ big or little endian
435
+
436
+ Primitives may be manipulated individually, but is more common to work
437
+ with them as part of a record.
438
+
439
+ Examples of individual usage:
440
+
441
+ int16 = BinData::Int16be.new
442
+ int16.value = 941
443
+ int16.to_binary_s #=> "\003\255"
444
+
445
+ fl = BinData::FloatBe.read("\100\055\370\124") #=> 2.71828174591064
446
+ fl.num_bytes #=> 4
447
+
448
+ fl * int16 #=> 2557.90320057996
449
+ {:ruby}
450
+
451
+ There are several parameters that are specific to primitives.
452
+
453
+ `:initial_value`
454
+
455
+ : This contains the initial value that the primitive will contain
456
+ after initialization. This is useful for setting default values.
457
+
458
+ obj = BinData::String.new(:initial_value => "hello ")
459
+ obj + "world" #=> "hello world"
460
+
461
+ obj.assign("good-bye " )
462
+ obj + "world" #=> "good-bye world"
463
+ {:ruby}
464
+
465
+ `:value`
466
+
467
+ : The primitive will always contain this value. Reading or assigning
468
+ will not change the value. This parameter is used to define
469
+ constants or dependent fields.
470
+
471
+ pi = BinData::FloatLe.new(:value => Math::PI)
472
+ pi.assign(3)
473
+ puts pi #=> 3.14159265358979
474
+ {:ruby}
475
+
476
+ `:check_value`
477
+
478
+ : When reading, will raise a `ValidityError` if the value read does
479
+ not match the value of this parameter.
480
+
481
+ obj = BinData::String.new(:check_value => lambda { /aaa/ =~ value })
482
+ obj.read("baaa!") #=> "baaa!"
483
+ obj.read("bbb") #=> raises ValidityError
484
+
485
+ obj = BinData::String.new(:check_value => "foo")
486
+ obj.read("foo") #=> "foo"
487
+ obj.read("bar") #=> raises ValidityError
488
+ {:ruby}
489
+
490
+ ## Numerics
491
+
492
+ There are three kinds of numeric types that are supported by BinData.
493
+
494
+ ### Byte based integers
495
+
496
+ These are the common integers that are used in most low level
497
+ programming languages (C, C++, Java etc). These integers can be signed
498
+ or unsigned. The endian must be specified so that the conversion is
499
+ independent of architecture. The bit size of these integers must be a
500
+ multiple of 8. Examples of byte based integers are:
501
+
502
+ `uint16be`
503
+ : unsigned 16 bit big endian integer
504
+
505
+ `int8`
506
+ : signed 8 bit integer
507
+
508
+ `int32le`
509
+ : signed 32 bit little endian integer
510
+
511
+ `uint40be`
512
+ : unsigned 40 bit big endian integer
513
+
514
+ The `be` | `le` suffix may be omitted if the `endian` keyword is in use.
515
+
516
+ ### Bit based integers
517
+
518
+ These unsigned integers are used to define bitfields in records.
519
+ Bitfields are big endian by default but little endian may be specified
520
+ explicitly. Little endian bitfields are rare, but do occur in older
521
+ file formats (e.g. The file allocation table for FAT12 filesystems is
522
+ stored as an array of 12bit little endian integers).
523
+
524
+ An array of bit based integers will be packed according to their endian.
525
+
526
+ In a record, adjacent bitfields will be packed according to their
527
+ endian. All other fields are byte aligned.
528
+
529
+ Examples of bit based integers are:
530
+
531
+ `bit1`
532
+ : 1 bit big endian integer (may be used as boolean)
533
+
534
+ `bit4_le`
535
+ : 4 bit little endian integer
536
+
537
+ `bit32`
538
+ : 32 bit big endian integer
539
+
540
+ The difference between byte and bit base integers of the same number of
541
+ bits (e.g. `uint8` vs `bit8`) is one of alignment.
542
+
543
+ This example is packed as 3 bytes
544
+
545
+ class A < BinData::Record
546
+ bit4 :a
547
+ uint8 :b
548
+ bit4 :c
549
+ end
550
+
551
+ Data is stored as: AAAA0000 BBBBBBBB CCCC0000
552
+ {:ruby}
553
+
554
+ Whereas this example is packed into only 2 bytes
555
+
556
+ class B < BinData::Record
557
+ bit4 :a
558
+ bit8 :b
559
+ bit4 :c
560
+ end
561
+
562
+ Data is stored as: AAAABBBB BBBBCCCC
563
+ {:ruby}
564
+
565
+ ### Floating point numbers
566
+
567
+ BinData supports 32 and 64 bit floating point numbers, in both big and
568
+ little endian format. These types are:
569
+
570
+ `float_le`
571
+ : single precision 32 bit little endian float
572
+
573
+ `float_be`
574
+ : single precision 32 bit big endian float
575
+
576
+ `double_le`
577
+ : double precision 64 bit little endian float
578
+
579
+ `double_be`
580
+ : double precision 64 bit big endian float
581
+
582
+ The `_be` | `_le` suffix may be omitted if the `endian` keyword is in use.
583
+
584
+ ### Example
585
+
586
+ Here is an example declaration for an Internet Protocol network packet.
587
+
588
+ class IP_PDU < BinData::Record
589
+ endian :big
590
+
591
+ bit4 :version, :value => 4
592
+ bit4 :header_length
593
+ uint8 :tos
594
+ uint16 :total_length
595
+ uint16 :ident
596
+ bit3 :flags
597
+ bit13 :frag_offset
598
+ uint8 :ttl
599
+ uint8 :protocol
600
+ uint16 :checksum
601
+ uint32 :src_addr
602
+ uint32 :dest_addr
603
+ string :options, :read_length => :options_length_in_bytes
604
+ string :data, :read_length => lambda { total_length - header_length_in_bytes }
605
+
606
+ def header_length_in_bytes
607
+ header_length * 4
608
+ end
609
+
610
+ def options_length_in_bytes
611
+ header_length_in_bytes - 20
612
+ end
613
+ end
614
+ {:ruby}
615
+
616
+ Three of the fields have parameters.
617
+ * The version field always has the value 4, as per the standard.
618
+ * The options field is read as a raw string, but not processed.
619
+ * The data field contains the payload of the packet. Its length is
620
+ calculated as the total length of the packet minus the length of
621
+ the header.
622
+
623
+ ## Strings
624
+
625
+ BinData supports two types of strings - fixed size and zero terminated.
626
+ Strings are treated as a sequence of 8bit bytes. This is the same as
627
+ strings in Ruby 1.8. The issue of character encoding is ignored by
628
+ BinData.
629
+
630
+ ### Fixed Sized Strings
631
+
632
+ Fixed sized strings may have a set length. If an assigned value is
633
+ shorter than this length, it will be padded to this length. If no
634
+ length is set, the length is taken to be the length of the assigned
635
+ value.
636
+
637
+ There are several parameters that are specific to fixed sized strings.
638
+
639
+ `:read_length`
640
+
641
+ : The length to use when reading a value.
642
+
643
+ obj = BinData::String.new(:read_length => 5)
644
+ obj.read("abcdefghij")
645
+ obj.value #=> "abcde"
646
+ {:ruby}
647
+
648
+ `:length`
649
+
650
+ : The fixed length of the string. If a shorter string is set, it
651
+ will be padded to this length. Longer strings will be truncated.
652
+
653
+ obj = BinData::String.new(:length => 6)
654
+ obj.read("abcdefghij")
655
+ obj.value #=> "abcdef"
656
+
657
+ obj = BinData::String.new(:length => 6)
658
+ obj.value = "abcd"
659
+ obj.value #=> "abcd\000\000"
660
+
661
+ obj = BinData::String.new(:length => 6)
662
+ obj.value = "abcdefghij"
663
+ obj.value #=> "abcdef"
664
+ {:ruby}
665
+
666
+ `:pad_char`
667
+
668
+ : The character to use when padding a string to a set length. Valid
669
+ values are `Integers` and `Strings` of length 1.
670
+ `"\0"` is the default.
671
+
672
+ obj = BinData::String.new(:length => 6, :pad_char => 'A')
673
+ obj.value = "abcd"
674
+ obj.value #=> "abcdAA"
675
+ obj.to_binary_s #=> "abcdAA"
676
+ {:ruby}
677
+
678
+ `:trim_padding`
679
+
680
+ : Boolean, default `false`. If set, the value of this string will
681
+ have all pad_chars trimmed from the end of the string. The value
682
+ will not be trimmed when writing.
683
+
684
+ obj = BinData::String.new(:length => 6, :trim_value => true)
685
+ obj.value = "abcd"
686
+ obj.value #=> "abcd"
687
+ obj.to_binary_s #=> "abcd\000\000"
688
+ {:ruby}
689
+
690
+ ### Zero Terminated Strings
691
+
692
+ These strings are modelled on the C style of string - a sequence of
693
+ bytes terminated by a null (`"\0"`) character.
694
+
695
+ obj = BinData::Stringz.new
696
+ obj.read("abcd\000efgh")
697
+ obj.value #=> "abcd"
698
+ obj.num_bytes #=> 5
699
+ obj.to_binary_s #=> "abcd\000"
700
+ {:ruby}
701
+
702
+ ## User Defined Primitive Types
703
+
704
+ Most user defined types will be Records, but occasionally we'd like to
705
+ create a custom type of primitive.
706
+
707
+ Let us revisit the Pascal String example.
708
+
709
+ class PascalString < BinData::Record
710
+ uint8 :len, :value => lambda { data.length }
711
+ string :data, :read_length => :len
712
+ end
713
+ {:ruby}
714
+
715
+ We'd like to make `PascalString` a user defined type that behaves like a
716
+ `BinData::BasePrimitive` object so we can use `:initial_value` etc.
717
+ Here's an example usage of what we'd like:
718
+
719
+ class Favourites < BinData::Record
720
+ pascal_string :language, :initial_value => "ruby"
721
+ pascal_string :os, :initial_value => "unix"
722
+ end
723
+
724
+ f = Favourites.new
725
+ f.os = "freebsd"
726
+ f.to_binary_s #=> "\004ruby\007freebsd"
727
+ {:ruby}
728
+
729
+ We create this type of custom string by inheriting from
730
+ `BinData::Primitive` (instead of `BinData::Record`) and implementing the
731
+ `#get` and `#set` methods.
732
+
733
+ class PascalString < BinData::Primitive
734
+ uint8 :len, :value => lambda { data.length }
735
+ string :data, :read_length => :len
736
+
737
+ def get; self.data; end
738
+ def set(v) self.data = v; end
739
+ end
740
+ {:ruby}
741
+
742
+ ### Advanced User Defined Primitive Types
743
+
744
+ Sometimes a user defined primitive type can not easily be declaratively
745
+ defined. In this case you should inherit from `BinData::BasePrimitive`
746
+ and implement the following three methods:
747
+
748
+ * `value_to_binary_string(value)`
749
+ * `read_and_return_value(io)`
750
+ * `sensible_default()`
751
+
752
+ Here is an example of a big integer implementation.
753
+
754
+ # A custom big integer format. Binary format is:
755
+ # 1 byte : 0 for positive, non zero for negative
756
+ # x bytes : Little endian stream of 7 bit bytes representing the
757
+ # positive form of the integer. The upper bit of each byte
758
+ # is set when there are more bytes in the stream.
759
+ class BigInteger < BinData::BasePrimitive
760
+ register_self
761
+
762
+ def value_to_binary_string(value)
763
+ negative = (value < 0) ? 1 : 0
764
+ value = value.abs
765
+ bytes = [negative]
766
+ loop do
767
+ seven_bit_byte = value & 0x7f
768
+ value >>= 7
769
+ has_more = value.nonzero? ? 0x80 : 0
770
+ byte = has_more | seven_bit_byte
771
+ bytes.push(byte)
772
+
773
+ break if has_more.zero?
774
+ end
775
+
776
+ bytes.collect { |b| b.chr }.join
777
+ end
778
+
779
+ def read_and_return_value(io)
780
+ negative = read_uint8(io).nonzero?
781
+ value = 0
782
+ bit_shift = 0
783
+ loop do
784
+ byte = read_uint8(io)
785
+ has_more = byte & 0x80
786
+ seven_bit_byte = byte & 0x7f
787
+ value |= seven_bit_byte << bit_shift
788
+ bit_shift += 7
789
+
790
+ break if has_more.zero?
791
+ end
792
+
793
+ negative ? -value : value
794
+ end
795
+
796
+ def sensible_default
797
+ 0
798
+ end
799
+
800
+ def read_uint8(io)
801
+ io.readbytes(1).unpack("C").at(0)
802
+ end
803
+ end
804
+ {:ruby}
805
+
806
+ ---------------------------------------------------------------------------
807
+
808
+ # Arrays
809
+
810
+ A BinData array is a list of data objects of the same type. It behaves
811
+ much the same as the standard Ruby array, supporting most of the common
812
+ methods.
813
+
814
+ When instantiating an array, the type of object it contains must be
815
+ specified.
816
+
817
+ arr = BinData::Array.new(:type => :uint8)
818
+ arr[3] = 5
819
+ arr.snapshot #=> [0, 0, 0, 5]
820
+ {:ruby}
821
+
822
+ Parameters can be passed to this object with a slightly clumsy syntax.
823
+
824
+ arr = BinData::Array.new(:type => [:uint8, {:initial_value => :index}])
825
+ arr[3] = 5
826
+ arr.snapshot #=> [0, 1, 2, 5]
827
+ {:ruby}
828
+
829
+ There are two different parameters that specify the length of the array.
830
+
831
+ `:initial_length`
832
+
833
+ : Specifies the initial length of a newly instantiated array.
834
+ The array may grow as elements are inserted.
835
+
836
+ obj = BinData::Array.new(:type => :int8, :initial_length => 4)
837
+ obj.read("\002\003\004\005\006\007")
838
+ obj.snapshot #=> [2, 3, 4, 5]
839
+ {:ruby}
840
+
841
+ `:read_until`
842
+
843
+ : While reading, elements are read until this condition is true. This
844
+ is typically used to read an array until a sentinel value is found.
845
+ The variables `index`, `element` and `array` are made available to
846
+ any lambda assigned to this parameter. If the value of this
847
+ parameter is the symbol `:eof`, then the array will read as much
848
+ data from the stream as possible.
849
+
850
+ obj = BinData::Array.new(:type => :int8,
851
+ :read_until => lambda { index == 1 })
852
+ obj.read("\002\003\004\005\006\007")
853
+ obj.snapshot #=> [2, 3]
854
+
855
+ obj = BinData::Array.new(:type => :int8,
856
+ :read_until => lambda { element >= 3.5 })
857
+ obj.read("\002\003\004\005\006\007")
858
+ obj.snapshot #=> [2, 3, 4]
859
+
860
+ obj = BinData::Array.new(:type => :int8,
861
+ :read_until => lambda { array[index] + array[index - 1] == 9 })
862
+ obj.read("\002\003\004\005\006\007")
863
+ obj.snapshot #=> [2, 3, 4, 5]
864
+
865
+ obj = BinData::Array.new(:type => :int8, :read_until => :eof)
866
+ obj.read("\002\003\004\005\006\007")
867
+ obj.snapshot #=> [2, 3, 4, 5, 6, 7]
868
+ {:ruby}
869
+
870
+ ---------------------------------------------------------------------------
871
+
872
+ # Choices
873
+
874
+ A Choice is a collection of data objects of which only one is active at
875
+ any particular time. Method calls will be delegated to the active
876
+ choice. The possible types of objects that a choice contains is
877
+ controlled by the `:choices` parameter, while the `:selection` parameter
878
+ specifies the active choice.
879
+
880
+ `:choices`
881
+
882
+ : Either an array or a hash specifying the possible data objects. The
883
+ format of the array/hash.values is a list of symbols representing
884
+ the data object type. If a choice is to have params passed to it,
885
+ then it should be provided as `[type_symbol, hash_params]`. An
886
+ implementation constraint is that the hash may not contain symbols
887
+ as keys.
888
+
889
+ `:selection`
890
+
891
+ : An index/key into the `:choices` array/hash which specifies the
892
+ currently active choice.
893
+
894
+ `:copy_on_change`
895
+
896
+ : If set to `true`, copy the value of the previous selection to the
897
+ current selection whenever the selection changes. Default is
898
+ `false`.
899
+
900
+ Examples
901
+
902
+ type1 = [:string, {:value => "Type1"}]
903
+ type2 = [:string, {:value => "Type2"}]
904
+
905
+ choices = {5 => type1, 17 => type2}
906
+ obj = BinData::Choice.new(:choices => choices, :selection => 5)
907
+ obj.value # => "Type1"
908
+
909
+ choices = [ type1, type2 ]
910
+ obj = BinData::Choice.new(:choices => choices, :selection => 1)
911
+ obj.value # => "Type2"
912
+
913
+ choices = [ nil, nil, nil, type1, nil, type2 ]
914
+ obj = BinData::Choice.new(:choices => choices, :selection => 3)
915
+ obj.value # => "Type1"
916
+
917
+ class MyNumber < BinData::Record
918
+ int8 :is_big_endian
919
+ choice :data, :choices => { true => :int32be, false => :int32le },
920
+ :selection => lambda { is_big_endian != 0 },
921
+ :copy_on_change => true
922
+ end
923
+
924
+ obj = MyNumber.new
925
+ obj.is_big_endian = 1
926
+ obj.data = 5
927
+ obj.to_binary_s #=> "\001\000\000\000\005"
928
+
929
+ obj.is_big_endian = 0
930
+ obj.to_binary_s #=> "\000\005\000\000\000"
931
+ {:ruby}
932
+
933
+ ---------------------------------------------------------------------------
934
+
935
+ # Advanced Topics
936
+
937
+ ## Skipping over unused data
938
+
939
+ Some binary structures contain data that is irrelevant to your purposes.
940
+
941
+ Say you are interested in 50 bytes of data located 10 megabytes into the
942
+ stream. One way of accessing this useful data is:
943
+
944
+ class MyData < BinData::Record
945
+ string :length => 10 * 1024 * 1024
946
+ string :data, :length => 50
947
+ end
948
+ {:ruby}
949
+
950
+ The advantage of this method is that the irrelevant data is preserved
951
+ when writing the record. The disadvantage is that even if you don't care
952
+ about preserving this irrelevant data, it still occupies memory.
953
+
954
+ If you don't need to preserve this data, an alternative is to use
955
+ `skip` instead of `string`. When reading it will seek over the irrelevant
956
+ data and won't consume space in memory. When writing it will write
957
+ `:length` number of zero bytes.
958
+
959
+ class MyData < BinData::Record
960
+ skip :length => 10 * 1024 * 1024
961
+ string :data, :length => 50
962
+ end
963
+ {:ruby}
964
+
965
+ ## Wrappers
966
+
967
+ Sometimes you wish to create a new type that is simply an existing type
968
+ with some predefined parameters. Examples could be an array with a
969
+ specified type, or an integer with an initial value.
970
+
971
+ This can be achieved with a wrapper. A wrapper creates a new type based
972
+ on an existing type which has predefined parameters. These parameters
973
+ can of course be overridden at initialisation time.
974
+
975
+ Here we define an array that contains big endian 16 bit integers. The
976
+ array has a preferred initial length.
977
+
978
+ class IntArray < BinData::Wrapper
979
+ endian :big
980
+ array :type => :uint16, :initial_length => 5
981
+ end
982
+
983
+ arr = IntArray.new
984
+ arr.size #=> 5
985
+ {:ruby}
986
+
987
+ The initial length can be overridden at initialisation time.
988
+
989
+ arr = IntArray.new(:initial_length => 8)
990
+ arr.size #=> 8
991
+ {:ruby}
992
+
993
+ ## Parameterizing User Defined Types
994
+
995
+ All BinData types have parameters that allow the behaviour of an object
996
+ to be specified at initialization time. User defined types may also
997
+ specify parameters. There are two types of parameters: mandatory and
998
+ default.
999
+
1000
+ ### Mandatory Parameters
1001
+
1002
+ Mandatory parameters must be specified when creating an instance of the
1003
+ type. The `:type` parameter of `Array` is an example of a mandatory
1004
+ type.
1005
+
1006
+ class IntArray < BinData::Wrapper
1007
+ mandatory_parameter :half_count
1008
+
1009
+ array :type => :uint8, :initial_length => lambda { half_count * 2 }
1010
+ end
1011
+
1012
+ arr = IntArray.new
1013
+ #=> raises ArgumentError: parameter 'half_count' must be specified in IntArray
1014
+
1015
+ arr = IntArray.new(:half_count => lambda { 1 + 2 })
1016
+ arr.snapshot #=> [0, 0, 0, 0, 0, 0]
1017
+ {:ruby}
1018
+
1019
+ ### Default Parameters
1020
+
1021
+ Default parameters are optional. These parameters have a default value
1022
+ that may be overridden when an instance of the type is created.
1023
+
1024
+ class Phrase < BinData::Primitive
1025
+ default_parameter :number => "three"
1026
+ default_parameter :adjective => "blind"
1027
+ default_parameter :noun => "mice"
1028
+
1029
+ stringz :a, :initial_value => :number
1030
+ stringz :b, :initial_value => :adjective
1031
+ stringz :c, :initial_value => :noun
1032
+
1033
+ def get; "#{a} #{b} #{c}"; end
1034
+ def set(v)
1035
+ if /(.*) (.*) (.*)/ =~ v
1036
+ self.a, self.b, self.c = $1, $2, $3
1037
+ end
1038
+ end
1039
+ end
1040
+
1041
+ obj = Phrase.new(:number => "two", :adjective => "deaf")
1042
+ obj.to_s #=> "two deaf mice"
1043
+ {:ruby}
1044
+
1045
+ ## Debugging
1046
+
1047
+ BinData includes several features to make it easier to debug
1048
+ declarations.
1049
+
1050
+ ### Tracing
1051
+
1052
+ BinData has the ability to trace the results of reading a data
1053
+ structure.
1054
+
1055
+ class A < BinData::Record
1056
+ int8 :a
1057
+ bit4 :b
1058
+ bit2 :c
1059
+ array :d, :initial_length => 6, :type => :bit1
1060
+ end
1061
+
1062
+ BinData::trace_reading do
1063
+ A.read("\373\225\220")
1064
+ end
1065
+ {:ruby}
1066
+
1067
+ Results in the following being written to `STDERR`.
1068
+
1069
+ obj.a => -5
1070
+ obj.b => 9
1071
+ obj.c => 1
1072
+ obj.d[0] => 0
1073
+ obj.d[1] => 1
1074
+ obj.d[2] => 1
1075
+ obj.d[3] => 0
1076
+ obj.d[4] => 0
1077
+ obj.d[5] => 1
1078
+ {:ruby}
1079
+
1080
+ ### Rest
1081
+
1082
+ The rest keyword will consume the input stream from the current position
1083
+ to the end of the stream.
1084
+
1085
+ class A < BinData::Record
1086
+ string :a, :read_length => 5
1087
+ rest :rest
1088
+ end
1089
+
1090
+ obj = A.read("abcdefghij")
1091
+ obj.a #=> "abcde"
1092
+ obj.rest #=" "fghij"
1093
+ {:ruby}
1094
+
1095
+ ### Hidden fields
1096
+
1097
+ The typical way to view the contents of a BinData record is to call
1098
+ `#snapshot` or `#inspect`. This gives all fields and their values. The
1099
+ `hide` keyword can be used to prevent certain fields from appearing in
1100
+ this output. This removes clutter and allows the developer to focus on
1101
+ what they are currently interested in.
1102
+
1103
+ class Testing < BinData::Record
1104
+ hide :a, :b
1105
+ string :a, :read_length => 10
1106
+ string :b, :read_length => 10
1107
+ string :c, :read_length => 10
1108
+ end
1109
+
1110
+ obj = Testing.read(("a" * 10) + ("b" * 10) + ("c" * 10))
1111
+ obj.snapshot #=> {"c"=>"cccccccccc"}
1112
+ obj.to_binary_s #=> "aaaaaaaaaabbbbbbbbbbcccccccccc"
1113
+ {:ruby}
1114
+
1115
+ ---------------------------------------------------------------------------
1116
+
1117
+ # Alternatives
1118
+
1119
+ There are several alternatives to BinData. Below is a comparison
1120
+ between BinData and its alternatives.
1121
+
1122
+ The short form is that BinData is the best choice for most cases. If
1123
+ decoding / encoding speed is very important and the binary formats are
1124
+ simple then BitStruct may be a good choice. (Though if speed is
1125
+ important, perhaps you should investigate a language other than Ruby.)
1126
+
1127
+ ### [BitStruct](http://rubyforge.org/projects/bit-struct)
1128
+
1129
+ BitStruct is the most complete of all the alternatives. It is
1130
+ declarative and supports all the same primitive types as BinData. In
1131
+ addition it includes a self documenting feature to make it easy to write
1132
+ reports.
1133
+
1134
+ The major limitation of BitStruct is that it does not support variable
1135
+ length fields and dependent fields. The simple PascalString example
1136
+ used previously is not possible with BitStruct. This limitation is due
1137
+ to the design choice to favour speed over flexibility.
1138
+
1139
+ Most non trivial file formats rely on dependent and variable length
1140
+ fields. It is difficult to use BitStruct with these formats as code
1141
+ must be written to explicitly handle the dependencies.
1142
+
1143
+ BitStruct does not currently support little endian bit fields, or
1144
+ bitfields that span more than 2 bytes. BitStruct is actively maintained
1145
+ so these limitations may be removed in a future release.
1146
+
1147
+ If speed is important and you are only dealing with simple binary data
1148
+ types then BitStruct is a good choice. For non trivial data types,
1149
+ BinData is the better choice.
1150
+
1151
+ ### [BinaryParse](http://rubyforge.org/projects/binaryparse)
1152
+
1153
+ BinaryParse is a declarative style packer / unpacker. It provides the
1154
+ same primitives as Ruby's `#pack`, with the addition of date and time.
1155
+ Like BitStruct, it doesn't provide dependent or variable length fields.
1156
+
1157
+ ### [BinStruct](http://rubyforge.org/projects/metafuzz)
1158
+
1159
+ BinStruct is an imperative approach to unpacking binary data. It does
1160
+ provide some declarative style syntax sugar. It provides support for
1161
+ the most common primitive types, as well as arbitrary length bitfields.
1162
+
1163
+ Its main focus is as a binary fuzzer, rather than as a generic decoding
1164
+ / encoding library.
1165
+
1166
+ ### [Packable](http://github.com/marcandre/packable/tree/master)
1167
+
1168
+ Packable makes it much nicer to use Ruby's `#pack` and `#unpack`
1169
+ methods. Instead of having to remember that, for example `"n"` is the
1170
+ code to pack a 16 bit big endian integer, packable provides many
1171
+ convenient shortcuts. In the case of `"n"`, `{:bytes => 2, :endian => :big}`
1172
+ may be used instead.
1173
+
1174
+ Using Packable improves the readability of `#pack` and `#unpack`
1175
+ methods, but explicitly calls to `#pack` and `#unpack` aren't as
1176
+ readable as a declarative approach.
1177
+
1178
+ ### [Bitpack](http://rubyforge.org/projects/bitpack)
1179
+
1180
+ Bitpack provides methods to extract big endian integers of arbitrary bit
1181
+ length from an octet stream.
1182
+
1183
+ The extraction code is written in `C`, so if speed is important and bit
1184
+ manipulation is all the functionality you require then this may be an
1185
+ alternative.
1186
+
1187
+ ---------------------------------------------------------------------------