• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Gallopsled / pwntools / 1

29 May 2020 09:05AM UTC coverage: 0.0% (-72.4%) from 72.414%
1

push

github

layderv
__str__ to __bytes__ in python2

0 of 8 new or added lines in 2 files covered. (0.0%)

11301 existing lines in 133 files now uncovered.

0 of 15497 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pwnlib/tubes/buffer.py
1
#!/usr/bin/env python2
UNCOV
2
from __future__ import absolute_import
×
UNCOV
3
from __future__ import division
×
4

UNCOV
5
from pwnlib.context import context
×
6

7

UNCOV
8
class Buffer(Exception):
×
9
    """
10
    List of strings with some helper routines.
11

12
    Example:
13

14
        >>> b = Buffer()
15
        >>> b.add(b"A" * 10)
16
        >>> b.add(b"B" * 10)
17
        >>> len(b)
18
        20
19
        >>> b.get(1)
20
        b'A'
21
        >>> len(b)
22
        19
23
        >>> b.get(9999)
24
        b'AAAAAAAAABBBBBBBBBB'
25
        >>> len(b)
26
        0
27
        >>> b.get(1)
28
        b''
29

30
    Implementation Details:
31

32
        Implemented as a list.  Strings are added onto the end.
33
        The ``0th`` item in the buffer is the oldest item, and
34
        will be received first.
35
    """
UNCOV
36
    def __init__(self, buffer_fill_size = None):
×
UNCOV
37
        self.data = [] # Buffer
×
UNCOV
38
        self.size = 0  # Length
×
UNCOV
39
        self.buffer_fill_size = buffer_fill_size
×
40

UNCOV
41
    def __len__(self):
×
42
        """
43
        >>> b = Buffer()
44
        >>> b.add(b'lol')
45
        >>> len(b) == 3
46
        True
47
        >>> b.add(b'foobar')
48
        >>> len(b) == 9
49
        True
50
        """
UNCOV
51
        return self.size
×
52

UNCOV
53
    def __nonzero__(self):
×
UNCOV
54
        return len(self) > 0
×
55

UNCOV
56
    def __contains__(self, x):
×
57
        """
58
        >>> b = Buffer()
59
        >>> b.add(b'asdf')
60
        >>> b'x' in b
61
        False
62
        >>> b.add(b'x')
63
        >>> b'x' in b
64
        True
65
        """
UNCOV
66
        for b in self.data:
×
UNCOV
67
            if x in b:
×
UNCOV
68
                return True
×
UNCOV
69
        return False
×
70

UNCOV
71
    def index(self, x):
×
72
        """
73
        >>> b = Buffer()
74
        >>> b.add(b'asdf')
75
        >>> b.add(b'qwert')
76
        >>> b.index(b't') == len(b) - 1
77
        True
78
        """
UNCOV
79
        sofar = 0
×
UNCOV
80
        for b in self.data:
×
UNCOV
81
            if x in b:
×
UNCOV
82
                return sofar + b.index(x)
×
UNCOV
83
            sofar += len(b)
×
84
        raise IndexError()
×
85

UNCOV
86
    def add(self, data):
×
87
        """
88
        Adds data to the buffer.
89

90
        Arguments:
91
            data(str,Buffer): Data to add
92
        """
93
        # Fast path for ''
UNCOV
94
        if not data: return
×
95

UNCOV
96
        if isinstance(data, Buffer):
×
97
            self.size += data.size
×
98
            self.data += data.data
×
99
        else:
UNCOV
100
            self.size += len(data)
×
UNCOV
101
            self.data.append(data)
×
102

UNCOV
103
    def unget(self, data):
×
104
        """
105
        Places data at the front of the buffer.
106

107
        Arguments:
108
            data(str,Buffer): Data to place at the beginning of the buffer.
109

110
        Example:
111

112
            >>> b = Buffer()
113
            >>> b.add(b"hello")
114
            >>> b.add(b"world")
115
            >>> b.get(5)
116
            b'hello'
117
            >>> b.unget(b"goodbye")
118
            >>> b.get()
119
            b'goodbyeworld'
120
        """
UNCOV
121
        if isinstance(data, Buffer):
×
UNCOV
122
            self.data = data.data + self.data
×
UNCOV
123
            self.size += data.size
×
124
        else:
UNCOV
125
            self.data.insert(0, data)
×
UNCOV
126
            self.size += len(data)
×
127

UNCOV
128
    def get(self, want=float('inf')):
×
129
        """
130
        Retrieves bytes from the buffer.
131

132
        Arguments:
133
            want(int): Maximum number of bytes to fetch
134

135
        Returns:
136
            Data as string
137

138
        Example:
139

140
            >>> b = Buffer()
141
            >>> b.add(b'hello')
142
            >>> b.add(b'world')
143
            >>> b.get(1)
144
            b'h'
145
            >>> b.get()
146
            b'elloworld'
147
        """
148
        # Fast path, get all of the data
UNCOV
149
        if want >= self.size:
×
UNCOV
150
            data   = b''.join(self.data)
×
UNCOV
151
            self.size = 0
×
UNCOV
152
            self.data = []
×
UNCOV
153
            return data
×
154

155
        # Slow path, find the correct-index chunk
UNCOV
156
        have = 0
×
UNCOV
157
        i    = 0
×
UNCOV
158
        while want >= have:
×
UNCOV
159
            have += len(self.data[i])
×
UNCOV
160
            i    += 1
×
161

162
        # Join the chunks, evict from the buffer
UNCOV
163
        data   = b''.join(self.data[:i])
×
UNCOV
164
        self.data = self.data[i:]
×
165

166
        # If the last chunk puts us over the limit,
167
        # stick the extra back at the beginning.
UNCOV
168
        if have > want:
×
UNCOV
169
            extra = data[want:]
×
UNCOV
170
            data  = data[:want]
×
UNCOV
171
            self.data.insert(0, extra)
×
172

173
        # Size update
UNCOV
174
        self.size -= len(data)
×
175

UNCOV
176
        return data
×
177

UNCOV
178
    def get_fill_size(self, size=None):
×
179
        """
180
        Retrieves the default fill size for this buffer class.
181

182
        Arguments:
183
            size (int): (Optional) If set and not None, returns the size variable back.
184

185
        Returns:
186
            Fill size as integer if size == None, else size.
187
        """
UNCOV
188
        if size is None:
×
UNCOV
189
            size = self.buffer_fill_size
×
190

UNCOV
191
        with context.local(buffer_size=size):
×
UNCOV
192
            return context.buffer_size
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc