<commit_before>/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2016 Treasure Data Inc.
 *
 *  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 <stdio.h>
#include <fluent-bit/flb_output.h>
#include <fluent-bit/flb_utils.h>
#include <cjson.h>

#include <msgpack.h>

#include "nats.h"

int cb_nats_init(struct flb_output_instance *ins, struct flb_config *config,
                   void *data)
{
    struct flb_upstream *upstream;
    struct flb_out_nats_config *ctx;

    /* Set default network configuration */
    if (!ins->host.name) {
        ins->host.name = flb_strdup("127.0.0.1");
    }
    if (ins->host.port == 0) {
        ins->host.port = 4222;
    }

    /* Allocate plugin context */
    ctx = flb_malloc(sizeof(struct flb_out_nats_config));
    if (!ctx) {
        perror("malloc");
        return -1;
    }

    /* Prepare an upstream handler */
    upstream = flb_upstream_create(config,
                                   ins->host.name,
                                   ins->host.port,
                                   FLB_IO_TCP,
                                   NULL);
    if (!upstream) {
        flb_free(ctx);
        return -1;
    }
    ctx->u   = upstream;
    ctx->ins = ins;
    flb_output_set_context(ins, ctx);

    return 0;
}

static int msgpack_to_json(void *data, size_t bytes, char *tag,
                           char **out_json, size_t *out_len)
{
    int i;
    int n_size;
    size_t off = 0;
    time_t atime;
    char tmp_key[32];
    char tmp_val[256];
    char *tmp_ext;
    json_t *j_root;
    json_t *j_arr;
    msgpack_object map;
    msgpack_object root;
    msgpack_object m_key;
    msgpack_object m_val;
    msgpack_packer   mp_pck;
    msgpack_sbuffer  mp_sbuf;
    msgpack_unpacked result;

    /* Convert MsgPack to JSON */
    msgpack_sbuffer_init(&mp_sbuf);
    msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
    msgpack_unpacked_init(&result);

    j_root = json_create_array();

    while (msgpack_unpack_next(&result, data, bytes, &off)) {
        if (result.data.type != MSGPACK_OBJECT_ARRAY) {
            continue;
        }

        j_arr = json_create_array();

        root = result.data;
        if (root.via.array.size != 2) {
            continue;
        }

        atime  = root.via.array.ptr[0].via.u64;
        map    = root.via.array.ptr[1];
        n_size = map.via.map.size + 1;

        json_add_to_array(j_arr, json_create_number(atime));

        json_t *j_obj = json_create_object();

        if (!tag) {
            json_add_to_object(j_obj, "tag",
                               json_create_string("fluentbit"));
        }
        else {
            json_add_to_object(j_obj, "tag",
                               json_create_string(tag));
        }

        for (i = 0; i < n_size - 1; i++) {
            m_key = map.via.map.ptr[i].key;
            m_val = map.via.map.ptr[i].val;

            memcpy(tmp_key, m_key.via.bin.ptr, m_key.via.bin.size);
            tmp_key[m_key.via.bin.size] = '\0';

            if (m_val.type == MSGPACK_OBJECT_NIL) {
                json_add_to_object(j_obj, tmp_key,
                                   json_create_null());
            }
            else if (m_val.type == MSGPACK_OBJECT_BOOLEAN) {
                json_add_to_object(j_obj, tmp_key,
                                   json_create_bool(m_val.via.boolean));
            }
            else if (m_val.type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
                json_add_to_object(j_obj, tmp_key,
                                   json_create_number(m_val.via.u64));
            }
            else if (m_val.type == MSGPACK_OBJECT_NEGATIVE_INTEGER) {
                json_add_to_object(j_obj, tmp_key,
                                   json_create_number(m_val.via.i64));
            }
            else if (m_val.type == MSGPACK_OBJECT_FLOAT) {
                json_add_to_object(j_obj, tmp_key,
                                   json_create_number(m_val.via.f64));
            }
            else if (m_val.type == MSGPACK_OBJECT_STR) {
                if (m_val.via.str.size > sizeof(tmp_val) - 1) {
                    tmp_ext = flb_malloc(m_val.via.str.size + 1);
                }
                else {
                    tmp_ext = tmp_val;
                }
                memcpy(tmp_ext, m_val.via.str.ptr, m_val.via.str.size);
                tmp_ext[m_val.via.str.size] = '\0';

                json_add_to_object(j_obj, tmp_key, json_create_string(tmp_ext));
                if (tmp_ext != tmp_val) {
                    flb_free(tmp_ext);
                }
            }
            else if (m_val.type == MSGPACK_OBJECT_BIN) {
                if (m_val.via.bin.size > sizeof(tmp_val)) {
                    tmp_ext = flb_malloc(m_val.via.bin.size + 1);
                }
                else {
                    tmp_ext = tmp_val;
                }
                memcpy(tmp_ext, m_val.via.bin.ptr, m_val.via.bin.size);
                tmp_ext[m_val.via.bin.size] = '\0';

                json_add_to_object(j_obj, tmp_key, json_create_string(tmp_ext));
                if (tmp_ext != tmp_val) {
                    flb_free(tmp_ext);
                }
            }
        }

        json_add_to_array(j_arr, j_obj);
        json_add_to_array(j_root, j_arr);
    }
    msgpack_unpacked_destroy(&result);

    *out_json = json_print_unformatted(j_root);
    json_delete(j_root);

    *out_len = strlen(*out_json);<commit_msg>out_nats: fix possible mem override (CID 149009)<commit_after>/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2016 Treasure Data Inc.
 *
 *  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 <stdio.h>
#include <fluent-bit/flb_output.h>
#include <fluent-bit/flb_utils.h>
#include <cjson.h>

#include <msgpack.h>

#include "nats.h"

int cb_nats_init(struct flb_output_instance *ins, struct flb_config *config,
                   void *data)
{
    struct flb_upstream *upstream;
    struct flb_out_nats_config *ctx;

    /* Set default network configuration */
    if (!ins->host.name) {
        ins->host.name = flb_strdup("127.0.0.1");
    }
    if (ins->host.port == 0) {
        ins->host.port = 4222;
    }

    /* Allocate plugin context */
    ctx = flb_malloc(sizeof(struct flb_out_nats_config));
    if (!ctx) {
        perror("malloc");
        return -1;
    }

    /* Prepare an upstream handler */
    upstream = flb_upstream_create(config,
                                   ins->host.name,
                                   ins->host.port,
                                   FLB_IO_TCP,
                                   NULL);
    if (!upstream) {
        flb_free(ctx);
        return -1;
    }
    ctx->u   = upstream;
    ctx->ins = ins;
    flb_output_set_context(ins, ctx);

    return 0;
}

static int msgpack_to_json(void *data, size_t bytes, char *tag,
                           char **out_json, size_t *out_len)
{
    int i;
    int n_size;
    size_t off = 0;
    time_t atime;
    char tmp_key[32];
    char tmp_val[256];
    char *tmp_ext;
    json_t *j_root;
    json_t *j_arr;
    msgpack_object map;
    msgpack_object root;
    msgpack_object m_key;
    msgpack_object m_val;
    msgpack_packer   mp_pck;
    msgpack_sbuffer  mp_sbuf;
    msgpack_unpacked result;

    /* Convert MsgPack to JSON */
    msgpack_sbuffer_init(&mp_sbuf);
    msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
    msgpack_unpacked_init(&result);

    j_root = json_create_array();

    while (msgpack_unpack_next(&result, data, bytes, &off)) {
        if (result.data.type != MSGPACK_OBJECT_ARRAY) {
            continue;
        }

        j_arr = json_create_array();

        root = result.data;
        if (root.via.array.size != 2) {
            continue;
        }

        atime  = root.via.array.ptr[0].via.u64;
        map    = root.via.array.ptr[1];
        n_size = map.via.map.size + 1;

        json_add_to_array(j_arr, json_create_number(atime));

        json_t *j_obj = json_create_object();

        if (!tag) {
            json_add_to_object(j_obj, "tag",
                               json_create_string("fluentbit"));
        }
        else {
            json_add_to_object(j_obj, "tag",
                               json_create_string(tag));
        }

        for (i = 0; i < n_size - 1; i++) {
            m_key = map.via.map.ptr[i].key;
            m_val = map.via.map.ptr[i].val;

            memcpy(tmp_key, m_key.via.bin.ptr, m_key.via.bin.size);
            tmp_key[m_key.via.bin.size] = '\0';

            if (m_val.type == MSGPACK_OBJECT_NIL) {
                json_add_to_object(j_obj, tmp_key,
                                   json_create_null());
            }
            else if (m_val.type == MSGPACK_OBJECT_BOOLEAN) {
                json_add_to_object(j_obj, tmp_key,
                                   json_create_bool(m_val.via.boolean));
            }
            else if (m_val.type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
                json_add_to_object(j_obj, tmp_key,
                                   json_create_number(m_val.via.u64));
            }
            else if (m_val.type == MSGPACK_OBJECT_NEGATIVE_INTEGER) {
                json_add_to_object(j_obj, tmp_key,
                                   json_create_number(m_val.via.i64));
            }
            else if (m_val.type == MSGPACK_OBJECT_FLOAT) {
                json_add_to_object(j_obj, tmp_key,
                                   json_create_number(m_val.via.f64));
            }
            else if (m_val.type == MSGPACK_OBJECT_STR) {
                if (m_val.via.str.size > sizeof(tmp_val) - 1) {
                    tmp_ext = flb_malloc(m_val.via.str.size + 1);
                }
                else {
                    tmp_ext = tmp_val;
                }
                memcpy(tmp_ext, m_val.via.str.ptr, m_val.via.str.size);
                tmp_ext[m_val.via.str.size] = '\0';

                json_add_to_object(j_obj, tmp_key, json_create_string(tmp_ext));
                if (tmp_ext != tmp_val) {
                    flb_free(tmp_ext);
                }
            }
            else if (m_val.type == MSGPACK_OBJECT_BIN) {
                if (m_val.via.bin.size > sizeof(tmp_val) - 1) {
                    tmp_ext = flb_malloc(m_val.via.bin.size + 1);
                }
                else {
                    tmp_ext = tmp_val;
                }
                memcpy(tmp_ext, m_val.via.bin.ptr, m_val.via.bin.size);
                tmp_ext[m_val.via.bin.size] = '\0';

                json_add_to_object(j_obj, tmp_key, json_create_string(tmp_ext));
                if (tmp_ext != tmp_val) {
                    flb_free(tmp_ext);
                }
            }
        }

        json_add_to_array(j_arr, j_obj);
        json_add_to_array(j_root, j_arr);
    }
    msgpack_unpacked_destroy(&result);

    *out_json = json_print_unformatted(j_root);
    json_delete(j_root);

    *out_len = strlen(*out_json);
{'fraction_non_alphanumeric': 0.10967476327706875, 'fraction_numerical': 0.009468917249897077, 'mean_word_length': 1.6694505494505494, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '202817', 'n_tokens_mistral': 4190, 'n_tokens_neox': 3992, 'n_words': 754}