tizen beta release
[framework/messaging/email-service.git] / ipc / api / ipc-param-list.cpp
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 #include "ipc-library-build.h"
26 #include "ipc-param-list.h"
27
28
29 ipcEmailParamList::ipcEmailParamList()
30 {
31         EM_DEBUG_FUNC_BEGIN();
32         m_nParamCount = 0;
33         m_pbyteStream = NULL;
34 }
35
36 ipcEmailParamList::~ipcEmailParamList()
37 {
38         EM_DEBUG_FUNC_BEGIN();
39         EM_SAFE_FREE(m_pbyteStream);
40         EM_DEBUG_FUNC_END();
41 }
42
43 /*  stream */
44 /*  +----------------------------------------------------------------------------------------------------------+ */
45 /*  | API ID(4B) | Resp. ID (4B) | Param Count(4B) | Param1 Len | Param1 Data | ... | ParamN Len | ParamN data |  */
46 /*  +----------------------------------------------------------------------------------------------------------+ */
47 bool ipcEmailParamList::ParseStream(void* a_pStreamData)
48 {
49         EM_DEBUG_FUNC_BEGIN();
50         long nParameterCount = ParseParameterCount(a_pStreamData);
51         if(nParameterCount <= 0) {
52                 EM_DEBUG_EXCEPTION("There is no parameter. ");
53                 return false;
54         }
55
56         unsigned char* pStream = (unsigned char*)a_pStreamData;
57         long nIndex, nParamLen, nPos = sizeof(long)*eSTREAM_DATA;
58 /*      _pal_ITrace_IPCPrintF("[IPCLib] Parameter Count [%x]  ", nParameterCount); */
59
60         for(nIndex = 0; nIndex < nParameterCount; nIndex++) {
61 /*              unsigned char szLen[5] = {NULL,}; */
62 /*              memcpy(szLen, pStream+nPos, sizeof(long)); */
63 /*              nParamLen = *(long*)(szLen); */
64
65                 long nLen =0;
66                 memcpy(&nLen, pStream+nPos, sizeof(long));
67                 nParamLen = nLen;
68                 EM_DEBUG_LOG("Parameter Length [%d] : %d ", nIndex, nParamLen);
69                 nPos += sizeof(long);   /*  Move from length position to data position */
70
71                 AddParam((void*)(pStream+nPos), nParamLen);
72                 nPos += nParamLen;              /*  move to next parameter       */
73         }
74         
75         return true;
76 }
77
78 void* ipcEmailParamList::GetStream()
79 {
80         EM_DEBUG_FUNC_BEGIN();
81         if(m_pbyteStream)
82                 return m_pbyteStream;
83         
84         int nStreamLen = GetStreamLength();
85         
86         if ( nStreamLen > 0 )  {
87                 m_pbyteStream = (unsigned char*) calloc(1, nStreamLen);
88                 int nPos = sizeof(long)*eSTREAM_COUNT;
89
90                 if ( nPos + (int)sizeof(m_nParamCount) > nStreamLen ) {
91                         EM_DEBUG_EXCEPTION("%d > nStreamLen", nPos + sizeof(m_nParamCount));                    
92                         EM_SAFE_FREE(m_pbyteStream);
93                         return NULL;
94                 }
95                 
96                 memcpy((void*)(m_pbyteStream+nPos), (void*)&m_nParamCount, sizeof(m_nParamCount));
97                 
98                 nPos += sizeof(long);
99                 int nIndex = 0, nLength = 0;
100
101                 /*   check memory overflow */
102                 for(nIndex=0; nIndex<m_nParamCount; nIndex++) {
103                         nLength = m_lstParams[nIndex].GetLength();
104                         if ( nLength <= 0 ) {
105                                 EM_DEBUG_EXCEPTION("nLength = %d", nIndex, nLength);                    
106                                 EM_SAFE_FREE(m_pbyteStream);
107                                 return NULL;
108                         }
109
110                         if ( nPos + (int)sizeof(long) > nStreamLen ) {
111                                 EM_DEBUG_EXCEPTION("%d > nStreamLen", nPos + sizeof(long));                     
112                                 EM_SAFE_FREE(m_pbyteStream);
113                                 return NULL;
114                         }
115                         memcpy((void*)(m_pbyteStream+nPos), (void*)&nLength, sizeof(long));
116                         nPos += sizeof(long);
117
118                         if ( nPos + nLength > nStreamLen ) {
119                                 EM_DEBUG_EXCEPTION("%d > nStreamLen", nPos + nLength);                  
120                                 EM_SAFE_FREE(m_pbyteStream);
121                                 return NULL;
122                         }
123                         
124                         memcpy((void*)(m_pbyteStream+nPos), (void*)m_lstParams[nIndex].GetData(), nLength);
125                         nPos += nLength;
126                 }
127                 return (void*)m_pbyteStream;
128         }
129         
130         EM_DEBUG_EXCEPTION("failed.");                  
131         EM_SAFE_FREE(m_pbyteStream);
132
133         EM_DEBUG_FUNC_END();
134         return NULL;
135 }
136
137
138 int ipcEmailParamList::GetStreamLength()
139 {
140         /* if(m_lstParams ) // prevent 20071030 : m_lstParams is array */ {
141                 int nLength = sizeof(long)*eSTREAM_DATA;        /* API ID, Response ID, Param count */
142                 int nIndex;
143                 for(nIndex=0; nIndex<m_nParamCount; nIndex++) {
144                         nLength += sizeof(long);                                        /* Length size */
145                         nLength += m_lstParams[nIndex].GetLength();     /* data size */
146                 }
147                 return nLength;
148         }
149         return 0;       
150 }
151
152 bool ipcEmailParamList::AddParam(void* a_pData, int a_nLen)
153 {
154         EM_DEBUG_FUNC_BEGIN();
155         
156         if(m_lstParams[m_nParamCount].SetParam(a_pData, a_nLen)) {
157                 m_nParamCount++;
158                 EM_SAFE_FREE(m_pbyteStream);
159                 return true;
160         }
161
162         return false;
163         
164 }
165
166 void* ipcEmailParamList::GetParam(int a_nIndex)
167 {
168         EM_DEBUG_FUNC_BEGIN("a_nIndex [%d]", a_nIndex);
169         if(a_nIndex < 0 || a_nIndex >= m_nParamCount) {
170                 EM_DEBUG_EXCEPTION("Index value is not valid ");
171                 return NULL;
172         }
173
174         return (void*)m_lstParams[a_nIndex].GetData();
175 }
176
177 int ipcEmailParamList::GetParamLen(int a_nIndex)
178 {
179         EM_DEBUG_FUNC_BEGIN();
180         if(a_nIndex < 0 || a_nIndex >= m_nParamCount) {
181                 EM_DEBUG_EXCEPTION("Index value is not valid ");
182                 return 0;
183         }
184         
185         return m_lstParams[a_nIndex].GetLength();
186 }
187
188 int ipcEmailParamList::GetParamCount()
189 {
190         EM_DEBUG_FUNC_BEGIN("Paramter count [%d]  ", m_nParamCount);
191         return m_nParamCount;
192 }
193
194 long ipcEmailParamList::ParseParameterCount(void *a_pStream)
195 {
196         EM_DEBUG_FUNC_BEGIN();
197         long *pParameterCountPostion = (long*)a_pStream + eSTREAM_COUNT;
198
199         return *pParameterCountPostion;
200 }
201