Skip to content
C_RFC_API.cpp 48.4 KiB
Newer Older
Luker's avatar
Luker committed
/*
Luker's avatar
Luker committed
 * Copyright (c) 2015-2016, Luca Fulchir<luca@fulchir.it>, All rights reserved.
Luker's avatar
Luker committed
 *
 * This file is part of "libRaptorQ".
 *
 * libRaptorQ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * libRaptorQ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * and a copy of the GNU Lesser General Public License
 * along with libRaptorQ.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "RaptorQ/v1/RFC.hpp"
Luker's avatar
Luker committed
#include "RaptorQ/v1/wrapper/C_RFC_API.h"
#include "RaptorQ/v1/wrapper/CPP_RFC_API.hpp"
#include "RaptorQ/v1/caches.hpp"
#include <chrono>
Luker's avatar
Luker committed
#include <future>
Luker's avatar
Luker committed
#include <memory>

Luker's avatar
Luker committed
struct RAPTORQ_LOCAL RaptorQ_ptr
{
Luker's avatar
Luker committed
    void *ptr;
    const RaptorQ_type type;
Luker's avatar
Luker committed

Luker's avatar
Luker committed
    RaptorQ_ptr (const RaptorQ_type _type) : ptr (nullptr), type (_type) {}
Luker's avatar
Luker committed
};

struct RAPTORQ_LOCAL RaptorQ_future
{
Luker's avatar
Luker committed
    std::future<std::pair<RFC6330__v1::Error, uint8_t>> f;
Luker's avatar
Luker committed
void RAPTORQ_LOCAL RFC6330_test_del (struct RaptorQ_ptr **ptr);
void RAPTORQ_LOCAL RFC6330_test_del (struct RaptorQ_ptr **ptr)
{
    if (ptr == nullptr || *ptr == nullptr ||
Luker's avatar
Luker committed
                                        (*ptr)->type == RaptorQ_type::RQ_NONE
Luker's avatar
Luker committed
                                                    || (*ptr)->ptr == nullptr) {
        if (ptr != nullptr) {
            delete *ptr;
            *ptr = nullptr;
            return;
        }
        return;
    }
    switch ((*ptr)->type) {
    case RaptorQ_type::RQ_ENC_8:
        if(!(reinterpret_cast<RFC6330__v1::Encoder<uint8_t*, uint8_t*>*> (
                                                                (*ptr)->ptr))) {
            RaptorQ_free (ptr);
            return;
        }
        break;
    case RaptorQ_type::RQ_ENC_16:
        if(!(reinterpret_cast<RFC6330__v1::Encoder<uint16_t, uint16_t*>*> (
                                                                (*ptr)->ptr))) {
            RaptorQ_free (ptr);
            return;
        }
        break;
    case RaptorQ_type::RQ_ENC_32:
        if(!(reinterpret_cast<RFC6330__v1::Encoder<uint32_t*, uint32_t*>*> (
                                                                (*ptr)->ptr))) {
            RaptorQ_free (ptr);
            return;
        }
        break;
    case RaptorQ_type::RQ_ENC_64:
        if(!(reinterpret_cast<RFC6330__v1::Encoder<uint64_t*, uint64_t*>*> (
                                                                (*ptr)->ptr))) {
            RaptorQ_free (ptr);
            return;
        }
        break;
    case RaptorQ_type::RQ_DEC_8:
        if(!(reinterpret_cast<RFC6330__v1::Decoder<uint8_t*, uint8_t*>*> (
                                                                (*ptr)->ptr))) {
            RaptorQ_free (ptr);
            return;
        }
        break;
    case RaptorQ_type::RQ_DEC_16:
        if(!(reinterpret_cast<RFC6330__v1::Decoder<uint16_t*, uint16_t*>*> (
                                                                (*ptr)->ptr))) {
            RaptorQ_free (ptr);
            return;
        }
        break;
    case RaptorQ_type::RQ_DEC_32:
        if(!(reinterpret_cast<RFC6330__v1::Decoder<uint32_t*, uint32_t*>*> (
                                                                (*ptr)->ptr))) {
            RaptorQ_free (ptr);
            return;
        }
        break;
    case RaptorQ_type::RQ_DEC_64:
        if(!(reinterpret_cast<RFC6330__v1::Decoder<uint64_t*, uint64_t*>*> (
                                                                (*ptr)->ptr))) {
            RaptorQ_free (ptr);
            return;
        }
        break;
    case RaptorQ_type::RQ_NONE:
        assert(false && "RaptorQ: C Wrapper: should not have gotten here");
        break;
    }
    return;
}

Luker's avatar
Luker committed

Luker's avatar
Luker committed
struct RaptorQ_ptr *RaptorQ_Enc (const RaptorQ_type type, void *data,
Luker's avatar
Luker committed
                                            const uint64_t size,
                                            const uint16_t min_subsymbol_size,
                                            const uint16_t symbol_size,
                                            const size_t max_memory)
Luker's avatar
Luker committed
{
Luker's avatar
Luker committed
    std::unique_ptr<RaptorQ_ptr> ret (new RaptorQ_ptr (type));
Luker's avatar
Luker committed

Luker's avatar
Luker committed
    switch (type) {
    case RaptorQ_type::RQ_ENC_8:
        ret->ptr = reinterpret_cast<void *> (
                    new RFC6330__v1::Encoder<uint8_t*, uint8_t*> (
                                    reinterpret_cast<uint8_t*> (data),
                                    reinterpret_cast<uint8_t*> (data) + size,
                                    min_subsymbol_size,
                                    symbol_size,
                                    max_memory));
        break;
    case RaptorQ_type::RQ_ENC_16:
        ret->ptr = reinterpret_cast<void *> (
                    new RFC6330__v1::Encoder<uint16_t*, uint16_t*> (
                                    reinterpret_cast<uint16_t*> (data),
                                    reinterpret_cast<uint16_t*> (data) + size,
                                    min_subsymbol_size,
                                    symbol_size,
                                    max_memory));
        break;
    case RaptorQ_type::RQ_ENC_32:
        ret->ptr = reinterpret_cast<void *> (
                    new RFC6330__v1::Encoder<uint32_t*, uint32_t*> (
                                    reinterpret_cast<uint32_t*> (data),
                                    reinterpret_cast<uint32_t*> (data) + size,
                                    min_subsymbol_size,
                                    symbol_size,
                                    max_memory));
        break;
    case RaptorQ_type::RQ_ENC_64:
        ret->ptr = reinterpret_cast<void *> (
                    new RFC6330__v1::Encoder<uint64_t*, uint64_t*> (
                                    reinterpret_cast<uint64_t*> (data),
                                    reinterpret_cast<uint64_t*> (data) + size,
                                    min_subsymbol_size,
                                    symbol_size,
                                    max_memory));
        break;
    case RaptorQ_type::RQ_DEC_8:
    case RaptorQ_type::RQ_DEC_16:
    case RaptorQ_type::RQ_DEC_32:
    case RaptorQ_type::RQ_DEC_64:
    case RaptorQ_type::RQ_NONE:
        return new RaptorQ_ptr (RaptorQ_type::RQ_NONE);
    }
    auto raw_ptr = ret.release();
    RFC6330_test_del (&raw_ptr);
    return raw_ptr;
Luker's avatar
Luker committed
}

struct RaptorQ_ptr *RaptorQ_Dec (const RaptorQ_type type,
Luker's avatar
Luker committed
                            const RaptorQ_OTI_Common_Data common,
                            const RaptorQ_OTI_Scheme_Specific_Data scheme)
Luker's avatar
Luker committed
{
Luker's avatar
Luker committed
    std::unique_ptr<RaptorQ_ptr> ret (new RaptorQ_ptr (type));
Luker's avatar
Luker committed

Luker's avatar
Luker committed
    switch (type) {
    case RaptorQ_type::RQ_DEC_8:
        ret->ptr = reinterpret_cast<void *> (
                new RFC6330__v1::Decoder<uint8_t*, uint8_t*> (common, scheme));
        break;
    case RaptorQ_type::RQ_DEC_16:
        ret->ptr = reinterpret_cast<void *> (
                new RFC6330__v1::Decoder<uint16_t*, uint16_t*> (common,scheme));
        break;
    case RaptorQ_type::RQ_DEC_32:
        ret->ptr = reinterpret_cast<void *> (
                new RFC6330__v1::Decoder<uint32_t*, uint32_t*> (common,scheme));
        break;
    case RaptorQ_type::RQ_DEC_64:
        ret->ptr = reinterpret_cast<void *> (
                new RFC6330__v1::Decoder<uint64_t*, uint64_t*> (common,scheme));
        break;
    case RaptorQ_type::RQ_ENC_8:
Loading full blame...