1
|
|
|
# MIT License |
2
|
|
|
# |
3
|
|
|
# Copyright (c) 2017 Matt Boyer |
4
|
|
|
# |
5
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
# in the Software without restriction, including without limitation the rights |
8
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
# furnished to do so, subject to the following conditions: |
11
|
|
|
# |
12
|
|
|
# The above copyright notice and this permission notice shall be included in |
13
|
|
|
# all copies or substantial portions of the Software. |
14
|
|
|
# |
15
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21
|
|
|
# SOFTWARE. |
22
|
|
|
|
23
|
|
|
|
24
|
|
View Code Duplication |
class Varint(object): |
|
|
|
|
25
|
|
|
def __init__(self, varint_bytes): |
26
|
|
|
self._bytes = varint_bytes |
27
|
|
|
self._len = 0 |
28
|
|
|
self._value = 0 |
29
|
|
|
|
30
|
|
|
varint_bits = [] |
31
|
|
|
for b in self._bytes: |
32
|
|
|
self._len += 1 |
33
|
|
|
if b & 0x80: |
34
|
|
|
varint_bits.append(b & 0x7F) |
35
|
|
|
else: |
36
|
|
|
varint_bits.append(b) |
37
|
|
|
break |
38
|
|
|
|
39
|
|
|
varint_twos_complement = 0 |
40
|
|
|
for position, b in enumerate(varint_bits[::-1]): |
41
|
|
|
varint_twos_complement += b * (1 << (7*position)) |
42
|
|
|
|
43
|
|
|
self._value = decode_twos_complement( |
44
|
|
|
int.to_bytes(varint_twos_complement, 4, byteorder='big'), 64 |
45
|
|
|
) |
46
|
|
|
|
47
|
|
|
def __int__(self): |
48
|
|
|
return self._value |
49
|
|
|
|
50
|
|
|
def __len__(self): |
51
|
|
|
return self._len |
52
|
|
|
|
53
|
|
|
def __repr__(self): |
54
|
|
|
return "<Varint {} ({} bytes)>".format(int(self), len(self)) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class IndexDict(dict): |
58
|
|
|
def __iter__(self): |
59
|
|
|
for k in sorted(self.keys()): |
60
|
|
|
yield k |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def decode_twos_complement(encoded, bit_length): |
64
|
|
|
assert(0 == bit_length % 8) |
65
|
|
|
encoded_int = int.from_bytes(encoded, byteorder='big') |
66
|
|
|
mask = 2**(bit_length - 1) |
67
|
|
|
value = -(encoded_int & mask) + (encoded_int & ~mask) |
68
|
|
|
return value |
69
|
|
|
|