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 <fstream>
19 #include <iostream>
20 #include <string>
21
22 #include "absl/flags/flag.h"
23 #include "absl/flags/parse.h"
24 #include "absl/log/globals.h"
25 #include "absl/log/initialize.h"
26 #include "contrib/zopfli/sandboxed.h"
27 #include "contrib/zopfli/utils/utils_zopfli.h"
28
29 ABSL_FLAG(bool, stream, false, "stream memory to sandbox");
30 ABSL_FLAG(bool, zlib, false, "zlib compression");
31 ABSL_FLAG(bool, gzip, false, "gzip compression");
32
CompressMain(ZopfliApi & api,std::string & infile_s,std::string & outfile_s,ZopfliFormat format)33 absl::Status CompressMain(ZopfliApi& api, std::string& infile_s,
34 std::string& outfile_s, ZopfliFormat format) {
35 std::ifstream infile(infile_s, std::ios::binary);
36 if (!infile.is_open()) {
37 return absl::UnavailableError(absl::StrCat("Unable to open ", infile_s));
38 }
39 std::ofstream outfile(outfile_s, std::ios::binary);
40 if (!outfile.is_open()) {
41 return absl::UnavailableError(absl::StrCat("Unable to open ", outfile_s));
42 }
43
44 return Compress(api, infile, outfile, format);
45 }
46
CompressMainFD(ZopfliApi & api,std::string & infile_s,std::string & outfile_s,ZopfliFormat format)47 absl::Status CompressMainFD(ZopfliApi& api, std::string& infile_s,
48 std::string& outfile_s, ZopfliFormat format) {
49 sapi::v::Fd infd(open(infile_s.c_str(), O_RDONLY));
50 if (infd.GetValue() < 0) {
51 return absl::UnavailableError(absl::StrCat("Unable to open ", infile_s));
52 }
53
54 sapi::v::Fd outfd(open(outfile_s.c_str(), O_WRONLY | O_CREAT));
55 if (outfd.GetValue() < 0) {
56 return absl::UnavailableError(absl::StrCat("Unable to open ", outfile_s));
57 }
58
59 return (CompressFD(api, infd, outfd, format));
60 }
61
main(int argc,char * argv[])62 int main(int argc, char* argv[]) {
63 std::string prog_name(argv[0]);
64 absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
65 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
66 absl::InitializeLog();
67
68 if (args.size() != 3) {
69 std::cerr << "Usage:\n " << prog_name << " INPUT OUTPUT\n";
70 return EXIT_FAILURE;
71 }
72
73 ZopfliSapiSandbox sandbox;
74 if (!sandbox.Init().ok()) {
75 std::cerr << "Unable to start sandbox\n";
76 return EXIT_FAILURE;
77 }
78 std::string infile_s(args[1]);
79 std::string outfile_s(args[2]);
80
81 ZopfliApi api(&sandbox);
82
83 ZopfliFormat format = ZOPFLI_FORMAT_DEFLATE;
84 if (absl::GetFlag(FLAGS_zlib)) {
85 format = ZOPFLI_FORMAT_ZLIB;
86 } else if (absl::GetFlag(FLAGS_gzip)) {
87 format = ZOPFLI_FORMAT_GZIP;
88 }
89
90 absl::Status status;
91 if (absl::GetFlag(FLAGS_stream)) {
92 status = CompressMain(api, infile_s, outfile_s, format);
93 } else {
94 status = CompressMainFD(api, infile_s, outfile_s, format);
95 }
96
97 if (!status.ok()) {
98 std::cerr << "Unable to compress file.\n";
99 std::cerr << status << std::endl;
100 return EXIT_FAILURE;
101 }
102
103 return EXIT_SUCCESS;
104 }
105