1 /* mhparse.h -- definitions for parsing/building of MIME content
2 * -- (mhparse.c/mhbuildsbr.c)
6 * Abstract type for header fields
8 typedef struct hfield
*HF
;
11 * Abstract types for MIME parsing/building
13 typedef struct cefile
*CE
;
14 typedef struct CTinfo
*CI
;
15 typedef struct Content
*CT
;
16 typedef struct Parameter
*PM
;
19 * type for Init function (both type and transfer encoding)
21 typedef int (*InitFunc
) (CT
);
24 * types for various transfer encoding access functions
26 typedef int (*OpenCEFunc
) (CT
, char **);
27 typedef void (*CloseCEFunc
) (CT
);
28 typedef unsigned long (*SizeCEFunc
) (CT
);
31 * Structure for storing/encoding/decoding
32 * a header field and its value.
35 char *name
; /* field name */
36 char *value
; /* field body */
37 int hf_encoding
; /* internal flag for transfer encoding to use */
38 HF next
; /* link to next header field */
42 * Structure for holding MIME parameter elements.
45 char *pm_name
; /* Parameter name */
46 char *pm_value
; /* Parameter value */
47 char *pm_charset
; /* Parameter character set (optional) */
48 char *pm_lang
; /* Parameter language tag (optional) */
49 PM pm_next
; /* Pointer to next element */
53 * Structure for storing parsed elements
54 * of the Content-Type component.
57 char *ci_type
; /* content type */
58 char *ci_subtype
; /* content subtype */
59 PM ci_first_pm
; /* Pointer to first MIME parameter */
60 PM ci_last_pm
; /* Pointer to last MIME parameter */
61 char *ci_comment
; /* RFC-822 comments */
66 * Structure for storing decoded contents after
67 * removing Content-Transfer-Encoding.
70 char *ce_file
; /* decoded content (file) */
71 FILE *ce_fp
; /* decoded content (stream) */
72 int ce_unlink
; /* remove file when done? */
76 * Primary structure for handling Content (Entity)
78 * Some more explanation of this:
80 * This structure recursively describes a complete MIME message.
81 * At the top level, the c_first_hf list has a list of all message
82 * headers. If the content-type is multipart (c_type == CT_MULTIPART)
83 * then c_ctparams will contain a pointer to a struct multipart.
84 * A struct multipart contains (among other things) a linked list
85 * of struct part elements, and THOSE contain a pointer to the sub-part's
88 * An extra note for message/external-body parts. The enclosing
89 * content structure is marked as a message/external-body; the c_ctparams
90 * contains a pointer to a struct exbody, which contains a pointer to
91 * (among other things) the "real" content (e.g., application/octet-stream).
92 * The "real" content structure has the c_ctexbody pointer back to the
93 * same struct exbody sees in the enclosing content structure (the struct
94 * exbody contains parent pointers if you need to traverse up the content
95 * structure). Hopefully this makes it clearer:
98 * Type: message/external-body
99 * c_ctparams: pointer to "struct exbody"
103 * Type: application/octet-stream (or whatever)
105 * c_ctexbody: pointer to "struct exbody"
109 /* source (read) file */
110 char *c_file
; /* read contents (file) */
111 FILE *c_fp
; /* read contents (stream) */
112 int c_unlink
; /* remove file when done? */
114 long c_begin
; /* where content body starts in file */
115 long c_end
; /* where content body ends in file */
117 /* linked list of header fields */
118 HF c_first_hf
; /* pointer to first header field */
119 HF c_last_hf
; /* pointer to last header field */
121 /* copies of MIME related header fields */
122 char *c_vrsn
; /* MIME-Version: */
123 char *c_ctline
; /* Content-Type: */
124 char *c_celine
; /* Content-Transfer-Encoding: */
125 char *c_id
; /* Content-ID: */
126 char *c_descr
; /* Content-Description: */
127 char *c_dispo
; /* Content-Disposition: */
128 char *c_dispo_type
; /* Type of Content-Disposition */
129 PM c_dispo_first
; /* Pointer to first disposition parm */
130 PM c_dispo_last
; /* Pointer to last disposition parm */
131 char *c_partno
; /* within multipart content */
133 /* Content-Type info */
134 struct CTinfo c_ctinfo
; /* parsed elements of Content-Type */
135 int c_type
; /* internal flag for content type */
136 int c_subtype
; /* internal flag for content subtype */
138 /* Content-Transfer-Encoding info (decoded contents) */
139 struct cefile c_cefile
; /* structure holding decoded content */
140 int c_encoding
; /* internal flag for encoding type */
141 int c_reqencoding
; /* Requested encoding (by mhbuild) */
143 /* Content-MD5 info */
144 int c_digested
; /* have we seen this header before? */
145 unsigned char c_digest
[16]; /* decoded MD5 checksum */
147 /* pointers to content-specific structures */
148 void *c_ctparams
; /* content type specific data */
149 struct exbody
*c_ctexbody
; /* data for type message/external */
151 /* function pointers */
152 InitFunc c_ctinitfnx
; /* parse content body */
153 OpenCEFunc c_ceopenfnx
; /* get a stream to decoded contents */
154 CloseCEFunc c_ceclosefnx
; /* release stream */
155 SizeCEFunc c_cesizefnx
; /* size of decoded contents */
157 int c_umask
; /* associated umask */
158 int c_rfc934
; /* RFC 934 compatibility flag */
160 char *c_showproc
; /* default, if not in profile */
161 char *c_termproc
; /* for charset madness... */
162 char *c_storeproc
; /* overrides profile entry, if any */
164 char *c_storage
; /* write contents (file) */
165 char *c_folder
; /* write contents (folder) */
169 * Flags for Content-Type (Content->c_type)
171 #define CT_UNKNOWN 0x00
172 #define CT_APPLICATION 0x01
173 #define CT_AUDIO 0x02
174 #define CT_IMAGE 0x03
175 #define CT_MESSAGE 0x04
176 #define CT_MULTIPART 0x05
178 #define CT_VIDEO 0x07
179 #define CT_EXTENSION 0x08
182 * Flags for Content-Transfer-Encoding (Content->c_encoding)
184 #define CE_UNKNOWN 0x00
185 #define CE_BASE64 0x01
186 #define CE_QUOTED 0x02
189 #define CE_BINARY 0x05
190 #define CE_EXTENSION 0x06
191 #define CE_EXTERNAL 0x07 /* for external-body */
197 /* Flags for subtypes of TEXT */
198 #define TEXT_UNKNOWN 0x00
199 #define TEXT_PLAIN 0x01
200 #define TEXT_RICHTEXT 0x02
201 #define TEXT_ENRICHED 0x03
203 /* Flags for character sets */
204 #define CHARSET_SPECIFIED 0x00
205 #define CHARSET_UNSPECIFIED 0x01 /* only needed when building drafts */
207 /* Structure for text content */
209 int tx_charset
; /* flag for character set */
210 int lf_line_endings
; /* Whether to use CR LF (0) or LF (1) line
211 endings. The meaning of 0 was selected so
212 that CR LF is the default, in accordance
213 with RFC 2046, Sec. 4.1.1, Par. 1. */
220 /* Flags for subtypes of MULTIPART */
221 #define MULTI_UNKNOWN 0x00
222 #define MULTI_MIXED 0x01
223 #define MULTI_ALTERNATE 0x02
224 #define MULTI_DIGEST 0x03
225 #define MULTI_PARALLEL 0x04
226 #define MULTI_RELATED 0x05
228 /* Structure for subparts of a multipart content */
230 CT mp_part
; /* Content structure for subpart */
231 struct part
*mp_next
; /* pointer to next subpart structure */
234 /* Main structure for multipart content */
236 char *mp_start
; /* boundary string separating parts */
237 char *mp_stop
; /* terminating boundary string */
238 char *mp_content_before
; /* any content before the first subpart */
239 char *mp_content_after
; /* any content after the last subpart */
240 struct part
*mp_parts
; /* pointer to first subpart structure */
247 /* Flags for subtypes of MESSAGE */
248 #define MESSAGE_UNKNOWN 0x00
249 #define MESSAGE_RFC822 0x01
250 #define MESSAGE_PARTIAL 0x02
251 #define MESSAGE_EXTERNAL 0x03
253 /* Structure for message/partial */
262 /* Structure for message/external */
264 CT eb_parent
; /* pointer to controlling content structure */
265 CT eb_content
; /* pointer to internal content structure */
274 unsigned long eb_size
;
282 * APPLICATION content
285 /* Flags for subtype of APPLICATION */
286 #define APPLICATION_UNKNOWN 0x00
287 #define APPLICATION_OCTETS 0x01
288 #define APPLICATION_POSTSCRIPT 0x02
292 * Structures for mapping types to their internal flags
298 extern struct k2v SubText
[];
299 extern struct k2v Charset
[];
300 extern struct k2v SubMultiPart
[];
301 extern struct k2v SubMessage
[];
302 extern struct k2v SubApplication
[];
305 * Structures for mapping (content) types to
306 * the functions to handle them.
313 extern struct str2init str2cts
[];
314 extern struct str2init str2ces
[];
315 extern struct str2init str2methods
[];
320 CT
parse_mime (char *);
323 * Translate a composition file into a MIME data structure. Arguments are:
325 * infile - Name of input filename
326 * autobuild - A flag to indicate if the composition file parser is
327 * being run in automatic mode or not. In auto mode,
328 * if a MIME-Version header is encountered it is assumed
329 * that the composition file is already in MIME format
330 * and will not be processed further. Otherwise, an
331 * error is generated.
332 * dist - A flag to indicate if we are being run by "dist". In
333 * that case, add no MIME headers to the message. Existing
334 * headers will still be encoded by RFC 2047.
335 * directives - A flag to control whether or not build directives are
336 * processed by default.
337 * encoding - The default encoding to use when doing RFC 2047 header
338 * encoding. Must be one of CE_UNKNOWN, CE_BASE64, or
340 * maxunencoded - The maximum line length before the default encoding for
341 * text parts is quoted-printable.
342 * verbose - If 1, output verbose information during message composition
344 * Returns a CT structure describing the resulting MIME message. If the
345 * -auto flag is set and a MIME-Version header is encountered, the return
348 CT
build_mime (char *infile
, int autobuild
, int dist
, int directives
,
349 int encoding
, size_t maxunencoded
, int verbose
);
351 int add_header (CT
, char *, char *);
352 int get_ctinfo (char *, CT
, int);
353 int params_external (CT
, int);
354 int open7Bit (CT
, char **);
355 void close_encoding (CT
);
356 char *ct_type_str (int) CONST
;
357 char *ct_subtype_str (int, int) CONST
;
358 int ct_str_type (const char *) PURE
;
359 int ct_str_subtype (int, const char *) PURE
;
360 const struct str2init
*get_ct_init (int) PURE
;
361 const char *ce_str (int) CONST
;
362 const struct str2init
*get_ce_method (const char *) PURE
;
363 char *content_charset (CT
);
364 void reverse_alternative_parts (CT
);
367 * Given a list of messages, display information about them on standard
372 * cts - An array of CT elements of messages that need to be
373 * displayed. Array is terminated by a NULL.
374 * headsw - If 1, display a column header.
375 * sizesw - If 1, display the size of the part.
376 * verbosw - If 1, display verbose information
377 * debugsw - If 1, turn on debugging for the output.
378 * disposw - If 1, display MIME part disposition information.
381 void list_all_messages(CT
*cts
, int headsw
, int sizesw
, int verbosw
,
382 int debugsw
, int disposw
);
385 * List the content information of a single MIME part on stdout.
389 * ct - MIME Content structure to display.
390 * toplevel - If set, we're at the top level of a message
391 * realsize - If set, determine the real size of the content
392 * verbose - If set, output verbose information
393 * debug - If set, turn on debugging for the output
394 * dispo - If set, display MIME part disposition information.
396 * Returns OK on success, NOTOK otherwise.
398 int list_content(CT ct
, int toplevel
, int realsize
, int verbose
, int debug
,
402 * Display content-appropriate information on MIME parts, descending recursively
403 * into multipart content if appropriate. Uses list_content() for displaying
404 * generic information.
406 * Arguments and return value are the same as list_content().
408 int list_switch(CT ct
, int toplevel
, int realsize
, int verbose
, int debug
,
412 * Given a linked list of parameters, build an output string for them. This
413 * string is designed to be concatenated on an already-built header.
417 * initialwidth - Current width of the header. Used to compute when to wrap
418 * parameters on the first line. The following lines will
419 * be prefixed by a tab (\t) character.
420 * params - Pointer to head of linked list of parameters.
421 * offsetout - The final line offset after all the parameters have been
422 * output. May be NULL.
423 * external - If set, outputting an external-body type and will not
424 * output a "body" parameter.
426 * Returns a pointer to the resulting parameter string. This string must
427 * be free()'d by the caller. Returns NULL on error.
429 char *output_params(size_t initialwidth
, PM params
, int *offsetout
,
433 * Encode a parameter value using RFC 2231 encode.
437 * pm - PM containing the parameter value and related info.
438 * output - Output buffer.
439 * len - Size, in octets, of output buffer.
440 * valuelen - Number of characters in the value
441 * valueoff - Offset into value field (pm->pm_value).
442 * index - If 0, output character set and language tag.
444 size_t encode_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
445 size_t valueoff
, int index
);
448 * Add a parameter to the parameter linked list.
452 * first - Pointer to head of linked list
453 * last - Pointer to tail of linked list
454 * name - Name of parameter
455 * value - Value of parameter
456 * nocopy - If set, will use the pointer values directly for "name"
457 * and "value" instead of making their own copy. These
458 * pointers will be free()'d later by the MIME routines, so
459 * they should not be used after calling this function!
461 * Returns allocated parameter element
463 PM
add_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
);
466 * Replace (or add) a parameter to the parameter linked list.
468 * If the named parameter already exists on the parameter linked list,
469 * replace the value with the new one. Otherwise add it to the linked
470 * list. All parameters are identical to add_param().
472 PM
replace_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
);
475 * Retrieve a parameter value from a parameter linked list. Convert to the
476 * local character set if required.
480 * first - Pointer to head of parameter linked list.
481 * name - Name of parameter.
482 * replace - If characters in the parameter list cannot be converted to
483 * the local character set, replace with this character.
484 * fetchonly - If true, return pointer to original value, no conversion
487 * Returns parameter value if found, NULL otherwise. Memory must be free()'d
488 * unless fetchonly is set.
491 char *get_param(PM first
, const char *name
, char replace
, int fetchonly
);
494 * Fetch a parameter value from a parameter structure, converting it to
495 * the local character set.
499 * pm - Pointer to parameter structure
500 * replace - If characters in the parameter list cannot be converted to
501 * the local character set, replace with this character.
503 * Returns a pointer to the parameter value. Memory is stored in an
504 * internal buffer, so the returned value is only valid until the next
505 * call to get_param_value() or get_param() (get_param() uses get_param_value()
508 char *get_param_value(PM pm
, char replace
);
510 extern int checksw
; /* Add Content-MD5 field */
514 * Put it here because it uses the CT typedef.
516 typedef struct mhstoreinfo
*mhstoreinfo_t
;
517 mhstoreinfo_t
mhstoreinfo_create(CT
*, char *, const char *, int, int);
518 int mhstoreinfo_files_not_clobbered(const mhstoreinfo_t
) PURE
;
519 void mhstoreinfo_free(mhstoreinfo_t
);
520 void store_all_messages (mhstoreinfo_t
);
522 extern bool skip_mp_cte_check
;
523 extern bool suppress_bogus_mp_content_warning
;
524 extern bool bogus_mp_content
;
525 extern bool suppress_extraneous_trailing_semicolon_warning
;
527 extern bool suppress_multiple_mime_version_warning
;
534 extern mime_type_subtype mime_preference
[NPREFS
];
535 extern int npreferred
;