<commit_before>
#include "u_rpc_data.h"


/**
 * @file u_rpc_data.c
 * @brief Implements serialize and deserialize functions.
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @version 0.1
 * @date 2015-02-09
 */


int type_exists(char type) {
    return type >= RPC_TY_VOID && type <= RPC_TY_STR;
}


int create_message(struct message *msg, const char *command, char return_type,
        int argc, struct rpc_arg *argv) {
    int cpt;

    if (msg == NULL || command == NULL || !type_exists(return_type)) {
        return -1;
    }

    msg->command_length = strlen(command);
    msg->command = strdup(command);
    if (msg->command == NULL) {
        return -1;
    }

    msg->return_type = return_type;
    msg->argc = argc;

    if (argc > 0) {
        if (argv == NULL) {
            free(msg->command);
            return -1;
        }

        msg->argv = malloc(sizeof(struct rpc_arg) * msg->argc);
        if (msg->argv == NULL) {
            free(msg->command);
            return -1;
        }

        cpt = 0;
        while (cpt < msg->argc) {
            msg->argv[cpt].typ       = argv[cpt].typ;

            switch (argv[cpt].typ) {
                case RPC_TY_VOID:
                    msg->argv[cpt].data = NULL;
                    break;

                case RPC_TY_INT:
                    msg->argv[cpt].data = malloc(sizeof(int));
                    if (msg->argv[cpt].data == NULL) {
                        free(msg->command);
                        free(msg->argv);
                        /* FIXME: possible memory leak here. */
                    }
                    memcpy(msg->argv[cpt].data, argv[cpt].data, sizeof(int));
                    break;

                case RPC_TY_STR:
                    msg->argv[cpt].data = strdup((char *)argv[cpt].data);
                    if (msg->argv[cpt].data == NULL) {
                        free(msg->command);
                        free(msg->argv);
                        /* FIXME: possible memory leak here. */
                    }
                    break;

                default:
                    break;
            }

            ++cpt;
        }
    } else {
        msg->argv = NULL;
    }

#ifdef DEBUGLOG
    dwrite_log(STDERR_FILENO, "-- END OF create_message,ret 0 --");
    __debug_display_message(msg);
#endif

    return 0;
}

void free_message(struct message *msg) {
    int cpt;

    free(msg->command);

    cpt = 0;
    while(cpt < msg->argc) {
        free(msg->argv[cpt].data);
        ++cpt;
    }

    free(msg->argv);
}

/**
 * @brief Returns the size of args array according to the protocol.
 * @param argc The number of argument.
 * @param args Arguments.
 * @return the size.
 */
int arg_size(int argc, struct rpc_arg *args) {
    int size, cpt, tmp;

    if (args == NULL) {
        return 0;
    }

    size = cpt = 0;
    while (cpt < argc) {
        ++size;

        switch(args[cpt].typ) {
            case RPC_TY_VOID:
                size += 1;
                break;

            case RPC_TY_STR:
                size += 1 + strlen(args[cpt].data);
                break;

            case RPC_TY_INT:
                memcpy(&tmp, args[cpt].data, sizeof(int));
                if (tmp < 0) {
                    ++size;
                    tmp = -tmp;
                }

                while(tmp != 0) {
                    tmp /= 10;
                    ++size;
                }<commit_msg>u_rpc_data.c: fix void size<commit_after>
#include "u_rpc_data.h"


/**
 * @file u_rpc_data.c
 * @brief Implements serialize and deserialize functions.
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @version 0.1
 * @date 2015-02-09
 */


int type_exists(char type) {
    return type >= RPC_TY_VOID && type <= RPC_TY_STR;
}


int create_message(struct message *msg, const char *command, char return_type,
        int argc, struct rpc_arg *argv) {
    int cpt;

    if (msg == NULL || command == NULL || !type_exists(return_type)) {
        return -1;
    }

    msg->command_length = strlen(command);
    msg->command = strdup(command);
    if (msg->command == NULL) {
        return -1;
    }

    msg->return_type = return_type;
    msg->argc = argc;

    if (argc > 0) {
        if (argv == NULL) {
            free(msg->command);
            return -1;
        }

        msg->argv = malloc(sizeof(struct rpc_arg) * msg->argc);
        if (msg->argv == NULL) {
            free(msg->command);
            return -1;
        }

        cpt = 0;
        while (cpt < msg->argc) {
            msg->argv[cpt].typ       = argv[cpt].typ;

            switch (argv[cpt].typ) {
                case RPC_TY_VOID:
                    msg->argv[cpt].data = NULL;
                    break;

                case RPC_TY_INT:
                    msg->argv[cpt].data = malloc(sizeof(int));
                    if (msg->argv[cpt].data == NULL) {
                        free(msg->command);
                        free(msg->argv);
                        /* FIXME: possible memory leak here. */
                    }
                    memcpy(msg->argv[cpt].data, argv[cpt].data, sizeof(int));
                    break;

                case RPC_TY_STR:
                    msg->argv[cpt].data = strdup((char *)argv[cpt].data);
                    if (msg->argv[cpt].data == NULL) {
                        free(msg->command);
                        free(msg->argv);
                        /* FIXME: possible memory leak here. */
                    }
                    break;

                default:
                    break;
            }

            ++cpt;
        }
    } else {
        msg->argv = NULL;
    }

#ifdef DEBUGLOG
    dwrite_log(STDERR_FILENO, "-- END OF create_message,ret 0 --");
    __debug_display_message(msg);
#endif

    return 0;
}

void free_message(struct message *msg) {
    int cpt;

    free(msg->command);

    cpt = 0;
    while(cpt < msg->argc) {
        free(msg->argv[cpt].data);
        ++cpt;
    }

    free(msg->argv);
}

/**
 * @brief Returns the size of args array according to the protocol.
 * @param argc The number of argument.
 * @param args Arguments.
 * @return the size.
 */
int arg_size(int argc, struct rpc_arg *args) {
    int size, cpt, tmp;

    if (args == NULL) {
        return 0;
    }

    size = cpt = 0;
    while (cpt < argc) {
        ++size;

        switch(args[cpt].typ) {
            case RPC_TY_VOID:
                break;

            case RPC_TY_STR:
                size += 1 + strlen(args[cpt].data);
                break;

            case RPC_TY_INT:
                memcpy(&tmp, args[cpt].data, sizeof(int));
                if (tmp < 0) {
                    ++size;
                    tmp = -tmp;
                }

                while(tmp != 0) {
                    tmp /= 10;
                    ++size;
                }
{'fraction_non_alphanumeric': 0.13082882619500072, 'fraction_numerical': 0.007747405350095015, 'mean_word_length': 1.2882943143812708, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 67, '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': '6153412', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2059, 'n_words': 475}