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

Gallopsled / pwntools / 1

28 Jan 2022 10:58AM UTC coverage: 0.0% (-73.8%) from 73.823%
1

push

github

web-flow
Fix CI after Groovy Gorilla went away for libc unstrip test (#2025)

* Fix CI after Groovy Gorilla went away for libc unstrip test

Build elfutils 0.181 from source since we can't use builds
from a newer ubuntu version anymore.

* Install python wheels in CI

0 of 1 new or added line in 1 file covered. (0.0%)

13713 existing lines in 142 files now uncovered.

0 of 16559 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
UNCOV
1
from __future__ import absolute_import
×
UNCOV
2
from __future__ import division
×
3

UNCOV
4
from pwnlib.context import context
×
5

6

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

11
    Example:
12

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

29
    Implementation Details:
30

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

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

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

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

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

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

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

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

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

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

109
        Example:
110

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

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

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

134
        Returns:
135
            Data as string
136

137
        Example:
138

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

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

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

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

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

UNCOV
175
        return data
×
176

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

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

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

UNCOV
190
        with context.local(buffer_size=size):
×
UNCOV
191
            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