File size: 645 Bytes
0aee47a |
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 |
"""
bilibili_api.utils.varint
变长数字字节相关。
"""
from typing import Tuple
def read_varint(stream: bytes) -> Tuple[int, int]:
"""
读取 varint。
Args:
stream (bytes): 字节流。
Returns:
Tuple[int, int],真实值和占用长度。
"""
value = 0
position = 0
shift = 0
while True:
if position >= len(stream):
break
byte = stream[position]
value += (byte & 0b01111111) << shift
if byte & 0b10000000 == 0:
break
position += 1
shift += 7
return value, position + 1
|