Sha256: 9d522aa3c74925e41962bb209db20f06297ae236db001e666ad8e63b216cc627

Contents?: true

Size: 1.23 KB

Versions: 43

Compression:

Stored size: 1.23 KB

Contents

#!/usr/bin/env ruby

# Rex::Struct2
module Rex
module Struct2

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

	attr_reader  :size, :default, :pad
	attr_writer  :default, :pad

	def initialize(size=nil, default=nil, pad=nil)
		self.size = size
		@default  = default
		@pad      = pad
		reset()
	end

	def size=(newsize)
		if !newsize
			self.restraint = nil
		else
			res = Rex::Struct2::Constant.new(newsize)
			self.restraint = Rex::Struct2::Restraint.new(res, res, false)
		end
	end

	def reset
		self.value = @default
	end

	def to_s
		string = self.value

		return if !string

		# pad if short
		if restraint && restraint.min && self.pad && restraint.min > string.length
			string += self.pad * (restraint.min - string.length)
		end
		# truncate if long
		if restraint && restraint.max
			string = string.slice(0, restraint.max)
		end

		return string
	end

	def from_s(bytes)
		# we don't have enough bytes to satisfy our minimum
		if restraint && restraint.min && bytes.length < restraint.min
			return
		end

		if restraint && restraint.max
			self.value = bytes.slice(0, restraint.max)
		else
			self.value = bytes.dup
		end


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