1 /* $Id: decode.h,v 1.21 2011/06/28 00:13:48 sbajic Exp $ */
2 
3 /*
4  DSPAM
5  COPYRIGHT (C) 2002-2012 DSPAM PROJECT
6 
7  This program is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Affero General Public License as
9  published by the Free Software Foundation, either version 3 of the
10  License, or (at your option) any later version.
11 
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
15  GNU Affero General Public License for more details.
16 
17  You should have received a copy of the GNU Affero General Public License
18  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 
20 */
21 
22 #include <stdio.h>
23 #include <string.h>
24 #ifdef HAVE_UNISTD_H
25 #   include <unistd.h>
26 #endif
27 
28 #include "nodetree.h"
29 #include "buffer.h"
30 
31 #ifndef _DECODE_H
32 #define _DECODE_H
33 
34 /*
35  * _ds_header_field
36  *
37  * DESCRIPTION
38  *   a single header/value paid from a message block
39  */
40 
41 typedef struct _ds_header_field
42 {
43   char *heading;
44   char *data;
45   char *original_data;		/* prior to decoding */
46   char *concatenated_data;	/* multi-line */
47 } *ds_header_t;
48 
49 /*
50  * _ds_message_part
51  *
52  * DESCRIPTION
53  *   a message block (or part) within a message. in a single-part message,
54  *   there will be only one block (block 0). in a multipart message, each part
55  *   will be separated into a separte block. the message block consists of:
56  *    - a dynamic array of headers (nodetree of ds_header_t's) for the block
57  *    - body data (NULL if there is no body)
58  *    - block encoding
59  *    - block media type information
60  *    - boundary and terminating boundary information
61  */
62 
63 typedef struct _ds_message_part
64 {
65   struct nt *	headers;
66   buffer *	body;
67   buffer *	original_signed_body;
68   char *	boundary;
69   char *	terminating_boundary;
70   int		encoding;
71   int 		original_encoding;
72   int 		media_type;
73   int 		media_subtype;
74   int           content_disposition;
75 } *ds_message_part_t;
76 
77 /*
78  * _ds_message
79  *
80  * DESCRIPTION
81  *   the actual message structure, comprised of an array of message blocks.
82  *   in a non-multipart email, there will only be one message block (block 0).
83  *   in multipart emails, however, the first message block will represent the
84  *   header (with a NULL body_data or something like "This is a multi-part
85  *   message"), and each additional block within the email will be given its
86  *   own message_part structure with its own headers, boundary, etc.
87  *
88  *   embedded multipart messages are not realized by the structure, but can
89  *   be identified by examining the media type or headers.
90  */
91 
92 typedef struct _ds_message
93 {
94   struct nt *	components;
95   int protect;
96 } *ds_message_t;
97 
98 /* adapter dependent functions */
99 
100 char *  _ds_decode_base64       (const char *body);
101 char *  _ds_decode_quoted       (const char *body);
102 
103 /* Adapter-independent functions */
104 
105 ds_message_t _ds_actualize_message (const char *message);
106 
107 char *  _ds_assemble_message (ds_message_t message, const char *newline);
108 char *  _ds_find_header (ds_message_t message, const char *heading);
109 
110 ds_message_part_t _ds_create_message_part (void);
111 ds_header_t        _ds_create_header_field  (const char *heading);
112 void               _ds_analyze_header
113   (ds_message_part_t block, ds_header_t  header, struct nt *boundaries);
114 
115 void _ds_destroy_message	(ds_message_t message);
116 void _ds_destroy_headers	(ds_message_part_t block);
117 void _ds_destroy_block		(ds_message_part_t block);
118 
119 char *	_ds_decode_block	(ds_message_part_t block);
120 int	_ds_encode_block	(ds_message_part_t block, int encoding);
121 char *	_ds_encode_base64	(const char *body);
122 char *	_ds_encode_quoted	(const char *body);
123 char *	_ds_decode_hex8bit	(const char *body);
124 int     _ds_decode_headers      (ds_message_part_t block);
125 
126 int	_ds_push_boundary	(struct nt *stack, const char *boundary);
127 int	_ds_match_boundary	(struct nt *stack, const char *buff);
128 int     _ds_extract_boundary    (char *buf, size_t size, char *data);
129 char *	_ds_pop_boundary	(struct nt *stack);
130 
131 char *	_ds_strip_html		(const char *html);
132 int	_ds_hex2dec		(unsigned char hex);
133 
134 /* Encoding values */
135 
136 #define EN_7BIT			0x00
137 #define EN_8BIT 		0x01
138 #define EN_QUOTED_PRINTABLE	0x02
139 #define EN_BASE64		0x03
140 #define EN_BINARY		0x04
141 #define EN_UNKNOWN		0xFE
142 #define EN_OTHER		0xFF
143 
144 /* Media types which are relevant to DSPAM */
145 
146 #define MT_TEXT			0x00
147 #define MT_MULTIPART		0x01
148 #define MT_MESSAGE		0x02
149 #define MT_APPLICATION		0x03
150 #define MT_UNKNOWN		0xFE
151 #define MT_OTHER		0xFF
152 
153 /* Media subtypes which are relevant to DSPAM */
154 
155 #define MST_PLAIN		0x00
156 #define	MST_HTML		0x01
157 #define MST_MIXED		0x02
158 #define MST_ALTERNATIVE		0x03
159 #define MST_RFC822		0x04
160 #define MST_DSPAM_SIGNATURE	0x05
161 #define MST_SIGNED		0x06
162 #define MST_INOCULATION		0x07
163 #define MST_ENCRYPTED		0x08
164 #define MST_UNKNOWN		0xFE
165 #define MST_OTHER		0xFF
166 
167 /* Part Content-Dispositions */
168 
169 #define PCD_INLINE		0x00
170 #define PCD_ATTACHMENT		0x01
171 #define PCD_UNKNOWN		0xFE
172 #define PCD_OTHER		0xFF
173 
174 /* Block position; used when analyzing a message */
175 
176 #define BP_HEADER		0x00
177 #define BP_BODY			0x01
178 
179 #endif /* _DECODE_H */
180 
181