/** * @file include/ActivMiddleware/ActivBase/ZstdFile.h * * @brief Header file for the zstd file class. */ #if (!defined ACTIV_ZSTD_FILE_H) #define ACTIV_ZSTD_FILE_H #include "ActivMiddleware/ActivBase/External.h" #include "ActivMiddleware/ActivBase/FileTypeHelper.h" #include "ActivMiddleware/CxxYZ/memory.h" #include ACTIV_BOOST_INCLUDE(container/vector.hpp) // Necessary Zstd stream forward declarations. struct ZSTD_CCtx_s; typedef struct ZSTD_CCtx_s ZSTD_CCtx; typedef ZSTD_CCtx ZSTD_CStream; struct ZSTD_DCtx_s; typedef struct ZSTD_DCtx_s ZSTD_DCtx; typedef ZSTD_DCtx ZSTD_DStream; //! Activ namespace. namespace Activ { /* * @brief Zstd file class for Zstandard compression. */ class ZstdFile ACTIV_FINAL : public FileTypeHelper { ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(ZstdFile) public: typedef FileTypeHelper Parent; ///< The parent type definiton. static const FileType FILE_TYPE = FILE_TYPE_ZSTD; ///< The file type. static const uint8_t HEADER_SIZE = 4; ///< The file header size. /** * @brief Default constructor. * * @throw StatusCodeException */ ACTIV_BASE_API ZstdFile(); /** * @brief Constructor. * * @param compressionLevel the compression level * @param dictionaryPath the dictionary path. * * @throw StatusCodeException */ ACTIV_BASE_API ZstdFile(const int32_t compressionLevel, const String &dictionaryPath = ""); /** * @brief Move constructor. * * @param rhs transfer ownership of the underlying file managed by @a rhs to this. */ ZstdFile(ACTIV_BOOST_RV_REF(ZstdFile) rhs) : Parent(ACTIV_BOOST_MOVE_BASE(Parent, rhs)), m_compressionLevel(rhs.m_compressionLevel), m_stream(std::move(rhs.m_stream)), m_pCompressionStream(std::move(rhs.m_pCompressionStream)), m_pDecompressionStream(std::move(rhs.m_pDecompressionStream)) { // Inline here for G++. m_dictionaryPath.swap(rhs.m_dictionaryPath); } /** * @brief Destructor. */ ACTIV_BASE_API virtual ~ZstdFile() ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode Create(const String& fileName, const AccessFlags::Type accessFlags, const ShareFlags::Type shareFlags, const bool shouldFailIfExists = false) ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode Open(const String& fileName, const AccessFlags::Type accessFlags, const ShareFlags::Type shareFlags) ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode Close() ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode Flush() ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode GetDecodedPosition(uint64_t& position) const ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode SetPosition(int64_t position, const SeekMethod seekMethod = SEEK_METHOD_BEGIN) ACTIV_OVERRIDE; /** * @brief Set the zstd parameters. * * Note: Changing parameters after writing has started is not allowed. * * @param compressionLevel the compressionLevel. * @param dictionaryPath the dictionary path. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_NOT_SUPPORTED * @retval ... */ ACTIV_BASE_API StatusCode SetParameters(const CompressionLevel compressionLevel, const String &dictionaryPath = ""); /** * @brief Move assignment operator. * * Transfer ownership of a file from another ZstdFile object to this. * * If this object has a file open, it is closed. The underlying file associated with rhs is transferred to this object, * and no file is then associated with rhs. * * @param rhs the file to transfer to this object. * * @return *this */ ZstdFile &operator=(ACTIV_BOOST_RV_REF(ZstdFile) rhs) { // Inline here for G++. if (this != &rhs) { Parent::operator=(ACTIV_BOOST_MOVE_BASE(Parent, rhs)); m_compressionLevel = rhs.m_compressionLevel; m_dictionaryPath.swap(rhs.m_dictionaryPath); m_stream = std::move(rhs.m_stream); m_pCompressionStream.swap(rhs.m_pCompressionStream); m_pDecompressionStream.swap(rhs.m_pDecompressionStream); } return *this; } /** * @brief Check if the header contains the zstd file header. * The magic number is 0xFD2FB528 little endian. * * @param pHeader the pointer to the header. * @param headerLength the length of the header. * * @return whether the header matches the codec. */ ACTIV_BASE_API static bool IsFileType(const void *pHeader, const size_t headerLength); /** * @brief Get the zstd file extension. * * @return the zstd file extension. */ ACTIV_BASE_API static const String &GetFileExtension(); protected: ACTIV_BASE_API virtual StatusCode Decode(void *pBuffer, const size_t nBytesToRead, size_t &nBytesRead) ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode Encode(const void *pBuffer, const size_t nBytesToWrite, size_t &nBytesWritten) ACTIV_OVERRIDE; private: /* * @brief The zstd stream custom deleter class. */ class ZstdStreamDeleter { public: void operator()(ZSTD_CStream * const pCompressionStream) const; void operator()(ZSTD_DStream * const pDecompressionStream) const; }; typedef CxxYZ::unique_ptr CStreamPtr; ///< The compression stream pointer type definition. typedef CxxYZ::unique_ptr DStreamPtr; ///< The decompression stream pointer type definition. typedef Boost::container::vector Buffer; ///< The buffer type definition. /* * @brief The stream class. */ class Stream { public: /* * @brief Default constructor */ ACTIV_BASE_API Stream(); /* * @brief Initialize the stream. * * @param accessFlags the file access flags. * * @retval STATUS_CODE_SUCCESS * @retval ... */ ACTIV_BASE_API StatusCode Initialize(const AccessFlags::Type accessFlags); /* * @brief Reset the stream. */ ACTIV_BASE_API void Reset(); Buffer m_buffer; size_t m_actualBufferSize; size_t m_bufferOffset; size_t m_uncompressedPosition; }; /** * @brief Convert Activ compression level to a zstd compression level. * * @param compressionLevel the compressionLevel * * @throw StatusCodeException * * @return zstd compression level */ CompressionLevel ConvertCompressionLevel(const CompressionLevel compressionLevel); /** * @brief Initialize zstd. * * @retval STATUS_CODE_SUCCESS * @retval ... */ StatusCode InitializeZstd(); /** * @brief Flush the zstd internal buffer. * * @param shouldCloseFrame should close the frame. * * @retval STATUS_CODE_SUCCESS * @retval ... */ StatusCode FlushZstdBuffer(const bool shouldCloseFrame = false); /** * @brief Flush internal buffer to file. * * @retval STATUS_CODE_SUCCESS; * @retval ... */ StatusCode FlushToFile(); /** * @brief Read data from file to the internal buffer. * * @retval STATUS_CODE_SUCCESS; * @retval ... */ StatusCode ReadDataFromFile(); /** * @brief Load dictionary from file to memory. * * @param dictionaryPath the location of the dictionary. * @param dictionaryBuffer the dictinary buffer. * * @retval STATUS_CODE_SUCCESS; * @retval ... */ StatusCode LoadDictionary(const String &dictionaryPath, Buffer &dictionaryBuffer); CompressionLevel m_compressionLevel; ///< The compression level. String m_dictionaryPath; ///< The dictionary path. Stream m_stream; ///< The stream object. CStreamPtr m_pCompressionStream; ///< The compression zstd stream. DStreamPtr m_pDecompressionStream; ///< The decompression zstd stream. }; } // namespace Activ #endif // (!defined ACTIV_ZSTD_FILE_H)