def CDR(s, alignment = 4, gap_byte = 0xFF):
    """If necessary, append to 's' a trailing NUL byte
    and fill with 'gap_byte' until properly aligned.
    """

    if len(s) % alignment == 0 and s[-1] in ("\x00", "\xFF"):
        return s
    s = s + "\x00"

    while len(s) % alignment:
        s = s + chr(gap_byte)
    return s