-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
buffer.py
191 lines (159 loc) · 4.41 KB
/
buffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from __future__ import absolute_import
from __future__ import division
from pwnlib.context import context
class Buffer(object):
"""
List of strings with some helper routines.
Example:
>>> b = Buffer()
>>> b.add(b"A" * 10)
>>> b.add(b"B" * 10)
>>> len(b)
20
>>> b.get(1)
b'A'
>>> len(b)
19
>>> b.get(9999)
b'AAAAAAAAABBBBBBBBBB'
>>> len(b)
0
>>> b.get(1)
b''
Implementation Details:
Implemented as a list. Strings are added onto the end.
The ``0th`` item in the buffer is the oldest item, and
will be received first.
"""
def __init__(self, buffer_fill_size = None):
self.data = [] # Buffer
self.size = 0 # Length
self.buffer_fill_size = buffer_fill_size
def __len__(self):
"""
>>> b = Buffer()
>>> b.add(b'lol')
>>> len(b) == 3
True
>>> b.add(b'foobar')
>>> len(b) == 9
True
"""
return self.size
def __nonzero__(self):
return len(self) > 0
def __contains__(self, x):
"""
>>> b = Buffer()
>>> b.add(b'asdf')
>>> b'x' in b
False
>>> b.add(b'x')
>>> b'x' in b
True
"""
for b in self.data:
if x in b:
return True
return False
def index(self, x):
"""
>>> b = Buffer()
>>> b.add(b'asdf')
>>> b.add(b'qwert')
>>> b.index(b't') == len(b) - 1
True
"""
sofar = 0
for b in self.data:
if x in b:
return sofar + b.index(x)
sofar += len(b)
raise IndexError()
def add(self, data):
"""
Adds data to the buffer.
Arguments:
data(str,Buffer): Data to add
"""
# Fast path for ''
if not data: return
if isinstance(data, Buffer):
self.size += data.size
self.data += data.data
else:
self.size += len(data)
self.data.append(data)
def unget(self, data):
"""
Places data at the front of the buffer.
Arguments:
data(str,Buffer): Data to place at the beginning of the buffer.
Example:
>>> b = Buffer()
>>> b.add(b"hello")
>>> b.add(b"world")
>>> b.get(5)
b'hello'
>>> b.unget(b"goodbye")
>>> b.get()
b'goodbyeworld'
"""
if isinstance(data, Buffer):
self.data = data.data + self.data
self.size += data.size
else:
self.data.insert(0, data)
self.size += len(data)
def get(self, want=float('inf')):
"""
Retrieves bytes from the buffer.
Arguments:
want(int): Maximum number of bytes to fetch
Returns:
Data as string
Example:
>>> b = Buffer()
>>> b.add(b'hello')
>>> b.add(b'world')
>>> b.get(1)
b'h'
>>> b.get()
b'elloworld'
"""
# Fast path, get all of the data
if want >= self.size:
data = b''.join(self.data)
self.size = 0
self.data = []
return data
# Slow path, find the correct-index chunk
have = 0
i = 0
while want >= have:
have += len(self.data[i])
i += 1
# Join the chunks, evict from the buffer
data = b''.join(self.data[:i])
self.data = self.data[i:]
# If the last chunk puts us over the limit,
# stick the extra back at the beginning.
if have > want:
extra = data[want:]
data = data[:want]
self.data.insert(0, extra)
# Size update
self.size -= len(data)
return data
def get_fill_size(self, size=None):
"""
Retrieves the default fill size for this buffer class.
Arguments:
size (int): (Optional) If set and not None, returns the size variable back.
Returns:
Fill size as integer if size is None, else size.
"""
if size is None:
size = self.buffer_fill_size
with context.local(buffer_size=size):
return context.buffer_size