From: Ken Hornstein Date: Mon, 21 Oct 2013 20:01:02 +0000 (-0400) Subject: Start of RFC 2047 encoding code. Not complete in the slightest. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/b776e28b89aa117da6d93888813aa52b2c292d02?ds=inline Start of RFC 2047 encoding code. Not complete in the slightest. --- diff --git a/h/prototypes.h b/h/prototypes.h index 0df91b1d..e94bee36 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -59,6 +59,27 @@ char *cpytrim (const char *); int decode_rfc2047 (char *, char *, size_t); void discard (FILE *); int default_done (int); + +/* + * Encode a message header using RFC 2047 encoding. If the message contains + * no non-ASCII characters, then leave the header as-is. + * + * Arguments include: + * + * name - Message header name + * value - Message header content; must point to allocated memory + * (may be changed if encoding is necessary) + * encoding - Encoding type. May be one of CE_UNKNOWN (function chooses + * the encoding), CE_BASE64 or CE_QUOTED + * charset - Charset used for encoding. If NULL, obtain from system + * locale. + * + * Returns 0 on success, any other value on failure. + */ + +int encode_rfc2047(const char *name, char **value, int encoding, + const char *charset); + void escape_display_name (char *, size_t); void escape_local_part (char *, size_t); int ext_hook(char *, char *, char *); diff --git a/sbr/encode_rfc2047.c b/sbr/encode_rfc2047.c new file mode 100644 index 00000000..ff77ff52 --- /dev/null +++ b/sbr/encode_rfc2047.c @@ -0,0 +1,43 @@ +/* + * Routines to encode message headers using RFC 2047-encoding. + * + * This code is Copyright (c) 2002, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. + */ + +#include + +/* + * Encode a message header using RFC 2047 encoding. We make the assumption + * that all characters < 128 are ASCII and as a consequence don't need any + * encoding. + */ + +int +encode_rfc2047(const char *name, char **value, int encoding, + const char *charset) +{ + char *p; + + /* + * First, check to see if we even need to encode the header + */ + + for (p = *value; *p != '\0'; p++) { + if (! isascii((int) *p) + goto encode; + } + + return 0; + +encode: + /* + * Some rules from RFC 2047: + * + * - Encoded words cannot be more than 75 characters long + * - Multiple "long" encoded words must be on new lines. + * + * Also, we're not permitted to encode email addresses, so + * we need to actually _parse_ email addresses and only encode + * the right bits.