|
9 | 9 |
|
10 | 10 | import struct |
11 | 11 |
|
12 | | -from maxminddb.compat import byte_from_int, int_from_bytes |
| 12 | +from maxminddb.compat import byte_from_int, int_from_byte, int_from_bytes |
13 | 13 | from maxminddb.errors import InvalidDatabaseError |
14 | 14 |
|
15 | 15 |
|
@@ -164,21 +164,22 @@ def _verify_size(self, expected, actual): |
164 | 164 |
|
165 | 165 | def _size_from_ctrl_byte(self, ctrl_byte, offset, type_num): |
166 | 166 | size = ctrl_byte & 0x1f |
167 | | - if type_num == 1: |
| 167 | + if type_num == 1 or size < 29: |
168 | 168 | return size, offset |
169 | | - bytes_to_read = 0 if size < 29 else size - 28 |
170 | | - |
171 | | - new_offset = offset + bytes_to_read |
172 | | - size_bytes = self._buffer[offset:new_offset] |
173 | 169 |
|
174 | 170 | # Using unpack rather than int_from_bytes as it is about 200 lookups |
175 | 171 | # per second faster here. |
176 | 172 | if size == 29: |
177 | | - size = 29 + struct.unpack(b'!B', size_bytes)[0] |
178 | | - elif size == 30: |
| 173 | + size = 29 + int_from_byte(self._buffer[offset]) |
| 174 | + return size, offset + 1 |
| 175 | + |
| 176 | + if size == 30: |
| 177 | + new_offset = offset + 2 |
| 178 | + size_bytes = self._buffer[offset:new_offset] |
179 | 179 | size = 285 + struct.unpack(b'!H', size_bytes)[0] |
180 | | - elif size > 30: |
181 | | - size = struct.unpack(b'!I', size_bytes.rjust(4, |
182 | | - b'\x00'))[0] + 65821 |
| 180 | + return size, new_offset |
183 | 181 |
|
| 182 | + new_offset = offset + 3 |
| 183 | + size_bytes = self._buffer[offset:new_offset] |
| 184 | + size = struct.unpack(b'!I', b'\x00' + size_bytes)[0] + 65821 |
184 | 185 | return size, new_offset |
0 commit comments