• 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

88.89
/src/lib/compression/bzip2/bzip2.cpp
1
/*
2
* Bzip2 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/bzip2.h>
11

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

15
#define BZ_NO_STDIO
16
#include <bzlib.h>
17

18
namespace Botan {
19

20
namespace {
21

22
class Bzip2_Stream : public Zlib_Style_Stream<bz_stream, char, unsigned int> {
42✔
23
   public:
24
      Bzip2_Stream() {
42✔
25
         streamp()->opaque = alloc();
42✔
26
         streamp()->bzalloc = Compression_Alloc_Info::malloc<int>;
42✔
27
         streamp()->bzfree = Compression_Alloc_Info::free;
42✔
28
      }
29

30
      uint32_t run_flag() const override { return BZ_RUN; }
77✔
31

32
      uint32_t flush_flag() const override { return BZ_FLUSH; }
10✔
33

34
      uint32_t finish_flag() const override { return BZ_FINISH; }
21✔
35
};
36

37
class Bzip2_Compression_Stream final : public Bzip2_Stream {
38
   public:
39
      explicit Bzip2_Compression_Stream(size_t block_size) {
21✔
40
         /*
41
         * Defaults to 900k blocks as the computation cost of
42
         * compression is not overly affected by the size, though
43
         * more memory is required.
44
         */
45
         if(block_size == 0 || block_size >= 9) {
21✔
46
            block_size = 9;
11✔
47
         }
48

49
         int rc = BZ2_bzCompressInit(streamp(), static_cast<unsigned int>(block_size), 0, 0);
21✔
50

51
         if(rc != BZ_OK) {
21✔
52
            throw Compression_Error("BZ2_bzCompressInit", ErrorType::Bzip2Error, rc);
×
53
         }
54
      }
21✔
55

56
      Bzip2_Compression_Stream(const Bzip2_Compression_Stream& other) = delete;
57
      Bzip2_Compression_Stream(Bzip2_Compression_Stream&& other) = delete;
58
      Bzip2_Compression_Stream& operator=(const Bzip2_Compression_Stream& other) = delete;
59
      Bzip2_Compression_Stream& operator=(Bzip2_Compression_Stream&& other) = delete;
60

61
      ~Bzip2_Compression_Stream() override { BZ2_bzCompressEnd(streamp()); }
21✔
62

63
      bool run(uint32_t flags) override {
62✔
64
         int rc = BZ2_bzCompress(streamp(), flags);
62✔
65

66
         if(rc < 0) {
62✔
67
            throw Compression_Error("BZ2_bzCompress", ErrorType::Bzip2Error, rc);
×
68
         }
69

70
         return (rc == BZ_STREAM_END);
62✔
71
      }
72
};
73

74
class Bzip2_Decompression_Stream final : public Bzip2_Stream {
75
   public:
76
      Bzip2_Decompression_Stream() {
21✔
77
         int rc = BZ2_bzDecompressInit(streamp(), 0, 0);
21✔
78

79
         if(rc != BZ_OK) {
21✔
80
            throw Compression_Error("BZ2_bzDecompressInit", ErrorType::Bzip2Error, rc);
×
81
         }
82
      }
21✔
83

84
      Bzip2_Decompression_Stream(const Bzip2_Decompression_Stream& other) = delete;
85
      Bzip2_Decompression_Stream(Bzip2_Decompression_Stream&& other) = delete;
86
      Bzip2_Decompression_Stream& operator=(const Bzip2_Decompression_Stream& other) = delete;
87
      Bzip2_Decompression_Stream& operator=(Bzip2_Decompression_Stream&& other) = delete;
88

89
      ~Bzip2_Decompression_Stream() override { BZ2_bzDecompressEnd(streamp()); }
21✔
90

91
      bool run(uint32_t) override {
44✔
92
         int rc = BZ2_bzDecompress(streamp());
44✔
93

94
         if(rc != BZ_OK && rc != BZ_STREAM_END) {
44✔
95
            throw Compression_Error("BZ2_bzDecompress", ErrorType::Bzip2Error, rc);
×
96
         }
97

98
         return (rc == BZ_STREAM_END);
44✔
99
      }
100
};
101

102
}  // namespace
103

104
std::unique_ptr<Compression_Stream> Bzip2_Compression::make_stream(size_t comp_level) const {
21✔
105
   return std::make_unique<Bzip2_Compression_Stream>(comp_level);
21✔
106
}
107

108
std::unique_ptr<Compression_Stream> Bzip2_Decompression::make_stream() const {
21✔
109
   return std::make_unique<Bzip2_Decompression_Stream>();
21✔
110
}
111

112
}  // 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