Class: Pwnlib::Tubes::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/pwnlib/tubes/buffer.rb

Overview

Buffer that support deque-like string operations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuffer

Instantiate a Pwnlib::Tubes::Buffer object.



9
10
11
12
# File 'lib/pwnlib/tubes/buffer.rb', line 9

def initialize
  @data = []
  @size = 0
end

Instance Attribute Details

#sizeObject (readonly) Also known as: length

Returns the value of attribute size.



14
15
16
# File 'lib/pwnlib/tubes/buffer.rb', line 14

def size
  @size
end

Instance Method Details

#add(data) ⇒ Object Also known as: <<

Adds data to the buffer.

Parameters:

  • data (String)

    Data to add.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pwnlib/tubes/buffer.rb', line 32

def add(data)
  case data
  when Buffer
    @data.concat(data.data)
  else
    data = data.to_s.dup
    return if data.empty?

    @data << data
  end
  @size += data.size
  self
end

#empty?Boalean

Check whether the buffer is empty.

Returns:

  • (Boalean)

    Returns true if contains no elements.



21
22
23
# File 'lib/pwnlib/tubes/buffer.rb', line 21

def empty?
  size.zero?
end

#get(n = nil) ⇒ String

Retrieves bytes from the buffer.

Parameters:

  • n (Integer?) (defaults to: nil)

    Maximum number of bytes to fetch.

Returns:

  • (String)

    Data as string.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pwnlib/tubes/buffer.rb', line 72

def get(n = nil)
  if n.nil? || n >= size
    data = @data.join
    @size = 0
    @data = []
  else
    have = 0
    idx = 0
    while have < n
      have += @data[idx].size
      idx += 1
    end
    data = @data.slice!(0...idx).join
    if have > n
      extra = data.slice!(n..-1)
      @data.unshift(extra)
    end
    @size -= n
  end
  data
end

#unget(data) ⇒ Object

Places data at the front of the buffer.

Parameters:

  • data (String)

    Data to place at the beginning of the buffer.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pwnlib/tubes/buffer.rb', line 51

def unget(data)
  case data
  when Buffer
    @data.unshift(*data.data)
  else
    data = data.to_s.dup
    return if data.empty?

    @data.unshift(data)
  end
  @size += data.size
  self
end