aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/compression/zlib/zlib.cpp
blob: b1862164030870b763c0041be98231f08b649853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Zlib Compressor
* (C) 2001 Peter J Jones
*     2001-2007,2014 Jack Lloyd
*     2006 Matt Johnston
*
* Distributed under the terms of the Botan license
*/

#include <botan/zlib.h>
#include <botan/internal/comp_util.h>
#include <zlib.h>

namespace Botan {

namespace {

class Zlib_Stream : public Zlib_Style_Stream<z_stream, Bytef>
   {
   public:
      Zlib_Stream()
         {
         streamp()->opaque = alloc();
         streamp()->zalloc = Compression_Alloc_Info::malloc<unsigned int>;
         streamp()->zfree = Compression_Alloc_Info::free;
         }

      u32bit run_flag() const override { return Z_NO_FLUSH; }
      u32bit flush_flag() const override { return Z_FULL_FLUSH; }
      u32bit finish_flag() const override { return Z_FINISH; }
   };

class Zlib_Compression_Stream : public Zlib_Stream
   {
   public:
      Zlib_Compression_Stream(size_t level, bool raw_deflate)
         {
         // FIXME: allow specifiying memLevel and strategy
         int rc = deflateInit2(streamp(), level, Z_DEFLATED,
                               (raw_deflate ? -15 : 15), 8, Z_DEFAULT_STRATEGY);
         if(rc != Z_OK)
            throw std::runtime_error("zlib deflate initialization failed");
         }

      ~Zlib_Compression_Stream()
         {
         deflateEnd(streamp());
         }

      bool run(u32bit flags) override
         {
         int rc = deflate(streamp(), flags);

         if(rc == Z_MEM_ERROR)
            throw std::bad_alloc();
         else if(rc != Z_OK && rc != Z_STREAM_END)
            throw std::runtime_error("zlib deflate error");

         return (rc == Z_STREAM_END);
         }
   };

class Zlib_Decompression_Stream : public Zlib_Stream
   {
   public:
      Zlib_Decompression_Stream(bool raw_deflate)
         {
         int rc = inflateInit2(streamp(), (raw_deflate ? -15 : 15));

         if(rc == Z_MEM_ERROR)
            throw std::bad_alloc();
         else if(rc != Z_OK)
            throw std::runtime_error("zlib inflate initialization failed");
         }

      ~Zlib_Decompression_Stream()
         {
         inflateEnd(streamp());
         }

      bool run(u32bit flags) override
         {
         int rc = inflate(streamp(), flags);

         if(rc == Z_MEM_ERROR)
            throw std::bad_alloc();
         else if(rc != Z_OK && rc != Z_STREAM_END)
            throw std::runtime_error("zlib deflate error");

         return (rc == Z_STREAM_END);
         }
   };

}

Compression_Stream* Zlib_Compression::make_stream() const
   {
   return new Zlib_Compression_Stream(m_level, m_raw_deflate);
   }

Compression_Stream* Zlib_Decompression::make_stream() const
   {
   return new Zlib_Decompression_Stream(m_raw_deflate);
   }

}