+
+void rfc3676_quote_line (STATE* s, char* dst, size_t dstlen,
+ const char* line) {
+ char quote[SHORT_STRING];
+ int offset = 0, i = 0, count = 0;
+ regmatch_t pmatch[1];
+
+ quote[0] = '\0';
+
+ while (regexec ((regex_t *) QuoteRegexp.rx, &line[offset],
+ 1, pmatch, 0) == 0)
+ offset += pmatch->rm_eo;
+
+ if (offset > 0) {
+ /* first count number of real quoting characters;
+ * read: non-spaces
+ * this maybe just plain wrong, but leaving spaces
+ * within quoting characters is what I consider
+ * more plain wrong...
+ */
+ for (i = 0; i < offset; i++)
+ if (line[i] != ' ')
+ count++;
+ /* just make sure we're inside quote althoug we
+ * likely won't have more than SHORT_STRING quote levels... */
+ i = (count > SHORT_STRING-1) ? SHORT_STRING-1 : count;
+ memset (quote, '>', i);
+ quote[i] = '\0';
+ }
+ debug_print (4, ("f=f: quotelevel = %d, new prefix = '%s'\n",
+ i, NONULL (quote)));
+ /* if we changed prefix, make sure we respect $stuff_quoted */
+ snprintf (dst, dstlen, "%s%s%s%s", NONULL (s->prefix), NONULL (quote),
+ option (OPTSTUFFQUOTED) && line[offset] != ' ' ? " " : "",
+ &line[offset]);
+}
+
+void rfc3676_space_stuff (HEADER* hdr) {
+#if DEBUG
+ int lc = 0;
+ size_t len = 0;
+ unsigned char c = '\0';
+#endif
+ FILE* in = NULL, *out = NULL;
+ char buf[LONG_STRING];
+ char tmpfile[_POSIX_PATH_MAX];
+
+ if (!hdr || !hdr->content || !hdr->content->filename)
+ return;
+
+ debug_print (2, ("f=f: postprocess %s\n", hdr->content->filename));
+ if ((in = safe_fopen (hdr->content->filename, "r")) == NULL)
+ return;
+ mutt_mktemp (tmpfile);
+ if ((out = safe_fopen (tmpfile, "w+")) == NULL) {
+ fclose (in);
+ return;
+ }
+
+ while (fgets (buf, sizeof (buf), in)) {
+ if (ascii_strncmp ("From ", buf, 4) == 0 || buf[0] == ' ') {
+ fputc (' ', out);
+#if DEBUG
+ lc++;
+ len = str_len (buf);
+ if (len > 0) {
+ c = buf[len-1];
+ buf[len-1] = '\0';
+ }
+ debug_print (4, ("f=f: line %d needs space-stuffing: '%s'\n",
+ lc, buf));
+ if (len > 0)
+ buf[len-1] = c;
+#endif
+ }
+ fputs (buf, out);
+ }
+ fclose (in);
+ unlink (hdr->content->filename);
+ fclose (out);
+ str_replace (&hdr->content->filename, tmpfile);
+}