Source

pyGAP / game / engine / codec / zlma.cpp

#ifdef COMPRESSION_LZMA

#include "codec/base.h"
#include "codec/lzma.h"

#include <cstring>

#include "lzma.h"


namespace codec {


Lzma::Lzma() : Base() {
    stream_ = new lzma_stream;
}


Lzma::~Lzma() {
    delete static_cast<lzma_stream*>(stream_);
}


bool Lzma::has_error() {
    return result_ != LZMA_OK && result_ != LZMA_FINISH;
}


bool Lzma::init(uint8_t* output) {
    lzma_stream* stream = static_cast<lzma_stream*>(stream_);
    memset(stream, 0, sizeof(lzma_stream));
    stream->next_out = output;
    result_ = lzma_stream_decoder(stream, UINT64_MAX, LZMA_CONCATENATED);
    return result_ == LZMA_OK;
}


bool Lzma::finish() {
    if (result_ == LZMA_STREAM_END)
        return true;
    result_ = lzma_code(static_cast<lzma_stream*>(stream_), LZMA_FINISH);
    return result_ == LZMA_STREAM_END;
}


bool Lzma::decode(uint8_t* output, uint32_t output_size, uint8_t* input,  uint32_t input_size)
{
    lzma_stream* stream = static_cast<lzma_stream*>(stream_);
    if (output)
        stream->next_out = output;
    stream->next_in = input;
    stream->avail_in = input_size;
    do {
        stream->avail_out = output_size;
        result_ = lzma_code(stream, LZMA_RUN);
        if (result_ != LZMA_OK)
            return false;
    } while (stream->avail_out == 0);
    return result_ != LZMA_STREAM_END;
}


} /* namespace codec */


#endif