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, int count, char **ppDest)
364 dataSize = (sizeof(MSG_REPORT_STATUS_INFO_S)*count + sizeof(int));
366 *ppDest = (char*)new char[dataSize];
368 void* p = (void*)*ppDest;
370 memcpy(p, &count, sizeof(int));
372 p = (void*)((int)p + sizeof(int));
374 memcpy(p, pReportStatus, sizeof(MSG_REPORT_STATUS_INFO_S)*count);
380 int MsgEncodeThreadId(msg_thread_id_t *pThreadId, char **ppDest)
384 dataSize = (sizeof(msg_thread_id_t));
386 *ppDest = (char*)new char[dataSize];
388 void* p = (void*)*ppDest;
390 memcpy(p, pThreadId, dataSize);
396 int MsgEncodeThreadInfo(MSG_THREAD_VIEW_S *pThreadInfo, char **ppDest)
400 dataSize = sizeof(MSG_THREAD_VIEW_S);
402 *ppDest = (char*)new char[dataSize];
404 void* p = (void*)*ppDest;
406 memcpy(p, pThreadInfo, sizeof(MSG_THREAD_VIEW_S));
408 p = (void*)((char*)p + sizeof(MSG_THREAD_VIEW_S));
416 void MsgDecodeMsgId(char *pSrc, msg_message_id_t *pMsgId)
418 memcpy(pMsgId, pSrc, sizeof(msg_message_id_t));
422 void MsgDecodeCountInfo(char *pSrc, MSG_COUNT_INFO_S *pCountInfo)
424 memcpy(pCountInfo, pSrc, sizeof(MSG_COUNT_INFO_S));
428 void MsgDecodeMemSize(char *pSrc, unsigned int *memsize)
430 memcpy(memsize, pSrc, sizeof(unsigned int));
434 void MsgDecodeMsgInfo(char *pSrc, MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S* pSendOptInfo)
436 memcpy(pMsgInfo, pSrc, sizeof(MSG_MESSAGE_INFO_S));
438 pSrc = pSrc + sizeof(MSG_MESSAGE_INFO_S);
440 memcpy(pSendOptInfo, pSrc, sizeof(MSG_SENDINGOPT_INFO_S));
443 void MsgDecodeRecipientList(char *pSrc, MSG_RECIPIENTS_LIST_S *pRecipientList)
447 memcpy(&count, pSrc, sizeof(int));
448 pSrc = pSrc + sizeof(int);
450 pRecipientList->recipientCnt= count;
451 pRecipientList->recipientAddr = (MSG_ADDRESS_INFO_S*)new char[sizeof(MSG_ADDRESS_INFO_S)*count];
453 MSG_ADDRESS_INFO_S* pInfoTmp = pRecipientList->recipientAddr;
455 for (int i = 0; i < count; i++)
457 memcpy(pInfoTmp, pSrc, sizeof(MSG_ADDRESS_INFO_S));
458 pSrc = pSrc + sizeof(MSG_ADDRESS_INFO_S);
464 void MsgDecodeFolderList(char *pSrc, msg_struct_list_s *pFolderList)
468 memcpy(&count, pSrc, sizeof(int));
469 pSrc = pSrc + sizeof(int);
473 pFolderList->nCount = count;
474 pFolderList->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_FOLDER_INFO_S *)*count];
476 msg_struct_s *pInfoTmp = NULL;
478 for (int i = 0; i < count; i++)
481 pFolderList->msg_struct_info[i] = (msg_struct_t )new char[sizeof(msg_struct_s)];
482 pInfoTmp = (msg_struct_s *)pFolderList->msg_struct_info[i];
483 pInfoTmp->type = MSG_STRUCT_FOLDER_INFO;
484 pInfoTmp->data = new char[sizeof(MSG_FOLDER_INFO_S)];
485 memcpy(pInfoTmp->data, pSrc, sizeof(MSG_FOLDER_INFO_S));
486 pSrc = pSrc + sizeof(MSG_FOLDER_INFO_S);
489 else if ( count == 0 )
491 pFolderList->nCount = count;
492 pFolderList->msg_struct_info = NULL;
496 void MsgDecodeFilterList(char *pSrc, msg_struct_list_s *pFilterList)
500 memcpy(&count, pSrc, sizeof(int));
501 pSrc = pSrc + sizeof(int);
505 pFilterList->nCount = count;
506 pFilterList->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_FILTER_S *)*count];
508 msg_struct_s *pStructTmp = NULL;
510 for (int i = 0; i < count; i++)
512 pFilterList->msg_struct_info[i] = (msg_struct_t )new char[sizeof(msg_struct_s)];
513 pStructTmp = (msg_struct_s *)pFilterList->msg_struct_info[i];
514 pStructTmp->type = MSG_STRUCT_FILTER;
515 pStructTmp->data = new char[sizeof(MSG_FILTER_S)];
516 memcpy(pStructTmp->data, pSrc, sizeof(MSG_FILTER_S));
517 pSrc = pSrc + sizeof(MSG_FILTER_S);
520 else if ( count == 0 )
522 pFilterList->nCount = count;
523 pFilterList->msg_struct_info = NULL;
529 void MsgDecodeFilterFlag(char *pSrc, bool *pSetFlag)
531 memcpy(pSetFlag, pSrc, sizeof(bool));
535 void MsgDecodeMsgType(char *pSrc, MSG_MESSAGE_TYPE_S* pMsgType)
537 memcpy(pMsgType, pSrc, sizeof(MSG_MESSAGE_TYPE_S));
540 void MsgDecodeContactCount(char *pSrc, MSG_THREAD_COUNT_INFO_S *pMsgThreadCountList)
547 memcpy(&count, pSrc, sizeof(int));
548 pSrc = pSrc + sizeof(int);
549 pMsgThreadCountList->totalCount = count;
552 memcpy(&count, pSrc, sizeof(int));
553 pSrc = pSrc + sizeof(int);
554 pMsgThreadCountList->unReadCount = count;
557 memcpy(&count, pSrc, sizeof(int));
558 pSrc = pSrc + sizeof(int);
559 pMsgThreadCountList->mmsMsgCount = count;
562 memcpy(&count, pSrc, sizeof(int));
563 pSrc = pSrc + sizeof(int);
564 pMsgThreadCountList->smsMsgCount = count;
571 void MsgDecodeReportStatus(char *pSrc, msg_struct_list_s *report_list)
578 memcpy(&count, pSrc, sizeof(int));
579 pSrc = pSrc + sizeof(int);
581 report_list->nCount = count;
583 msg_struct_t *report_status = (msg_struct_t *)new char[sizeof(msg_struct_t)*count];
584 for (int i = 0; i < count; i++) {
586 msg_struct_s *report_status_item = new msg_struct_s;
587 report_status_item->type = MSG_STRUCT_REPORT_STATUS_INFO;
588 report_status_item->data = new MSG_REPORT_STATUS_INFO_S;
589 memset(report_status_item->data, 0x00, sizeof(MSG_REPORT_STATUS_INFO_S));
591 MSG_REPORT_STATUS_INFO_S *report_status_info = (MSG_REPORT_STATUS_INFO_S *)report_status_item->data;
592 memcpy(report_status_info, pSrc, sizeof(MSG_REPORT_STATUS_INFO_S));
594 pSrc = pSrc + sizeof(MSG_REPORT_STATUS_INFO_S);
596 report_status[i] = (msg_struct_t)report_status_item;
598 MSG_DEBUG("Report_type = %d, status addr = %s, status = %d, time = %d",
599 report_status_info->type, report_status_info->addressVal,
600 report_status_info->status, report_status_info->statusTime);
603 report_list->msg_struct_info = report_status;
607 void MsgDecodeThreadId(char *pSrc, msg_thread_id_t *pThreadId)
609 memcpy(pThreadId, pSrc, sizeof(msg_thread_id_t));
612 void MsgDecodeThreadInfo(char *pSrc, MSG_THREAD_VIEW_S *pThreadInfo)
614 memcpy(pThreadInfo, pSrc, sizeof(MSG_THREAD_VIEW_S));
618 int MsgMakeEvent(const void *pData, int DataSize, MSG_EVENT_TYPE_T MsgEvent, msg_error_t MsgError, void **ppEvent)
620 MSG_EVENT_S* pMsgEvent = NULL;
622 *ppEvent = (MSG_EVENT_S*)new char[sizeof(MSG_EVENT_S) + DataSize];
624 pMsgEvent = (MSG_EVENT_S*)*ppEvent;
626 pMsgEvent->eventType = MsgEvent;
627 pMsgEvent->result = MsgError;
629 MSG_DEBUG("eventType [%d : %s]", pMsgEvent->eventType, MsgDbgEvtStr(pMsgEvent->eventType));
630 MSG_DEBUG("result [%d]", pMsgEvent->result);
633 memcpy((void*)pMsgEvent->data, pData, DataSize);
635 return (sizeof(MSG_EVENT_S) + DataSize);
638 int msg_verify_number(const char *raw, char *trimmed)
640 if (!(raw && trimmed)) {
641 MSG_DEBUG("Phone Number is NULL");
642 return MSG_ERR_NULL_POINTER;
645 for (int i=0, j=0 ; raw[i] ; i++) {
646 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == ',' || raw[i] == ' ' \
647 || raw[i] == '*' || raw[i] == '#') {
648 trimmed[j++] = raw[i];
649 } else if (raw[i] == '-') {
652 MSG_DEBUG("Unacceptable character in telephone number: [%c]", raw[i]);
653 return MSG_ERR_INVALID_PARAMETER;
657 MSG_DEBUG("Trimming [%s]->[%s]", raw, trimmed);
661 int msg_verify_email(const char *raw)
664 bool atExist = false;
667 MSG_DEBUG("Email is NULL");
668 return MSG_ERR_NULL_POINTER;
671 for (int i = 0; raw[i]; i++) {
676 if (atExist == false) {
680 MSG_DEBUG("Character [@] is included more than twice in email address.");
681 return MSG_ERR_INVALID_PARAMETER;
685 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == '*' || raw[i] == '#') {
687 } else if ((raw[i] >= 'a' && raw[i] <= 'z') ||(raw[i] >= 'A' && raw[i] <= 'Z') ||(raw[i] == '.') || raw[i] == '_' || raw[i] == '-') {
690 } else if (raw[i] == ',') {
691 if (onlyNum == false && atExist == false) {
692 MSG_DEBUG("Unacceptable type in address.");
693 return MSG_ERR_INVALID_PARAMETER;
699 MSG_DEBUG("Unacceptable character in address : [%c]", raw[i]);
700 return MSG_ERR_INVALID_PARAMETER;