<commit_before>/*
// Copyright (c) 2016 Intel Corporation
//
// 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 at
//
//      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 <stddef.h>
#include <os/os_mempool.h>

#include "oic/oc_rep.h"
#include "oic/oc_log.h"
#include "config.h"
#include "port/oc_assert.h"
#include "api/oc_priv.h"
#include <tinycbor/cbor_mbuf_writer.h>
#include <tinycbor/cbor_mbuf_reader.h>

#ifdef OC_CLIENT
static struct os_mempool oc_rep_objects;
static uint8_t oc_rep_objects_area[OS_MEMPOOL_BYTES(EST_NUM_REP_OBJECTS,
      sizeof(oc_rep_t))];
#endif

static const CborEncoder g_empty;
static struct os_mbuf *g_outm;
CborEncoder g_encoder, root_map, links_array;
CborError g_err;
struct cbor_mbuf_writer g_buf_writer;

void
oc_rep_new(struct os_mbuf *m)
{
    g_err = CborNoError;
    g_outm = m;
    cbor_mbuf_writer_init(&g_buf_writer, m);
    cbor_encoder_init(&g_encoder, &g_buf_writer.enc, 0);
}

int
oc_rep_finalize(void)
{
    int size = OS_MBUF_PKTLEN(g_outm);
    oc_rep_reset();
    if (g_err != CborNoError) {
        return -1;
    }
    return size;
}

void
oc_rep_reset(void)
{
    g_encoder = g_empty;
}

#ifdef OC_CLIENT
static oc_rep_t *
_alloc_rep(void)
{
    oc_rep_t *rep = os_memblock_get(&oc_rep_objects);
#ifdef DEBUG
    oc_assert(rep != NULL);
#endif
    return rep;
}

static void
_free_rep(oc_rep_t *rep_value)
{
    os_memblock_put(&oc_rep_objects, rep_value);
}

void
oc_free_rep(oc_rep_t *rep)
{
    if (rep == NULL) {
        return;
    }
    oc_free_rep(rep->next);
    switch (rep->type) {<commit_msg>oic; on oc_reset memset() g_encoder with 0.<commit_after>/*
// Copyright (c) 2016 Intel Corporation
//
// 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 at
//
//      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 <stddef.h>
#include <os/os_mempool.h>

#include "oic/oc_rep.h"
#include "oic/oc_log.h"
#include "config.h"
#include "port/oc_assert.h"
#include "api/oc_priv.h"
#include <tinycbor/cbor_mbuf_writer.h>
#include <tinycbor/cbor_mbuf_reader.h>

#ifdef OC_CLIENT
static struct os_mempool oc_rep_objects;
static uint8_t oc_rep_objects_area[OS_MEMPOOL_BYTES(EST_NUM_REP_OBJECTS,
      sizeof(oc_rep_t))];
#endif

static struct os_mbuf *g_outm;
CborEncoder g_encoder, root_map, links_array;
CborError g_err;
struct cbor_mbuf_writer g_buf_writer;

void
oc_rep_new(struct os_mbuf *m)
{
    g_err = CborNoError;
    g_outm = m;
    cbor_mbuf_writer_init(&g_buf_writer, m);
    cbor_encoder_init(&g_encoder, &g_buf_writer.enc, 0);
}

int
oc_rep_finalize(void)
{
    int size = OS_MBUF_PKTLEN(g_outm);
    oc_rep_reset();
    if (g_err != CborNoError) {
        return -1;
    }
    return size;
}

void
oc_rep_reset(void)
{
    memset(&g_encoder, 0, sizeof(g_encoder));
}

#ifdef OC_CLIENT
static oc_rep_t *
_alloc_rep(void)
{
    oc_rep_t *rep = os_memblock_get(&oc_rep_objects);
#ifdef DEBUG
    oc_assert(rep != NULL);
#endif
    return rep;
}

static void
_free_rep(oc_rep_t *rep_value)
{
    os_memblock_put(&oc_rep_objects, rep_value);
}

void
oc_free_rep(oc_rep_t *rep)
{
    if (rep == NULL) {
        return;
    }
    oc_free_rep(rep->next);
    switch (rep->type) {
{'fraction_non_alphanumeric': 0.11083683041224389, 'fraction_numerical': 0.005924463095531967, 'mean_word_length': 4.065, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5415191', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1525, 'n_words': 385}