10#include <botan/zlib.h>
12#include <botan/exceptn.h>
13#include <botan/internal/compress_utils.h>
20class Zlib_Stream :
public Zlib_Style_Stream<z_stream, Bytef, unsigned int> {
23 streamp()->opaque = alloc();
28 uint32_t run_flag()
const override {
return Z_NO_FLUSH; }
30 uint32_t flush_flag()
const override {
return Z_SYNC_FLUSH; }
32 uint32_t finish_flag()
const override {
return Z_FINISH; }
34 int compute_window_bits(
int wbits,
int wbits_offset)
const {
35 if(wbits_offset == -1) {
38 return wbits + wbits_offset;
43class Zlib_Compression_Stream :
public Zlib_Stream {
45 Zlib_Compression_Stream(
size_t level,
int wbits,
int wbits_offset = 0) {
46 wbits = compute_window_bits(wbits, wbits_offset);
50 }
else if(level == 0) {
54 int rc = ::deflateInit2(streamp(),
static_cast<int>(level), Z_DEFLATED, wbits, 8, Z_DEFAULT_STRATEGY);
61 ~Zlib_Compression_Stream()
override { ::deflateEnd(streamp()); }
63 Zlib_Compression_Stream(
const Zlib_Compression_Stream& other) =
delete;
64 Zlib_Compression_Stream(Zlib_Compression_Stream&& other) =
delete;
65 Zlib_Compression_Stream& operator=(
const Zlib_Compression_Stream& other) =
delete;
66 Zlib_Compression_Stream& operator=(Zlib_Compression_Stream&& other) =
delete;
68 bool run(uint32_t flags)
override {
69 int rc = ::deflate(streamp(), flags);
71 if(rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) {
75 return (rc == Z_STREAM_END);
79class Zlib_Decompression_Stream :
public Zlib_Stream {
81 Zlib_Decompression_Stream(
int wbits,
int wbits_offset = 0) {
82 int rc = ::inflateInit2(streamp(), compute_window_bits(wbits, wbits_offset));
89 ~Zlib_Decompression_Stream()
override { ::inflateEnd(streamp()); }
91 Zlib_Decompression_Stream(
const Zlib_Decompression_Stream& other) =
delete;
92 Zlib_Decompression_Stream(Zlib_Decompression_Stream&& other) =
delete;
93 Zlib_Decompression_Stream& operator=(
const Zlib_Decompression_Stream& other) =
delete;
94 Zlib_Decompression_Stream& operator=(Zlib_Decompression_Stream&& other) =
delete;
96 bool run(uint32_t flags)
override {
97 int rc = ::inflate(streamp(), flags);
99 if(rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) {
103 return (rc == Z_STREAM_END);
107class Deflate_Compression_Stream
final :
public Zlib_Compression_Stream {
109 Deflate_Compression_Stream(
size_t level,
int wbits) : Zlib_Compression_Stream(level, wbits, -1) {}
112class Deflate_Decompression_Stream
final :
public Zlib_Decompression_Stream {
114 explicit Deflate_Decompression_Stream(
int wbits) : Zlib_Decompression_Stream(wbits, -1) {}
117class Gzip_Compression_Stream
final :
public Zlib_Compression_Stream {
119 Gzip_Compression_Stream(
size_t level,
int wbits, uint8_t os_code, uint64_t hdr_time) :
120 Zlib_Compression_Stream(level, wbits, 16) {
122 m_header.os = os_code;
123 m_header.time =
static_cast<uLong
>(hdr_time);
125 int rc = deflateSetHeader(streamp(), &m_header);
132 ::gz_header m_header;
135class Gzip_Decompression_Stream
final :
public Zlib_Decompression_Stream {
137 explicit Gzip_Decompression_Stream(
int wbits) : Zlib_Decompression_Stream(wbits, 16) {}
142std::unique_ptr<Compression_Stream> Zlib_Compression::make_stream(
size_t level)
const {
143 return std::make_unique<Zlib_Compression_Stream>(level, 15);
146std::unique_ptr<Compression_Stream> Zlib_Decompression::make_stream()
const {
147 return std::make_unique<Zlib_Decompression_Stream>(15);
150std::unique_ptr<Compression_Stream> Deflate_Compression::make_stream(
size_t level)
const {
151 return std::make_unique<Deflate_Compression_Stream>(level, 15);
154std::unique_ptr<Compression_Stream> Deflate_Decompression::make_stream()
const {
155 return std::make_unique<Deflate_Decompression_Stream>(15);
158std::unique_ptr<Compression_Stream> Gzip_Compression::make_stream(
size_t level)
const {
159 return std::make_unique<Gzip_Compression_Stream>(level, 15, m_os_code, m_hdr_time);
162std::unique_ptr<Compression_Stream> Gzip_Decompression::make_stream()
const {
163 return std::make_unique<Gzip_Decompression_Stream>(15);
static void free(void *self, void *ptr)
static void * malloc(void *self, T n, T size)
int(* final)(unsigned char *, CTX *)
constexpr void clear_mem(T *ptr, size_t n)