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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.74
/src/cli/compress.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "cli.h"
8

9
#if defined(BOTAN_HAS_COMPRESSION)
10
   #include <botan/compression.h>
11
   #include <fstream>
12
#endif
13

14
namespace Botan_CLI {
15

16
#if defined(BOTAN_HAS_COMPRESSION)
17

18
class Compress final : public Command {
19
   public:
20
      Compress() : Command("compress --type=gzip --level=6 --buf-size=8192 file") {}
4✔
21

22
      std::string output_filename(const std::string& input_fsname, const std::string& comp_type) {
1✔
23
         const std::map<std::string, std::string> suffixes = {
1✔
24
            {"zlib", "zlib"},
25
            {"gzip", "gz"},
26
            {"bzip2", "bz2"},
27
            {"lzma", "xz"},
28
         };
5✔
29

30
         auto suffix_info = suffixes.find(comp_type);
1✔
31
         if(suffixes.count(comp_type) == 0) {
1✔
32
            throw CLI_Error_Unsupported("Compressing", comp_type);
×
33
         }
34

35
         return input_fsname + "." + suffix_info->second;
2✔
36
      }
1✔
37

38
      std::string group() const override { return "compression"; }
1✔
39

40
      std::string description() const override { return "Compress a given file"; }
1✔
41

42
      void go() override {
1✔
43
         const std::string comp_type = get_arg("type");
1✔
44
         const size_t buf_size = get_arg_sz("buf-size");
1✔
45
         const size_t comp_level = get_arg_sz("level");
1✔
46

47
         auto compress = Botan::Compression_Algorithm::create(comp_type);
1✔
48
         if(!compress) {
1✔
49
            throw CLI_Error_Unsupported("Compression", comp_type);
×
50
         }
51

52
         const std::string in_file = get_arg("file");
1✔
53
         std::ifstream in(in_file, std::ios::binary);
1✔
54

55
         if(!in.good()) {
1✔
56
            throw CLI_IO_Error("reading", in_file);
×
57
         }
58

59
         const std::string out_file = output_filename(in_file, comp_type);
1✔
60
         std::ofstream out(out_file, std::ios::binary);
1✔
61
         if(!out.good()) {
1✔
62
            throw CLI_IO_Error("writing", out_file);
×
63
         }
64

65
         Botan::secure_vector<uint8_t> buf;
1✔
66

67
         compress->start(comp_level);
1✔
68

69
         while(in.good()) {
2✔
70
            buf.resize(buf_size);
1✔
71
            in.read(reinterpret_cast<char*>(buf.data()), buf.size());
1✔
72
            buf.resize(in.gcount());
1✔
73

74
            compress->update(buf);
1✔
75

76
            out.write(reinterpret_cast<const char*>(buf.data()), buf.size());
1✔
77
         }
78

79
         buf.clear();
1✔
80
         compress->finish(buf);
1✔
81
         out.write(reinterpret_cast<const char*>(buf.data()), buf.size());
1✔
82
         out.close();
1✔
83
      }
4✔
84
};
85

86
BOTAN_REGISTER_COMMAND("compress", Compress);
2✔
87

88
class Decompress final : public Command {
89
   public:
90
      Decompress() : Command("decompress --buf-size=8192 file") {}
4✔
91

92
      void parse_extension(const std::string& in_file, std::string& out_file, std::string& suffix) {
1✔
93
         auto last_dot = in_file.find_last_of('.');
1✔
94
         if(last_dot == std::string::npos || last_dot == 0) {
1✔
95
            throw CLI_Error("No extension detected in filename '" + in_file + "'");
×
96
         }
97

98
         out_file = in_file.substr(0, last_dot);
1✔
99
         suffix = in_file.substr(last_dot + 1, std::string::npos);
1✔
100
      }
1✔
101

102
      std::string group() const override { return "compression"; }
1✔
103

104
      std::string description() const override { return "Decompress a given compressed archive"; }
1✔
105

106
      void go() override {
1✔
107
         const size_t buf_size = get_arg_sz("buf-size");
1✔
108
         const std::string in_file = get_arg("file");
1✔
109
         std::string out_file, suffix;
1✔
110
         parse_extension(in_file, out_file, suffix);
1✔
111

112
         std::ifstream in(in_file, std::ios::binary);
1✔
113

114
         if(!in.good()) {
1✔
115
            throw CLI_IO_Error("reading", in_file);
×
116
         }
117

118
         auto decompress = Botan::Decompression_Algorithm::create(suffix);
1✔
119

120
         if(!decompress) {
1✔
121
            throw CLI_Error_Unsupported("Decompression", suffix);
×
122
         }
123

124
         std::ofstream out(out_file, std::ios::binary);
1✔
125
         if(!out.good()) {
1✔
126
            throw CLI_IO_Error("writing", out_file);
×
127
         }
128

129
         Botan::secure_vector<uint8_t> buf;
1✔
130

131
         decompress->start();
1✔
132

133
         while(in.good()) {
2✔
134
            buf.resize(buf_size);
1✔
135
            in.read(reinterpret_cast<char*>(buf.data()), buf.size());
1✔
136
            buf.resize(in.gcount());
1✔
137

138
            decompress->update(buf);
1✔
139

140
            out.write(reinterpret_cast<const char*>(buf.data()), buf.size());
1✔
141
         }
142

143
         buf.clear();
1✔
144
         decompress->finish(buf);
1✔
145
         out.write(reinterpret_cast<const char*>(buf.data()), buf.size());
1✔
146
         out.close();
1✔
147
      }
4✔
148
};
149

150
BOTAN_REGISTER_COMMAND("decompress", Decompress);
2✔
151

152
#endif
153

154
}
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