diff options
author | lloyd <[email protected]> | 2014-11-19 02:52:04 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-11-19 02:52:04 +0000 |
commit | dfe33209a78f28e1b5b5b9a43d99d360fd029889 (patch) | |
tree | c19115343f564c00eaabe2dfb45822ba089b0742 /src/cmd | |
parent | 173ccf53649b4635df5fd51974c44dd59eaf9e95 (diff) |
Add gzip compression transform and compress command line prog.
Diffstat (limited to 'src/cmd')
-rw-r--r-- | src/cmd/compress.cpp | 79 | ||||
-rw-r--r-- | src/cmd/main.cpp | 6 |
2 files changed, 82 insertions, 3 deletions
diff --git a/src/cmd/compress.cpp b/src/cmd/compress.cpp new file mode 100644 index 000000000..fc1ece7bd --- /dev/null +++ b/src/cmd/compress.cpp @@ -0,0 +1,79 @@ + +#include "apps.h" + +#include <botan/compression.h> +#include <fstream> + +namespace { + +void do_compress(Transformation& comp, std::ifstream& in, std::ostream& out) + { + secure_vector<byte> buf; + + comp.start(); + + while(in.good()) + { + buf.resize(16*1024); + in.read(reinterpret_cast<char*>(&buf[0]), buf.size()); + buf.resize(in.gcount()); + + comp.update(buf); + + out.write(reinterpret_cast<const char*>(&buf[0]), buf.size()); + } + + buf.clear(); + comp.finish(buf); + out.write(reinterpret_cast<const char*>(&buf[0]), buf.size()); + } + +int compress(int argc, char* argv[]) + { + const std::string in_file = argv[1]; + std::ifstream in(in_file.c_str()); + + if(!in.good()) + { + std::cout << "Couldn't read " << in_file << "\n"; + return 1; + } + + const size_t level = 6; + const std::string suffix = "gz"; + + std::unique_ptr<Transformation> compress(make_compressor(suffix, level)); + + const std::string out_file = in_file + "." + suffix; + std::ofstream out(out_file.c_str()); + + do_compress(*compress, in, out); + + return 0; + } + +int uncompress(int argc, char* argv[]) + { + const std::string in_file = argv[1]; + std::ifstream in(in_file.c_str()); + + if(!in.good()) + { + std::cout << "Couldn't read " << argv[1] << "\n"; + return 1; + } + + std::ofstream out("out"); + const std::string suffix = "gz"; + + std::unique_ptr<Transformation> decompress(make_decompressor(suffix)); + + do_compress(*decompress, in, out); + + return 0; + } + +REGISTER_APP(compress); +REGISTER_APP(uncompress); + +} diff --git a/src/cmd/main.cpp b/src/cmd/main.cpp index 65530b23a..42acf99e7 100644 --- a/src/cmd/main.cpp +++ b/src/cmd/main.cpp @@ -170,17 +170,17 @@ int main(int argc, char* argv[]) if(apps.has(cmd)) return apps.run(cmd, argc - 1, argv + 1); - std::cout << "Unknown command " << cmd << "\n"; + std::cerr << "Unknown command " << cmd << std::endl; return help(argc, argv); } catch(std::exception& e) { - std::cerr << "Exception: " << e.what() << std::endl; + std::cerr << e.what() << std::endl; return 1; } catch(...) { - std::cerr << "Unknown (...) exception caught" << std::endl; + std::cerr << "Unknown exception caught" << std::endl; return 1; } |