class Packetman::Filter
Attributes
input[RW]
Public Class Methods
bit_density(radix=config.radix)
click to toggle source
# File lib/packetman/filter.rb, line 21 def self.bit_density(radix=config.radix) (radix.nil?) ? 8 : Math.log2(radix).ceil end
bit_length(num)
click to toggle source
# File lib/packetman/filter.rb, line 12 def self.bit_length(num) case num when /^0x/ $'.length * bit_density(16) else nil end end
new(input) { |config| ... }
click to toggle source
# File lib/packetman/filter.rb, line 7 def initialize(input) self.input = input yield config if block_given? end
Public Instance Methods
bin_chr(chr)
click to toggle source
Converts the `chr` from `config.radix` to binary, substituting wildcards as necessary
@param chr [String] character to convert to binary @return [String] binary string
# File lib/packetman/filter.rb, line 58 def bin_chr(chr) chr = '0' if chr == config.wildcard if config.radix raise "invalid character '#{chr}' for radix=#{config.radix}" if chr.downcase != chr.to_i(config.radix).to_s(config.radix).downcase chr.to_i(config.radix) else chr.ord end.to_s(2).rjust(self.class.bit_density, '0') end
clauses()
click to toggle source
# File lib/packetman/filter.rb, line 86 def clauses start_bit = 0 [].tap do |filter| search_hex.zip(mask_hex).each do |search, mask| filter << Packetman::Clause.new(search, mask, start_bit) start_bit += self.class.bit_length(search) end end end
hex_encode(bin_str)
click to toggle source
Transform bin_str to array of 32, 16, and 8 bit hex encoded strings
# File lib/packetman/filter.rb, line 78 def hex_encode(bin_str) bin_str.reverse.scan(/.{1,4}/).map{ |chunk| chunk.reverse.to_i(2).to_s(16) }.reverse.join.scan(/.{8}|.{4}|.{2}/).map{ |hex| hex.prepend('0x') } end
map_chr() { |chr| ... }
click to toggle source
# File lib/packetman/filter.rb, line 25 def map_chr shift_and_pad(input.scan(/./).map{ |chr| yield chr }.join) end
mask_chr(chr)
click to toggle source
Mask string for chr substituting wildcards as necessary
# File lib/packetman/filter.rb, line 46 def mask_chr(chr) if chr == config.wildcard 0 else radix_mask end.to_s(2).rjust(self.class.bit_density, '0') end
mask_hex()
click to toggle source
# File lib/packetman/filter.rb, line 69 def mask_hex hex_encode(map_chr{ |c| mask_chr(c) }) end
radix_mask()
click to toggle source
Mask for 1 character of current radix
# File lib/packetman/filter.rb, line 41 def radix_mask ("1"*self.class.bit_density).to_i(2) end
search_hex()
click to toggle source
# File lib/packetman/filter.rb, line 73 def search_hex hex_encode(map_chr{ |c| bin_chr(c) }) end
shift_and_pad(bin_str)
click to toggle source
# File lib/packetman/filter.rb, line 29 def shift_and_pad(bin_str) #shift bin_str.ljust(target_bit_length, '0'). #pad rjust(target_bit_length + config.offset_bits % 8, '0') end
target_bit_length()
click to toggle source
# File lib/packetman/filter.rb, line 36 def target_bit_length ((input.length*self.class.bit_density + config.offset_bits)/8.to_f).ceil*8 - config.offset_bits end
to_s()
click to toggle source
# File lib/packetman/filter.rb, line 96 def to_s clauses.map{ |clause| clause.to_s }.join(' && ') end