require 'bit-struct/bit-struct' class BitStruct # Class for unsigned integers in network order, 1-16 bits, or 8n bits. # Declared with BitStruct.unsigned. class UnsignedField < Field # Used in describe. def self.class_name @class_name ||= "unsigned" end def add_accessors_to(cl, attr = name) # :nodoc: offset_byte = offset / 8 offset_bit = offset % 8 length_bit = offset_bit + length length_byte = (length_bit/8.0).ceil last_byte = offset_byte + length_byte - 1 divisor = options[:fixed] || options["fixed"] divisor_f = divisor && divisor.to_f # if divisor and not divisor.is_a? Fixnum # raise ArgumentError, "fixed-point divisor must be a fixnum" # end endian = (options[:endian] || options["endian"]).to_s case endian when "native" ctl = length_byte <= 2 ? "S" : "L" when "little" ctl = length_byte <= 2 ? "v" : "V" when "network", "big", "" ctl = length_byte <= 2 ? "n" : "N" else raise ArgumentError, "Unrecognized endian option: #{endian.inspect}" end data_is_big_endian = ([1234].pack(ctl) == [1234].pack(length_byte <= 2 ? "n" : "N")) if length_byte == 1 rest = 8 - length_bit mask = ["0"*offset_bit + "1"*length + "0"*rest].pack("B8")[0] mask2 = ["1"*offset_bit + "0"*length + "1"*rest].pack("B8")[0] cl.class_eval do if divisor define_method attr do || ((self[offset_byte] & mask) >> rest) / divisor_f end define_method "#{attr}=" do |val| val = (val * divisor).round self[offset_byte] = (self[offset_byte] & mask2) | ((val<> rest end define_method "#{attr}=" do |val| self[offset_byte] = (self[offset_byte] & mask2) | ((val< 0 bytes.push val % 256 val = val >> 8 end if bytes.length < length_byte bytes.concat [0] * (length_byte - bytes.length) end bytes.reverse! if data_is_big_endian bytes.pack("C*") end if divisor define_method attr do || reader_helper[self[byte_range]] / divisor_f end define_method "#{attr}=" do |val| self[byte_range] = writer_helper[(val * divisor).round] end else define_method attr do || reader_helper[self[byte_range]] end define_method "#{attr}=" do |val| self[byte_range] = writer_helper[val] end end end end elsif length_byte == 2 # unaligned field that fits within two whole bytes byte_range = offset_byte..last_byte rest = 16 - length_bit mask = ["0"*offset_bit + "1"*length + "0"*rest] mask = mask.pack("B16").unpack(ctl).first mask2 = ["1"*offset_bit + "0"*length + "1"*rest] mask2 = mask2.pack("B16").unpack(ctl).first cl.class_eval do if divisor define_method attr do || ((self[byte_range].unpack(ctl).first & mask) >> rest) / divisor_f end define_method "#{attr}=" do |val| val = (val * divisor).round x = (self[byte_range].unpack(ctl).first & mask2) | ((val<> rest end define_method "#{attr}=" do |val| x = (self[byte_range].unpack(ctl).first & mask2) | ((val<> rest) / divisor_f end define_method "#{attr}=" do |val| val = (val * divisor).round bytes = self[byte_range] bytes << 0 x = (bytes.unpack(ctl).first & mask2) | ((val<> rest end define_method "#{attr}=" do |val| bytes = self[byte_range] bytes << 0 x = (bytes.unpack(ctl).first & mask2) | ((val<:fixed => divisor option, which specifies # that the internally stored value is interpreted as a fixed point real # number with the specified +divisor+. # # The :endian => :native option overrides the default of # :network byte ordering, in favor of native byte ordering. Also # permitted are :big (same as :network) and # :little. # def unsigned name, length, *rest opts = parse_options(rest, name, UnsignedField) add_field(name, length, opts) end end end