examples/gzip.rb in bindata-0.9.0 vs examples/gzip.rb in bindata-0.9.1

- old
+ new

@@ -8,73 +8,60 @@ # Known compression methods DEFLATE = 8 class Extra < BinData::MultiValue - uint16le :len, :length => lambda { data.length } - string :data, :read_length => :len + endian :little + + uint16 :len, :length => lambda { data.length } + string :data, :read_length => :len end class Header < BinData::MultiValue - uint16le :ident, :value => 0x8b1f, :check_value => 0x8b1f - uint8 :compression_method, :initial_value => DEFLATE - uint8 :flags, :value => :calculate_flags_val, - # Upper 3 bits must be zero - :check_value => lambda { (value & 0xe0) == 0 } - uint32le :mtime - uint8 :extra_flags - uint8 :os, :initial_value => 255 # unknown OS + endian :little - # These fields are optional depending on the bits in flags - extra :extra, :readwrite => :extra? - stringz :file_name, :readwrite => :file_name? - stringz :comment, :readwrite => :comment? - uint16le :crc16, :readwrite => :crc16? + uint16 :ident, :value => 0x8b1f, :check_value => 0x8b1f + uint8 :compression_method, :initial_value => DEFLATE + bit3 :freserved, :value => 0, :check_value => 0 + bit1 :fcomment, :value => lambda { comment.length > 0 ? 1 : 0 } + bit1 :ffile_name, :value => lambda { file_name.length > 0 ? 1 : 0 } + bit1 :fextra, :value => lambda { extra.len > 0 ? 1 : 0 } + bit1 :fcrc16, :value => 0 # see comment below + bit1 :ftext - ## Convenience methods for accessing and manipulating flags + # Never include header crc. This is because the current versions of the + # command-line version of gzip (up through version 1.3.x) do not + # support header crc's, and will report that it is a "multi-part gzip + # file" and give up. - attr_writer :text + uint32 :mtime + uint8 :extra_flags + uint8 :os, :initial_value => 255 # unknown OS - # Access bits of flags - def text?; flag_val(0) end - def crc16?; flag_val(1) end - def extra?; flag_val(2) end - def file_name?; flag_val(3) end - def comment?; flag_val(4) end - - def flag_val(bit) (flags & (1 << bit)) != 0 end - - # Calculate the value of flags based on current state. - def calculate_flags_val - ((@text ? 1 : 0) << 0) | - - # Never include header crc. This is because the current versions of the - # command-line version of gzip (up through version 1.3.x) do not - # support header crc's, and will report that it is a "multi-part gzip - # file" and give up. - ((!clear?(:crc16) ? 0 : 0) << 1) | - - ((!clear?(:extra) ? 1 : 0) << 2) | - ((!clear?(:file_name) ? 1 : 0) << 3) | - ((!clear?(:comment) ? 1 : 0) << 4) - end + # These fields are optional depending on the bits in flags + extra :extra, :onlyif => lambda { fextra.nonzero? } + stringz :file_name, :onlyif => lambda { ffile_name.nonzero? } + stringz :comment, :onlyif => lambda { fcomment.nonzero? } + uint16 :crc16, :onlyif => lambda { fcrc16.nonzero? } end class Footer < BinData::MultiValue - uint32le :crc32 - uint32le :uncompressed_size + endian :little + + uint32 :crc32 + uint32 :uncompressed_size end def initialize @header = Header.new @footer = Footer.new end attr_accessor :compressed - def_delegators :@header, :file_name=, :file_name, :file_name? - def_delegators :@header, :comment=, :comment, :comment? + def_delegators :@header, :file_name=, :file_name + def_delegators :@header, :comment=, :comment def_delegators :@header, :compression_method def_delegators :@footer, :crc32, :uncompressed_size def mtime Time.at(@header.mtime) @@ -164,10 +151,10 @@ g.mtime.strftime('%H:%M'), g.total_size, g.uncompressed_size, ratio, g.file_name] - puts "Comment: #{g.comment}" if g.comment? + puts "Comment: #{g.comment}" if g.comment != "" puts puts "Executing gzip -l -v" puts `gzip -l -v poetry.gz` end