Initialize the gmime for upstream
[platform/upstream/gmime.git] / gmime / gmime-encodings.h
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 2000-2012 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_ENCODINGS_H__
23 #define __GMIME_ENCODINGS_H__
24
25 #include <glib.h>
26 #include <sys/types.h>
27
28 G_BEGIN_DECLS
29
30
31 /**
32  * GMimeContentEncoding:
33  * @GMIME_CONTENT_ENCODING_DEFAULT: Default transfer encoding.
34  * @GMIME_CONTENT_ENCODING_7BIT: 7bit text transfer encoding.
35  * @GMIME_CONTENT_ENCODING_8BIT: 8bit text transfer encoding.
36  * @GMIME_CONTENT_ENCODING_BINARY: Binary transfer encoding.
37  * @GMIME_CONTENT_ENCODING_BASE64: Base64 transfer encoding.
38  * @GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE: Quoted-printable transfer encoding.
39  * @GMIME_CONTENT_ENCODING_UUENCODE: Uuencode transfer encoding.
40  *
41  * A Content-Transfer-Encoding enumeration.
42  **/
43 typedef enum {
44         GMIME_CONTENT_ENCODING_DEFAULT,
45         GMIME_CONTENT_ENCODING_7BIT,
46         GMIME_CONTENT_ENCODING_8BIT,
47         GMIME_CONTENT_ENCODING_BINARY,
48         GMIME_CONTENT_ENCODING_BASE64,
49         GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE,
50         GMIME_CONTENT_ENCODING_UUENCODE
51 } GMimeContentEncoding;
52
53
54 /**
55  * GMimeEncodingConstraint:
56  * @GMIME_ENCODING_CONSTRAINT_7BIT: The stream data must fit within the 7bit ASCII range.
57  * @GMIME_ENCODING_CONSTRAINT_8BIT: The stream data may have bytes with the high bit set, but no null bytes.
58  * @GMIME_ENCODING_CONSTRAINT_BINARY: The stream may contain any binary data.
59  *
60  * Used with functions like g_mime_filter_best_encoding() and
61  * g_mime_object_encode() as the 'constraint' argument. These values
62  * provide a means of letting the filter know what the encoding
63  * constraints are for the stream.
64  **/
65 typedef enum {
66         GMIME_ENCODING_CONSTRAINT_7BIT,
67         GMIME_ENCODING_CONSTRAINT_8BIT,
68         GMIME_ENCODING_CONSTRAINT_BINARY
69 } GMimeEncodingConstraint;
70
71
72 GMimeContentEncoding g_mime_content_encoding_from_string (const char *str);
73 const char *g_mime_content_encoding_to_string (GMimeContentEncoding encoding);
74
75
76 /**
77  * GMIME_BASE64_ENCODE_LEN:
78  * @x: Length of the input data to encode
79  *
80  * Calculates the maximum number of bytes needed to base64 encode the
81  * full input buffer of length @x.
82  *
83  * Returns: the number of output bytes needed to base64 encode an input
84  * buffer of size @x.
85  **/
86 #define GMIME_BASE64_ENCODE_LEN(x) ((size_t) (((((x) + 2) / 57) * 77) + 77))
87
88
89 /**
90  * GMIME_QP_ENCODE_LEN:
91  * @x: Length of the input data to encode
92  *
93  * Calculates the maximum number of bytes needed to encode the full
94  * input buffer of length @x using the quoted-printable encoding.
95  *
96  * Returns: the number of output bytes needed to encode an input buffer
97  * of size @x using the quoted-printable encoding.
98  **/
99 #define GMIME_QP_ENCODE_LEN(x)     ((size_t) ((((x) / 24) * 74) + 74))
100
101
102 /**
103  * GMIME_UUENCODE_LEN:
104  * @x: Length of the input data to encode
105  *
106  * Calculates the maximum number of bytes needed to uuencode the full
107  * input buffer of length @x.
108  *
109  * Returns: the number of output bytes needed to uuencode an input
110  * buffer of size @x.
111  **/
112 #define GMIME_UUENCODE_LEN(x)      ((size_t) (((((x) + 2) / 45) * 62) + 64))
113
114
115 /**
116  * GMIME_UUDECODE_STATE_INIT:
117  *
118  * Initial state for the g_mime_encoding_uudecode_step() function.
119  **/
120 #define GMIME_UUDECODE_STATE_INIT   (0)
121
122
123 /**
124  * GMIME_UUDECODE_STATE_BEGIN:
125  *
126  * State for the g_mime_encoding_uudecode_step() function, denoting that
127  * the 'begin' line has been found.
128  **/
129 #define GMIME_UUDECODE_STATE_BEGIN  (1 << 16)
130
131
132 /**
133  * GMIME_UUDECODE_STATE_END:
134  *
135  * State for the g_mime_encoding_uudecode_step() function, denoting that
136  * the end of the UU encoded block has been found.
137  **/
138 #define GMIME_UUDECODE_STATE_END    (1 << 17)
139 #define GMIME_UUDECODE_STATE_MASK   (GMIME_UUDECODE_STATE_BEGIN | GMIME_UUDECODE_STATE_END)
140
141
142 /**
143  * GMimeEncoding:
144  * @encoding: the type of encoding
145  * @uubuf: a temporary buffer needed when uuencoding data
146  * @encode: %TRUE if encoding or %FALSE if decoding
147  * @save: saved bytes from the previous step
148  * @state: current encder/decoder state
149  *
150  * A context used for encoding or decoding data.
151  **/
152 typedef struct _GMimeEncoding {
153         GMimeContentEncoding encoding;
154         unsigned char uubuf[60];
155         gboolean encode;
156         guint32 save;
157         int state;
158 } GMimeEncoding;
159
160
161 void g_mime_encoding_init_encode (GMimeEncoding *state, GMimeContentEncoding encoding);
162 void g_mime_encoding_init_decode (GMimeEncoding *state, GMimeContentEncoding encoding);
163 void g_mime_encoding_reset (GMimeEncoding *state);
164
165 size_t g_mime_encoding_outlen (GMimeEncoding *state, size_t inlen);
166
167 size_t g_mime_encoding_step (GMimeEncoding *state, const char *inbuf, size_t inlen, char *outbuf);
168 size_t g_mime_encoding_flush (GMimeEncoding *state, const char *inbuf, size_t inlen, char *outbuf);
169
170
171 /* do incremental base64 (de/en)coding */
172 size_t g_mime_encoding_base64_decode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save);
173 size_t g_mime_encoding_base64_encode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save);
174 size_t g_mime_encoding_base64_encode_close (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save);
175
176 /* do incremental uu (de/en)coding */
177 size_t g_mime_encoding_uudecode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save);
178 size_t g_mime_encoding_uuencode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, unsigned char *uubuf, int *state, guint32 *save);
179 size_t g_mime_encoding_uuencode_close (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, unsigned char *uubuf, int *state, guint32 *save);
180
181 /* do incremental quoted-printable (de/en)coding */
182 size_t g_mime_encoding_quoted_decode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save);
183 size_t g_mime_encoding_quoted_encode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save);
184 size_t g_mime_encoding_quoted_encode_close (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save);
185
186
187 G_END_DECLS
188
189 #endif /* __GMIME_ENCODINGS_H__ */