summaryrefslogtreecommitdiffstats
path: root/branches/1.0/tinyMEDIA/ragel/tmedia_content_cpim.rl
blob: c43f972d6e7f7b65e9060cd5080e3505f90d9dcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright (C) 2009-2010 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tmedia_content_cpim.c
 * @brief Common Presence and Instant Messaging (CPIM): Message Format (RFC 3862)
 *
 * @author Mamadou Diop <diopmamadou(at)doubango.org>
 *
 */
#include "tinymedia/content/tmedia_content_cpim.h"

#include "tsk_debug.h"
#include "tsk_memory.h"
#include "tsk_string.h"
#include "tsk_ragel_state.h"

#include <string.h>

/*	RFC 3862 - 2. Overall Message Structure
A complete message looks something like this:

      m: Content-type: Message/CPIM
      s:
      h: (message-metadata-headers)
      s:
      e: (encapsulated MIME message-body)

   The end of the message body is defined by the framing mechanism of
   the protocol used.  The tags 'm:', 's:', 'h:', 'e:', and 'x:' are not
   part of the message format and are used here to indicate the
   different parts of the message, thus:

      m:  MIME headers for the overall message
      s:  a blank separator line
      h:  message headers
      e:  encapsulated MIME object containing the message content
      x:  MIME security multipart message wrapper
*/





/***********************************
*	Ragel state machine.
*/
%%{
	machine tmedia_machine_content_cpim;

	# Includes
	include tmedia_machine_utils "./ragel/tmedia_machine_utils.rl";
	
	action tag{
		tag_start = p;
	}
	
	action is_parsing_mime_headers{
		parsing_mime_headers = tsk_true;
	}

	action is_parsing_message_headers{
		parsing_mime_headers = tsk_false;
	}

	action parse_hname{
		TSK_PARSER_SET_STRING(hname);
	}
	
	action parse_hvalue{
		tmedia_content_header_t* header;
		TSK_PARSER_SET_STRING(hvalue);
		header = tmedia_content_header_create(hname, hvalue);
		TSK_FREE(hname); TSK_FREE(hvalue);

		if(parsing_mime_headers){
			if(!TMEDIA_CONTENT_CPIM(self)->m_headers){
				TMEDIA_CONTENT_CPIM(self)->m_headers = tsk_list_create();
			}
			tsk_list_push_back_data(TMEDIA_CONTENT_CPIM(self)->m_headers, (void**)&header);
		}
		else{
			if(!TMEDIA_CONTENT_CPIM(self)->h_headers){
				TMEDIA_CONTENT_CPIM(self)->h_headers = tsk_list_create();
			}
			tsk_list_push_back_data(TMEDIA_CONTENT_CPIM(self)->h_headers, (void**)&header);
		}
	}

	action parse_e{
		int len = (int)(p  - tag_start);
		if(len && tag_start){
			if(TMEDIA_CONTENT_CPIM(self)->e){
				TSK_OBJECT_SAFE_FREE(TMEDIA_CONTENT_CPIM(self)->e); \
			}
			TMEDIA_CONTENT_CPIM(self)->e = tsk_buffer_create(tag_start, len);
		}
	}
	
	hname = token>tag %parse_hname;
	hvalue = any*>tag %parse_hvalue;

	Header = hname :>SP*:> ":" SP*<: hvalue :> CRLF;
	m = Header+ >is_parsing_mime_headers;
	s = CRLF;	
	h = Header+ >is_parsing_message_headers;
	e = any*>tag %parse_e;
	
	# Entry point
	main := m s h s e;
}%%


static int tmedia_content_cpim_parse(tmedia_content_t* self, const void* in_data, tsk_size_t in_size)
{
	int cs = 0;
	const char *p = in_data;
	const char *pe = p + in_size;
	const char *eof = pe;
	
	const char *tag_start;

	char* hname = tsk_null;
	char* hvalue = tsk_null;
	tsk_bool_t parsing_mime_headers = tsk_true;

	%%write data;
	%%write init;
	%%write exec;
	
	TSK_FREE(hname);
	TSK_FREE(hvalue);

	if( cs < %%{ write first_final; }%% ){
		TSK_DEBUG_ERROR("Failed to parse CPIM content");
		return -1;
	}
	
	return 0;
}

static tsk_buffer_t* tmedia_content_cpim_get_data(tmedia_content_t* self)
{
	tsk_buffer_t* data = tsk_buffer_create_null();
	tmedia_content_cpim_t *cpim = TMEDIA_CONTENT_CPIM(self);
	const tsk_list_item_t* item;
	  /*
		  m: Content-type: Message/CPIM
		  s:
		  h: (message-metadata-headers)
		  s:
		  e: (encapsulated MIME message-body)
		  x:  MIME security multipart message wrapper
	  */
	if(cpim->m_headers){
		tsk_list_foreach(item, cpim->m_headers){
			char* hstring = tmedia_content_header_tostring(TMEDIA_CONTENT_HEADER(item->data));
			tsk_buffer_append_2(data, TSK_LIST_IS_LAST(cpim->m_headers, item) ? "%s\r\n\r\n" : "%s\r\n", hstring);
			TSK_FREE(hstring);
		}
	}
	if(cpim->h_headers){
		tsk_list_foreach(item, cpim->h_headers){
			char* hstring = tmedia_content_header_tostring(TMEDIA_CONTENT_HEADER(item->data));
			tsk_buffer_append_2(data, TSK_LIST_IS_LAST(cpim->h_headers, item) ? "%s\r\n\r\n" : "%s\r\n", hstring);
			TSK_FREE(hstring);
		}
	}
	if(cpim->e){
		tsk_buffer_append(data, TSK_BUFFER_DATA(cpim->e), TSK_BUFFER_SIZE(cpim->e));
	}
	if(cpim->x){
		tsk_buffer_append(data, TSK_BUFFER_DATA(cpim->x), TSK_BUFFER_SIZE(cpim->x));
	}

	return data;
}

//=================================================================================================
//	object/plugin definitions
//
/* constructor */
static tsk_object_t* tmedia_content_cpim_ctor(tsk_object_t * self, va_list * app)
{
	tmedia_content_cpim_t *cpim = self;
	if(cpim){
		/* init base: called by tmedia_content_create() */
		/* init self */
	}
	return self;
}
/* destructor */
static tsk_object_t* tmedia_content_cpim_dtor(tsk_object_t * self)
{ 
	tmedia_content_cpim_t *cpim = self;
	if(cpim){
		/* deinit base */
		tmedia_content_deinit(TMEDIA_CONTENT(cpim));
		/* deinit self */
		TSK_OBJECT_SAFE_FREE(cpim->m_headers);
		TSK_OBJECT_SAFE_FREE(cpim->h_headers);
		TSK_OBJECT_SAFE_FREE(cpim->e);
		TSK_OBJECT_SAFE_FREE(cpim->x);
	}

	return self;
}
/* object definition */
static const tsk_object_def_t tmedia_content_cpim_def_s = 
{
	sizeof(tmedia_content_cpim_t),
	tmedia_content_cpim_ctor, 
	tmedia_content_cpim_dtor,
	tsk_null, 
};
/* plugin definition*/
static const tmedia_content_plugin_def_t tmedia_content_cpim_plugin_def_s = 
{
	&tmedia_content_cpim_def_s,

	TMEDIA_CONTENT_CPIM_TYPE,
	tmedia_content_cpim_parse,
	tmedia_content_cpim_get_data
};
const tmedia_content_plugin_def_t *tmedia_content_cpim_plugin_def_t = &tmedia_content_cpim_plugin_def_s;
OpenPOWER on IntegriCloud