Source

pyGAP / game / engine / io / file / decoder.cpp

#include "io/file/decoder.h"

#include <algorithm>

#include "codec/base.h"
#include "io/file/base.h"


namespace io { namespace file {

enum {
    DECODER_SIZE = 16 * 1024
};


Decoder::Decoder() :
    codec_(codec::create()),
    size_(DECODER_SIZE),
    buffer_(new uint8_t [size_])
{
}


Decoder::~Decoder() {
    delete codec_;
    delete buffer_;
}


uint8_t* Decoder::decode(Base* file, uint32_t input_size, uint32_t output_size) {
    uint64_t size = input_size;
    uint8_t* output = new uint8_t [output_size];
    if (!codec_->init(output)) {
        delete output;
        return NULL;
    }
    do {
        size = file->read(buffer_, sizeof(uint8_t), std::min(input_size, size_));
        input_size -= size;
    } while (codec_->decode(NULL, output_size, buffer_, size) && input_size > 0);
    if (!codec_->finish()) {
        delete output;
        return NULL;
    }
    return output;
}


} /* namespace io */ } /* namespace file */