import struct


def decode_pair(string, position=0):
    """Decode a name/value pair.

    The number of bytes decoded as well as the name/value pair
    are returned.

    """
    name_length = string[position]
    if name_length & 128:
        name_length = struct.unpack(
            '!L', string[position:position+4])[0] & 0x7fffffff
        position += 4
    else:
        position += 1

    value_length = string[position]
    if value_length & 128:
        value_length = struct.unpack(
            '!L', string[position:position+4])[0] & 0x7fffffff
        position += 4
    else:
        position += 1

    name = string[position:position+name_length]
    position += name_length
    value = string[position:position+value_length]
    position += value_length

    return (position, (name, value))