1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000-2003 Ximian Inc.
5 * Authors: Michael Zucchi <notzed@ximian.com>
6 * Jeffrey Stedfast <fejj@ximian.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU Lesser General Public
10 * License as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #ifndef _CAMEL_MIME_UTILS_H
25 #define _CAMEL_MIME_UTILS_H
30 /* maximum recommended size of a line from camel_header_fold() */
31 #define CAMEL_FOLD_SIZE (77)
32 /* maximum hard size of a line from camel_header_fold() */
33 #define CAMEL_FOLD_MAX_SIZE (998)
35 #define CAMEL_UUDECODE_STATE_INIT (0)
36 #define CAMEL_UUDECODE_STATE_BEGIN (1 << 16)
37 #define CAMEL_UUDECODE_STATE_END (1 << 17)
38 #define CAMEL_UUDECODE_STATE_MASK (CAMEL_UUDECODE_STATE_BEGIN | CAMEL_UUDECODE_STATE_END)
42 /* note, if you change this, make sure you change the 'encodings' array in camel-mime-part.c */
43 typedef enum _CamelTransferEncoding {
44 CAMEL_TRANSFER_ENCODING_DEFAULT,
45 CAMEL_TRANSFER_ENCODING_7BIT,
46 CAMEL_TRANSFER_ENCODING_8BIT,
47 CAMEL_TRANSFER_ENCODING_BASE64,
48 CAMEL_TRANSFER_ENCODING_QUOTEDPRINTABLE,
49 CAMEL_TRANSFER_ENCODING_BINARY,
50 CAMEL_TRANSFER_ENCODING_UUENCODE,
51 CAMEL_TRANSFER_NUM_ENCODINGS
52 } CamelTransferEncoding;
54 /* a list of references for this message */
55 struct _camel_header_references {
56 struct _camel_header_references *next;
60 struct _camel_header_param {
61 struct _camel_header_param *next;
66 /* describes a content-type */
70 struct _camel_header_param *params;
71 unsigned int refcount;
74 /* a raw rfc822 header */
75 /* the value MUST be US-ASCII */
76 struct _camel_header_raw {
77 struct _camel_header_raw *next;
80 int offset; /* in file, if known */
83 typedef struct _CamelContentDisposition {
85 struct _camel_header_param *params;
86 unsigned int refcount;
87 } CamelContentDisposition;
89 typedef enum _camel_header_address_t {
90 CAMEL_HEADER_ADDRESS_NONE, /* uninitialised */
91 CAMEL_HEADER_ADDRESS_NAME,
92 CAMEL_HEADER_ADDRESS_GROUP
93 } camel_header_address_t;
95 struct _camel_header_address {
96 struct _camel_header_address *next;
97 camel_header_address_t type;
101 struct _camel_header_address *members;
103 unsigned int refcount;
106 struct _camel_header_newsgroup {
107 struct _camel_header_newsgroup *next;
113 struct _camel_header_address *camel_header_address_new (void);
114 struct _camel_header_address *camel_header_address_new_name (const char *name, const char *addr);
115 struct _camel_header_address *camel_header_address_new_group (const char *name);
116 void camel_header_address_ref (struct _camel_header_address *addrlist);
117 void camel_header_address_unref (struct _camel_header_address *addrlist);
118 void camel_header_address_set_name (struct _camel_header_address *addrlist, const char *name);
119 void camel_header_address_set_addr (struct _camel_header_address *addrlist, const char *addr);
120 void camel_header_address_set_members (struct _camel_header_address *addrlist, struct _camel_header_address *group);
121 void camel_header_address_add_member (struct _camel_header_address *addrlist, struct _camel_header_address *member);
122 void camel_header_address_list_append_list (struct _camel_header_address **addrlistp, struct _camel_header_address **addrs);
123 void camel_header_address_list_append (struct _camel_header_address **addrlistp, struct _camel_header_address *addr);
124 void camel_header_address_list_clear (struct _camel_header_address **addrlistp);
126 struct _camel_header_address *camel_header_address_decode (const char *in, const char *charset);
127 struct _camel_header_address *camel_header_mailbox_decode (const char *in, const char *charset);
129 char *camel_header_address_list_encode (struct _camel_header_address *addrlist);
131 char *camel_header_address_list_format (struct _camel_header_address *addrlist);
133 /* structured header prameters */
134 struct _camel_header_param *camel_header_param_list_decode (const char *in);
135 char *camel_header_param (struct _camel_header_param *params, const char *name);
136 struct _camel_header_param *camel_header_set_param (struct _camel_header_param **paramsp, const char *name, const char *value);
137 void camel_header_param_list_format_append (GString *out, struct _camel_header_param *params);
138 char *camel_header_param_list_format (struct _camel_header_param *params);
139 void camel_header_param_list_free (struct _camel_header_param *params);
141 /* Content-Type header */
142 CamelContentType *camel_content_type_new (const char *type, const char *subtype);
143 CamelContentType *camel_content_type_decode (const char *in);
144 void camel_content_type_unref (CamelContentType *content_type);
145 void camel_content_type_ref (CamelContentType *content_type);
146 const char *camel_content_type_param (CamelContentType *content_type, const char *name);
147 void camel_content_type_set_param (CamelContentType *content_type, const char *name, const char *value);
148 int camel_content_type_is (CamelContentType *content_type, const char *type, const char *subtype);
149 char *camel_content_type_format (CamelContentType *content_type);
150 char *camel_content_type_simple (CamelContentType *content_type);
152 /* DEBUGGING function */
153 void camel_content_type_dump (CamelContentType *content_type);
155 /* Content-Disposition header */
156 CamelContentDisposition *camel_content_disposition_decode (const char *in);
157 void camel_content_disposition_ref (CamelContentDisposition *disposition);
158 void camel_content_disposition_unref (CamelContentDisposition *disposition);
159 char *camel_content_disposition_format (CamelContentDisposition *disposition);
161 /* decode the contents of a content-encoding header */
162 char *camel_content_transfer_encoding_decode (const char *in);
165 void camel_header_raw_append (struct _camel_header_raw **list, const char *name, const char *value, int offset);
166 void camel_header_raw_append_parse (struct _camel_header_raw **list, const char *header, int offset);
167 const char *camel_header_raw_find (struct _camel_header_raw **list, const char *name, int *offset);
168 const char *camel_header_raw_find_next (struct _camel_header_raw **list, const char *name, int *offset, const char *last);
169 void camel_header_raw_replace (struct _camel_header_raw **list, const char *name, const char *value, int offset);
170 void camel_header_raw_remove (struct _camel_header_raw **list, const char *name);
171 void camel_header_raw_fold (struct _camel_header_raw **list);
172 void camel_header_raw_clear (struct _camel_header_raw **list);
174 char *camel_header_raw_check_mailing_list (struct _camel_header_raw **list);
177 char *camel_header_address_fold (const char *in, size_t headerlen);
178 char *camel_header_fold (const char *in, size_t headerlen);
179 char *camel_header_unfold (const char *in);
181 /* decode a header which is a simple token */
182 char *camel_header_token_decode (const char *in);
184 int camel_header_decode_int (const char **in);
186 /* decode/encode a string type, like a subject line */
187 char *camel_header_decode_string (const char *in, const char *default_charset);
188 char *camel_header_encode_string (const unsigned char *in);
190 /* decode (text | comment) - a one-way op */
191 char *camel_header_format_ctext (const char *in, const char *default_charset);
193 /* encode a phrase, like the real name of an address */
194 char *camel_header_encode_phrase (const unsigned char *in);
196 /* FIXME: these are the only 2 functions in this header which are ch_(action)_type
197 rather than ch_type_(action) */
199 /* decode an email date field into a GMT time, + optional offset */
200 time_t camel_header_decode_date (const char *in, int *saveoffset);
201 char *camel_header_format_date (time_t time, int offset);
203 /* decode a message id */
204 char *camel_header_msgid_decode (const char *in);
205 char *camel_header_contentid_decode (const char *in);
207 /* generate msg id */
208 char *camel_header_msgid_generate (void);
210 /* decode a References or In-Reply-To header */
211 struct _camel_header_references *camel_header_references_inreplyto_decode (const char *in);
212 struct _camel_header_references *camel_header_references_decode (const char *in);
213 void camel_header_references_list_clear (struct _camel_header_references **list);
214 void camel_header_references_list_append_asis (struct _camel_header_references **list, char *ref);
215 int camel_header_references_list_size (struct _camel_header_references **list);
216 struct _camel_header_references *camel_header_references_dup (const struct _camel_header_references *list);
218 /* decode content-location */
219 char *camel_header_location_decode (const char *in);
222 struct _camel_header_newsgroup *camel_header_newsgroups_decode(const char *in);
223 void camel_header_newsgroups_free(struct _camel_header_newsgroup *ng);
225 const char *camel_transfer_encoding_to_string (CamelTransferEncoding encoding);
226 CamelTransferEncoding camel_transfer_encoding_from_string (const char *string);
228 /* decode the mime-type header */
229 void camel_header_mime_decode (const char *in, int *maj, int *min);
231 /* do incremental base64/quoted-printable (de/en)coding */
232 size_t camel_base64_decode_step (unsigned char *in, size_t len, unsigned char *out, int *state, unsigned int *save);
234 size_t camel_base64_encode_step (unsigned char *in, size_t len, gboolean break_lines, unsigned char *out, int *state, int *save);
235 size_t camel_base64_encode_close (unsigned char *in, size_t len, gboolean break_lines, unsigned char *out, int *state, int *save);
237 size_t camel_uudecode_step (unsigned char *in, size_t len, unsigned char *out, int *state, guint32 *save);
239 size_t camel_uuencode_step (unsigned char *in, size_t len, unsigned char *out, unsigned char *uubuf, int *state,
241 size_t camel_uuencode_close (unsigned char *in, size_t len, unsigned char *out, unsigned char *uubuf, int *state,
244 size_t camel_quoted_decode_step (unsigned char *in, size_t len, unsigned char *out, int *savestate, int *saveme);
246 size_t camel_quoted_encode_step (unsigned char *in, size_t len, unsigned char *out, int *state, int *save);
247 size_t camel_quoted_encode_close (unsigned char *in, size_t len, unsigned char *out, int *state, int *save);
249 char *camel_base64_encode_simple (const char *data, size_t len);
250 size_t camel_base64_decode_simple (char *data, size_t len);
252 /* camel ctype type functions for rfc822/rfc2047/other, which are non-locale specific */
254 CAMEL_MIME_IS_CTRL = 1<<0,
255 CAMEL_MIME_IS_LWSP = 1<<1,
256 CAMEL_MIME_IS_TSPECIAL = 1<<2,
257 CAMEL_MIME_IS_SPECIAL = 1<<3,
258 CAMEL_MIME_IS_SPACE = 1<<4,
259 CAMEL_MIME_IS_DSPECIAL = 1<<5,
260 CAMEL_MIME_IS_QPSAFE = 1<<6,
261 CAMEL_MIME_IS_ESAFE = 1<<7, /* encoded word safe */
262 CAMEL_MIME_IS_PSAFE = 1<<8, /* encoded word in phrase safe */
263 CAMEL_MIME_IS_ATTRCHAR = 1<<9, /* attribute-char safe (rfc2184) */
266 extern unsigned short camel_mime_special_table[256];
268 #define camel_mime_is_ctrl(x) ((camel_mime_special_table[(unsigned char)(x)] & CAMEL_MIME_IS_CTRL) != 0)
269 #define camel_mime_is_lwsp(x) ((camel_mime_special_table[(unsigned char)(x)] & CAMEL_MIME_IS_LWSP) != 0)
270 #define camel_mime_is_tspecial(x) ((camel_mime_special_table[(unsigned char)(x)] & CAMEL_MIME_IS_TSPECIAL) != 0)
271 #define camel_mime_is_type(x, t) ((camel_mime_special_table[(unsigned char)(x)] & (t)) != 0)
272 #define camel_mime_is_ttoken(x) ((camel_mime_special_table[(unsigned char)(x)] & (CAMEL_MIME_IS_TSPECIAL|CAMEL_MIME_IS_LWSP|CAMEL_MIME_IS_CTRL)) == 0)
273 #define camel_mime_is_atom(x) ((camel_mime_special_table[(unsigned char)(x)] & (CAMEL_MIME_IS_SPECIAL|CAMEL_MIME_IS_SPACE|CAMEL_MIME_IS_CTRL)) == 0)
274 #define camel_mime_is_dtext(x) ((camel_mime_special_table[(unsigned char)(x)] & CAMEL_MIME_IS_DSPECIAL) == 0)
275 #define camel_mime_is_fieldname(x) ((camel_mime_special_table[(unsigned char)(x)] & (CAMEL_MIME_IS_CTRL|CAMEL_MIME_IS_SPACE)) == 0)
276 #define camel_mime_is_qpsafe(x) ((camel_mime_special_table[(unsigned char)(x)] & CAMEL_MIME_IS_QPSAFE) != 0)
277 #define camel_mime_is_especial(x) ((camel_mime_special_table[(unsigned char)(x)] & CAMEL_MIME_IS_ESPECIAL) != 0)
278 #define camel_mime_is_psafe(x) ((camel_mime_special_table[(unsigned char)(x)] & CAMEL_MIME_IS_PSAFE) != 0)
279 #define camel_mime_is_attrchar(x) ((camel_mime_special_table[(unsigned char)(x)] & CAMEL_MIME_IS_ATTRCHAR) != 0)
283 #endif /* ! _CAMEL_MIME_UTILS_H */