-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.rb
272 lines (256 loc) · 6.34 KB
/
actions.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
268
269
270
271
272
# Copyright (c) 2010, Nephi Johnson
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
# * Neither the name of Funder nor the names of its contributors may be used to
# endorse or promote products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
require 'funder'
require 'fields'
class Action
attr_accessor :parent, :block, :fields, :owner
def initialize(block=nil, do_it_once_str=nil)
@block = block
@do_it_once_str = do_it_once_str
@owner = nil
end
def get_fields
if @do_it_once_str
if @do_it_once_str.kind_of? Proc
[Field.new(nil, (instance_eval &(@do_it_once_str)), nil)]
elsif @do_it_once_str.kind_of? String
[Field.new(nil, @do_it_once_str, nil)]
else
raise "Unknown type of do_it_once_str"
end
elsif @block
@parent.instance_eval &block
elsif @owner
[@owner]
end
end
def self.action
raise "this should be implemented by inherited classes"
end
def do_it
raise "this should be done by inherited classes"
end
def do_it_once(str)
# need to do some fu here so it works as an action with a section
self.class.class_eval { alias :real_get_fields :get_fields }
self.class.send(:define_method, :get_fields) { [Field.new(nil,str,nil)] }
result = do_it
self.class.class_eval { alias :get_fields :real_get_fields }
result
end
def desc
"Action:#{self.class.to_s}"
end
def inspect(level=0)
@fields ||= get_fields() || []
fields_str = @fields.map do |field|
field.name
end.join(", ")
if level == 0
return "#<#{desc} fields=[#{fields_str}]>"
elsif level == 1
return "#{desc}(#{fields_str})"
elsif level == 2
return "#{desc}(#{fields_str})"
else
return ""
end
end
end
class ReversibleAction < Action
def self.reverse_it(data)
raise "this should be implemented by inherited classes"
end
end
class Length < Action
def self.action(data)
data.length
end
def do_it
Length.action(get_fields.map{|f| f.to_out }.join)
end
end
class Crc32 < Action
def self.action(data)
require 'zlib'
Zlib::crc32(data)
end
def do_it
res = get_fields.map{|f|f.to_out}.join
Crc32.action(res)
end
end
class Reverse < ReversibleAction
def self.action(data)
data.reverse
end
def self.reverse_it(data)
data.reverse
end
def do_it
Reverse.action(get_fields.map{|f| f.to_out }.join)
end
end
class ZlibDeflate < ReversibleAction
def self.action(data)
require 'zlib'
Zlib::Deflate.deflate(data)
end
def self.reverse_it(data)
require 'zlib'
Zlib::Inflate.inflate(data)
end
def do_it
ZlibDeflate.action(get_fields.map{|f| f.to_out }.join)
end
end
class Base64Encode < ReversibleAction
def self.reverse_it(data)
require 'base64'
Base64.decode64(data)
end
def self.action(data)
require 'base64'
# need the chomp b/c it has an extra \n
Base64.encode64(data).chomp
end
def do_it
Base64Encode.action(get_fields.map{|f| f.to_out }.join)
end
end
class Unicode < ReversibleAction
def self.action(data)
res = ""
data.each_byte do |b|
res << b.chr + "\x00"
end
res
end
def self.reverse_it(data)
res = ""
tmp = data.clone
while tmp.length > 0
char, null = tmp.unpack("ac")
return data if null != 0 || null == ""
res << char
end
res
end
def do_it
Unicode.action(get_fields.map{|f| f.to_out }.join)
end
end
class NullTerminate < ReversibleAction
def self.action(data)
data + "\x00"
end
def self.reverse_it(data)
return data.slice(0, data.length - 1) if data[-1, 1] == "\x00"
data
end
def do_it
NullTerminate.action(get_fields.map{|f| f.to_out }.join)
end
end
class Offset < Action
def do_it
fields = get_fields
raise "this action can only be done on _one_ field" if fields.length > 1
fields[0].offset
end
end
#Default Action
class DA < Action
def do_it
get_fields.map{|f| f.to_out }.join
end
end
class CustomAction < Action
attr_accessor :custom_proc
def initialize(custom_proc, block=nil, do_it_once_str=nil)
super(block, do_it_once_str)
@custom_proc = custom_proc
end
def do_it
@custom_proc.call(get_fields)
end
end
# ---------------- CONDITIONAL ACTIONS ----------------------
class ConditionalAction < Action
def initialize(*args)
@ifs = []
@else = nil
i = 0
raise "need at least to args for this to work!" if args.length < 2
while i < args.length
if i == args.length - 1
@else = args[i]
@else.parent = @parent
i += 1
else
args[i+1].parent = @parent
@ifs << [args[i], args[i+1]]
i += 2
end
end
end
def parent=(val)
super(val)
@ifs.each do |iif, tthen|
tthen.parent = val
end
@else.parent = val
end
def do_it_once(val)
resolve_it(:do_it_once, val)
end
def do_it
resolve_it(:do_it)
end
def resolve_it(method, *args)
res = nil
counter = 0
@ifs.each do |iif, tthen|
if @parent.instance_eval &iif
return tthen.send(method, *args)
end
end
return @else.send(method, *args) if @else
""
end
def inspect(level=0)
if level == 0
return "#<CondAction:#{self.class.to_s}>"
elsif level == 1
return "CondAction:#{self.class.to_s}"
elsif level == 2
return "CondAction:#{self.class.to_s}"
else
return ""
end
end
end
class If < ConditionalAction
end