Sha256: bcb041cf30853baf18d3f9e29d5f152cc6651ca72e0bbfeb94cf1fa608bbde96

Contents?: true

Size: 1.32 KB

Versions: 21

Compression:

Stored size: 1.32 KB

Contents

# -*- coding: binary -*-

# 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

21 entries across 21 versions & 4 rubygems

Version Path
rex-struct2-0.1.5 lib/rex/struct2/s_string.rb
rex-struct2-0.1.4 lib/rex/struct2/s_string.rb
rex-struct2-0.1.3 lib/rex/struct2/s_string.rb
rex-2.0.13 lib/rex/struct2/s_string.rb
rex-2.0.12 lib/rex/struct2/s_string.rb
rex-2.0.11 lib/rex/struct2/s_string.rb
rex-struct2-0.1.2 lib/rex/struct2/s_string.rb
rex-struct2-0.1.1 lib/rex/struct2/s_string.rb
rex-struct2-0.1.0 lib/rex/struct2/s_string.rb
rex-2.0.10 lib/rex/struct2/s_string.rb
rex-2.0.9 lib/rex/struct2/s_string.rb
rex-2.0.8 lib/rex/struct2/s_string.rb
rex-2.0.7 lib/rex/struct2/s_string.rb
rex-2.0.5 lib/rex/struct2/s_string.rb
rex-2.0.4 lib/rex/struct2/s_string.rb
dstruct-0.0.1 lib/rex/struct2/s_string.rb
rex-2.0.3 lib/rex/struct2/s_string.rb
librex-0.0.999 lib/rex/struct2/s_string.rb
rex-2.0.2 lib/rex/struct2/s_string.rb
librex-0.0.71 lib/rex/struct2/s_string.rb