[*] Initial commit

This commit is contained in:
Lorenzo Cogotti
2021-06-07 16:55:13 +02:00
commit b0ef4dd774
117 changed files with 29737 additions and 0 deletions

134
lonetix/include/df/cpr/bzip2.h Executable file
View File

@@ -0,0 +1,134 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
/**
* \file cpr/bzip2.h
*
* BurrowsWheeler bzip2 compression streaming support library.
*
* \copyright The DoubleFourteen Code Forge (C) All Rights Reserved
* \author Lorenzo Cogotti
*/
#ifndef DF_CPR_BZIP2_H_
#define DF_CPR_BZIP2_H_
#include "stm.h"
/// Bzip2 stream handle.
typedef struct Bzip2StmObj *Bzip2StmHn;
/**
* \brief BZip2 Compression options.
*
* \see `Bz2_InitCompress()`
*/
typedef struct {
/**
* \brief Compression level, from 1 to 9 inclusive.
*
* Higher values imply better compression, at the price of speed loss
* and memory consumption.
* Out of range values are silently clamped to allowed range.
*/
int compression;
/// Compressor load factor.
int factor;
/// Compressor buffer size in bytes, 0 to use suggested value.
size_t bufsiz;
/**
* \brief Debugging message verbosity.
*
* Values higher than 0 make the stream log debug messages to standard
* error, debugging level goes from 0 to 4 inclusive.
* Larger values are silently truncated to the maximum allowed.
*/
unsigned verbose;
} Bzip2CprOpts;
/**
* \brief Decompression options.
*
* \see `Bz2_InitDecompress()`
*/
typedef struct {
/// Decompressor buffer size in bytes, 0 to use suggested value.
size_t bufsiz;
/// Conserve memory during decompression, in spite of speed.
Boolean low_mem;
/**
* \brief Debugging message verbosity.
*
* Values higher than 0 make the stream log debug messages to standard
* error, debugging level goes from 0 to 4 inclusive.
* Larger values are implicitly truncated to the maximum allowed.
*/
unsigned verbose;
} Bzip2DecOpts;
/// BZip2 result status, returned by `Bzip2_GetErrStat()`.
typedef int Bzip2Ret; // 0 == OK
/// Implementation of `StmOps` for BZip2 compression/decompression.
extern const StmOps *const Bzip2_StmOps;
/// Non-closing variant of `Bzip2_StmOps`.
extern const StmOps *const Bzip2_NcStmOps;
/// Return last BZip2 operation result.
Bzip2Ret Bzip2_GetErrStat(void);
/// Convert `Bzip2Ret` value to human readable string.
const char *Bzip2_ErrorString(Bzip2Ret ret);
/**
* \brief Open stream for compression.
*
* \param [in,out] streamp Output stream for compressed data
* \param [in] ops Write operations interface for `streamp`, must not be `NULL` and provide `Write()`
* \param [in] opts Compression options, may be `NULL` for defaults
*
* \return The BZip2 compressor handle on success, `NULL` on failure.
*/
Bzip2StmHn Bzip2_OpenCompress(void *streamp, const StmOps *ops, const Bzip2CprOpts *opts);
/**
* \brief Open a stream for decompressing.
*
* \param [in,out] streamp Input stream for BZip2 compressed data
* \param [in] ops Read operations interface for `streamp`, must not be `NULL` and provide `Read()`
* \param [in] opts Decompression options, may be `NULL` for defaults
*
* \return The BZip2 decompressor handle on success, `NULL` on failure.
*/
Bzip2StmHn Bzip2_OpenDecompress(void *streamp, const StmOps *ops, const Bzip2DecOpts *opts);
/**
* \brief Decompress `nbytes` bytes from `hn` to `buf`.
*
* \return Number of actual bytes written to `buf`, 0 on end of stream,
* -1 on error.
*/
Sint64 Bzip2_Read(Bzip2StmHn hn, void *buf, size_t nbytes);
/**
* \brief Compress `nbytes` bytes from `buf` to `hn`.
*
* \return Number of bytes actually written to `hn`, which may be less
* than `nbytes`, -1 on error.
*
* \note Compression should be finalized with `Bzip2_Finish()` once all
* data is written.
*/
Sint64 Bzip2_Write(Bzip2StmHn hn, const void *buf, size_t nbytes);
/**
* \brief Flush Bzip2 encoder.
*
* Should be called before closing a BZip2 encoder.
*
* \param [in,out] hn Stream to be finalized, must not be `NULL`
*
* \return `OK` on success, `NG` on failure.
*/
Judgement Bzip2_Finish(Bzip2StmHn hn);
/// Close a Bzip2 stream.
void Bzip2_Close(Bzip2StmHn hn);
#endif

121
lonetix/include/df/cpr/flate.h Executable file
View File

@@ -0,0 +1,121 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
/**
* \file cpr/flate.h
*
* Compressor DEFLATE and inflate stream implementation.
*
* \copyright The DoubleFourteen Code Forge (C) All Rights Reserved
* \author Lorenzo Cogotti
*
* \see [RFC 1950](https://tools.ietf.org/html/rfc1950)
* \see [RFC 1951](https://tools.ietf.org/html/rfc1951)
* \see [RFC 1952](https://tools.ietf.org/html/rfc1952)
*/
#ifndef DF_CPR_FLATE_H_
#define DF_CPR_FLATE_H_
#include "stm.h"
/// DEFLATE or inflate stream handle.
typedef struct ZlibStmObj *ZlibStmHn;
/// Supported DEFLATE data formats.
typedef enum {
ZFMT_RFC1952, ///< gzip compression, see [RFC 1952](https://tools.ietf.org/html/rfc1952)
ZFMT_RFC1951, ///< Original deflate format, see [RFC 1951](https://tools.ietf.org/html/rfc1951)
ZFMT_RFC1950 ///< Zlib format, see [RFC 1950](https://tools.ietf.org/html/rfc1950)
} Zlibfmt;
/// Inflate (decompression) options.
typedef struct {
unsigned win_bits; ///< Compression window size in bits
Zlibfmt format; ///< Input DEFLATE encoding format
size_t bufsiz; ///< Input buffer size in bytes, 0 for default
} InflateOpts;
/// DEFLATE (compression) options.
typedef struct {
int compression; ///< Compression, range `[0-9]` (0 = none, 9 = best)
unsigned win_bits; ///< Compression window size in bits
Zlibfmt format; ///< Output DEFLATE format
size_t bufsiz; ///< Output buffer size in bytes, leave to 0 for default
} DeflateOpts;
/// Zlib result status.
typedef Sint64 ZlibRet; // 0 == OK
/**
* \brief Implementation of the `StmOps` interface for DEFLATE streams.
*
* Passing these interfaces to any API working with streams allows it to
* operate on DEFLATE streams.
* `Zlib_StmOps` implements the `Close()` method, while the non-closing
* `Zlib_NcStmOps` leaves `Close()` to `NULL`, effectively preventing any
* attempt to close such stream. Use this variant when this behavior is
* desirable (e.g. streams similar to `stdout` or `stdin`).
*
* @{
* \var Zlib_StmOps
* \var Zlib_NcStmOps
* @}
*/
extern const StmOps *const Zlib_StmOps;
extern const StmOps *const Zlib_NcStmOps;
/// Return last Zlib operation's return status.
ZlibRet Zlib_GetErrStat(void);
/// Convert `ZlibRet` result to human readable string.
const char *Zlib_ErrorString(ZlibRet res);
/**
* \brief Start Zlib decompression over a stream.
*
* \param [in,out] streamp Compressed input source stream
* \param [in] ops Read operations over `streamp`, must not be `NULL` and provide `Read()`
* \param [in] opts Decompression options, `NULL` for defaults
*
* \return Opened Zlib handle on success, `NULL` on failure.
*/
ZlibStmHn Zlib_InflateOpen(void *streamp, const StmOps *ops, const InflateOpts *opts);
/**
* \brief Start Zlib compression over a stream.
*
* \param [in,out] streamp Destination for compressed output
* \param [in] ops Write operations over `ßtreamp`, must not be `NULL` and provide `Write()`
* \param [in] opts Compression options, `NULL` for defaults.
*
* \return Opened Zlib handle on success, `NULL` on failure.
*/
ZlibStmHn Zlib_DeflateOpen(void *streamp, const StmOps *ops, const DeflateOpts *opts);
/**
* \brief Decompress `nbytes` bytes from `hn` into `buf`.
*
* \return Number of bytes actually written to `buf`, at most `nbytes`,
* 0 on end of stream, -1 on error.
*/
Sint64 Zlib_Read(ZlibStmHn hn, void *buf, size_t nbytes);
/**
* \brief Compresses `nbytes` bytes from `buf` to `hn`.
*
* \return Count of bytes actually compressed to `hn`, at most `nbytes`,
* -1 on error.
*
* \note Compression should be finalized with `Zlib_Finish()` once all
* data is written.
*/
Sint64 Zlib_Write(ZlibStmHn hn, const void *buf, size_t nbytes);
/**
* \brief Finalize DEFLATE compression.
*
* \return `OK` on success, `NG` otherwise.
*/
Judgement Zlib_Finish(ZlibStmHn hn);
/// Close Zlib stream handle.
void Zlib_Close(ZlibStmHn hn);
#endif

118
lonetix/include/df/cpr/xz.h Executable file
View File

@@ -0,0 +1,118 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
/**
* \file cpr/xz.h
*
* Compressors, LZMA/LZMA2 encoding and decoding implementation.
*
* \copyright The DoubleFourteen Code Forge (C) All Rights Reserved
* \author Lorenzo Cogotti
*/
#ifndef DF_CPR_XZ_H_
#define DF_CPR_XZ_H_
#include "stm.h"
/// LZMA/LZMA2 stream handle.
typedef struct XzStmObj *XzStmHn;
/// LZMA/LZMA2 data integrity checksum algorithms.
typedef enum {
XZCHK_NONE = 0, ///< Do not include any data checksum
XZCHK_CRC32 = 1, ///< Cyclic Redundancy Check, 32-bit
XZCHK_CRC64 = 4, ///< Cyclic Redundancy Check, 64-bit
XZCHK_SHA256 = 10 ///< Secure Hash Algorithm, 256 bit
} Xzchk;
/// LZMA/LZMA2 compressor flags.
typedef struct {
unsigned compress; ///< Compression level, range [0-9]
Boolean extreme; ///< Severely sacrifice speed for compression
Xzchk chk; ///< Checksum algorithm to use
size_t bufsiz; ///< Output buffer size in bytes
} XzEncOpts;
/// LZMA/LZMA2 decompression flags.
typedef struct {
Uint64 memlimit; ///< Decoder memory usage limit
Boolean no_concat; ///< Do not support concatenated xz streams
Boolean no_chk; ///< Disregard data checksum during decoding
size_t bufsiz; ///< Input buffer size in bytes
} XzDecOpts;
/// LZMA operation status result.
typedef int XzRet; // 0 == OK
/**
* \brief Implementation of the `StmOps` interface for LZMA streams.
*
* Allows any API working with streams to function with LZMA streams.
* `Xz_StmOps` implements the `Close()` method, while the non-closing
* `Xz_NcStmOps` leaves `Close()` to `NULL`, preventing any
* attempt to close the stream. Use this variant when such behavior is
* desirable (e.g. streams similar to `stdout` or `stdin`).
*
* @{
* \var Xz_StmOps
* \var Xz_NcStmOps
* @}
*/
extern const StmOps *const Xz_StmOps;
extern const StmOps *const Xz_NcStmOps;
/// Retrieve last operation's result status.
XzRet Xz_GetErrStat(void);
/// Convert `XzRet` to human readable string.
const char *Xz_ErrorString(XzRet res);
/**
* \brief Open stream for compression.
*
* \param [in,out] streamp Output stream for compressed LZMA data
* \param [in] ops Write operations interface for `streamp`, must not be `NULL` and provide `Write()`
* \param [in] opts Compression options, may be `NULL` for defaults
*
* \return The LZMA compressor handle on success, `NULL` on failure.
*/
XzStmHn Xz_OpenCompress(void *streamp, const StmOps *ops, const XzEncOpts *opts);
/**
* \brief Open an LZMA stream for decompressing.
*
* \param [in,out] streamp Input stream for LZMA compressed data
* \param [in] ops Read operations interface for `streamp`, must not be `NULL` and provide `Read()`
* \param [in] opts Decompression options, may be `NULL` for defaults
*
* \return The LZMA decompressor handle on success, `NULL` on failure.
*/
XzStmHn Xz_OpenDecompress(void *streamp, const StmOps *ops, const XzDecOpts *opts);
/**
* \brief Decompress `nbytes` bytes from `hn` into `buf`.
*
* \return Number of bytes actually written to `buf`, at most `nbytes`,
* 0 on end of stream, -1 on error.
*/
Sint64 Xz_Read(XzStmHn hn, void *buf, size_t nbytes);
/**
* \brief Compresses `nbytes` bytes from `buf` to `hn`.
*
* \return Count of bytes actually compressed to `hn`, at most `nbytes`,
* -1 on error.
*
* \note Compression should be finalized with `Xz_Finish()` once all
* data is written.
*/
Sint64 Xz_Write(XzStmHn hn, const void *buf, size_t nbytes);
/**
* \brief Finalize LZMA compression.
*
* \return `OK` on success, `NG` otherwise.
*/
Judgement Xz_Finish(XzStmHn hn);
/// Close LZMA stream.
void Xz_Close(XzStmHn hn);
#endif