]> diplodocus.org Git - nmh/commitdiff
Start of RFC 2047 encoding code. Not complete in the slightest.
authorKen Hornstein <kenh@pobox.com>
Mon, 21 Oct 2013 20:01:02 +0000 (16:01 -0400)
committerKen Hornstein <kenh@pobox.com>
Mon, 21 Oct 2013 20:01:02 +0000 (16:01 -0400)
h/prototypes.h
sbr/encode_rfc2047.c [new file with mode: 0644]

index 0df91b1..e94bee3 100644 (file)
@@ -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 (file)
index 0000000..ff77ff5
--- /dev/null
@@ -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 <h/mh.h>
+
+/*
+ * 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.