Skip to content
CPP_RFC_API_void.cpp 41.1 KiB
Newer Older
 * Copyright (c) 2015-2018, Luca Fulchir<luca@fulchir.it>, All rights reserved.
 *
 * 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/wrapper/CPP_RFC_API_void.hpp"
#include "RaptorQ/v1/RFC.hpp"
#include <cmath>
#include <memory>
#include <utility>

/////////////////////
//
//  These templates are just a wrapper around the
//  functionalities offered by the RaptorQ__v1::Impl namespace
//  So if you want to see what the algorithm looks like,
//  you are in the wrong place
//
/////////////////////

namespace RFC6330__v1 {
namespace Impl {

// quic "casting" to avoid really long, repetitive lines.
union RAPTORQ_LOCAL cast_enc {
    void* const _raw;
    Impl::Encoder<uint8_t*,  uint8_t* >* const _8;
    Impl::Encoder<uint16_t*, uint16_t*>* const _16;
    Impl::Encoder<uint32_t*, uint32_t*>* const _32;
    Impl::Encoder<uint64_t*, uint64_t*>* const _64;
    cast_enc (void * raw) : _raw(raw) {}
};
union RAPTORQ_LOCAL cast_dec {
    void* const _raw;
    Impl::Decoder<uint8_t*,  uint8_t* >* const _8;
    Impl::Decoder<uint16_t*, uint16_t*>* const _16;
    Impl::Decoder<uint32_t*, uint32_t*>* const _32;
    Impl::Decoder<uint64_t*, uint64_t*>* const _64;
    cast_dec (void * raw) : _raw(raw) {}
};

// check that we have the right type, else keep error.
static RaptorQ_type init_t (const RaptorQ_type type, const bool is_encoder)
{
    switch (type) {
    case RaptorQ_type::RQ_ENC_8:
        return (is_encoder ? RaptorQ_type::RQ_ENC_8  : RaptorQ_type::RQ_NONE);
    case RaptorQ_type::RQ_ENC_16:
        return (is_encoder ? RaptorQ_type::RQ_ENC_16 : RaptorQ_type::RQ_NONE);
    case RaptorQ_type::RQ_ENC_32:
        return (is_encoder ? RaptorQ_type::RQ_ENC_32 : RaptorQ_type::RQ_NONE);
    case RaptorQ_type::RQ_ENC_64:
        return (is_encoder ? RaptorQ_type::RQ_ENC_64 : RaptorQ_type::RQ_NONE);
    case RaptorQ_type::RQ_DEC_8:
        return (is_encoder ? RaptorQ_type::RQ_NONE : RaptorQ_type::RQ_DEC_8);
    case RaptorQ_type::RQ_DEC_16:
        return (is_encoder ? RaptorQ_type::RQ_NONE : RaptorQ_type::RQ_DEC_16);
    case RaptorQ_type::RQ_DEC_32:
        return (is_encoder ? RaptorQ_type::RQ_NONE : RaptorQ_type::RQ_DEC_32);
    case RaptorQ_type::RQ_DEC_64:
        return (is_encoder ? RaptorQ_type::RQ_NONE : RaptorQ_type::RQ_DEC_64);
    case RaptorQ_type::RQ_NONE:
        break;
    }
    return RaptorQ_type::RQ_NONE;
}


////////////////////
//// Encoder
////////////////////

Encoder_void::~Encoder_void()
{
    switch (_type) {
    case RaptorQ_type::RQ_ENC_8:
        delete reinterpret_cast<Impl::Encoder<uint8_t*,  uint8_t*> *>(_encoder);
        break;
    case RaptorQ_type::RQ_ENC_16:
        delete reinterpret_cast<Impl::Encoder<uint16_t*, uint16_t*>*>(_encoder);
        break;
    case RaptorQ_type::RQ_ENC_32:
        delete reinterpret_cast<Impl::Encoder<uint32_t*, uint32_t*>*>(_encoder);
        break;
    case RaptorQ_type::RQ_ENC_64:
        delete reinterpret_cast<Impl::Encoder<uint64_t*, uint64_t*>*>(_encoder);
        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:
        break;
    }
    _type = RaptorQ_type::RQ_NONE;
    _encoder = nullptr;
}

Encoder_void::Encoder_void (const RaptorQ_type type, const void* data_from,
                                            const void* data_to,
                                            const uint16_t min_subsymbol_size,
                                            const uint16_t symbol_size,
                                            const size_t max_sub_block)
    : _type (init_t (type, true))
{
    _encoder = nullptr;
    switch (_type) {
    case RaptorQ_type::RQ_ENC_8:
        _encoder = new Impl::Encoder<uint8_t*, uint8_t*> (
                    reinterpret_cast<uint8_t*> (const_cast<void*>(data_from)),
                    reinterpret_cast<uint8_t*> (const_cast<void*>(data_to)),
                                                min_subsymbol_size,
                                                symbol_size, max_sub_block);
        break;
    case RaptorQ_type::RQ_ENC_16:
        _encoder = new Impl::Encoder<uint16_t*, uint16_t*> (
                    reinterpret_cast<uint16_t*> (const_cast<void*>(data_from)),
                    reinterpret_cast<uint16_t*> (const_cast<void*>(data_to)),
                                                min_subsymbol_size,
                                                symbol_size, max_sub_block);
        break;
    case RaptorQ_type::RQ_ENC_32:
        _encoder = new Impl::Encoder<uint32_t*, uint32_t*> (
                    reinterpret_cast<uint32_t*> (const_cast<void*>(data_from)),
                    reinterpret_cast<uint32_t*> (const_cast<void*>(data_to)),
                                                min_subsymbol_size,
                                                symbol_size, max_sub_block);
        break;
    case RaptorQ_type::RQ_ENC_64:
        _encoder = new Impl::Encoder<uint64_t*, uint64_t*> (
                    reinterpret_cast<uint64_t*> (const_cast<void*>(data_from)),
                    reinterpret_cast<uint64_t*> (const_cast<void*>(data_to)),
                                                min_subsymbol_size,
                                                symbol_size, max_sub_block);
        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:
        break;
    }
}

Encoder_void::operator bool() const
{
    const cast_enc _enc (_encoder);
    switch (_type) {
    case RaptorQ_type::RQ_ENC_8:
        return static_cast<bool> (_enc._8);
    case RaptorQ_type::RQ_ENC_16:
        return static_cast<bool> (_enc._16);
    case RaptorQ_type::RQ_ENC_32:
        return static_cast<bool> (_enc._32);
    case RaptorQ_type::RQ_ENC_64:
        return static_cast<bool> (_enc._64);
    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:
        break;
    }
    return false;
}

RFC6330_OTI_Common_Data Encoder_void::OTI_Common() const
{
    const cast_enc _enc (_encoder);
    switch (_type) {
    case RaptorQ_type::RQ_ENC_8:
        return _enc._8->OTI_Common();
    case RaptorQ_type::RQ_ENC_16:
        return _enc._16->OTI_Common();
    case RaptorQ_type::RQ_ENC_32:
        return _enc._32->OTI_Common();
    case RaptorQ_type::RQ_ENC_64:
        return _enc._64->OTI_Common();
    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:
        break;
    }
Loading full blame...