def read_with_check(buf, pos, nbytes):
    """
    Reads and returns n bytes.
    :param buf: The input buffer
    :param pos: The current position
    :param nbytes: number of bytes to open_topography in
    :return: The bytes and the new position in the buffer
    """

    if len(buf) < nbytes or len(buf) - nbytes < pos:
        raise ValueError("Some sizes went wrong.")

    out = buf[pos:pos + nbytes]
    pos += int(nbytes)

    out = out[0] if nbytes == 1 else out
    return out, pos