import struct


def __check_block_id(descriptor, check_id, error_message):
    """
    Ensures that block id number matches that specified in check_id, and
    determines byte order (endianness) of file
    :param byte[] descriptor: bytes in file/trace descriptor block
    :param byte check_id:     number against which block id is checked.
                              0x3a55 / 0x553a for file descriptor
                              0x4422 / 0x2244 for trace descriptor
    :param str error_message: Error message to display if id does not match
    :return str endian:       Byte order of file. '>' for big-endian,
                              '<' for little endian
    """
    id = struct.unpack('h', descriptor[0:2])[0]
    if id == check_id:
        endian = '<'
    else:
        if struct.unpack('>h', descriptor[0:2])[0] == check_id:
            endian = '>'
        else:
            raise Exception(error_message)
    return endian