]>
diplodocus.org Git - nmh/blob - h/mhparse.h
1 /* mhparse.h -- definitions for parsing/building of MIME content
2 * -- (mhparse.c/mhbuildsbr.c)
10 * Abstract type for header fields
12 typedef struct hfield
*HF
;
15 * Abstract types for MIME parsing/building
17 typedef struct cefile
*CE
;
18 typedef struct CTinfo
*CI
;
19 typedef struct Content
*CT
;
20 typedef struct Parameter
*PM
;
23 * type for Init function (both type and transfer encoding)
25 typedef int (*InitFunc
) (CT
);
28 * types for various transfer encoding access functions
30 typedef int (*OpenCEFunc
) (CT
, char **);
31 typedef void (*CloseCEFunc
) (CT
);
32 typedef unsigned long (*SizeCEFunc
) (CT
);
35 * Structure for storing/encoding/decoding
36 * a header field and its value.
39 char *name
; /* field name */
40 char *value
; /* field body */
41 int hf_encoding
; /* internal flag for transfer encoding to use */
42 HF next
; /* link to next header field */
46 * Structure for holding MIME parameter elements.
49 char *pm_name
; /* Parameter name */
50 char *pm_value
; /* Parameter value */
51 char *pm_charset
; /* Parameter character set (optional) */
52 char *pm_lang
; /* Parameter language tag (optional) */
53 PM pm_next
; /* Pointer to next element */
57 * Structure for storing parsed elements
58 * of the Content-Type component.
61 char *ci_type
; /* content type */
62 char *ci_subtype
; /* content subtype */
63 PM ci_first_pm
; /* Pointer to first MIME parameter */
64 PM ci_last_pm
; /* Pointer to last MIME parameter */
65 char *ci_comment
; /* RFC-822 comments */
70 * Structure for storing decoded contents after
71 * removing Content-Transfer-Encoding.
74 char *ce_file
; /* decoded content (file) */
75 FILE *ce_fp
; /* decoded content (stream) */
76 int ce_unlink
; /* remove file when done? */
80 * Primary structure for handling Content (Entity)
82 * Some more explanation of this:
84 * This structure recursively describes a complete MIME message.
85 * At the top level, the c_first_hf list has a list of all message
86 * headers. If the content-type is multipart (c_type == CT_MULTIPART)
87 * then c_ctparams will contain a pointer to a struct multipart.
88 * A struct multipart contains (among other things) a linked list
89 * of struct part elements, and THOSE contain a pointer to the sub-part's
92 * An extra note for message/external-body parts. The enclosing
93 * content structure is marked as a message/external-body; the c_ctparams
94 * contains a pointer to a struct exbody, which contains a pointer to
95 * (among other things) the "real" content (e.g., application/octet-stream).
96 * The "real" content structure has the c_ctexbody pointer back to the
97 * same struct exbody sees in the enclosing content structure (the struct
98 * exbody contains parent pointers if you need to traverse up the content
99 * structure). Hopefully this makes it clearer:
102 * Type: message/external-body
103 * c_ctparams: pointer to "struct exbody"
107 * Type: application/octet-stream (or whatever)
109 * c_ctexbody: pointer to "struct exbody"
113 /* source (read) file */
114 char *c_file
; /* read contents (file) */
115 FILE *c_fp
; /* read contents (stream) */
116 int c_unlink
; /* remove file when done? */
118 long c_begin
; /* where content body starts in file */
119 long c_end
; /* where content body ends in file */
121 /* linked list of header fields */
122 HF c_first_hf
; /* pointer to first header field */
123 HF c_last_hf
; /* pointer to last header field */
125 /* copies of MIME related header fields */
126 char *c_vrsn
; /* MIME-Version: */
127 char *c_ctline
; /* Content-Type: */
128 char *c_celine
; /* Content-Transfer-Encoding: */
129 char *c_id
; /* Content-ID: */
130 char *c_descr
; /* Content-Description: */
131 char *c_dispo
; /* Content-Disposition: */
132 char *c_dispo_type
; /* Type of Content-Disposition */
133 PM c_dispo_first
; /* Pointer to first disposition parm */
134 PM c_dispo_last
; /* Pointer to last disposition parm */
135 char *c_partno
; /* within multipart content */
137 /* Content-Type info */
138 struct CTinfo c_ctinfo
; /* parsed elements of Content-Type */
139 int c_type
; /* internal flag for content type */
140 int c_subtype
; /* internal flag for content subtype */
142 /* Content-Transfer-Encoding info (decoded contents) */
143 struct cefile c_cefile
; /* structure holding decoded content */
144 int c_encoding
; /* internal flag for encoding type */
145 int c_reqencoding
; /* Requested encoding (by mhbuild) */
147 /* Content-MD5 info */
148 int c_digested
; /* have we seen this header before? */
149 unsigned char c_digest
[16]; /* decoded MD5 checksum */
151 /* pointers to content-specific structures */
152 void *c_ctparams
; /* content type specific data */
153 struct exbody
*c_ctexbody
; /* data for type message/external */
155 /* function pointers */
156 InitFunc c_ctinitfnx
; /* parse content body */
157 OpenCEFunc c_ceopenfnx
; /* get a stream to decoded contents */
158 CloseCEFunc c_ceclosefnx
; /* release stream */
159 SizeCEFunc c_cesizefnx
; /* size of decoded contents */
161 int c_umask
; /* associated umask */
162 int c_rfc934
; /* RFC 934 compatibility flag */
164 char *c_showproc
; /* default, if not in profile */
165 char *c_termproc
; /* for charset madness... */
166 char *c_storeproc
; /* overrides profile entry, if any */
168 char *c_storage
; /* write contents (file) */
169 char *c_folder
; /* write contents (folder) */
173 * Flags for Content-Type (Content->c_type)
175 #define CT_UNKNOWN 0x00
176 #define CT_APPLICATION 0x01
177 #define CT_AUDIO 0x02
178 #define CT_IMAGE 0x03
179 #define CT_MESSAGE 0x04
180 #define CT_MULTIPART 0x05
182 #define CT_VIDEO 0x07
183 #define CT_EXTENSION 0x08
186 * Flags for Content-Transfer-Encoding (Content->c_encoding)
188 #define CE_UNKNOWN 0x00
189 #define CE_BASE64 0x01
190 #define CE_QUOTED 0x02
193 #define CE_BINARY 0x05
194 #define CE_EXTENSION 0x06
195 #define CE_EXTERNAL 0x07 /* for external-body */
201 /* Flags for subtypes of TEXT */
202 #define TEXT_UNKNOWN 0x00
203 #define TEXT_PLAIN 0x01
204 #define TEXT_RICHTEXT 0x02
205 #define TEXT_ENRICHED 0x03
207 /* Flags for character sets */
208 #define CHARSET_SPECIFIED 0x00
209 #define CHARSET_UNSPECIFIED 0x01 /* only needed when building drafts */
211 /* Structure for text content */
213 int tx_charset
; /* flag for character set */
214 int lf_line_endings
; /* Whether to use CR LF (0) or LF (1) line
215 endings. The meaning of 0 was selected so
216 that CR LF is the default, in accordance
217 with RFC 2046, Sec. 4.1.1, Par. 1. */
224 /* Flags for subtypes of MULTIPART */
225 #define MULTI_UNKNOWN 0x00
226 #define MULTI_MIXED 0x01
227 #define MULTI_ALTERNATE 0x02
228 #define MULTI_DIGEST 0x03
229 #define MULTI_PARALLEL 0x04
230 #define MULTI_RELATED 0x05
232 /* Structure for subparts of a multipart content */
234 CT mp_part
; /* Content structure for subpart */
235 struct part
*mp_next
; /* pointer to next subpart structure */
238 /* Main structure for multipart content */
240 char *mp_start
; /* boundary string separating parts */
241 char *mp_stop
; /* terminating boundary string */
242 char *mp_content_before
; /* any content before the first subpart */
243 char *mp_content_after
; /* any content after the last subpart */
244 struct part
*mp_parts
; /* pointer to first subpart structure */
251 /* Flags for subtypes of MESSAGE */
252 #define MESSAGE_UNKNOWN 0x00
253 #define MESSAGE_RFC822 0x01
254 #define MESSAGE_PARTIAL 0x02
255 #define MESSAGE_EXTERNAL 0x03
257 /* Structure for message/partial */
266 /* Structure for message/external */
268 CT eb_parent
; /* pointer to controlling content structure */
269 CT eb_content
; /* pointer to internal content structure */
278 unsigned long eb_size
;
286 * APPLICATION content
289 /* Flags for subtype of APPLICATION */
290 #define APPLICATION_UNKNOWN 0x00
291 #define APPLICATION_OCTETS 0x01
292 #define APPLICATION_POSTSCRIPT 0x02
296 * Structures for mapping types to their internal flags
302 extern struct k2v SubText
[];
303 extern struct k2v Charset
[];
304 extern struct k2v SubMultiPart
[];
305 extern struct k2v SubMessage
[];
306 extern struct k2v SubApplication
[];
309 * Structures for mapping (content) types to
310 * the functions to handle them.
317 extern struct str2init str2cts
[];
318 extern struct str2init str2ces
[];
319 extern struct str2init str2methods
[];
324 CT
parse_mime (char *);
327 * Translate a composition file into a MIME data structure. Arguments are:
329 * infile - Name of input filename
330 * autobuild - A flag to indicate if the composition file parser is
331 * being run in automatic mode or not. In auto mode,
332 * if a MIME-Version header is encountered it is assumed
333 * that the composition file is already in MIME format
334 * and will not be processed further. Otherwise, an
335 * error is generated.
336 * dist - A flag to indicate if we are being run by "dist". In
337 * that case, add no MIME headers to the message. Existing
338 * headers will still be encoded by RFC 2047.
339 * directives - A flag to control whether or not build directives are
340 * processed by default.
341 * encoding - The default encoding to use when doing RFC 2047 header
342 * encoding. Must be one of CE_UNKNOWN, CE_BASE64, or
344 * maxunencoded - The maximum line length before the default encoding for
345 * text parts is quoted-printable.
346 * verbose - If 1, output verbose information during message composition
348 * Returns a CT structure describing the resulting MIME message. If the
349 * -auto flag is set and a MIME-Version header is encountered, the return
352 CT
build_mime (char *infile
, int autobuild
, int dist
, int directives
,
353 int encoding
, size_t maxunencoded
, int verbose
);
355 int add_header (CT
, char *, char *);
356 int get_ctinfo (char *, CT
, int);
357 int params_external (CT
, int);
358 int open7Bit (CT
, char **);
359 void close_encoding (CT
);
360 char *ct_type_str (int);
361 char *ct_subtype_str (int, int);
362 int ct_str_type (const char *);
363 int ct_str_subtype (int, const char *);
364 const struct str2init
*get_ct_init (int);
365 const char *ce_str (int);
366 const struct str2init
*get_ce_method (const char *);
367 char *content_charset (CT
);
368 void reverse_alternative_parts (CT
);
371 * Given a content structure, return true if the content has a disposition
376 * ct - Content structure to examine
378 int is_inline(CT ct
);
381 * Given a list of messages, display information about them on standard
386 * cts - An array of CT elements of messages that need to be
387 * displayed. Array is terminated by a NULL.
388 * headsw - If 1, display a column header.
389 * sizesw - If 1, display the size of the part.
390 * verbosw - If 1, display verbose information
391 * debugsw - If 1, turn on debugging for the output.
392 * disposw - If 1, display MIME part disposition information.
395 void list_all_messages(CT
*cts
, int headsw
, int sizesw
, int verbosw
,
396 int debugsw
, int disposw
);
399 * List the content information of a single MIME part on stdout.
403 * ct - MIME Content structure to display.
404 * toplevel - If set, we're at the top level of a message
405 * realsize - If set, determine the real size of the content
406 * verbose - If set, output verbose information
407 * debug - If set, turn on debugging for the output
408 * dispo - If set, display MIME part disposition information.
410 * Returns OK on success, NOTOK otherwise.
412 int list_content(CT ct
, int toplevel
, int realsize
, int verbose
, int debug
,
416 * Display content-appropriate information on MIME parts, descending recursively
417 * into multipart content if appropriate. Uses list_content() for displaying
418 * generic information.
420 * Arguments and return value are the same as list_content().
422 int list_switch(CT ct
, int toplevel
, int realsize
, int verbose
, int debug
,
426 * Given a linked list of parameters, build an output string for them. This
427 * string is designed to be concatenated on an already-built header.
431 * initialwidth - Current width of the header. Used to compute when to wrap
432 * parameters on the first line. The following lines will
433 * be prefixed by a tab (\t) character.
434 * params - Pointer to head of linked list of parameters.
435 * offsetout - The final line offset after all the parameters have been
436 * output. May be NULL.
437 * external - If set, outputting an external-body type and will not
438 * output a "body" parameter.
440 * Returns a pointer to the resulting parameter string. This string must
441 * be free()'d by the caller. Returns NULL on error.
443 char *output_params(size_t initialwidth
, PM params
, int *offsetout
,
447 * Encode a parameter value using RFC 2231 encode.
451 * pm - PM containing the parameter value and related info.
452 * output - Output buffer.
453 * len - Size, in octets, of output buffer.
454 * valuelen - Number of characters in the value
455 * valueoff - Offset into value field (pm->pm_value).
456 * index - If 0, output character set and language tag.
458 size_t encode_param(PM pm
, char *output
, size_t len
, size_t valuelen
,
459 size_t valueoff
, int index
);
462 * Add a parameter to the parameter linked list.
466 * first - Pointer to head of linked list
467 * last - Pointer to tail of linked list
468 * name - Name of parameter
469 * value - Value of parameter
470 * nocopy - If set, will use the pointer values directly for "name"
471 * and "value" instead of making their own copy. These
472 * pointers will be free()'d later by the MIME routines, so
473 * they should not be used after calling this function!
475 * Returns allocated parameter element
477 PM
add_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
);
480 * Replace (or add) a parameter to the parameter linked list.
482 * If the named parameter already exists on the parameter linked list,
483 * replace the value with the new one. Otherwise add it to the linked
484 * list. All parameters are identical to add_param().
486 PM
replace_param(PM
*first
, PM
*last
, char *name
, char *value
, int nocopy
);
489 * Retrieve a parameter value from a parameter linked list. Convert to the
490 * local character set if required.
494 * first - Pointer to head of parameter linked list.
495 * name - Name of parameter.
496 * replace - If characters in the parameter list cannot be converted to
497 * the local character set, replace with this character.
498 * fetchonly - If true, return pointer to original value, no conversion
501 * Returns parameter value if found, NULL otherwise. Memory must be free()'d
502 * unless fetchonly is set.
505 char *get_param(PM first
, const char *name
, char replace
, int fetchonly
);
508 * Fetch a parameter value from a parameter structure, converting it to
509 * the local character set.
513 * pm - Pointer to parameter structure
514 * replace - If characters in the parameter list cannot be converted to
515 * the local character set, replace with this character.
517 * Returns a pointer to the parameter value. Memory is stored in an
518 * internal buffer, so the returned value is only valid until the next
519 * call to get_param_value() or get_param() (get_param() uses get_param_value()
522 char *get_param_value(PM pm
, char replace
);
524 extern int checksw
; /* Add Content-MD5 field */
528 * Put it here because it uses the CT typedef.
530 typedef struct mhstoreinfo
*mhstoreinfo_t
;
531 mhstoreinfo_t
mhstoreinfo_create(CT
*, char *, const char *, int, int);
532 int mhstoreinfo_files_not_clobbered(const mhstoreinfo_t
);
533 void mhstoreinfo_free(mhstoreinfo_t
);
534 void store_all_messages (mhstoreinfo_t
);