Bug about parsing queryparam is 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 OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *entityHandlerRequest)
24 {
25     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - IN");
26
27     OCEntityHandlerRequest *copyOfRequest =
28             (OCEntityHandlerRequest *)OICMalloc(sizeof(OCEntityHandlerRequest));
29
30     if (copyOfRequest)
31     {
32         // Do shallow copy
33         memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest));
34
35         if (copyOfRequest->query)
36         {
37             copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
38             if(!copyOfRequest->query)
39             {
40                 NS_LOG(ERROR, "Copy failed due to allocation failure");
41                 OICFree(copyOfRequest);
42                 return NULL;
43             }
44         }
45
46         if (entityHandlerRequest->payload)
47         {
48             copyOfRequest->payload = (OCPayload *)
49                     (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
50         }
51
52         // Ignore vendor specific header options for example
53         copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
54         copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
55     }
56
57     if (copyOfRequest)
58     {
59         NS_LOG(DEBUG, "Copied client request");
60     }
61     else
62     {
63         NS_LOG(DEBUG, "Error copying client request");
64     }
65
66     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
67     return copyOfRequest;
68 }
69
70 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
71 {
72     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
73
74     OICFree(entityHandlerRequest->query);
75     OCPayloadDestroy(entityHandlerRequest->payload);
76     OICFree(entityHandlerRequest);
77
78     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
79
80     return NS_OK;
81 }
82
83 NSResult NSFreeMessage(NSMessage * obj)
84 {
85     if (!obj)
86     {
87         return NS_ERROR;
88     }
89
90     obj->messageId = 0;
91     (obj->providerId)[0] = '\0';
92
93     NSFreeMalloc(&(obj->dateTime));
94     obj->ttl = 0;
95     NSFreeMalloc(&(obj->title));
96     NSFreeMalloc(&(obj->contentText));
97     NSFreeMalloc(&(obj->sourceName));
98     NSFreeMediaContents(obj->mediaContents);
99
100     OICFree(obj);
101
102     return NS_OK;
103 }
104
105 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
106 {
107     NSMessage * newMsg = NULL;
108
109     if(copyMsg == NULL)
110     {
111         NS_LOG(ERROR, "Copy Msg is NULL");
112         return NULL;
113     }
114
115     newMsg = NSInitializeMessage();
116
117     newMsg->messageId = copyMsg->messageId;
118     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
119
120     if(copyMsg->dateTime)
121     {
122         newMsg->dateTime = OICStrdup(copyMsg->dateTime);
123     }
124
125     newMsg->ttl = copyMsg->ttl;
126
127     if (copyMsg->title)
128     {
129         newMsg->title = OICStrdup(copyMsg->title);
130     }
131
132     if (copyMsg->contentText)
133     {
134         newMsg->contentText = OICStrdup(copyMsg->contentText);
135     }
136
137     if (copyMsg->sourceName)
138     {
139        newMsg->sourceName = OICStrdup(copyMsg->sourceName);
140     }
141
142     if (copyMsg->mediaContents)
143     {
144        newMsg->mediaContents = NSDuplicateMediaContents(copyMsg->mediaContents);
145     }
146
147     return newMsg;
148 }
149
150 NSResult NSFreeSync(NSSyncInfo * obj)
151 {
152     if (!obj)
153     {
154         return NS_ERROR;
155     }
156
157     OICFree(obj);
158
159     return NS_OK;
160 }
161
162 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
163 {
164     NSSyncInfo * newMsg = NULL;
165
166     if(copyMsg == NULL)
167     {
168         NS_LOG(ERROR, "Copy Msg is NULL");
169         return NULL;
170     }
171
172     newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
173
174     newMsg->messageId = copyMsg->messageId;
175     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
176     newMsg->state = copyMsg->state;
177
178     return newMsg;
179 }
180
181 NSResult NSFreeConsumer(NSConsumer * obj)
182 {
183     if (!obj)
184     {
185         return NS_ERROR;
186     }
187
188     (obj->consumerId)[0] = '\0';
189
190     OICFree(obj);
191     obj = NULL;
192
193     return NS_OK;
194 }
195
196 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
197 {
198     NSConsumer * newMsg = NULL;
199
200     if(copyMsg == NULL)
201     {
202         NS_LOG(ERROR, "Copy Msg is NULL");
203         return NULL;
204     }
205
206     newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
207     (newMsg->consumerId)[0] = '\0';
208
209     OICStrcpy(newMsg->consumerId, UUID_STRING_SIZE, copyMsg->consumerId);
210
211     return newMsg;
212 }
213
214 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
215         const char * copyString)
216 {
217     if(copyString)
218     {
219         OCRepPayloadSetPropString(*msgPayload, name, copyString);
220     }
221 }
222
223 void NSDuplicateSetPropertyInt(OCRepPayload** msgPayload, const char * name,
224         int64_t value)
225 {
226     if(value)
227     {
228         OCRepPayloadSetPropInt(*msgPayload, name, value);
229     }
230 }
231
232 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
233 {
234     NS_LOG(DEBUG, "NSGetSyncInfo - IN");
235     char * providerId = NULL;
236     int64_t state;
237
238     if(!payload)
239     {
240         return NULL;
241     }
242     NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
243     if (!retSync)
244     {
245         return NULL;
246     }
247
248     retSync->messageId = 0;
249     retSync->state = NS_SYNC_READ;
250
251     OCRepPayload * repPayload = (OCRepPayload *)payload;
252     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&retSync->messageId))
253     {
254         OICFree(retSync);
255         return NULL;
256     }
257
258     if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
259     {
260         OICFree(retSync);
261         return NULL;
262     }
263
264     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
265     {
266         OICFree(retSync);
267         return NULL;
268     }
269
270     retSync->state = (NSSyncType) state;
271     OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
272     OICFree(providerId);
273
274     NS_LOG_V(DEBUG, "Provider ID : %s", retSync->providerId);
275     NS_LOG_V(DEBUG, "Sync ID : %ld", retSync->messageId);
276     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
277
278     NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
279
280     return retSync;
281 }
282
283 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
284 {
285     uint8_t uuid[UUID_SIZE] = { 0, };
286
287     if (RAND_UUID_OK == OCGenerateUuid(uuid))
288     {
289         if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
290         {
291             return NS_OK;
292         }
293     }
294     return NS_ERROR;
295 }
296
297 char * NSGetValueFromQuery(char *query, char * compareKey)
298 {
299     char *key = NULL;
300     char *value = NULL;
301     char *restOfQuery = NULL;
302     int numKeyValuePairsParsed = 0;
303
304     NS_LOG_V(INFO, "NS Query Params = %s", query);
305
306     if(!query || query[0] == '\0' || !strlen(query))
307     {
308         NS_LOG(ERROR, "query is null or \\0 or size is 0");
309         return NULL;
310     }
311
312     char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
313
314     while(keyValuePair)
315     {
316         if (numKeyValuePairsParsed >= 2)
317         {
318             NS_LOG(ERROR, "More than 2 queries params in URI.");
319             return NULL;
320         }
321
322         key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
323
324         if (!key || !value)
325         {
326             NS_LOG(ERROR, "More than 2 queries params in URI.");
327             return NULL;
328         }
329
330         if (strcmp(key, compareKey) == 0)
331         {
332             NS_LOG_V(DEBUG, "found Key : [%s] - Value : [%s] = ", key, value);
333             return value;
334         }
335
336         ++numKeyValuePairsParsed;
337
338         keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
339     }
340
341     return NULL;
342 }
343
344 NSResult NSFreeMalloc(char ** obj)
345 {
346     if(*obj)
347     {
348         OICFree(*obj);
349         *obj = NULL;
350         return NS_OK;
351     }
352
353     return NS_FAIL;
354 }
355
356 NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
357 {
358     if(!copyObj)
359     {
360         return NULL;
361     }
362
363     NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
364
365     if(copyObj->iconImage)
366     {
367         newObj->iconImage = OICStrdup(copyObj->iconImage);
368     }
369
370     return newObj;
371 }
372
373 NSResult NSFreeMediaContents(NSMediaContents * obj)
374 {
375     if(!obj)
376     {
377         return NS_OK;
378     }
379
380     NSFreeMalloc(&(obj->iconImage));
381     OICFree(obj);
382
383     return NS_OK;
384 }
385
386 NSMessage * NSInitializeMessage()
387 {
388     NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
389     msg->messageId = OICGetCurrentTime(TIME_IN_MS);
390     (msg->providerId)[0] = '\0';
391     msg->type = 0;
392     msg->dateTime = NULL;
393     msg->ttl = 0;
394     msg->title = NULL;
395     msg->contentText = NULL;
396     msg->sourceName = NULL;
397     msg->mediaContents = NULL;
398
399     return msg;
400 }