import six
import binascii


def _uvarint(buf):
    """Reads a varint from a bytes buffer and returns the value and # bytes"""
    x = 0
    s = 0
    for i, b_str in enumerate(buf):
        if six.PY3:
            b = b_str
        else:
            b = int(binascii.b2a_hex(b_str), 16)
        if b < 0x80:
            if i > 9 or (i == 9 and b > 1):
                raise ValueError("Overflow")
            return (x | b << s, i + 1)
        x |= (b & 0x7f) << s
        s += 7
    return 0, 0