Encode a attribute: value pair for the LDIF format.

    See RFC2849 for details.

    Rules are:
    - Text containing only chars <= 127 except control, ' ', '<', ':'
      is passed as-is
    - Other text is encoded through UTF-8 and base64-encoded
    - Binary data is simply base64-encoded

    Returns:
        A 'key: value' or 'key:: b64value' text line.

def ldif_encode(attr, value):
    """Encode a attribute: value pair for the LDIF format.

    See RFC2849 for details.

    Rules are:
    - Text containing only chars <= 127 except control, ' ', '<', ':'
      is passed as-is
    - Other text is encoded through UTF-8 and base64-encoded
    - Binary data is simply base64-encoded

    Returns:
        A 'key: value' or 'key:: b64value' text line.
    """
    if isinstance(value, bytes):
        return '%s:: %s' % (attr, base64.b64encode(value).decode('ascii'))
    elif any(c not in _BASE_LDIF for c in value):
        return '%s:: %s' % (attr, base64.b64encode(value.encode('utf-8')).decode('ascii'))
    else:
        return '%s: %s' % (attr, value)