1 /* mhparse.c -- routines to parse the contents of MIME messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
14 #include <h/mhparse.h>
16 #include "../sbr/m_mktemp.h"
20 #endif /* HAVE_ICONV */
26 extern int rcachesw
; /* mhcachesbr.c */
27 extern int wcachesw
; /* mhcachesbr.c */
29 int checksw
= 0; /* check Content-MD5 field */
32 * These are for mhfixmsg to:
33 * 1) Instruct parser not to detect invalid Content-Transfer-Encoding
35 * 2) Suppress the warning about bogus multipart content, and report it.
36 * 3) Suppress the warning about extraneous trailing ';' in header parameter
39 int skip_mp_cte_check
;
40 int suppress_bogus_mp_content_warning
;
42 int suppress_extraneous_trailing_semicolon_warning
;
45 * By default, suppress warning about multiple MIME-Version header fields.
47 int suppress_multiple_mime_version_warning
= 1;
49 /* list of preferred type/subtype pairs, for -prefer */
50 char *preferred_types
[NPREFS
],
51 *preferred_subtypes
[NPREFS
];
56 * Structures for TEXT messages
58 struct k2v SubText
[] = {
59 { "plain", TEXT_PLAIN
},
60 { "richtext", TEXT_RICHTEXT
}, /* defined in RFC 1341 */
61 { "enriched", TEXT_ENRICHED
}, /* defined in RFC 1896 */
62 { NULL
, TEXT_UNKNOWN
} /* this one must be last! */
65 /* Charset[] removed -- yozo. Mon Oct 8 01:03:41 JST 2012 */
68 * Structures for MULTIPART messages
70 struct k2v SubMultiPart
[] = {
71 { "mixed", MULTI_MIXED
},
72 { "alternative", MULTI_ALTERNATE
},
73 { "digest", MULTI_DIGEST
},
74 { "parallel", MULTI_PARALLEL
},
75 { "related", MULTI_RELATED
},
76 { NULL
, MULTI_UNKNOWN
} /* this one must be last! */
80 * Structures for MESSAGE messages
82 struct k2v SubMessage
[] = {
83 { "rfc822", MESSAGE_RFC822
},
84 { "partial", MESSAGE_PARTIAL
},
85 { "external-body", MESSAGE_EXTERNAL
},
86 { NULL
, MESSAGE_UNKNOWN
} /* this one must be last! */
90 * Structure for APPLICATION messages
92 struct k2v SubApplication
[] = {
93 { "octet-stream", APPLICATION_OCTETS
},
94 { "postscript", APPLICATION_POSTSCRIPT
},
95 { NULL
, APPLICATION_UNKNOWN
} /* this one must be last! */
99 * Mapping of names of CTE types in mhbuild directives
101 static struct k2v EncodingType
[] = {
104 { "q-p", CE_QUOTED
},
105 { "quoted-printable", CE_QUOTED
},
106 { "b64", CE_BASE64
},
107 { "base64", CE_BASE64
},
113 int find_cache (CT
, int, int *, char *, char *, int);
117 int type_ok (CT
, int);
118 void content_error (char *, CT
, char *, ...);
123 static CT
get_content (FILE *, char *, int);
124 static int get_comment (const char *, const char *, char **, char **);
126 static int InitGeneric (CT
);
127 static int InitText (CT
);
128 static int InitMultiPart (CT
);
129 static void reverse_parts (CT
);
130 static void prefer_parts(CT ct
);
131 static int InitMessage (CT
);
132 static int InitApplication (CT
);
133 static int init_encoding (CT
, OpenCEFunc
);
134 static unsigned long size_encoding (CT
);
135 static int InitBase64 (CT
);
136 static int openBase64 (CT
, char **);
137 static int InitQuoted (CT
);
138 static int openQuoted (CT
, char **);
139 static int Init7Bit (CT
);
140 static int openExternal (CT
, CT
, CE
, char **, int *);
141 static int InitFile (CT
);
142 static int openFile (CT
, char **);
143 static int InitFTP (CT
);
144 static int openFTP (CT
, char **);
145 static int InitMail (CT
);
146 static int openMail (CT
, char **);
147 static int readDigest (CT
, char *);
148 static int get_leftover_mp_content (CT
, int);
149 static int InitURL (CT
);
150 static int openURL (CT
, char **);
151 static int parse_header_attrs (const char *, const char *, char **, PM
*,
153 static size_t param_len(PM
, int, size_t, int *, int *, size_t *);
154 static size_t normal_param(PM
, char *, size_t, size_t, size_t);
155 static int get_dispo (char *, CT
, int);
157 struct str2init str2cts
[] = {
158 { "application", CT_APPLICATION
, InitApplication
},
159 { "audio", CT_AUDIO
, InitGeneric
},
160 { "image", CT_IMAGE
, InitGeneric
},
161 { "message", CT_MESSAGE
, InitMessage
},
162 { "multipart", CT_MULTIPART
, InitMultiPart
},
163 { "text", CT_TEXT
, InitText
},
164 { "video", CT_VIDEO
, InitGeneric
},
165 { NULL
, CT_EXTENSION
, NULL
}, /* these two must be last! */
166 { NULL
, CT_UNKNOWN
, NULL
},
169 struct str2init str2ces
[] = {
170 { "base64", CE_BASE64
, InitBase64
},
171 { "quoted-printable", CE_QUOTED
, InitQuoted
},
172 { "8bit", CE_8BIT
, Init7Bit
},
173 { "7bit", CE_7BIT
, Init7Bit
},
174 { "binary", CE_BINARY
, Init7Bit
},
175 { NULL
, CE_EXTENSION
, NULL
}, /* these two must be last! */
176 { NULL
, CE_UNKNOWN
, NULL
},
180 * NOTE WELL: si_key MUST NOT have value of NOTOK
182 * si_key is 1 if access method is anonymous.
184 struct str2init str2methods
[] = {
185 { "afs", 1, InitFile
},
186 { "anon-ftp", 1, InitFTP
},
187 { "ftp", 0, InitFTP
},
188 { "local-file", 0, InitFile
},
189 { "mail-server", 0, InitMail
},
190 { "url", 0, InitURL
},
196 * Main entry point for parsing a MIME message or file.
197 * It returns the Content structure for the top level
198 * entity in the file.
202 parse_mime (char *file
)
211 bogus_mp_content
= 0;
214 * Check if file is actually standard input
216 if ((is_stdin
= !(strcmp (file
, "-")))) {
217 char *tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
219 advise("mhparse", "unable to create temporary file in %s",
223 file
= mh_xstrdup(tfile
);
225 while ((n
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0) {
226 if (fwrite(buffer
, 1, n
, fp
) != n
) {
227 (void) m_unlink (file
);
228 advise (file
, "error copying to temporary file");
234 if (ferror (stdin
)) {
235 (void) m_unlink (file
);
236 advise ("stdin", "error reading");
240 (void) m_unlink (file
);
241 advise (file
, "error writing");
244 fseek (fp
, 0L, SEEK_SET
);
245 } else if (stat (file
, &statbuf
) == NOTOK
) {
246 advise (file
, "unable to stat");
248 } else if (S_ISDIR(statbuf
.st_mode
)) {
249 /* Don't try to parse a directory. */
250 inform("%s is a directory", file
);
252 } else if ((fp
= fopen (file
, "r")) == NULL
) {
253 advise (file
, "unable to read");
257 if (!(ct
= get_content (fp
, file
, 1))) {
259 (void) m_unlink (file
);
260 inform("unable to decode %s", file
);
265 ct
->c_unlink
= 1; /* temp file to remove */
269 if (ct
->c_end
== 0L) {
270 fseek (fp
, 0L, SEEK_END
);
271 ct
->c_end
= ftell (fp
);
274 if (ct
->c_ctinitfnx
&& (*ct
->c_ctinitfnx
) (ct
) == NOTOK
) {
286 * Main routine for reading/parsing the headers
287 * of a message content.
289 * toplevel = 1 # we are at the top level of the message
290 * toplevel = 0 # we are inside message type or multipart type
291 * # other than multipart/digest
292 * toplevel = -1 # we are inside multipart/digest
293 * NB: on failure we will fclose(in)!
297 get_content (FILE *in
, char *file
, int toplevel
)
300 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
304 m_getfld_state_t gstate
= 0;
306 /* allocate the content structure */
309 ct
->c_file
= add (file
, NULL
);
310 ct
->c_begin
= ftell (ct
->c_fp
) + 1;
313 * Parse the header fields for this
314 * content into a linked list.
316 m_getfld_track_filepos (&gstate
, in
);
317 for (compnum
= 1;;) {
318 int bufsz
= sizeof buf
;
319 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
324 /* get copies of the buffers */
325 np
= mh_xstrdup(name
);
326 vp
= mh_xstrdup(buf
);
328 /* if necessary, get rest of field */
329 while (state
== FLDPLUS
) {
331 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
332 vp
= add (buf
, vp
); /* add to previous value */
335 /* Now add the header data to the list */
336 add_header (ct
, np
, vp
);
338 /* continue, to see if this isn't the last header field */
339 ct
->c_begin
= ftell (in
) + 1;
343 if (name
[0] == ':') {
344 /* Special case: no blank line between header and body. The
345 file position indicator is on the newline at the end of the
346 line, but it needs to be one prior to the beginning of the
347 line. So subtract the length of the line, bufsz, plus 1. */
348 ct
->c_begin
= ftell (in
) - (bufsz
+ 1);
350 ct
->c_begin
= ftell (in
) - (bufsz
- 1);
355 ct
->c_begin
= ftell (in
);
360 adios (NULL
, "message format error in component #%d", compnum
);
363 adios (NULL
, "getfld() returned %d", state
);
366 /* break out of the loop */
369 m_getfld_state_destroy (&gstate
);
372 * Read the content headers. We will parse the
373 * MIME related header fields into their various
374 * structures and set internal flags related to
375 * content type/subtype, etc.
378 hp
= ct
->c_first_hf
; /* start at first header field */
380 /* Get MIME-Version field */
381 if (!strcasecmp (hp
->name
, VRSN_FIELD
)) {
386 vrsn
= add (hp
->value
, NULL
);
388 /* Now, cleanup this field */
391 while (isspace ((unsigned char) *cp
))
393 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
395 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
396 if (!isspace ((unsigned char) *dp
))
400 fprintf (stderr
, "%s: %s\n", VRSN_FIELD
, cp
);
403 get_comment (ct
->c_file
, VRSN_FIELD
, &cp
, NULL
) == NOTOK
)
406 for (dp
= cp
; istoken (*dp
); dp
++)
410 ucmp
= !strcasecmp (cp
, VRSN_VALUE
);
413 inform("message %s has unknown value for %s: field (%s), continuing...",
414 ct
->c_file
, VRSN_FIELD
, cp
);
419 if (! suppress_multiple_mime_version_warning
)
420 inform("message %s has multiple %s: fields",
421 ct
->c_file
, VRSN_FIELD
);
425 else if (!strcasecmp (hp
->name
, TYPE_FIELD
)) {
426 /* Get Content-Type field */
427 struct str2init
*s2i
;
428 CI ci
= &ct
->c_ctinfo
;
430 /* Check if we've already seen a Content-Type header */
432 inform("message %s has multiple %s: fields",
433 ct
->c_file
, TYPE_FIELD
);
437 /* Parse the Content-Type field */
438 if (get_ctinfo (hp
->value
, ct
, 0) == NOTOK
)
442 * Set the Init function and the internal
443 * flag for this content type.
445 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
446 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
448 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
450 ct
->c_type
= s2i
->si_val
;
451 ct
->c_ctinitfnx
= s2i
->si_init
;
453 else if (!strcasecmp (hp
->name
, ENCODING_FIELD
)) {
454 /* Get Content-Transfer-Encoding field */
456 struct str2init
*s2i
;
459 * Check if we've already seen the
460 * Content-Transfer-Encoding field
463 inform("message %s has multiple %s: fields",
464 ct
->c_file
, ENCODING_FIELD
);
468 /* get copy of this field */
469 ct
->c_celine
= cp
= add (hp
->value
, NULL
);
471 while (isspace ((unsigned char) *cp
))
473 for (dp
= cp
; istoken (*dp
); dp
++)
479 * Find the internal flag and Init function
480 * for this transfer encoding.
482 for (s2i
= str2ces
; s2i
->si_key
; s2i
++)
483 if (!strcasecmp (cp
, s2i
->si_key
))
485 if (!s2i
->si_key
&& !uprf (cp
, "X-"))
488 ct
->c_encoding
= s2i
->si_val
;
490 /* Call the Init function for this encoding */
491 if (s2i
->si_init
&& (*s2i
->si_init
) (ct
) == NOTOK
)
494 else if (!strcasecmp (hp
->name
, MD5_FIELD
)) {
495 /* Get Content-MD5 field */
501 if (ct
->c_digested
) {
502 inform("message %s has multiple %s: fields",
503 ct
->c_file
, MD5_FIELD
);
507 ep
= cp
= add (hp
->value
, NULL
); /* get a copy */
509 while (isspace ((unsigned char) *cp
))
511 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
513 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
514 if (!isspace ((unsigned char) *dp
))
518 fprintf (stderr
, "%s: %s\n", MD5_FIELD
, cp
);
521 get_comment (ct
->c_file
, MD5_FIELD
, &cp
, NULL
) == NOTOK
) {
526 for (dp
= cp
; *dp
&& !isspace ((unsigned char) *dp
); dp
++)
534 else if (!strcasecmp (hp
->name
, ID_FIELD
)) {
535 /* Get Content-ID field */
536 ct
->c_id
= add (hp
->value
, ct
->c_id
);
538 else if (!strcasecmp (hp
->name
, DESCR_FIELD
)) {
539 /* Get Content-Description field */
540 ct
->c_descr
= add (hp
->value
, ct
->c_descr
);
542 else if (!strcasecmp (hp
->name
, DISPO_FIELD
)) {
543 /* Get Content-Disposition field */
544 if (get_dispo(hp
->value
, ct
, 0) == NOTOK
)
549 hp
= hp
->next
; /* next header field */
553 * Check if we saw a Content-Type field.
554 * If not, then assign a default value for
555 * it, and the Init function.
559 * If we are inside a multipart/digest message,
560 * so default type is message/rfc822
563 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
565 ct
->c_type
= CT_MESSAGE
;
566 ct
->c_ctinitfnx
= InitMessage
;
569 * Else default type is text/plain
571 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
)
573 ct
->c_type
= CT_TEXT
;
574 ct
->c_ctinitfnx
= InitText
;
578 /* Use default Transfer-Encoding, if necessary */
580 ct
->c_encoding
= CE_7BIT
;
593 * small routine to add header field to list
597 add_header (CT ct
, char *name
, char *value
)
601 /* allocate header field structure */
604 /* link data into header structure */
609 /* link header structure into the list */
610 if (ct
->c_first_hf
== NULL
) {
611 ct
->c_first_hf
= hp
; /* this is the first */
614 ct
->c_last_hf
->next
= hp
; /* add it to the end */
623 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
624 * directives. Fills in the information of the CTinfo structure.
627 get_ctinfo (char *cp
, CT ct
, int magic
)
636 /* store copy of Content-Type line */
637 cp
= ct
->c_ctline
= add (cp
, NULL
);
639 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
642 /* change newlines to spaces */
643 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
646 /* trim trailing spaces */
647 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
648 if (!isspace ((unsigned char) *dp
))
653 fprintf (stderr
, "%s: %s\n", TYPE_FIELD
, cp
);
655 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
656 &ci
->ci_comment
) == NOTOK
)
659 for (dp
= cp
; istoken (*dp
); dp
++)
663 ci
->ci_type
= mh_xstrdup(cp
); /* store content type */
668 inform("invalid %s: field in message %s (empty type)",
669 TYPE_FIELD
, ct
->c_file
);
672 to_lower(ci
->ci_type
);
674 while (isspace ((unsigned char) *cp
))
677 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
678 &ci
->ci_comment
) == NOTOK
)
683 ci
->ci_subtype
= mh_xstrdup("");
688 while (isspace ((unsigned char) *cp
))
691 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
692 &ci
->ci_comment
) == NOTOK
)
695 for (dp
= cp
; istoken (*dp
); dp
++)
699 ci
->ci_subtype
= mh_xstrdup(cp
); /* store the content subtype */
703 if (!*ci
->ci_subtype
) {
704 inform("invalid %s: field in message %s (empty subtype for \"%s\")",
705 TYPE_FIELD
, ct
->c_file
, ci
->ci_type
);
708 to_lower(ci
->ci_subtype
);
711 while (isspace ((unsigned char) *cp
))
714 if (*cp
== '(' && get_comment (ct
->c_file
, TYPE_FIELD
, &cp
,
715 &ci
->ci_comment
) == NOTOK
)
718 if ((status
= parse_header_attrs (ct
->c_file
, TYPE_FIELD
, &cp
,
719 &ci
->ci_first_pm
, &ci
->ci_last_pm
,
720 &ci
->ci_comment
)) != OK
) {
721 return status
== NOTOK
? NOTOK
: OK
;
725 * Get any <Content-Id> given in buffer
727 if (magic
&& *cp
== '<') {
730 if (!(dp
= strchr(ct
->c_id
= ++cp
, '>'))) {
731 inform("invalid ID in message %s", ct
->c_file
);
737 ct
->c_id
= concat ("<", ct
->c_id
, ">\n", NULL
);
743 while (isspace ((unsigned char) *cp
))
748 * Get any [Content-Description] given in buffer.
750 if (magic
&& *cp
== '[') {
752 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
756 inform("invalid description in message %s", ct
->c_file
);
764 ct
->c_descr
= concat (ct
->c_descr
, "\n", NULL
);
770 while (isspace ((unsigned char) *cp
))
775 * Get any {Content-Disposition} given in buffer.
777 if (magic
&& *cp
== '{') {
779 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
783 inform("invalid disposition in message %s", ct
->c_file
);
791 if (get_dispo(cp
, ct
, 1) != OK
)
797 while (isspace ((unsigned char) *cp
))
802 * Get any extension directives (right now just the content transfer
803 * encoding, but maybe others) that we care about.
806 if (magic
&& *cp
== '*') {
808 * See if it's a CTE we match on
813 while (*cp
!= '\0' && ! isspace((unsigned char) *cp
))
817 inform("invalid null transfer encoding specification");
824 ct
->c_reqencoding
= CE_UNKNOWN
;
826 for (kv
= EncodingType
; kv
->kv_key
; kv
++) {
827 if (strcasecmp(kv
->kv_key
, dp
) == 0) {
828 ct
->c_reqencoding
= kv
->kv_value
;
833 if (ct
->c_reqencoding
== CE_UNKNOWN
) {
834 inform("invalid CTE specification: \"%s\"", dp
);
838 while (isspace ((unsigned char) *cp
))
843 * Check if anything is left over
847 ci
->ci_magic
= mh_xstrdup(cp
);
849 /* If there is a Content-Disposition header and it doesn't
850 have a *filename=, extract it from the magic contents.
851 The r1bindex call skips any leading directory
853 if (ct
->c_dispo_type
&&
854 !get_param(ct
->c_dispo_first
, "filename", '_', 1)) {
855 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename",
856 r1bindex(ci
->ci_magic
, '/'), 0);
860 inform("extraneous information in message %s's %s: field\n%*s(%s)",
861 ct
->c_file
, TYPE_FIELD
, strlen(invo_name
) + 2, "", cp
);
869 * Parse out a Content-Disposition header. A lot of this is cribbed from
873 get_dispo (char *cp
, CT ct
, int buildflag
)
875 char *dp
, *dispoheader
;
880 * Save the whole copy of the Content-Disposition header, unless we're
881 * processing a mhbuild directive. A NULL c_dispo will be a flag to
882 * mhbuild that the disposition header needs to be generated at that
886 dispoheader
= cp
= add(cp
, NULL
);
888 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
891 /* change newlines to spaces */
892 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
895 /* trim trailing spaces */
896 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
897 if (!isspace ((unsigned char) *dp
))
902 fprintf (stderr
, "%s: %s\n", DISPO_FIELD
, cp
);
904 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) ==
910 for (dp
= cp
; istoken (*dp
); dp
++)
914 ct
->c_dispo_type
= mh_xstrdup(cp
); /* store disposition type */
918 if (*cp
== '(' && get_comment (ct
->c_file
, DISPO_FIELD
, &cp
, NULL
) == NOTOK
)
921 if ((status
= parse_header_attrs (ct
->c_file
, DISPO_FIELD
, &cp
,
922 &ct
->c_dispo_first
, &ct
->c_dispo_last
,
924 if (status
== NOTOK
) {
929 inform("extraneous information in message %s's %s: field\n%*s(%s)",
930 ct
->c_file
, DISPO_FIELD
, strlen(invo_name
) + 2, "", cp
);
936 ct
->c_dispo
= dispoheader
;
943 get_comment (const char *filename
, const char *fieldname
, char **ap
,
948 char c
, buffer
[BUFSIZ
], *dp
;
958 inform("invalid comment in message %s's %s: field",
959 filename
, fieldname
);
964 if ((c
= *cp
++) == '\0')
987 if ((dp
= *commentp
)) {
988 *commentp
= concat (dp
, " ", buffer
, NULL
);
991 *commentp
= mh_xstrdup(buffer
);
995 while (isspace ((unsigned char) *cp
))
1006 * Handles content types audio, image, and video.
1007 * There's not much to do right here.
1015 return OK
; /* not much to do here */
1026 char buffer
[BUFSIZ
];
1031 CI ci
= &ct
->c_ctinfo
;
1033 /* check for missing subtype */
1034 if (!*ci
->ci_subtype
)
1035 ci
->ci_subtype
= add ("plain", ci
->ci_subtype
);
1038 ct
->c_subtype
= ct_str_subtype (CT_TEXT
, ci
->ci_subtype
);
1040 /* allocate text character set structure */
1042 ct
->c_ctparams
= (void *) t
;
1044 /* scan for charset parameter */
1045 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
)
1046 if (!strcasecmp (pm
->pm_name
, "charset"))
1049 /* check if content specified a character set */
1051 chset
= pm
->pm_value
;
1052 t
->tx_charset
= CHARSET_SPECIFIED
;
1054 t
->tx_charset
= CHARSET_UNSPECIFIED
;
1058 * If we can not handle character set natively,
1059 * then check profile for string to modify the
1060 * terminal or display method.
1062 * termproc is for mhshow, though mhlist -debug prints it, too.
1064 if (chset
!= NULL
&& !check_charset (chset
, strlen (chset
))) {
1065 snprintf (buffer
, sizeof(buffer
), "%s-charset-%s", invo_name
, chset
);
1066 if ((cp
= context_find (buffer
)))
1067 ct
->c_termproc
= mh_xstrdup(cp
);
1079 InitMultiPart (CT ct
)
1089 struct multipart
*m
;
1090 struct part
*part
, **next
;
1091 CI ci
= &ct
->c_ctinfo
;
1096 * The encoding for multipart messages must be either
1097 * 7bit, 8bit, or binary (per RFC 2045).
1099 if (! skip_mp_cte_check
&& ct
->c_encoding
!= CE_7BIT
&&
1100 ct
->c_encoding
!= CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
) {
1101 /* Copy the Content-Transfer-Encoding header field body so we can
1102 remove any trailing whitespace and leading blanks from it. */
1103 char *cte
= mh_xstrdup(ct
->c_celine
? ct
->c_celine
: "(null)");
1105 bp
= cte
+ strlen (cte
) - 1;
1106 while (bp
>= cte
&& isspace ((unsigned char) *bp
)) *bp
-- = '\0';
1107 for (bp
= cte
; *bp
&& isblank ((unsigned char) *bp
); ++bp
) continue;
1109 inform("\"%s/%s\" type in message %s must be encoded in\n"
1110 "7bit, 8bit, or binary, per RFC 2045 (6.4). "
1111 "mhfixmsg -fixcte can fix it, or\n"
1112 "manually edit the file and change the \"%s\"\n"
1113 "Content-Transfer-Encoding to one of those. For now, continuing...",
1114 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, bp
);
1121 ct
->c_subtype
= ct_str_subtype (CT_MULTIPART
, ci
->ci_subtype
);
1124 * Check for "boundary" parameter, which is
1125 * required for multipart messages.
1128 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1129 if (!strcasecmp (pm
->pm_name
, "boundary")) {
1135 /* complain if boundary parameter is missing */
1137 inform("a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1138 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1142 /* allocate primary structure for multipart info */
1144 ct
->c_ctparams
= (void *) m
;
1146 /* check if boundary parameter contains only whitespace characters */
1147 for (cp
= bp
; isspace ((unsigned char) *cp
); cp
++)
1150 inform("invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1151 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1155 /* remove trailing whitespace from boundary parameter */
1156 for (cp
= bp
, dp
= cp
+ strlen (cp
) - 1; dp
> cp
; dp
--)
1157 if (!isspace ((unsigned char) *dp
))
1161 /* record boundary separators */
1162 m
->mp_start
= concat (bp
, "\n", NULL
);
1163 m
->mp_stop
= concat (bp
, "--\n", NULL
);
1165 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1166 advise (ct
->c_file
, "unable to open for reading");
1170 fseek (fp
= ct
->c_fp
, pos
= ct
->c_begin
, SEEK_SET
);
1172 next
= &m
->mp_parts
;
1176 while ((gotlen
= getline(&bufp
, &buflen
, fp
)) != -1) {
1181 if (bufp
[0] != '-' || bufp
[1] != '-')
1184 if (strcmp (bufp
+ 2, m
->mp_start
))
1189 next
= &part
->mp_next
;
1191 if (!(p
= get_content (fp
, ct
->c_file
,
1192 ct
->c_subtype
== MULTI_DIGEST
? -1 : 0))) {
1200 fseek (fp
, pos
, SEEK_SET
);
1203 if (strcmp (bufp
+ 2, m
->mp_start
) == 0) {
1207 p
->c_end
= ftell(fp
) - (gotlen
+ 1);
1208 if (p
->c_end
< p
->c_begin
)
1209 p
->c_begin
= p
->c_end
;
1214 if (strcmp (bufp
+ 2, m
->mp_stop
) == 0)
1219 if (! suppress_bogus_mp_content_warning
) {
1220 inform("bogus multipart content in message %s", ct
->c_file
);
1222 bogus_mp_content
= 1;
1224 if (!inout
&& part
) {
1226 p
->c_end
= ct
->c_end
;
1228 if (p
->c_begin
>= p
->c_end
) {
1229 for (next
= &m
->mp_parts
; *next
!= part
;
1230 next
= &((*next
)->mp_next
))
1239 /* reverse the order of the parts for multipart/alternative */
1240 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1246 * label all subparts with part number, and
1247 * then initialize the content of the subpart.
1252 char partnam
[BUFSIZ
];
1255 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1256 pp
= partnam
+ strlen (partnam
);
1261 for (part
= m
->mp_parts
, partnum
= 1; part
;
1262 part
= part
->mp_next
, partnum
++) {
1265 sprintf (pp
, "%d", partnum
);
1266 p
->c_partno
= mh_xstrdup(partnam
);
1268 /* initialize the content of the subparts */
1269 if (p
->c_ctinitfnx
&& (*p
->c_ctinitfnx
) (p
) == NOTOK
) {
1278 get_leftover_mp_content (ct
, 1);
1279 get_leftover_mp_content (ct
, 0);
1289 * reverse the order of the parts of a multipart/alternative,
1290 * presumably to put the "most favored" alternative first, for
1291 * ease of choosing/displaying it later on. from a mail message on
1292 * nmh-workers, from kenh:
1293 * "Stock" MH 6.8.5 did not have a reverse_parts() function, but I
1294 * see code in mhn that did the same thing... According to the RCS
1295 * logs, that code was around from the initial checkin of mhn.c by
1296 * John Romine in 1992, which is as far back as we have."
1299 reverse_parts (CT ct
)
1301 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1305 /* Reverse the order of its parts by walking the mp_parts list
1306 and pushing each node to the front. */
1307 for (part
= m
->mp_parts
, m
->mp_parts
= NULL
; part
; part
= next
) {
1308 next
= part
->mp_next
;
1309 part
->mp_next
= m
->mp_parts
;
1315 move_preferred_part (CT ct
, char *type
, char *subtype
)
1317 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1318 struct part
*part
, *prev
, *head
, *nhead
, *ntail
;
1322 /* move the matching part(s) to the head of the list: walk the
1323 * list of parts, move matching parts to a new list (maintaining
1324 * their order), and finally, concatenate the old list onto the
1331 head
->mp_next
= m
->mp_parts
;
1332 nhead
->mp_next
= NULL
;
1336 part
= head
->mp_next
;
1337 while (part
!= NULL
) {
1338 ci
= &part
->mp_part
->c_ctinfo
;
1339 if (!strcasecmp(ci
->ci_type
, type
) &&
1340 (!subtype
|| !strcasecmp(ci
->ci_subtype
, subtype
))) {
1341 prev
->mp_next
= part
->mp_next
;
1342 part
->mp_next
= NULL
;
1343 ntail
->mp_next
= part
;
1345 part
= prev
->mp_next
;
1348 part
= prev
->mp_next
;
1351 ntail
->mp_next
= head
->mp_next
;
1352 m
->mp_parts
= nhead
->mp_next
;
1357 * move parts that match the user's preferences (-prefer) to the head
1358 * of the line. process preferences in reverse so first one given
1359 * ends up first in line
1365 for (i
= npreferred
-1; i
>= 0; i
--)
1366 move_preferred_part(ct
, preferred_types
[i
], preferred_subtypes
[i
]);
1371 /* parse_mime() arranges alternates in reverse (priority) order. This
1372 function can be used to reverse them back. This will put, for
1373 example, a text/plain part before a text/html part in a
1374 multipart/alternative part, for example, where it belongs. */
1376 reverse_alternative_parts (CT ct
) {
1377 if (ct
->c_type
== CT_MULTIPART
) {
1378 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1381 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
1385 /* And call recursively on each part of a multipart. */
1386 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1387 reverse_alternative_parts (part
->mp_part
);
1400 CI ci
= &ct
->c_ctinfo
;
1402 if ((ct
->c_encoding
!= CE_7BIT
) && (ct
->c_encoding
!= CE_8BIT
)) {
1403 inform("\"%s/%s\" type in message %s should be encoded in "
1404 "7bit or 8bit, continuing...", ci
->ci_type
, ci
->ci_subtype
,
1409 /* check for missing subtype */
1410 if (!*ci
->ci_subtype
)
1411 ci
->ci_subtype
= add ("rfc822", ci
->ci_subtype
);
1414 ct
->c_subtype
= ct_str_subtype (CT_MESSAGE
, ci
->ci_subtype
);
1416 switch (ct
->c_subtype
) {
1417 case MESSAGE_RFC822
:
1420 case MESSAGE_PARTIAL
:
1426 ct
->c_ctparams
= (void *) p
;
1428 /* scan for parameters "id", "number", and "total" */
1429 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1430 if (!strcasecmp (pm
->pm_name
, "id")) {
1431 p
->pm_partid
= add (pm
->pm_value
, NULL
);
1434 if (!strcasecmp (pm
->pm_name
, "number")) {
1435 if (sscanf (pm
->pm_value
, "%d", &p
->pm_partno
) != 1
1436 || p
->pm_partno
< 1) {
1438 inform("invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1439 pm
->pm_name
, ci
->ci_type
, ci
->ci_subtype
,
1440 ct
->c_file
, TYPE_FIELD
);
1445 if (!strcasecmp (pm
->pm_name
, "total")) {
1446 if (sscanf (pm
->pm_value
, "%d", &p
->pm_maxno
) != 1
1455 || (p
->pm_maxno
&& p
->pm_partno
> p
->pm_maxno
)) {
1456 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1457 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1463 case MESSAGE_EXTERNAL
:
1471 ct
->c_ctparams
= (void *) e
;
1474 && (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1475 advise (ct
->c_file
, "unable to open for reading");
1479 fseek (fp
= ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1481 if (!(p
= get_content (fp
, ct
->c_file
, 0))) {
1489 p
->c_ceopenfnx
= NULL
;
1490 if ((exresult
= params_external (ct
, 0)) != NOTOK
1491 && p
->c_ceopenfnx
== openMail
) {
1495 if ((size
= ct
->c_end
- p
->c_begin
) <= 0) {
1497 content_error (NULL
, ct
,
1498 "empty body for access-type=mail-server");
1502 e
->eb_body
= bp
= mh_xmalloc ((unsigned) size
);
1503 fseek (p
->c_fp
, p
->c_begin
, SEEK_SET
);
1505 switch (cc
= fread (bp
, sizeof(*bp
), size
, p
->c_fp
)) {
1507 adios ("failed", "fread");
1510 adios (NULL
, "unexpected EOF from fread");
1513 bp
+= cc
, size
-= cc
;
1520 p
->c_end
= p
->c_begin
;
1525 if (exresult
== NOTOK
)
1527 if (e
->eb_flags
== NOTOK
)
1530 switch (p
->c_type
) {
1535 if (p
->c_subtype
!= MESSAGE_RFC822
)
1539 e
->eb_partno
= ct
->c_partno
;
1541 (*p
->c_ctinitfnx
) (p
);
1556 params_external (CT ct
, int composing
)
1559 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1560 CI ci
= &ct
->c_ctinfo
;
1562 ct
->c_ceopenfnx
= NULL
;
1563 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1564 if (!strcasecmp (pm
->pm_name
, "access-type")) {
1565 struct str2init
*s2i
;
1566 CT p
= e
->eb_content
;
1568 for (s2i
= str2methods
; s2i
->si_key
; s2i
++)
1569 if (!strcasecmp (pm
->pm_value
, s2i
->si_key
))
1572 e
->eb_access
= pm
->pm_value
;
1573 e
->eb_flags
= NOTOK
;
1574 p
->c_encoding
= CE_EXTERNAL
;
1577 e
->eb_access
= s2i
->si_key
;
1578 e
->eb_flags
= s2i
->si_val
;
1579 p
->c_encoding
= CE_EXTERNAL
;
1581 /* Call the Init function for this external type */
1582 if ((*s2i
->si_init
)(p
) == NOTOK
)
1586 if (!strcasecmp (pm
->pm_name
, "name")) {
1587 e
->eb_name
= pm
->pm_value
;
1590 if (!strcasecmp (pm
->pm_name
, "permission")) {
1591 e
->eb_permission
= pm
->pm_value
;
1594 if (!strcasecmp (pm
->pm_name
, "site")) {
1595 e
->eb_site
= pm
->pm_value
;
1598 if (!strcasecmp (pm
->pm_name
, "directory")) {
1599 e
->eb_dir
= pm
->pm_value
;
1602 if (!strcasecmp (pm
->pm_name
, "mode")) {
1603 e
->eb_mode
= pm
->pm_value
;
1606 if (!strcasecmp (pm
->pm_name
, "size")) {
1607 sscanf (pm
->pm_value
, "%lu", &e
->eb_size
);
1610 if (!strcasecmp (pm
->pm_name
, "server")) {
1611 e
->eb_server
= pm
->pm_value
;
1614 if (!strcasecmp (pm
->pm_name
, "subject")) {
1615 e
->eb_subject
= pm
->pm_value
;
1618 if (!strcasecmp (pm
->pm_name
, "url")) {
1620 * According to RFC 2017, we have to remove all whitespace from
1624 char *u
, *p
= pm
->pm_value
;
1625 e
->eb_url
= u
= mh_xmalloc(strlen(pm
->pm_value
) + 1);
1627 for (; *p
!= '\0'; p
++) {
1628 if (! isspace((unsigned char) *p
))
1635 if (composing
&& !strcasecmp (pm
->pm_name
, "body")) {
1636 e
->eb_body
= getcpy (pm
->pm_value
);
1641 if (!e
->eb_access
) {
1642 inform("invalid parameters for \"%s/%s\" type in message %s's %s field",
1643 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1656 InitApplication (CT ct
)
1658 CI ci
= &ct
->c_ctinfo
;
1661 ct
->c_subtype
= ct_str_subtype (CT_APPLICATION
, ci
->ci_subtype
);
1668 * TRANSFER ENCODINGS
1672 init_encoding (CT ct
, OpenCEFunc openfnx
)
1674 ct
->c_ceopenfnx
= openfnx
;
1675 ct
->c_ceclosefnx
= close_encoding
;
1676 ct
->c_cesizefnx
= size_encoding
;
1683 close_encoding (CT ct
)
1685 CE ce
= &ct
->c_cefile
;
1694 static unsigned long
1695 size_encoding (CT ct
)
1700 CE ce
= &ct
->c_cefile
;
1703 if (ce
->ce_fp
&& fstat (fileno (ce
->ce_fp
), &st
) != NOTOK
)
1704 return (long) st
.st_size
;
1707 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1708 return (long) st
.st_size
;
1712 if (ct
->c_encoding
== CE_EXTERNAL
)
1713 return (ct
->c_end
- ct
->c_begin
);
1716 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
1717 return (ct
->c_end
- ct
->c_begin
);
1719 if (fstat (fd
, &st
) != NOTOK
)
1720 size
= (long) st
.st_size
;
1724 (*ct
->c_ceclosefnx
) (ct
);
1736 return init_encoding (ct
, openBase64
);
1741 openBase64 (CT ct
, char **file
)
1744 int fd
, own_ct_fp
= 0;
1745 char *cp
, *buffer
= NULL
;
1746 /* sbeck -- handle suffixes */
1748 CE ce
= &ct
->c_cefile
;
1749 unsigned char *decoded
;
1751 unsigned char digest
[16];
1754 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1759 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1760 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1766 if (*file
== NULL
) {
1769 ce
->ce_file
= mh_xstrdup(*file
);
1773 /* sbeck@cise.ufl.edu -- handle suffixes */
1775 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
1776 if (ce
->ce_unlink
) {
1777 /* Create temporary file with filename extension. */
1778 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1779 adios(NULL
, "unable to create temporary file in %s",
1783 ce
->ce_file
= add (cp
, ce
->ce_file
);
1785 } else if (*file
== NULL
) {
1787 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1788 adios(NULL
, "unable to create temporary file in %s",
1791 ce
->ce_file
= mh_xstrdup(tempfile
);
1794 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1795 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1799 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1800 adios (NULL
, "internal error(1)");
1802 buffer
= mh_xmalloc (len
+ 1);
1805 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1806 content_error (ct
->c_file
, ct
, "unable to open for reading");
1812 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
1815 switch (cc
= read (fd
, cp
, len
)) {
1817 content_error (ct
->c_file
, ct
, "error reading from");
1821 content_error (NULL
, ct
, "premature eof");
1832 /* decodeBase64() requires null-terminated input. */
1835 if (decodeBase64 (buffer
, &decoded
, &decoded_len
, ct
->c_type
== CT_TEXT
,
1836 ct
->c_digested
? digest
: NULL
) == OK
) {
1838 unsigned char *decoded_p
= decoded
;
1839 for (i
= 0; i
< decoded_len
; ++i
) {
1840 putc (*decoded_p
++, ce
->ce_fp
);
1843 if (ferror (ce
->ce_fp
)) {
1844 content_error (ce
->ce_file
, ct
, "error writing to");
1848 if (ct
->c_digested
) {
1849 if (memcmp(digest
, ct
->c_digest
,
1850 sizeof(digest
) / sizeof(digest
[0]))) {
1851 content_error (NULL
, ct
,
1852 "content integrity suspect (digest mismatch) -- continuing");
1855 fprintf (stderr
, "content integrity confirmed\n");
1863 fseek (ct
->c_fp
, 0L, SEEK_SET
);
1865 if (fflush (ce
->ce_fp
)) {
1866 content_error (ce
->ce_file
, ct
, "error writing to");
1870 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1873 *file
= ce
->ce_file
;
1879 return fileno (ce
->ce_fp
);
1886 free_encoding (ct
, 0);
1896 static char hex2nib
[0x80] = {
1897 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1898 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1899 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1900 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1901 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1902 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1903 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1904 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1905 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1906 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1907 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1908 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1909 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1910 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1911 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1912 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1919 return init_encoding (ct
, openQuoted
);
1924 openQuoted (CT ct
, char **file
)
1926 int cc
, digested
, len
, quoted
, own_ct_fp
= 0;
1932 CE ce
= &ct
->c_cefile
;
1933 /* sbeck -- handle suffixes */
1938 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1943 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1944 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1950 if (*file
== NULL
) {
1953 ce
->ce_file
= mh_xstrdup(*file
);
1957 /* sbeck@cise.ufl.edu -- handle suffixes */
1959 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
1960 if (ce
->ce_unlink
) {
1961 /* Create temporary file with filename extension. */
1962 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
1963 adios(NULL
, "unable to create temporary file in %s",
1967 ce
->ce_file
= add (cp
, ce
->ce_file
);
1969 } else if (*file
== NULL
) {
1971 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1972 adios(NULL
, "unable to create temporary file in %s",
1975 ce
->ce_file
= mh_xstrdup(tempfile
);
1978 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1979 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1983 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1984 adios (NULL
, "internal error(2)");
1987 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1988 content_error (ct
->c_file
, ct
, "unable to open for reading");
1994 if ((digested
= ct
->c_digested
))
1995 MD5Init (&mdContext
);
2002 fseek (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2004 if ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) == -1) {
2005 content_error (NULL
, ct
, "premature eof");
2009 if ((cc
= gotlen
) > len
)
2013 for (ep
= (cp
= bufp
) + cc
- 1; cp
<= ep
; ep
--)
2014 if (!isspace ((unsigned char) *ep
))
2019 for (; cp
< ep
; cp
++) {
2021 /* in an escape sequence */
2023 /* at byte 1 of an escape sequence */
2024 mask
= hex2nib
[((unsigned char) *cp
) & 0x7f];
2025 /* next is byte 2 */
2028 /* at byte 2 of an escape sequence */
2030 mask
|= hex2nib
[((unsigned char) *cp
) & 0x7f];
2031 putc (mask
, ce
->ce_fp
);
2033 MD5Update (&mdContext
, &mask
, 1);
2034 if (ferror (ce
->ce_fp
)) {
2035 content_error (ce
->ce_file
, ct
, "error writing to");
2038 /* finished escape sequence; next may be literal or a new
2039 * escape sequence */
2042 /* on to next byte */
2046 /* not in an escape sequence */
2048 /* starting an escape sequence, or invalid '='? */
2049 if (cp
+ 1 < ep
&& cp
[1] == '\n') {
2050 /* "=\n" soft line break, eat the \n */
2054 if (cp
+ 1 >= ep
|| cp
+ 2 >= ep
) {
2055 /* We don't have 2 bytes left, so this is an invalid
2056 * escape sequence; just show the raw bytes (below). */
2057 } else if (isxdigit ((unsigned char) cp
[1]) &&
2058 isxdigit ((unsigned char) cp
[2])) {
2059 /* Next 2 bytes are hex digits, making this a valid escape
2060 * sequence; let's decode it (above). */
2064 /* One or both of the next 2 is out of range, making this
2065 * an invalid escape sequence; just show the raw bytes
2069 /* Just show the raw byte. */
2070 putc (*cp
, ce
->ce_fp
);
2073 MD5Update (&mdContext
, (unsigned char *) "\r\n",2);
2075 MD5Update (&mdContext
, (unsigned char *) cp
, 1);
2078 if (ferror (ce
->ce_fp
)) {
2079 content_error (ce
->ce_file
, ct
, "error writing to");
2085 content_error (NULL
, ct
,
2086 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2090 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2092 if (fflush (ce
->ce_fp
)) {
2093 content_error (ce
->ce_file
, ct
, "error writing to");
2098 unsigned char digest
[16];
2100 MD5Final (digest
, &mdContext
);
2101 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
2102 sizeof(digest
) / sizeof(digest
[0])))
2103 content_error (NULL
, ct
,
2104 "content integrity suspect (digest mismatch) -- continuing");
2107 fprintf (stderr
, "content integrity confirmed\n");
2110 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2113 *file
= ce
->ce_file
;
2119 return fileno (ce
->ce_fp
);
2122 free_encoding (ct
, 0);
2139 if (init_encoding (ct
, open7Bit
) == NOTOK
)
2142 ct
->c_cesizefnx
= NULL
; /* no need to decode for real size */
2148 open7Bit (CT ct
, char **file
)
2150 int cc
, fd
, len
, own_ct_fp
= 0;
2151 char buffer
[BUFSIZ
];
2152 /* sbeck -- handle suffixes */
2155 CE ce
= &ct
->c_cefile
;
2158 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2163 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2164 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2170 if (*file
== NULL
) {
2173 ce
->ce_file
= mh_xstrdup(*file
);
2177 /* sbeck@cise.ufl.edu -- handle suffixes */
2179 if ((cp
= context_find_by_type ("suffix", ci
->ci_type
, ci
->ci_subtype
))) {
2180 if (ce
->ce_unlink
) {
2181 /* Create temporary file with filename extension. */
2182 if ((ce
->ce_file
= m_mktemps(invo_name
, cp
, NULL
, NULL
)) == NULL
) {
2183 adios(NULL
, "unable to create temporary file in %s",
2187 ce
->ce_file
= add (cp
, ce
->ce_file
);
2189 } else if (*file
== NULL
) {
2191 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2192 adios(NULL
, "unable to create temporary file in %s",
2195 ce
->ce_file
= mh_xstrdup(tempfile
);
2198 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2199 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2203 if (ct
->c_type
== CT_MULTIPART
) {
2204 CI ci
= &ct
->c_ctinfo
;
2208 fprintf (ce
->ce_fp
, "%s: %s/%s", TYPE_FIELD
, ci
->ci_type
, ci
->ci_subtype
);
2209 len
+= strlen (TYPE_FIELD
) + 2 + strlen (ci
->ci_type
)
2210 + 1 + strlen (ci
->ci_subtype
);
2211 buffer
= output_params(len
, ci
->ci_first_pm
, &len
, 0);
2214 fputs (buffer
, ce
->ce_fp
);
2218 if (ci
->ci_comment
) {
2219 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
2220 fputs ("\n\t", ce
->ce_fp
);
2224 putc (' ', ce
->ce_fp
);
2227 fprintf (ce
->ce_fp
, "(%s)", ci
->ci_comment
);
2230 fprintf (ce
->ce_fp
, "\n");
2232 fprintf (ce
->ce_fp
, "%s:%s", ID_FIELD
, ct
->c_id
);
2234 fprintf (ce
->ce_fp
, "%s:%s", DESCR_FIELD
, ct
->c_descr
);
2236 fprintf (ce
->ce_fp
, "%s:%s", DISPO_FIELD
, ct
->c_dispo
);
2237 fprintf (ce
->ce_fp
, "\n");
2240 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2241 adios (NULL
, "internal error(3)");
2244 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2245 content_error (ct
->c_file
, ct
, "unable to open for reading");
2251 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
2253 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
2255 content_error (ct
->c_file
, ct
, "error reading from");
2259 content_error (NULL
, ct
, "premature eof");
2267 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, ce
->ce_fp
) < cc
) {
2268 advise ("open7Bit", "fwrite");
2270 if (ferror (ce
->ce_fp
)) {
2271 content_error (ce
->ce_file
, ct
, "error writing to");
2276 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2278 if (fflush (ce
->ce_fp
)) {
2279 content_error (ce
->ce_file
, ct
, "error writing to");
2283 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2286 *file
= ce
->ce_file
;
2291 return fileno (ce
->ce_fp
);
2294 free_encoding (ct
, 0);
2308 openExternal (CT ct
, CT cb
, CE ce
, char **file
, int *fd
)
2310 char cachefile
[BUFSIZ
];
2313 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2318 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2319 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2325 if (find_cache (ct
, rcachesw
, (int *) 0, cb
->c_id
,
2326 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2327 if ((ce
->ce_fp
= fopen (cachefile
, "r"))) {
2328 ce
->ce_file
= mh_xstrdup(cachefile
);
2332 admonish (cachefile
, "unable to fopen for reading");
2335 *fd
= fileno (ce
->ce_fp
);
2339 *file
= ce
->ce_file
;
2340 *fd
= fileno (ce
->ce_fp
);
2351 return init_encoding (ct
, openFile
);
2356 openFile (CT ct
, char **file
)
2359 char cachefile
[BUFSIZ
];
2360 struct exbody
*e
= ct
->c_ctexbody
;
2361 CE ce
= &ct
->c_cefile
;
2363 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2375 content_error (NULL
, ct
, "missing name parameter");
2379 ce
->ce_file
= mh_xstrdup(e
->eb_name
);
2382 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2383 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2387 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2388 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2389 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2393 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2394 if ((fp
= fopen (cachefile
, "w"))) {
2396 char buffer
[BUFSIZ
];
2397 FILE *gp
= ce
->ce_fp
;
2399 fseek (gp
, 0L, SEEK_SET
);
2401 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2403 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2404 advise ("openFile", "fwrite");
2409 admonish (ce
->ce_file
, "error reading");
2410 (void) m_unlink (cachefile
);
2414 admonish (cachefile
, "error writing");
2415 (void) m_unlink (cachefile
);
2422 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2423 *file
= ce
->ce_file
;
2424 return fileno (ce
->ce_fp
);
2434 return init_encoding (ct
, openFTP
);
2439 openFTP (CT ct
, char **file
)
2441 int cachetype
, caching
, fd
;
2443 char *bp
, *ftp
, *user
, *pass
;
2444 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2446 CE ce
= &ct
->c_cefile
;
2447 static char *username
= NULL
;
2448 static char *password
= NULL
;
2452 if ((ftp
= context_find (nmhaccessftp
)) && !*ftp
)
2458 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2469 if (!e
->eb_name
|| !e
->eb_site
) {
2470 content_error (NULL
, ct
, "missing %s parameter",
2471 e
->eb_name
? "site": "name");
2475 /* Get the buffer ready to go */
2477 buflen
= sizeof(buffer
);
2480 * Construct the query message for user
2482 snprintf (bp
, buflen
, "Retrieve %s", e
->eb_name
);
2488 snprintf (bp
, buflen
, " (content %s)", e
->eb_partno
);
2494 snprintf (bp
, buflen
, "\n using %sFTP from site %s",
2495 e
->eb_flags
? "anonymous " : "", e
->eb_site
);
2500 if (e
->eb_size
> 0) {
2501 snprintf (bp
, buflen
, " (%lu octets)", e
->eb_size
);
2506 snprintf (bp
, buflen
, "? ");
2509 * Now, check the answer
2511 if (!read_yes_or_no_if_tty (buffer
))
2516 snprintf (buffer
, sizeof(buffer
), "%s@%s", getusername (),
2520 ruserpass (e
->eb_site
, &username
, &password
, 0);
2525 ce
->ce_unlink
= (*file
== NULL
);
2527 cachefile
[0] = '\0';
2528 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2529 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2530 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2531 if (*file
== NULL
) {
2538 ce
->ce_file
= mh_xstrdup(*file
);
2540 ce
->ce_file
= mh_xstrdup(cachefile
);
2543 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2544 adios(NULL
, "unable to create temporary file in %s",
2547 ce
->ce_file
= mh_xstrdup(tempfile
);
2550 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2551 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2556 int child_id
, i
, vecp
;
2560 vec
[vecp
++] = r1bindex (ftp
, '/');
2561 vec
[vecp
++] = e
->eb_site
;
2564 vec
[vecp
++] = e
->eb_dir
;
2565 vec
[vecp
++] = e
->eb_name
;
2566 vec
[vecp
++] = ce
->ce_file
,
2567 vec
[vecp
++] = e
->eb_mode
&& !strcasecmp (e
->eb_mode
, "ascii")
2568 ? "ascii" : "binary";
2573 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
2577 adios ("fork", "unable to");
2581 close (fileno (ce
->ce_fp
));
2583 fprintf (stderr
, "unable to exec ");
2589 if (pidXwait (child_id
, NULL
)) {
2590 username
= password
= NULL
;
2600 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
2605 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2606 if ((fp
= fopen (cachefile
, "w"))) {
2608 FILE *gp
= ce
->ce_fp
;
2610 fseek (gp
, 0L, SEEK_SET
);
2612 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2614 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2615 advise ("openFTP", "fwrite");
2620 admonish (ce
->ce_file
, "error reading");
2621 (void) m_unlink (cachefile
);
2625 admonish (cachefile
, "error writing");
2626 (void) m_unlink (cachefile
);
2634 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2635 *file
= ce
->ce_file
;
2636 return fileno (ce
->ce_fp
);
2647 return init_encoding (ct
, openMail
);
2652 openMail (CT ct
, char **file
)
2654 int child_id
, fd
, i
, vecp
;
2656 char *bp
, buffer
[BUFSIZ
], *vec
[7];
2657 struct exbody
*e
= ct
->c_ctexbody
;
2658 CE ce
= &ct
->c_cefile
;
2660 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2671 if (!e
->eb_server
) {
2672 content_error (NULL
, ct
, "missing server parameter");
2676 /* Get buffer ready to go */
2678 buflen
= sizeof(buffer
);
2680 /* Now, construct query message */
2681 snprintf (bp
, buflen
, "Retrieve content");
2687 snprintf (bp
, buflen
, " %s", e
->eb_partno
);
2693 snprintf (bp
, buflen
, " by asking %s\n\n%s\n? ",
2695 e
->eb_subject
? e
->eb_subject
: e
->eb_body
);
2697 /* Now, check answer */
2698 if (!read_yes_or_no_if_tty (buffer
))
2702 vec
[vecp
++] = r1bindex (mailproc
, '/');
2703 vec
[vecp
++] = e
->eb_server
;
2704 vec
[vecp
++] = "-subject";
2705 vec
[vecp
++] = e
->eb_subject
? e
->eb_subject
: "mail-server request";
2706 vec
[vecp
++] = "-body";
2707 vec
[vecp
++] = e
->eb_body
;
2710 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
2714 advise ("fork", "unable to");
2718 execvp (mailproc
, vec
);
2719 fprintf (stderr
, "unable to exec ");
2725 if (pidXwait (child_id
, NULL
) == OK
)
2726 inform("request sent");
2730 if (*file
== NULL
) {
2732 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2733 adios(NULL
, "unable to create temporary file in %s",
2736 ce
->ce_file
= mh_xstrdup(tempfile
);
2739 ce
->ce_file
= mh_xstrdup(*file
);
2743 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2744 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2748 /* showproc is for mhshow and mhstore, though mhlist -debug
2749 * prints it, too. */
2750 mh_xfree(ct
->c_showproc
);
2751 ct
->c_showproc
= mh_xstrdup("true");
2753 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2754 *file
= ce
->ce_file
;
2755 return fileno (ce
->ce_fp
);
2766 return init_encoding (ct
, openURL
);
2771 openURL (CT ct
, char **file
)
2773 struct exbody
*e
= ct
->c_ctexbody
;
2774 CE ce
= &ct
->c_cefile
;
2775 char *urlprog
, *program
;
2776 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2777 int fd
, caching
, cachetype
;
2778 struct msgs_array args
= { 0, 0, NULL
};
2781 if ((urlprog
= context_find(nmhaccessurl
)) && *urlprog
== '\0')
2785 content_error(NULL
, ct
, "No entry for nmh-access-url in profile");
2789 switch (openExternal(e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2801 content_error(NULL
, ct
, "missing url parameter");
2805 ce
->ce_unlink
= (*file
== NULL
);
2807 cachefile
[0] = '\0';
2809 if (find_cache(NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2810 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2811 if (*file
== NULL
) {
2818 ce
->ce_file
= mh_xstrdup(*file
);
2820 ce
->ce_file
= mh_xstrdup(cachefile
);
2823 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2824 adios(NULL
, "unable to create temporary file in %s",
2827 ce
->ce_file
= mh_xstrdup(tempfile
);
2830 if ((ce
->ce_fp
= fopen(ce
->ce_file
, "w+")) == NULL
) {
2831 content_error(ce
->ce_file
, ct
, "unable to fopen for read/writing");
2835 switch (child_id
= fork()) {
2837 adios ("fork", "unable to");
2841 argsplit_msgarg(&args
, urlprog
, &program
);
2842 app_msgarg(&args
, e
->eb_url
);
2843 app_msgarg(&args
, NULL
);
2844 dup2(fileno(ce
->ce_fp
), 1);
2845 close(fileno(ce
->ce_fp
));
2846 execvp(program
, args
.msgs
);
2847 fprintf(stderr
, "Unable to exec ");
2853 if (pidXwait(child_id
, NULL
)) {
2861 chmod(cachefile
, cachetype
? m_gmprot() : 0444);
2866 mask
= umask (cachetype
? ~m_gmprot() : 0222);
2867 if ((fp
= fopen(cachefile
, "w"))) {
2869 FILE *gp
= ce
->ce_fp
;
2871 fseeko(gp
, 0, SEEK_SET
);
2873 while ((cc
= fread(buffer
, sizeof(*buffer
),
2874 sizeof(buffer
), gp
)) > 0)
2875 if ((int) fwrite(buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
2876 advise ("openURL", "fwrite");
2882 admonish(ce
->ce_file
, "error reading");
2883 (void) m_unlink (cachefile
);
2890 fseeko(ce
->ce_fp
, 0, SEEK_SET
);
2891 *file
= ce
->ce_file
;
2897 * Stores MD5 digest (in cp, from Content-MD5 header) in ct->c_digest. It
2898 * has to be base64 decoded.
2901 readDigest (CT ct
, char *cp
)
2903 unsigned char *digest
;
2906 if (decodeBase64 (cp
, &digest
, &len
, 0, NULL
) == OK
) {
2907 const size_t maxlen
= sizeof ct
->c_digest
/ sizeof ct
->c_digest
[0];
2909 if (strlen ((char *) digest
) <= maxlen
) {
2910 memcpy (ct
->c_digest
, digest
, maxlen
);
2915 fprintf (stderr
, "MD5 digest=");
2916 for (i
= 0; i
< maxlen
; ++i
) {
2917 fprintf (stderr
, "%02x", ct
->c_digest
[i
] & 0xff);
2919 fprintf (stderr
, "\n");
2925 fprintf (stderr
, "invalid MD5 digest (got %d octets)\n",
2926 (int) strlen ((char *) digest
));
2936 /* Multipart parts might have content before the first subpart and/or
2937 after the last subpart that hasn't been stored anywhere else, so do
2940 get_leftover_mp_content (CT ct
, int before
/* or after */)
2942 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2944 int found_boundary
= 0;
2950 char *content
= NULL
;
2952 if (! m
) return NOTOK
;
2955 if (! m
->mp_parts
|| ! m
->mp_parts
->mp_part
) return NOTOK
;
2957 /* Isolate the beginning of this part to the beginning of the
2958 first subpart and save any content between them. */
2959 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2960 max
= m
->mp_parts
->mp_part
->c_begin
- ct
->c_begin
;
2961 boundary
= concat ("--", m
->mp_start
, NULL
);
2963 struct part
*last_subpart
= NULL
;
2964 struct part
*subpart
;
2966 /* Go to the last subpart to get its end position. */
2967 for (subpart
= m
->mp_parts
; subpart
; subpart
= subpart
->mp_next
) {
2968 last_subpart
= subpart
;
2971 if (last_subpart
== NULL
) return NOTOK
;
2973 /* Isolate the end of the last subpart to the end of this part
2974 and save any content between them. */
2975 fseeko (ct
->c_fp
, last_subpart
->mp_part
->c_end
, SEEK_SET
);
2976 max
= ct
->c_end
- last_subpart
->mp_part
->c_end
;
2977 boundary
= concat ("--", m
->mp_stop
, NULL
);
2980 /* Back up by 1 to pick up the newline. */
2981 while ((gotlen
= getline(&bufp
, &buflen
, ct
->c_fp
)) != -1) {
2983 /* Don't look beyond beginning of first subpart (before) or
2984 next part (after). */
2985 if (read
> max
) bufp
[read
-max
] = '\0';
2988 if (! strcmp (bufp
, boundary
)) {
2992 if (! found_boundary
&& ! strcmp (bufp
, boundary
)) {
2998 if ((before
&& ! found_boundary
) || (! before
&& found_boundary
)) {
3000 char *old_content
= content
;
3001 content
= concat (content
, bufp
, NULL
);
3005 ? concat ("\n", bufp
, NULL
)
3006 : concat (bufp
, NULL
);
3011 if (found_boundary
|| read
> max
) break;
3013 if (read
> max
) break;
3017 /* Skip the newline if that's all there is. */
3021 /* Remove trailing newline, except at EOF. */
3022 if ((before
|| ! feof (ct
->c_fp
)) &&
3023 (cp
= content
+ strlen (content
)) > content
&&
3028 if (strlen (content
) > 1) {
3030 m
->mp_content_before
= content
;
3032 m
->mp_content_after
= content
;
3047 ct_type_str (int type
) {
3049 case CT_APPLICATION
:
3050 return "application";
3066 return "unknown_type";
3072 ct_subtype_str (int type
, int subtype
) {
3074 case CT_APPLICATION
:
3076 case APPLICATION_OCTETS
:
3078 case APPLICATION_POSTSCRIPT
:
3079 return "postscript";
3081 return "unknown_app_subtype";
3085 case MESSAGE_RFC822
:
3087 case MESSAGE_PARTIAL
:
3089 case MESSAGE_EXTERNAL
:
3092 return "unknown_msg_subtype";
3098 case MULTI_ALTERNATE
:
3099 return "alternative";
3102 case MULTI_PARALLEL
:
3107 return "unknown_multipart_subtype";
3118 return "unknown_text_subtype";
3121 return "unknown_type";
3127 ct_str_type (const char *type
) {
3128 struct str2init
*s2i
;
3130 for (s2i
= str2cts
; s2i
->si_key
; ++s2i
) {
3131 if (! strcasecmp (type
, s2i
->si_key
)) {
3135 if (! s2i
->si_key
&& ! uprf (type
, "X-")) {
3144 ct_str_subtype (int type
, const char *subtype
) {
3148 case CT_APPLICATION
:
3149 for (kv
= SubApplication
; kv
->kv_key
; ++kv
) {
3150 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3154 return kv
->kv_value
;
3156 for (kv
= SubMessage
; kv
->kv_key
; ++kv
) {
3157 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3161 return kv
->kv_value
;
3163 for (kv
= SubMultiPart
; kv
->kv_key
; ++kv
) {
3164 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3168 return kv
->kv_value
;
3170 for (kv
= SubText
; kv
->kv_key
; ++kv
) {
3171 if (! strcasecmp (subtype
, kv
->kv_key
)) {
3175 return kv
->kv_value
;
3182 /* Find the content type and InitFunc for the CT. */
3183 const struct str2init
*
3184 get_ct_init (int type
) {
3185 const struct str2init
*sp
;
3187 for (sp
= str2cts
; sp
->si_key
; ++sp
) {
3188 if (type
== sp
->si_val
) {
3197 ce_str (int encoding
) {
3202 return "quoted-printable";
3218 /* Find the content type and InitFunc for the content encoding method. */
3219 const struct str2init
*
3220 get_ce_method (const char *method
) {
3221 struct str2init
*sp
;
3223 for (sp
= str2ces
; sp
->si_key
; ++sp
) {
3224 if (! strcasecmp (method
, sp
->si_key
)) {
3233 * Parse a series of MIME attributes (or parameters) given a header as
3236 * Arguments include:
3238 * filename - Name of input file (for error messages)
3239 * fieldname - Name of field being processed
3240 * headerp - Pointer to pointer of the beginning of the MIME attributes.
3241 * Updated to point to end of attributes when finished.
3242 * param_head - Pointer to head of parameter list
3243 * param_tail - Pointer to tail of parameter list
3244 * commentp - Pointer to header comment pointer (may be NULL)
3246 * Returns OK if parsing was successful, NOTOK if parsing failed, and
3247 * DONE to indicate a benign error (minor parsing error, but the program
3252 parse_header_attrs (const char *filename
, const char *fieldname
,
3253 char **header_attrp
, PM
*param_head
, PM
*param_tail
,
3256 char *cp
= *header_attrp
;
3262 struct sectlist
*next
;
3268 struct sectlist
*sechead
;
3269 struct parmlist
*next
;
3270 } *pp
, *pp2
, *phead
= NULL
;
3272 while (*cp
== ';') {
3273 char *dp
, *vp
, *up
, *nameptr
, *valptr
, *charset
= NULL
, *lang
= NULL
;
3274 int encoded
= 0, partial
= 0, len
= 0, index
= 0;
3277 while (isspace ((unsigned char) *cp
))
3281 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3286 if (! suppress_extraneous_trailing_semicolon_warning
) {
3287 inform("extraneous trailing ';' in message %s's %s: "
3288 "parameter list", filename
, fieldname
);
3293 /* down case the attribute name */
3294 for (dp
= cp
; istoken ((unsigned char) *dp
); dp
++)
3295 *dp
= tolower ((unsigned char) *dp
);
3297 for (up
= dp
; isspace ((unsigned char) *dp
);)
3299 if (dp
== cp
|| *dp
!= '=') {
3300 inform("invalid parameter in message %s's %s: "
3301 "field\n%*sparameter %s (error detected at offset %d)",
3302 filename
, fieldname
, strlen(invo_name
) + 2, "",cp
, dp
- cp
);
3307 * To handle RFC 2231, we have to deal with the following extensions:
3309 * name*=encoded-value
3310 * name*<N>=part-N-of-a-parameter-value
3311 * name*<N>*=encoded-part-N-of-a-parameter-value
3314 * If there's a * right before the equal sign, it's encoded.
3315 * If there's a * and one or more digits, then it's section N.
3317 * Remember we can have one or the other, or both. cp points to
3318 * beginning of name, up points past the last character in the
3322 for (vp
= cp
; vp
< up
; vp
++) {
3323 if (*vp
== '*' && vp
< up
- 1) {
3327 if (*vp
== '*' && vp
== up
- 1) {
3329 } else if (partial
) {
3330 if (isdigit((unsigned char) *vp
))
3331 index
= *vp
- '0' + index
* 10;
3333 inform("invalid parameter index in message %s's "
3334 "%s: field\n%*s(parameter %s)", filename
,
3335 fieldname
, strlen(invo_name
) + 2, "", cp
);
3344 * Break out the parameter name and value sections and allocate
3348 nameptr
= mh_xmalloc(len
+ 1);
3349 strncpy(nameptr
, cp
, len
);
3350 nameptr
[len
] = '\0';
3352 for (dp
++; isspace ((unsigned char) *dp
);)
3357 * Single quotes delimit the character set and language tag.
3358 * They are required on the first section (or a complete
3363 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3369 charset
= mh_xmalloc(len
+ 1);
3370 strncpy(charset
, dp
, len
);
3371 charset
[len
] = '\0';
3377 inform("missing charset in message %s's %s: "
3378 "field\n%*s(parameter %s)", filename
, fieldname
,
3379 strlen(invo_name
) + 2, "", nameptr
);
3385 while (*vp
!= '\'' && !isspace((unsigned char) *vp
) &&
3392 lang
= mh_xmalloc(len
+ 1);
3393 strncpy(lang
, dp
, len
);
3400 inform("missing language tag in message %s's %s: "
3401 "field\n%*s(parameter %s)", filename
, fieldname
,
3402 strlen(invo_name
) + 2, "", nameptr
);
3412 * At this point vp should be pointing at the beginning
3413 * of the encoded value/section. Continue until we reach
3414 * the end or get whitespace. But first, calculate the
3415 * length so we can allocate the correct buffer size.
3418 for (vp
= dp
, len
= 0; istoken(*vp
); vp
++) {
3420 if (*(vp
+ 1) == '\0' ||
3421 !isxdigit((unsigned char) *(vp
+ 1)) ||
3422 *(vp
+ 2) == '\0' ||
3423 !isxdigit((unsigned char) *(vp
+ 2))) {
3424 inform("invalid encoded sequence in message "
3425 "%s's %s: field\n%*s(parameter %s)",
3426 filename
, fieldname
, strlen(invo_name
) + 2,
3438 up
= valptr
= mh_xmalloc(len
+ 1);
3440 for (vp
= dp
; istoken(*vp
); vp
++) {
3442 *up
++ = decode_qp(*(vp
+ 1), *(vp
+ 2));
3453 * A "normal" string. If it's got a leading quote, then we
3454 * strip the quotes out. Otherwise go until we reach the end
3455 * or get whitespace. Note we scan it twice; once to get the
3456 * length, then the second time copies it into the destination
3463 for (cp
= dp
+ 1;;) {
3467 inform("invalid quoted-string in message %s's %s: "
3468 "field\n%*s(parameter %s)", filename
,
3469 fieldname
, strlen(invo_name
) + 2, "", nameptr
);
3489 for (cp
= dp
; istoken (*cp
); cp
++) {
3494 valptr
= mh_xmalloc(len
+ 1);
3498 for (cp
= dp
+ 1, vp
= valptr
, i
= 0; i
< len
; i
++) {
3506 strncpy(valptr
, cp
= dp
, len
);
3514 * If 'partial' is set, we don't allocate a parameter now. We
3515 * put it on the parameter linked list to be reassembled later.
3517 * "phead" points to a list of all parameters we need to reassemble.
3518 * Each parameter has a list of sections. We insert the sections in
3523 for (pp
= phead
; pp
!= NULL
; pp
= pp
->next
) {
3524 if (strcasecmp(nameptr
, pp
->name
) == 0) {
3539 * Insert this into the section linked list
3547 if (pp
->sechead
== NULL
|| pp
->sechead
->index
> index
) {
3548 sp
->next
= pp
->sechead
;
3551 for (sp2
= pp
->sechead
; sp2
!= NULL
; sp2
= sp2
->next
) {
3552 if (sp2
->index
== sp
->index
) {
3553 inform("duplicate index (%d) in message "
3554 "%s's %s: field\n%*s(parameter %s)", sp
->index
,
3555 filename
, fieldname
, strlen(invo_name
) + 2, "",
3559 if (sp2
->index
< sp
->index
&&
3560 (sp2
->next
== NULL
|| sp2
->next
->index
> sp
->index
)) {
3561 sp
->next
= sp2
->next
;
3568 inform("Internal error: cannot insert partial "
3569 "param in message %s's %s: field\n%*s(parameter %s)",
3570 filename
, fieldname
, strlen(invo_name
) + 2, "",
3577 * Save our charset and lang tags.
3580 if (index
== 0 && encoded
) {
3581 mh_xfree(pp
->charset
);
3582 pp
->charset
= charset
;
3587 pm
= add_param(param_head
, param_tail
, nameptr
, valptr
, 1);
3588 pm
->pm_charset
= charset
;
3592 while (isspace ((unsigned char) *cp
))
3596 get_comment (filename
, fieldname
, &cp
, commentp
) == NOTOK
) {
3602 * Now that we're done, reassemble all of the partial parameters.
3605 for (pp
= phead
; pp
!= NULL
; ) {
3609 for (sp
= pp
->sechead
; sp
!= NULL
; sp
= sp
->next
) {
3610 if (sp
->index
!= pindex
++) {
3611 inform("missing section %d for parameter in "
3612 "message %s's %s: field\n%*s(parameter %s)", pindex
- 1,
3613 filename
, fieldname
, strlen(invo_name
) + 2, "",
3620 p
= q
= mh_xmalloc(tlen
+ 1);
3621 for (sp
= pp
->sechead
; sp
!= NULL
; ) {
3622 memcpy(q
, sp
->value
, sp
->len
);
3632 pm
= add_param(param_head
, param_tail
, pp
->name
, p
, 1);
3633 pm
->pm_charset
= pp
->charset
;
3634 pm
->pm_lang
= pp
->lang
;
3645 * Return the charset for a particular content type.
3649 content_charset (CT ct
) {
3650 char *ret_charset
= NULL
;
3652 ret_charset
= get_param(ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 0);
3654 return ret_charset
? ret_charset
: mh_xstrdup("US-ASCII");
3659 * Create a string based on a list of output parameters. Assume that this
3660 * parameter string will be appended to an existing header, so start out
3661 * with the separator (;). Perform RFC 2231 encoding when necessary.
3665 output_params(size_t initialwidth
, PM params
, int *offsetout
, int external
)
3667 char *paramout
= NULL
;
3668 char line
[CPERLIN
* 2], *q
;
3669 int curlen
, index
, cont
, encode
, i
;
3670 size_t valoff
, numchars
;
3672 while (params
!= NULL
) {
3678 if (external
&& strcasecmp(params
->pm_name
, "body") == 0)
3681 if (strlen(params
->pm_name
) > CPERLIN
) {
3682 inform("Parameter name \"%s\" is too long", params
->pm_name
);
3687 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
, &numchars
);
3690 * Loop until we get a parameter that fits within a line. We
3691 * assume new lines start with a tab, so check our overflow based
3701 * At this point we're definitely continuing the line, so
3702 * be sure to include the parameter name and section index.
3705 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3706 params
->pm_name
, index
);
3709 * Both of these functions do a NUL termination
3713 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3714 numchars
, valoff
, index
);
3716 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3726 curlen
= param_len(params
, index
, valoff
, &encode
, &cont
,
3731 * "line" starts with a ;\n\t, so that doesn't count against
3732 * the length. But add 8 since it starts with a tab; that's
3733 * how we end up with 5.
3736 initialwidth
= strlen(line
) + 5;
3739 * At this point the line should be built, so add it to our
3740 * current output buffer.
3743 paramout
= add(line
, paramout
);
3747 * If this won't fit on the line, start a new one. Save room in
3748 * case we need a semicolon on the end
3751 if (initialwidth
+ curlen
> CPERLIN
- 1) {
3763 * At this point, we're either finishing a continued parameter, or
3764 * we're working on a new one.
3768 q
+= snprintf(q
, sizeof(line
) - (q
- line
), "%s*%d",
3769 params
->pm_name
, index
);
3771 strncpy(q
, params
->pm_name
, sizeof(line
) - (q
- line
));
3776 i
= encode_param(params
, q
, sizeof(line
) - (q
- line
),
3777 strlen(params
->pm_value
+ valoff
), valoff
, index
);
3779 i
= normal_param(params
, q
, sizeof(line
) - (q
- line
),
3780 strlen(params
->pm_value
+ valoff
), valoff
);
3787 paramout
= add(line
, paramout
);
3788 initialwidth
+= strlen(line
);
3790 params
= params
->pm_next
;
3794 *offsetout
= initialwidth
;
3800 * Calculate the size of a parameter.
3804 * pm - The parameter being output
3805 * index - If continuing the parameter, the index of the section
3807 * valueoff - The current offset into the parameter value that we're
3808 * working on (previous sections have consumed valueoff bytes).
3809 * encode - Set if we should perform encoding on this parameter section
3810 * (given that we're consuming bytesfit bytes).
3811 * cont - Set if the remaining data in value will not fit on a single
3812 * line and will need to be continued.
3813 * bytesfit - The number of bytes that we can consume from the parameter
3814 * value and still fit on a completely new line. The
3815 * calculation assumes the new line starts with a tab,
3816 * includes the parameter name and any encoding, and fits
3817 * within CPERLIN bytes. Will always be at least 1.
3821 param_len(PM pm
, int index
, size_t valueoff
, int *encode
, int *cont
,
3824 char *start
= pm
->pm_value
+ valueoff
, *p
, indexchar
[32];
3825 size_t len
= 0, fit
= 0;
3826 int fitlimit
= 0, eightbit
, maxfit
;
3831 * Add up the length. First, start with the parameter name.
3834 len
= strlen(pm
->pm_name
);
3837 * Scan the parameter value and see if we need to do encoding for this
3841 eightbit
= contains8bit(start
, NULL
);
3844 * Determine if we need to encode this section. Encoding is necessary if:
3846 * - There are any 8-bit characters at all and we're on the first
3848 * - There are 8-bit characters within N bytes of our section start.
3849 * N is calculated based on the number of bytes it would take to
3850 * reach CPERLIN. Specifically:
3851 * 8 (starting tab) +
3852 * strlen(param name) +
3853 * 4 ('* for section marker, '=', opening/closing '"')
3855 * is the number of bytes used by everything that isn't part of the
3856 * value. So that gets subtracted from CPERLIN.
3859 snprintf(indexchar
, sizeof(indexchar
), "%d", index
);
3860 maxfit
= CPERLIN
- (12 + len
+ strlen(indexchar
));
3861 if ((eightbit
&& index
== 0) || contains8bit(start
, start
+ maxfit
)) {
3865 len
++; /* Add in equal sign */
3869 * We're using maxfit as a marker for how many characters we can
3870 * fit into the line. Bump it by two because we're not using quotes
3877 * If we don't have a charset or language tag in this parameter,
3881 if (! pm
->pm_charset
) {
3882 pm
->pm_charset
= mh_xstrdup(write_charset_8bit());
3883 if (strcasecmp(pm
->pm_charset
, "US-ASCII") == 0)
3884 adios(NULL
, "8-bit characters in parameter \"%s\", but "
3885 "local character set is US-ASCII", pm
->pm_name
);
3888 pm
->pm_lang
= mh_xstrdup(""); /* Default to a blank lang tag */
3890 len
++; /* For the encoding marker */
3893 int enclen
= strlen(pm
->pm_charset
) + strlen(pm
->pm_lang
) + 2;
3898 * We know we definitely need to include an index. maxfit already
3899 * includes the section marker.
3901 len
+= strlen(indexchar
);
3903 for (p
= start
; *p
!= '\0'; p
++) {
3904 if (isparamencode(*p
)) {
3912 * Just so there's no confusion: maxfit is counting OUTPUT
3913 * characters (post-encoding). fit is counting INPUT characters.
3915 if (! fitlimit
&& maxfit
>= 0)
3917 else if (! fitlimit
)
3922 * Calculate the string length, but add room for quoting \
3923 * and " if necessary. Also account for quotes at beginning
3926 for (p
= start
; *p
!= '\0'; p
++) {
3937 if (! fitlimit
&& maxfit
>= 0)
3939 else if (! fitlimit
)
3956 * Output an encoded parameter string.
3960 encode_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
3961 size_t valueoff
, int index
)
3963 size_t outlen
= 0, n
;
3964 char *endptr
= output
+ len
, *p
;
3967 * First, output the marker for an encoded string.
3975 * If the index is 0, output the character set and language tag.
3976 * If theses were NULL, they should have already been filled in
3981 n
= snprintf(output
, len
- outlen
, "%s'%s'", pm
->pm_charset
,
3985 if (output
> endptr
) {
3986 inform("Internal error: parameter buffer overflow");
3992 * Copy over the value, encoding if necessary
3995 p
= pm
->pm_value
+ valueoff
;
3996 while (valuelen
-- > 0) {
3997 if (isparamencode(*p
)) {
3998 n
= snprintf(output
, len
- outlen
, "%%%02X", (unsigned char) *p
++);
4005 if (output
> endptr
) {
4006 inform("Internal error: parameter buffer overflow");
4017 * Output a "normal" parameter, without encoding. Be sure to escape
4018 * quotes and backslashes if necessary.
4022 normal_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
4026 char *endptr
= output
+ len
, *p
;
4032 p
= pm
->pm_value
+ valueoff
;
4034 while (valuelen
-- > 0) {
4045 if (output
> endptr
) {
4046 inform("Internal error: parameter buffer overflow");
4051 if (output
- 2 > endptr
) {
4052 inform("Internal error: parameter buffer overflow");
4063 * Add a parameter to the parameter linked list
4067 add_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4072 pm
->pm_name
= nocopy
? name
: getcpy(name
);
4073 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4076 (*last
)->pm_next
= pm
;
4087 * Either replace a current parameter with a new value, or add the parameter
4088 * to the parameter linked list.
4092 replace_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
)
4096 for (pm
= *first
; pm
!= NULL
; pm
= pm
->pm_next
) {
4097 if (strcasecmp(name
, pm
->pm_name
) == 0) {
4099 * If nocopy is set, it's assumed that we own both name
4100 * and value. We don't need name, so we discard it now.
4105 pm
->pm_value
= nocopy
? value
: getcpy(value
);
4110 return add_param(first
, last
, name
, value
, nocopy
);
4114 * Retrieve a parameter value from a parameter linked list. If the parameter
4115 * value needs converted to the local character set, do that now.
4119 get_param(PM first
, const char *name
, char replace
, int fetchonly
)
4121 while (first
!= NULL
) {
4122 if (strcasecmp(name
, first
->pm_name
) == 0) {
4124 return first
->pm_value
;
4125 return getcpy(get_param_value(first
, replace
));
4127 first
= first
->pm_next
;
4134 * Return a parameter value, converting to the local character set if
4138 char *get_param_value(PM pm
, char replace
)
4140 static char buffer
[4096]; /* I hope no parameters are larger */
4141 size_t bufsize
= sizeof(buffer
);
4146 ICONV_CONST
char *p
;
4147 #else /* HAVE_ICONV */
4149 #endif /* HAVE_ICONV */
4154 * If we don't have a character set indicated, it's assumed to be
4155 * US-ASCII. If it matches our character set, we don't need to convert
4159 if (!pm
->pm_charset
|| check_charset(pm
->pm_charset
,
4160 strlen(pm
->pm_charset
))) {
4161 return pm
->pm_value
;
4165 * In this case, we need to convert. If we have iconv support, use
4166 * that. Otherwise, go through and simply replace every non-ASCII
4167 * character with the substitution character.
4172 bufsize
= sizeof(buffer
);
4173 utf8
= strcasecmp(pm
->pm_charset
, "UTF-8") == 0;
4175 cd
= iconv_open(get_charset(), pm
->pm_charset
);
4176 if (cd
== (iconv_t
) -1) {
4180 inbytes
= strlen(pm
->pm_value
);
4184 if (iconv(cd
, &p
, &inbytes
, &q
, &bufsize
) == (size_t)-1) {
4185 if (errno
!= EILSEQ
) {
4190 * Reset shift state, substitute our character,
4191 * try to restart conversion.
4194 iconv(cd
, NULL
, NULL
, &q
, &bufsize
);
4207 for (++p
, --inbytes
;
4208 inbytes
> 0 && (((unsigned char) *p
) & 0xc0) == 0x80;
4227 #endif /* HAVE_ICONV */
4230 * Take everything non-ASCII and substitute the replacement character
4234 bufsize
= sizeof(buffer
);
4235 for (p
= pm
->pm_value
; *p
!= '\0' && bufsize
> 1; p
++, q
++, bufsize
--) {
4236 if (isascii((unsigned char) *p
) && isprint((unsigned char) *p
))