Skip to content

Commit d35a813

Browse files
committed
Add neopg compress.
1 parent 3aac43f commit d35a813

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Command line parsing
2+
Copyright 2017 The NeoPG developers
3+
4+
NeoPG is released under the Simplified BSD License (see license.txt)
5+
*/
6+
7+
#pragma once
8+
9+
#include <neopg/cli/command.h>
10+
11+
namespace NeoPG {
12+
namespace CLI {
13+
14+
class ListCompressCommand : public Command {
15+
public:
16+
ListCompressCommand(CLI::App& app, const std::string& flag,
17+
const std::string& description,
18+
const std::string& group_name = "")
19+
: Command(app, flag, description, group_name) {}
20+
void run();
21+
};
22+
23+
class CompressCommand : public Command {
24+
public:
25+
std::vector<std::string> m_files;
26+
std::string m_algo{"gz"};
27+
int m_level = 0;
28+
bool m_decode = false;
29+
const std::string group = "Commands";
30+
ListCompressCommand cmd_list;
31+
32+
void run() override;
33+
CompressCommand(CLI::App& app, const std::string& flag,
34+
const std::string& description,
35+
const std::string& group_name = "")
36+
: Command(app, flag, description, group_name),
37+
cmd_list(m_cmd, "list", "list supported compression functions", group) {
38+
m_cmd.add_flag("-d,--decompress", m_decode,
39+
"decompress already compressed data");
40+
m_cmd.add_option("file", m_files, "file to hash");
41+
m_cmd.add_option("--algo", m_algo, "compression function", true);
42+
m_cmd.add_option("--level", m_level, "compression level (0 default, 1-9)");
43+
}
44+
};
45+
46+
} // Namespace CLI
47+
} // Namespace NeoPG

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,15 @@ add_executable(neopg
205205
../include/neopg/cli/hash_command.h
206206
../include/neopg/cli/armor_command.h
207207
../include/neopg/cli/cat_command.h
208+
../include/neopg/cli/compress_command.h
208209
cli/command.cpp
209210
cli/version_command.cpp
210211
cli/packet_command.cpp
211212
cli/random_command.cpp
212213
cli/hash_command.cpp
213214
cli/armor_command.cpp
214215
cli/cat_command.cpp
216+
cli/compress_command.cpp
215217
neopg.cpp
216218
)
217219
target_include_directories(neopg PRIVATE

src/cli/compress_command.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* NeoPG
2+
Copyright 2017 The NeoPG developers
3+
4+
NeoPG is released under the Simplified BSD License (see license.txt)
5+
*/
6+
7+
#include <iostream>
8+
#include <map>
9+
10+
#include <botan/comp_filter.h>
11+
#include <botan/compression.h>
12+
#include <botan/filters.h>
13+
14+
#include <neopg/cli/compress_command.h>
15+
16+
namespace NeoPG {
17+
namespace CLI {
18+
19+
void ListCompressCommand::run() {
20+
std::cout << "Any Botan-compatible algorithm specifier can be used:\n\n";
21+
#if defined(BOTAN_HAS_ZLIB)
22+
std::cout << "Zlib, zlib\n";
23+
std::cout << "Gzip, gzip, gz\n";
24+
std::cout << "Deflate, deflate\n";
25+
#endif
26+
27+
#if defined(BOTAN_HAS_BZIP2)
28+
std::cout << "bzip2, bz2, Bzip2\n";
29+
#endif
30+
31+
#if defined(BOTAN_HAS_LZMA)
32+
std::cout << "lzma, xz, LZMA";
33+
#endif
34+
}
35+
36+
const std::map<std::string, std::string> algo_to_suffix = {
37+
{"Deflate_Compression", ".zip"},
38+
{"Zlib_Compression", ".zlib"},
39+
{"Gzip_Compression", ".gz"},
40+
{"Bzip2_Compression", ".bz2"},
41+
{"Lzma_Compression", ".xz"}};
42+
43+
void CompressCommand::run() {
44+
bool multi_files = false;
45+
46+
if (!m_cmd.get_subcommands().empty()) return;
47+
48+
if (m_files.empty()) m_files.emplace_back("-");
49+
50+
std::unique_ptr<Botan::Compression_Algorithm> compressor{
51+
Botan::make_compressor(m_algo)};
52+
if (!compressor) throw Botan::Lookup_Error("Compression", m_algo, "");
53+
const std::string suffix(algo_to_suffix.at(compressor->name()));
54+
55+
for (auto& file : m_files) {
56+
std::unique_ptr<Botan::DataSource_Stream> source{
57+
(file == "-") ? new Botan::DataSource_Stream{std::cin}
58+
: new Botan::DataSource_Stream{file}};
59+
Botan::Filter* compress =
60+
m_decode
61+
? (Botan::Filter*)new Botan::Decompression_Filter(m_algo)
62+
: (Botan::Filter*)new Botan::Compression_Filter(m_algo, m_level);
63+
Botan::Filter* sink = (file == "-")
64+
? new Botan::DataSink_Stream(std::cout)
65+
: new Botan::DataSink_Stream(file + suffix, true);
66+
Botan::Pipe pipe{compress, sink};
67+
pipe.process_msg(*source);
68+
}
69+
}
70+
71+
} // Namespace CLI
72+
} // Namespace NeoPG

src/neopg.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <neopg/cli/armor_command.h>
1212
#include <neopg/cli/cat_command.h>
1313
#include <neopg/cli/command.h>
14+
#include <neopg/cli/compress_command.h>
1415
#include <neopg/cli/hash_command.h>
1516
#include <neopg/cli/packet_command.h>
1617
#include <neopg/cli/random_command.h>
@@ -115,6 +116,8 @@ int main(int argc, char* argv[]) {
115116
tools_group);
116117
RandomCommand cmd_random(app, "random", "output random bytes", tools_group);
117118
HashCommand cmd_hash(app, "hash", "calculate hash function", tools_group);
119+
CompressCommand cmd_compress(app, "compress", "compress and decompress data",
120+
tools_group);
118121
ArmorCommand cmd_armor(app, "armor", "ASCII-encode and decode binary data",
119122
tools_group);
120123
CatCommand cmd_cat(app, "cat", "the beginning of a new Unix system",

0 commit comments

Comments
 (0)