2.0_alpha release commit
[framework/messaging/email-service.git] / email-ipc / email-ipc-api / email-ipc-param-list.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "email-ipc-build.h"
27 #include "email-ipc-param-list.h"
28
29 #include "email-debug-log.h"
30 #include "email-utilities.h"
31
32 /*  stream */
33 /*  +----------------------------------------------------------------------------------------------------------+ */
34 /*  | API ID(4B) | Resp. ID (4B) | Param Count(4B) | Param1 Len | Param1 Data | ... | ParamN Len | ParamN data |  */
35 /*  +----------------------------------------------------------------------------------------------------------+ */
36
37
38 EXPORT_API emipc_param_list *emipc_create_param_list()
39 {
40         emipc_param_list *new_param_list = NULL;
41
42         new_param_list = (emipc_param_list *) em_malloc (sizeof(emipc_param_list));
43         if (new_param_list == NULL) {
44                 EM_DEBUG_EXCEPTION("em_malloc failed.");
45                 return NULL;
46         }
47         memset(new_param_list, 0x00, sizeof(emipc_param_list));
48
49         return new_param_list;
50 }
51
52 EXPORT_API bool emipc_destroy_param_list(emipc_param_list *param_list)
53 {
54         int count = 10;
55         int index = 0;
56
57         if (!param_list) {
58                 EM_DEBUG_EXCEPTION("Invalid parameter.");
59                 return false;
60         }
61
62         for (index = 0; index < count; index++) {
63                 emipc_free_param(param_list->params[index]);
64         }
65         EM_SAFE_FREE(param_list->byte_stream);
66         EM_SAFE_FREE(param_list);
67         return true;
68 }
69
70 /* making stream into param length and param data */
71 EXPORT_API bool emipc_parse_stream_of_param_list(emipc_param_list *param_list, void *stream)
72 {
73         EM_DEBUG_FUNC_BEGIN();
74         long parameter_count = *((long *)stream + eSTREAM_COUNT);
75         if(parameter_count <= 0) {
76                 EM_DEBUG_EXCEPTION("INVALID_PARAM : count %d", parameter_count);
77                 return false;
78         }
79
80         unsigned char* cur = ((unsigned char*)stream) + sizeof(int)*eSTREAM_DATA;
81
82         int i = 0;
83         /* stream is composed of data type which is encoded into length and data field */
84         int len = 0;
85         for(i = 0; i < parameter_count; i++) {
86                 /* reading length */
87                 memcpy(&len, cur, sizeof(int));
88
89                 /* moving from length field to data field */
90                 cur += sizeof(int);
91                 emipc_add_param_to_param_list(param_list, (void*)cur, len);
92
93                 EM_DEBUG_LOG("Parsing stream : element %d is %dbyte long ", i, len);
94
95                 /*  move to next parameter       */
96                 cur += len;
97         }
98
99         EM_DEBUG_FUNC_END();
100         return true;
101 }
102
103 EXPORT_API unsigned char *emipc_serialize_param_list(emipc_param_list *param_list, int *stream_length)
104 {
105         EM_DEBUG_FUNC_BEGIN();
106         if(param_list->byte_stream)
107                 return param_list->byte_stream;
108
109         int stream_len = emipc_sum_param_list_length (param_list);
110
111         if (stream_len <= 0) {
112                 EM_DEBUG_EXCEPTION("stream_len error %d", stream_len);
113                 EM_SAFE_FREE(param_list->byte_stream);
114                 return NULL;
115         }
116
117                 param_list->byte_stream = (unsigned char*)calloc(1, stream_len);
118                 int pos = sizeof(long)*eSTREAM_COUNT;
119
120                 if (pos + (int)sizeof(param_list->param_count) > stream_len ) {
121                         EM_DEBUG_EXCEPTION("%d > stream_len", pos + sizeof(param_list->param_count));
122                         EM_SAFE_FREE(param_list->byte_stream);
123                         return NULL;
124                 }
125
126                 memcpy((param_list->byte_stream + pos), &param_list->param_count, sizeof(param_list->param_count));
127
128                 pos += sizeof(long);
129                 int index = 0, length = 0;
130
131         /* stream format */
132         /* | param1 length | (param1 data) | param2 length | (param2 data) | ... |*/
133         /* if param is 0 long, the param data is omitted */
134         for(index=0; index<param_list->param_count; index++) {
135                 length = emipc_get_length(param_list->params[index]);
136                 if (length < 0) {
137                         EM_DEBUG_EXCEPTION("index = %d, length = %d", index, length);
138                         EM_SAFE_FREE(param_list->byte_stream);
139                         return NULL;
140                 }
141
142                 if (pos + (int)sizeof(long) > stream_len) {
143                         EM_DEBUG_EXCEPTION("%d > stream_len", pos + sizeof(long));
144                         EM_SAFE_FREE(param_list->byte_stream);
145                         return NULL;
146                 }
147                 /* write param i length */
148                 memcpy((param_list->byte_stream+pos), &length, sizeof(long));
149                 pos += sizeof(long);
150
151                 if (pos + length > stream_len) {
152                         EM_DEBUG_EXCEPTION("%d > stream_len", pos + length);
153                         EM_SAFE_FREE(param_list->byte_stream);
154                         return NULL;
155                 }
156                 /* write param i data if length is greater than 0 */
157                 if( length > 0 ) {
158                         memcpy((param_list->byte_stream+pos), emipc_get_data(param_list->params[index]), length);
159                         pos += length;
160                 }
161         }
162         *stream_length = stream_len;
163
164         EM_DEBUG_FUNC_END();
165         return param_list->byte_stream;
166 }
167
168 EXPORT_API int emipc_sum_param_list_length(emipc_param_list *param_list)
169 {
170         int length = sizeof(long) * eSTREAM_DATA;
171         int index;
172         for (index = 0; index < param_list->param_count; index++) {
173                 length += sizeof(long);
174                 length += emipc_get_length(param_list->params[index]);
175         }
176         return length;
177 }
178
179 EXPORT_API bool emipc_add_param_to_param_list(emipc_param_list *param_list, void *data, int len)
180 {
181         EM_DEBUG_FUNC_BEGIN();
182         if (emipc_set_param(&(param_list->params[param_list->param_count]), data, len)) {
183                 param_list->param_count++;
184                 EM_SAFE_FREE(param_list->byte_stream);
185                 return true;
186         }
187         return false;
188 }
189
190 EXPORT_API void emipc_add_dynamic_param_to_param_list(emipc_param_list *param_list, void *data, int len)
191 {
192         EM_DEBUG_FUNC_BEGIN();
193         emipc_set_dynamic_param(&(param_list->params[param_list->param_count]), data, len);
194         param_list->param_count++;
195         EM_SAFE_FREE(param_list->byte_stream);
196 }
197
198 EXPORT_API void *emipc_get_param_of_param_list(emipc_param_list *param_list, int index)
199 {
200         EM_DEBUG_FUNC_BEGIN("index [%d]", index);
201         if (index < 0 || index >= param_list->param_count) {
202                 EM_DEBUG_EXCEPTION("Index value is not valid");
203                 return NULL;
204         }
205         return emipc_get_data(param_list->params[index]);
206 }
207
208 EXPORT_API int emipc_get_param_len_of_param_list(emipc_param_list *param_list, int index)
209 {
210         EM_DEBUG_FUNC_BEGIN();
211         if (index < 0 || index >= param_list->param_count) {
212                 EM_DEBUG_EXCEPTION("Index valud is not valid");
213                 return 0;
214         }
215         return emipc_get_length(param_list->params[index]);
216 }
217
218