From: Ken Hornstein Date: Thu, 6 Mar 2014 04:17:17 +0000 (-0500) Subject: Merge branch 'extended-params' X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/28c3595a77a8c942bee1057085776dad0b3d53f4?hp=7b9cb49c7c683bc69ac7fd2f20436ae2b40fbb6d Merge branch 'extended-params' --- diff --git a/Makefile.am b/Makefile.am index 587dfb84..a73a4215 100644 --- a/Makefile.am +++ b/Makefile.am @@ -68,10 +68,12 @@ TESTS = test/ali/test-ali test/anno/test-anno \ test/manpages/test-manpages \ test/mhbuild/test-attach \ test/mhbuild/test-cte \ + test/mhbuild/test-ext-params \ test/mhbuild/test-forw test/mhbuild/test-header-encode \ test/mhbuild/test-utf8-body \ test/mhfixmsg/test-mhfixmsg \ - test/mhlist/test-mhlist test/mhmail/test-mhmail \ + test/mhlist/test-mhlist test/mhlist/test-ext-params \ + test/mhmail/test-mhmail \ test/mhparam/test-mhparam test/mhpath/test-mhpath \ test/mhshow/test-charset test/mhshow/test-textcharset \ test/mhshow/test-cte-binary test/mhshow/test-qp \ @@ -289,7 +291,7 @@ uip_anno_LDADD = $(LDADD) $(POSTLINK) uip_burst_SOURCES = uip/burst.c uip/mhparse.c uip/mhmisc.c uip/mhfree.c \ uip/mhcachesbr.c uip/md5.c -uip_burst_LDADD = $(LDADD) $(POSTLINK) +uip_burst_LDADD = $(LDADD) $(ICONVLIB) $(POSTLINK) uip_comp_SOURCES = uip/comp.c uip/whatnowproc.c uip/whatnowsbr.c uip/sendsbr.c \ uip/annosbr.c uip/distsbr.c @@ -321,7 +323,7 @@ uip_mark_LDADD = $(LDADD) $(POSTLINK) uip_mhbuild_SOURCES = uip/mhbuild.c uip/mhbuildsbr.c uip/mhcachesbr.c \ uip/mhlistsbr.c uip/mhoutsbr.c uip/mhmisc.c \ uip/mhfree.c uip/mhparse.c uip/md5.c -uip_mhbuild_LDADD = $(LDADD) $(TERMLIB) $(POSTLINK) +uip_mhbuild_LDADD = $(LDADD) $(TERMLIB) $(ICONVLIB) $(POSTLINK) uip_mhfixmsg_SOURCES = uip/mhfixmsg.c uip/mhparse.c uip/mhcachesbr.c \ uip/mhoutsbr.c uip/mhmisc.c uip/mhfree.c \ @@ -330,7 +332,7 @@ uip_mhfixmsg_LDADD = $(LDADD) $(TERMLIB) $(ICONVLIB) $(POSTLINK) uip_mhlist_SOURCES = uip/mhlist.c uip/mhparse.c uip/mhcachesbr.c \ uip/mhlistsbr.c uip/mhmisc.c uip/mhfree.c uip/md5.c -uip_mhlist_LDADD = $(LDADD) $(TERMLIB) $(POSTLINK) +uip_mhlist_LDADD = $(LDADD) $(TERMLIB) $(ICONVLIB) $(POSTLINK) uip_mhn_SOURCES = uip/mhn.c uip/mhparse.c uip/mhcachesbr.c uip/mhshowsbr.c \ uip/mhlistsbr.c uip/mhstoresbr.c uip/mhmisc.c uip/mhfree.c \ @@ -430,7 +432,7 @@ uip_mhl_LDADD = $(LDADD) $(TERMLIB) $(ICONVLIB) $(POSTLINK) uip_mhtest_SOURCES = uip/mhtest.c uip/mhparse.c uip/mhcachesbr.c \ uip/mhoutsbr.c uip/mhmisc.c uip/mhfree.c \ uip/md5.c -uip_mhtest_LDADD = $(LDADD) $(TERMLIB) $(POSTLINK) +uip_mhtest_LDADD = $(LDADD) $(TERMLIB) $(ICONVLIB) $(POSTLINK) uip_mkstemp_SOURCES = uip/mkstemp.c uip_mkstemp_LDADD = $(LDADD) $(POSTLINK) @@ -453,7 +455,7 @@ uip_rcvtty_LDADD = $(LDADD) $(TERMLIB) $(ICONVLIB) $(POSTLINK) uip_slocal_SOURCES = uip/slocal.c uip/aliasbr.c uip/dropsbr.c uip_slocal_LDADD = $(LDADD) $(NDBM_LIBS) $(POSTLINK) -uip_viamail_SOURCES = uip/viamail.c uip/mhmisc.c uip/mhoutsbr.c uip/sendsbr.c \ +uip_viamail_SOURCES = uip/viamail.c uip/mhmisc.c uip/sendsbr.c \ uip/annosbr.c uip/distsbr.c uip_viamail_LDADD = $(LDADD) $(POSTLINK) diff --git a/docs/pending-release-notes b/docs/pending-release-notes index a4db0a16..780b2abe 100644 --- a/docs/pending-release-notes +++ b/docs/pending-release-notes @@ -78,6 +78,8 @@ NEW FEATURES necessary, the charset of text/plain content to match the user's locale setting. - Added support for %{charset} display string escape to mhshow(1). +- The MIME parsing and generating routines now support RFC 2231 extended + parameter information. ----------------- OBSOLETE FEATURES diff --git a/h/mhparse.h b/h/mhparse.h index 72188c6d..7ade8a3c 100644 --- a/h/mhparse.h +++ b/h/mhparse.h @@ -16,9 +16,10 @@ typedef struct hfield *HF; /* * Abstract types for MIME parsing/building */ -typedef struct cefile *CE; -typedef struct CTinfo *CI; -typedef struct Content *CT; +typedef struct cefile *CE; +typedef struct CTinfo *CI; +typedef struct Content *CT; +typedef struct Parameter *PM; /* * type for Init function (both type and transfer encoding) @@ -43,6 +44,17 @@ struct hfield { HF next; /* link to next header field */ }; +/* + * Structure for holding MIME parameter elements. + */ +struct Parameter { + char *pm_name; /* Parameter name */ + char *pm_value; /* Parameter value */ + char *pm_charset; /* Parameter character set (optional) */ + char *pm_lang; /* Parameter language tag (optional) */ + PM pm_next; /* Pointer to next element */ +}; + /* * Structure for storing parsed elements * of the Content-Type component. @@ -50,8 +62,8 @@ struct hfield { struct CTinfo { char *ci_type; /* content type */ char *ci_subtype; /* content subtype */ - char *ci_attrs[NPARMS + 2]; /* attribute names */ - char *ci_values[NPARMS]; /* attribute values */ + PM ci_first_pm; /* Pointer to first MIME parameter */ + PM ci_last_pm; /* Pointer to last MIME parameter */ char *ci_comment; /* RFC-822 comments */ char *ci_magic; }; @@ -99,6 +111,9 @@ struct Content { char *c_id; /* Content-ID: */ char *c_descr; /* Content-Description: */ char *c_dispo; /* Content-Disposition: */ + char *c_dispo_type; /* Type of Content-Disposition */ + PM c_dispo_first; /* Pointer to first disposition parm */ + PM c_dispo_last; /* Pointer to last disposition parm */ char *c_partno; /* within multipart content */ /* Content-Type info */ @@ -307,13 +322,14 @@ CT parse_mime (char *); * CE_QUOTED. * maxunencoded - The maximum line length before the default encoding for * text parts is quoted-printable. + * verbose - If 1, output verbose information during message composition * * Returns a CT structure describing the resulting MIME message. If the * -auto flag is set and a MIME-Version header is encountered, the return * value is NULL. */ CT build_mime (char *infile, int autobuild, int dist, int directives, - int encoding, size_t maxunencoded); + int encoding, size_t maxunencoded, int verbose); int add_header (CT, char *, char *); int get_ctinfo (char *, CT, int); @@ -326,9 +342,136 @@ char *ct_subtype_str (int, int); const struct str2init *get_ct_init (int); const char *ce_str (int); const struct str2init *get_ce_method (const char *); -int parse_header_attrs (const char *, int, char **, CI, int *); -char *update_attr (char *, const char *, const char *e); char *content_charset (CT); int convert_charset (CT, char *, int *); +/* + * Given a list of messages, display information about them on standard + * output. + * + * Argumens are: + * + * cts - An array of CT elements of messages that need to be + * displayed. Array is terminated by a NULL. + * headsw - If 1, display a column header. + * sizesw - If 1, display the size of the part. + * verbosw - If 1, display verbose information + * debugsw - If 1, turn on debugging for the output. + * disposw - If 1, display MIME part disposition information. + * + */ +void list_all_messages(CT *cts, int headsw, int sizesw, int verbosw, + int debugsw, int disposw); + +/* + * List the content information of a single MIME part on stdout. + * + * Arguments are: + * + * ct - MIME Content structure to display. + * toplevel - If set, we're at the top level of a message + * realsize - If set, determine the real size of the content + * verbose - If set, output verbose information + * debug - If set, turn on debugging for the output + * dispo - If set, display MIME part disposition information. + * + * Returns OK on success, NOTOK otherwise. + */ +int list_content(CT ct, int toplevel, int realsize, int verbose, int debug, + int dispo); + +/* + * Display content-appropriate information on MIME parts, decending recursively + * into multipart content if appropriate. Uses list_content() for displaying + * generic information. + * + * Arguments and return value are the same as list_content(). + */ +int list_switch(CT ct, int toplevel, int realsize, int verbose, int debug, + int dispo); + +/* + * Given a linked list of parameters, build an output string for them. This + * string is designed to be concatenated on an already-built header. + * + * Arguments are: + * + * initialwidth - Current width of the header. Used to compute when to wrap + * parameters on the first line. The following lines will + * be prefixed by a tab (\t) character. + * params - Pointer to head of linked list of parameters. + * offsetout - The final line offset after all the parameters have been + * output. May be NULL. + * external - If set, outputting an external-body type and will not + * output a "body" parameter. + + * Returns a pointer to the resulting parameter string. This string must + * be free()'d by the caller. Returns NULL on error. + */ +char *output_params(size_t initialwidth, PM params, int *offsetout, + int external); + +/* + * Add a parameter to the parameter linked list. + * + * Arguments are: + * + * first - Pointer to head of linked list + * last - Pointer to tail of linked list + * name - Name of parameter + * value - Value of parameter + * nocopy - If set, will use the pointer values directly for "name" + * and "value" instead of making their own copy. These + * pointers will be free()'d later by the MIME routines, so + * they should not be used after calling this function! + * + * Returns allocated parameter element + */ +PM add_param(PM *first, PM *last, char *name, char *value, int nocopy); + +/* + * Replace (or add) a parameter to the parameter linked list. + * + * If the named parameter already exists on the parameter linked list, + * replace the value with the new one. Otherwise add it to the linked + * list. All parameters are identical to add_param(). + */ +PM replace_param(PM *first, PM *last, char *name, char *value, int nocopy); + +/* + * Retrieve a parameter value from a parameter linked list. Convert to the + * local character set if required. + * + * Arguments are: + * + * first - Pointer to head of parameter linked list. + * name - Name of parameter. + * replace - If characters in the parameter list cannot be converted to + * the local character set, replace with this character. + * fetchonly - If true, return pointer to original value, no conversion + * performed. + * + * Returns parameter value if found, NULL otherwise. Memory must be free()'d + * unless fetchonly is set. + */ + +char *get_param(PM first, const char *name, char replace, int fetchonly); + +/* + * Fetch a parameter value from a parameter structure, converting it to + * the local character set. + * + * Arguments are: + * + * pm - Pointer to parameter structure + * replace - If characters in the parameter list cannot be converted to + * the local character set, replace with this character. + * + * Returns a pointer to the parameter value. Memory is stored in an + * internal buffer, so the returned value is only valid until the next + * call to get_param_value() or get_param() (get_param() uses get_param_value() + * internally). + */ +char *get_param_value(PM pm, char replace); + extern int checksw; /* Add Content-MD5 field */ diff --git a/h/mime.h b/h/mime.h index d152094a..92d6462a 100644 --- a/h/mime.h +++ b/h/mime.h @@ -35,6 +35,20 @@ && (c) != '/' && (c) != '[' && (c) != ']' \ && (c) != '?' && (c) != '=') +/* + * Definitions for RFC 2231 encoding + */ +#define istspecial(c) ((c) == '(' || (c) == ')' || (c) == '<' || (c) == '>' \ + || (c) == '@' || (c) == ',' || (c) == ';' \ + || (c) == ':' || (c) == '\\' || (c) == '"' \ + || (c) == '/' || (c) == '[' || (c) == ']' \ + || (c) == '?' || (c) == '=') + +#define isparamencode(c) (!isascii((unsigned char) c) || \ + iscntrl((unsigned char) c) || istspecial(c) || \ + (c) == ' ' || (c) == '*' || (c) == '\'' || \ + (c) == '%') + #define MAXTEXTPERLN 78 #define MAXLONGLINE 998 #define CPERLIN 76 diff --git a/h/prototypes.h b/h/prototypes.h index 621146c3..f8adbac5 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -47,6 +47,19 @@ char *cpytrim (const char *); int decode_rfc2047 (char *, char *, size_t); void discard (FILE *); char *upcase (const char *); + +/* + * Decode two characters into their quoted-printable representation. + * + * Arguments are: + * + * byte1 - First character of Q-P representation + * byte2 - Second character of Q-P representation + * + * Returns the decoded value, -1 if the conversion failed. + */ +int decode_qp(unsigned char byte1, unsigned char byte2); + int default_done (int); /* diff --git a/h/utils.h b/h/utils.h index d07e46c1..92c940de 100644 --- a/h/utils.h +++ b/h/utils.h @@ -43,3 +43,16 @@ int open_form(char **, char *); char *find_str (const char [], size_t, const char *); char *rfind_str (const char [], size_t, const char *); char *nmh_strcasestr (const char *, const char *); + +/* + * See if a string contains 8 bit characters (use isascii() for the test). + * Arguments include: + * + * start - Pointer to start of string to test. + * end - End of string to test (test will stop before reaching + * this point). If NULL, continue until reaching '\0'. + * + * This function always stops at '\0' regardless of the value of 'end'. + * Returns 1 if the string contains an 8-bit character, 0 if it does not. + */ +int contains8bit(const char *start, const char *end); diff --git a/man/mhbuild.man b/man/mhbuild.man index db80e232..cbbaae67 100644 --- a/man/mhbuild.man +++ b/man/mhbuild.man @@ -17,6 +17,7 @@ mhbuild \- translate MIME composition draft .RB [ \-rfc934mode " | " \-norfc934mode ] .RB [ \-contentid " | " \-nocontentid ] .RB [ \-verbose " | " \-noverbose ] +.RB [ \-disposition " | " \-nodisposition ] .RB [ \-check " | " \-nocheck ] .RB [ \-headerencoding .IR encoding\-algorithm @@ -36,7 +37,7 @@ a valid MIME message. .B mhbuild creates multi-media messages as specified in RFC 2045 to RFC 2049. This includes the encoding of message headers as specified -by RFC 2047. +by RFC 2047, and the encoding of MIME parameters as specified in RFC 2231. .PP If you specify the name of the composition file as \*(lq-\*(rq, then @@ -81,6 +82,11 @@ switch is present, then the listing will show any \*(lqextra\*(rq information that is present in the message, such as comments in the \*(lqContent-Type\*(rq header. +.PP +If the +.B \-disposition +switch is present, then the listing will show any relevant information from +the \*(lqContent-Disposition\*(rq header. .SS "Simplified Attachment Interface" For users who wish to simply attach files to text content, .B mhbuild @@ -300,7 +306,7 @@ would be as follows: .fi .RE .PP -Any long URLs will be wrapped according to RFC 2017 rules. +Any long URLs will be wrapped according to RFC 2231 rules. .PP The \*(lqmessage\*(rq directive (#forw) is used to specify a message or group of messages to include. You may optionally specify the name of @@ -777,6 +783,7 @@ is checked. .RB ` \-contentid ' .RB ` \-nocheck ' .RB ` \-noverbose ' +.RB ` \-nodisposition ' .RB ` \-autoheaderencoding ' .RB ` "\-maxunencoded\ 78"' .fi diff --git a/man/mhlist.man b/man/mhlist.man index 46b75636..69394fec 100644 --- a/man/mhlist.man +++ b/man/mhlist.man @@ -27,6 +27,7 @@ mhlist \- list information about MIME messages .RB [ \-check " | " \-nocheck ] .RB [ \-changecur " | " \-nochangecur ] .RB [ \-verbose " | " \-noverbose ] +.RB [ \-disposition " | " \-nodisposition ] .RB [ \-version ] .RB [ \-help ] .ad @@ -61,6 +62,11 @@ switch is present, then the listing will show any \*(lqextra\*(rq information that is present in the message, such as comments in the \*(lqContent-Type\*(rq header. .PP +If the +.B \-disposition +switch is present, then the listing will show any relevant information from +the \*(lqContent-Disposition\*(rq header. +.PP The option .B \-file .I file @@ -187,6 +193,7 @@ integrity of the content. .RB ` \-wcache\ ask ' .RB ` \-changecur ' .RB ` \-noverbose ' +.RB ` \-nodisposition ' .fi .SH CONTEXT If a folder is given, it will become the current folder. The last diff --git a/sbr/fmt_rfc2047.c b/sbr/fmt_rfc2047.c index 5e5a5a8e..aa01913b 100644 --- a/sbr/fmt_rfc2047.c +++ b/sbr/fmt_rfc2047.c @@ -21,6 +21,14 @@ static signed char hexindex[] = { -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 }; @@ -37,8 +45,12 @@ static signed char index_64[128] = { #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)]) -static int -unqp (unsigned char byte1, unsigned char byte2) +/* + * Decode two quoted-pair characters + */ + +int +decode_qp (unsigned char byte1, unsigned char byte2) { if (hexindex[byte1] == -1 || hexindex[byte2] == -1) return -1; @@ -234,7 +246,7 @@ decode_rfc2047 (char *str, char *dst, size_t dstlen) if (quoted_printable) { for (pp = startofmime; pp < endofmime; pp++) { if (*pp == '=') { - c = unqp (pp[1], pp[2]); + c = decode_qp (pp[1], pp[2]); if (c == -1) continue; if (c != 0) diff --git a/sbr/mf.c b/sbr/mf.c index c8868178..d99d2b38 100644 --- a/sbr/mf.c +++ b/sbr/mf.c @@ -21,7 +21,6 @@ static int local_part (char *); static int domain (char *); static int route (char *); static int my_lex (char *); -static int contains8bit (const char *); int @@ -235,8 +234,8 @@ getadrx (const char *addrs) * Reject the address if key fields contain 8bit characters */ - if (contains8bit(mbox) || contains8bit(host) || contains8bit(path) || - contains8bit(grp)) { + if (contains8bit(mbox, NULL) || contains8bit(host, NULL) || + contains8bit(path, NULL) || contains8bit(grp, NULL)) { strcpy(err, "Address contains 8-bit characters"); } @@ -728,25 +727,6 @@ got_atom: ; } -/* - * Return true if the string contains an 8-bit character - */ - -static int -contains8bit(const char *p) -{ - if (! p) - return 0; - - for (; *p; p++) { - if (! isascii((unsigned char) *p)) - return 1; - } - - return 0; -} - - char * legal_person (const char *p) { diff --git a/sbr/utils.c b/sbr/utils.c index 59ab7c72..4405f4c9 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -365,3 +365,24 @@ upcase (const char *str) { return up; } + + +/* + * Scan for any 8-bit characters. Return 1 if they exist. + * + * Scan up until the given endpoint (but not the actual endpoint itself). + * If the endpoint is NULL, scan until a '\0' is reached. + */ + +int +contains8bit(const char *start, const char *end) +{ + if (! start) + return 0; + + while (*start != '\0' && (!end || (start < end))) + if (! isascii((unsigned char) *start++)) + return 1; + + return 0; +} diff --git a/test/mhbuild/test-ext-params b/test/mhbuild/test-ext-params new file mode 100755 index 00000000..8294677a --- /dev/null +++ b/test/mhbuild/test-ext-params @@ -0,0 +1,226 @@ +#!/bin/sh +###################################################### +# +# Test the creation of RFC 2231 encoded parameters +# +###################################################### + +if test -z "${MH_OBJ_DIR}"; then + srcdir=`dirname "$0"`/../.. + MH_OBJ_DIR=`cd "$srcdir" && pwd`; export MH_OBJ_DIR +fi + +. "$MH_OBJ_DIR/test/common.sh" + +setup_test + +LC_ALL=en_US.UTF-8; export LC_ALL + +draft="$MH_TEST_DIR/$$.draft" +expected="$MH_TEST_DIR/$$.expected" + +# +# Try out a draft with some 8-bit encoded parameters +# + +cat > "$draft" < +cc: +Fcc: +outbox +------ +This is a test message +#image/jpeg {attachment; filename="tïny.jpg"} ${srcdir}/test/mhbuild/tiny.jpg +EOF + +run_prog mhbuild "$draft" + +cat > "$expected" < +cc: +Fcc: +outbox +MIME-Version: 1.0 +Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" + +------- =_aaaaaaaaaa0 +Content-Type: text/plain; charset="us-ascii" + +This is a test message + +------- =_aaaaaaaaaa0 +Content-Type: image/jpeg +Content-Disposition: attachment; filename*=UTF-8''t%C3%AFny.jpg +Content-Transfer-Encoding: base64 + +/9g= + +------- =_aaaaaaaaaa0-- +EOF + +check "$draft" "$expected" + +# +# Try out a draft with some long parameters +# + +cat > "$draft" < +cc: +Fcc: +outbox +------ +This is a test message +#image/jpeg {attachment; filename="This is an example of a rather long filename that is longer than would fit on a normal line.jpg"} \ +${srcdir}/test/mhbuild/tiny.jpg +EOF + +run_prog mhbuild "$draft" + +cat > "$expected" < +cc: +Fcc: +outbox +MIME-Version: 1.0 +Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" + +------- =_aaaaaaaaaa0 +Content-Type: text/plain; charset="us-ascii" + +This is a test message + +------- =_aaaaaaaaaa0 +Content-Type: image/jpeg +Content-Disposition: attachment; + filename*0="This is an example of a rather long filename that is lo"; + filename*1="nger than would fit on a normal line.jpg" +Content-Transfer-Encoding: base64 + +/9g= + +------- =_aaaaaaaaaa0-- +EOF + +check "$draft" "$expected" + +# +# Try out attach with a filename with 8-bit characters +# + +cp "${srcdir}/test/mhbuild/tiny.jpg" "$MH_TEST_DIR/tïny.jpg" + +cat > "$draft" < +cc: +Fcc: +outbox +Attach: $MH_TEST_DIR/tïny.jpg +------ +This is a test message +EOF + +run_prog mhbuild "$draft" + +cat > "$expected" < +cc: +Fcc: +outbox +MIME-Version: 1.0 +Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" + +------- =_aaaaaaaaaa0 +Content-Type: text/plain; charset="us-ascii" + +This is a test message + +------- =_aaaaaaaaaa0 +Content-Type: image/jpeg; name*=UTF-8''t%C3%AFny.jpg +Content-Description: =?UTF-8?B?dMOvbnkuanBn?= +Content-Disposition: attachment; filename*=UTF-8''t%C3%AFny.jpg +Content-Transfer-Encoding: base64 + +/9g= + +------- =_aaaaaaaaaa0-- +EOF + +check "$draft" "$expected" + +# +# Alternate version; specify a disposion, but not an explicit filename +# + +cat > "$draft" < +cc: +Fcc: +outbox +------ +This is a test message +#image/jpeg {attachment} $MH_TEST_DIR/tïny.jpg +EOF + +run_prog mhbuild "$draft" + +cat > "$expected" < +cc: +Fcc: +outbox +MIME-Version: 1.0 +Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" + +------- =_aaaaaaaaaa0 +Content-Type: text/plain; charset="us-ascii" + +This is a test message + +------- =_aaaaaaaaaa0 +Content-Type: image/jpeg +Content-Disposition: attachment; filename*=UTF-8''t%C3%AFny.jpg +Content-Transfer-Encoding: base64 + +/9g= + +------- =_aaaaaaaaaa0-- +EOF + +check "$draft" "$expected" + +# +# Test out message/external-body decoding +# + +cat > "$draft" < +cc: +Fcc: +outbox +------ +This is a test message +#@application/octet-stream [Test of a long URL] {attachment; \ +filename=test.tar.gz} access-type=url; url="http://www.example.com/this/is/an/example/of/a/very/long/url/that-should-be-wrapped/name.tar.gz" +EOF + +run_prog mhbuild "$draft" + +cat > "$expected" < +cc: +Fcc: +outbox +MIME-Version: 1.0 +Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" + +------- =_aaaaaaaaaa0 +Content-Type: text/plain; charset="us-ascii" + +This is a test message + +------- =_aaaaaaaaaa0 +Content-Type: message/external-body; access-type="url"; + url*0="http://www.example.com/this/is/an/example/of/a/very/long/url"; + url*1="/that-should-be-wrapped/name.tar.gz" + +Content-Type: application/octet-stream +Content-Description: Test of a long URL +Content-Disposition: attachment; filename="test.tar.gz" + +------- =_aaaaaaaaaa0-- +EOF + +check "$draft" "$expected" + +exit ${failed:-0} diff --git a/test/mhfixmsg/test-mhfixmsg b/test/mhfixmsg/test-mhfixmsg index d3516175..d60b9d76 100755 --- a/test/mhfixmsg/test-mhfixmsg +++ b/test/mhfixmsg/test-mhfixmsg @@ -770,7 +770,7 @@ fi cat >"$expected" < diff --git a/test/mhlist/test-ext-params b/test/mhlist/test-ext-params new file mode 100755 index 00000000..409a4eda --- /dev/null +++ b/test/mhlist/test-ext-params @@ -0,0 +1,180 @@ +#!/bin/sh +###################################################### +# +# Test mhlist with extended parameters (RFC 2231) +# +###################################################### + +set -e + +if test -z "${MH_OBJ_DIR}"; then + srcdir=`dirname $0`/../.. + MH_OBJ_DIR=`cd $srcdir && pwd`; export MH_OBJ_DIR +fi + +. "$MH_OBJ_DIR/test/common.sh" + +setup_test + +expected=$MH_TEST_DIR/$$.expected +actual=$MH_TEST_DIR/$$.actual +LC_ALL=en_US.UTF-8; export LC_ALL + +# +# Write a message with some extended parameters +# + +msgfile=`mhpath new` +cat > $msgfile < $msgfile < $msgfile <c_cefile; @@ -1075,7 +1074,7 @@ compose_content (CT ct) sprintf (pp, "%d", partnum); p->c_partno = add (partnam, NULL); - if (compose_content (p) == NOTOK) + if (compose_content (p, verbose) == NOTOK) return NOTOK; } @@ -1127,7 +1126,7 @@ compose_content (CT ct) if (!ce->ce_file) { pid_t child_id; int i, xstdout, len, buflen; - char *bp, **ap, *cp; + char *bp, *cp; char *vec[4], buffer[BUFSIZ]; FILE *out; CI ci = &ct->c_ctinfo; @@ -1159,11 +1158,12 @@ compose_content (CT ct) case 'a': { /* insert parameters from directive */ - char **ep; char *s = ""; + PM pm; - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) { - snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep); + for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) { + snprintf (bp, buflen, "%s%s=\"%s\"", s, + pm->pm_name, get_param_value(pm, '?')); len = strlen (bp); bp += len; buflen -= len; @@ -1211,7 +1211,7 @@ raw: } } - if (verbosw) + if (verbose) printf ("composing content %s/%s from command\n\t%s\n", ci->ci_type, ci->ci_subtype, buffer); @@ -1466,22 +1466,10 @@ scan_content (CT ct, size_t maxunencoded) t = (struct text *) ct->c_ctparams; if (t->tx_charset == CHARSET_UNSPECIFIED) { CI ci = &ct->c_ctinfo; - char **ap, **ep; - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) - continue; - - if (contains8bit) { - *ap = concat ("charset=", write_charset_8bit(), NULL); - } else { - *ap = add ("charset=us-ascii", NULL); - } + add_param(&ci->ci_first_pm, &ci->ci_last_pm, "charset", + contains8bit ? write_charset_8bit() : "us-ascii", 0); t->tx_charset = CHARSET_SPECIFIED; - - cp = strchr(*ap++, '='); - *ap = NULL; - *cp++ = '\0'; - *ep = cp; } } @@ -1534,10 +1522,9 @@ scan_content (CT ct, size_t maxunencoded) */ static int -build_headers (CT ct) +build_headers (CT ct, int header_encoding) { int cc, mailbody, extbody, len; - char **ap, **ep; char *np, *vp, buffer[BUFSIZ]; CI ci = &ct->c_ctinfo; @@ -1546,16 +1533,10 @@ build_headers (CT ct) * boundary to the list of attribute/value pairs. */ if (ct->c_type == CT_MULTIPART) { - char *cp; static int level = 0; /* store nesting level */ - ap = ci->ci_attrs; - ep = ci->ci_values; - snprintf (buffer, sizeof(buffer), "boundary=%s%d", prefix, level++); - cp = strchr(*ap++ = add (buffer, NULL), '='); - *ap = NULL; - *cp++ = '\0'; - *ep = cp; + snprintf (buffer, sizeof(buffer), "%s%d", prefix, level++); + add_param(&ci->ci_first_pm, &ci->ci_last_pm, "boundary", buffer, 0); } /* @@ -1585,59 +1566,16 @@ build_headers (CT ct) * Append the attribute/value pairs to * the end of the Content-Type line. */ - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) { - if (mailbody && !strcasecmp (*ap, "body")) - continue; - - vp = add (";", vp); - len++; - - /* - * According to RFC 2017, if we have a URL longer than 40 characters - * we have to break it across multiple lines - */ - - if (extbody && strcasecmp (*ap, "url") == 0) { - char *value = *ep; - - /* 7 here refers to " url=\"\"" */ - if (len + 1 + (cc = (min(MAXURLTOKEN, strlen(value)) + 7)) >= - CPERLIN) { - vp = add ("\n\t", vp); - len = 8; - } else { - vp = add (" ", vp); - len++; - } - vp = add ("url=\"", vp); - len += 5; + if (ci->ci_first_pm) { + char *s = output_params(len, ci->ci_first_pm, &len, mailbody); - while (strlen(value) > MAXURLTOKEN) { - strncpy(buffer, value, MAXURLTOKEN); - buffer[MAXURLTOKEN] = '\0'; - vp = add (buffer, vp); - vp = add ("\n\t", vp); - value += MAXURLTOKEN; - len = 8; - } - - vp = add (value, vp); - vp = add ("\"", vp); - len += strlen(value) + 1; - continue; - } + if (!s) + adios(NULL, "Internal error: failed outputting Content-Type " + "parameters"); - snprintf (buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep); - if (len + 1 + (cc = strlen (buffer)) >= CPERLIN) { - vp = add ("\n\t", vp); - len = 8; - } else { - vp = add (" ", vp); - len++; - } - vp = add (buffer, vp); - len += cc; + vp = add (s, vp); + free(s); } /* @@ -1667,23 +1605,34 @@ build_headers (CT ct) vp = concat (" ", ct->c_id, NULL); add_header (ct, np, vp); } - /* * output the Content-Description */ if (ct->c_descr) { np = add (DESCR_FIELD, NULL); vp = concat (" ", ct->c_descr, NULL); + if (encode_rfc2047(DESCR_FIELD, &vp, header_encoding, NULL)) + adios(NULL, "Unable to encode %s header", DESCR_FIELD); add_header (ct, np, vp); } /* - * output the Content-Disposition + * output the Content-Disposition. If it's NULL but c_dispo_type is + * set, then we need to build it. */ if (ct->c_dispo) { np = add (DISPO_FIELD, NULL); vp = concat (" ", ct->c_dispo, NULL); add_header (ct, np, vp); + } else if (ct->c_dispo_type) { + vp = concat (" ", ct->c_dispo_type, NULL); + len = strlen(DISPO_FIELD) + strlen(vp) + 1; + np = output_params(len, ct->c_dispo_first, NULL, 0); + vp = add(np, vp); + vp = add("\n", vp); + if (np) + free(np); + add_header (ct, getcpy(DISPO_FIELD), vp); } skip_headers: @@ -1767,7 +1716,7 @@ skip_headers: CT p; p = part->mp_part; - build_headers (p); + build_headers (p, header_encoding); } } break; @@ -1777,7 +1726,7 @@ skip_headers: struct exbody *e; e = (struct exbody *) ct->c_ctparams; - build_headers (e->eb_content); + build_headers (e->eb_content, header_encoding); } break; @@ -1889,8 +1838,9 @@ calculate_digest (CT ct, int asciiP) static void setup_attach_content(CT ct, char *filename) { - char *type, **ap, **ep, *simplename = r1bindex(filename, '/'); + char *type, *simplename = r1bindex(filename, '/'); struct str2init *s2i; + PM pm; if (! (type = mime_type(filename))) { adios(NULL, "Unable to determine MIME type of \"%s\"", filename); @@ -1946,20 +1896,18 @@ setup_attach_content(CT ct, char *filename) * content-description, and the content-disposition. */ - for (ap = ct->c_ctinfo.ci_attrs, ep = ct->c_ctinfo.ci_values; *ap; - ap++, ep++) { - if (strcasecmp(*ap, "name") == 0) { - if (*ep) - free(*ep); - *ep = getcpy(simplename); + for (pm = ct->c_ctinfo.ci_first_pm; pm; pm = pm->pm_next) { + if (strcasecmp(pm->pm_name, "name") == 0) { + if (pm->pm_value) + free(pm->pm_value); + pm->pm_value = getcpy(simplename); break; } } - if (*ap == NULL) { - *ap = getcpy("name"); - *ep = getcpy(simplename); - } + if (pm == NULL) + add_param(&ct->c_ctinfo.ci_first_pm, &ct->c_ctinfo.ci_last_pm, + "name", simplename, 0); ct->c_descr = getcpy(simplename); ct->c_descr = add("\n", ct->c_descr); @@ -1973,11 +1921,10 @@ setup_attach_content(CT ct, char *filename) if (strcasecmp(ct->c_ctinfo.ci_type, "text") == 0 && strcasecmp(ct->c_ctinfo.ci_subtype, "calendar") == 0) { - ct->c_dispo = getcpy("inline; filename=\""); + ct->c_dispo_type = getcpy("inline"); } else { - ct->c_dispo = getcpy("attachment; filename=\""); + ct->c_dispo_type = getcpy("attachment"); } - ct->c_dispo = add(simplename, ct->c_dispo); - ct->c_dispo = add("\"\n", ct->c_dispo); + add_param(&ct->c_dispo_first, &ct->c_dispo_last, "filename", simplename, 0); } diff --git a/uip/mhfixmsg.c b/uip/mhfixmsg.c index 47c03051..f00fc97d 100644 --- a/uip/mhfixmsg.c +++ b/uip/mhfixmsg.c @@ -85,7 +85,7 @@ int mhfixmsgsbr (CT *, const fix_transformations *, char *); static void reverse_alternative_parts (CT); static int fix_boundary (CT *, int *); static int get_multipart_boundary (CT, char **); -static int replace_boundary (CT, char *, const char *); +static int replace_boundary (CT, char *, char *); static int fix_multipart_cte (CT, int *); static int set_ce (CT, int); static int ensure_text_plain (CT *, CT, int *, int); @@ -622,7 +622,7 @@ get_multipart_boundary (CT ct, char **part_boundary) { /* Open and copy ct->c_file to file, replacing the multipart boundary. */ static int -replace_boundary (CT ct, char *file, const char *boundary) { +replace_boundary (CT ct, char *file, char *boundary) { FILE *fpin, *fpout; int compnum, state; char buf[BUFSIZ], name[NAMESZ]; @@ -668,10 +668,22 @@ replace_boundary (CT ct, char *file, const char *boundary) { if (strcasecmp (TYPE_FIELD, np)) { fprintf (fpout, "%s:%s", np, vp); } else { - char *new_boundary = update_attr (vp, "boundary=", boundary); - - fprintf (fpout, "%s:%s\n", np, new_boundary); - free (new_boundary); + char *new_ctline, *new_params; + + replace_param(&ct->c_ctinfo.ci_first_pm, + &ct->c_ctinfo.ci_last_pm, "boundary", + boundary, 0); + + new_ctline = concat(" ", ct->c_ctinfo.ci_type, "/", + ct->c_ctinfo.ci_subtype, NULL); + new_params = output_params(strlen(TYPE_FIELD) + + strlen(new_ctline) + 1, + ct->c_ctinfo.ci_first_pm, NULL, 0); + fprintf (fpout, "%s:%s%s\n", np, new_ctline, + new_params ? new_params : ""); + free(new_ctline); + if (new_params) + free(new_params); } free (vp); @@ -1028,19 +1040,19 @@ divide_part (CT ct) { static void copy_ctinfo (CI dest, CI src) { - char **s_ap, **d_ap, **s_vp, **d_vp; + PM s_pm, d_pm; dest->ci_type = src->ci_type ? add (src->ci_type, NULL) : NULL; dest->ci_subtype = src->ci_subtype ? add (src->ci_subtype, NULL) : NULL; - for (s_ap = src->ci_attrs, d_ap = dest->ci_attrs, - s_vp = src->ci_values, d_vp = dest->ci_values; - *s_ap; - ++s_ap, ++d_ap, ++s_vp, ++d_vp) { - *d_ap = add (*s_ap, NULL); - *d_vp = *s_vp; + for (s_pm = src->ci_first_pm; s_pm; s_pm = s_pm->pm_next) { + d_pm = add_param(&dest->ci_first_pm, &dest->ci_last_pm, s_pm->pm_name, + s_pm->pm_value, 0); + if (s_pm->pm_charset) + d_pm->pm_charset = getcpy(s_pm->pm_charset); + if (s_pm->pm_lang) + d_pm->pm_lang = getcpy(s_pm->pm_lang); } - *d_ap = NULL; dest->ci_comment = src->ci_comment ? add (src->ci_comment, NULL) : NULL; dest->ci_magic = src->ci_magic ? add (src->ci_magic, NULL) : NULL; @@ -1169,7 +1181,6 @@ build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) { CT ct; struct part *p; struct multipart *m; - char *cp; const struct str2init *ctinit; if ((ct = (CT) calloc (1, sizeof *ct)) == NULL) @@ -1269,23 +1280,8 @@ build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) { ct->c_ctinfo.ci_subtype = add (subtypename, NULL); } - /* - * The boundary indicator string must be stored in a - * c_ctinfo.ci_values slot. We use the first slot because this is - * a new object. ci_attrs[0] can be anything, it is never output. - * It is chosen to be very unlikely to collide with a real - * attribute, to contains text that ci_values[0] can point to, and - * gives traceability back to the origin of this part, though that - * would only be useful in a debugger. - */ - name = concat (" ", typename, "/", subtypename, boundary_indicator, - boundary, NULL); - if ((cp = strstr (name, boundary_indicator))) { - ct->c_ctinfo.ci_attrs[0] = name; - ct->c_ctinfo.ci_attrs[1] = NULL; - /* ci_values don't get free'd, so point into ci_attrs. */ - ct->c_ctinfo.ci_values[0] = cp + strlen (boundary_indicator); - } + add_param(&ct->c_ctinfo.ci_first_pm, &ct->c_ctinfo.ci_last_pm, + "boundary", boundary, 0); p = (struct part *) mh_xmalloc (sizeof *p); p->mp_next = (struct part *) mh_xmalloc (sizeof *p->mp_next); diff --git a/uip/mhfree.c b/uip/mhfree.c index 29756876..f2726d0f 100644 --- a/uip/mhfree.c +++ b/uip/mhfree.c @@ -30,6 +30,7 @@ static void free_text (CT); static void free_multi (CT); static void free_partial (CT); static void free_external (CT); +static void free_pmlist (PM); /* @@ -103,6 +104,9 @@ free_content (CT ct) free (ct->c_descr); if (ct->c_dispo) free (ct->c_dispo); + if (ct->c_dispo_type) + free (ct->c_dispo_type); + free_pmlist (ct->c_dispo_first); if (ct->c_file) { if (ct->c_unlink) @@ -150,7 +154,6 @@ free_header (CT ct) void free_ctinfo (CT ct) { - char **ap; CI ci; ci = &ct->c_ctinfo; @@ -162,10 +165,7 @@ free_ctinfo (CT ct) free (ci->ci_subtype); ci->ci_subtype = NULL; } - for (ap = ci->ci_attrs; *ap; ap++) { - free (*ap); - *ap = NULL; - } + free_pmlist(ci->ci_first_pm); if (ci->ci_comment) { free (ci->ci_comment); ci->ci_comment = NULL; @@ -253,6 +253,27 @@ free_external (CT ct) } +static void +free_pmlist (PM pm) +{ + PM pm2; + + while (pm != NULL) { + if (pm->pm_name) + free (pm->pm_name); + if (pm->pm_value) + free (pm->pm_value); + if (pm->pm_charset) + free (pm->pm_charset); + if (pm->pm_lang) + free (pm->pm_lang); + pm2 = pm->pm_next; + free(pm); + pm = pm2; + } +} + + /* * Free data structures related to encoding/decoding * Content-Transfer-Encodings. diff --git a/uip/mhlist.c b/uip/mhlist.c index f37041c3..f9dbbd68 100644 --- a/uip/mhlist.c +++ b/uip/mhlist.c @@ -27,6 +27,8 @@ X("norealsize", 0, NSIZESW) \ X("verbose", 0, VERBSW) \ X("noverbose", 0, NVERBSW) \ + X("disposition", 0, DISPOSW) \ + X("nodisposition", 0, NDISPOSW) \ X("file file", 0, FILESW) \ X("part number", 0, PARTSW) \ X("type content", 0, TYPESW) \ @@ -67,7 +69,6 @@ extern int userrs; pid_t xpid = 0; int debugsw = 0; -int verbosw = 0; #define quitser pipeser @@ -79,9 +80,6 @@ int part_ok (CT, int); int type_ok (CT, int); void flush_errors (void); -/* mhlistsbr.c */ -void list_all_messages (CT *, int, int, int, int); - /* mhfree.c */ extern CT *cts; void freects_done (int) NORETURN; @@ -95,7 +93,7 @@ static void pipeser (int); int main (int argc, char **argv) { - int sizesw = 1, headsw = 1, chgflag = 1; + int sizesw = 1, headsw = 1, chgflag = 1, verbosw = 0, dispo = 0; int msgnum, *icachesw; char *cp, *file = NULL, *folder = NULL; char *maildir, buf[100], **argp; @@ -209,6 +207,12 @@ do_cache: case NVERBSW: verbosw = 0; continue; + case DISPOSW: + dispo = 1; + continue; + case NDISPOSW: + dispo = 0; + continue; case DEBUGSW: debugsw = 1; continue; @@ -319,7 +323,7 @@ do_cache: /* * List the message content */ - list_all_messages (cts, headsw, sizesw, verbosw, debugsw); + list_all_messages (cts, headsw, sizesw, verbosw, debugsw, dispo); /* Now free all the structures for the content */ for (ctp = cts; *ctp; ctp++) diff --git a/uip/mhlistsbr.c b/uip/mhlistsbr.c index 9380c3d7..d2d394fa 100644 --- a/uip/mhlistsbr.c +++ b/uip/mhlistsbr.c @@ -22,22 +22,15 @@ int part_ok (CT, int); int type_ok (CT, int); void flush_errors (void); -/* - * prototypes - */ -void list_all_messages (CT *, int, int, int, int); -int list_switch (CT, int, int, int, int); -int list_content (CT, int, int, int, int); - /* * static prototypes */ -static void list_single_message (CT, int, int, int); +static void list_single_message (CT, int, int, int, int); static int list_debug (CT); -static int list_multi (CT, int, int, int, int); -static int list_partial (CT, int, int, int, int); -static int list_external (CT, int, int, int, int); -static int list_application (CT, int, int, int, int); +static int list_multi (CT, int, int, int, int, int); +static int list_partial (CT, int, int, int, int, int); +static int list_external (CT, int, int, int, int, int); +static int list_application (CT, int, int, int, int, int); static int list_encoding (CT); @@ -60,7 +53,8 @@ static int list_encoding (CT); */ void -list_all_messages (CT *cts, int headers, int realsize, int verbose, int debug) +list_all_messages (CT *cts, int headers, int realsize, int verbose, int debug, + int dispo) { CT ct, *ctp; @@ -69,7 +63,7 @@ list_all_messages (CT *cts, int headers, int realsize, int verbose, int debug) for (ctp = cts; *ctp; ctp++) { ct = *ctp; - list_single_message (ct, realsize, verbose, debug); + list_single_message (ct, realsize, verbose, debug, dispo); } flush_errors (); @@ -81,11 +75,11 @@ list_all_messages (CT *cts, int headers, int realsize, int verbose, int debug) */ static void -list_single_message (CT ct, int realsize, int verbose, int debug) +list_single_message (CT ct, int realsize, int verbose, int debug, int dispo) { if (type_ok (ct, 1)) { umask (ct->c_umask); - list_switch (ct, 1, realsize, verbose, debug); + list_switch (ct, 1, realsize, verbose, debug, dispo); if (ct->c_fp) { fclose (ct->c_fp); ct->c_fp = NULL; @@ -101,33 +95,38 @@ list_single_message (CT ct, int realsize, int verbose, int debug) */ int -list_switch (CT ct, int toplevel, int realsize, int verbose, int debug) +list_switch (CT ct, int toplevel, int realsize, int verbose, int debug, + int dispo) { switch (ct->c_type) { case CT_MULTIPART: - return list_multi (ct, toplevel, realsize, verbose, debug); + return list_multi (ct, toplevel, realsize, verbose, debug, dispo); case CT_MESSAGE: switch (ct->c_subtype) { case MESSAGE_PARTIAL: - return list_partial (ct, toplevel, realsize, verbose, debug); + return list_partial (ct, toplevel, realsize, verbose, + debug, dispo); case MESSAGE_EXTERNAL: - return list_external (ct, toplevel, realsize, verbose, debug); + return list_external (ct, toplevel, realsize, verbose, + debug, dispo); case MESSAGE_RFC822: default: - return list_content (ct, toplevel, realsize, verbose, debug); + return list_content (ct, toplevel, realsize, verbose, + debug, dispo); } case CT_TEXT: case CT_AUDIO: case CT_IMAGE: case CT_VIDEO: - return list_content (ct, toplevel, realsize, verbose, debug); + return list_content (ct, toplevel, realsize, verbose, debug, dispo); case CT_APPLICATION: - return list_application (ct, toplevel, realsize, verbose, debug); + return list_application (ct, toplevel, realsize, verbose, debug, + dispo); default: /* list_debug (ct); */ @@ -145,11 +144,13 @@ list_switch (CT ct, int toplevel, int realsize, int verbose, int debug) */ int -list_content (CT ct, int toplevel, int realsize, int verbose, int debug) +list_content (CT ct, int toplevel, int realsize, int verbose, int debug, + int dispo) { unsigned long size; char *cp, buffer[BUFSIZ]; CI ci = &ct->c_ctinfo; + PM pm; if (toplevel > 0) printf (LSTFMT2a, atoi (r1bindex (empty (ct->c_file), '/'))); @@ -199,11 +200,12 @@ list_content (CT ct, int toplevel, int realsize, int verbose, int debug) printf ("\n"); if (verbose) { - char **ap, **ep; CI ci = &ct->c_ctinfo; - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) - printf ("\t %s=\"%s\"\n", *ap, *ep); + for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) { + printf ("\t %s=\"%s\"\n", pm->pm_name, + get_param_value(pm, '?')); + } /* * If verbose, print any RFC-822 comments in the @@ -219,6 +221,17 @@ list_content (CT ct, int toplevel, int realsize, int verbose, int debug) } } + if (dispo && ct->c_dispo_type) { + printf ("\t disposition \"%s\"\n", ct->c_dispo_type); + + if (verbose) { + for (pm = ct->c_dispo_first; pm; pm = pm->pm_next) { + printf ("\t %s=\"%s\"\n", pm->pm_name, + get_param_value(pm, '?')); + } + } + } + if (debug) list_debug (ct); @@ -233,8 +246,8 @@ list_content (CT ct, int toplevel, int realsize, int verbose, int debug) static int list_debug (CT ct) { - char **ap, **ep; CI ci = &ct->c_ctinfo; + PM pm; fflush (stdout); fprintf (stderr, " partno \"%s\"\n", empty (ct->c_partno)); @@ -255,8 +268,9 @@ list_debug (CT ct) /* print parsed parameters attached to content type */ fprintf (stderr, " parameters\n"); - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) - fprintf (stderr, " %s=\"%s\"\n", *ap, *ep); + for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) + fprintf (stderr, " %s=\"%s\"\n", pm->pm_name, + get_param_value(pm, '?')); /* print internal flags for type/subtype */ fprintf (stderr, " type 0x%x subtype 0x%x params 0x%x\n", @@ -283,6 +297,16 @@ list_debug (CT ct) if (ct->c_descr) fprintf (stderr, " %s:%s", DESCR_FIELD, ct->c_descr); + /* print Content-Disposition */ + if (ct->c_dispo) + fprintf (stderr, " %s:%s", DISPO_FIELD, ct->c_dispo); + + fprintf(stderr, " disposition \"%s\"\n", empty (ct->c_dispo_type)); + fprintf(stderr, " disposition parameters\n"); + for (pm = ct->c_dispo_first; pm; pm = pm->pm_next) + fprintf (stderr, " %s=\"%s\"\n", pm->pm_name, + get_param_value(pm, '?')); + fprintf (stderr, " read fp 0x%x file \"%s\" begin %ld end %ld\n", (unsigned int)(unsigned long) ct->c_fp, empty (ct->c_file), ct->c_begin, ct->c_end); @@ -301,20 +325,21 @@ list_debug (CT ct) */ static int -list_multi (CT ct, int toplevel, int realsize, int verbose, int debug) +list_multi (CT ct, int toplevel, int realsize, int verbose, int debug, + int dispo) { struct multipart *m = (struct multipart *) ct->c_ctparams; struct part *part; /* list the content for toplevel of this multipart */ - list_content (ct, toplevel, realsize, verbose, debug); + list_content (ct, toplevel, realsize, verbose, debug, dispo); /* now list for all the subparts */ for (part = m->mp_parts; part; part = part->mp_next) { CT p = part->mp_part; if (part_ok (p, 1) && type_ok (p, 1)) - list_switch (p, 0, realsize, verbose, debug); + list_switch (p, 0, realsize, verbose, debug, dispo); } return OK; @@ -326,11 +351,12 @@ list_multi (CT ct, int toplevel, int realsize, int verbose, int debug) */ static int -list_partial (CT ct, int toplevel, int realsize, int verbose, int debug) +list_partial (CT ct, int toplevel, int realsize, int verbose, int debug, + int dispo) { struct partial *p = (struct partial *) ct->c_ctparams; - list_content (ct, toplevel, realsize, verbose, debug); + list_content (ct, toplevel, realsize, verbose, debug, dispo); if (verbose) { printf ("\t [message %s, part %d", p->pm_partid, p->pm_partno); if (p->pm_maxno) @@ -347,7 +373,8 @@ list_partial (CT ct, int toplevel, int realsize, int verbose, int debug) */ static int -list_external (CT ct, int toplevel, int realsize, int verbose, int debug) +list_external (CT ct, int toplevel, int realsize, int verbose, int debug, + int dispo) { struct exbody *e = (struct exbody *) ct->c_ctparams; @@ -355,7 +382,7 @@ list_external (CT ct, int toplevel, int realsize, int verbose, int debug) * First list the information for the * message/external content itself. */ - list_content (ct, toplevel, realsize, verbose, debug); + list_content (ct, toplevel, realsize, verbose, debug, dispo); if (verbose) { if (e->eb_name) @@ -368,6 +395,8 @@ list_external (CT ct, int toplevel, int realsize, int verbose, int debug) printf ("\t server=\"%s\"\n", e->eb_server); if (e->eb_subject) printf ("\t subject=\"%s\"\n", e->eb_subject); + if (e->eb_url) + printf ("\t url=\"%s\"\n", e->eb_url); /* This must be defined */ printf ("\t access-type=\"%s\"\n", e->eb_access); @@ -379,13 +408,14 @@ list_external (CT ct, int toplevel, int realsize, int verbose, int debug) if (e->eb_flags == NOTOK) printf ("\t [service unavailable]\n"); + } /* * Now list the information for the external content * to which this content points. */ - list_content (e->eb_content, 0, realsize, verbose, debug); + list_content (e->eb_content, 0, realsize, verbose, debug, dispo); return OK; } @@ -399,9 +429,10 @@ list_external (CT ct, int toplevel, int realsize, int verbose, int debug) */ static int -list_application (CT ct, int toplevel, int realsize, int verbose, int debug) +list_application (CT ct, int toplevel, int realsize, int verbose, int debug, + int dispo) { - list_content (ct, toplevel, realsize, verbose, debug); + list_content (ct, toplevel, realsize, verbose, debug, dispo); return OK; } diff --git a/uip/mhn.c b/uip/mhn.c index 8cd3acdd..c0b7e781 100644 --- a/uip/mhn.c +++ b/uip/mhn.c @@ -128,9 +128,6 @@ void flush_errors (void); /* mhshowsbr.c */ void show_all_messages (CT *); -/* mhlistsbr.c */ -void list_all_messages (CT *, int, int, int, int); - /* mhstoresbr.c */ typedef struct mhstoreinfo *mhstoreinfo_t; mhstoreinfo_t mhstoreinfo_create(CT *, char *, const char *, int, int); @@ -557,7 +554,7 @@ do_cache: * List the message content */ if (listsw) - list_all_messages (cts, headsw, sizesw, verbosw, debugsw); + list_all_messages (cts, headsw, sizesw, verbosw, debugsw, 0); /* * Store the message content diff --git a/uip/mhoutsbr.c b/uip/mhoutsbr.c index 88f610dd..9f024e08 100644 --- a/uip/mhoutsbr.c +++ b/uip/mhoutsbr.c @@ -81,14 +81,10 @@ output_content (CT ct, FILE *out) { int result = 0; CI ci = &ct->c_ctinfo; - char *boundary = ci->ci_values[0], **ap, **vp; + char *boundary = "", *cp; - for (ap = ci->ci_attrs, vp = ci->ci_values; *ap; ++ap, ++vp) { - if (! strcasecmp ("boundary", *ap)) { - boundary = *vp; - break; - } - } + if ((cp = get_param(ci->ci_first_pm, "boundary", '-', 0))) + boundary = cp; /* * Output all header fields for this content @@ -100,8 +96,11 @@ output_content (CT ct, FILE *out) * "message/external", then we are done with the * headers (since it has no body). */ - if (ct->c_ctexbody) + if (ct->c_ctexbody) { + if (boundary && *boundary != '\0') + free(boundary); return OK; + } /* * Now output the content bodies. @@ -125,8 +124,11 @@ output_content (CT ct, FILE *out) CT p = part->mp_part; fprintf (out, "\n--%s\n", boundary); - if (output_content (p, out) == NOTOK) + if (output_content (p, out) == NOTOK) { + if (boundary && *boundary != '\0') + free(boundary); return NOTOK; + } } fprintf (out, "\n--%s--\n", boundary); @@ -163,7 +165,7 @@ output_content (CT ct, FILE *out) body, don't emit the newline that would appear between the headers and body. In that case, the call to write8Bit() shouldn't be needed, but is harmless. */ - if (ct->c_ctinfo.ci_attrs[0] != NULL || + if (ct->c_ctinfo.ci_first_pm != NULL || ct->c_begin != ct->c_end) { putc ('\n', out); } @@ -198,6 +200,9 @@ output_content (CT ct, FILE *out) break; } + if (boundary && *boundary != '\0') + free(boundary); + return result; } @@ -226,7 +231,7 @@ output_headers (CT ct, FILE *out) static int writeExternalBody (CT ct, FILE *out) { - char **ap, **ep, *cp; + char *cp; struct exbody *e = (struct exbody *) ct->c_ctparams; putc ('\n', out); @@ -246,17 +251,22 @@ writeExternalBody (CT ct, FILE *out) continue; case 'N': - for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++) - if (!strcasecmp (*ap, "name")) { - fprintf (out, "%s", *ep); - break; - } + cp = get_param(ci2->ci_first_pm, "name", '_', 0); + if (cp) { + fputs (cp, out); + free (cp); + } continue; case 'T': fprintf (out, "%s/%s", ci2->ci_type, ci2->ci_subtype); - for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++) - fprintf (out, "; %s=\"%s\"", *ap, *ep); + cp = output_params(strlen(ci2->ci_type) + + strlen(ci2->ci_subtype) + 1, + ci2->ci_first_pm, NULL, 0); + if (cp) { + fputs (cp, out); + free (cp); + } continue; case 'n': diff --git a/uip/mhparse.c b/uip/mhparse.c index 9873e0b1..ec7db7a4 100644 --- a/uip/mhparse.c +++ b/uip/mhparse.c @@ -16,6 +16,9 @@ #include #include #include +#ifdef HAVE_ICONV +# include +#endif /* HAVE_ICONV */ extern int debugsw; @@ -109,7 +112,7 @@ void free_encoding (CT, int); * static prototypes */ static CT get_content (FILE *, char *, int); -static int get_comment (const char *, CI, char **, int); +static int get_comment (const char *, const char *, char **, char **); static int InitGeneric (CT); static int InitText (CT); @@ -135,6 +138,12 @@ static int readDigest (CT, char *); static int get_leftover_mp_content (CT, int); static int InitURL (CT); static int openURL (CT, char **); +static int parse_header_attrs (const char *, const char *, char **, PM *, + PM *, char **); +static size_t param_len(PM, int, size_t, int *, int *, size_t *); +static size_t encode_param(PM, char *, size_t, size_t, size_t, int); +static size_t normal_param(PM, char *, size_t, size_t, size_t); +static int get_dispo (char *, CT, int); struct str2init str2cts[] = { { "application", CT_APPLICATION, InitApplication }, @@ -377,7 +386,7 @@ get_content (FILE *in, char *file, int toplevel) fprintf (stderr, "%s: %s\n", VRSN_FIELD, cp); if (*cp == '(' && - get_comment (ct->c_file, &ct->c_ctinfo, &cp, 0) == NOTOK) + get_comment (ct->c_file, VRSN_FIELD, &cp, NULL) == NOTOK) goto out; for (dp = cp; istoken (*dp); dp++) @@ -487,7 +496,7 @@ get_content (FILE *in, char *file, int toplevel) fprintf (stderr, "%s: %s\n", MD5_FIELD, cp); if (*cp == '(' && - get_comment (ct->c_file, &ct->c_ctinfo, &cp, 0) == NOTOK) { + get_comment (ct->c_file, MD5_FIELD, &cp, NULL) == NOTOK) { free (ep); goto out; } @@ -510,7 +519,8 @@ get_content (FILE *in, char *file, int toplevel) } else if (!strcasecmp (hp->name, DISPO_FIELD)) { /* Get Content-Disposition field */ - ct->c_dispo = add (hp->value, ct->c_dispo); + if (get_dispo(hp->value, ct, 0) == NOTOK) + goto out; } next_header: @@ -587,88 +597,6 @@ add_header (CT ct, char *name, char *value) } -/* Make sure that buf contains at least one appearance of name, - followed by =. If not, insert both name and value, just after - first semicolon, if any. Note that name should not contain a - trailing =. And quotes will be added around the value. Typical - usage: make sure that a Content-Disposition header contains - filename="foo". If it doesn't and value does, use value from - that. */ -static char * -incl_name_value (char *buf, char *name, char *value) { - char *newbuf = buf; - - /* Assume that name is non-null. */ - if (buf && value) { - char *name_plus_equal = concat (name, "=", NULL); - - if (! strstr (buf, name_plus_equal)) { - char *insertion; - char *cp, *prefix, *suffix; - - /* Trim trailing space, esp. newline. */ - for (cp = &buf[strlen (buf) - 1]; - cp >= buf && isspace ((unsigned char) *cp); - --cp) { - *cp = '\0'; - } - - insertion = concat ("; ", name, "=", "\"", value, "\"", NULL); - - /* Insert at first semicolon, if any. If none, append to - end. */ - prefix = add (buf, NULL); - if ((cp = strchr (prefix, ';'))) { - suffix = concat (cp, NULL); - *cp = '\0'; - newbuf = concat (prefix, insertion, suffix, "\n", NULL); - free (suffix); - } else { - /* Append to end. */ - newbuf = concat (buf, insertion, "\n", NULL); - } - - free (prefix); - free (insertion); - free (buf); - } - - free (name_plus_equal); - } - - return newbuf; -} - -/* Extract just name_suffix="foo", if any, from value. If there isn't - one, return the entire value. Note that, for example, a name_suffix - of name will match filename="foo", and return foo. */ -static char * -extract_name_value (char *name_suffix, char *value) { - char *extracted_name_value = value; - char *name_suffix_plus_quote = concat (name_suffix, "=\"", NULL); - char *name_suffix_equals = strstr (value, name_suffix_plus_quote); - char *cp; - - free (name_suffix_plus_quote); - if (name_suffix_equals) { - char *name_suffix_begin; - - /* Find first \". */ - for (cp = name_suffix_equals; *cp != '"'; ++cp) /* empty */; - name_suffix_begin = ++cp; - /* Find second \". */ - for (; *cp != '"'; ++cp) /* empty */; - - extracted_name_value = mh_xmalloc (cp - name_suffix_begin + 1); - memcpy (extracted_name_value, - name_suffix_begin, - cp - name_suffix_begin); - extracted_name_value[cp - name_suffix_begin] = '\0'; - } - - return extracted_name_value; -} - /* * Parse Content-Type line and (if `magic' is non-zero) mhbuild composition * directives. Fills in the information of the CTinfo structure. @@ -676,14 +604,12 @@ extract_name_value (char *name_suffix, char *value) { int get_ctinfo (char *cp, CT ct, int magic) { - int i; char *dp; char c; CI ci; int status; ci = &ct->c_ctinfo; - i = strlen (invo_name) + 2; /* store copy of Content-Type line */ cp = ct->c_ctline = add (cp, NULL); @@ -704,7 +630,8 @@ get_ctinfo (char *cp, CT ct, int magic) if (debugsw) fprintf (stderr, "%s: %s\n", TYPE_FIELD, cp); - if (*cp == '(' && get_comment (ct->c_file, &ct->c_ctinfo, &cp, 1) == NOTOK) + if (*cp == '(' && get_comment (ct->c_file, TYPE_FIELD, &cp, + &ci->ci_comment) == NOTOK) return NOTOK; for (dp = cp; istoken (*dp); dp++) @@ -727,7 +654,8 @@ get_ctinfo (char *cp, CT ct, int magic) while (isspace ((unsigned char) *cp)) cp++; - if (*cp == '(' && get_comment (ct->c_file, &ct->c_ctinfo, &cp, 1) == NOTOK) + if (*cp == '(' && get_comment (ct->c_file, TYPE_FIELD, &cp, + &ci->ci_comment) == NOTOK) return NOTOK; if (*cp != '/') { @@ -740,7 +668,8 @@ get_ctinfo (char *cp, CT ct, int magic) while (isspace ((unsigned char) *cp)) cp++; - if (*cp == '(' && get_comment (ct->c_file, &ct->c_ctinfo, &cp, 1) == NOTOK) + if (*cp == '(' && get_comment (ct->c_file, TYPE_FIELD, &cp, + &ci->ci_comment) == NOTOK) return NOTOK; for (dp = cp; istoken (*dp); dp++) @@ -765,11 +694,14 @@ magic_skip: while (isspace ((unsigned char) *cp)) cp++; - if (*cp == '(' && get_comment (ct->c_file, &ct->c_ctinfo, &cp, 1) == NOTOK) + if (*cp == '(' && get_comment (ct->c_file, TYPE_FIELD, &cp, + &ci->ci_comment) == NOTOK) return NOTOK; - if (parse_header_attrs (ct->c_file, i, &cp, ci, &status) == NOTOK) { - return status; + if ((status = parse_header_attrs (ct->c_file, TYPE_FIELD, &cp, + &ci->ci_first_pm, &ci->ci_last_pm, + &ci->ci_comment)) != OK) { + return status == NOTOK ? NOTOK : OK; } /* @@ -828,7 +760,7 @@ magic_skip: * Get any {Content-Disposition} given in buffer. */ if (magic && *cp == '{') { - ct->c_dispo = ++cp; + ++cp; for (dp = cp + strlen (cp) - 1; dp >= cp; dp--) if (*dp == '}') break; @@ -840,10 +772,10 @@ magic_skip: c = *dp; *dp = '\0'; - if (*ct->c_dispo) - ct->c_dispo = concat (ct->c_dispo, "\n", NULL); - else - ct->c_dispo = NULL; + + if (get_dispo(cp, ct, 1) != OK) + return NOTOK; + *dp++ = c; cp = dp; @@ -903,27 +835,98 @@ magic_skip: have a *filename=, extract it from the magic contents. The r1bindex call skips any leading directory components. */ - if (ct->c_dispo) - ct->c_dispo = - incl_name_value (ct->c_dispo, - "filename", - r1bindex (extract_name_value ("name", - ci-> - ci_magic), - '/')); + if (ct->c_dispo_type && + !get_param(ct->c_dispo_first, "filename", '_', 1)) { + add_param(&ct->c_dispo_first, &ct->c_dispo_last, "filename", + r1bindex(ci->ci_magic, '/'), 0); + } } else advise (NULL, - "extraneous information in message %s's %s: field\n%*.*s(%s)", - ct->c_file, TYPE_FIELD, i, i, "", cp); + "extraneous information in message %s's %s: field\n%*s(%s)", + ct->c_file, TYPE_FIELD, strlen(invo_name) + 2, "", cp); + } + + return OK; +} + + +/* + * Parse out a Content-Disposition header. A lot of this is cribbed from + * get_ctinfo(). + */ +static int +get_dispo (char *cp, CT ct, int buildflag) +{ + char *dp, *dispoheader; + char c; + int status; + + /* + * Save the whole copy of the Content-Disposition header, unless we're + * processing a mhbuild directive. A NULL c_dispo will be a flag to + * mhbuild that the disposition header needs to be generated at that + * time. + */ + + dispoheader = cp = add(cp, NULL); + + while (isspace ((unsigned char) *cp)) /* trim leading spaces */ + cp++; + + /* change newlines to spaces */ + for (dp = strchr(cp, '\n'); dp; dp = strchr(dp, '\n')) + *dp++ = ' '; + + /* trim trailing spaces */ + for (dp = cp + strlen (cp) - 1; dp >= cp; dp--) + if (!isspace ((unsigned char) *dp)) + break; + *++dp = '\0'; + + if (debugsw) + fprintf (stderr, "%s: %s\n", DISPO_FIELD, cp); + + if (*cp == '(' && get_comment (ct->c_file, DISPO_FIELD, &cp, NULL) == + NOTOK) { + free(dispoheader); + return NOTOK; + } + + for (dp = cp; istoken (*dp); dp++) + continue; + c = *dp, *dp = '\0'; + ct->c_dispo_type = add (cp, NULL); /* store disposition type */ + *dp = c, cp = dp; + + if (*cp == '(' && get_comment (ct->c_file, DISPO_FIELD, &cp, NULL) == NOTOK) + return NOTOK; + + if ((status = parse_header_attrs (ct->c_file, DISPO_FIELD, &cp, + &ct->c_dispo_first, &ct->c_dispo_last, + NULL)) != OK) { + if (status == NOTOK) { + free(dispoheader); + return NOTOK; + } + } else if (*cp) { + advise (NULL, + "extraneous information in message %s's %s: field\n%*s(%s)", + ct->c_file, DISPO_FIELD, strlen(invo_name) + 2, "", cp); } + if (buildflag) + free(dispoheader); + else + ct->c_dispo = dispoheader; + return OK; } static int -get_comment (const char *filename, CI ci, char **ap, int istype) +get_comment (const char *filename, const char *fieldname, char **ap, + char **commentp) { int i; char *bp, *cp; @@ -938,7 +941,7 @@ get_comment (const char *filename, CI ci, char **ap, int istype) case '\0': invalid: advise (NULL, "invalid comment in message %s's %s: field", - filename, istype ? TYPE_FIELD : VRSN_FIELD); + filename, fieldname); return NOTOK; case '\\': @@ -965,12 +968,12 @@ invalid: } *bp = '\0'; - if (istype) { - if ((dp = ci->ci_comment)) { - ci->ci_comment = concat (dp, " ", buffer, NULL); + if (commentp) { + if ((dp = *commentp)) { + *commentp = concat (dp, " ", buffer, NULL); free (dp); } else { - ci->ci_comment = add (buffer, NULL); + *commentp = add (buffer, NULL); } } @@ -1007,7 +1010,8 @@ InitText (CT ct) { char buffer[BUFSIZ]; char *chset = NULL; - char **ap, **ep, *cp; + char *cp; + PM pm; struct k2v *kv; struct text *t; CI ci = &ct->c_ctinfo; @@ -1028,13 +1032,13 @@ InitText (CT ct) ct->c_ctparams = (void *) t; /* scan for charset parameter */ - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) - if (!strcasecmp (*ap, "charset")) + for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) + if (!strcasecmp (pm->pm_name, "charset")) break; /* check if content specified a character set */ - if (*ap) { - chset = *ep; + if (pm) { + chset = pm->pm_value; t->tx_charset = CHARSET_SPECIFIED; } else { t->tx_charset = CHARSET_UNSPECIFIED; @@ -1066,7 +1070,8 @@ InitMultiPart (CT ct) { int inout; long last, pos; - char *cp, *dp, **ap, **ep; + char *cp, *dp; + PM pm; char *bp, buffer[BUFSIZ]; struct multipart *m; struct k2v *kv; @@ -1111,15 +1116,15 @@ InitMultiPart (CT ct) * required for multipart messages. */ bp = 0; - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) { - if (!strcasecmp (*ap, "boundary")) { - bp = *ep; + for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) { + if (!strcasecmp (pm->pm_name, "boundary")) { + bp = pm->pm_value; break; } } /* complain if boundary parameter is missing */ - if (!*ap) { + if (!pm) { advise (NULL, "a \"boundary\" parameter is mandatory for \"%s/%s\" type in message %s's %s: field", ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD); @@ -1324,7 +1329,7 @@ InitMessage (CT ct) case MESSAGE_PARTIAL: { - char **ap, **ep; + PM pm; struct partial *p; if ((p = (struct partial *) calloc (1, sizeof(*p))) == NULL) @@ -1332,25 +1337,25 @@ InitMessage (CT ct) ct->c_ctparams = (void *) p; /* scan for parameters "id", "number", and "total" */ - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) { - if (!strcasecmp (*ap, "id")) { - p->pm_partid = add (*ep, NULL); + for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) { + if (!strcasecmp (pm->pm_name, "id")) { + p->pm_partid = add (pm->pm_value, NULL); continue; } - if (!strcasecmp (*ap, "number")) { - if (sscanf (*ep, "%d", &p->pm_partno) != 1 + if (!strcasecmp (pm->pm_name, "number")) { + if (sscanf (pm->pm_value, "%d", &p->pm_partno) != 1 || p->pm_partno < 1) { invalid_param: advise (NULL, "invalid %s parameter for \"%s/%s\" type in message %s's %s field", - *ap, ci->ci_type, ci->ci_subtype, + pm->pm_name, ci->ci_type, ci->ci_subtype, ct->c_file, TYPE_FIELD); return NOTOK; } continue; } - if (!strcasecmp (*ap, "total")) { - if (sscanf (*ep, "%d", &p->pm_maxno) != 1 + if (!strcasecmp (pm->pm_name, "total")) { + if (sscanf (pm->pm_value, "%d", &p->pm_maxno) != 1 || p->pm_maxno < 1) goto invalid_param; continue; @@ -1465,21 +1470,21 @@ no_body: int params_external (CT ct, int composing) { - char **ap, **ep; + PM pm; struct exbody *e = (struct exbody *) ct->c_ctparams; CI ci = &ct->c_ctinfo; ct->c_ceopenfnx = NULL; - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) { - if (!strcasecmp (*ap, "access-type")) { + for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) { + if (!strcasecmp (pm->pm_name, "access-type")) { struct str2init *s2i; CT p = e->eb_content; for (s2i = str2methods; s2i->si_key; s2i++) - if (!strcasecmp (*ep, s2i->si_key)) + if (!strcasecmp (pm->pm_value, s2i->si_key)) break; if (!s2i->si_key) { - e->eb_access = *ep; + e->eb_access = pm->pm_value; e->eb_flags = NOTOK; p->c_encoding = CE_EXTERNAL; continue; @@ -1493,46 +1498,46 @@ params_external (CT ct, int composing) return NOTOK; continue; } - if (!strcasecmp (*ap, "name")) { - e->eb_name = *ep; + if (!strcasecmp (pm->pm_name, "name")) { + e->eb_name = pm->pm_value; continue; } - if (!strcasecmp (*ap, "permission")) { - e->eb_permission = *ep; + if (!strcasecmp (pm->pm_name, "permission")) { + e->eb_permission = pm->pm_value; continue; } - if (!strcasecmp (*ap, "site")) { - e->eb_site = *ep; + if (!strcasecmp (pm->pm_name, "site")) { + e->eb_site = pm->pm_value; continue; } - if (!strcasecmp (*ap, "directory")) { - e->eb_dir = *ep; + if (!strcasecmp (pm->pm_name, "directory")) { + e->eb_dir = pm->pm_value; continue; } - if (!strcasecmp (*ap, "mode")) { - e->eb_mode = *ep; + if (!strcasecmp (pm->pm_name, "mode")) { + e->eb_mode = pm->pm_value; continue; } - if (!strcasecmp (*ap, "size")) { - sscanf (*ep, "%lu", &e->eb_size); + if (!strcasecmp (pm->pm_name, "size")) { + sscanf (pm->pm_value, "%lu", &e->eb_size); continue; } - if (!strcasecmp (*ap, "server")) { - e->eb_server = *ep; + if (!strcasecmp (pm->pm_name, "server")) { + e->eb_server = pm->pm_value; continue; } - if (!strcasecmp (*ap, "subject")) { - e->eb_subject = *ep; + if (!strcasecmp (pm->pm_name, "subject")) { + e->eb_subject = pm->pm_value; continue; } - if (!strcasecmp (*ap, "url")) { + if (!strcasecmp (pm->pm_name, "url")) { /* * According to RFC 2017, we have to remove all whitespace from * the URL */ - char *u, *p = *ep; - e->eb_url = u = mh_xmalloc(strlen(*ep) + 1); + char *u, *p = pm->pm_value; + e->eb_url = u = mh_xmalloc(strlen(pm->pm_value) + 1); for (; *p != '\0'; p++) { if (! isspace((unsigned char) *p)) @@ -1542,8 +1547,8 @@ params_external (CT ct, int composing) *u = '\0'; continue; } - if (composing && !strcasecmp (*ap, "body")) { - e->eb_body = getcpy (*ep); + if (composing && !strcasecmp (pm->pm_name, "body")) { + e->eb_body = getcpy (pm->pm_value); continue; } } @@ -2206,28 +2211,18 @@ open7Bit (CT ct, char **file) } if (ct->c_type == CT_MULTIPART) { - char **ap, **ep; CI ci = &ct->c_ctinfo; + char *buffer; len = 0; fprintf (ce->ce_fp, "%s: %s/%s", TYPE_FIELD, ci->ci_type, ci->ci_subtype); len += strlen (TYPE_FIELD) + 2 + strlen (ci->ci_type) + 1 + strlen (ci->ci_subtype); - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) { - putc (';', ce->ce_fp); - len++; - - snprintf (buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep); + buffer = output_params(len, ci->ci_first_pm, &len, 0); - if (len + 1 + (cc = strlen (buffer)) >= CPERLIN) { - fputs ("\n\t", ce->ce_fp); - len = 8; - } else { - putc (' ', ce->ce_fp); - len++; - } - fprintf (ce->ce_fp, "%s", buffer); - len += cc; + if (buffer) { + fputs (buffer, ce->ce_fp); + free(buffer); } if (ci->ci_comment) { @@ -3231,33 +3226,56 @@ get_ce_method (const char *method) { return NULL; } -int -parse_header_attrs (const char *filename, int len, char **header_attrp, CI ci, - int *status) { - char **attr = ci->ci_attrs; +/* + * Parse a series of MIME attributes (or parameters) given a header as + * input. + * + * Arguments include: + * + * filename - Name of input file (for error messages) + * fieldname - Name of field being processed + * headerp - Pointer to pointer of the beginning of the MIME attributes. + * Updated to point to end of attributes when finished. + * param_head - Pointer to head of parameter list + * param_tail - Pointer to tail of parameter list + * commentp - Pointer to header comment pointer (may be NULL) + * + * Returns OK if parsing was successful, NOTOK if parsing failed, and + * DONE to indicate a benign error (minor parsing error, but the program + * should continue). + */ + +static int +parse_header_attrs (const char *filename, const char *fieldname, + char **header_attrp, PM *param_head, PM *param_tail, + char **commentp) +{ char *cp = *header_attrp; + PM pm; + struct sectlist { + char *value; + int index; + int len; + struct sectlist *next; + } *sp, *sp2; + struct parmlist { + char *name; + char *charset; + char *lang; + struct sectlist *sechead; + struct parmlist *next; + } *pp, *pp2, *phead = NULL; while (*cp == ';') { - char *dp, *vp, *up, c; - - /* Relies on knowledge of this declaration: - * char *ci_attrs[NPARMS + 2]; - */ - if (attr >= ci->ci_attrs + sizeof ci->ci_attrs/sizeof (char *) - 2) { - advise (NULL, - "too many parameters in message %s's %s: field (%d max)", - filename, TYPE_FIELD, NPARMS); - *status = NOTOK; - return NOTOK; - } + char *dp, *vp, *up, *nameptr, *valptr, *charset = NULL, *lang = NULL; + int encoded = 0, partial = 0, len = 0, index = 0; cp++; while (isspace ((unsigned char) *cp)) cp++; if (*cp == '(' && - get_comment (filename, ci, &cp, 1) == NOTOK) { - *status = NOTOK; + get_comment (filename, fieldname, &cp, commentp) == NOTOK) { return NOTOK; } @@ -3265,9 +3283,8 @@ parse_header_attrs (const char *filename, int len, char **header_attrp, CI ci, advise (NULL, "extraneous trailing ';' in message %s's %s: " "parameter list", - filename, TYPE_FIELD); - *status = OK; - return NOTOK; + filename, fieldname); + return DONE; } /* down case the attribute name */ @@ -3280,135 +3297,956 @@ parse_header_attrs (const char *filename, int len, char **header_attrp, CI ci, if (dp == cp || *dp != '=') { advise (NULL, "invalid parameter in message %s's %s: " - "field\n%*.*sparameter %s (error detected at offset %d)", - filename, TYPE_FIELD, len, len, "", cp, dp - cp); - *status = NOTOK; + "field\n%*sparameter %s (error detected at offset %d)", + filename, fieldname, strlen(invo_name) + 2, "",cp, dp - cp); return NOTOK; } - vp = (*attr = add (cp, NULL)) + (up - cp); - *vp = '\0'; + /* + * To handle RFC 2231, we have to deal with the following extensions: + * + * name*=encoded-value + * name*=part-N-of-a-parameter-value + * name**=encoded-part-N-of-a-parameter-value + * + * So the rule is: + * If there's a * right before the equal sign, it's encoded. + * If there's a * and one or more digits, then it's section N. + * + * Remember we can have one or the other, or both. cp points to + * beginning of name, up points past the last character in the + * parameter name. + */ + + for (vp = cp; vp < up; vp++) { + if (*vp == '*' && vp < up - 1) { + partial = 1; + continue; + } else if (*vp == '*' && vp == up - 1) { + encoded = 1; + } else if (partial) { + if (isdigit((unsigned char) *vp)) + index = *vp - '0' + index * 10; + else { + advise (NULL, "invalid parameter index in message %s's " + "%s: field\n%*s(parameter %s)", filename, + fieldname, strlen(invo_name) + 2, "", cp); + return NOTOK; + } + } else { + len++; + } + } + + /* + * Break out the parameter name and value sections and allocate + * memory for each. + */ + + nameptr = mh_xmalloc(len + 1); + strncpy(nameptr, cp, len); + nameptr[len] = '\0'; + for (dp++; isspace ((unsigned char) *dp);) dp++; - /* Now store the attribute value. */ - ci->ci_values[attr - ci->ci_attrs] = vp = *attr + (dp - cp); + if (encoded) { + /* + * Single quotes delimit the character set and language tag. + * They are required on the first section (or a complete + * parameter). + */ + if (index == 0) { + vp = dp; + while (*vp != '\'' && !isspace((unsigned char) *vp) && + *vp != '\0') + vp++; + if (*vp == '\'') { + if (vp != dp) { + len = vp - dp; + charset = mh_xmalloc(len + 1); + strncpy(charset, dp, len); + charset[len] = '\0'; + } else { + charset = NULL; + } + vp++; + } else { + advise(NULL, "missing charset in message %s's %s: " + "field\n%*s(parameter %s)", filename, fieldname, + strlen(invo_name) + 2, "", nameptr); + free(nameptr); + return NOTOK; + } + dp = vp; + + while (*vp != '\'' && !isspace((unsigned char) *vp) && + *vp != '\0') + vp++; + + if (*vp == '\'') { + if (vp != dp) { + len = vp - dp; + lang = mh_xmalloc(len + 1); + strncpy(lang, dp, len); + lang[len] = '\0'; + } else { + lang = NULL; + } + vp++; + } else { + advise(NULL, "missing language tag in message %s's %s: " + "field\n%*s(parameter %s)", filename, fieldname, + strlen(invo_name) + 2, "", nameptr); + free(nameptr); + if (charset) + free(charset); + return NOTOK; + } + + dp = vp; + } + + /* + * At this point vp should be pointing at the beginning + * of the encoded value/section. Continue until we reach + * the end or get whitespace. But first, calculate the + * length so we can allocate the correct buffer size. + */ + + for (vp = dp, len = 0; istoken(*vp); vp++) { + if (*vp == '%') { + if (*(vp + 1) == '\0' || + !isxdigit((unsigned char) *(vp + 1)) || + *(vp + 2) == '\0' || + !isxdigit((unsigned char) *(vp + 2))) { + advise(NULL, "invalid encoded sequence in message " + "%s's %s: field\n%*s(parameter %s)", + filename, fieldname, strlen(invo_name) + 2, + "", nameptr); + free(nameptr); + if (charset) + free(charset); + if (lang) + free(lang); + return NOTOK; + } + vp += 2; + } + len++; + } + + up = valptr = mh_xmalloc(len + 1); - if (*dp == '"') { - for (cp = ++dp, dp = vp;;) { - switch (c = *cp++) { + for (vp = dp; istoken(*vp); vp++) { + if (*vp == '%') { + *up++ = decode_qp(*(vp + 1), *(vp + 2)); + vp += 2; + } else { + *up++ = *vp; + } + } + + *up = '\0'; + cp = vp; + } else { + /* + * A "normal" string. If it's got a leading quote, then we + * strip the quotes out. Otherwise go until we reach the end + * or get whitespace. Note we scan it twice; once to get the + * length, then the second time copies it into the destination + * buffer. + */ + + len = 0; + + if (*dp == '"') { + for (cp = dp + 1;;) { + switch (*cp++) { case '\0': bad_quote: advise (NULL, "invalid quoted-string in message %s's %s: " - "field\n%*.*s(parameter %s)", - filename, TYPE_FIELD, len, len, "", *attr); - *status = NOTOK; + "field\n%*s(parameter %s)", + filename, fieldname, strlen(invo_name) + 2, "", + nameptr); + free(nameptr); + if (charset) + free(charset); + if (lang) + free(lang); return NOTOK; + case '"': + break; case '\\': - *dp++ = c; - if ((c = *cp++) == '\0') + if (*++cp == '\0') goto bad_quote; - /* else fall... */ - + /* FALL THROUGH */ default: - *dp++ = c; + len++; continue; + } + break; + } - case '"': - *dp = '\0'; + } else { + for (cp = dp; istoken (*cp); cp++) { + len++; + } + } + + valptr = mh_xmalloc(len + 1); + + if (*dp == '"') { + int i; + for (cp = dp + 1, vp = valptr, i = 0; i < len; i++) { + if (*cp == '\\') { + cp++; + } + *vp++ = *cp++; + } + cp++; + } else { + strncpy(valptr, cp = dp, len); + cp += len; + } + + valptr[len] = '\0'; + } + + /* + * If 'partial' is set, we don't allocate a parameter now. We + * put it on the parameter linked list to be reassembled later. + * + * "phead" points to a list of all parameters we need to reassemble. + * Each parameter has a list of sections. We insert the sections in + * order. + */ + + if (partial) { + for (pp = phead; pp != NULL; pp = pp->next) { + if (strcasecmp(nameptr, pp->name) == 0) + break; + } + + if (pp == NULL) { + pp = mh_xmalloc(sizeof(*pp)); + memset(pp, 0, sizeof(*pp)); + pp->name = nameptr; + pp->next = phead; + phead = pp; + } + + /* + * Insert this into the section linked list + */ + + sp = mh_xmalloc(sizeof(*sp)); + memset(sp, 0, sizeof(*sp)); + sp->value = valptr; + sp->index = index; + sp->len = len; + + if (pp->sechead == NULL || pp->sechead->index > index) { + sp->next = pp->sechead; + pp->sechead = sp; + } else { + for (sp2 = pp->sechead; sp2 != NULL; sp2 = sp2->next) { + if (sp2->index == sp->index) { + advise (NULL, "duplicate index (%d) in message " + "%s's %s: field\n%*s(parameter %s)", sp->index, + filename, fieldname, strlen(invo_name) + 2, "", + nameptr); + return NOTOK; + } + if (sp2->index < sp->index && + (sp2->next == NULL || sp2->next->index > sp->index)) { + sp->next = sp2->next; + sp2->next = sp; break; + } + } + + if (sp2 == NULL) { + advise(NULL, "Internal error: cannot insert partial " + "param in message %s's %s: field\n%*s(parameter %s)", + filename, fieldname, strlen(invo_name) + 2, "", + nameptr); + return NOTOK; } - break; + } + + /* + * Save our charset and lang tags. + */ + + if (index == 0 && encoded) { + if (pp->charset) + free(pp->charset); + pp->charset = charset; + if (pp->lang) + free(pp->lang); + pp->lang = lang; } } else { - for (cp = dp, dp = vp; istoken (*cp); cp++, dp++) - continue; - *dp = '\0'; - } - if (!*vp) { - advise (NULL, - "invalid parameter in message %s's %s: " - "field\n%*.*s(parameter %s)", - filename, TYPE_FIELD, len, len, "", *attr); - *status = NOTOK; - return NOTOK; + pm = add_param(param_head, param_tail, nameptr, valptr, 1); + pm->pm_charset = charset; + pm->pm_lang = lang; } while (isspace ((unsigned char) *cp)) cp++; if (*cp == '(' && - get_comment (filename, ci, &cp, 1) == NOTOK) { - *status = NOTOK; + get_comment (filename, fieldname, &cp, commentp) == NOTOK) { return NOTOK; } + } + + /* + * Now that we're done, reassemble all of the partial parameters. + */ - ++attr; + for (pp = phead; pp != NULL; ) { + char *p, *q; + size_t tlen = 0; + int pindex = 0; + for (sp = pp->sechead; sp != NULL; sp = sp->next) { + if (sp->index != pindex++) { + advise(NULL, "missing section %d for parameter in " + "message %s's %s: field\n%*s(parameter %s)", pindex - 1, + filename, fieldname, strlen(invo_name) + 2, "", + pp->name); + return NOTOK; + } + tlen += sp->len; + } + + p = q = mh_xmalloc(tlen + 1); + for (sp = pp->sechead; sp != NULL; ) { + memcpy(q, sp->value, sp->len); + q += sp->len; + free(sp->value); + sp2 = sp->next; + free(sp); + sp = sp2; + } + + p[tlen] = '\0'; + + pm = add_param(param_head, param_tail, pp->name, p, 1); + pm->pm_charset = pp->charset; + pm->pm_lang = pp->lang; + pp2 = pp->next; + free(pp); + pp = pp2; } *header_attrp = cp; return OK; } +/* + * Return the charset for a particular content type. Return pointer is + * only valid until the next call to content_charset(). + */ char * content_charset (CT ct) { - const char *const charset = "charset"; - char *default_charset = NULL; - CI ctinfo = &ct->c_ctinfo; - char **ap, **vp; - char **src_charset = NULL; - - for (ap = ctinfo->ci_attrs, vp = ctinfo->ci_values; *ap; ++ap, ++vp) { - if (! strcasecmp (*ap, charset)) { - src_charset = vp; - break; - } + static char *ret_charset = NULL; + + if (ret_charset != NULL) { + free(ret_charset); } - /* RFC 2045, Sec. 5.2: default to us-ascii. */ - if (src_charset == NULL) src_charset = &default_charset; - if (*src_charset == NULL) *src_charset = "US-ASCII"; + ret_charset = get_param(ct->c_ctinfo.ci_first_pm, "charset", '?', 0); - return *src_charset; + return ret_charset ? ret_charset : "US-ASCII"; } -/* Change the value of a name=value pair in a header field body. - If the name isn't there, append them. In any case, a new - string will be allocated and must be free'd by the caller. - Trims any trailing newlines. */ +/* + * Create a string based on a list of output parameters. Assume that this + * parameter string will be appended to an existing header, so start out + * with the separator (;). Perform RFC 2231 encoding when necessary. + */ + char * -update_attr (char *body, const char *name, const char *value) { - char *bp = nmh_strcasestr (body, name); - char *new_body; +output_params(size_t initialwidth, PM params, int *offsetout, int external) +{ + char *paramout = NULL; + char line[CPERLIN * 2], *q; + int curlen, index, cont, encode, i; + size_t valoff, numchars; + + while (params != NULL) { + encode = 0; + index = 0; + valoff = 0; + q = line; + + if (external && strcasecmp(params->pm_name, "body") == 0) + continue; - if (bp) { - char *other_attrs = strchr (bp, ';'); + if (strlen(params->pm_name) > CPERLIN) { + advise(NULL, "Parameter name \"%s\" is too long", params->pm_name); + if (paramout) + free(paramout); + return NULL; + } - *(bp + strlen (name)) = '\0'; - new_body = concat (body, "\"", value, "\"", NULL); + curlen = param_len(params, index, valoff, &encode, &cont, &numchars); - if (other_attrs) { - char *cp; + /* + * Loop until we get a parameter that fits within a line. We + * assume new lines start with a tab, so check our overflow based + * on that. + */ - /* Trim any trailing newlines. */ - for (cp = &other_attrs[strlen (other_attrs) - 1]; - cp > other_attrs && *cp == '\n'; - *cp-- = '\0') continue; - new_body = add (other_attrs, new_body); - } + while (cont) { + *q++ = ';'; + *q++ = '\n'; + *q++ = '\t'; + + /* + * At this point we're definitely continuing the line, so + * be sure to include the parameter name and section index. + */ + + q += snprintf(q, sizeof(line) - (q - line), "%s*%d", + params->pm_name, index); + + /* + * Both of these functions do a NUL termination + */ + + if (encode) + i = encode_param(params, q, sizeof(line) - (q - line), + numchars, valoff, index); + else + i = normal_param(params, q, sizeof(line) - (q - line), + numchars, valoff); + + if (i == 0) { + if (paramout) + free(paramout); + return NULL; + } + + valoff += numchars; + index++; + curlen = param_len(params, index, valoff, &encode, &cont, + &numchars); + q = line; + + /* + * "line" starts with a ;\n\t, so that doesn't count against + * the length. But add 8 since it starts with a tab; that's + * how we end up with 5. + */ + + initialwidth = strlen(line) + 5; + + /* + * At this point the line should be built, so add it to our + * current output buffer. + */ + + paramout = add(line, paramout); + } + + /* + * If this won't fit on the line, start a new one. Save room in + * case we need a semicolon on the end + */ + + if (initialwidth + curlen > CPERLIN - 1) { + *q++ = ';'; + *q++ = '\n'; + *q++ = '\t'; + initialwidth = 8; + } else { + *q++ = ';'; + *q++ = ' '; + initialwidth += 2; + } + + /* + * At this point, we're either finishing a contined parameter, or + * we're working on a new one. + */ + + if (index > 0) { + q += snprintf(q, sizeof(line) - (q - line), "%s*%d", + params->pm_name, index); + } else { + strncpy(q, params->pm_name, sizeof(line) - (q - line)); + q += strlen(q); + } + + if (encode) + i = encode_param(params, q, sizeof(line) - (q - line), + strlen(params->pm_value + valoff), valoff, index); + else + i = normal_param(params, q, sizeof(line) - (q - line), + strlen(params->pm_value + valoff), valoff); + + if (i == 0) { + if (paramout) + free(paramout); + return NULL; + } + + paramout = add(line, paramout); + initialwidth += strlen(line); + + params = params->pm_next; + } + + if (offsetout) + *offsetout = initialwidth; + + return paramout; +} + +/* + * Calculate the size of a parameter. + * + * Arguments include + * + * pm - The parameter being output + * index - If continuing the parameter, the index of the section + * we're on. + * valueoff - The current offset into the parameter value that we're + * working on (previous sections have consumed valueoff bytes). + * encode - Set if we should perform encoding on this parameter section + * (given that we're consuming bytesfit bytes). + * cont - Set if the remaining data in value will not fit on a single + * line and will need to be continued. + * bytesfit - The number of bytes that we can consume from the parameter + * value and still fit on a completely new line. The + * calculation assumes the new line starts with a tab, + * includes the parameter name and any encoding, and fits + * within CPERLIN bytes. Will always be at least 1. + */ + +static size_t +param_len(PM pm, int index, size_t valueoff, int *encode, int *cont, + size_t *bytesfit) +{ + char *start = pm->pm_value + valueoff, *p, indexchar[32]; + size_t len = 0, fit = 0; + int fitlimit = 0, eightbit, maxfit; + + *encode = 0; + + /* + * Add up the length. First, start with the parameter name. + */ + + len = strlen(pm->pm_name); + + /* + * Scan the parameter value and see if we need to do encoding for this + * section. + */ + + eightbit = contains8bit(start, NULL); + + /* + * Determine if we need to encode this section. Encoding is necessary if: + * + * - There are any 8-bit characters at all and we're on the first + * section. + * - There are 8-bit characters within N bytes of our section start. + * N is calculated based on the number of bytes it would take to + * reach CPERLIN. Specifically: + * 8 (starting tab) + + * strlen(param name) + + * 4 ('* for section marker, '=', opening/closing '"') + * strlen (index) + * is the number of bytes used by everything that isn't part of the + * value. So that gets subtracted from CPERLIN. + */ + + snprintf(indexchar, sizeof(indexchar), "%d", index); + maxfit = CPERLIN - (12 + len + strlen(indexchar)); + if ((eightbit && index == 0) || contains8bit(start, start + maxfit)) { + *encode = 1; + } + + len++; /* Add in equal sign */ + + if (*encode) { + /* + * We're using maxfit as a marker for how many characters we can + * fit into the line. Bump it by two because we're not using quotes + * when encoding. + */ + + maxfit += 2; + + /* + * If we don't have a charset or language tag in this parameter, + * add them now. + */ + + if (! pm->pm_charset) + pm->pm_charset = getcpy(write_charset_8bit()); + if (! pm->pm_lang) + pm->pm_lang = getcpy(NULL); /* Default to a blank lang tag */ + + len++; /* For the encoding marker */ + maxfit--; + if (index == 0) { + int enclen = strlen(pm->pm_charset) + strlen(pm->pm_lang) + 2; + len += enclen; + maxfit-= enclen; + } else { + /* + * We know we definitely need to include an index. maxfit already + * includes the section marker. + */ + len += strlen(indexchar); + } + for (p = start; *p != '\0'; p++) { + if (isparamencode(*p)) { + len += 3; + maxfit -= 3; + } else { + len++; + maxfit--; + } + /* + * Just so there's no confusion: maxfit is counting OUTPUT + * characters (post-encoding). fit is counting INPUT characters. + */ + if (! fitlimit && maxfit >= 0) + fit++; + else if (! fitlimit) + fitlimit++; + } } else { - char *cp; + /* + * Calculate the string length, but add room for quoting \ + * and " if necessary. Also account for quotes at beginning + * and end. + */ + for (p = start; *p != '\0'; p++) { + switch (*p) { + case '"': + case '\\': + len++; + maxfit--; + /* FALL THROUGH */ + default: + len++; + maxfit--; + } + if (! fitlimit && maxfit >= 0) + fit++; + else if (! fitlimit) + fitlimit++; + } + + len += 2; + } + + if (fit < 1) + fit = 1; + + *cont = fitlimit; + *bytesfit = fit; + + return len; +} + +/* + * Output an encoded parameter string. + */ + +static size_t +encode_param(PM pm, char *output, size_t len, size_t valuelen, + size_t valueoff, int index) +{ + size_t outlen = 0, n; + char *endptr = output + len, *p; + + /* + * First, output the marker for an encoded string. + */ + + *output++ = '*'; + *output++ = '='; + outlen += 2; - /* Append name/value pair, after first removing a final newline - and (extraneous) semicolon. */ - if (*(cp = &body[strlen (body) - 1]) == '\n') *cp = '\0'; - if (*(cp = &body[strlen (body) - 1]) == ';') *cp = '\0'; - new_body = concat (body, "; ", name, "\"", value, "\"", NULL); + /* + * If the index is 0, output the character set and language tag. + * If theses were NULL, they should have already been filled in + * by param_len(). + */ + + if (index == 0) { + n = snprintf(output, len - outlen, "%s'%s'", pm->pm_charset, + pm->pm_lang); + output += n; + outlen += n; + if (output > endptr) { + advise(NULL, "Internal error: parameter buffer overflow"); + return 0; + } + } + + /* + * Copy over the value, encoding if necessary + */ + + p = pm->pm_value + valueoff; + while (valuelen-- > 0) { + if (isparamencode(*p)) { + n = snprintf(output, len - outlen, "%%%02X", (unsigned char) *p++); + output += n; + outlen += n; + } else { + *output++ = *p++; + outlen++; + } + if (output > endptr) { + advise(NULL, "Internal error: parameter buffer overflow"); + return 0; + } + } + + *output = '\0'; + + return outlen; +} + +/* + * Output a "normal" parameter, without encoding. Be sure to escape + * quotes and backslashes if necessary. + */ + +static size_t +normal_param(PM pm, char *output, size_t len, size_t valuelen, + size_t valueoff) +{ + size_t outlen = 0; + char *endptr = output + len, *p; + + *output++ = '='; + *output++ = '"'; + outlen += 2; + + p = pm->pm_value + valueoff; + + while (valuelen-- > 0) { + switch (*p) { + case '\\': + case '"': + *output++ = '\\'; + outlen++; + default: + *output++ = *p++; + outlen++; + } + if (output > endptr) { + advise(NULL, "Internal error: parameter buffer overflow"); + return 0; + } + } + + if (output - 2 > endptr) { + advise(NULL, "Internal error: parameter buffer overflow"); + return 0; + } + + *output++ = '"'; + *output++ = '\0'; + + return outlen + 1; +} + +/* + * Add a parameter to the parameter linked list + */ + +PM +add_param(PM *first, PM *last, char *name, char *value, int nocopy) +{ + PM pm = mh_xmalloc(sizeof(*pm)); + + memset(pm, 0, sizeof(*pm)); + + pm->pm_name = nocopy ? name : getcpy(name); + pm->pm_value = nocopy ? value : getcpy(value); + + if (*first) { + (*last)->pm_next = pm; + *last = pm; + } else { + *first = pm; + *last = pm; + } + + return pm; +} + +/* + * Either replace a current parameter with a new value, or add the parameter + * to the parameter linked list. + */ + +PM +replace_param(PM *first, PM *last, char *name, char *value, int nocopy) +{ + PM pm; + + for (pm = *first; pm != NULL; pm = pm->pm_next) { + if (strcasecmp(name, pm->pm_name) == 0) { + /* + * If nocopy is set, it's assumed that we own both name + * and value. We don't need name, so we discard it now. + */ + if (nocopy) + free(name); + free(pm->pm_value); + pm->pm_value = nocopy ? value : getcpy(value); + return pm; + } } - return new_body; + return add_param(first, last, name, value, nocopy); +} + +/* + * Retrieve a parameter value from a parameter linked list. If the parameter + * value needs converted to the local character set, do that now. + */ + +char * +get_param(PM first, const char *name, char replace, int fetchonly) +{ + while (first != NULL) { + if (strcasecmp(name, first->pm_name) == 0) { + if (fetchonly) + return first->pm_value; + else + return getcpy(get_param_value(first, replace)); + } + first = first->pm_next; + } + + return NULL; +} + +/* + * Return a parameter value, converting to the local character set if + * necessary + */ + +char *get_param_value(PM pm, char replace) +{ + static char buffer[4096]; /* I hope no parameters are larger */ + size_t bufsize = sizeof(buffer); +#ifdef HAVE_ICONV + size_t inbytes; + int utf8; + iconv_t cd; + ICONV_CONST char *p; +#endif /* HAVE_ICONV */ + char *q; + + /* + * If we don't have a character set indicated, it's assumed to be + * US-ASCII. If it matches our character set, we don't need to convert + * anything. + */ + + if (!pm->pm_charset || check_charset(pm->pm_charset, + strlen(pm->pm_charset))) { + return pm->pm_value; + } + + /* + * In this case, we need to convert. If we have iconv support, use + * that. Otherwise, go through and simply replace every non-ASCII + * character with the substitution character. + */ + +#ifdef HAVE_ICONV + q = buffer; + bufsize = sizeof(buffer); + utf8 = strcasecmp(pm->pm_charset, "UTF-8") == 0; + + cd = iconv_open(get_charset(), pm->pm_charset); + if (cd == (iconv_t) -1) { + goto noiconv; + } + + inbytes = strlen(pm->pm_value); + p = pm->pm_value; + + while (inbytes) { + if (iconv(cd, &p, &inbytes, &q, &bufsize) == (size_t)-1) { + if (errno != EILSEQ) { + iconv_close(cd); + goto noiconv; + } + /* + * Reset shift state, substitute our character, + * try to restart conversion. + */ + + iconv(cd, NULL, NULL, &q, &bufsize); + + if (bufsize == 0) { + iconv_close(cd); + goto noiconv; + } + *q++ = replace; + bufsize--; + if (bufsize == 0) { + iconv_close(cd); + goto noiconv; + } + if (utf8) { + for (++p, --inbytes; + inbytes > 0 && (((unsigned char) *q) & 0xc0) == 0x80; + ++p, --inbytes) + continue; + } else { + p++; + inbytes--; + } + } + } + + iconv_close(cd); + + if (bufsize == 0) + q--; + *q = '\0'; + + return buffer; +#endif /* HAVE_ICONV */ + +noiconv: + /* + * Take everything non-ASCII and substituite the replacement character + */ + + q = buffer; + bufsize = sizeof(buffer); + for (p = pm->pm_value; *p != '\0' && bufsize > 1; p++, q++, bufsize--) { + if (isascii((unsigned char) *p) && !iscntrl((unsigned char) *p)) + *q = *p; + else + *q = replace; + } + + *q = '\0'; + + return buffer; } diff --git a/uip/mhshowsbr.c b/uip/mhshowsbr.c index eabf387c..295f43e8 100644 --- a/uip/mhshowsbr.c +++ b/uip/mhshowsbr.c @@ -44,10 +44,6 @@ int type_ok (CT, int); void content_error (char *, CT, char *, ...); void flush_errors (void); -/* mhlistsbr.c */ -int list_switch (CT, int, int, int, int); -int list_content (CT, int, int, int, int); - /* * prototypes */ @@ -73,7 +69,6 @@ static int show_external (CT, int, int); static int parse_display_string (CT, char *, int *, int *, int *, int *, char *, char *, size_t, int multipart); static int convert_content_charset (CT, char **); -static const char *parameter_value (CI, const char *); static void intrser (int); @@ -385,9 +380,9 @@ show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer char prompt[BUFSIZ]; if (ct->c_type == CT_MULTIPART) - list_content (ct, -1, 1, 0, 0); + list_content (ct, -1, 1, 0, 0, 0); else - list_switch (ct, -1, 1, 0, 0); + list_switch (ct, -1, 1, 0, 0, 0); if (xpause && isatty (fileno (stdout))) { int intr; @@ -795,11 +790,12 @@ parse_display_string (CT ct, char *cp, int *xstdin, int *xlist, int *xpause, case 'a': /* insert parameters from Content-Type field */ { - char **ap, **ep; + PM pm; char *s = ""; - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) { - snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep); + for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) { + snprintf (bp, buflen, "%s%s=\"%s\"", s, pm->pm_name, + get_param_value(pm, '?')); len = strlen (bp); bp += len; buflen -= len; @@ -905,11 +901,11 @@ parse_display_string (CT ct, char *cp, int *xstdin, int *xlist, int *xpause, if (closing_brace) { const size_t param_len = closing_brace - cp - 1; char *param = mh_xmalloc(param_len + 1); - const char *value; + char *value; (void) strncpy(param, cp + 1, param_len); param[param_len] = '\0'; - value = parameter_value(ci, param); + value = get_param(ci->ci_first_pm, param, '?', 0); free(param); cp += param_len + 1; /* Skip both braces, too. */ @@ -918,6 +914,7 @@ parse_display_string (CT ct, char *cp, int *xstdin, int *xlist, int *xpause, /* %{param} is set in the Content-Type header. After the break below, quote it if necessary. */ (void) strncpy(bp, value, buflen); + free(value); } else { /* %{param} not found, so skip it completely. cp was advanced above. */ @@ -1160,8 +1157,19 @@ convert_charset (CT ct, char *dest_charset, int *message_mods) { /* Update ct->c_ctline. */ if (ct->c_ctline) { - char *ctline = - update_attr (ct->c_ctline, "charset=", dest_charset); + char *ctline = concat(" ", ct->c_ctinfo.ci_type, "/", + ct->c_ctinfo.ci_subtype, NULL); + replace_param(&ct->c_ctinfo.ci_first_pm, + &ct->c_ctinfo.ci_last_pm, "charset", + dest_charset, 0); + char *outline = output_params(strlen(TYPE_FIELD) + 1 + + strlen(ctline), + ct->c_ctinfo.ci_first_pm, + NULL, 0); + if (outline) { + ctline = add(outline, ctline); + free(outline); + } free (ct->c_ctline); ct->c_ctline = ctline; @@ -1170,10 +1178,7 @@ convert_charset (CT ct, char *dest_charset, int *message_mods) { /* Update Content-Type header field. */ for (hf = ct->c_first_hf; hf; hf = hf->next) { if (! strcasecmp (TYPE_FIELD, hf->name)) { - char *ctline_less_newline = - update_attr (hf->value, "charset=", dest_charset); - char *ctline = concat (ctline_less_newline, "\n", NULL); - free (ctline_less_newline); + char *ctline = concat (ct->c_ctline, "\n", NULL); free (hf->value); hf->value = ctline; @@ -1222,26 +1227,6 @@ convert_content_charset (CT ct, char **file) { } -/* - * If a Content-Type parameter named param exists, returns its value. - * Otherwise, returns NULL. - */ -static const char * -parameter_value (CI ci, const char *param) { - const char *value = NULL; - char **ap, **vp; - - for (ap = ci->ci_attrs, vp = ci->ci_values; *ap; ++ap, ++vp) { - if (! strcasecmp (*ap, param)) { - value = *vp; - break; - } - } - - return value; -} - - static void intrser (int i) { diff --git a/uip/mhstoresbr.c b/uip/mhstoresbr.c index 89a2f278..fc265542 100644 --- a/uip/mhstoresbr.c +++ b/uip/mhstoresbr.c @@ -227,7 +227,6 @@ store_generic (CT ct, mhstoreinfo_t info) static int store_application (CT ct, mhstoreinfo_t info) { - char **ap, **ep; CI ci = &ct->c_ctinfo; /* Check if the content specifies a filename */ @@ -241,28 +240,23 @@ store_application (CT ct, mhstoreinfo_t info) */ if (!ct->c_storeproc && ct->c_subtype == APPLICATION_OCTETS) { int tarP = 0, zP = 0, gzP = 0; + char *cp; - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) { - /* check for "type=tar" attribute */ - if (!strcasecmp (*ap, "type")) { - if (strcasecmp (*ep, "tar")) - break; - + if ((cp = get_param(ci->ci_first_pm, "type", ' ', 1))) { + if (strcasecmp (cp, "tar") == 0) tarP = 1; - continue; - } + } - /* check for "conversions=compress" attribute */ - if ((!strcasecmp (*ap, "conversions") || !strcasecmp (*ap, "x-conversions")) - && (!strcasecmp (*ep, "compress") || !strcasecmp (*ep, "x-compress"))) { + /* check for "conversions=compress" attribute */ + if ((cp = get_param(ci->ci_first_pm, "conversions", ' ', 1)) || + (cp = get_param(ci->ci_first_pm, "x-conversions", ' ', 1))) { + if (strcasecmp (cp, "compress") == 0 || + strcasecmp (cp, "x-compress") == 0) { zP = 1; - continue; } - /* check for "conversions=gzip" attribute */ - if ((!strcasecmp (*ap, "conversions") || !strcasecmp (*ap, "x-conversions")) - && (!strcasecmp (*ep, "gzip") || !strcasecmp (*ep, "x-gzip"))) { + if (strcasecmp (cp, "gzip") == 0 || + strcasecmp (cp, "x-gzip") == 0) { gzP = 1; - continue; } } @@ -998,12 +992,12 @@ parse_format_string (CT ct, char *cp, char *buffer, int buflen, char *dir) buflen--; continue; } else { - char **ap, **ep; + PM pm; char *s = ""; - for (ap = ci->ci_attrs, ep = ci->ci_values; - *ap; ap++, ep++) { - snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep); + for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) { + snprintf (bp, buflen, "%s%s=\"%s\"", s, + pm->pm_name, get_param_value(pm, '?')); len = strlen (bp); bp += len; buflen -= len; @@ -1075,7 +1069,7 @@ raw: static void get_storeproc (CT ct) { - char **ap, **ep, *cp; + char *cp; CI ci; /* @@ -1091,29 +1085,18 @@ get_storeproc (CT ct) * use that (RFC-2183). */ if (ct->c_dispo) { - char *cp = strchr (ct->c_dispo, ';'); - CI ci = calloc (1, sizeof *ci); - int status; - int found_filename = 0; - - if (cp && parse_header_attrs (ct->c_file, strlen (invo_name) + 2, &cp, - ci, &status) == OK) { - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) { - if (! strcasecmp (*ap, "filename") - && *(cp = *ep) != '/' - && *cp != '.' - && *cp != '|' - && *cp != '!' - && !strchr (cp, '%')) { - ct->c_storeproc = add (cp, NULL); - found_filename = 1; - } - free (*ap); - } + if ((cp = get_param(ct->c_dispo_first, "filename", '_', 0)) + && *cp != '/' + && *cp != '.' + && *cp != '|' + && *cp != '!' + && !strchr (cp, '%')) { + ct->c_storeproc = add (cp, NULL); + free(cp); + return; } - - free (ci); - if (found_filename) return; + if (cp) + free(cp); } /* @@ -1122,17 +1105,17 @@ get_storeproc (CT ct) * the storeproc. */ ci = &ct->c_ctinfo; - for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) { - if (! strcasecmp (*ap, "name") - && *(cp = *ep) != '/' - && *cp != '.' - && *cp != '|' - && *cp != '!' - && !strchr (cp, '%')) { + if ((cp = get_param(ci->ci_first_pm, "name", '_', 0)) + && *cp != '/' + && *cp != '.' + && *cp != '|' + && *cp != '!' + && !strchr (cp, '%')) { ct->c_storeproc = add (cp, NULL); - return; - } + } + if (cp) + free(cp); }