-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonkey_patch.rb
executable file
·267 lines (238 loc) · 4.88 KB
/
monkey_patch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
106
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
137
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
class Integer
def to_bin(addr_size, is_little)
case addr_size
when 1
to_bin8
when 2
to_bin16(is_little)
when 4
to_bin32(is_little)
when 8
to_bin64(is_little)
else
raise "unexpected addr size"
end
end
def to_bin8_ary
ary = []
if self < 0xFF
ary.push(0x00)
ary.push(self)
else
throw "Unexpected Integer value #{self}"
end
ary
end
def to_bin8
ary = to_bin8_ary
ary.pack("C*")
end
def to_bin16_ary(is_little=true)
ary = []
if self < 0xFF
ary.push(0x00)
ary.push(self)
elsif self < 0xFFFF
tmp = (self & 0xFF00) >> 8
ary.push(tmp)
tmp = (self & 0x00FF)
ary.push(tmp)
else
throw "Unexpected Integer value #{self}"
end
ary.reverse! if is_little
ary
end
def to_bin16(is_little=true)
ary = to_bin16_ary(is_little)
ary.pack("C*")
end
def to_bin32_ary(is_little=true)
ary = []
if self < 0xFF
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
ary.push(self)
elsif self < 0xFFFF
ary.push(0x00)
ary.push(0x00)
tmp = (self & 0xFF00) >> 8
ary.push(tmp)
tmp = (self & 0x00FF)
ary.push(tmp)
elsif self < 0xFFFFFFFF
tmp = (self & 0xFF000000) >> 24
ary.push(tmp)
tmp = (self & 0x00FF0000) >> 16
ary.push(tmp)
tmp = (self & 0x0000FF00) >> 8
ary.push(tmp)
tmp = (self & 0x000000FF)
ary.push(tmp)
else
throw "Unexpected Integer value #{self}"
end
ary.reverse! if is_little
ary
end
def to_bin32(is_little = true)
ary = to_bin32_ary(is_little)
ary.pack("C*")
end
def to_bin64_ary(is_little = true)
ary = []
if self < 0xFF
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
ary.push(self)
elsif self < 0xFFFF
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
tmp = (self & 0xFF00) >> 8
ary.push(tmp)
tmp = (self & 0x00FF)
ary.push(tmp)
elsif self < 0xFFFFFFFF
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
ary.push(0x00)
tmp = (self & 0xFF000000) >> 24
ary.push(tmp)
tmp = (self & 0x00FF0000) >> 16
ary.push(tmp)
tmp = (self & 0x0000FF00) >> 8
ary.push(tmp)
tmp = (self & 0x000000FF)
ary.push(tmp)
elsif self < 0xFFFFFFFFFFFFFFFF
tmp = (self & 0xFF00000000000000) >> 56
ary.push(tmp)
tmp = (self & 0x00FF000000000000) >> 48
ary.push(tmp)
tmp = (self & 0x0000FF0000000000) >> 40
ary.push(tmp)
tmp = (self & 0x000000FF00000000) >> 32
ary.push(tmp)
tmp = (self & 0x00000000FF000000) >> 24
ary.push(tmp)
tmp = (self & 0x0000000000FF0000) >> 16
ary.push(tmp)
tmp = (self & 0x000000000000FF00) >> 8
ary.push(tmp)
tmp = (self & 0x00000000000000FF)
ary.push(tmp)
else
throw "Unexpected Integer value #{self}"
end
ary.reverse! if is_little
ary
end
def to_bin64(is_little=true)
ary = to_bin64_ary(is_little)
ary.pack("C*")
end
def to_h(prefix=true, capital=true)
prefix_str = ""
prefix_str = "0x" if prefix
format_str = ""
if capital
format_str += "X"
else
format_str += "x"
end
if self < 0xFF
sprintf("#{prefix_str}%02#{format_str}", self)
elsif self < 0xFFFF
sprintf("#{prefix_str}%04#{format_str}", self)
elsif self < 0xFFFFFFFF
sprintf("#{prefix_str}%08#{format_str}", self)
elsif self < 0xFFFFFFFFFFFFFFFF
sprintf("#{prefix_str}%16#{format_str}", self)
else
# TODO
puts "error..."
end
end
end
class Array
def c_str(offset = 0)
str = ""
while offset < self.length
break if self[offset] == 0
str << self[offset].chr
offset += 1
end
str
end
def hex_dump(prefix=true, capital=true)
dump_str = ""
self.each_with_index do |val, i|
dump_str += "#{val.to_h(prefix, capital)} "
dump_str += "\n" if i % 16 == 15
end
dump_str
end
def to_i(is_little=true)
case self.length
when 8
to_int64(is_little)
when 4
to_int32(is_little)
when 2
to_int16(is_little)
when 1
self[0].to_i
else
# TODO exception...
end
end
# int(64bit)
def to_int64(is_little=true)
if is_little
self[0].to_i +
(self[1].to_i << 8) +
(self[2].to_i << 16) +
(self[3].to_i << 24) +
(self[4].to_i << 32) +
(self[5].to_i << 40) +
(self[6].to_i << 48) +
(self[7].to_i << 56)
else
(self[7].to_i << 56) +
(self[6].to_i << 48) +
(self[5].to_i << 40) +
(self[4].to_i << 32) +
(self[3].to_i << 24) +
(self[2].to_i << 16) +
(self[1].to_i << 8) +
(self[0].to_i)
end
end
# int(32bit)
def to_int32(is_little=true)
if is_little
self[0].to_i + (self[1].to_i << 8) + (self[2].to_i << 16) + (self[3].to_i << 24)
else
(self[0].to_i << 24) + (self[1].to_i << 16) + (self[2].to_i << 8) + (self[3].to_i)
end
end
# short(16bit)
def to_int16(is_little=true)
if is_little
self[0].to_i + (self[1].to_i << 8)
else
self[1].to_i + (self[0].to_i << 8)
end
end
end