Class: Arcana::Pattern

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value) ⇒ Pattern

Returns a new instance of Pattern.



133
134
135
136
137
# File 'lib/arcana.rb', line 133

def initialize(type, value)
  type, *@flags = type.split("/")
  @type, *@type_ops = type.split(/(?=[&%+-])/)
  @value = value
end

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



131
132
133
# File 'lib/arcana.rb', line 131

def flags
  @flags
end

#typeObject (readonly)

Returns the value of attribute type.



131
132
133
# File 'lib/arcana.rb', line 131

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



131
132
133
# File 'lib/arcana.rb', line 131

def value
  @value
end

Instance Method Details

#match?(input) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/arcana.rb', line 139

def match?(input)
  return true if @value == "x"

  return if !input
  return if input.eof?
  flags = @flags.dup

  case @type
  when "string", "ustring"
    flags.delete("b") # force on binary files
    flags.delete("t") # force on text files 

    flags.delete("w") # FIXME: blanks
    flags.delete("W") # FIXME: blanks
    flags.delete("c") # FIXME: case insensitive
    flags.delete("C") # FIXME: case insensitive

    if @value.start_with?("!")
      test_string = parse_string(@value[1..])
      input.read(test_string.length) != test_string
    elsif @value.start_with?("=")
      test_string = parse_string(@value[1..])
      input.read(test_string.length) == test_string
    else
      test_string = parse_string(@value)
      input.read(test_string.length) == test_string
    end
  when "byte"
    match_packed_integer?(input, "c", 1)
  when "ubyte"
    match_packed_integer?(input, "C", 1)
  when "short"
    match_packed_integer?(input, "s", 2)
  when "ushort"
    match_packed_integer?(input, "S", 2)
  when "long"
    match_packed_integer?(input, "l", 4)
  when "ulong"
    match_packed_integer?(input, "L", 4)
  when "quad"
    match_packed_integer?(input, "q", 8)
  when "uquad"
    match_packed_integer?(input, "Q", 8)
  when "leshort"
    match_packed_integer?(input, "s<", 2)
  when "uleshort"
    match_packed_integer?(input, "S<", 2)
  when "beshort"
    match_packed_integer?(input, "s>", 2)
  when "ubeshort"
    match_packed_integer?(input, "S>", 2)
  when "lelong"
    match_packed_integer?(input, "l<", 4)
  when "ulelong"
    match_packed_integer?(input, "L<", 4)
  when "belong"
    match_packed_integer?(input, "l>", 4)
  when "ubelong"
    match_packed_integer?(input, "L>", 4)
  when "bequad"
    match_packed_integer?(input, "q>", 8)
  when "ubequad"
    match_packed_integer?(input, "Q>", 8)
  when "lequad"
    match_packed_integer?(input, "q<", 8)
  when "ulequad"
    match_packed_integer?(input, "Q<", 8)
  when "pstring"
    return false # FIXME
  when "guid"
    return false # FIXME
  when "der"
    return false # FIXME
  when "lestring16"
    return false # FIXME
  when "default"
    return true # FIXME
  when "clear"
    return true # FIXME
  when "name"
    return false
  when "use"
    return false
  when "offset"
    match_integer?(input.offset)
  when "indirect"
    return false # FIXME
  when "ledate"
    return false # FIXME
  when "bedate"
    return false # FIXME
  when "beldate"
    return false # FIXME
  when "beqdate"
    return false # FIXME
  when "lefloat"
    return false # FIXME
  when "regex"
    if length = flags[0]
      if length.end_with?("l")
        # lines
        length = 8196
      elsif length.match?(/\A[0-9]+\z/)
        length = Integer(length)
      else
        return false # FIXME
      end
    else
      length = 8196
    end
    regex = parse_string(@value)
    # FIXME: seek input to result location
    input.peek(length).match?(regex)
  when "search"
    flags = @flags

    flags.delete("b") # force on binary files
    flags.delete("t") # force on text files 

    flags.delete("c") # FIXME: case insensitive
    flags.delete("C") # FIXME: case insensitive

    flags = ["1"] if flags.empty? # FIXME: WTF?
    search_input = input.peek(@value.size + Integer(flags[0]) - 1)
    flags = flags[1..]

    value = parse_string(@value)

    # FIXME: seek input to result location
    search_input.include?(value)
  else
    raise "Unsupported match type: #{@type}"
  end
end