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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 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
         int rc = BZ2_bzCompressInit(streamp(), static_cast<unsigned int>(block_size), 0, 0);
21✔
49

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

54
      ~Bzip2_Compression_Stream() { BZ2_bzCompressEnd(streamp()); }
21✔
55

56
      bool run(uint32_t flags) override {
62✔
57
         int rc = BZ2_bzCompress(streamp(), flags);
62✔
58

59
         if(rc < 0)
62✔
60
            throw Compression_Error("BZ2_bzCompress", ErrorType::Bzip2Error, rc);
×
61

62
         return (rc == BZ_STREAM_END);
62✔
63
      }
64
};
65

66
class Bzip2_Decompression_Stream final : public Bzip2_Stream {
67
   public:
68
      Bzip2_Decompression_Stream() {
21✔
69
         int rc = BZ2_bzDecompressInit(streamp(), 0, 0);
21✔
70

71
         if(rc != BZ_OK)
21✔
72
            throw Compression_Error("BZ2_bzDecompressInit", ErrorType::Bzip2Error, rc);
×
73
      }
21✔
74

75
      ~Bzip2_Decompression_Stream() { BZ2_bzDecompressEnd(streamp()); }
21✔
76

77
      bool run(uint32_t) override {
44✔
78
         int rc = BZ2_bzDecompress(streamp());
44✔
79

80
         if(rc != BZ_OK && rc != BZ_STREAM_END)
44✔
81
            throw Compression_Error("BZ2_bzDecompress", ErrorType::Bzip2Error, rc);
×
82

83
         return (rc == BZ_STREAM_END);
44✔
84
      }
85
};
86

87
}  // namespace
88

89
std::unique_ptr<Compression_Stream> Bzip2_Compression::make_stream(size_t comp_level) const {
21✔
90
   return std::make_unique<Bzip2_Compression_Stream>(comp_level);
21✔
91
}
92

93
std::unique_ptr<Compression_Stream> Bzip2_Decompression::make_stream() const {
21✔
94
   return std::make_unique<Bzip2_Decompression_Stream>();
21✔
95
}
96

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