• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5395967798

28 Jun 2023 12:28AM UTC coverage: 91.737% (+0.01%) from 91.726%
5395967798

push

github

randombit
Merge GH #3606 Add example of custom entropy source

78190 of 85233 relevant lines covered (91.74%)

12513919.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.94
/src/lib/compression/zlib/zlib.cpp
1
/*
2
* Zlib Compressor
3
* (C) 2001 Peter J Jones
4
*     2001-2007,2014 Jack Lloyd
5
*     2006 Matt Johnston
6
*
7
* Botan is released under the Simplified BSD License (see license.txt)
8
*/
9

10
#include <botan/zlib.h>
11

12
#include <botan/exceptn.h>
13
#include <botan/internal/compress_utils.h>
14
#include <zlib.h>
15

16
namespace Botan {
17

18
namespace {
19

20
class Zlib_Stream : public Zlib_Style_Stream<z_stream, Bytef, unsigned int> {
57✔
21
   public:
22
      Zlib_Stream() {
139✔
23
         streamp()->opaque = alloc();
139✔
24
         streamp()->zalloc = Compression_Alloc_Info::malloc<unsigned int>;
139✔
25
         streamp()->zfree = Compression_Alloc_Info::free;
139✔
26
      }
27

28
      uint32_t run_flag() const override { return Z_NO_FLUSH; }
228✔
29

30
      uint32_t flush_flag() const override { return Z_SYNC_FLUSH; }
30✔
31

32
      uint32_t finish_flag() const override { return Z_FINISH; }
77✔
33

34
      int compute_window_bits(int wbits, int wbits_offset) const {
139✔
35
         if(wbits_offset == -1) {
139✔
36
            return -wbits;
40✔
37
         } else {
38
            return wbits + wbits_offset;
99✔
39
         }
40
      }
41
};
42

43
class Zlib_Compression_Stream : public Zlib_Stream {
44
   public:
45
      Zlib_Compression_Stream(size_t level, int wbits, int wbits_offset = 0) {
77✔
46
         wbits = compute_window_bits(wbits, wbits_offset);
77✔
47

48
         if(level >= 9) {
77✔
49
            level = 9;
50
         } else if(level == 0) {
35✔
51
            level = 6;
4✔
52
         }
53

54
         int rc = ::deflateInit2(streamp(), static_cast<int>(level), Z_DEFLATED, wbits, 8, Z_DEFAULT_STRATEGY);
77✔
55

56
         if(rc != Z_OK) {
77✔
57
            throw Compression_Error("deflateInit2", ErrorType::ZlibError, rc);
×
58
         }
59
      }
77✔
60

61
      ~Zlib_Compression_Stream() override { ::deflateEnd(streamp()); }
36✔
62

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;
67

68
      bool run(uint32_t flags) override {
202✔
69
         int rc = ::deflate(streamp(), flags);
202✔
70

71
         if(rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) {
202✔
72
            throw Compression_Error("zlib deflate", ErrorType::ZlibError, rc);
×
73
         }
74

75
         return (rc == Z_STREAM_END);
202✔
76
      }
77
};
78

79
class Zlib_Decompression_Stream : public Zlib_Stream {
80
   public:
81
      Zlib_Decompression_Stream(int wbits, int wbits_offset = 0) {
62✔
82
         int rc = ::inflateInit2(streamp(), compute_window_bits(wbits, wbits_offset));
124✔
83

84
         if(rc != Z_OK) {
62✔
85
            throw Compression_Error("inflateInit2", ErrorType::ZlibError, rc);
×
86
         }
87
      }
62✔
88

89
      ~Zlib_Decompression_Stream() override { ::inflateEnd(streamp()); }
21✔
90

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;
95

96
      bool run(uint32_t flags) override {
136✔
97
         int rc = ::inflate(streamp(), flags);
136✔
98

99
         if(rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) {
136✔
100
            throw Compression_Error("zlib inflate", ErrorType::ZlibError, rc);
×
101
         }
102

103
         return (rc == Z_STREAM_END);
136✔
104
      }
105
};
106

107
class Deflate_Compression_Stream final : public Zlib_Compression_Stream {
108
   public:
109
      Deflate_Compression_Stream(size_t level, int wbits) : Zlib_Compression_Stream(level, wbits, -1) {}
20✔
110
};
111

112
class Deflate_Decompression_Stream final : public Zlib_Decompression_Stream {
113
   public:
114
      explicit Deflate_Decompression_Stream(int wbits) : Zlib_Decompression_Stream(wbits, -1) {}
20✔
115
};
116

117
class Gzip_Compression_Stream final : public Zlib_Compression_Stream {
118
   public:
119
      Gzip_Compression_Stream(size_t level, int wbits, uint8_t os_code, uint64_t hdr_time) :
21✔
120
            Zlib_Compression_Stream(level, wbits, 16) {
21✔
121
         clear_mem(&m_header, 1);
21✔
122
         m_header.os = os_code;
21✔
123
         m_header.time = static_cast<uLong>(hdr_time);
21✔
124

125
         int rc = deflateSetHeader(streamp(), &m_header);
21✔
126
         if(rc != Z_OK) {
21✔
127
            throw Compression_Error("deflateSetHeader", ErrorType::ZlibError, rc);
×
128
         }
129
      }
21✔
130

131
   private:
132
      ::gz_header m_header;
133
};
134

135
class Gzip_Decompression_Stream final : public Zlib_Decompression_Stream {
136
   public:
137
      explicit Gzip_Decompression_Stream(int wbits) : Zlib_Decompression_Stream(wbits, 16) {}
21✔
138
};
139

140
}  // namespace
141

142
std::unique_ptr<Compression_Stream> Zlib_Compression::make_stream(size_t level) const {
36✔
143
   return std::make_unique<Zlib_Compression_Stream>(level, 15);
36✔
144
}
145

146
std::unique_ptr<Compression_Stream> Zlib_Decompression::make_stream() const {
21✔
147
   return std::make_unique<Zlib_Decompression_Stream>(15);
21✔
148
}
149

150
std::unique_ptr<Compression_Stream> Deflate_Compression::make_stream(size_t level) const {
20✔
151
   return std::make_unique<Deflate_Compression_Stream>(level, 15);
20✔
152
}
153

154
std::unique_ptr<Compression_Stream> Deflate_Decompression::make_stream() const {
20✔
155
   return std::make_unique<Deflate_Decompression_Stream>(15);
20✔
156
}
157

158
std::unique_ptr<Compression_Stream> Gzip_Compression::make_stream(size_t level) const {
21✔
159
   return std::make_unique<Gzip_Compression_Stream>(level, 15, m_os_code, m_hdr_time);
21✔
160
}
161

162
std::unique_ptr<Compression_Stream> Gzip_Decompression::make_stream() const {
21✔
163
   return std::make_unique<Gzip_Decompression_Stream>(15);
21✔
164
}
165

166
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc