# Path: asn1crypto/util.py
# def int_from_bytes(value, signed=False):
#     """
#     Converts a byte string to an integer
# 
#     :param value:
#         The byte string to convert
# 
#     :param signed:
#         If the byte string should be interpreted using two's complement
# 
#     :return:
#         An integer
#     """
# 
#     if value == b'':
#         return 0
# 
#     num = long(value.encode("hex"), 16)  # noqa
# 
#     if not signed:
#         return num
# 
#     # Check for sign bit and handle two's complement
#     if ord(value[0:1]) & 0x80:
#         bit_len = len(value) * 8
#         return num - (1 << bit_len)
# 
#     return num
# 
# def int_to_bytes(value, signed=False, width=None):
#     """
#     Converts an integer to a byte string
# 
#     :param value:
#         The integer to convert
# 
#     :param signed:
#         If the byte string should be encoded using two's complement
# 
#     :param width:
#         If None, the minimal possible size (but at least 1),
#         otherwise an integer of the byte width for the return value
# 
#     :return:
#         A byte string
#     """
# 
#     if value == 0 and width == 0:
#         return b''
# 
#     # Handle negatives in two's complement
#     is_neg = False
#     if signed and value < 0:
#         is_neg = True
#         bits = int(math.ceil(len('%x' % abs(value)) / 2.0) * 8)
#         value = (value + (1 << bits)) % (1 << bits)
# 
#     hex_str = '%x' % value
#     if len(hex_str) & 1:
#         hex_str = '0' + hex_str
# 
#     output = hex_str.decode('hex')
# 
#     if signed and not is_neg and ord(output[0:1]) & 0x80:
#         output = b'\x00' + output
# 
#     if width is not None:
#         if len(output) > width:
#             raise OverflowError('int too big to convert')
#         if is_neg:
#             pad_char = b'\xFF'
#         else:
#             pad_char = b'\x00'
#         output = (pad_char * (width - len(output))) + output
#     elif is_neg and ord(output[0:1]) & 0x80 == 0:
#         output = b'\xFF' + output
# 
#     return output


import sys
from ._types import byte_cls, chr_cls, type_name
from .util import int_from_bytes, int_to_bytes
        tag = 0
        while True:
            if data_len < pointer + 1:
                raise ValueError(_INSUFFICIENT_DATA_MESSAGE % (1, data_len - pointer))
            num = ord(encoded_data[pointer]) if _PY2 else encoded_data[pointer]
            pointer += 1
            if num == 0x80 and tag == 0:
                raise ValueError('Non-minimal tag encoding')
            tag *= 128
            tag += num & 127
            if num >> 7 == 0:
                break
        if tag < 31:
            raise ValueError('Non-minimal tag encoding')

    if data_len < pointer + 1:
        raise ValueError(_INSUFFICIENT_DATA_MESSAGE % (1, data_len - pointer))
    length_octet = ord(encoded_data[pointer]) if _PY2 else encoded_data[pointer]
    pointer += 1
    trailer = b''

    if length_octet >> 7 == 0:
        contents_end = pointer + (length_octet & 127)

    else:
        length_octets = length_octet & 127
        if length_octets:
            if data_len < pointer + length_octets:
                raise ValueError(_INSUFFICIENT_DATA_MESSAGE % (length_octets, data_len - pointer))
            pointer += length_octets

# Path: asn1crypto/util.py
# def int_from_bytes(value, signed=False):
#     """
#     Converts a byte string to an integer
# 
#     :param value:
#         The byte string to convert
# 
#     :param signed:
#         If the byte string should be interpreted using two's complement
# 
#     :return:
#         An integer
#     """
# 
#     if value == b'':
#         return 0
# 
#     num = long(value.encode("hex"), 16)  # noqa
# 
#     if not signed:
#         return num
# 
#     # Check for sign bit and handle two's complement
#     if ord(value[0:1]) & 0x80:
#         bit_len = len(value) * 8
#         return num - (1 << bit_len)
# 
#     return num
# 
# def int_to_bytes(value, signed=False, width=None):
#     """
#     Converts an integer to a byte string
# 
#     :param value:
#         The integer to convert
# 
#     :param signed:
#         If the byte string should be encoded using two's complement
# 
#     :param width:
#         If None, the minimal possible size (but at least 1),
#         otherwise an integer of the byte width for the return value
# 
#     :return:
#         A byte string
#     """
# 
#     if value == 0 and width == 0:
#         return b''
# 
#     # Handle negatives in two's complement
#     is_neg = False
#     if signed and value < 0:
#         is_neg = True
#         bits = int(math.ceil(len('%x' % abs(value)) / 2.0) * 8)
#         value = (value + (1 << bits)) % (1 << bits)
# 
#     hex_str = '%x' % value
#     if len(hex_str) & 1:
#         hex_str = '0' + hex_str
# 
#     output = hex_str.decode('hex')
# 
#     if signed and not is_neg and ord(output[0:1]) & 0x80:
#         output = b'\x00' + output
# 
#     if width is not None:
#         if len(output) > width:
#             raise OverflowError('int too big to convert')
#         if is_neg:
#             pad_char = b'\xFF'
#         else:
#             pad_char = b'\x00'
#         output = (pad_char * (width - len(output))) + output
#     elif is_neg and ord(output[0:1]) & 0x80 == 0:
#         output = b'\xFF' + output
# 
#     return output


# Path: asn1crypto/parser.py
import sys
from ._types import byte_cls, chr_cls, type_name
from .util import int_from_bytes, int_to_bytes

        tag = 0
        while True:
            if data_len < pointer + 1:
                raise ValueError(_INSUFFICIENT_DATA_MESSAGE % (1, data_len - pointer))
            num = ord(encoded_data[pointer]) if _PY2 else encoded_data[pointer]
            pointer += 1
            if num == 0x80 and tag == 0:
                raise ValueError('Non-minimal tag encoding')
            tag *= 128
            tag += num & 127
            if num >> 7 == 0:
                break
        if tag < 31:
            raise ValueError('Non-minimal tag encoding')

    if data_len < pointer + 1:
        raise ValueError(_INSUFFICIENT_DATA_MESSAGE % (1, data_len - pointer))
    length_octet = ord(encoded_data[pointer]) if _PY2 else encoded_data[pointer]
    pointer += 1
    trailer = b''

    if length_octet >> 7 == 0:
        contents_end = pointer + (length_octet & 127)

    else:
        length_octets = length_octet & 127
        if length_octets:
            if data_len < pointer + length_octets:
                raise ValueError(_INSUFFICIENT_DATA_MESSAGE % (length_octets, data_len - pointer))
            pointer += length_octets

            contents_end = pointer + int_from_bytes(encoded_data[pointer - length_octets:pointer], signed=False)