<commit_before>//******************************************************************
//
// Copyright 2016 Samsung Electronics All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License a
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include "rd_client.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "oic_malloc.h"
#include "oic_string.h"
#include "octypes.h"
#include "ocstack.h"
#include "ocpayload.h"
#include "ocendpoint.h"
#include "experimental/payload_logging.h"
#include "cainterface.h"

#define TAG "OIC_RD_CLIENT"

#ifdef RD_CLIENT

OCStackResult OC_CALL OCRDDiscover(OCDoHandle *handle, OCConnectivityType connectivityType,
                                   OCCallbackData *cbBiasFactor, OCQualityOfService qos)
{
    if (!cbBiasFactor || !cbBiasFactor->cb)
    {
        OIC_LOG(DEBUG, TAG, "No callback function specified.");
        return OC_STACK_INVALID_CALLBACK;
    }

    /* Start a discovery query*/
    char queryUri[MAX_URI_LENGTH] = { '\0' };
    snprintf(queryUri, MAX_URI_LENGTH, "coap://%s%s", OC_MULTICAST_PREFIX, OC_RSRVD_RD_URI);
    OIC_LOG_V(DEBUG, TAG, "Querying RD: %s\n", queryUri);

    return OCDoResource(handle, OC_REST_DISCOVER, queryUri, NULL, NULL, connectivityType, qos,
                        cbBiasFactor, NULL, 0);
}

static void RDPublishContextDeleter(void *ctx)
{
    OCCallbackData *cbData = (OCCallbackData*)ctx;
    if (cbData->cd)
    {
        cbData->cd(cbData->context);
    }
    OICFree(cbData);
}

OCStackApplicationResult RDPublishCallback(void *ctx,
                                           OCDoHandle handle,
                                           OCClientResponse *clientResponse)
{
    OCCallbackData *cbData = (OCCallbackData*)ctx;

    // Update resource unique id in stack.
    OCRepPayload **links = NULL;
    size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0};
    if (clientResponse && clientResponse->payload)
    {
        OCRepPayload *rdPayload = (OCRepPayload *) clientResponse->payload;
        if (!OCRepPayloadGetPropObjectArray(rdPayload, OC_RSRVD_LINKS, &links, dimensions))
        {
            OIC_LOG(DEBUG, TAG, "No links in publish response");
            goto exit;
        }
        for(size_t i = 0; i < dimensions[0]; i++)
        {
            char *uri = NULL;
            if (!OCRepPayloadGetPropString(links[i], OC_RSRVD_HREF, &uri))
            {
                OIC_LOG(ERROR, TAG, "Missing 'href' in publish response");
                goto next;
            }
            OCResourceHandle resourceHandle = OCGetResourceHandleAtUri(uri);
            if (resourceHandle == NULL)
            {
                OIC_LOG_V(ERROR, TAG, "No resource exists with uri: %s", uri);
                goto next;
            }
            int64_t ins = 0;
            if (!OCRepPayloadGetPropInt(links[i], OC_RSRVD_INS, &ins))
            {
                OIC_LOG(ERROR, TAG, "Missing 'ins' in publish response");
                goto next;
            }
            OCBindResourceInsToResource(resourceHandle, ins);
        next:
            OICFree(uri);
        }
    }

exit:
    for (size_t i = 0; i < dimensions[0]; i++)
    {
        OCRepPayloadDestroy(links[i]);
    }
    OICFree(links);
    return cbData->cb(cbData->context, handle, clientResponse);
}

OCStackResult OC_CALL OCRDPublish(OCDoHandle *handle, const char *host,
                                  OCConnectivityType connectivityType,
                                  OCResourceHandle *resourceHandles, uint8_t nHandles,
                                  OCCallbackData *cbData, OCQualityOfService qos)
{
    // Validate input parameters.
    if (!host)
    {
        return OC_STACK_INVALID_IP;
    }

    if (!cbData || !cbData->cb)
    {<commit_msg>resource-directory: Check links before free<commit_after>//******************************************************************
//
// Copyright 2016 Samsung Electronics All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License a
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include "rd_client.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "oic_malloc.h"
#include "oic_string.h"
#include "octypes.h"
#include "ocstack.h"
#include "ocpayload.h"
#include "ocendpoint.h"
#include "experimental/payload_logging.h"
#include "cainterface.h"

#define TAG "OIC_RD_CLIENT"

#ifdef RD_CLIENT

OCStackResult OC_CALL OCRDDiscover(OCDoHandle *handle, OCConnectivityType connectivityType,
                                   OCCallbackData *cbBiasFactor, OCQualityOfService qos)
{
    if (!cbBiasFactor || !cbBiasFactor->cb)
    {
        OIC_LOG(DEBUG, TAG, "No callback function specified.");
        return OC_STACK_INVALID_CALLBACK;
    }

    /* Start a discovery query*/
    char queryUri[MAX_URI_LENGTH] = { '\0' };
    snprintf(queryUri, MAX_URI_LENGTH, "coap://%s%s", OC_MULTICAST_PREFIX, OC_RSRVD_RD_URI);
    OIC_LOG_V(DEBUG, TAG, "Querying RD: %s\n", queryUri);

    return OCDoResource(handle, OC_REST_DISCOVER, queryUri, NULL, NULL, connectivityType, qos,
                        cbBiasFactor, NULL, 0);
}

static void RDPublishContextDeleter(void *ctx)
{
    OCCallbackData *cbData = (OCCallbackData*)ctx;
    if (cbData->cd)
    {
        cbData->cd(cbData->context);
    }
    OICFree(cbData);
}

OCStackApplicationResult RDPublishCallback(void *ctx,
                                           OCDoHandle handle,
                                           OCClientResponse *clientResponse)
{
    OCCallbackData *cbData = (OCCallbackData*)ctx;

    // Update resource unique id in stack.
    OCRepPayload **links = NULL;
    size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0};
    if (clientResponse && clientResponse->payload)
    {
        OCRepPayload *rdPayload = (OCRepPayload *) clientResponse->payload;
        if (!OCRepPayloadGetPropObjectArray(rdPayload, OC_RSRVD_LINKS, &links, dimensions))
        {
            OIC_LOG(DEBUG, TAG, "No links in publish response");
            goto exit;
        }
        for(size_t i = 0; i < dimensions[0]; i++)
        {
            char *uri = NULL;
            if (!OCRepPayloadGetPropString(links[i], OC_RSRVD_HREF, &uri))
            {
                OIC_LOG(ERROR, TAG, "Missing 'href' in publish response");
                goto next;
            }
            OCResourceHandle resourceHandle = OCGetResourceHandleAtUri(uri);
            if (resourceHandle == NULL)
            {
                OIC_LOG_V(ERROR, TAG, "No resource exists with uri: %s", uri);
                goto next;
            }
            int64_t ins = 0;
            if (!OCRepPayloadGetPropInt(links[i], OC_RSRVD_INS, &ins))
            {
                OIC_LOG(ERROR, TAG, "Missing 'ins' in publish response");
                goto next;
            }
            OCBindResourceInsToResource(resourceHandle, ins);
        next:
            OICFree(uri);
        }
    }

exit:
    if (links)
    {
        for (size_t i = 0; i < dimensions[0]; i++)
        {
            OCRepPayloadDestroy(links[i]);
        }
        OICFree(links);
    }
    return cbData->cb(cbData->context, handle, clientResponse);
}

OCStackResult OC_CALL OCRDPublish(OCDoHandle *handle, const char *host,
                                  OCConnectivityType connectivityType,
                                  OCResourceHandle *resourceHandles, uint8_t nHandles,
                                  OCCallbackData *cbData, OCQualityOfService qos)
{
    // Validate input parameters.
    if (!host)
    {
        return OC_STACK_INVALID_IP;
    }

    if (!cbData || !cbData->cb)
    {
{'fraction_non_alphanumeric': 0.14468760630456967, 'fraction_numerical': 0.0043088785576595985, 'mean_word_length': 2.2861400894187778, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5209953', 'n_tokens_mistral': 2776, 'n_tokens_neox': 2617, 'n_words': 648}