aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-12-09 01:53:06 +0000
committerlloyd <[email protected]>2014-12-09 01:53:06 +0000
commit0bc9c6b170bd2c52a2fccfda12f767700bb40968 (patch)
tree5c1328b44da71c512332442e3d36989df70ef7c7 /src
parente14d04baf0261d5250285fcb6486950078b2c6e7 (diff)
Figure out which decompressor to use based on the input file extension.
Rename Bzip to Bzip2, and split Zlib and Deflate compressors into two completely distinct types rather than using a bool flag to the Zlib constructor. Ignore null pointers to our free implementation (LZMA does this).
Diffstat (limited to 'src')
-rw-r--r--src/cmd/compress.cpp30
-rw-r--r--src/lib/compression/bzip2/bzip2.cpp31
-rw-r--r--src/lib/compression/bzip2/bzip2.h28
-rw-r--r--src/lib/compression/comp_util.cpp16
-rw-r--r--src/lib/compression/compression.cpp20
-rw-r--r--src/lib/compression/compression.h1
-rw-r--r--src/lib/compression/zlib/zlib.cpp25
-rw-r--r--src/lib/compression/zlib/zlib.h44
8 files changed, 123 insertions, 72 deletions
diff --git a/src/cmd/compress.cpp b/src/cmd/compress.cpp
index fc1ece7bd..c7ee5841c 100644
--- a/src/cmd/compress.cpp
+++ b/src/cmd/compress.cpp
@@ -14,7 +14,7 @@ void do_compress(Transformation& comp, std::ifstream& in, std::ostream& out)
while(in.good())
{
- buf.resize(16*1024);
+ buf.resize(64*1024);
in.read(reinterpret_cast<char*>(&buf[0]), buf.size());
buf.resize(in.gcount());
@@ -30,6 +30,12 @@ void do_compress(Transformation& comp, std::ifstream& in, std::ostream& out)
int compress(int argc, char* argv[])
{
+ if(argc != 2 && argc != 3)
+ {
+ std::cout << "Usage: " << argv[0] << " input [type]\n";
+ return 1;
+ }
+
const std::string in_file = argv[1];
std::ifstream in(in_file.c_str());
@@ -39,8 +45,8 @@ int compress(int argc, char* argv[])
return 1;
}
- const size_t level = 6;
- const std::string suffix = "gz";
+ const size_t level = 9;
+ const std::string suffix = argc == 3 ? argv[2] : "gz";
std::unique_ptr<Transformation> compress(make_compressor(suffix, level));
@@ -52,6 +58,18 @@ int compress(int argc, char* argv[])
return 0;
}
+void parse_extension(const std::string& in_file,
+ std::string& out_file,
+ std::string& suffix)
+ {
+ auto last_dot = in_file.find_last_of('.');
+ if(last_dot == std::string::npos || last_dot == 0)
+ throw std::runtime_error("No extension detected in filename '" + in_file + "'");
+
+ out_file = in_file.substr(0, last_dot);
+ suffix = in_file.substr(last_dot+1, std::string::npos);
+ }
+
int uncompress(int argc, char* argv[])
{
const std::string in_file = argv[1];
@@ -63,8 +81,10 @@ int uncompress(int argc, char* argv[])
return 1;
}
- std::ofstream out("out");
- const std::string suffix = "gz";
+ std::string out_file, suffix;
+ parse_extension(in_file, out_file, suffix);
+
+ std::ofstream out(out_file.c_str());
std::unique_ptr<Transformation> decompress(make_decompressor(suffix));
diff --git a/src/lib/compression/bzip2/bzip2.cpp b/src/lib/compression/bzip2/bzip2.cpp
index 313b1ed46..ed464d332 100644
--- a/src/lib/compression/bzip2/bzip2.cpp
+++ b/src/lib/compression/bzip2/bzip2.cpp
@@ -1,5 +1,5 @@
/*
-* Bzip Compressor
+* Bzip2 Compressor
* (C) 2001 Peter J Jones
* 2001-2007,2014 Jack Lloyd
* 2006 Matt Johnston
@@ -17,10 +17,10 @@ namespace Botan {
namespace {
-class Bzip_Stream : public Zlib_Style_Stream<bz_stream, char>
+class Bzip2_Stream : public Zlib_Style_Stream<bz_stream, char>
{
public:
- Bzip_Stream()
+ Bzip2_Stream()
{
streamp()->opaque = alloc();
streamp()->bzalloc = Compression_Alloc_Info::malloc<int>;
@@ -32,12 +32,12 @@ class Bzip_Stream : public Zlib_Style_Stream<bz_stream, char>
u32bit finish_flag() const override { return BZ_FINISH; }
};
-class Bzip_Compression_Stream : public Bzip_Stream
+class Bzip2_Compression_Stream : public Bzip2_Stream
{
public:
- Bzip_Compression_Stream(size_t level)
+ Bzip2_Compression_Stream(size_t block_size)
{
- int rc = BZ2_bzCompressInit(streamp(), level, 0, 0);
+ int rc = BZ2_bzCompressInit(streamp(), block_size, 0, 0);
if(rc == BZ_MEM_ERROR)
throw std::bad_alloc();
@@ -45,7 +45,7 @@ class Bzip_Compression_Stream : public Bzip_Stream
throw std::runtime_error("bzip compress initialization failed");
}
- ~Bzip_Compression_Stream()
+ ~Bzip2_Compression_Stream()
{
BZ2_bzCompressEnd(streamp());
}
@@ -61,15 +61,12 @@ class Bzip_Compression_Stream : public Bzip_Stream
return (rc == BZ_STREAM_END);
}
-
- private:
- size_t m_level;
};
-class Bzip_Decompression_Stream : public Bzip_Stream
+class Bzip2_Decompression_Stream : public Bzip2_Stream
{
public:
- Bzip_Decompression_Stream()
+ Bzip2_Decompression_Stream()
{
int rc = BZ2_bzDecompressInit(streamp(), 0, 0);
@@ -79,7 +76,7 @@ class Bzip_Decompression_Stream : public Bzip_Stream
throw std::runtime_error("bzip decompress initialization failed");
}
- ~Bzip_Decompression_Stream()
+ ~Bzip2_Decompression_Stream()
{
BZ2_bzDecompressEnd(streamp());
}
@@ -99,14 +96,14 @@ class Bzip_Decompression_Stream : public Bzip_Stream
}
-Compression_Stream* Bzip_Compression::make_stream() const
+Compression_Stream* Bzip2_Compression::make_stream() const
{
- return new Bzip_Compression_Stream(m_level);
+ return new Bzip2_Compression_Stream(m_block_size);
}
-Compression_Stream* Bzip_Decompression::make_stream() const
+Compression_Stream* Bzip2_Decompression::make_stream() const
{
- return new Bzip_Decompression_Stream;
+ return new Bzip2_Decompression_Stream;
}
}
diff --git a/src/lib/compression/bzip2/bzip2.h b/src/lib/compression/bzip2/bzip2.h
index 945f6a051..6d033e430 100644
--- a/src/lib/compression/bzip2/bzip2.h
+++ b/src/lib/compression/bzip2/bzip2.h
@@ -1,7 +1,7 @@
/*
-* Bzip Compressor
+* Bzip2 Compressor
* (C) 2001 Peter J Jones
-* 2001-2007 Jack Lloyd
+* 2001-2007,2014 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -14,33 +14,35 @@
namespace Botan {
/**
-* Bzip Compression
+* Bzip2 Compression
*/
-class BOTAN_DLL Bzip_Compression : public Stream_Compression
+class BOTAN_DLL Bzip2_Compression : public Stream_Compression
{
public:
/**
- * @param level how much effort to use on compressing (0 to 9);
- * higher levels are slower but tend to give better
- * compression
+ * @param block_size in 1024 KiB increments, in range from 1 to 9.
+ *
+ * Lowering this does not noticably modify the compression or
+ * decompression speed, though less memory is required for both
+ * compression and decompression.
*/
- Bzip_Compression(size_t level = 6) : m_level(level) {}
+ Bzip2_Compression(size_t block_size = 9) : m_block_size(block_size) {}
- std::string name() const override { return "Bzip_Compression"; }
+ std::string name() const override { return "Bzip2_Compression"; }
private:
Compression_Stream* make_stream() const;
- const size_t m_level;
+ const size_t m_block_size;
};
/**
-* Bzip Deccompression
+* Bzip2 Deccompression
*/
-class BOTAN_DLL Bzip_Decompression : public Stream_Decompression
+class BOTAN_DLL Bzip2_Decompression : public Stream_Decompression
{
public:
- std::string name() const override { return "Bzip_Decompression"; }
+ std::string name() const override { return "Bzip2_Decompression"; }
private:
Compression_Stream* make_stream() const;
};
diff --git a/src/lib/compression/comp_util.cpp b/src/lib/compression/comp_util.cpp
index 7fca1852d..5f2e37d49 100644
--- a/src/lib/compression/comp_util.cpp
+++ b/src/lib/compression/comp_util.cpp
@@ -22,13 +22,17 @@ void* Compression_Alloc_Info::do_malloc(size_t n, size_t size)
void Compression_Alloc_Info::do_free(void* ptr)
{
- auto i = m_current_allocs.find(ptr);
- if(i == m_current_allocs.end())
- throw std::runtime_error("Compression_Alloc_Info::free got pointer not allocated by us");
+ if(ptr)
+ {
+ auto i = m_current_allocs.find(ptr);
- std::memset(ptr, 0, i->second);
- std::free(ptr);
- m_current_allocs.erase(i);
+ if(i == m_current_allocs.end())
+ throw std::runtime_error("Compression_Alloc_Info::free got pointer not allocated by us");
+
+ std::memset(ptr, 0, i->second);
+ std::free(ptr);
+ m_current_allocs.erase(i);
+ }
}
}
diff --git a/src/lib/compression/compression.cpp b/src/lib/compression/compression.cpp
index 35b2a4bc0..191d894c4 100644
--- a/src/lib/compression/compression.cpp
+++ b/src/lib/compression/compression.cpp
@@ -11,8 +11,8 @@
#include <botan/zlib.h>
#endif
-#if defined(BOTAN_HAS_BZIP_TRANSFORM)
- #include <botan/bzip.h>
+#if defined(BOTAN_HAS_BZIP2_TRANSFORM)
+ #include <botan/bzip2.h>
#endif
#if defined(BOTAN_HAS_LZMA_TRANSFORM)
@@ -25,16 +25,16 @@ Compressor_Transformation* make_compressor(const std::string& type, size_t level
{
#if defined(BOTAN_HAS_ZLIB_TRANSFORM)
if(type == "zlib")
- return new Zlib_Compression(level, false);
+ return new Zlib_Compression(level);
if(type == "deflate")
- return new Zlib_Compression(level, true);
+ return new Deflate_Compression(level);
if(type == "gzip" || type == "gz")
return new Gzip_Compression(level);
#endif
-#if defined(BOTAN_HAS_BZIP_TRANSFORM)
+#if defined(BOTAN_HAS_BZIP2_TRANSFORM)
if(type == "bzip2" || type == "bz2")
- return new Bzip_Compression(level);
+ return new Bzip2_Compression(level);
#endif
#if defined(BOTAN_HAS_LZMA_TRANSFORM)
@@ -49,16 +49,16 @@ Compressor_Transformation* make_decompressor(const std::string& type)
{
#if defined(BOTAN_HAS_ZLIB_TRANSFORM)
if(type == "zlib")
- return new Zlib_Decompression(false);
+ return new Zlib_Decompression;
if(type == "deflate")
- return new Zlib_Decompression(true);
+ return new Deflate_Decompression;
if(type == "gzip" || type == "gz")
return new Gzip_Decompression;
#endif
-#if defined(BOTAN_HAS_BZIP_TRANSFORM)
+#if defined(BOTAN_HAS_BZIP2_TRANSFORM)
if(type == "bzip2" || type == "bz2")
- return new Bzip_Decompression;
+ return new Bzip2_Decompression;
#endif
#if defined(BOTAN_HAS_LZMA_TRANSFORM)
diff --git a/src/lib/compression/compression.h b/src/lib/compression/compression.h
index 75a100cc6..68b25886b 100644
--- a/src/lib/compression/compression.h
+++ b/src/lib/compression/compression.h
@@ -65,7 +65,6 @@ class BOTAN_DLL Stream_Compression : public Compressor_Transformation
void finish(secure_vector<byte>& buf, size_t offset = 0) override;
void clear() override;
-
private:
secure_vector<byte> start_raw(const byte[], size_t) override;
diff --git a/src/lib/compression/zlib/zlib.cpp b/src/lib/compression/zlib/zlib.cpp
index b352519fd..b31420254 100644
--- a/src/lib/compression/zlib/zlib.cpp
+++ b/src/lib/compression/zlib/zlib.cpp
@@ -44,8 +44,9 @@ class Zlib_Compression_Stream : public Zlib_Stream
public:
Zlib_Compression_Stream(size_t level, int wbits, int wbits_offset = 0)
{
- int rc = deflateInit2(streamp(), level, Z_DEFLATED,
- compute_window_bits(wbits, wbits_offset),
+ wbits = compute_window_bits(wbits, wbits_offset);
+
+ int rc = deflateInit2(streamp(), level, Z_DEFLATED, wbits,
8, Z_DEFAULT_STRATEGY);
if(rc != Z_OK)
throw std::runtime_error("zlib deflate initialization failed");
@@ -142,18 +143,22 @@ class Gzip_Decompression_Stream : public Zlib_Decompression_Stream
Compression_Stream* Zlib_Compression::make_stream() const
{
- if(m_raw_deflate)
- return new Deflate_Compression_Stream(m_level, 15);
- else
- return new Zlib_Compression_Stream(m_level, 15);
+ return new Zlib_Compression_Stream(m_level, 15);
}
Compression_Stream* Zlib_Decompression::make_stream() const
{
- if(m_raw_deflate)
- return new Deflate_Decompression_Stream(15);
- else
- return new Zlib_Decompression_Stream(15);
+ return new Zlib_Decompression_Stream(15);
+ }
+
+Compression_Stream* Deflate_Compression::make_stream() const
+ {
+ return new Deflate_Compression_Stream(m_level, 15);
+ }
+
+Compression_Stream* Deflate_Decompression::make_stream() const
+ {
+ return new Deflate_Decompression_Stream(15);
}
Compression_Stream* Gzip_Compression::make_stream() const
diff --git a/src/lib/compression/zlib/zlib.h b/src/lib/compression/zlib/zlib.h
index 5805efb57..963fbc269 100644
--- a/src/lib/compression/zlib/zlib.h
+++ b/src/lib/compression/zlib/zlib.h
@@ -1,7 +1,7 @@
/*
* Zlib Compressor
* (C) 2001 Peter J Jones
-* 2001-2007 Jack Lloyd
+* 2001-2007,2014 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -23,10 +23,9 @@ class BOTAN_DLL Zlib_Compression : public Stream_Compression
* @param level how much effort to use on compressing (0 to 9);
* higher levels are slower but tend to give better
* compression
- * @param raw_deflate if true no zlib header/trailer will be used
*/
- Zlib_Compression(size_t level = 6, bool raw_deflate = false) :
- m_level(level), m_raw_deflate(raw_deflate) {}
+
+ Zlib_Compression(size_t level = 6) : m_level(level) {}
std::string name() const override { return "Zlib_Compression"; }
@@ -34,26 +33,51 @@ class BOTAN_DLL Zlib_Compression : public Stream_Compression
Compression_Stream* make_stream() const;
const size_t m_level;
- const bool m_raw_deflate;
};
/**
-* Zlib Deccompression
+* Zlib Decompression
*/
class BOTAN_DLL Zlib_Decompression : public Stream_Decompression
{
public:
+ std::string name() const override { return "Zlib_Decompression"; }
+
+ private:
+ Compression_Stream* make_stream() const;
+ };
+
+/**
+* Deflate Compression
+*/
+class BOTAN_DLL Deflate_Compression : public Stream_Compression
+ {
+ public:
/**
- * @param raw_deflate if true no zlib header/trailer will be used
+ * @param level how much effort to use on compressing (0 to 9);
+ * higher levels are slower but tend to give better
+ * compression
*/
- Zlib_Decompression(bool raw_deflate = false) : m_raw_deflate(raw_deflate) {}
+ Deflate_Compression(size_t level = 6) : m_level(level) {}
- std::string name() const override { return "Zlib_Decompression"; }
+ std::string name() const override { return "Deflate_Compression"; }
private:
Compression_Stream* make_stream() const;
- const bool m_raw_deflate;
+ const size_t m_level;
+ };
+
+/**
+* Deflate Decompression
+*/
+class BOTAN_DLL Deflate_Decompression : public Stream_Decompression
+ {
+ public:
+ std::string name() const override { return "Deflate_Decompression"; }
+
+ private:
+ Compression_Stream* make_stream() const;
};
/**