1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 2000-2009 Jeffrey Stedfast
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public License
7  *  as published by the Free Software Foundation; either version 2.1
8  *  of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free
17  *  Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
18  *  02110-1301, USA.
19  */
20 
21 
22 #ifndef __GMIME_UTILS_H__
23 #define __GMIME_UTILS_H__
24 
25 #include <glib.h>
26 #include <time.h>
27 #include <stdarg.h>
28 
29 G_BEGIN_DECLS
30 
31 
32 /**
33  * GMimePartEncodingType:
34  * @GMIME_PART_ENCODING_DEFAULT: Default transfer encoding.
35  * @GMIME_PART_ENCODING_7BIT: 7bit text transfer encoding.
36  * @GMIME_PART_ENCODING_8BIT: 8bit text transfer encoding.
37  * @GMIME_PART_ENCODING_BINARY: Binary transfer encoding.
38  * @GMIME_PART_ENCODING_BASE64: Base64 transfer encoding.
39  * @GMIME_PART_ENCODING_QUOTEDPRINTABLE: Quoted-printable transfer encoding.
40  * @GMIME_PART_ENCODING_UUENCODE: Uuencode transfer encoding.
41  * @GMIME_PART_NUM_ENCODINGS: The number of available transfer encoding enum values.
42  *
43  * A Content-Transfer-Encoding type.
44  **/
45 typedef enum {
46 	GMIME_PART_ENCODING_DEFAULT,
47 	GMIME_PART_ENCODING_7BIT,
48 	GMIME_PART_ENCODING_8BIT,
49 	GMIME_PART_ENCODING_BINARY,
50 	GMIME_PART_ENCODING_BASE64,
51 	GMIME_PART_ENCODING_QUOTEDPRINTABLE,
52 	GMIME_PART_ENCODING_UUENCODE,
53 	GMIME_PART_NUM_ENCODINGS
54 } GMimePartEncodingType;
55 
56 
57 typedef struct _GMimeReferences GMimeReferences;
58 
59 /**
60  * GMimeReferences:
61  * @next: Pointer to the next reference.
62  * @msgid: Reference message-id.
63  *
64  * A List of references, as per the References or In-Reply-To header
65  * fields.
66  **/
67 struct _GMimeReferences {
68 	GMimeReferences *next;
69 	char *msgid;
70 };
71 
72 
73 /**
74  * GMIME_BASE64_ENCODE_LEN:
75  * @x: Length of the input data to encode
76  *
77  * Calculates the maximum number of bytes needed to base64 encode the
78  * full input buffer of length @x.
79  *
80  * Returns the number of output bytes needed to base64 encode an input
81  * buffer of size @x.
82  **/
83 #define GMIME_BASE64_ENCODE_LEN(x) ((size_t) (((((x) + 2) / 57) * 77) + 77))
84 
85 
86 /**
87  * GMIME_QP_ENCODE_LEN:
88  * @x: Length of the input data to encode
89  *
90  * Calculates the maximum number of bytes needed to encode the full
91  * input buffer of length @x using the quoted-printable encoding.
92  *
93  * Returns the number of output bytes needed to encode an input buffer
94  * of size @x using the quoted-printable encoding.
95  **/
96 #define GMIME_QP_ENCODE_LEN(x)     ((size_t) ((((x) / 24) * 74) + 74))
97 
98 
99 /**
100  * GMIME_UUENCODE_LEN:
101  * @x: Length of the input data to encode
102  *
103  * Calculates the maximum number of bytes needed to uuencode the full
104  * input buffer of length @x.
105  *
106  * Returns the number of output bytes needed to uuencode an input
107  * buffer of size @x.
108  **/
109 #define GMIME_UUENCODE_LEN(x)      ((size_t) (((((x) + 2) / 45) * 62) + 64))
110 
111 
112 /**
113  * GMIME_UUDECODE_STATE_INIT:
114  *
115  * Initial state for the g_mime_utils_uudecode_step() function.
116  **/
117 #define GMIME_UUDECODE_STATE_INIT   (0)
118 
119 
120 /**
121  * GMIME_UUDECODE_STATE_BEGIN:
122  *
123  * State for the g_mime_utils_uudecode_step() function, denoting that
124  * the 'begin' line has been found.
125  **/
126 #define GMIME_UUDECODE_STATE_BEGIN  (1 << 16)
127 
128 
129 /**
130  * GMIME_UUDECODE_STATE_END:
131  *
132  * State for the g_mime_utils_uudecode_step() function, denoting that
133  * the end of the UU encoded block has been found.
134  **/
135 #define GMIME_UUDECODE_STATE_END    (1 << 17)
136 #define GMIME_UUDECODE_STATE_MASK   (GMIME_UUDECODE_STATE_BEGIN | GMIME_UUDECODE_STATE_END)
137 
138 time_t g_mime_utils_header_decode_date (const char *in, int *tz_offset);
139 char  *g_mime_utils_header_format_date (time_t date, int tz_offset);
140 
141 char *g_mime_utils_generate_message_id (const char *fqdn);
142 
143 /* decode a message-id */
144 char *g_mime_utils_decode_message_id (const char *message_id);
145 
146 /* decode a References or In-Reply-To header */
147 GMimeReferences *g_mime_references_decode (const char *text);
148 void g_mime_references_append (GMimeReferences **refs, const char *msgid);
149 void g_mime_references_clear (GMimeReferences **refs);
150 GMimeReferences *g_mime_references_next (const GMimeReferences *ref);
151 
152 char  *g_mime_utils_structured_header_fold (const char *in);
153 char  *g_mime_utils_unstructured_header_fold (const char *in);
154 char  *g_mime_utils_header_fold (const char *in);
155 char  *g_mime_utils_header_printf (const char *format, ...);
156 
157 char  *g_mime_utils_quote_string (const char *string);
158 void   g_mime_utils_unquote_string (char *string);
159 
160 /* encoding decision making utilities ;-) */
161 gboolean g_mime_utils_text_is_8bit (const unsigned char *text, size_t len);
162 GMimePartEncodingType g_mime_utils_best_encoding (const unsigned char *text, size_t len);
163 
164 /* utility function to convert text in an unknown 8bit/multibyte charset to UTF-8 */
165 char *g_mime_utils_decode_8bit (const char *text, size_t len);
166 
167 /* utilities to (de/en)code headers */
168 char *g_mime_utils_header_decode_text (const char *in);
169 char *g_mime_utils_header_encode_text (const char *in);
170 
171 char *g_mime_utils_header_decode_phrase (const char *in);
172 char *g_mime_utils_header_encode_phrase (const char *in);
173 
174 #ifndef GMIME_DISABLE_DEPRECATED
175 char *g_mime_utils_8bit_header_decode (const unsigned char *in);
176 char *g_mime_utils_8bit_header_encode (const unsigned char *in);
177 char *g_mime_utils_8bit_header_encode_phrase (const unsigned char *in);
178 #endif
179 
180 /* do incremental base64 (de/en)coding */
181 size_t g_mime_utils_base64_decode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, guint32 *save);
182 size_t g_mime_utils_base64_encode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, guint32 *save);
183 size_t g_mime_utils_base64_encode_close (const unsigned char *in, size_t inlen, unsigned char *out, int *state, guint32 *save);
184 
185 /* do incremental uu (de/en)coding */
186 size_t g_mime_utils_uudecode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, guint32 *save);
187 size_t g_mime_utils_uuencode_step (const unsigned char *in, size_t inlen, unsigned char *out, unsigned char *uubuf, int *state, guint32 *save);
188 size_t g_mime_utils_uuencode_close (const unsigned char *in, size_t inlen, unsigned char *out, unsigned char *uubuf, int *state, guint32 *save);
189 
190 /* do incremental quoted-printable (de/en)coding */
191 size_t g_mime_utils_quoted_decode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, guint32 *save);
192 size_t g_mime_utils_quoted_encode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, guint32 *save);
193 size_t g_mime_utils_quoted_encode_close (const unsigned char *in, size_t inlen, unsigned char *out, int *state, guint32 *save);
194 
195 
196 G_END_DECLS
197 
198 #endif /* __GMIME_UTILS_H__ */
199