Class: Undll32::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/undll32.rb

Overview

represents a string/struct. Buffer.new({ :x => :L, :y => 4 }) Buffer.new([ :L, 4 ]) Buffer.new(:L) Buffer.new(4) buffer.buffer => “0000” buffer.unpack => 0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(struct) ⇒ Buffer

Returns a new instance of Buffer.



64
65
66
67
68
69
70
71
# File 'lib/undll32.rb', line 64

def initialize struct
  case struct
  when Hash, Array, Symbol, Integer
    @struct = struct
  else
    raise ArgumentError, "expected hash, array, sym, int, got #{struct}"
  end
end

Instance Attribute Details

#structObject

Returns the value of attribute struct.



62
63
64
# File 'lib/undll32.rb', line 62

def struct
  @struct
end

Class Method Details

.from(code) ⇒ Object



250
251
252
253
254
255
# File 'lib/undll32.rb', line 250

def self.from code
  return from_array(code) if code.start_with?('[')
  return from_size(code) if code.start_with?(':')
  code = "{#{code}}" unless code.start_with?('{')
  return from_struct(code)
end

.from_array(code) ⇒ Object



165
166
167
168
169
170
# File 'lib/undll32.rb', line 165

def self.from_array code
  unless code =~ /^\[[CSLQ]+\]$/
    raise ArgumentError, "expected [CSLQ], got #{code}"
  end
  return new(code[1..-2].chars.map(&:to_sym))
end

.from_size(code) ⇒ Object

Raises:

  • (ArgumentError)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/undll32.rb', line 172

def self.from_size code
  m = code.match(/^\:(?<size>[^=]+)=?(?<value>.+)?$/)
  if m.nil?
    raise ArgumentError, "expected <number> or CSLQ, got #{code}"
  end
  if m[:size] =~ /^[CSLQ]+$/
    s = m[:size].chars.map(&:to_sym)
    if s.size == 1
      return new(s[0]).tap { |b| b.load(m[:value].to_i) if m[:value] }
    else
      b = new(s)
      b.load(m[:value].split(':').map(&:to_i)) if m[:value]
      return b
    end
  end
  if (n = m[:size].to_i)
    return new(n).tap { |b| b.load(m[:value]) if m[:value] }
  end
  raise ArgumentError, "expected <number> or CSLQ, got #{code}"
end

.from_struct(code) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/undll32.rb', line 198

def self.from_struct code
  default = {}
  # code := '{' [name] [:type] [=value] [,...] '}'
  env, keys, key = [], [], ''
  seq = code.scan /\w+|\:[[:digit:]]+|\:[CSLQ:]+|=[^,\}]+|./
  ret = while (token = seq.shift)
    case token[0]
    when '{'
      keys.push(key.to_sym) unless key.empty?
      env.push({})
      key = ''
    when '}'
      env[-1][key.to_sym] = :L unless key.empty?
      x = env.pop
      break x if env.empty?
      env[-1][keys.pop] = x
      key = ''
    when ':'
      token << seq.shift if seq.first&.start_with?('=')
      b = from_size(token)
      key = next_placeholder if key.empty?
      env[-1][key.to_sym] = b.struct
      d = default
      keys.each { |k| d[k] ||= {}; d = d[k] }
      d[key.to_sym] = b.unpack
      key = ''
    when '='
      e = token[1..-1]
      e = Integer(e) rescue nil
      type = Integer === e ? :L : (e.length + 1)
      env[-1][key.to_sym] = type
      d = default
      keys.each { |k| d[k] ||= {}; d = d[k] }
      if Integer === type
        e = "\"#{e}\"".undump rescue e.undump
      end
      d[key.to_sym] = e
      key = ''
    when ','
      next if key.empty?
      env[-1][key.to_sym] = :L
      key = ''
    else
      key << token
    end
  end
  if ret.nil?
    raise ArgumentError, 'expected }, got end-of-input'
  end
  new(ret).tap { |b| b.load(default) }
end

.next_placeholderObject



193
194
195
196
# File 'lib/undll32.rb', line 193

def self.next_placeholder
  @_placeholder_counter ||= 0
  "__#{@_placeholder_counter += 1}__"
end

Instance Method Details

#_buffer(x) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/undll32.rb', line 144

def _buffer x
  case x
  when Integer
    [].pack("x#{x}")
  when Symbol
    n = { C: 1, S: 2, L: 4, Q: 8 }[x]
    if n.nil?
      raise ArgumentError, "expected CSLQ, got #{x}"
    end
    [].pack("x#{n}")
  when Array
    x.map { |e| _buffer e }.join
  when Hash
    _buffer x.values
  else
    raise ArgumentError, "expected hash, array, sym, int, got #{x}"
  end
ensure
  $@.shift if $@
end

#_load(x, v) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/undll32.rb', line 88

def _load x, v
  case x
  when Integer
    return @_i += x if v.nil?
    unless String === v
      raise ArgumentError, "expected str, got #{v}"
    end
    x = [x, v.length].min
    buffer[(@_i += x) - x, x] = v[0, x]
  when Symbol
    n = { C: 1, S: 2, L: 4, Q: 8 }[x]
    if n.nil?
      raise ArgumentError, "expected CSLQ, got #{x}"
    end
    return @_i += n if v.nil?
    unless Integer === v
      raise ArgumentError, "expected int, got #{v}"
    end
    buffer[(@_i += n) - n, n] = [v].pack("#{x}")
  when Array
    return x.each { |a| _load a, nil } if v.nil?
    unless Array === v and v.size == x.size
      raise ArgumentError, "expected array[#{x.size}], got #{v}"
    end
    x.zip(v) { |a, b| _load a, b }
  when Hash
    return x.each { |k, y| _load y, nil } if v.nil?
    unless Hash === v
      raise ArgumentError, "expected hash, got #{v}"
    end
    x.each { |k, y| _load y, v[k] }
  else
    raise ArgumentError, "expected hash, array, sym, int, got #{x}"
  end
ensure
  $@.shift if $@
end

#_unpack(x) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/undll32.rb', line 126

def _unpack x
  case x
  when Integer
    buffer[(@_i += x) - x, x].sub(/\0+$/, '')
  when Symbol
    n = { C: 1, S: 2, L: 4, Q: 8 }[x]
    buffer[(@_i += n) - n, n].unpack1("#{x}")
  when Array
    x.map { |e| _unpack e }
  when Hash
    Hash[x.map { |k, v| [k, _unpack(v)] }]
  else
    raise ArgumentError, "expected hash, array, sym, int, got #{x}"
  end
ensure
  $@.shift if $@
end

#bufferObject



73
74
75
# File 'lib/undll32.rb', line 73

def buffer
  @buffer ||= _buffer(@struct)
end

#load(v) ⇒ Object



82
83
84
85
86
# File 'lib/undll32.rb', line 82

def load v
  @_i = 0
  _load @struct, v
  unpack
end

#unpackObject



77
78
79
80
# File 'lib/undll32.rb', line 77

def unpack
  @_i = 0
  _unpack @struct
end