1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #define _POSIX_C_SOURCE 200112L
23 #include "srmutility.h"
24 #include "srmresourcestrings.h"
26 #include "oic_malloc.h"
29 #define TAG "SRM-UTILITY"
32 * This method initializes the OicParseQueryIter_t struct
34 *@param query - REST query, to be parsed
35 *@param parseIter - OicParseQueryIter_t struct, to be initialized
38 void ParseQueryIterInit(unsigned char * query, OicParseQueryIter_t * parseIter)
40 OC_LOG (INFO, TAG, "Initializing coap iterator");
41 if((NULL == query) || (NULL == parseIter))
44 parseIter->attrPos = NULL;
45 parseIter->attrLen = 0;
46 parseIter->valPos = NULL;
47 parseIter->valLen = 0;
48 coap_parse_iterator_init(query, strlen((char *)query),
49 (unsigned char *)OIC_SEC_REST_QUERY_SEPARATOR, (unsigned char *) "", 0, &parseIter->pi);
53 * This method fills the OicParseQueryIter_t struct with next REST query's
54 * attribute's and value's information
56 *@param parseIter - OicParseQueryIter_t struct, has next query's attribute's & value's info
59 * OicParseQueryIter_t * - has parsed query info
60 * NULL - has no query to parse
62 OicParseQueryIter_t * GetNextQuery(OicParseQueryIter_t * parseIter)
64 OC_LOG (INFO, TAG, "Getting Next Query");
68 unsigned char * qrySeg = NULL;
71 //Get the next query. Querys are separated by OIC_SEC_REST_QUERY_SEPARATOR
72 qrySeg = coap_parse_next(&parseIter->pi);
76 delimPos = strchr((char *)qrySeg, OIC_SEC_REST_QUERY_DELIMETER);
79 parseIter->attrPos = parseIter->pi.pos;
80 parseIter->attrLen = (unsigned char *)delimPos - parseIter->pi.pos;
81 parseIter->valPos = (unsigned char *)delimPos + 1;
82 parseIter->valLen = &qrySeg[parseIter->pi.segment_length] - parseIter->valPos;
90 // TODO This functionality is replicated in all SVR's and therefore we need
91 // to encapsulate it in a common method. However, this may not be the right
92 // file for this method.
93 OCStackResult AddUuidArray(cJSON* jsonRoot, const char* arrayItem,
94 size_t *numUuids, OicUuid_t** uuids )
97 cJSON* jsonObj = cJSON_GetObjectItem(jsonRoot, arrayItem);
98 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
99 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
101 *numUuids = cJSON_GetArraySize(jsonObj);
102 VERIFY_SUCCESS(TAG, *numUuids > 0, ERROR);
103 *uuids = (OicUuid_t*)OICCalloc(*numUuids, sizeof(OicUuid_t));
104 VERIFY_NON_NULL(TAG, *uuids, ERROR);
108 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
110 B64Result b64Ret = B64_OK;
112 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
113 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
114 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
117 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
118 sizeof(base64Buff), &outLen);
120 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof((*uuids)[idxx].id)),
122 memcpy((*uuids)[idxx].id, base64Buff, outLen);
123 } while ( ++idxx < *numUuids);
128 return OC_STACK_ERROR;