<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 *
 *    Copyright 2018 (c) Fraunhofer IOSB (Author: <NAME>)
 */

/* Enable POSIX features */
#if !defined(_XOPEN_SOURCE)
# define _XOPEN_SOURCE 600
#endif
#ifndef _DEFAULT_SOURCE
# define _DEFAULT_SOURCE
#endif
/* On older systems we need to define _BSD_SOURCE.
 * _DEFAULT_SOURCE is an alias for that. */
#ifndef _BSD_SOURCE
# define _BSD_SOURCE
#endif

#include <open62541/types.h>

#include <stdio.h>

/* Internal headers */
#include <open62541/types_generated.h>
#include <open62541/types_generated_handling.h>

#include "ua_pubsub_networkmessage.h"
#include "ua_types_encoding_binary.h"
#include "ua_types_encoding_json.h"

static UA_StatusCode
encode(const UA_ByteString *buf, UA_ByteString *out,
       const UA_DataType *type) {
    void *data = malloc(type->memSize);
    if(!data)
        return UA_STATUSCODE_BADOUTOFMEMORY;

    size_t offset = 0;
    UA_StatusCode retval = UA_decodeBinary(buf, &offset, data, type, NULL);
    if(retval != UA_STATUSCODE_GOOD) {
        free(data);
        return retval;
    }
    if(offset != buf->length) {
        UA_delete(data, type);
        fprintf(stderr, "Input buffer not completely read\n");
        return UA_STATUSCODE_BADINTERNALERROR;
    }

    size_t jsonLength = UA_calcSizeJson(data, type, NULL, 0, NULL, 0, true);
    retval = UA_ByteString_allocBuffer(out, jsonLength);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_delete(data, type);
        return retval;
    }

    uint8_t *bufPos = &out->data[0];
    const uint8_t *bufEnd = &out->data[out->length];
    retval = UA_encodeJson(data, type, &bufPos, &bufEnd, NULL, 0, NULL, 0, true);
    UA_delete(data, type);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_ByteString_deleteMembers(out);
        return retval;
    }

    out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
    return UA_STATUSCODE_GOOD;
}

static UA_StatusCode
decode(const UA_ByteString *buf, UA_ByteString *out,
       const UA_DataType *type) {
    void *data = malloc(type->memSize);
    if(!data)
        return UA_STATUSCODE_BADOUTOFMEMORY;

    UA_StatusCode retval = UA_decodeJson(buf, data, type);
    if(retval != UA_STATUSCODE_GOOD) {
        free(data);
        return retval;
    }

    size_t binLength = UA_calcSizeBinary(data, type);
    retval = UA_ByteString_allocBuffer(out, binLength);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_delete(data, type);
        return retval;
    }

    uint8_t *bufPos = &out->data[0];
    const uint8_t *bufEnd = &out->data[out->length];
    retval = UA_encodeBinary(data, type, &bufPos, &bufEnd, NULL, NULL);
    UA_delete(data, type);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_ByteString_deleteMembers(out);
        return retval;
    }

    out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
    return UA_STATUSCODE_GOOD;
}

#ifdef UA_ENABLE_PUBSUB

static UA_StatusCode
encodeNetworkMessage(const UA_ByteString *buf, UA_ByteString *out) {
    size_t offset = 0;
    UA_NetworkMessage msg;
    UA_StatusCode retval = UA_NetworkMessage_decodeBinary(buf, &offset, &msg);
    if(retval != UA_STATUSCODE_GOOD)
        return retval;

    if(offset != buf->length) {
        UA_NetworkMessage_deleteMembers(&msg);
        fprintf(stderr, "Input buffer not completely read\n");
        return UA_STATUSCODE_BADINTERNALERROR;
    }

    size_t jsonLength = UA_NetworkMessage_calcSizeJson(&msg, NULL, 0, NULL, 0, true);
    retval = UA_ByteString_allocBuffer(out, jsonLength);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_NetworkMessage_deleteMembers(&msg);
        return retval;
    }

    uint8_t *bufPos = &out->data[0];
    const uint8_t *bufEnd = &out->data[out->length];
    retval = UA_NetworkMessage_encodeJson(&msg, &bufPos, &bufEnd, NULL, 0, NULL, 0, true);
    UA_NetworkMessage_deleteMembers(&msg);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_ByteString_deleteMembers(out);
        return retval;
    }

    out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
    return UA_STATUSCODE_GOOD;
}

static UA_StatusCode
decodeNetworkMessage(const UA_ByteString *buf, UA_ByteString *out) {
    UA_NetworkMessage msg;
    UA_StatusCode retval = UA_NetworkMessage_decodeJson(&msg, buf);
    if(retval != UA_STATUSCODE_GOOD)
        return retval;

    size_t binLength = UA_NetworkMessage_calcSizeBinary(&msg);<commit_msg>fix(tools): Fix ua2json after PubSub API change<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 *
 *    Copyright 2018 (c) Fraunhofer IOSB (Author: <NAME>)
 */

/* Enable POSIX features */
#if !defined(_XOPEN_SOURCE)
# define _XOPEN_SOURCE 600
#endif
#ifndef _DEFAULT_SOURCE
# define _DEFAULT_SOURCE
#endif
/* On older systems we need to define _BSD_SOURCE.
 * _DEFAULT_SOURCE is an alias for that. */
#ifndef _BSD_SOURCE
# define _BSD_SOURCE
#endif

#include <open62541/types.h>

#include <stdio.h>

/* Internal headers */
#include <open62541/types_generated.h>
#include <open62541/types_generated_handling.h>

#include "ua_pubsub_networkmessage.h"
#include "ua_types_encoding_binary.h"
#include "ua_types_encoding_json.h"

static UA_StatusCode
encode(const UA_ByteString *buf, UA_ByteString *out,
       const UA_DataType *type) {
    void *data = malloc(type->memSize);
    if(!data)
        return UA_STATUSCODE_BADOUTOFMEMORY;

    size_t offset = 0;
    UA_StatusCode retval = UA_decodeBinary(buf, &offset, data, type, NULL);
    if(retval != UA_STATUSCODE_GOOD) {
        free(data);
        return retval;
    }
    if(offset != buf->length) {
        UA_delete(data, type);
        fprintf(stderr, "Input buffer not completely read\n");
        return UA_STATUSCODE_BADINTERNALERROR;
    }

    size_t jsonLength = UA_calcSizeJson(data, type, NULL, 0, NULL, 0, true);
    retval = UA_ByteString_allocBuffer(out, jsonLength);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_delete(data, type);
        return retval;
    }

    uint8_t *bufPos = &out->data[0];
    const uint8_t *bufEnd = &out->data[out->length];
    retval = UA_encodeJson(data, type, &bufPos, &bufEnd, NULL, 0, NULL, 0, true);
    UA_delete(data, type);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_ByteString_deleteMembers(out);
        return retval;
    }

    out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
    return UA_STATUSCODE_GOOD;
}

static UA_StatusCode
decode(const UA_ByteString *buf, UA_ByteString *out,
       const UA_DataType *type) {
    void *data = malloc(type->memSize);
    if(!data)
        return UA_STATUSCODE_BADOUTOFMEMORY;

    UA_StatusCode retval = UA_decodeJson(buf, data, type);
    if(retval != UA_STATUSCODE_GOOD) {
        free(data);
        return retval;
    }

    size_t binLength = UA_calcSizeBinary(data, type);
    retval = UA_ByteString_allocBuffer(out, binLength);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_delete(data, type);
        return retval;
    }

    uint8_t *bufPos = &out->data[0];
    const uint8_t *bufEnd = &out->data[out->length];
    retval = UA_encodeBinary(data, type, &bufPos, &bufEnd, NULL, NULL);
    UA_delete(data, type);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_ByteString_deleteMembers(out);
        return retval;
    }

    out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
    return UA_STATUSCODE_GOOD;
}

#ifdef UA_ENABLE_PUBSUB

static UA_StatusCode
encodeNetworkMessage(const UA_ByteString *buf, UA_ByteString *out) {
    size_t offset = 0;
    UA_NetworkMessage msg;
    UA_StatusCode retval = UA_NetworkMessage_decodeBinary(buf, &offset, &msg);
    if(retval != UA_STATUSCODE_GOOD)
        return retval;

    if(offset != buf->length) {
        UA_NetworkMessage_deleteMembers(&msg);
        fprintf(stderr, "Input buffer not completely read\n");
        return UA_STATUSCODE_BADINTERNALERROR;
    }

    size_t jsonLength = UA_NetworkMessage_calcSizeJson(&msg, NULL, 0, NULL, 0, true);
    retval = UA_ByteString_allocBuffer(out, jsonLength);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_NetworkMessage_deleteMembers(&msg);
        return retval;
    }

    uint8_t *bufPos = &out->data[0];
    const uint8_t *bufEnd = &out->data[out->length];
    retval = UA_NetworkMessage_encodeJson(&msg, &bufPos, &bufEnd, NULL, 0, NULL, 0, true);
    UA_NetworkMessage_deleteMembers(&msg);
    if(retval != UA_STATUSCODE_GOOD) {
        UA_ByteString_deleteMembers(out);
        return retval;
    }

    out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
    return UA_STATUSCODE_GOOD;
}

static UA_StatusCode
decodeNetworkMessage(const UA_ByteString *buf, UA_ByteString *out) {
    UA_NetworkMessage msg;
    UA_StatusCode retval = UA_NetworkMessage_decodeJson(&msg, buf);
    if(retval != UA_STATUSCODE_GOOD)
        return retval;

    size_t binLength = UA_NetworkMessage_calcSizeBinary(&msg, NULL);
{'fraction_non_alphanumeric': 0.11179039301310044, 'fraction_numerical': 0.009934497816593887, 'mean_word_length': 3.6931352459016393, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5360868', 'n_tokens_mistral': 3264, 'n_tokens_neox': 3183, 'n_words': 692}