1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <unistd.h>
16
17 #include <fstream>
18 #include <iostream>
19 #include <string>
20
21 #include "absl/flags/flag.h"
22 #include "absl/flags/parse.h"
23 #include "absl/log/globals.h"
24 #include "absl/log/initialize.h"
25 #include "contrib/c-blosc/sandboxed.h"
26 #include "contrib/c-blosc/utils/utils_blosc.h"
27
28 ABSL_FLAG(bool, decompress, false, "decompress");
29 ABSL_FLAG(int, clevel, 5, "compression level");
30 ABSL_FLAG(uint32_t, nthreads, 5, "number of threads");
31 ABSL_FLAG(std::string, compressor, "blosclz",
32 "compressor engine. Available: blosclz, lz4, lz4hc, zlib, zstd");
33
Stream(CbloscApi & api,std::string & infile_s,std::string & outfile_s)34 absl::Status Stream(CbloscApi& api, std::string& infile_s,
35 std::string& outfile_s) {
36 std::ifstream infile(infile_s, std::ios::binary);
37 if (!infile.is_open()) {
38 return absl::UnavailableError(absl::StrCat("Unable to open ", infile_s));
39 }
40 std::ofstream outfile(outfile_s, std::ios::binary);
41 if (!outfile.is_open()) {
42 return absl::UnavailableError(absl::StrCat("Unable to open ", outfile_s));
43 }
44
45 std::string compressor(absl::GetFlag(FLAGS_compressor));
46
47 if (absl::GetFlag(FLAGS_decompress)) {
48 return Decompress(api, infile, outfile, 5);
49 }
50
51 return Compress(api, infile, outfile, absl::GetFlag(FLAGS_clevel), compressor,
52 absl::GetFlag(FLAGS_nthreads));
53 }
54
main(int argc,char * argv[])55 int main(int argc, char* argv[]) {
56 std::string prog_name(argv[0]);
57 absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
58 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
59 absl::InitializeLog();
60
61 if (args.size() != 3) {
62 std::cerr << "Usage:\n " << prog_name << " INPUT OUTPUT\n";
63 return EXIT_FAILURE;
64 }
65
66 CbloscSapiSandbox sandbox;
67 if (!sandbox.Init().ok()) {
68 std::cerr << "Unable to start sandbox\n";
69 return EXIT_FAILURE;
70 }
71 CbloscApi api(&sandbox);
72
73 if (absl::Status status = api.blosc_init(); !status.ok()) {
74 std::cerr << "Unable to init library\n";
75 std::cerr << status << std::endl;
76 return EXIT_FAILURE;
77 }
78
79 std::string infile_s(args[1]);
80 std::string outfile_s(args[2]);
81
82 if (absl::Status status = Stream(api, infile_s, outfile_s); !status.ok()) {
83 std::cerr << "Unable to ";
84 std::cerr << (absl::GetFlag(FLAGS_decompress) ? "de" : "");
85 std::cerr << "compress file\n";
86 std::cerr << status << std::endl;
87 return EXIT_FAILURE;
88 }
89
90 if (absl::Status status = api.blosc_destroy(); !status.ok()) {
91 std::cerr << "Unable to uninitialize library\n";
92 std::cerr << status << std::endl;
93 return EXIT_FAILURE;
94 }
95
96 return EXIT_SUCCESS;
97 }
98