NSDuplicateMessage copy logic not working(bug fixed).
[platform/upstream/iotivity.git] / service / notification / src / common / NSUtil.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "NSUtil.h"
22
23 NSResult NSFreeMessage(NSMessage * obj)
24 {
25     if (!obj)
26     {
27         return NS_ERROR;
28     }
29
30     if (obj->mId)
31     {
32         OICFree(obj->mId);
33         obj->mId = NULL;
34     }
35
36     if (obj->mTitle)
37     {
38         OICFree(obj->mTitle);
39         obj->mTitle = NULL;
40     }
41
42     if (obj->mContentText)
43     {
44         OICFree(obj->mContentText);
45         obj->mContentText = NULL;
46     }
47
48     if (obj->mSource)
49     {
50         OICFree(obj->mSource);
51         obj->mSource = NULL;
52     }
53
54     OICFree(obj);
55
56     return NS_OK;
57 }
58
59 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
60 {
61     NSMessage * newMsg = NULL;
62
63     if(copyMsg == NULL)
64     {
65         NS_LOG(ERROR, "Copy Msg is NULL");
66         return NULL;
67     }
68
69     newMsg = (NSMessage *)OICMalloc(sizeof(NSMessage));
70
71     if (copyMsg->mId)
72     {
73         newMsg->mId = OICStrdup(copyMsg->mId);
74     }
75
76     if (copyMsg->mTitle)
77     {
78         newMsg->mTitle = OICStrdup(copyMsg->mTitle);
79     }
80
81     if (copyMsg->mContentText)
82     {
83         newMsg->mContentText = OICStrdup(copyMsg->mContentText);
84     }
85
86     if (copyMsg->mSource)
87     {
88        newMsg->mSource = OICStrdup(copyMsg->mSource);
89     }
90
91     return newMsg;
92 }
93
94 OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *entityHandlerRequest)
95 {
96     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - IN");
97
98     OCEntityHandlerRequest *copyOfRequest =
99             (OCEntityHandlerRequest *)OICMalloc(sizeof(OCEntityHandlerRequest));
100
101     if (copyOfRequest)
102     {
103         // Do shallow copy
104         memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest));
105
106
107         if (copyOfRequest->query)
108         {
109             copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
110             if(!copyOfRequest->query)
111             {
112                 NS_LOG(ERROR, "Copy failed due to allocation failure");
113                 OICFree(copyOfRequest);
114                 return NULL;
115             }
116         }
117
118         if (entityHandlerRequest->payload)
119         {
120             copyOfRequest->payload = (OCPayload *)
121                     (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
122         }
123
124         // Ignore vendor specific header options for example
125         copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
126         copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
127     }
128
129     if (copyOfRequest)
130     {
131         NS_LOG(DEBUG, "Copied client request");
132     }
133     else
134     {
135         NS_LOG(DEBUG, "Error copying client request");
136     }
137
138     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
139
140     return copyOfRequest;
141 }
142
143 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
144 {
145     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
146
147     OICFree(entityHandlerRequest->query);
148     OCPayloadDestroy(entityHandlerRequest->payload);
149     OICFree(entityHandlerRequest);
150
151     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
152
153     return NS_OK;
154 }
155
156