Class: Pwnlib::Tubes::Buffer
- Inherits:
-
Object
- Object
- Pwnlib::Tubes::Buffer
- Defined in:
- lib/pwnlib/tubes/buffer.rb
Overview
Buffer that support deque-like string operations.
Instance Attribute Summary collapse
-
#size ⇒ Object
(also: #length)
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#add(data) ⇒ Object
(also: #<<)
Adds data to the buffer.
-
#empty? ⇒ Boalean
Check whether the buffer is empty.
-
#get(n = nil) ⇒ String
Retrieves bytes from the buffer.
-
#initialize ⇒ Buffer
constructor
Instantiate a Buffer object.
-
#unget(data) ⇒ Object
Places data at the front of the buffer.
Constructor Details
#initialize ⇒ Buffer
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
#size ⇒ Object (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.
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.
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.
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.
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 |