2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-03-26 16:52:59 +00:00
|
|
|
|
|
|
|
# Rex::Struct2
|
|
|
|
module Rex
|
|
|
|
module Struct2
|
|
|
|
|
|
|
|
class Generic
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/struct2/element'
|
2005-03-26 16:52:59 +00:00
|
|
|
include Rex::Struct2::Element
|
|
|
|
|
|
|
|
attr_reader :default
|
|
|
|
attr_writer :default
|
|
|
|
|
2005-09-05 07:21:31 +00:00
|
|
|
attr_accessor :mask, :check_mask
|
|
|
|
|
|
|
|
def initialize(packspec, signed=false, default=nil)
|
2005-03-26 16:52:59 +00:00
|
|
|
@packspec = packspec
|
|
|
|
@default = default
|
2005-09-05 07:21:31 +00:00
|
|
|
|
|
|
|
bytelen = [ -1 ].pack(@packspec).length
|
|
|
|
self.mask = (1 << (8 * bytelen)) - 1
|
|
|
|
|
|
|
|
if signed
|
|
|
|
self.check_mask = 1 << (8 * bytelen - 1)
|
|
|
|
else
|
|
|
|
self.check_mask = 0
|
|
|
|
end
|
|
|
|
|
2005-03-26 16:52:59 +00:00
|
|
|
reset()
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
self.value = @default
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
# I realize this will bomb out if this isn't an integer, for
|
|
|
|
# example if it is nil. That should only happen for a user
|
|
|
|
# error so that's what I want it to do...
|
|
|
|
string = [ @value ].pack(@packspec)
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2005-03-26 16:52:59 +00:00
|
|
|
if restraint && restraint.max
|
|
|
|
return string.slice(0, restraint.max)
|
|
|
|
else
|
|
|
|
return string
|
|
|
|
end
|
|
|
|
# what to do for restraint.min?!?
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_s(bytes)
|
|
|
|
value = bytes.unpack(@packspec)[0]
|
|
|
|
# return nil on unpack error
|
|
|
|
return if !value
|
|
|
|
len = slength()
|
|
|
|
# error on any restraint issues
|
|
|
|
return if restraint && restraint.max && len > restraint.max
|
|
|
|
return if restraint && restraint.min && len < restraint.min
|
|
|
|
# else set our value and return length used for this element
|
2005-09-05 07:21:31 +00:00
|
|
|
|
|
|
|
if (value & check_mask) != 0
|
|
|
|
value = -((~value & mask) + 1)
|
|
|
|
end
|
|
|
|
|
2005-03-26 16:52:59 +00:00
|
|
|
self.value = value
|
|
|
|
return(len)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# end Rex::Struct2
|
|
|
|
end
|
2009-06-02 23:36:58 +00:00
|
|
|
end
|