xref: /aosp_15_r20/external/sandboxed-api/contrib/zstd/example/main.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
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 <fcntl.h>
16 #include <unistd.h>
17 
18 #include <cstdlib>
19 #include <fstream>
20 #include <iostream>
21 #include <string>
22 #include <vector>
23 
24 #include "absl/flags/flag.h"
25 #include "absl/flags/parse.h"
26 #include "absl/log/globals.h"
27 #include "absl/log/initialize.h"
28 #include "contrib/zstd/sandboxed.h"
29 #include "contrib/zstd/utils/utils_zstd.h"
30 
31 ABSL_FLAG(bool, stream, false, "stream data to sandbox");
32 ABSL_FLAG(bool, decompress, false, "decompress");
33 ABSL_FLAG(bool, memory_mode, false, "in memory operations");
34 ABSL_FLAG(uint32_t, level, 0, "compression level");
35 
Stream(ZstdApi & api,std::string infile_s,std::string outfile_s)36 absl::Status Stream(ZstdApi& api, std::string infile_s, std::string outfile_s) {
37   std::ifstream infile(infile_s, std::ios::binary);
38   if (!infile.is_open()) {
39     return absl::UnavailableError(absl::StrCat("Unable to open ", infile_s));
40   }
41   std::ofstream outfile(outfile_s, std::ios::binary);
42   if (!outfile.is_open()) {
43     return absl::UnavailableError(absl::StrCat("Unable to open ", outfile_s));
44   }
45 
46   if (absl::GetFlag(FLAGS_memory_mode)) {
47     if (absl::GetFlag(FLAGS_decompress)) {
48       return DecompressInMemory(api, infile, outfile);
49     }
50     return CompressInMemory(api, infile, outfile, absl::GetFlag(FLAGS_level));
51   }
52   if (absl::GetFlag(FLAGS_decompress)) {
53     return DecompressStream(api, infile, outfile);
54   }
55   return CompressStream(api, infile, outfile, absl::GetFlag(FLAGS_level));
56 }
57 
FileDescriptor(ZstdApi & api,std::string infile_s,std::string outfile_s)58 absl::Status FileDescriptor(ZstdApi& api, std::string infile_s,
59                             std::string outfile_s) {
60   sapi::v::Fd infd(open(infile_s.c_str(), O_RDONLY));
61   if (infd.GetValue() < 0) {
62     return absl::UnavailableError(absl::StrCat("Unable to open ", infile_s));
63   }
64   sapi::v::Fd outfd(open(outfile_s.c_str(), O_WRONLY | O_CREAT));
65   if (outfd.GetValue() < 0) {
66     return absl::UnavailableError(absl::StrCat("Unable to open ", outfile_s));
67   }
68 
69   if (absl::GetFlag(FLAGS_memory_mode)) {
70     if (absl::GetFlag(FLAGS_decompress)) {
71       return DecompressInMemoryFD(api, infd, outfd);
72     }
73     return CompressInMemoryFD(api, infd, outfd, absl::GetFlag(FLAGS_level));
74   }
75   if (absl::GetFlag(FLAGS_decompress)) {
76     return DecompressStreamFD(api, infd, outfd);
77   }
78   return CompressStreamFD(api, infd, outfd, absl::GetFlag(FLAGS_level));
79 }
80 
main(int argc,char * argv[])81 int main(int argc, char* argv[]) {
82   std::string prog_name(argv[0]);
83   absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
84   std::vector<char*> args = absl::ParseCommandLine(argc, argv);
85   absl::InitializeLog();
86 
87   if (args.size() != 3) {
88     std::cerr << "Usage:\n  " << prog_name << " INPUT OUTPUT\n";
89     return EXIT_FAILURE;
90   }
91 
92   ZstdSapiSandbox sandbox;
93   if (!sandbox.Init().ok()) {
94     std::cerr << "Unable to start sandbox\n";
95     return EXIT_FAILURE;
96   }
97 
98   ZstdApi api(&sandbox);
99 
100   absl::Status status;
101   if (absl::GetFlag(FLAGS_stream)) {
102     status = Stream(api, argv[1], argv[2]);
103   } else {
104     status = FileDescriptor(api, argv[1], argv[2]);
105   }
106 
107   if (!status.ok()) {
108     std::cerr << "Unable to ";
109     std::cerr << (absl::GetFlag(FLAGS_decompress) ? "decompress" : "compress");
110     std::cerr << " file.\n" << status << "\n";
111     return EXIT_FAILURE;
112   }
113 
114   return EXIT_SUCCESS;
115 }
116