Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / security / src / srmutility.c
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH 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 #define _POSIX_C_SOURCE 200112L
21 #include <string.h>
22
23 #include "srmutility.h"
24 #include "srmresourcestrings.h"
25 #include "logger.h"
26
27 #define TAG  PCF("SRM-UTILITY")
28
29 /**
30  * This method initializes the OicParseQueryIter_t struct
31  *
32  *@param query     - REST query, to be parsed
33  *@param parseIter - OicParseQueryIter_t struct, to be initialized
34  *
35  */
36 void ParseQueryIterInit(unsigned char * query, OicParseQueryIter_t * parseIter)
37 {
38     OC_LOG (INFO, TAG, PCF("Initializing coap iterator"));
39     if((NULL == query) || (NULL == parseIter))
40         return;
41
42     parseIter->attrPos = NULL;
43     parseIter->attrLen = 0;
44     parseIter->valPos = NULL;
45     parseIter->valLen = 0;
46     coap_parse_iterator_init(query, strlen((char *)query),
47           (unsigned char *)OIC_SEC_REST_QUERY_SEPARATOR, (unsigned char *) "", 0, &parseIter->pi);
48 }
49
50
51 /**
52  * This method fills the OicParseQueryIter_t struct with next REST query's
53  * attribute's and value's information
54  *
55  *@param parseIter - OicParseQueryIter_t struct, has next query's attribute's & value's info
56  *
57  * @retval
58  *     OicParseQueryIter_t *  - has parsed query info
59  *     NULL                   - has no query to parse
60  */
61 OicParseQueryIter_t * GetNextQuery(OicParseQueryIter_t * parseIter)
62 {
63     OC_LOG (INFO, TAG, PCF("Getting Next Query"));
64     if(NULL == parseIter)
65         return NULL;
66
67     unsigned char * qrySeg = NULL;
68     char * delimPos;
69
70     //Get the next query. Querys are separated by OIC_SEC_REST_QUERY_SEPARATOR
71     qrySeg = coap_parse_next(&parseIter->pi);
72
73     if(qrySeg)
74     {
75         delimPos = strchr((char *)qrySeg, OIC_SEC_REST_QUERY_DELIMETER);
76         if(delimPos)
77         {
78             parseIter->attrPos = parseIter->pi.pos;
79             parseIter->attrLen = (unsigned char *)delimPos - parseIter->pi.pos;
80             parseIter->valPos  = (unsigned char *)delimPos + 1;
81             parseIter->valLen  = &qrySeg[parseIter->pi.segment_length] - parseIter->valPos;
82             return parseIter;
83         }
84     }
85     return NULL;
86 }