Sha256: 82a704d91f688d7484b65ab596ad1b35fa59b8f6b65e3cc6da2cda1fe3104b67

Contents?: true

Size: 1.38 KB

Versions: 43

Compression:

Stored size: 1.38 KB

Contents

#!/usr/bin/env ruby

# Rex::Struct2
module Rex
module Struct2

class Generic
	
	require 'rex/struct2/element'
	include Rex::Struct2::Element

	attr_reader  :default
	attr_writer  :default

	attr_accessor :mask, :check_mask

	def initialize(packspec, signed=false, default=nil)
		@packspec = packspec
		@default  = default

		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

		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)
		
		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

		if (value & check_mask) != 0
			value = -((~value & mask) + 1)
		end

		self.value = value
		return(len)
	end

end

# end Rex::Struct2
end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
librex-0.0.65 lib/rex/struct2/generic.rb
librex-0.0.63 lib/rex/struct2/generic.rb
librex-0.0.54 lib/rex/struct2/generic.rb
librex-0.0.53 lib/rex/struct2/generic.rb
librex-0.0.52 lib/rex/struct2/generic.rb
librex-0.0.51 lib/rex/struct2/generic.rb
librex-0.0.50 lib/rex/struct2/generic.rb
librex-0.0.49 lib/rex/struct2/generic.rb
librex-0.0.48 lib/rex/struct2/generic.rb
librex-0.0.47 lib/rex/struct2/generic.rb
librex-0.0.46 lib/rex/struct2/generic.rb
librex-0.0.44 lib/rex/struct2/generic.rb
librex-0.0.43 lib/rex/struct2/generic.rb
librex-0.0.42 lib/rex/struct2/generic.rb
librex-0.0.41 lib/rex/struct2/generic.rb
librex-0.0.40 lib/rex/struct2/generic.rb
librex-0.0.39 lib/rex/struct2/generic.rb
librex-0.0.38 lib/rex/struct2/generic.rb
librex-0.0.37 lib/rex/struct2/generic.rb
librex-0.0.36 lib/rex/struct2/generic.rb