Module: BinData::Int

Defined in:
lib/bindata/int.rb

Overview

Defines a number of classes that contain an integer. The integer is defined by endian, signedness and number of bytes.

Class Method Summary collapse

Class Method Details

.class_name(nbits, endian, signed) ⇒ Object



24
25
26
27
28
29
# File 'lib/bindata/int.rb', line 24

def class_name(nbits, endian, signed)
  endian_str = (endian == :big) ? "be" : "le"
  base = (signed == :signed) ? "Int" : "Uint"

  "#{base}#{nbits}#{endian_str}"
end

.create_clamp_code(min, max) ⇒ Object



61
62
63
# File 'lib/bindata/int.rb', line 61

def create_clamp_code(min, max)
  "val = (val < #{min}) ? #{min} : (val > #{max}) ? #{max} : val"
end

.create_int2uint_code(nbits) ⇒ Object



65
66
67
# File 'lib/bindata/int.rb', line 65

def create_int2uint_code(nbits)
  "val = val & #{(1 << nbits) - 1}"
end

.create_int_methods(int_class, nbits, endian) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bindata/int.rb', line 44

def create_int_methods(int_class, nbits, endian)
  raise "nbits must be divisible by 8" unless (nbits % 8).zero?

  max = (1 << (nbits - 1)) - 1
  min = -(max + 1)

  clamp = create_clamp_code(min, max)
  read = create_read_code(nbits, endian)
  to_binary_s = create_to_binary_s_code(nbits, endian)

  int2uint = create_int2uint_code(nbits)
  uint2int = create_uint2int_code(nbits)

  define_methods(int_class, nbits / 8, clamp, read, to_binary_s,
                   int2uint, uint2int)
end

.create_read_code(nbits, endian) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bindata/int.rb', line 76

def create_read_code(nbits, endian)
  # determine "word" size and unpack directive
  if (nbits % 32).zero?
    bytes_per_word = 4
    d = (endian == :big) ? 'N' : 'V'
  elsif (nbits % 16).zero?
    bytes_per_word = 2
    d = (endian == :big) ? 'n' : 'v'
  else
    bytes_per_word = 1
    d = 'C'
  end

  bits_per_word = bytes_per_word * 8
  nwords        = nbits / bits_per_word
  nbytes        = nbits / 8

  idx = (0 ... nwords).to_a
  idx.reverse! if (endian == :big)

  unpack_str = "a = io.readbytes(#{nbytes}).unpack('#{d * nwords}')"

  parts = (0 ... nwords).collect do |i|
            i.zero? ? "a.at(#{idx[i]})" :
                      "(a.at(#{idx[i]}) << #{bits_per_word * i})"
          end
  assemble_str = parts.join(" + ")

  "(#{unpack_str}; #{assemble_str})"
end

.create_to_binary_s_code(nbits, endian) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bindata/int.rb', line 107

def create_to_binary_s_code(nbits, endian)
  # special case 8bit integers for speed
  return "val.chr" if nbits == 8

  # determine "word" size and pack directive
  if (nbits % 32).zero?
    bytes_per_word = 4
    d = (endian == :big) ? 'N' : 'V'
  elsif (nbits % 16).zero?
    bytes_per_word = 2
    d = (endian == :big) ? 'n' : 'v'
  else
    bytes_per_word = 1
    d = 'C'
  end

  bits_per_word = bytes_per_word * 8
  nwords        = nbits / bits_per_word
  mask          = (1 << bits_per_word) - 1

  vals = (0 ... nwords).collect do |i|
           i.zero? ? "val" : "(val >> #{bits_per_word * i})"
         end
  vals.reverse! if (endian == :big)

  parts = (0 ... nwords).collect { |i| "#{vals[i]} & #{mask}" }
  array_str = "[" + parts.join(", ") + "]"

  "#{array_str}.pack('#{d * nwords}')"
end

.create_uint2int_code(nbits) ⇒ Object



69
70
71
72
73
74
# File 'lib/bindata/int.rb', line 69

def create_uint2int_code(nbits)
  mask = (1 << (nbits - 1)) - 1

  "val = ((val & #{1 << (nbits - 1)}).zero?) ? " +
           "val & #{mask} : -(((~val) & #{mask}) + 1)"
end

.create_uint_methods(int_class, nbits, endian) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bindata/int.rb', line 31

def create_uint_methods(int_class, nbits, endian)
  raise "nbits must be divisible by 8" unless (nbits % 8).zero?

  min = 0
  max = (1 << nbits) - 1

  clamp = create_clamp_code(min, max)
  read = create_read_code(nbits, endian)
  to_binary_s = create_to_binary_s_code(nbits, endian)

  define_methods(int_class, nbits / 8, clamp, read, to_binary_s)
end

.define_class(nbits, endian, signed) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bindata/int.rb', line 9

def define_class(nbits, endian, signed)
  name = class_name(nbits, endian, signed)
  return if BinData.const_defined?(name)

  int_type = (signed == :signed) ? 'int' : 'uint'
  creation_method = "create_#{int_type}_methods"

  BinData.module_eval <<-END
    class #{name} < BinData::BasePrimitive
      register(self.name, self)
      Int.#{creation_method}(self, #{nbits}, :#{endian.to_s})
    end
  END
end

.define_methods(int_class, nbytes, clamp, read, to_binary_s, int2uint = nil, uint2int = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/bindata/int.rb', line 138

def define_methods(int_class, nbytes, clamp, read, to_binary_s,
                     int2uint = nil, uint2int = nil)
  int_class.module_eval <<-END
    #---------------
    private

    def _assign(val)
      #{clamp}
      super(val)
    end

    def _do_num_bytes
      #{nbytes}
    end

    def sensible_default
      0
    end

    def value_to_binary_string(val)
      #{clamp}
      #{int2uint unless int2uint.nil?}
      #{to_binary_s}
    end

    def read_and_return_value(io)
      val = #{read}
      #{uint2int unless uint2int.nil?}
    end
  END
end