Botan  2.4.0
Crypto and TLS for C++11
bzip2.cpp
Go to the documentation of this file.
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 #include <botan/exceptn.h>
12 #include <botan/internal/compress_utils.h>
13 
14 #define BZ_NO_STDIO
15 #include <bzlib.h>
16 
17 namespace Botan {
18 
19 namespace {
20 
21 class Bzip2_Stream : public Zlib_Style_Stream<bz_stream, char>
22  {
23  public:
24  Bzip2_Stream()
25  {
26  streamp()->opaque = alloc();
27  streamp()->bzalloc = Compression_Alloc_Info::malloc<int>;
28  streamp()->bzfree = Compression_Alloc_Info::free;
29  }
30 
31  uint32_t run_flag() const override { return BZ_RUN; }
32  uint32_t flush_flag() const override { return BZ_FLUSH; }
33  uint32_t finish_flag() const override { return BZ_FINISH; }
34  };
35 
36 class Bzip2_Compression_Stream final : public Bzip2_Stream
37  {
38  public:
39  explicit Bzip2_Compression_Stream(size_t block_size)
40  {
41  /*
42  * Defaults to 900k blocks as the computation cost of
43  * compression is not overly affected by the size, though
44  * more memory is required.
45  */
46  if(block_size == 0 || block_size >= 9)
47  block_size = 9;
48 
49  int rc = BZ2_bzCompressInit(streamp(), block_size, 0, 0);
50 
51  if(rc == BZ_MEM_ERROR)
52  throw Exception("bzip memory allocation failure");
53  else if(rc != BZ_OK)
54  throw Exception("bzip compress initialization failed");
55  }
56 
57  ~Bzip2_Compression_Stream()
58  {
59  BZ2_bzCompressEnd(streamp());
60  }
61 
62  bool run(uint32_t flags) override
63  {
64  int rc = BZ2_bzCompress(streamp(), flags);
65 
66  if(rc == BZ_MEM_ERROR)
67  throw Exception("bzip memory allocation failure");
68  else if(rc < 0)
69  throw Exception("bzip compress error " + std::to_string(-rc));
70 
71  return (rc == BZ_STREAM_END);
72  }
73  };
74 
75 class Bzip2_Decompression_Stream final : public Bzip2_Stream
76  {
77  public:
78  Bzip2_Decompression_Stream()
79  {
80  int rc = BZ2_bzDecompressInit(streamp(), 0, 0);
81 
82  if(rc == BZ_MEM_ERROR)
83  throw Exception("bzip memory allocation failure");
84  else if(rc != BZ_OK)
85  throw Exception("bzip decompress initialization failed");
86  }
87 
88  ~Bzip2_Decompression_Stream()
89  {
90  BZ2_bzDecompressEnd(streamp());
91  }
92 
93  bool run(uint32_t) override
94  {
95  int rc = BZ2_bzDecompress(streamp());
96 
97  if(rc == BZ_MEM_ERROR)
98  throw Exception("bzip memory allocation failure");
99  else if(rc != BZ_OK && rc != BZ_STREAM_END)
100  throw Exception("bzip decompress error " + std::to_string(-rc));
101 
102  return (rc == BZ_STREAM_END);
103  }
104  };
105 
106 }
107 
108 Compression_Stream* Bzip2_Compression::make_stream(size_t comp_level) const
109  {
110  return new Bzip2_Compression_Stream(comp_level);
111  }
112 
113 Compression_Stream* Bzip2_Decompression::make_stream() const
114  {
115  return new Bzip2_Decompression_Stream;
116  }
117 
118 }
static void free(void *self, void *ptr)
Flags flags(Flag flags)
Definition: p11.h:858
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:108
Definition: alg_id.cpp:13