Module: WWMD::ViewStateUtils

Included in:
VSStubHelpers, ViewState
Defined in:
lib/wwmd/viewstate/viewstate_utils.rb

Instance Method Summary collapse

Instance Method Details

#deserialize_type(t = nil) ⇒ Object



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

def deserialize_type(t=nil)
  op = self.read_byte
  case op
    when VIEWSTATE_TYPES.index(:typeref)
      type = read_7bit_encoded_int
      return [op,type]
    when VIEWSTATE_TYPES.index(:typeref_add_local)
      name  = read_string
      return [op,name]
    when VIEWSTATE_TYPES.index(:typeref_add)
      name  = read_string
      return [op,name]
    else
      raise "Invalid Type Error 0x#{op.to_s(16)}"
  end
end

#dlog(t, msg) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 15

def dlog(t,msg)
  raise "null token passed to dlog()" if t.nil?
  o = WWMD::VIEWSTATE_TYPES[t]
  @obj_counts[o] ||= 0
  @obj_counts[o] += 1
  return nil if !self.debug
  putd "#{self.last_offset} [0x#{t.to_s(16).rjust(2,"0")}] #{VIEWSTATE_TYPES[t]}: #{msg}"
end

#magic?Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 100

def magic?
  @magic = [@buf.read(1),@buf.read(1)].join("")
  VIEWSTATE_MAGIC.include?(@magic)
end

#next_typeObject



156
157
158
159
160
161
162
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 156

def next_type
  b = @buf.pos
  t = read_byte
  @buf.pos = b
  throw(t) if not VIEWSTATE_TYPES.include?(t)
  VIEWSTATE_TYPES[t]
end

#offset(cur = nil) ⇒ Object



144
145
146
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 144

def offset(cur=nil)
    @buf.pos.to_s(16).rjust(8,"0")
end

#putd(msg) ⇒ Object



4
5
6
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 4

def putd(msg)
  puts(msg) if self.debug
end

#read(count) ⇒ Object



60
61
62
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 60

def read(count)
  @buf.read(count)
end

#read_7bit_encoded_int(buf = nil) ⇒ Object

why oh why did I have to go find this? System.IO.BinaryReader.Read7BitEncodedInt



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 36

def read_7bit_encoded_int(buf=nil)
  l = 0  # length
  s = 0  # shift
  b = "" # byte
  buf = buf.scan(/./m) if buf
  begin
    if not buf
      b = self.read_int
    else
      b = buf.shift.unpack("C").first
    end
    l |= (b & 0x7f) << s
    s += 7
  end while ((b & 0x80) != 0)
  return l
end

#read_doubleObject



92
93
94
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 92

def read_double
  @buf.read(8).unpack("Q").first
end

#read_intObject Also known as: read_byte



64
65
66
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 64

def read_int
  @buf.read(1).unpack("C").first
end

#read_int32Object Also known as: read_single



82
83
84
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 82

def read_int32
  @buf.read(4).unpack("L").first
end

#read_raw_byteObject



105
106
107
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 105

def read_raw_byte
  @buf.read
end

#read_shortObject



74
75
76
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 74

def read_short
  self.read(2).unpack("S").first
end

#read_stringObject



53
54
55
56
57
58
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 53

def read_string
  len = read_7bit_encoded_int
  starr = []
  (1..len).each { |i| starr << @buf.read(1) }
  return starr.to_s
end

#serialize_type(op, ref) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 109

def serialize_type(op,ref)
  op_str = [op].pack("C")
  s = op_str
  case op
    when VIEWSTATE_TYPES.index(:typeref)
      s << write_7bit_encoded_int(ref)
    when VIEWSTATE_TYPES.index(:typeref_add_local)
      s << write_7bit_encoded_int(ref.size)
      s << ref
    when VIEWSTATE_TYPES.index(:typeref_add)
      s << write_7bit_encoded_int(ref.size)
      s << ref
    else
      raise "Invalid Type Error #{op.to_s(16)}"
  end
  return s
end

#slog(obj, msg = nil) ⇒ Object



8
9
10
11
12
13
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 8

def slog(obj,msg=nil)
  raise "No @value" if not obj.respond_to?(:value)
  raise "No @size"  if not obj.respond_to?(:size)
  return nil if !self.debug
  putd "#{@stack.size.to_s(16).rjust(8,"0")} [0x#{obj.opcode.to_s(16)}] #{obj.class}: #{msg}"
end

#throw(t = nil) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 148

def throw(t=nil)
  puts t.class
  STDERR.puts "==== Error at Type 0x#{t.to_s(16).rjust(2,"0")}"
  STDERR.puts "offset: #{self.offset}"
  STDERR.puts "left:   #{@buf.size}"
  STDERR.puts @buf.read(32).hexdump
end

#write_7bit_encoded_int(val) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 24

def write_7bit_encoded_int(val)
  s = ""
  while (val >= 0x80) do
    s << [val | 0x80].pack("C")
    val = val >> 7
  end
  s << [val].pack("C")
  return s
end

#write_double(val) ⇒ Object



96
97
98
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 96

def write_double(val)
  [val].pack("Q")
end

#write_int(val) ⇒ Object Also known as: write_byte



69
70
71
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 69

def write_int(val)
  [val].pack("C")
end

#write_int32(val) ⇒ Object Also known as: write_single



87
88
89
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 87

def write_int32(val)
  [val].pack("I")
end

#write_short(val) ⇒ Object



78
79
80
# File 'lib/wwmd/viewstate/viewstate_utils.rb', line 78

def write_short(val)
  [val].pack("n")
end