import struct


def _decode_plen(reader):
    """
    Reads and decodes event's payload length.

    Parameters
    ----------
    reader : apptrace.Reader
        Trace reader object.

    Returns
    -------
    int
        decoded value.
    """
    plen = 0
    b0, = struct.unpack('<B', reader.read(1))
    if b0 & 0x80:
        b1, = struct.unpack('<B', reader.read(1))
        plen = b1  # higher part
        plen = (plen << 7) | (b0 & ~0x80)  # lower 7 bits
    else:
        plen = b0
    return plen