Skip to content

feat(core): Add boilerplate for LZMA decompressor. #708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,8 @@ set(SOURCE_FILES_unitTest
src/clp/streaming_compression/Decompressor.hpp
src/clp/streaming_compression/lzma/Compressor.cpp
src/clp/streaming_compression/lzma/Compressor.hpp
src/clp/streaming_compression/lzma/Decompressor.cpp
src/clp/streaming_compression/lzma/Decompressor.hpp
src/clp/streaming_compression/lzma/Constants.hpp
src/clp/streaming_compression/passthrough/Compressor.cpp
src/clp/streaming_compression/passthrough/Compressor.hpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "Decompressor.hpp"

#include <cstddef>

#include "../../ErrorCode.hpp"
#include "../../ReaderInterface.hpp"
#include "../../TraceableException.hpp"

namespace clp::streaming_compression::lzma {
[[nodiscard]] auto Decompressor::try_read(
[[maybe_unused]] char* buf,
[[maybe_unused]] size_t num_bytes_to_read,
[[maybe_unused]] size_t& num_bytes_read
) -> ErrorCode {
return ErrorCode_Unsupported;
}

[[nodiscard]] auto Decompressor::try_seek_from_begin([[maybe_unused]] size_t pos) -> ErrorCode {
return ErrorCode_Unsupported;
}

[[nodiscard]] auto Decompressor::try_get_pos([[maybe_unused]] size_t& pos) -> ErrorCode {
return ErrorCode_Unsupported;
}

auto Decompressor::open(
[[maybe_unused]] char const* compressed_data_buf,
[[maybe_unused]] size_t compressed_data_buf_size
) -> void {
throw OperationFailed(ErrorCode_Unsupported, __FILENAME__, __LINE__);
}

auto Decompressor::open(
[[maybe_unused]] ReaderInterface& file_reader,
[[maybe_unused]] size_t file_read_buffer_capacity
) -> void {
throw OperationFailed(ErrorCode_Unsupported, __FILENAME__, __LINE__);
}

auto Decompressor::close() -> void {
throw OperationFailed(ErrorCode_Unsupported, __FILENAME__, __LINE__);
}

[[nodiscard]] auto Decompressor::get_decompressed_stream_region(
[[maybe_unused]] size_t decompressed_stream_pos,
[[maybe_unused]] char* extraction_buf,
[[maybe_unused]] size_t extraction_len
) -> ErrorCode {
return ErrorCode_Unsupported;
}
} // namespace clp::streaming_compression::lzma
105 changes: 105 additions & 0 deletions components/core/src/clp/streaming_compression/lzma/Decompressor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#ifndef CLP_STREAMING_COMPRESSION_LZMA_DECOMPRESSOR_HPP
#define CLP_STREAMING_COMPRESSION_LZMA_DECOMPRESSOR_HPP

#include <cstddef>

#include "../../ErrorCode.hpp"
#include "../../ReaderInterface.hpp"
#include "../../TraceableException.hpp"
#include "../Constants.hpp"
#include "../Decompressor.hpp"

namespace clp::streaming_compression::lzma {
class Decompressor : public ::clp::streaming_compression::Decompressor {
public:
// Types
class OperationFailed : public TraceableException {
public:
// Constructors
OperationFailed(ErrorCode error_code, char const* const filename, int line_number)
: TraceableException(error_code, filename, line_number) {}

// Methods
[[nodiscard]] auto what() const noexcept -> char const* override {
return "streaming_compression::lzma::Decompressor operation failed";
}
};

// Constructor
Decompressor() : clp::streaming_compression::Decompressor(CompressorType::LZMA) {}

// Destructor
~Decompressor() override = default;

// Delete copy constructor and assignment operator
Decompressor(Decompressor const&) = delete;
auto operator=(Decompressor const&) -> Decompressor& = delete;

// Delete move constructor and assignment operator
// TODO: change to default when the base decompressor class has been updated
Decompressor(Decompressor&&) noexcept = delete;
auto operator=(Decompressor&&) noexcept -> Decompressor& = delete;

// Methods implementing the ReaderInterface
/**
* Tries to read up to a given number of bytes from the decompressor.
* @param buf
* @param num_bytes_to_read The number of bytes to try reading
* @param num_bytes_read The actual number of bytes read
* @return ErrorCode_Unsupported
*/
[[nodiscard]] auto try_read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read)
-> ErrorCode override;

/**
* Tries to seek from the beginning to the given position.
* @param pos
* @return ErrorCode_Unsupported
*/
[[nodiscard]] auto try_seek_from_begin(size_t pos) -> ErrorCode override;

/**
* Tries to get the current position of the read head.
* @param pos Position of the read head in the file
* @return ErrorCode_Unsupported
*/
[[nodiscard]] auto try_get_pos(size_t& pos) -> ErrorCode override;

// Methods implementing the Decompressor interface
/***
* Initialize streaming decompressor to decompress from the specified compressed data buffer
* @param compressed_data_buf
* @param compressed_data_buf_size
* @throw clp::streaming_compression::lzma::Decompressor::OperationFailed if unsupported
*/
auto open(char const* compressed_data_buf, size_t compressed_data_buf_size) -> void override;

/**
* Initializes the decompressor to decompress from an open file
* @param file_reader
* @param file_read_buffer_capacity The maximum amount of data to read from a file at a time
* @throw clp::streaming_compression::lzma::Decompressor::OperationFailed if unsupported
*/
auto open(ReaderInterface& file_reader, size_t file_read_buffer_capacity) -> void override;

/*
* @throw clp::streaming_compression::lzma::Decompressor::OperationFailed if unsupported
*/
auto close() -> void override;

/**
* Decompresses and copies the range of uncompressed data described by decompressed_stream_pos
* and extraction_len into extraction_buf.
* @param decompressed_stream_pos
* @param extraction_buf
* @param extraction_len
* @return ErrorCode_Unsupported
*/
[[nodiscard]] auto get_decompressed_stream_region(
size_t decompressed_stream_pos,
char* extraction_buf,
size_t extraction_len
) -> ErrorCode override;
};
} // namespace clp::streaming_compression::lzma
#endif // CLP_STREAMING_COMPRESSION_LZMA_DECOMPRESSOR_HPP
2 changes: 2 additions & 0 deletions components/core/tests/test-StreamingCompression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "../src/clp/streaming_compression/Compressor.hpp"
#include "../src/clp/streaming_compression/Decompressor.hpp"
#include "../src/clp/streaming_compression/lzma/Compressor.hpp"
#include "../src/clp/streaming_compression/lzma/Decompressor.hpp"
#include "../src/clp/streaming_compression/passthrough/Compressor.hpp"
#include "../src/clp/streaming_compression/passthrough/Decompressor.hpp"
#include "../src/clp/streaming_compression/zstd/Compressor.hpp"
Expand Down Expand Up @@ -133,6 +134,7 @@ TEST_CASE("StreamingCompression", "[StreamingCompression]") {
SECTION("LZMA compression") {
compressor = std::make_unique<clp::streaming_compression::lzma::Compressor>();
compress(std::move(compressor), uncompressed_buffer.data());
decompressor = std::make_unique<clp::streaming_compression::lzma::Decompressor>();
}

boost::filesystem::remove(string(cCompressedFilePath));
Expand Down
Loading