import struct


def read_until_null(filebuffer, maxbytes=1024):
    """Reads a file until a null character

    Args:
        filebuffer (obj): a file that was opened with open()
        maxbytes (int): number of bytes that will be read if no null byte is found
        Avoids infinite loops.

    Returns:
        str: string concatenation of the bytes read
        int: how many bytes were read
    """

    current_string = b''
    current_byte = filebuffer.read(1)
    bytes_read = 1

    while current_byte != b'\x00':

        current_string += current_byte
        # print('current byte: {}'.format(current_byte))

        bytes_read += 1
        current_byte = struct.unpack('c', filebuffer.read(1))[0]

        if bytes_read > maxbytes:
            print('exiting due to infinite loop')
            break

    return current_string, bytes_read