forked from aiortc/aioquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_buffer.py
155 lines (130 loc) · 4.43 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
import struct
from typing import Optional
uint16 = struct.Struct(">H")
uint32 = struct.Struct(">L")
uint64 = struct.Struct(">Q")
class BufferReadError(ValueError):
def __init__(self, message: str = "Read out of bounds") -> None:
super().__init__(message)
class BufferWriteError(ValueError):
def __init__(self, message: str = "Write out of bounds") -> None:
super().__init__(message)
class Buffer:
def __init__(self, capacity: int = 0, data: Optional[bytes] = None):
self._pos = 0
self._data = memoryview(bytearray(capacity if data is None else data))
self._capacity = len(self._data)
@property
def capacity(self) -> int:
return self._capacity
@property
def data(self) -> bytes:
return bytes(self._data[0 : self._pos])
def data_slice(self, start: int, end: int) -> bytes:
if (
start < 0
or self._capacity < start
or end < 0
or self._capacity < end
or end < start
):
raise BufferReadError()
return bytes(self._data[start:end])
def eof(self) -> bool:
return self._pos == self._capacity
def seek(self, pos: int) -> None:
if pos < 0 or pos > self._capacity:
raise BufferReadError("Seek out of bounds")
self._pos = pos
def tell(self) -> int:
return self._pos
def pull_bytes(self, length: int) -> bytes:
if length < 0 or self._capacity < self._pos + length:
raise BufferReadError()
result = bytes(self._data[self._pos : (self._pos + length)])
self._pos += length
return result
def pull_uint8(self) -> int:
try:
result = self._data[self._pos]
except IndexError:
raise BufferReadError()
self._pos += 1
return result
def pull_uint16(self) -> int:
try:
(result,) = uint16.unpack_from(self._data, self._pos)
except struct.error:
raise BufferReadError()
self._pos += 2
return result
def pull_uint32(self) -> int:
try:
(result,) = uint32.unpack_from(self._data, self._pos)
except struct.error:
raise BufferReadError()
self._pos += 4
return result
def pull_uint64(self) -> int:
try:
(result,) = uint64.unpack_from(self._data, self._pos)
except struct.error:
raise BufferReadError()
self._pos += 8
return result
def pull_uint_var(self) -> int:
try:
first = self._data[self._pos]
except IndexError:
raise BufferReadError()
type = first >> 6
if type == 0:
self._pos += 1
return first
elif type == 1:
return self.pull_uint16() & 0x3FFF
elif type == 2:
return self.pull_uint32() & 0x3FFFFFFF
else:
return self.pull_uint64() & 0x3FFFFFFFFFFFFFFF
def push_bytes(self, value: bytes) -> None:
end_pos = self._pos + len(value)
if self._capacity < end_pos:
raise BufferWriteError()
self._data[self._pos : end_pos] = value
self._pos = end_pos
def push_uint8(self, value: int) -> None:
try:
self._data[self._pos] = value
except IndexError:
raise BufferWriteError()
self._pos += 1
def push_uint16(self, value: int) -> None:
try:
uint16.pack_into(self._data, self._pos, value)
except struct.error:
raise BufferWriteError()
self._pos += 2
def push_uint32(self, value: int) -> None:
try:
uint32.pack_into(self._data, self._pos, value)
except struct.error:
raise BufferWriteError()
self._pos += 4
def push_uint64(self, value: int) -> None:
try:
uint64.pack_into(self._data, self._pos, value)
except struct.error:
raise BufferWriteError()
self._pos += 8
def push_uint_var(self, value: int) -> None:
if value <= 0x3F:
self.push_uint8(value)
elif value <= 0x3FFF:
self.push_uint16(value | 0x4000)
elif value <= 0x3FFFFFFF:
self.push_uint32(value | 0x80000000)
elif value <= 0x3FFFFFFFFFFFFFFF:
self.push_uint64(value | 0xC000000000000000)
else:
raise ValueError("Integer is too big for a variable-length integer")