2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "MsgUtilFunction.h"
20 /*==================================================================================================
21 FUNCTION IMPLEMENTATION
22 ==================================================================================================*/
25 int MsgEncodeCountInfo(MSG_COUNT_INFO_S *pCountInfo, char **ppDest)
29 dataSize = sizeof(MSG_COUNT_INFO_S);
31 *ppDest = (char*)new char[dataSize];
33 void* p = (void*)*ppDest;
35 memcpy(p, pCountInfo, dataSize);
41 int MsgEncodeRecipientList(MSG_RECIPIENTS_LIST_S *pRecipientList, char **ppDest)
43 int count = 0, dataSize = 0;
45 count = pRecipientList->recipientCnt;
46 dataSize = sizeof(int) + (sizeof(MSG_ADDRESS_INFO_S)*count);
48 *ppDest = (char*)new char[dataSize];
50 void* p = (void*)*ppDest;
52 memcpy(p, &count, sizeof(int));
53 p = (void*)((char*)p + sizeof(int));
55 for (int i = 0; i < count; i++)
57 memcpy(p, &(pRecipientList->recipientAddr[i]), sizeof(MSG_ADDRESS_INFO_S));
58 p = (void*)((char*)p + sizeof(MSG_ADDRESS_INFO_S));
65 int MsgEncodeCountByMsgType(int MsgCount, char **ppDest)
69 dataSize = sizeof(int);
71 *ppDest = (char*)new char[dataSize];
73 void* p = (void*)*ppDest;
75 memcpy(p, &MsgCount, dataSize);
81 int MsgEncodeMsgId(msg_message_id_t *pMsgId, char **ppDest)
85 dataSize = (sizeof(msg_message_id_t));
87 *ppDest = (char*)new char[dataSize];
89 void* p = (void*)*ppDest;
91 memcpy(p, pMsgId, dataSize);
97 int MsgEncodeMsgInfo(MSG_MESSAGE_INFO_S *pMsgInfo, char **ppDest)
101 dataSize = sizeof(MSG_MESSAGE_INFO_S);
103 *ppDest = (char*)new char[dataSize];
105 void* p = (void*)*ppDest;
107 memcpy(p, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
109 p = (void*)((char*)p + sizeof(MSG_MESSAGE_INFO_S));
115 int MsgEncodeMsgInfo(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S* pSendOptInfo, char **ppDest)
119 dataSize = (sizeof(MSG_MESSAGE_INFO_S) + sizeof(MSG_SENDINGOPT_INFO_S));
121 *ppDest = (char*)new char[dataSize];
123 void* p = (void*)*ppDest;
125 memcpy(p, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
127 p = (void*)((char*)p + sizeof(MSG_MESSAGE_INFO_S));
129 memcpy(p, pSendOptInfo, sizeof(MSG_SENDINGOPT_INFO_S));
134 int MsgEncodeFolderList(msg_struct_list_s *pFolderList, char **ppDest)
136 int count = 0, dataSize = 0;
138 count = pFolderList->nCount;
139 dataSize = sizeof(int) + (sizeof(MSG_FOLDER_INFO_S)*count);
141 *ppDest = (char*)new char[dataSize];
143 void* p = (void*)*ppDest;
145 memcpy(p, &count, sizeof(int));
146 p = (void*)((char*)p + sizeof(int));
148 msg_struct_s *folder_info = NULL;
150 for (int i = 0; i < count; i++)
152 folder_info = (msg_struct_s *)pFolderList->msg_struct_info[i];
153 memcpy(p, folder_info->data, sizeof(MSG_FOLDER_INFO_S));
154 p = (void*)((char*)p + sizeof(MSG_FOLDER_INFO_S));
160 int MsgEncodeFilterList(msg_struct_list_s *pFilterList, char **ppDest)
162 int count = 0, dataSize = 0;
164 count = pFilterList->nCount;
165 dataSize = sizeof(int) + (sizeof(MSG_FILTER_S)*count);
167 *ppDest = (char*)new char[dataSize];
169 void* p = (void*)*ppDest;
171 memcpy(p, &count, sizeof(int));
172 p = (void*)((char*)p + sizeof(int));
174 msg_struct_s *filter_info = NULL;
176 for (int i = 0; i < count; i++)
178 filter_info = (msg_struct_s *)pFilterList->msg_struct_info[i];
179 memcpy(p, filter_info->data, sizeof(MSG_FILTER_S));
180 p = (void*)((char*)p + sizeof(MSG_FILTER_S));
187 int MsgEncodeFilterFlag(bool *pSetFlag, char **ppDest)
191 dataSize = (sizeof(bool));
193 *ppDest = (char*)new char[dataSize];
195 void* p = (void*)*ppDest;
197 memcpy(p, pSetFlag, dataSize);
203 int MsgEncodeMsgType(MSG_MESSAGE_TYPE_S *pMsgType, char **ppDest)
207 dataSize = (sizeof(MSG_MESSAGE_TYPE_S));
209 *ppDest = (char*)new char[dataSize];
211 void* p = (void*)*ppDest;
213 memcpy(p, pMsgType, dataSize);
219 int MsgEncodeThreadViewList(msg_struct_list_s *pThreadViewList, char **ppDest)
221 int count = 0, dataSize = 0;
223 count = pThreadViewList->nCount;
225 dataSize = sizeof(int) + (sizeof(MSG_THREAD_VIEW_S)*count);
227 *ppDest = (char*)new char[dataSize];
229 void* p = (void*)*ppDest;
231 memcpy(p, &count, sizeof(int));
232 p = (void*)((char*)p + sizeof(int));
234 msg_struct_s *thread_info = NULL;
236 for (int i = 0; i < count; i++)
238 thread_info = (msg_struct_s *)pThreadViewList->msg_struct_info[i];
239 memcpy(p, thread_info->data, sizeof(MSG_THREAD_VIEW_S));
240 p = (void*)((char*)p + sizeof(MSG_THREAD_VIEW_S));
247 int MsgEncodeConversationViewList(msg_struct_list_s *pConvViewList, char **ppDest)
249 int count = 0, dataSize = 0;
251 count = pConvViewList->nCount;
253 dataSize = sizeof(int) + (sizeof(msg_struct_list_s)*count);
255 *ppDest = (char*)new char[dataSize];
257 void* p = (void*)*ppDest;
259 memcpy(p, &count, sizeof(int));
260 p = (void*)((char*)p + sizeof(int));
262 for (int i = 0; i < count; i++)
264 memcpy(p, &(pConvViewList->msg_struct_info[i]), sizeof(msg_struct_list_s));
265 p = (void*)((char*)p + sizeof(msg_struct_list_s));
272 int MsgEncodeMsgGetContactCount(MSG_THREAD_COUNT_INFO_S *threadCountInfo, char **ppDest)
274 int dataSize = sizeof(int) + sizeof(int) + sizeof(int) + sizeof(int);
276 MSG_DEBUG("datasize = [%d] \n", dataSize);
278 *ppDest = (char*)new char[dataSize];
280 void* p = (void*)*ppDest;
282 memcpy(p, &(threadCountInfo->totalCount), sizeof(int));
283 p = (void*)((char*)p + sizeof(int));
285 memcpy(p, &(threadCountInfo->unReadCount), sizeof(int));
286 p = (void*)((char*)p + sizeof(int));
288 memcpy(p, &(threadCountInfo->mmsMsgCount), sizeof(int));
289 p = (void*)((char*)p + sizeof(int));
291 memcpy(p, &(threadCountInfo->smsMsgCount), sizeof(int));
292 p = (void*)((char*)p + sizeof(int));
297 int MsgEncodeMemSize(unsigned int *memsize, char **ppDest)
301 dataSize = sizeof(unsigned int);
303 *ppDest = (char*)new char[dataSize];
305 void* p = (void*)*ppDest;
307 memcpy(p, memsize, dataSize);
313 int MsgEncodeSyncMLOperationData(int msgId, int extId, char **ppDest)
317 dataSize = sizeof(int) + sizeof(int);
319 *ppDest = (char*)new char[dataSize];
321 void* p = (void*)*ppDest;
323 memcpy(p, &msgId, sizeof(int));
324 p = (void*)((char*)p + sizeof(int));
326 memcpy(p, &extId, sizeof(int));
332 int MsgEncodeStorageChangeData(const msg_storage_change_type_t storageChangeType, const msg_id_list_s *pMsgIdList, char **ppDest)
337 count = pMsgIdList->nCount;
339 dataSize = sizeof(msg_storage_change_type_t) + sizeof(int) + (sizeof(msg_message_id_t)*count);
341 *ppDest = (char*)new char[dataSize];
343 void* p = (void*)*ppDest;
345 memcpy(p, &storageChangeType, sizeof(msg_storage_change_type_t));
346 p = (void*)((char*)p + sizeof(msg_storage_change_type_t));
348 memcpy(p, &count, sizeof(int));
349 p = (void*)((char*)p + sizeof(int));
351 for (int i = 0; i < count; i++) {
352 memcpy(p, &(pMsgIdList->msgIdList[i]), sizeof(msg_message_id_t));
353 p = (void*)((char*)p + sizeof(msg_message_id_t));
360 int MsgEncodeReportStatus(MSG_REPORT_STATUS_INFO_S* pReportStatus, char **ppDest)
364 dataSize = (sizeof(MSG_REPORT_STATUS_INFO_S));
366 *ppDest = (char*)new char[dataSize];
368 void* p = (void*)*ppDest;
370 memcpy(p, pReportStatus, dataSize);
376 int MsgEncodeThreadId(msg_thread_id_t *pThreadId, char **ppDest)
380 dataSize = (sizeof(msg_thread_id_t));
382 *ppDest = (char*)new char[dataSize];
384 void* p = (void*)*ppDest;
386 memcpy(p, pThreadId, dataSize);
392 int MsgEncodeThreadInfo(MSG_THREAD_VIEW_S *pThreadInfo, char **ppDest)
396 dataSize = sizeof(MSG_THREAD_VIEW_S);
398 *ppDest = (char*)new char[dataSize];
400 void* p = (void*)*ppDest;
402 memcpy(p, pThreadInfo, sizeof(MSG_THREAD_VIEW_S));
404 p = (void*)((char*)p + sizeof(MSG_THREAD_VIEW_S));
412 void MsgDecodeMsgId(char *pSrc, msg_message_id_t *pMsgId)
414 memcpy(pMsgId, pSrc, sizeof(msg_message_id_t));
418 void MsgDecodeCountInfo(char *pSrc, MSG_COUNT_INFO_S *pCountInfo)
420 memcpy(pCountInfo, pSrc, sizeof(MSG_COUNT_INFO_S));
424 void MsgDecodeMemSize(char *pSrc, unsigned int *memsize)
426 memcpy(memsize, pSrc, sizeof(unsigned int));
430 void MsgDecodeMsgInfo(char *pSrc, MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S* pSendOptInfo)
432 memcpy(pMsgInfo, pSrc, sizeof(MSG_MESSAGE_INFO_S));
434 pSrc = pSrc + sizeof(MSG_MESSAGE_INFO_S);
436 memcpy(pSendOptInfo, pSrc, sizeof(MSG_SENDINGOPT_INFO_S));
439 void MsgDecodeRecipientList(char *pSrc, MSG_RECIPIENTS_LIST_S *pRecipientList)
443 memcpy(&count, pSrc, sizeof(int));
444 pSrc = pSrc + sizeof(int);
446 pRecipientList->recipientCnt= count;
447 pRecipientList->recipientAddr = (MSG_ADDRESS_INFO_S*)new char[sizeof(MSG_ADDRESS_INFO_S)*count];
449 MSG_ADDRESS_INFO_S* pInfoTmp = pRecipientList->recipientAddr;
451 for (int i = 0; i < count; i++)
453 memcpy(pInfoTmp, pSrc, sizeof(MSG_ADDRESS_INFO_S));
454 pSrc = pSrc + sizeof(MSG_ADDRESS_INFO_S);
460 void MsgDecodeFolderList(char *pSrc, msg_struct_list_s *pFolderList)
464 memcpy(&count, pSrc, sizeof(int));
465 pSrc = pSrc + sizeof(int);
469 pFolderList->nCount = count;
470 pFolderList->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_FOLDER_INFO_S *)*count];
472 msg_struct_s *pInfoTmp = NULL;
474 for (int i = 0; i < count; i++)
477 pFolderList->msg_struct_info[i] = (msg_struct_t )new char[sizeof(msg_struct_s)];
478 pInfoTmp = (msg_struct_s *)pFolderList->msg_struct_info[i];
479 pInfoTmp->type = MSG_STRUCT_FOLDER_INFO;
480 pInfoTmp->data = new char[sizeof(MSG_FOLDER_INFO_S)];
481 memcpy(pInfoTmp->data, pSrc, sizeof(MSG_FOLDER_INFO_S));
482 pSrc = pSrc + sizeof(MSG_FOLDER_INFO_S);
485 else if ( count == 0 )
487 pFolderList->nCount = count;
488 pFolderList->msg_struct_info = NULL;
492 void MsgDecodeFilterList(char *pSrc, msg_struct_list_s *pFilterList)
496 memcpy(&count, pSrc, sizeof(int));
497 pSrc = pSrc + sizeof(int);
501 pFilterList->nCount = count;
502 pFilterList->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_FILTER_S *)*count];
504 msg_struct_s *pStructTmp = NULL;
506 for (int i = 0; i < count; i++)
508 pFilterList->msg_struct_info[i] = (msg_struct_t )new char[sizeof(msg_struct_s)];
509 pStructTmp = (msg_struct_s *)pFilterList->msg_struct_info[i];
510 pStructTmp->type = MSG_STRUCT_FILTER;
511 pStructTmp->data = new char[sizeof(MSG_FILTER_S)];
512 memcpy(pStructTmp->data, pSrc, sizeof(MSG_FILTER_S));
513 pSrc = pSrc + sizeof(MSG_FILTER_S);
516 else if ( count == 0 )
518 pFilterList->nCount = count;
519 pFilterList->msg_struct_info = NULL;
525 void MsgDecodeFilterFlag(char *pSrc, bool *pSetFlag)
527 memcpy(pSetFlag, pSrc, sizeof(bool));
531 void MsgDecodeMsgType(char *pSrc, MSG_MESSAGE_TYPE_S* pMsgType)
533 memcpy(pMsgType, pSrc, sizeof(MSG_MESSAGE_TYPE_S));
536 void MsgDecodeContactCount(char *pSrc, MSG_THREAD_COUNT_INFO_S *pMsgThreadCountList)
543 memcpy(&count, pSrc, sizeof(int));
544 pSrc = pSrc + sizeof(int);
545 pMsgThreadCountList->totalCount = count;
548 memcpy(&count, pSrc, sizeof(int));
549 pSrc = pSrc + sizeof(int);
550 pMsgThreadCountList->unReadCount = count;
553 memcpy(&count, pSrc, sizeof(int));
554 pSrc = pSrc + sizeof(int);
555 pMsgThreadCountList->mmsMsgCount = count;
558 memcpy(&count, pSrc, sizeof(int));
559 pSrc = pSrc + sizeof(int);
560 pMsgThreadCountList->smsMsgCount = count;
567 void MsgDecodeReportStatus(char *pSrc, MSG_REPORT_STATUS_INFO_S *pReportStatus)
574 memcpy(&count, pSrc, sizeof(msg_delivery_report_status_t));
575 pSrc = pSrc + sizeof(msg_delivery_report_status_t);
576 pReportStatus->deliveryStatus = count;
579 memcpy(&count, pSrc, sizeof(time_t));
580 pSrc = pSrc + sizeof(time_t);
581 pReportStatus->deliveryStatusTime = count;
584 memcpy(&count, pSrc, sizeof(msg_read_report_status_t));
585 pSrc = pSrc + sizeof(msg_read_report_status_t);
586 pReportStatus->readStatus = count;
589 memcpy(&count, pSrc, sizeof(time_t));
590 pSrc = pSrc + sizeof(time_t);
591 pReportStatus->readStatusTime = count;
597 void MsgDecodeThreadId(char *pSrc, msg_thread_id_t *pThreadId)
599 memcpy(pThreadId, pSrc, sizeof(msg_thread_id_t));
602 void MsgDecodeThreadInfo(char *pSrc, MSG_THREAD_VIEW_S *pThreadInfo)
604 memcpy(pThreadInfo, pSrc, sizeof(MSG_THREAD_VIEW_S));
608 int MsgMakeEvent(const void *pData, int DataSize, MSG_EVENT_TYPE_T MsgEvent, msg_error_t MsgError, void **ppEvent)
610 MSG_EVENT_S* pMsgEvent = NULL;
612 *ppEvent = (MSG_EVENT_S*)new char[sizeof(MSG_EVENT_S) + DataSize];
614 pMsgEvent = (MSG_EVENT_S*)*ppEvent;
616 pMsgEvent->eventType = MsgEvent;
617 pMsgEvent->result = MsgError;
619 MSG_DEBUG("eventType [%d : %s]", pMsgEvent->eventType, MsgDbgEvtStr(pMsgEvent->eventType));
620 MSG_DEBUG("result [%d]", pMsgEvent->result);
623 memcpy((void*)pMsgEvent->data, pData, DataSize);
625 return (sizeof(MSG_EVENT_S) + DataSize);
628 int msg_verify_number(const char *raw, char *trimmed)
630 if (!(raw && trimmed)) {
631 MSG_DEBUG("Phone Number is NULL");
632 return MSG_ERR_NULL_POINTER;
635 for (int i=0, j=0 ; raw[i] ; i++) {
636 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == ',' || raw[i] == ' ' \
637 || raw[i] == '*' || raw[i] == '#') {
638 trimmed[j++] = raw[i];
639 } else if (raw[i] == '-') {
642 MSG_DEBUG("Unacceptable character in telephone number: [%c]", raw[i]);
643 return MSG_ERR_INVALID_PARAMETER;
647 MSG_DEBUG("Trimming [%s]->[%s]", raw, trimmed);
651 int msg_verify_email(const char *raw)
654 bool atExist = false;
657 MSG_DEBUG("Email is NULL");
658 return MSG_ERR_NULL_POINTER;
661 for (int i = 0; raw[i]; i++) {
666 if (atExist == false) {
670 MSG_DEBUG("Character [@] is included more than twice in email address.");
671 return MSG_ERR_INVALID_PARAMETER;
675 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == '*' || raw[i] == '#') {
677 } else if ((raw[i] >= 'a' && raw[i] <= 'z') ||(raw[i] >= 'A' && raw[i] <= 'Z') ||(raw[i] == '.') || raw[i] == '_' || raw[i] == '-') {
680 } else if (raw[i] == ',') {
681 if (onlyNum == false && atExist == false) {
682 MSG_DEBUG("Unacceptable type in address.");
683 return MSG_ERR_INVALID_PARAMETER;
689 MSG_DEBUG("Unacceptable character in address : [%c]", raw[i]);
690 return MSG_ERR_INVALID_PARAMETER;