4 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include "MsgUtilFunction.h"
23 /*==================================================================================================
24 FUNCTION IMPLEMENTATION
25 ==================================================================================================*/
28 int MsgEncodeCountInfo(MSG_COUNT_INFO_S *pCountInfo, char **ppDest)
32 dataSize = sizeof(MSG_COUNT_INFO_S);
34 *ppDest = (char*)new char[dataSize];
36 void* p = (void*)*ppDest;
38 memcpy(p, pCountInfo, dataSize);
44 int MsgEncodeRecipientList(MSG_RECIPIENTS_LIST_S *pRecipientList, char **ppDest)
46 int count = 0, dataSize = 0;
48 count = pRecipientList->recipientCnt;
49 dataSize = sizeof(int) + (sizeof(MSG_ADDRESS_INFO_S)*count);
51 *ppDest = (char*)new char[dataSize];
53 void* p = (void*)*ppDest;
55 memcpy(p, &count, sizeof(int));
56 p = (void*)((char*)p + sizeof(int));
58 for (int i = 0; i < count; i++)
60 memcpy(p, &(pRecipientList->recipientAddr[i]), sizeof(MSG_ADDRESS_INFO_S));
61 p = (void*)((char*)p + sizeof(MSG_ADDRESS_INFO_S));
68 int MsgEncodeCountByMsgType(int MsgCount, char **ppDest)
72 dataSize = sizeof(int);
74 *ppDest = (char*)new char[dataSize];
76 void* p = (void*)*ppDest;
78 memcpy(p, &MsgCount, dataSize);
84 int MsgEncodeMsgId(msg_message_id_t *pMsgId, char **ppDest)
88 dataSize = (sizeof(msg_message_id_t));
90 *ppDest = (char*)new char[dataSize];
92 void* p = (void*)*ppDest;
94 memcpy(p, pMsgId, dataSize);
100 int MsgEncodeMsgInfo(MSG_MESSAGE_INFO_S *pMsgInfo, char **ppDest)
104 dataSize = sizeof(MSG_MESSAGE_INFO_S);
106 *ppDest = (char*)new char[dataSize];
108 void* p = (void*)*ppDest;
110 memcpy(p, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
112 p = (void*)((char*)p + sizeof(MSG_MESSAGE_INFO_S));
118 int MsgEncodeMsgInfo(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S* pSendOptInfo, char **ppDest)
122 dataSize = (sizeof(MSG_MESSAGE_INFO_S) + sizeof(MSG_SENDINGOPT_INFO_S));
124 *ppDest = (char*)new char[dataSize];
126 void* p = (void*)*ppDest;
128 memcpy(p, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
130 p = (void*)((char*)p + sizeof(MSG_MESSAGE_INFO_S));
132 memcpy(p, pSendOptInfo, sizeof(MSG_SENDINGOPT_INFO_S));
137 int MsgEncodeFolderList(msg_struct_list_s *pFolderList, char **ppDest)
139 int count = 0, dataSize = 0;
141 count = pFolderList->nCount;
142 dataSize = sizeof(int) + (sizeof(MSG_FOLDER_INFO_S)*count);
144 *ppDest = (char*)new char[dataSize];
146 void* p = (void*)*ppDest;
148 memcpy(p, &count, sizeof(int));
149 p = (void*)((char*)p + sizeof(int));
151 msg_struct_s *folder_info = NULL;
153 for (int i = 0; i < count; i++)
155 folder_info = (msg_struct_s *)pFolderList->msg_struct_info[i];
156 memcpy(p, folder_info->data, sizeof(MSG_FOLDER_INFO_S));
157 p = (void*)((char*)p + sizeof(MSG_FOLDER_INFO_S));
163 int MsgEncodeFilterList(msg_struct_list_s *pFilterList, char **ppDest)
165 int count = 0, dataSize = 0;
167 count = pFilterList->nCount;
168 dataSize = sizeof(int) + (sizeof(MSG_FILTER_S)*count);
170 *ppDest = (char*)new char[dataSize];
172 void* p = (void*)*ppDest;
174 memcpy(p, &count, sizeof(int));
175 p = (void*)((char*)p + sizeof(int));
177 msg_struct_s *filter_info = NULL;
179 for (int i = 0; i < count; i++)
181 filter_info = (msg_struct_s *)pFilterList->msg_struct_info[i];
182 memcpy(p, filter_info->data, sizeof(MSG_FILTER_S));
183 p = (void*)((char*)p + sizeof(MSG_FILTER_S));
190 int MsgEncodeFilterFlag(bool *pSetFlag, char **ppDest)
194 dataSize = (sizeof(bool));
196 *ppDest = (char*)new char[dataSize];
198 void* p = (void*)*ppDest;
200 memcpy(p, pSetFlag, dataSize);
206 int MsgEncodeMsgType(MSG_MESSAGE_TYPE_S *pMsgType, char **ppDest)
210 dataSize = (sizeof(MSG_MESSAGE_TYPE_S));
212 *ppDest = (char*)new char[dataSize];
214 void* p = (void*)*ppDest;
216 memcpy(p, pMsgType, dataSize);
222 int MsgEncodeThreadViewList(msg_struct_list_s *pThreadViewList, char **ppDest)
224 int count = 0, dataSize = 0;
226 count = pThreadViewList->nCount;
228 dataSize = sizeof(int) + (sizeof(MSG_THREAD_VIEW_S)*count);
230 *ppDest = (char*)new char[dataSize];
232 void* p = (void*)*ppDest;
234 memcpy(p, &count, sizeof(int));
235 p = (void*)((char*)p + sizeof(int));
237 msg_struct_s *thread_info = NULL;
239 for (int i = 0; i < count; i++)
241 thread_info = (msg_struct_s *)pThreadViewList->msg_struct_info[i];
242 memcpy(p, thread_info->data, sizeof(MSG_THREAD_VIEW_S));
243 p = (void*)((char*)p + sizeof(MSG_THREAD_VIEW_S));
250 int MsgEncodeConversationViewList(msg_struct_list_s *pConvViewList, char **ppDest)
252 int count = 0, dataSize = 0;
254 count = pConvViewList->nCount;
256 dataSize = sizeof(int) + (sizeof(msg_struct_list_s)*count);
258 *ppDest = (char*)new char[dataSize];
260 void* p = (void*)*ppDest;
262 memcpy(p, &count, sizeof(int));
263 p = (void*)((char*)p + sizeof(int));
265 for (int i = 0; i < count; i++)
267 memcpy(p, &(pConvViewList->msg_struct_info[i]), sizeof(msg_struct_list_s));
268 p = (void*)((char*)p + sizeof(msg_struct_list_s));
275 int MsgEncodeMsgGetContactCount(MSG_THREAD_COUNT_INFO_S *threadCountInfo, char **ppDest)
277 int dataSize = sizeof(int) + sizeof(int) + sizeof(int) + sizeof(int);
279 MSG_DEBUG("datasize = [%d] \n", dataSize);
281 *ppDest = (char*)new char[dataSize];
283 void* p = (void*)*ppDest;
285 memcpy(p, &(threadCountInfo->totalCount), sizeof(int));
286 p = (void*)((char*)p + sizeof(int));
288 memcpy(p, &(threadCountInfo->unReadCount), sizeof(int));
289 p = (void*)((char*)p + sizeof(int));
291 memcpy(p, &(threadCountInfo->mmsMsgCount), sizeof(int));
292 p = (void*)((char*)p + sizeof(int));
294 memcpy(p, &(threadCountInfo->smsMsgCount), sizeof(int));
295 p = (void*)((char*)p + sizeof(int));
300 int MsgEncodeMemSize(unsigned int *memsize, char **ppDest)
304 dataSize = sizeof(unsigned int);
306 *ppDest = (char*)new char[dataSize];
308 void* p = (void*)*ppDest;
310 memcpy(p, memsize, dataSize);
316 int MsgEncodeSyncMLOperationData(int msgId, int extId, char **ppDest)
320 dataSize = sizeof(int) + sizeof(int);
322 *ppDest = (char*)new char[dataSize];
324 void* p = (void*)*ppDest;
326 memcpy(p, &msgId, sizeof(int));
327 p = (void*)((char*)p + sizeof(int));
329 memcpy(p, &extId, sizeof(int));
335 int MsgEncodeStorageChangeData(const msg_storage_change_type_t storageChangeType, const msg_id_list_s *pMsgIdList, char **ppDest)
340 count = pMsgIdList->nCount;
342 dataSize = sizeof(msg_storage_change_type_t) + sizeof(int) + (sizeof(msg_message_id_t)*count);
344 *ppDest = (char*)new char[dataSize];
346 void* p = (void*)*ppDest;
348 memcpy(p, &storageChangeType, sizeof(msg_storage_change_type_t));
349 p = (void*)((char*)p + sizeof(msg_storage_change_type_t));
351 memcpy(p, &count, sizeof(int));
352 p = (void*)((char*)p + sizeof(int));
354 for (int i = 0; i < count; i++) {
355 memcpy(p, &(pMsgIdList->msgIdList[i]), sizeof(msg_message_id_t));
356 p = (void*)((char*)p + sizeof(msg_message_id_t));
363 int MsgEncodeReportStatus(MSG_REPORT_STATUS_INFO_S* pReportStatus, int count, char **ppDest)
367 dataSize = (sizeof(MSG_REPORT_STATUS_INFO_S)*count + sizeof(int));
369 *ppDest = (char*)new char[dataSize];
371 void* p = (void*)*ppDest;
373 memcpy(p, &count, sizeof(int));
375 p = (void*)(p + sizeof(int));
377 memcpy(p, pReportStatus, sizeof(MSG_REPORT_STATUS_INFO_S)*count);
383 int MsgEncodeThreadId(msg_thread_id_t *pThreadId, char **ppDest)
387 dataSize = (sizeof(msg_thread_id_t));
389 *ppDest = (char*)new char[dataSize];
391 void* p = (void*)*ppDest;
393 memcpy(p, pThreadId, dataSize);
399 int MsgEncodeThreadInfo(MSG_THREAD_VIEW_S *pThreadInfo, char **ppDest)
403 dataSize = sizeof(MSG_THREAD_VIEW_S);
405 *ppDest = (char*)new char[dataSize];
407 void* p = (void*)*ppDest;
409 memcpy(p, pThreadInfo, sizeof(MSG_THREAD_VIEW_S));
411 p = (void*)((char*)p + sizeof(MSG_THREAD_VIEW_S));
419 void MsgDecodeMsgId(char *pSrc, msg_message_id_t *pMsgId)
421 memcpy(pMsgId, pSrc, sizeof(msg_message_id_t));
425 void MsgDecodeCountInfo(char *pSrc, MSG_COUNT_INFO_S *pCountInfo)
427 memcpy(pCountInfo, pSrc, sizeof(MSG_COUNT_INFO_S));
431 void MsgDecodeMemSize(char *pSrc, unsigned int *memsize)
433 memcpy(memsize, pSrc, sizeof(unsigned int));
437 void MsgDecodeMsgInfo(char *pSrc, MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S* pSendOptInfo)
439 memcpy(pMsgInfo, pSrc, sizeof(MSG_MESSAGE_INFO_S));
441 pSrc = pSrc + sizeof(MSG_MESSAGE_INFO_S);
443 memcpy(pSendOptInfo, pSrc, sizeof(MSG_SENDINGOPT_INFO_S));
446 void MsgDecodeRecipientList(char *pSrc, MSG_RECIPIENTS_LIST_S *pRecipientList)
450 memcpy(&count, pSrc, sizeof(int));
451 pSrc = pSrc + sizeof(int);
453 pRecipientList->recipientCnt= count;
454 pRecipientList->recipientAddr = (MSG_ADDRESS_INFO_S*)new char[sizeof(MSG_ADDRESS_INFO_S)*count];
456 MSG_ADDRESS_INFO_S* pInfoTmp = pRecipientList->recipientAddr;
458 for (int i = 0; i < count; i++)
460 memcpy(pInfoTmp, pSrc, sizeof(MSG_ADDRESS_INFO_S));
461 pSrc = pSrc + sizeof(MSG_ADDRESS_INFO_S);
467 void MsgDecodeFolderList(char *pSrc, msg_struct_list_s *pFolderList)
471 memcpy(&count, pSrc, sizeof(int));
472 pSrc = pSrc + sizeof(int);
476 pFolderList->nCount = count;
477 pFolderList->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_FOLDER_INFO_S *)*count];
479 msg_struct_s *pInfoTmp = NULL;
481 for (int i = 0; i < count; i++)
484 pFolderList->msg_struct_info[i] = (msg_struct_t )new char[sizeof(msg_struct_s)];
485 pInfoTmp = (msg_struct_s *)pFolderList->msg_struct_info[i];
486 pInfoTmp->type = MSG_STRUCT_FOLDER_INFO;
487 pInfoTmp->data = new char[sizeof(MSG_FOLDER_INFO_S)];
488 memcpy(pInfoTmp->data, pSrc, sizeof(MSG_FOLDER_INFO_S));
489 pSrc = pSrc + sizeof(MSG_FOLDER_INFO_S);
492 else if ( count == 0 )
494 pFolderList->nCount = count;
495 pFolderList->msg_struct_info = NULL;
499 void MsgDecodeFilterList(char *pSrc, msg_struct_list_s *pFilterList)
503 memcpy(&count, pSrc, sizeof(int));
504 pSrc = pSrc + sizeof(int);
508 pFilterList->nCount = count;
509 pFilterList->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_FILTER_S *)*count];
511 msg_struct_s *pStructTmp = NULL;
513 for (int i = 0; i < count; i++)
515 pFilterList->msg_struct_info[i] = (msg_struct_t )new char[sizeof(msg_struct_s)];
516 pStructTmp = (msg_struct_s *)pFilterList->msg_struct_info[i];
517 pStructTmp->type = MSG_STRUCT_FILTER;
518 pStructTmp->data = new char[sizeof(MSG_FILTER_S)];
519 memcpy(pStructTmp->data, pSrc, sizeof(MSG_FILTER_S));
520 pSrc = pSrc + sizeof(MSG_FILTER_S);
523 else if ( count == 0 )
525 pFilterList->nCount = count;
526 pFilterList->msg_struct_info = NULL;
532 void MsgDecodeFilterFlag(char *pSrc, bool *pSetFlag)
534 memcpy(pSetFlag, pSrc, sizeof(bool));
538 void MsgDecodeMsgType(char *pSrc, MSG_MESSAGE_TYPE_S* pMsgType)
540 memcpy(pMsgType, pSrc, sizeof(MSG_MESSAGE_TYPE_S));
543 void MsgDecodeContactCount(char *pSrc, MSG_THREAD_COUNT_INFO_S *pMsgThreadCountList)
550 memcpy(&count, pSrc, sizeof(int));
551 pSrc = pSrc + sizeof(int);
552 pMsgThreadCountList->totalCount = count;
555 memcpy(&count, pSrc, sizeof(int));
556 pSrc = pSrc + sizeof(int);
557 pMsgThreadCountList->unReadCount = count;
560 memcpy(&count, pSrc, sizeof(int));
561 pSrc = pSrc + sizeof(int);
562 pMsgThreadCountList->mmsMsgCount = count;
565 memcpy(&count, pSrc, sizeof(int));
566 pSrc = pSrc + sizeof(int);
567 pMsgThreadCountList->smsMsgCount = count;
574 void MsgDecodeReportStatus(char *pSrc, msg_struct_list_s *report_list)
581 memcpy(&count, pSrc, sizeof(int));
582 pSrc = pSrc + sizeof(int);
584 report_list->nCount = count;
586 msg_struct_t *report_status = (msg_struct_t *)new char[sizeof(msg_struct_t)*count];
587 for (int i = 0; i < count; i++) {
589 msg_struct_s *report_status_item = new msg_struct_s;
590 report_status_item->type = MSG_STRUCT_REPORT_STATUS_INFO;
591 report_status_item->data = new MSG_REPORT_STATUS_INFO_S;
592 memset(report_status_item->data, 0x00, sizeof(MSG_REPORT_STATUS_INFO_S));
594 MSG_REPORT_STATUS_INFO_S *report_status_info = (MSG_REPORT_STATUS_INFO_S *)report_status_item->data;
595 memcpy(report_status_info, pSrc, sizeof(MSG_REPORT_STATUS_INFO_S));
597 pSrc = pSrc + sizeof(MSG_REPORT_STATUS_INFO_S);
599 report_status[i] = (msg_struct_t)report_status_item;
601 MSG_DEBUG("Report_type = %d, status addr = %s, status = %d, time = %d",
602 report_status_info->type, report_status_info->addressVal,
603 report_status_info->status, report_status_info->statusTime);
606 report_list->msg_struct_info = report_status;
610 void MsgDecodeThreadId(char *pSrc, msg_thread_id_t *pThreadId)
612 memcpy(pThreadId, pSrc, sizeof(msg_thread_id_t));
615 void MsgDecodeThreadInfo(char *pSrc, MSG_THREAD_VIEW_S *pThreadInfo)
617 memcpy(pThreadInfo, pSrc, sizeof(MSG_THREAD_VIEW_S));
621 int MsgMakeEvent(const void *pData, int DataSize, MSG_EVENT_TYPE_T MsgEvent, msg_error_t MsgError, void **ppEvent)
623 MSG_EVENT_S* pMsgEvent = NULL;
625 *ppEvent = (MSG_EVENT_S*)new char[sizeof(MSG_EVENT_S) + DataSize];
627 pMsgEvent = (MSG_EVENT_S*)*ppEvent;
629 pMsgEvent->eventType = MsgEvent;
630 pMsgEvent->result = MsgError;
632 MSG_DEBUG("eventType [%d : %s]", pMsgEvent->eventType, MsgDbgEvtStr(pMsgEvent->eventType));
633 MSG_DEBUG("result [%d]", pMsgEvent->result);
636 memcpy((void*)pMsgEvent->data, pData, DataSize);
638 return (sizeof(MSG_EVENT_S) + DataSize);
641 int msg_verify_number(const char *raw, char *trimmed)
643 if (!(raw && trimmed)) {
644 MSG_DEBUG("Phone Number is NULL");
645 return MSG_ERR_NULL_POINTER;
648 for (int i=0, j=0 ; raw[i] ; i++) {
649 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == ',' || raw[i] == ' ' \
650 || raw[i] == '*' || raw[i] == '#') {
651 trimmed[j++] = raw[i];
652 } else if (raw[i] == '-') {
655 MSG_DEBUG("Unacceptable character in telephone number: [%c]", raw[i]);
656 return MSG_ERR_INVALID_PARAMETER;
660 MSG_DEBUG("Trimming [%s]->[%s]", raw, trimmed);
664 int msg_verify_email(const char *raw)
667 bool atExist = false;
670 MSG_DEBUG("Email is NULL");
671 return MSG_ERR_NULL_POINTER;
674 for (int i = 0; raw[i]; i++) {
679 if (atExist == false) {
683 MSG_DEBUG("Character [@] is included more than twice in email address.");
684 return MSG_ERR_INVALID_PARAMETER;
688 if ((raw[i] >= '0' && raw[i] <= '9') || raw[i] == '+' || raw[i] == '*' || raw[i] == '#') {
690 } else if ((raw[i] >= 'a' && raw[i] <= 'z') ||(raw[i] >= 'A' && raw[i] <= 'Z') ||(raw[i] == '.') || raw[i] == '_' || raw[i] == '-') {
693 } else if (raw[i] == ',') {
694 if (onlyNum == false && atExist == false) {
695 MSG_DEBUG("Unacceptable type in address.");
696 return MSG_ERR_INVALID_PARAMETER;
702 MSG_DEBUG("Unacceptable character in address : [%c]", raw[i]);
703 return MSG_ERR_INVALID_PARAMETER;