'Utility method to search a header block for the END card and handle
invalid END cards.
This method can also returned a modified copy of the input header block
in case an invalid end card needs to be sanitized.'

@classmethod
def _find_end_card(cls, block, card_len):

    for mo in HEADER_END_RE.finditer(block):
        if ((mo.start() % card_len) != 0):
            continue
        if mo.group('invalid'):
            offset = mo.start()
            trailing = block[(offset + 3):((offset + card_len) - 3)].rstrip()
            if trailing:
                trailing = repr(trailing).lstrip('ub')
                warnings.warn('Unexpected    bytes    trailing    END    keyword:    {0};    these    bytes    will    be    replaced    with    spaces    on    write.'.format(trailing), AstropyUserWarning)
            else:
                warnings.warn('Missing    padding    to    end    of    the    FITS    block    after    the    END    keyword;    additional    spaces    will    be    appended    to    the    file    upon    writing    to    pad    out    to    {0}    bytes.'.format(BLOCK_SIZE), AstropyUserWarning)
            block = ((block[:offset] + encode_ascii(END_CARD)) + block[(offset + len(END_CARD)):])
        return (True, block)
    return (False, block)
