Sha256: 02fddc903217300198dc93a3edf57f7fd811fd1f1dc426d29a6a3ac2d4e2ef35

Contents?: true

Size: 1.88 KB

Versions: 43

Compression:

Stored size: 1.88 KB

Contents

#!/usr/bin/env ruby

# Rex::Struct2
module Rex
module Struct2

class SStruct

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

	attr_reader  :leftover, :elements
	attr_writer  :leftover, :elements

	private :elements, :elements=

	# watch out!, leftover returns our copy of the string!  so don't do
	# anything stupid like struct.leftover.slice! !!

	def initialize(*opts)
		self.elements = [ ]
		self.add_element(*opts)
	end


	def reset
		elements.each {|e| e.reset}
		return self
	end

	def add_element(*objs)
		objs.each { |o|
			elements << o
			o.container = self
		}
		return self
	end

	def <<(obj)
		self.add_element(obj)
	end

	def to_s
		# !!! what do we do on mix restraint issues? just fail?
		# maybe throw an exception, because that is most likely
		# a usage error

		buff = ""
		elements.each do |e|
			buff << e.to_s
		end
		
		if restraint && restraint.max
			return buff.slice(0, restraint.max)
		else
			return buff
		end
	end

	def length
		return elements.length
	end

	def [](obj)
		return elements[obj]
	end

	def each(&block)
		return elements.each(&block)
	end

	def from_s(obytes)
		# make my own copy so I can chop it up
		bytes = obytes.dup
		length = 0

		# I don't think we should call update_restraint here, but
		# I could have mis thought or something

		# if we have a restraint (and if there is a val) truncate
		if restraint
			max = restraint.max
			bytes = bytes.slice(0, max) if max
		end

		elements.each { |e|
			used = e.from_s(bytes)
			return if !used
			bytes.slice!(0, used)
			length += used
		}

		# make sure we matched out min restraint, else return failure
		if restraint
			min = restraint.min
			return if min && length < min
		end

		# I guess this is me getting "set", so I should have a value
		# and I should update my restraints on set
		self.value = obytes.slice(0, length)

		self.leftover = bytes
		return(length)
	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/s_struct.rb
librex-0.0.63 lib/rex/struct2/s_struct.rb
librex-0.0.54 lib/rex/struct2/s_struct.rb
librex-0.0.53 lib/rex/struct2/s_struct.rb
librex-0.0.52 lib/rex/struct2/s_struct.rb
librex-0.0.51 lib/rex/struct2/s_struct.rb
librex-0.0.50 lib/rex/struct2/s_struct.rb
librex-0.0.49 lib/rex/struct2/s_struct.rb
librex-0.0.48 lib/rex/struct2/s_struct.rb
librex-0.0.47 lib/rex/struct2/s_struct.rb
librex-0.0.46 lib/rex/struct2/s_struct.rb
librex-0.0.44 lib/rex/struct2/s_struct.rb
librex-0.0.43 lib/rex/struct2/s_struct.rb
librex-0.0.42 lib/rex/struct2/s_struct.rb
librex-0.0.41 lib/rex/struct2/s_struct.rb
librex-0.0.40 lib/rex/struct2/s_struct.rb
librex-0.0.39 lib/rex/struct2/s_struct.rb
librex-0.0.38 lib/rex/struct2/s_struct.rb
librex-0.0.37 lib/rex/struct2/s_struct.rb
librex-0.0.36 lib/rex/struct2/s_struct.rb