3 * mhparse.c -- routines to parse the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
23 extern pid_t xpid
; /* mhshowsbr.c */
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.
37 int skip_mp_cte_check
;
38 int suppress_bogus_mp_content_warning
;
42 * Structures for TEXT messages
44 struct k2v SubText
[] = {
45 { "plain", TEXT_PLAIN
},
46 { "richtext", TEXT_RICHTEXT
}, /* defined in RFC-1341 */
47 { "enriched", TEXT_ENRICHED
}, /* defined in RFC-1896 */
48 { NULL
, TEXT_UNKNOWN
} /* this one must be last! */
51 /* Charset[] removed -- yozo. Mon Oct 8 01:03:41 JST 2012 */
54 * Structures for MULTIPART messages
56 struct k2v SubMultiPart
[] = {
57 { "mixed", MULTI_MIXED
},
58 { "alternative", MULTI_ALTERNATE
},
59 { "digest", MULTI_DIGEST
},
60 { "parallel", MULTI_PARALLEL
},
61 { NULL
, MULTI_UNKNOWN
} /* this one must be last! */
65 * Structures for MESSAGE messages
67 struct k2v SubMessage
[] = {
68 { "rfc822", MESSAGE_RFC822
},
69 { "partial", MESSAGE_PARTIAL
},
70 { "external-body", MESSAGE_EXTERNAL
},
71 { NULL
, MESSAGE_UNKNOWN
} /* this one must be last! */
75 * Structure for APPLICATION messages
77 struct k2v SubApplication
[] = {
78 { "octet-stream", APPLICATION_OCTETS
},
79 { "postscript", APPLICATION_POSTSCRIPT
},
80 { NULL
, APPLICATION_UNKNOWN
} /* this one must be last! */
85 int find_cache (CT
, int, int *, char *, char *, int);
88 int part_ok (CT
, int);
89 int type_ok (CT
, int);
90 void content_error (char *, CT
, char *, ...);
93 void free_encoding (CT
, int);
98 static CT
get_content (FILE *, char *, int);
99 static int get_comment (const char *, CI
, char **, int);
101 static int InitGeneric (CT
);
102 static int InitText (CT
);
103 static int InitMultiPart (CT
);
104 void reverse_parts (CT
);
105 static int InitMessage (CT
);
106 static int InitApplication (CT
);
107 static int init_encoding (CT
, OpenCEFunc
);
108 static unsigned long size_encoding (CT
);
109 static int InitBase64 (CT
);
110 static int openBase64 (CT
, char **);
111 static int InitQuoted (CT
);
112 static int openQuoted (CT
, char **);
113 static int Init7Bit (CT
);
114 static int openExternal (CT
, CT
, CE
, char **, int *);
115 static int InitFile (CT
);
116 static int openFile (CT
, char **);
117 static int InitFTP (CT
);
118 static int openFTP (CT
, char **);
119 static int InitMail (CT
);
120 static int openMail (CT
, char **);
121 static int readDigest (CT
, char *);
122 static int get_leftover_mp_content (CT
, int);
123 static int InitURL (CT
);
124 static int openURL (CT
, char **);
126 struct str2init str2cts
[] = {
127 { "application", CT_APPLICATION
, InitApplication
},
128 { "audio", CT_AUDIO
, InitGeneric
},
129 { "image", CT_IMAGE
, InitGeneric
},
130 { "message", CT_MESSAGE
, InitMessage
},
131 { "multipart", CT_MULTIPART
, InitMultiPart
},
132 { "text", CT_TEXT
, InitText
},
133 { "video", CT_VIDEO
, InitGeneric
},
134 { NULL
, CT_EXTENSION
, NULL
}, /* these two must be last! */
135 { NULL
, CT_UNKNOWN
, NULL
},
138 struct str2init str2ces
[] = {
139 { "base64", CE_BASE64
, InitBase64
},
140 { "quoted-printable", CE_QUOTED
, InitQuoted
},
141 { "8bit", CE_8BIT
, Init7Bit
},
142 { "7bit", CE_7BIT
, Init7Bit
},
143 { "binary", CE_BINARY
, Init7Bit
},
144 { NULL
, CE_EXTENSION
, NULL
}, /* these two must be last! */
145 { NULL
, CE_UNKNOWN
, NULL
},
149 * NOTE WELL: si_key MUST NOT have value of NOTOK
151 * si_key is 1 if access method is anonymous.
153 struct str2init str2methods
[] = {
154 { "afs", 1, InitFile
},
155 { "anon-ftp", 1, InitFTP
},
156 { "ftp", 0, InitFTP
},
157 { "local-file", 0, InitFile
},
158 { "mail-server", 0, InitMail
},
159 { "url", 0, InitURL
},
165 pidcheck (int status
)
167 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)
178 * Main entry point for parsing a MIME message or file.
179 * It returns the Content structure for the top level
180 * entity in the file.
184 parse_mime (char *file
)
192 * Check if file is actually standard input
194 if ((is_stdin
= !(strcmp (file
, "-")))) {
195 char *tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
197 advise("mhparse", "unable to create temporary file in %s",
201 file
= add (tfile
, NULL
);
203 while (fgets (buffer
, sizeof(buffer
), stdin
))
207 if (ferror (stdin
)) {
209 advise ("stdin", "error reading");
214 advise (file
, "error writing");
217 fseek (fp
, 0L, SEEK_SET
);
218 } else if ((fp
= fopen (file
, "r")) == NULL
) {
219 advise (file
, "unable to read");
223 if (!(ct
= get_content (fp
, file
, 1))) {
226 advise (NULL
, "unable to decode %s", file
);
231 ct
->c_unlink
= 1; /* temp file to remove */
235 if (ct
->c_end
== 0L) {
236 fseek (fp
, 0L, SEEK_END
);
237 ct
->c_end
= ftell (fp
);
240 if (ct
->c_ctinitfnx
&& (*ct
->c_ctinitfnx
) (ct
) == NOTOK
) {
252 * Main routine for reading/parsing the headers
253 * of a message content.
255 * toplevel = 1 # we are at the top level of the message
256 * toplevel = 0 # we are inside message type or multipart type
257 * # other than multipart/digest
258 * toplevel = -1 # we are inside multipart/digest
259 * NB: on failure we will fclose(in)!
263 get_content (FILE *in
, char *file
, int toplevel
)
266 char buf
[BUFSIZ
], name
[NAMESZ
];
270 m_getfld_state_t gstate
= 0;
272 /* allocate the content structure */
273 if (!(ct
= (CT
) calloc (1, sizeof(*ct
))))
274 adios (NULL
, "out of memory");
277 ct
->c_file
= add (file
, NULL
);
278 ct
->c_begin
= ftell (ct
->c_fp
) + 1;
281 * Parse the header fields for this
282 * content into a linked list.
284 m_getfld_track_filepos (&gstate
, in
);
285 for (compnum
= 1;;) {
286 int bufsz
= sizeof buf
;
287 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
292 /* get copies of the buffers */
293 np
= add (name
, NULL
);
294 vp
= add (buf
, NULL
);
296 /* if necessary, get rest of field */
297 while (state
== FLDPLUS
) {
299 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
300 vp
= add (buf
, vp
); /* add to previous value */
303 /* Now add the header data to the list */
304 add_header (ct
, np
, vp
);
306 /* continue, to see if this isn't the last header field */
307 ct
->c_begin
= ftell (in
) + 1;
311 ct
->c_begin
= ftell (in
) - strlen (buf
);
315 ct
->c_begin
= ftell (in
);
320 adios (NULL
, "message format error in component #%d", compnum
);
323 adios (NULL
, "getfld() returned %d", state
);
326 /* break out of the loop */
329 m_getfld_state_destroy (&gstate
);
332 * Read the content headers. We will parse the
333 * MIME related header fields into their various
334 * structures and set internal flags related to
335 * content type/subtype, etc.
338 hp
= ct
->c_first_hf
; /* start at first header field */
340 /* Get MIME-Version field */
341 if (!strcasecmp (hp
->name
, VRSN_FIELD
)) {
346 advise (NULL
, "message %s has multiple %s: fields",
347 ct
->c_file
, VRSN_FIELD
);
350 ct
->c_vrsn
= add (hp
->value
, NULL
);
352 /* Now, cleanup this field */
355 while (isspace ((unsigned char) *cp
))
357 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
359 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
360 if (!isspace ((unsigned char) *dp
))
364 fprintf (stderr
, "%s: %s\n", VRSN_FIELD
, cp
);
367 get_comment (ct
->c_file
, &ct
->c_ctinfo
, &cp
, 0) == NOTOK
)
370 for (dp
= cp
; istoken (*dp
); dp
++)
374 ucmp
= !strcasecmp (cp
, VRSN_VALUE
);
377 admonish (NULL
, "message %s has unknown value for %s: field (%s)",
378 ct
->c_file
, VRSN_FIELD
, cp
);
381 else if (!strcasecmp (hp
->name
, TYPE_FIELD
)) {
382 /* Get Content-Type field */
383 struct str2init
*s2i
;
384 CI ci
= &ct
->c_ctinfo
;
386 /* Check if we've already seen a Content-Type header */
388 advise (NULL
, "message %s has multiple %s: fields",
389 ct
->c_file
, TYPE_FIELD
);
393 /* Parse the Content-Type field */
394 if (get_ctinfo (hp
->value
, ct
, 0) == NOTOK
)
398 * Set the Init function and the internal
399 * flag for this content type.
401 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
402 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
404 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
406 ct
->c_type
= s2i
->si_val
;
407 ct
->c_ctinitfnx
= s2i
->si_init
;
409 else if (!strcasecmp (hp
->name
, ENCODING_FIELD
)) {
410 /* Get Content-Transfer-Encoding field */
412 struct str2init
*s2i
;
415 * Check if we've already seen the
416 * Content-Transfer-Encoding field
419 advise (NULL
, "message %s has multiple %s: fields",
420 ct
->c_file
, ENCODING_FIELD
);
424 /* get copy of this field */
425 ct
->c_celine
= cp
= add (hp
->value
, NULL
);
427 while (isspace ((unsigned char) *cp
))
429 for (dp
= cp
; istoken (*dp
); dp
++)
435 * Find the internal flag and Init function
436 * for this transfer encoding.
438 for (s2i
= str2ces
; s2i
->si_key
; s2i
++)
439 if (!strcasecmp (cp
, s2i
->si_key
))
441 if (!s2i
->si_key
&& !uprf (cp
, "X-"))
444 ct
->c_encoding
= s2i
->si_val
;
446 /* Call the Init function for this encoding */
447 if (s2i
->si_init
&& (*s2i
->si_init
) (ct
) == NOTOK
)
450 else if (!strcasecmp (hp
->name
, MD5_FIELD
)) {
451 /* Get Content-MD5 field */
457 if (ct
->c_digested
) {
458 advise (NULL
, "message %s has multiple %s: fields",
459 ct
->c_file
, MD5_FIELD
);
463 ep
= cp
= add (hp
->value
, NULL
); /* get a copy */
465 while (isspace ((unsigned char) *cp
))
467 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
469 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
470 if (!isspace ((unsigned char) *dp
))
474 fprintf (stderr
, "%s: %s\n", MD5_FIELD
, cp
);
477 get_comment (ct
->c_file
, &ct
->c_ctinfo
, &cp
, 0) == NOTOK
) {
482 for (dp
= cp
; *dp
&& !isspace ((unsigned char) *dp
); dp
++)
490 else if (!strcasecmp (hp
->name
, ID_FIELD
)) {
491 /* Get Content-ID field */
492 ct
->c_id
= add (hp
->value
, ct
->c_id
);
494 else if (!strcasecmp (hp
->name
, DESCR_FIELD
)) {
495 /* Get Content-Description field */
496 ct
->c_descr
= add (hp
->value
, ct
->c_descr
);
498 else if (!strcasecmp (hp
->name
, DISPO_FIELD
)) {
499 /* Get Content-Disposition field */
500 ct
->c_dispo
= add (hp
->value
, ct
->c_dispo
);
504 hp
= hp
->next
; /* next header field */
508 * Check if we saw a Content-Type field.
509 * If not, then assign a default value for
510 * it, and the Init function.
514 * If we are inside a multipart/digest message,
515 * so default type is message/rfc822
518 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
520 ct
->c_type
= CT_MESSAGE
;
521 ct
->c_ctinitfnx
= InitMessage
;
524 * Else default type is text/plain
526 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
)
528 ct
->c_type
= CT_TEXT
;
529 ct
->c_ctinitfnx
= InitText
;
533 /* Use default Transfer-Encoding, if necessary */
535 ct
->c_encoding
= CE_7BIT
;
548 * small routine to add header field to list
552 add_header (CT ct
, char *name
, char *value
)
556 /* allocate header field structure */
557 hp
= mh_xmalloc (sizeof(*hp
));
559 /* link data into header structure */
564 /* link header structure into the list */
565 if (ct
->c_first_hf
== NULL
) {
566 ct
->c_first_hf
= hp
; /* this is the first */
569 ct
->c_last_hf
->next
= hp
; /* add it to the end */
577 /* Make sure that buf contains at least one appearance of name,
578 followed by =. If not, insert both name and value, just after
579 first semicolon, if any. Note that name should not contain a
580 trailing =. And quotes will be added around the value. Typical
581 usage: make sure that a Content-Disposition header contains
582 filename="foo". If it doesn't and value does, use value from
585 incl_name_value (char *buf
, char *name
, char *value
) {
588 /* Assume that name is non-null. */
590 char *name_plus_equal
= concat (name
, "=", NULL
);
592 if (! strstr (buf
, name_plus_equal
)) {
594 char *cp
, *prefix
, *suffix
;
596 /* Trim trailing space, esp. newline. */
597 for (cp
= &buf
[strlen (buf
) - 1];
598 cp
>= buf
&& isspace ((unsigned char) *cp
);
603 insertion
= concat ("; ", name
, "=", "\"", value
, "\"", NULL
);
605 /* Insert at first semicolon, if any. If none, append to
607 prefix
= add (buf
, NULL
);
608 if ((cp
= strchr (prefix
, ';'))) {
609 suffix
= concat (cp
, NULL
);
611 newbuf
= concat (prefix
, insertion
, suffix
, "\n", NULL
);
615 newbuf
= concat (buf
, insertion
, "\n", NULL
);
623 free (name_plus_equal
);
629 /* Extract just name_suffix="foo", if any, from value. If there isn't
630 one, return the entire value. Note that, for example, a name_suffix
631 of name will match filename="foo", and return foo. */
633 extract_name_value (char *name_suffix
, char *value
) {
634 char *extracted_name_value
= value
;
635 char *name_suffix_plus_quote
= concat (name_suffix
, "=\"", NULL
);
636 char *name_suffix_equals
= strstr (value
, name_suffix_plus_quote
);
639 free (name_suffix_plus_quote
);
640 if (name_suffix_equals
) {
641 char *name_suffix_begin
;
644 for (cp
= name_suffix_equals
; *cp
!= '"'; ++cp
) /* empty */;
645 name_suffix_begin
= ++cp
;
646 /* Find second \". */
647 for (; *cp
!= '"'; ++cp
) /* empty */;
649 extracted_name_value
= mh_xmalloc (cp
- name_suffix_begin
+ 1);
650 memcpy (extracted_name_value
,
652 cp
- name_suffix_begin
);
653 extracted_name_value
[cp
- name_suffix_begin
] = '\0';
656 return extracted_name_value
;
660 * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition
661 * directives. Fills in the information of the CTinfo structure.
664 get_ctinfo (char *cp
, CT ct
, int magic
)
673 i
= strlen (invo_name
) + 2;
675 /* store copy of Content-Type line */
676 cp
= ct
->c_ctline
= add (cp
, NULL
);
678 while (isspace ((unsigned char) *cp
)) /* trim leading spaces */
681 /* change newlines to spaces */
682 for (dp
= strchr(cp
, '\n'); dp
; dp
= strchr(dp
, '\n'))
685 /* trim trailing spaces */
686 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
687 if (!isspace ((unsigned char) *dp
))
692 fprintf (stderr
, "%s: %s\n", TYPE_FIELD
, cp
);
694 if (*cp
== '(' && get_comment (ct
->c_file
, &ct
->c_ctinfo
, &cp
, 1) == NOTOK
)
697 for (dp
= cp
; istoken (*dp
); dp
++)
700 ci
->ci_type
= add (cp
, NULL
); /* store content type */
704 advise (NULL
, "invalid %s: field in message %s (empty type)",
705 TYPE_FIELD
, ct
->c_file
);
709 /* down case the content type string */
710 for (dp
= ci
->ci_type
; *dp
; dp
++)
711 if (isalpha((unsigned char) *dp
) && isupper ((unsigned char) *dp
))
712 *dp
= tolower ((unsigned char) *dp
);
714 while (isspace ((unsigned char) *cp
))
717 if (*cp
== '(' && get_comment (ct
->c_file
, &ct
->c_ctinfo
, &cp
, 1) == NOTOK
)
722 ci
->ci_subtype
= add ("", NULL
);
727 while (isspace ((unsigned char) *cp
))
730 if (*cp
== '(' && get_comment (ct
->c_file
, &ct
->c_ctinfo
, &cp
, 1) == NOTOK
)
733 for (dp
= cp
; istoken (*dp
); dp
++)
736 ci
->ci_subtype
= add (cp
, NULL
); /* store the content subtype */
739 if (!*ci
->ci_subtype
) {
741 "invalid %s: field in message %s (empty subtype for \"%s\")",
742 TYPE_FIELD
, ct
->c_file
, ci
->ci_type
);
746 /* down case the content subtype string */
747 for (dp
= ci
->ci_subtype
; *dp
; dp
++)
748 if (isalpha((unsigned char) *dp
) && isupper ((unsigned char) *dp
))
749 *dp
= tolower ((unsigned char) *dp
);
752 while (isspace ((unsigned char) *cp
))
755 if (*cp
== '(' && get_comment (ct
->c_file
, &ct
->c_ctinfo
, &cp
, 1) == NOTOK
)
758 if (parse_header_attrs (ct
->c_file
, i
, &cp
, ci
, &status
) == NOTOK
) {
763 * Get any <Content-Id> given in buffer
765 if (magic
&& *cp
== '<') {
770 if (!(dp
= strchr(ct
->c_id
= ++cp
, '>'))) {
771 advise (NULL
, "invalid ID in message %s", ct
->c_file
);
777 ct
->c_id
= concat ("<", ct
->c_id
, ">\n", NULL
);
783 while (isspace ((unsigned char) *cp
))
788 * Get any [Content-Description] given in buffer.
790 if (magic
&& *cp
== '[') {
792 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
796 advise (NULL
, "invalid description in message %s", ct
->c_file
);
804 ct
->c_descr
= concat (ct
->c_descr
, "\n", NULL
);
810 while (isspace ((unsigned char) *cp
))
815 * Get any {Content-Disposition} given in buffer.
817 if (magic
&& *cp
== '{') {
819 for (dp
= cp
+ strlen (cp
) - 1; dp
>= cp
; dp
--)
823 advise (NULL
, "invalid disposition in message %s", ct
->c_file
);
831 ct
->c_dispo
= concat (ct
->c_dispo
, "\n", NULL
);
837 while (isspace ((unsigned char) *cp
))
842 * Check if anything is left over
846 ci
->ci_magic
= add (cp
, NULL
);
848 /* If there is a Content-Disposition header and it doesn't
849 have a *filename=, extract it from the magic contents.
850 The r1bindex call skips any leading directory
854 incl_name_value (ct
->c_dispo
,
856 r1bindex (extract_name_value ("name",
863 "extraneous information in message %s's %s: field\n%*.*s(%s)",
864 ct
->c_file
, TYPE_FIELD
, i
, i
, "", cp
);
872 get_comment (const char *filename
, CI ci
, char **ap
, int istype
)
876 char c
, buffer
[BUFSIZ
], *dp
;
886 advise (NULL
, "invalid comment in message %s's %s: field",
887 filename
, istype
? TYPE_FIELD
: VRSN_FIELD
);
892 if ((c
= *cp
++) == '\0')
915 if ((dp
= ci
->ci_comment
)) {
916 ci
->ci_comment
= concat (dp
, " ", buffer
, NULL
);
919 ci
->ci_comment
= add (buffer
, NULL
);
923 while (isspace ((unsigned char) *cp
))
934 * Handles content types audio, image, and video.
935 * There's not much to do right here.
943 return OK
; /* not much to do here */
956 char **ap
, **ep
, *cp
;
959 CI ci
= &ct
->c_ctinfo
;
961 /* check for missing subtype */
962 if (!*ci
->ci_subtype
)
963 ci
->ci_subtype
= add ("plain", ci
->ci_subtype
);
966 for (kv
= SubText
; kv
->kv_key
; kv
++)
967 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
969 ct
->c_subtype
= kv
->kv_value
;
971 /* allocate text character set structure */
972 if ((t
= (struct text
*) calloc (1, sizeof(*t
))) == NULL
)
973 adios (NULL
, "out of memory");
974 ct
->c_ctparams
= (void *) t
;
976 /* scan for charset parameter */
977 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++)
978 if (!strcasecmp (*ap
, "charset"))
981 /* check if content specified a character set */
984 t
->tx_charset
= CHARSET_SPECIFIED
;
986 t
->tx_charset
= CHARSET_UNSPECIFIED
;
990 * If we can not handle character set natively,
991 * then check profile for string to modify the
992 * terminal or display method.
994 * termproc is for mhshow, though mhlist -debug prints it, too.
996 if (chset
!= NULL
&& !check_charset (chset
, strlen (chset
))) {
997 snprintf (buffer
, sizeof(buffer
), "%s-charset-%s", invo_name
, chset
);
998 if ((cp
= context_find (buffer
)))
999 ct
->c_termproc
= getcpy (cp
);
1011 InitMultiPart (CT ct
)
1015 char *cp
, *dp
, **ap
, **ep
;
1016 char *bp
, buffer
[BUFSIZ
];
1017 struct multipart
*m
;
1019 struct part
*part
, **next
;
1020 CI ci
= &ct
->c_ctinfo
;
1025 * The encoding for multipart messages must be either
1026 * 7bit, 8bit, or binary (per RFC2045).
1028 if (! skip_mp_cte_check
&& ct
->c_encoding
!= CE_7BIT
&&
1029 ct
->c_encoding
!= CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
) {
1030 /* Copy the Content-Transfer-Encoding header field body so we can
1031 remove any trailing whitespace and leading blanks from it. */
1032 char *cte
= add (ct
->c_celine
? ct
->c_celine
: "(null)", NULL
);
1034 bp
= cte
+ strlen (cte
) - 1;
1035 while (bp
>= cte
&& isspace ((unsigned char) *bp
)) *bp
-- = '\0';
1036 for (bp
= cte
; *bp
&& isblank ((unsigned char) *bp
); ++bp
) continue;
1039 "\"%s/%s\" type in message %s must be encoded in\n"
1040 "7bit, 8bit, or binary, per RFC 2045 (6.4). One workaround "
1041 "is to\nmanually edit the file and change the \"%s\"\n"
1042 "Content-Transfer-Encoding to one of those. For now",
1043 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, bp
);
1050 for (kv
= SubMultiPart
; kv
->kv_key
; kv
++)
1051 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1053 ct
->c_subtype
= kv
->kv_value
;
1056 * Check for "boundary" parameter, which is
1057 * required for multipart messages.
1060 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1061 if (!strcasecmp (*ap
, "boundary")) {
1067 /* complain if boundary parameter is missing */
1070 "a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field",
1071 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1075 /* allocate primary structure for multipart info */
1076 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
1077 adios (NULL
, "out of memory");
1078 ct
->c_ctparams
= (void *) m
;
1080 /* check if boundary parameter contains only whitespace characters */
1081 for (cp
= bp
; isspace ((unsigned char) *cp
); cp
++)
1084 advise (NULL
, "invalid \"boundary\" parameter for \"%s/%s\" type in message %s's %s: field",
1085 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1089 /* remove trailing whitespace from boundary parameter */
1090 for (cp
= bp
, dp
= cp
+ strlen (cp
) - 1; dp
> cp
; dp
--)
1091 if (!isspace ((unsigned char) *dp
))
1095 /* record boundary separators */
1096 m
->mp_start
= concat (bp
, "\n", NULL
);
1097 m
->mp_stop
= concat (bp
, "--\n", NULL
);
1099 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1100 advise (ct
->c_file
, "unable to open for reading");
1104 fseek (fp
= ct
->c_fp
, pos
= ct
->c_begin
, SEEK_SET
);
1106 next
= &m
->mp_parts
;
1110 while (fgets (buffer
, sizeof(buffer
) - 1, fp
)) {
1114 pos
+= strlen (buffer
);
1115 if (buffer
[0] != '-' || buffer
[1] != '-')
1118 if (strcmp (buffer
+ 2, m
->mp_start
))
1121 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
1122 adios (NULL
, "out of memory");
1124 next
= &part
->mp_next
;
1126 if (!(p
= get_content (fp
, ct
->c_file
,
1127 ct
->c_subtype
== MULTI_DIGEST
? -1 : 0))) {
1134 fseek (fp
, pos
, SEEK_SET
);
1137 if (strcmp (buffer
+ 2, m
->mp_start
) == 0) {
1141 p
->c_end
= ftell(fp
) - (strlen(buffer
) + 1);
1142 if (p
->c_end
< p
->c_begin
)
1143 p
->c_begin
= p
->c_end
;
1148 if (strcmp (buffer
+ 2, m
->mp_stop
) == 0)
1154 if (! suppress_bogus_mp_content_warning
) {
1155 advise (NULL
, "bogus multipart content in message %s", ct
->c_file
);
1157 bogus_mp_content
= 1;
1159 if (!inout
&& part
) {
1161 p
->c_end
= ct
->c_end
;
1163 if (p
->c_begin
>= p
->c_end
) {
1164 for (next
= &m
->mp_parts
; *next
!= part
;
1165 next
= &((*next
)->mp_next
))
1169 free ((char *) part
);
1174 /* reverse the order of the parts for multipart/alternative */
1175 if (ct
->c_subtype
== MULTI_ALTERNATE
)
1179 * label all subparts with part number, and
1180 * then initialize the content of the subpart.
1185 char partnam
[BUFSIZ
];
1188 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1189 pp
= partnam
+ strlen (partnam
);
1194 for (part
= m
->mp_parts
, partnum
= 1; part
;
1195 part
= part
->mp_next
, partnum
++) {
1198 sprintf (pp
, "%d", partnum
);
1199 p
->c_partno
= add (partnam
, NULL
);
1201 /* initialize the content of the subparts */
1202 if (p
->c_ctinitfnx
&& (*p
->c_ctinitfnx
) (p
) == NOTOK
) {
1210 get_leftover_mp_content (ct
, 1);
1211 get_leftover_mp_content (ct
, 0);
1220 * reverse the order of the parts of a multipart/alternative
1224 reverse_parts (CT ct
)
1226 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1230 /* Reverse the order of its parts by walking the mp_parts list
1231 and pushing each node to the front. */
1232 for (part
= m
->mp_parts
, m
->mp_parts
= NULL
; part
; part
= next
) {
1233 next
= part
->mp_next
;
1234 part
->mp_next
= m
->mp_parts
;
1248 CI ci
= &ct
->c_ctinfo
;
1250 if ((ct
->c_encoding
!= CE_7BIT
) && (ct
->c_encoding
!= CE_8BIT
)) {
1252 "\"%s/%s\" type in message %s should be encoded in 7bit or 8bit",
1253 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
);
1257 /* check for missing subtype */
1258 if (!*ci
->ci_subtype
)
1259 ci
->ci_subtype
= add ("rfc822", ci
->ci_subtype
);
1262 for (kv
= SubMessage
; kv
->kv_key
; kv
++)
1263 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1265 ct
->c_subtype
= kv
->kv_value
;
1267 switch (ct
->c_subtype
) {
1268 case MESSAGE_RFC822
:
1271 case MESSAGE_PARTIAL
:
1276 if ((p
= (struct partial
*) calloc (1, sizeof(*p
))) == NULL
)
1277 adios (NULL
, "out of memory");
1278 ct
->c_ctparams
= (void *) p
;
1280 /* scan for parameters "id", "number", and "total" */
1281 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1282 if (!strcasecmp (*ap
, "id")) {
1283 p
->pm_partid
= add (*ep
, NULL
);
1286 if (!strcasecmp (*ap
, "number")) {
1287 if (sscanf (*ep
, "%d", &p
->pm_partno
) != 1
1288 || p
->pm_partno
< 1) {
1291 "invalid %s parameter for \"%s/%s\" type in message %s's %s field",
1292 *ap
, ci
->ci_type
, ci
->ci_subtype
,
1293 ct
->c_file
, TYPE_FIELD
);
1298 if (!strcasecmp (*ap
, "total")) {
1299 if (sscanf (*ep
, "%d", &p
->pm_maxno
) != 1
1308 || (p
->pm_maxno
&& p
->pm_partno
> p
->pm_maxno
)) {
1310 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1311 ci
->ci_type
, ci
->ci_subtype
,
1312 ct
->c_file
, TYPE_FIELD
);
1318 case MESSAGE_EXTERNAL
:
1325 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
1326 adios (NULL
, "out of memory");
1327 ct
->c_ctparams
= (void *) e
;
1330 && (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1331 advise (ct
->c_file
, "unable to open for reading");
1335 fseek (fp
= ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1337 if (!(p
= get_content (fp
, ct
->c_file
, 0))) {
1345 p
->c_ceopenfnx
= NULL
;
1346 if ((exresult
= params_external (ct
, 0)) != NOTOK
1347 && p
->c_ceopenfnx
== openMail
) {
1351 if ((size
= ct
->c_end
- p
->c_begin
) <= 0) {
1353 content_error (NULL
, ct
,
1354 "empty body for access-type=mail-server");
1358 e
->eb_body
= bp
= mh_xmalloc ((unsigned) size
);
1359 fseek (p
->c_fp
, p
->c_begin
, SEEK_SET
);
1361 switch (cc
= fread (bp
, sizeof(*bp
), size
, p
->c_fp
)) {
1363 adios ("failed", "fread");
1366 adios (NULL
, "unexpected EOF from fread");
1369 bp
+= cc
, size
-= cc
;
1376 p
->c_end
= p
->c_begin
;
1381 if (exresult
== NOTOK
)
1383 if (e
->eb_flags
== NOTOK
)
1386 switch (p
->c_type
) {
1391 if (p
->c_subtype
!= MESSAGE_RFC822
)
1395 e
->eb_partno
= ct
->c_partno
;
1397 (*p
->c_ctinitfnx
) (p
);
1412 params_external (CT ct
, int composing
)
1415 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1416 CI ci
= &ct
->c_ctinfo
;
1418 ct
->c_ceopenfnx
= NULL
;
1419 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1420 if (!strcasecmp (*ap
, "access-type")) {
1421 struct str2init
*s2i
;
1422 CT p
= e
->eb_content
;
1424 for (s2i
= str2methods
; s2i
->si_key
; s2i
++)
1425 if (!strcasecmp (*ep
, s2i
->si_key
))
1429 e
->eb_flags
= NOTOK
;
1430 p
->c_encoding
= CE_EXTERNAL
;
1433 e
->eb_access
= s2i
->si_key
;
1434 e
->eb_flags
= s2i
->si_val
;
1435 p
->c_encoding
= CE_EXTERNAL
;
1437 /* Call the Init function for this external type */
1438 if ((*s2i
->si_init
)(p
) == NOTOK
)
1442 if (!strcasecmp (*ap
, "name")) {
1446 if (!strcasecmp (*ap
, "permission")) {
1447 e
->eb_permission
= *ep
;
1450 if (!strcasecmp (*ap
, "site")) {
1454 if (!strcasecmp (*ap
, "directory")) {
1458 if (!strcasecmp (*ap
, "mode")) {
1462 if (!strcasecmp (*ap
, "size")) {
1463 sscanf (*ep
, "%lu", &e
->eb_size
);
1466 if (!strcasecmp (*ap
, "server")) {
1470 if (!strcasecmp (*ap
, "subject")) {
1471 e
->eb_subject
= *ep
;
1474 if (!strcasecmp (*ap
, "url")) {
1476 * According to RFC 2017, we have to remove all whitespace from
1481 e
->eb_url
= u
= mh_xmalloc(strlen(*ep
) + 1);
1483 for (; *p
!= '\0'; p
++) {
1484 if (! isspace((unsigned char) *p
))
1491 if (composing
&& !strcasecmp (*ap
, "body")) {
1492 e
->eb_body
= getcpy (*ep
);
1497 if (!e
->eb_access
) {
1499 "invalid parameters for \"%s/%s\" type in message %s's %s field",
1500 ci
->ci_type
, ci
->ci_subtype
, ct
->c_file
, TYPE_FIELD
);
1513 InitApplication (CT ct
)
1516 CI ci
= &ct
->c_ctinfo
;
1519 for (kv
= SubApplication
; kv
->kv_key
; kv
++)
1520 if (!strcasecmp (ci
->ci_subtype
, kv
->kv_key
))
1522 ct
->c_subtype
= kv
->kv_value
;
1529 * TRANSFER ENCODINGS
1533 init_encoding (CT ct
, OpenCEFunc openfnx
)
1535 ct
->c_ceopenfnx
= openfnx
;
1536 ct
->c_ceclosefnx
= close_encoding
;
1537 ct
->c_cesizefnx
= size_encoding
;
1544 close_encoding (CT ct
)
1546 CE ce
= &ct
->c_cefile
;
1555 static unsigned long
1556 size_encoding (CT ct
)
1561 CE ce
= &ct
->c_cefile
;
1564 if (ce
->ce_fp
&& fstat (fileno (ce
->ce_fp
), &st
) != NOTOK
)
1565 return (long) st
.st_size
;
1568 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1569 return (long) st
.st_size
;
1574 if (ct
->c_encoding
== CE_EXTERNAL
)
1575 return (ct
->c_end
- ct
->c_begin
);
1578 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
1579 return (ct
->c_end
- ct
->c_begin
);
1581 if (fstat (fd
, &st
) != NOTOK
)
1582 size
= (long) st
.st_size
;
1586 (*ct
->c_ceclosefnx
) (ct
);
1595 static unsigned char b642nib
[0x80] = {
1596 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1597 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1598 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1599 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1600 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1601 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
1602 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
1603 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1604 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1605 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
1606 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
1607 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
1608 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
1609 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
1610 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
1611 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
1618 return init_encoding (ct
, openBase64
);
1623 openBase64 (CT ct
, char **file
)
1625 int bitno
, cc
, digested
;
1626 int fd
, len
, skip
, own_ct_fp
= 0;
1628 unsigned char value
, b
;
1629 char *cp
, *ep
, buffer
[BUFSIZ
];
1630 /* sbeck -- handle suffixes */
1632 CE ce
= &ct
->c_cefile
;
1636 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1641 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1642 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1648 if (*file
== NULL
) {
1650 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1651 adios(NULL
, "unable to create temporary file in %s",
1654 ce
->ce_file
= add (tempfile
, NULL
);
1657 ce
->ce_file
= add (*file
, NULL
);
1661 /* sbeck@cise.ufl.edu -- handle suffixes */
1663 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
1664 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1665 cp
= context_find (buffer
);
1666 if (cp
== NULL
|| *cp
== '\0') {
1667 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
1669 cp
= context_find (buffer
);
1671 if (cp
!= NULL
&& *cp
!= '\0') {
1672 if (ce
->ce_unlink
) {
1673 /* Temporary file already exists, so we rename to
1674 version with extension. */
1675 char *file_org
= strdup(ce
->ce_file
);
1676 ce
->ce_file
= add (cp
, ce
->ce_file
);
1677 if (rename(file_org
, ce
->ce_file
)) {
1678 adios (ce
->ce_file
, "unable to rename %s to ", file_org
);
1683 ce
->ce_file
= add (cp
, ce
->ce_file
);
1687 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1688 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1692 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1693 adios (NULL
, "internal error(1)");
1696 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1697 content_error (ct
->c_file
, ct
, "unable to open for reading");
1703 if ((digested
= ct
->c_digested
))
1704 MD5Init (&mdContext
);
1710 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
1712 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
1714 content_error (ct
->c_file
, ct
, "error reading from");
1718 content_error (NULL
, ct
, "premature eof");
1726 for (ep
= (cp
= buffer
) + cc
; cp
< ep
; cp
++) {
1729 if (isspace ((unsigned char) *cp
))
1731 if (skip
|| (((unsigned char) *cp
) & 0x80)
1732 || (value
= b642nib
[((unsigned char) *cp
) & 0x7f]) > 0x3f) {
1734 fprintf (stderr
, "*cp=0x%x pos=%ld skip=%d\n",
1735 (unsigned char) *cp
,
1736 (long) (lseek (fd
, (off_t
) 0, SEEK_CUR
) - (ep
- cp
)),
1739 content_error (NULL
, ct
,
1740 "invalid BASE64 encoding -- continuing");
1744 bits
|= value
<< bitno
;
1746 if ((bitno
-= 6) < 0) {
1747 b
= (bits
>> 16) & 0xff;
1748 putc ((char) b
, ce
->ce_fp
);
1750 MD5Update (&mdContext
, &b
, 1);
1752 b
= (bits
>> 8) & 0xff;
1753 putc ((char) b
, ce
->ce_fp
);
1755 MD5Update (&mdContext
, &b
, 1);
1758 putc ((char) b
, ce
->ce_fp
);
1760 MD5Update (&mdContext
, &b
, 1);
1764 if (ferror (ce
->ce_fp
)) {
1765 content_error (ce
->ce_file
, ct
,
1766 "error writing to");
1769 bitno
= 18, bits
= 0L, skip
= 0;
1775 goto self_delimiting
;
1784 fprintf (stderr
, "premature ending (bitno %d)\n", bitno
);
1786 content_error (NULL
, ct
, "invalid BASE64 encoding");
1791 fseek (ct
->c_fp
, 0L, SEEK_SET
);
1793 if (fflush (ce
->ce_fp
)) {
1794 content_error (ce
->ce_file
, ct
, "error writing to");
1799 unsigned char digest
[16];
1801 MD5Final (digest
, &mdContext
);
1802 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
1803 sizeof(digest
) / sizeof(digest
[0])))
1804 content_error (NULL
, ct
,
1805 "content integrity suspect (digest mismatch) -- continuing");
1808 fprintf (stderr
, "content integrity confirmed\n");
1811 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1814 *file
= ce
->ce_file
;
1819 return fileno (ce
->ce_fp
);
1826 free_encoding (ct
, 0);
1835 static char hex2nib
[0x80] = {
1836 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1837 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1838 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1839 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1840 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1841 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1842 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1843 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1844 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
1845 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1846 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1847 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1848 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
1849 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1850 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1851 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1858 return init_encoding (ct
, openQuoted
);
1863 openQuoted (CT ct
, char **file
)
1865 int cc
, digested
, len
, quoted
, own_ct_fp
= 0;
1867 char buffer
[BUFSIZ
];
1869 CE ce
= &ct
->c_cefile
;
1870 /* sbeck -- handle suffixes */
1875 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
1880 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1881 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
1887 if (*file
== NULL
) {
1889 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1890 adios(NULL
, "unable to create temporary file in %s",
1893 ce
->ce_file
= add (tempfile
, NULL
);
1896 ce
->ce_file
= add (*file
, NULL
);
1900 /* sbeck@cise.ufl.edu -- handle suffixes */
1902 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
1903 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1904 cp
= context_find (buffer
);
1905 if (cp
== NULL
|| *cp
== '\0') {
1906 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
1908 cp
= context_find (buffer
);
1910 if (cp
!= NULL
&& *cp
!= '\0') {
1911 if (ce
->ce_unlink
) {
1912 /* Temporary file already exists, so we rename to
1913 version with extension. */
1914 char *file_org
= strdup(ce
->ce_file
);
1915 ce
->ce_file
= add (cp
, ce
->ce_file
);
1916 if (rename(file_org
, ce
->ce_file
)) {
1917 adios (ce
->ce_file
, "unable to rename %s to ", file_org
);
1922 ce
->ce_file
= add (cp
, ce
->ce_file
);
1926 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
1927 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
1931 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
1932 adios (NULL
, "internal error(2)");
1935 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
1936 content_error (ct
->c_file
, ct
, "unable to open for reading");
1942 if ((digested
= ct
->c_digested
))
1943 MD5Init (&mdContext
);
1950 fseek (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
1952 if (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
) == NULL
) {
1953 content_error (NULL
, ct
, "premature eof");
1957 if ((cc
= strlen (buffer
)) > len
)
1961 for (ep
= (cp
= buffer
) + cc
- 1; cp
<= ep
; ep
--)
1962 if (!isspace ((unsigned char) *ep
))
1966 for (; cp
< ep
; cp
++) {
1968 /* in an escape sequence */
1970 /* at byte 1 of an escape sequence */
1971 mask
= hex2nib
[((unsigned char) *cp
) & 0x7f];
1972 /* next is byte 2 */
1975 /* at byte 2 of an escape sequence */
1977 mask
|= hex2nib
[((unsigned char) *cp
) & 0x7f];
1978 putc (mask
, ce
->ce_fp
);
1980 MD5Update (&mdContext
, &mask
, 1);
1981 if (ferror (ce
->ce_fp
)) {
1982 content_error (ce
->ce_file
, ct
, "error writing to");
1985 /* finished escape sequence; next may be literal or a new
1986 * escape sequence */
1989 /* on to next byte */
1993 /* not in an escape sequence */
1995 /* starting an escape sequence, or invalid '='? */
1996 if (cp
+ 1 < ep
&& cp
[1] == '\n') {
1997 /* "=\n" soft line break, eat the \n */
2001 if (cp
+ 1 >= ep
|| cp
+ 2 >= ep
) {
2002 /* We don't have 2 bytes left, so this is an invalid
2003 * escape sequence; just show the raw bytes (below). */
2004 } else if (isxdigit ((unsigned char) cp
[1]) &&
2005 isxdigit ((unsigned char) cp
[2])) {
2006 /* Next 2 bytes are hex digits, making this a valid escape
2007 * sequence; let's decode it (above). */
2011 /* One or both of the next 2 is out of range, making this
2012 * an invalid escape sequence; just show the raw bytes
2017 /* Just show the raw byte. */
2018 putc (*cp
, ce
->ce_fp
);
2021 MD5Update (&mdContext
, (unsigned char *) "\r\n",2);
2023 MD5Update (&mdContext
, (unsigned char *) cp
, 1);
2026 if (ferror (ce
->ce_fp
)) {
2027 content_error (ce
->ce_file
, ct
, "error writing to");
2033 content_error (NULL
, ct
,
2034 "invalid QUOTED-PRINTABLE encoding -- end-of-content while still quoting");
2038 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2040 if (fflush (ce
->ce_fp
)) {
2041 content_error (ce
->ce_file
, ct
, "error writing to");
2046 unsigned char digest
[16];
2048 MD5Final (digest
, &mdContext
);
2049 if (memcmp((char *) digest
, (char *) ct
->c_digest
,
2050 sizeof(digest
) / sizeof(digest
[0])))
2051 content_error (NULL
, ct
,
2052 "content integrity suspect (digest mismatch) -- continuing");
2055 fprintf (stderr
, "content integrity confirmed\n");
2058 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2061 *file
= ce
->ce_file
;
2066 return fileno (ce
->ce_fp
);
2069 free_encoding (ct
, 0);
2085 if (init_encoding (ct
, open7Bit
) == NOTOK
)
2088 ct
->c_cesizefnx
= NULL
; /* no need to decode for real size */
2094 open7Bit (CT ct
, char **file
)
2096 int cc
, fd
, len
, own_ct_fp
= 0;
2097 char buffer
[BUFSIZ
];
2098 /* sbeck -- handle suffixes */
2101 CE ce
= &ct
->c_cefile
;
2104 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2109 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2110 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2116 if (*file
== NULL
) {
2118 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2119 adios(NULL
, "unable to create temporary file in %s",
2122 ce
->ce_file
= add (tempfile
, NULL
);
2125 ce
->ce_file
= add (*file
, NULL
);
2129 /* sbeck@cise.ufl.edu -- handle suffixes */
2131 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s/%s",
2132 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
2133 cp
= context_find (buffer
);
2134 if (cp
== NULL
|| *cp
== '\0') {
2135 snprintf (buffer
, sizeof(buffer
), "%s-suffix-%s", invo_name
,
2137 cp
= context_find (buffer
);
2139 if (cp
!= NULL
&& *cp
!= '\0') {
2140 if (ce
->ce_unlink
) {
2141 /* Temporary file already exists, so we rename to
2142 version with extension. */
2143 char *file_org
= strdup(ce
->ce_file
);
2144 ce
->ce_file
= add (cp
, ce
->ce_file
);
2145 if (rename(file_org
, ce
->ce_file
)) {
2146 adios (ce
->ce_file
, "unable to rename %s to ", file_org
);
2151 ce
->ce_file
= add (cp
, ce
->ce_file
);
2155 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2156 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2160 if (ct
->c_type
== CT_MULTIPART
) {
2162 CI ci
= &ct
->c_ctinfo
;
2165 fprintf (ce
->ce_fp
, "%s: %s/%s", TYPE_FIELD
, ci
->ci_type
, ci
->ci_subtype
);
2166 len
+= strlen (TYPE_FIELD
) + 2 + strlen (ci
->ci_type
)
2167 + 1 + strlen (ci
->ci_subtype
);
2168 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
2169 putc (';', ce
->ce_fp
);
2172 snprintf (buffer
, sizeof(buffer
), "%s=\"%s\"", *ap
, *ep
);
2174 if (len
+ 1 + (cc
= strlen (buffer
)) >= CPERLIN
) {
2175 fputs ("\n\t", ce
->ce_fp
);
2178 putc (' ', ce
->ce_fp
);
2181 fprintf (ce
->ce_fp
, "%s", buffer
);
2185 if (ci
->ci_comment
) {
2186 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
2187 fputs ("\n\t", ce
->ce_fp
);
2191 putc (' ', ce
->ce_fp
);
2194 fprintf (ce
->ce_fp
, "(%s)", ci
->ci_comment
);
2197 fprintf (ce
->ce_fp
, "\n");
2199 fprintf (ce
->ce_fp
, "%s:%s", ID_FIELD
, ct
->c_id
);
2201 fprintf (ce
->ce_fp
, "%s:%s", DESCR_FIELD
, ct
->c_descr
);
2203 fprintf (ce
->ce_fp
, "%s:%s", DISPO_FIELD
, ct
->c_dispo
);
2204 fprintf (ce
->ce_fp
, "\n");
2207 if ((len
= ct
->c_end
- ct
->c_begin
) < 0)
2208 adios (NULL
, "internal error(3)");
2211 if ((ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
2212 content_error (ct
->c_file
, ct
, "unable to open for reading");
2218 lseek (fd
= fileno (ct
->c_fp
), (off_t
) ct
->c_begin
, SEEK_SET
);
2220 switch (cc
= read (fd
, buffer
, sizeof(buffer
) - 1)) {
2222 content_error (ct
->c_file
, ct
, "error reading from");
2226 content_error (NULL
, ct
, "premature eof");
2234 fwrite (buffer
, sizeof(*buffer
), cc
, ce
->ce_fp
);
2235 if (ferror (ce
->ce_fp
)) {
2236 content_error (ce
->ce_file
, ct
, "error writing to");
2241 fseek (ct
->c_fp
, 0L, SEEK_SET
);
2243 if (fflush (ce
->ce_fp
)) {
2244 content_error (ce
->ce_file
, ct
, "error writing to");
2248 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2251 *file
= ce
->ce_file
;
2256 return fileno (ce
->ce_fp
);
2259 free_encoding (ct
, 0);
2273 openExternal (CT ct
, CT cb
, CE ce
, char **file
, int *fd
)
2275 char cachefile
[BUFSIZ
];
2278 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2283 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2284 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2290 if (find_cache (ct
, rcachesw
, (int *) 0, cb
->c_id
,
2291 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2292 if ((ce
->ce_fp
= fopen (cachefile
, "r"))) {
2293 ce
->ce_file
= getcpy (cachefile
);
2297 admonish (cachefile
, "unable to fopen for reading");
2304 *file
= ce
->ce_file
;
2305 *fd
= fileno (ce
->ce_fp
);
2316 return init_encoding (ct
, openFile
);
2321 openFile (CT ct
, char **file
)
2324 char cachefile
[BUFSIZ
];
2325 struct exbody
*e
= ct
->c_ctexbody
;
2326 CE ce
= &ct
->c_cefile
;
2328 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2340 content_error (NULL
, ct
, "missing name parameter");
2344 ce
->ce_file
= getcpy (e
->eb_name
);
2347 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2348 content_error (ce
->ce_file
, ct
, "unable to fopen for reading");
2352 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2353 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2354 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2358 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2359 if ((fp
= fopen (cachefile
, "w"))) {
2361 char buffer
[BUFSIZ
];
2362 FILE *gp
= ce
->ce_fp
;
2364 fseek (gp
, 0L, SEEK_SET
);
2366 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2368 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
2372 admonish (ce
->ce_file
, "error reading");
2377 admonish (cachefile
, "error writing");
2385 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2386 *file
= ce
->ce_file
;
2387 return fileno (ce
->ce_fp
);
2397 return init_encoding (ct
, openFTP
);
2402 openFTP (CT ct
, char **file
)
2404 int cachetype
, caching
, fd
;
2406 char *bp
, *ftp
, *user
, *pass
;
2407 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2409 CE ce
= &ct
->c_cefile
;
2410 static char *username
= NULL
;
2411 static char *password
= NULL
;
2415 if ((ftp
= context_find (nmhaccessftp
)) && !*ftp
)
2421 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2432 if (!e
->eb_name
|| !e
->eb_site
) {
2433 content_error (NULL
, ct
, "missing %s parameter",
2434 e
->eb_name
? "site": "name");
2441 pidcheck (pidwait (xpid
, NOTOK
));
2445 /* Get the buffer ready to go */
2447 buflen
= sizeof(buffer
);
2450 * Construct the query message for user
2452 snprintf (bp
, buflen
, "Retrieve %s", e
->eb_name
);
2458 snprintf (bp
, buflen
, " (content %s)", e
->eb_partno
);
2464 snprintf (bp
, buflen
, "\n using %sFTP from site %s",
2465 e
->eb_flags
? "anonymous " : "", e
->eb_site
);
2470 if (e
->eb_size
> 0) {
2471 snprintf (bp
, buflen
, " (%lu octets)", e
->eb_size
);
2476 snprintf (bp
, buflen
, "? ");
2479 * Now, check the answer
2481 if (!getanswer (buffer
))
2486 snprintf (buffer
, sizeof(buffer
), "%s@%s", getusername (),
2490 ruserpass (e
->eb_site
, &username
, &password
);
2495 ce
->ce_unlink
= (*file
== NULL
);
2497 cachefile
[0] = '\0';
2498 if ((!e
->eb_permission
|| strcasecmp (e
->eb_permission
, "read-write"))
2499 && find_cache (NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2500 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2501 if (*file
== NULL
) {
2508 ce
->ce_file
= add (*file
, NULL
);
2510 ce
->ce_file
= add (cachefile
, NULL
);
2513 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2514 adios(NULL
, "unable to create temporary file in %s",
2517 ce
->ce_file
= add (tempfile
, NULL
);
2520 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2521 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2526 int child_id
, i
, vecp
;
2530 vec
[vecp
++] = r1bindex (ftp
, '/');
2531 vec
[vecp
++] = e
->eb_site
;
2534 vec
[vecp
++] = e
->eb_dir
;
2535 vec
[vecp
++] = e
->eb_name
;
2536 vec
[vecp
++] = ce
->ce_file
,
2537 vec
[vecp
++] = e
->eb_mode
&& !strcasecmp (e
->eb_mode
, "ascii")
2538 ? "ascii" : "binary";
2543 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
2547 adios ("fork", "unable to");
2551 close (fileno (ce
->ce_fp
));
2553 fprintf (stderr
, "unable to exec ");
2559 if (pidXwait (child_id
, NULL
)) {
2560 username
= password
= NULL
;
2570 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
2575 mask
= umask (cachetype
? ~m_gmprot () : 0222);
2576 if ((fp
= fopen (cachefile
, "w"))) {
2578 FILE *gp
= ce
->ce_fp
;
2580 fseek (gp
, 0L, SEEK_SET
);
2582 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
2584 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
2588 admonish (ce
->ce_file
, "error reading");
2593 admonish (cachefile
, "error writing");
2602 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2603 *file
= ce
->ce_file
;
2604 return fileno (ce
->ce_fp
);
2615 return init_encoding (ct
, openMail
);
2620 openMail (CT ct
, char **file
)
2622 int child_id
, fd
, i
, vecp
;
2624 char *bp
, buffer
[BUFSIZ
], *vec
[7];
2625 struct exbody
*e
= ct
->c_ctexbody
;
2626 CE ce
= &ct
->c_cefile
;
2628 switch (openExternal (e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2639 if (!e
->eb_server
) {
2640 content_error (NULL
, ct
, "missing server parameter");
2647 pidcheck (pidwait (xpid
, NOTOK
));
2651 /* Get buffer ready to go */
2653 buflen
= sizeof(buffer
);
2655 /* Now, construct query message */
2656 snprintf (bp
, buflen
, "Retrieve content");
2662 snprintf (bp
, buflen
, " %s", e
->eb_partno
);
2668 snprintf (bp
, buflen
, " by asking %s\n\n%s\n? ",
2670 e
->eb_subject
? e
->eb_subject
: e
->eb_body
);
2672 /* Now, check answer */
2673 if (!getanswer (buffer
))
2677 vec
[vecp
++] = r1bindex (mailproc
, '/');
2678 vec
[vecp
++] = e
->eb_server
;
2679 vec
[vecp
++] = "-subject";
2680 vec
[vecp
++] = e
->eb_subject
? e
->eb_subject
: "mail-server request";
2681 vec
[vecp
++] = "-body";
2682 vec
[vecp
++] = e
->eb_body
;
2685 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
2689 advise ("fork", "unable to");
2693 execvp (mailproc
, vec
);
2694 fprintf (stderr
, "unable to exec ");
2700 if (pidXwait (child_id
, NULL
) == OK
)
2701 advise (NULL
, "request sent");
2705 if (*file
== NULL
) {
2707 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2708 adios(NULL
, "unable to create temporary file in %s",
2711 ce
->ce_file
= add (tempfile
, NULL
);
2714 ce
->ce_file
= add (*file
, NULL
);
2718 if ((ce
->ce_fp
= fopen (ce
->ce_file
, "w+")) == NULL
) {
2719 content_error (ce
->ce_file
, ct
, "unable to fopen for reading/writing");
2723 /* showproc is for mhshow and mhstore, though mhlist -debug
2724 * prints it, too. */
2726 free (ct
->c_showproc
);
2727 ct
->c_showproc
= add ("true", NULL
);
2729 fseek (ce
->ce_fp
, 0L, SEEK_SET
);
2730 *file
= ce
->ce_file
;
2731 return fileno (ce
->ce_fp
);
2742 return init_encoding (ct
, openURL
);
2747 openURL (CT ct
, char **file
)
2749 struct exbody
*e
= ct
->c_ctexbody
;
2750 CE ce
= &ct
->c_cefile
;
2751 char *urlprog
, *program
;
2752 char buffer
[BUFSIZ
], cachefile
[BUFSIZ
];
2753 int fd
, caching
, cachetype
;
2754 struct msgs_array args
= { 0, 0, NULL
};
2757 if ((urlprog
= context_find(nmhaccessurl
)) && *urlprog
== '\0')
2761 content_error(NULL
, ct
, "No entry for nmh-access-url in profile");
2765 switch (openExternal(e
->eb_parent
, e
->eb_content
, ce
, file
, &fd
)) {
2777 content_error(NULL
, ct
, "missing url parameter");
2784 pidcheck (pidwait (xpid
, NOTOK
));
2788 ce
->ce_unlink
= (*file
== NULL
);
2790 cachefile
[0] = '\0';
2792 if (find_cache(NULL
, wcachesw
, &cachetype
, e
->eb_content
->c_id
,
2793 cachefile
, sizeof(cachefile
)) != NOTOK
) {
2794 if (*file
== NULL
) {
2801 ce
->ce_file
= add(*file
, NULL
);
2803 ce
->ce_file
= add(cachefile
, NULL
);
2806 if ((tempfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
2807 adios(NULL
, "unable to create temporary file in %s",
2810 ce
->ce_file
= add (tempfile
, NULL
);
2813 if ((ce
->ce_fp
= fopen(ce
->ce_file
, "w+")) == NULL
) {
2814 content_error(ce
->ce_file
, ct
, "unable to fopen for read/writing");
2818 switch (child_id
= fork()) {
2820 adios ("fork", "unable to");
2824 argsplit_msgarg(&args
, urlprog
, &program
);
2825 app_msgarg(&args
, e
->eb_url
);
2826 app_msgarg(&args
, NULL
);
2827 dup2(fileno(ce
->ce_fp
), 1);
2828 close(fileno(ce
->ce_fp
));
2829 execvp(program
, args
.msgs
);
2830 fprintf(stderr
, "Unable to exec ");
2836 if (pidXwait(child_id
, NULL
)) {
2844 chmod(cachefile
, cachetype
? m_gmprot() : 0444);
2849 mask
= umask (cachetype
? ~m_gmprot() : 0222);
2850 if ((fp
= fopen(cachefile
, "w"))) {
2852 FILE *gp
= ce
->ce_fp
;
2854 fseeko(gp
, 0, SEEK_SET
);
2856 while ((cc
= fread(buffer
, sizeof(*buffer
),
2857 sizeof(buffer
), gp
)) > 0)
2858 fwrite(buffer
, sizeof(*buffer
), cc
, fp
);
2863 admonish(ce
->ce_file
, "error reading");
2871 fseeko(ce
->ce_fp
, 0, SEEK_SET
);
2872 *file
= ce
->ce_file
;
2877 readDigest (CT ct
, char *cp
)
2882 unsigned char *dp
, value
, *ep
;
2888 for (ep
= (dp
= ct
->c_digest
)
2889 + sizeof(ct
->c_digest
) / sizeof(ct
->c_digest
[0]); *cp
; cp
++)
2894 || (value
= b642nib
[*cp
& 0x7f]) > 0x3f) {
2896 fprintf (stderr
, "invalid BASE64 encoding\n");
2900 bits
|= value
<< bitno
;
2902 if ((bitno
-= 6) < 0) {
2903 if (dp
+ (3 - skip
) > ep
)
2904 goto invalid_digest
;
2905 *dp
++ = (bits
>> 16) & 0xff;
2907 *dp
++ = (bits
>> 8) & 0xff;
2909 *dp
++ = bits
& 0xff;
2919 goto self_delimiting
;
2924 fprintf (stderr
, "premature ending (bitno %d)\n", bitno
);
2934 fprintf (stderr
, "invalid MD5 digest (got %d octets)\n",
2942 fprintf (stderr
, "MD5 digest=");
2943 for (dp
= ct
->c_digest
; dp
< ep
; dp
++)
2944 fprintf (stderr
, "%02x", *dp
& 0xff);
2945 fprintf (stderr
, "\n");
2952 /* Multipart parts might have content before the first subpart and/or
2953 after the last subpart that hasn't been stored anywhere else, so do
2956 get_leftover_mp_content (CT ct
, int before
/* or after */) {
2957 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2959 int found_boundary
= 0;
2960 char buffer
[BUFSIZ
];
2963 char *content
= NULL
;
2965 if (! m
) return NOTOK
;
2968 if (! m
->mp_parts
|| ! m
->mp_parts
->mp_part
) return NOTOK
;
2970 /* Isolate the beginning of this part to the beginning of the
2971 first subpart and save any content between them. */
2972 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
2973 max
= m
->mp_parts
->mp_part
->c_begin
- ct
->c_begin
;
2974 boundary
= concat ("--", m
->mp_start
, NULL
);
2976 struct part
*last_subpart
= NULL
;
2977 struct part
*subpart
;
2979 /* Go to the last subpart to get its end position. */
2980 for (subpart
= m
->mp_parts
; subpart
; subpart
= subpart
->mp_next
) {
2981 last_subpart
= subpart
;
2984 if (last_subpart
== NULL
) return NOTOK
;
2986 /* Isolate the end of the last subpart to the end of this part
2987 and save any content between them. */
2988 fseeko (ct
->c_fp
, last_subpart
->mp_part
->c_end
, SEEK_SET
);
2989 max
= ct
->c_end
- last_subpart
->mp_part
->c_end
;
2990 boundary
= concat ("--", m
->mp_stop
, NULL
);
2993 /* Back up by 1 to pick up the newline. */
2994 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
2995 read
+= strlen (buffer
);
2996 /* Don't look beyond beginning of first subpart (before) or
2997 next part (after). */
2998 if (read
> max
) buffer
[read
-max
] = '\0';
3001 if (! strcmp (buffer
, boundary
)) {
3005 if (! found_boundary
&& ! strcmp (buffer
, boundary
)) {
3011 if ((before
&& ! found_boundary
) || (! before
&& found_boundary
)) {
3013 char *old_content
= content
;
3014 content
= concat (content
, buffer
, NULL
);
3018 ? concat ("\n", buffer
, NULL
)
3019 : concat (buffer
, NULL
);
3024 if (found_boundary
|| read
> max
) break;
3026 if (read
> max
) break;
3030 /* Skip the newline if that's all there is. */
3034 /* Remove trailing newline, except at EOF. */
3035 if ((before
|| ! feof (ct
->c_fp
)) &&
3036 (cp
= content
+ strlen (content
)) > content
&&
3041 if (strlen (content
) > 1) {
3043 m
->mp_content_before
= content
;
3045 m
->mp_content_after
= content
;
3059 ct_type_str (int type
) {
3061 case CT_APPLICATION
:
3062 return "application";
3078 return "unknown_type";
3084 ct_subtype_str (int type
, int subtype
) {
3086 case CT_APPLICATION
:
3088 case APPLICATION_OCTETS
:
3090 case APPLICATION_POSTSCRIPT
:
3091 return "postscript";
3093 return "unknown_app_subtype";
3097 case MESSAGE_RFC822
:
3099 case MESSAGE_PARTIAL
:
3101 case MESSAGE_EXTERNAL
:
3104 return "unknown_msg_subtype";
3110 case MULTI_ALTERNATE
:
3111 return "alternative";
3114 case MULTI_PARALLEL
:
3117 return "unknown_multipart_subtype";
3128 return "unknown_text_subtype";
3131 return "unknown_type";
3136 /* Find the content type and InitFunc for the CT. */
3137 const struct str2init
*
3138 get_ct_init (int type
) {
3139 const struct str2init
*sp
;
3141 for (sp
= str2cts
; sp
->si_key
; ++sp
) {
3142 if (type
== sp
->si_val
) {
3151 ce_str (int encoding
) {
3156 return "quoted-printable";
3172 /* Find the content type and InitFunc for the content encoding method. */
3173 const struct str2init
*
3174 get_ce_method (const char *method
) {
3175 struct str2init
*sp
;
3177 for (sp
= str2ces
; sp
->si_key
; ++sp
) {
3178 if (! strcasecmp (method
, sp
->si_key
)) {
3187 parse_header_attrs (const char *filename
, int len
, char **header_attrp
, CI ci
,
3189 char **attr
= ci
->ci_attrs
;
3190 char *cp
= *header_attrp
;
3192 while (*cp
== ';') {
3193 char *dp
, *vp
, *up
, c
;
3195 /* Relies on knowledge of this declaration:
3196 * char *ci_attrs[NPARMS + 2];
3198 if (attr
>= ci
->ci_attrs
+ sizeof ci
->ci_attrs
/sizeof (char *) - 2) {
3200 "too many parameters in message %s's %s: field (%d max)",
3201 filename
, TYPE_FIELD
, NPARMS
);
3207 while (isspace ((unsigned char) *cp
))
3211 get_comment (filename
, ci
, &cp
, 1) == NOTOK
) {
3218 "extraneous trailing ';' in message %s's %s: "
3220 filename
, TYPE_FIELD
);
3225 /* down case the attribute name */
3226 for (dp
= cp
; istoken ((unsigned char) *dp
); dp
++)
3227 if (isalpha((unsigned char) *dp
) && isupper ((unsigned char) *dp
))
3228 *dp
= tolower ((unsigned char) *dp
);
3230 for (up
= dp
; isspace ((unsigned char) *dp
);)
3232 if (dp
== cp
|| *dp
!= '=') {
3234 "invalid parameter in message %s's %s: "
3235 "field\n%*.*sparameter %s (error detected at offset %d)",
3236 filename
, TYPE_FIELD
, len
, len
, "", cp
, dp
- cp
);
3241 vp
= (*attr
= add (cp
, NULL
)) + (up
- cp
);
3243 for (dp
++; isspace ((unsigned char) *dp
);)
3246 /* Now store the attribute value. */
3247 ci
->ci_values
[attr
- ci
->ci_attrs
] = vp
= *attr
+ (dp
- cp
);
3250 for (cp
= ++dp
, dp
= vp
;;) {
3251 switch (c
= *cp
++) {
3255 "invalid quoted-string in message %s's %s: "
3256 "field\n%*.*s(parameter %s)",
3257 filename
, TYPE_FIELD
, len
, len
, "", *attr
);
3263 if ((c
= *cp
++) == '\0')
3278 for (cp
= dp
, dp
= vp
; istoken (*cp
); cp
++, dp
++)
3284 "invalid parameter in message %s's %s: "
3285 "field\n%*.*s(parameter %s)",
3286 filename
, TYPE_FIELD
, len
, len
, "", *attr
);
3291 while (isspace ((unsigned char) *cp
))
3295 get_comment (filename
, ci
, &cp
, 1) == NOTOK
) {