diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/io/xfrm.h | 98 | ||||
-rw-r--r-- | include/xfrm/compress.h | 117 | ||||
-rw-r--r-- | include/xfrm/stream.h | 39 |
3 files changed, 169 insertions, 85 deletions
diff --git a/include/io/xfrm.h b/include/io/xfrm.h index 22a42b6..3e601ea 100644 --- a/include/io/xfrm.h +++ b/include/io/xfrm.h @@ -9,117 +9,45 @@ #include "io/istream.h" #include "io/ostream.h" - -enum { - /** - * @brief Deflate compressor with gzip headers. - * - * This actually creates a gzip compatible file, including a - * gzip header and trailer. - */ - IO_COMPRESSOR_GZIP = 1, - - IO_COMPRESSOR_XZ = 2, - - IO_COMPRESSOR_ZSTD = 3, - - IO_COMPRESSOR_BZIP2 = 4, - - IO_COMPRESSOR_MIN = 1, - IO_COMPRESSOR_MAX = 4, -}; +#include "xfrm/stream.h" #ifdef __cplusplus extern "C" { #endif /** - * @brief Create an input stream that transparently uncompresses data. + * @brief Create an input stream that transparently decodes data. * * @memberof istream_t * * This function creates an input stream that wraps an underlying input stream - * that is compressed and transparently uncompresses the data when reading + * that is encoded/compressed and transparently decodes the data when reading * from it. * - * The new stream takes ownership of the wrapped stream and destroys it when - * the compressor stream is destroyed. If this function fails, the wrapped - * stream is also destroyed. - * * @param strm A pointer to another stream that should be wrapped. - * @param comp_id An identifier describing the compressor to use. + * @param xfrm The transformation stream to use. * * @return A pointer to an input stream on success, NULL on failure. */ -SQFS_INTERNAL istream_t *istream_compressor_create(istream_t *strm, - int comp_id); +SQFS_INTERNAL istream_t *istream_xfrm_create(istream_t *strm, + xfrm_stream_t *xfrm); /** - * @brief Create an output stream that transparently compresses data. + * @brief Create an output stream that transparently encodes data. * * @memberof ostream_t * - * This function creates an output stream that transparently compresses all - * data appended to it and writes the compressed data to an underlying, wrapped - * output stream. - * - * The new stream takes ownership of the wrapped stream and destroys it when - * the compressor stream is destroyed. If this function fails, the wrapped - * stream is also destroyed. + * This function creates an output stream that transparently encodes + * (e.g. compresses) all data appended to it and writes it to an + * underlying, wrapped output stream. * * @param strm A pointer to another stream that should be wrapped. - * @param comp_id An identifier describing the compressor to use. + * @param xfrm The transformation stream to use. * * @return A pointer to an output stream on success, NULL on failure. */ -SQFS_INTERNAL ostream_t *ostream_compressor_create(ostream_t *strm, - int comp_id); - -/** - * @brief Probe the buffered data in an istream to check if it is compressed. - * - * @memberof istream_t - * - * This function peeks into the internal buffer of an input stream to check - * for magic signatures of various compressors. - * - * @param strm A pointer to an input stream to check - * @param probe A callback used to check if raw/decoded data matches an - * expected format. Returns 0 if not, -1 on failure and +1 - * on success. - * - * @return A compressor ID on success, 0 if no match was found, -1 on failure. - */ -SQFS_INTERNAL int istream_detect_compressor(istream_t *strm, - int (*probe)(const sqfs_u8 *data, - size_t size)); - -/** - * @brief Resolve a compressor name to an ID. - * - * @param name A compressor name. - * - * @return A compressor ID on success, -1 on failure. - */ -SQFS_INTERNAL int io_compressor_id_from_name(const char *name); - -/** - * @brief Resolve a id to a compressor name. - * - * @param id A compressor ID. - * - * @return A compressor name on success, NULL on failure. - */ -SQFS_INTERNAL const char *io_compressor_name_from_id(int id); - -/** - * @brief Check if support for a given compressor has been built in. - * - * @param id A compressor ID. - * - * @return True if the compressor is supported, false if not. - */ -SQFS_INTERNAL bool io_compressor_exists(int id); +SQFS_INTERNAL ostream_t *ostream_xfrm_create(ostream_t *strm, + xfrm_stream_t *xfrm); #ifdef __cplusplus } diff --git a/include/xfrm/compress.h b/include/xfrm/compress.h new file mode 100644 index 0000000..b5db985 --- /dev/null +++ b/include/xfrm/compress.h @@ -0,0 +1,117 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * compress.h + * + * Copyright (C) 2021 David Oberhollenzer <goliath@infraroot.at> + */ +#ifndef XFRM_COMPRESS_H +#define XFRM_COMPRESS_H + +#include "xfrm/stream.h" + +typedef struct { + uint32_t flags; + uint32_t level; + + union { + struct { + uint8_t vli; + uint8_t pad0[15]; + } xz; + + struct { + uint16_t window_size; + uint16_t padd0[7]; + } gzip; + + struct { + uint8_t work_factor; + uint8_t padd0[15]; + } bzip2; + + uint64_t padd0[2]; + } opt; +} compressor_config_t; + +typedef enum { + COMP_FLAG_XZ_EXTREME = 0x0001, +} COMP_FLAG_XZ; + +typedef enum { + COMP_XZ_VLI_X86 = 1, + COMP_XZ_VLI_POWERPC = 2, + COMP_XZ_VLI_IA64 = 3, + COMP_XZ_VLI_ARM = 4, + COMP_XZ_VLI_ARMTHUMB = 5, + COMP_XZ_VLI_SPARC = 6, +} COMP_XZ_VLI; + +#define COMP_GZIP_MIN_LEVEL (1) +#define COMP_GZIP_MAX_LEVEL (9) +#define COMP_GZIP_DEFAULT_LEVEL (9) + +#define COMP_GZIP_MIN_WINDOW (8) +#define COMP_GZIP_MAX_WINDOW (15) +#define COMP_GZIP_DEFAULT_WINDOW (15) + +#define COMP_ZSTD_MIN_LEVEL (1) +#define COMP_ZSTD_MAX_LEVEL (22) +#define COMP_ZSTD_DEFAULT_LEVEL (15) + +#define COMP_BZIP2_MIN_LEVEL (1) +#define COMP_BZIP2_MAX_LEVEL (9) +#define COMP_BZIP2_DEFAULT_LEVEL (9) + +#define COMP_BZIP2_MIN_WORK_FACTOR (0) +#define COMP_BZIP2_MAX_WORK_FACTOR (250) +#define COMP_BZIP2_DEFAULT_WORK_FACTOR (30) + +#define COMP_XZ_MIN_LEVEL (0) +#define COMP_XZ_MAX_LEVEL (9) +#define COMP_XZ_DEFAULT_LEVEL (6) + +enum { + XFRM_COMPRESSOR_GZIP = 1, + XFRM_COMPRESSOR_XZ = 2, + XFRM_COMPRESSOR_ZSTD = 3, + XFRM_COMPRESSOR_BZIP2 = 4, + + XFRM_COMPRESSOR_MIN = 1, + XFRM_COMPRESSOR_MAX = 4, +}; + +#ifdef __cplusplus +extern "C" { +#endif + +xfrm_stream_t *compressor_stream_bzip2_create(const compressor_config_t *cfg); + +xfrm_stream_t *decompressor_stream_bzip2_create(void); + +xfrm_stream_t *compressor_stream_xz_create(const compressor_config_t *cfg); + +xfrm_stream_t *decompressor_stream_xz_create(void); + +xfrm_stream_t *compressor_stream_gzip_create(const compressor_config_t *cfg); + +xfrm_stream_t *decompressor_stream_gzip_create(void); + +xfrm_stream_t *compressor_stream_zstd_create(const compressor_config_t *cfg); + +xfrm_stream_t *decompressor_stream_zstd_create(void); + +int xfrm_compressor_id_from_name(const char *name); + +int xfrm_compressor_id_from_magic(const void *data, size_t count); + +const char *xfrm_compressor_name_from_id(int id); + +xfrm_stream_t *compressor_stream_create(int id, const compressor_config_t *cfg); + +xfrm_stream_t *decompressor_stream_create(int id); + +#ifdef __cplusplus +} +#endif + +#endif /* XFRM_COMPRESS_H */ diff --git a/include/xfrm/stream.h b/include/xfrm/stream.h new file mode 100644 index 0000000..01639cd --- /dev/null +++ b/include/xfrm/stream.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * stream.h + * + * Copyright (C) 2021 David Oberhollenzer <goliath@infraroot.at> + */ +#ifndef XFRM_STREAM_H +#define XFRM_STREAM_H + +#include "sqfs/predef.h" + +typedef enum { + XFRM_STREAM_FLUSH_NONE = 0, + XFRM_STREAM_FLUSH_SYNC, + XFRM_STREAM_FLUSH_FULL, + + XFRM_STREAM_FLUSH_COUNT, +} XFRM_STREAM_FLUSH; + +typedef enum { + XFRM_STREAM_ERROR = -1, + XFRM_STREAM_OK = 0, + XFRM_STREAM_END = 1, + XFRM_STREAM_BUFFER_FULL = 2, +} XFRM_STREAM_RESULT; + +typedef struct xfrm_stream_t xfrm_stream_t; + +struct xfrm_stream_t { + sqfs_object_t base; + + int (*process_data)(xfrm_stream_t *stream, + const void *in, sqfs_u32 in_size, + void *out, sqfs_u32 out_size, + sqfs_u32 *in_read, sqfs_u32 *out_written, + int flush_mode); +}; + +#endif /* XFRM_STREAM_H */ |