xref: /aosp_15_r20/external/sandboxed-api/contrib/brotli/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 <iostream>
16 #include <string>
17 
18 #include "absl/flags/flag.h"
19 #include "absl/flags/parse.h"
20 #include "absl/log/globals.h"
21 #include "absl/log/initialize.h"
22 #include "contrib/brotli/sandboxed.h"
23 #include "contrib/brotli/utils/utils_brotli.h"
24 #include "contrib/brotli/utils/utils_brotli_dec.h"
25 #include "contrib/brotli/utils/utils_brotli_enc.h"
26 
27 ABSL_FLAG(bool, decompress, false, "decompress");
28 
CompressInMemory(BrotliSandbox & sandbox,const std::string & in_file_s,const std::string & out_file_s)29 absl::Status CompressInMemory(BrotliSandbox& sandbox,
30                               const std::string& in_file_s,
31                               const std::string& out_file_s) {
32   BrotliEncoder enc(&sandbox);
33   if (!enc.IsInit()) {
34     return absl::UnavailableError("Unable to init brotli encoder");
35   }
36 
37   SAPI_ASSIGN_OR_RETURN(std::vector<uint8_t> buf_in, ReadFile(in_file_s));
38   SAPI_RETURN_IF_ERROR(enc.Compress(buf_in));
39   SAPI_ASSIGN_OR_RETURN(std::vector<uint8_t> buf_out, enc.TakeOutput());
40   SAPI_RETURN_IF_ERROR(WriteFile(out_file_s, buf_out));
41 
42   return absl::OkStatus();
43 }
44 
DecompressInMemory(BrotliSandbox & sandbox,const std::string & in_file_s,const std::string & out_file_s)45 absl::Status DecompressInMemory(BrotliSandbox& sandbox,
46                                 const std::string& in_file_s,
47                                 const std::string& out_file_s) {
48   BrotliDecoder dec(&sandbox);
49   if (!dec.IsInit()) {
50     return absl::UnavailableError("Unable to init brotli decoder");
51   }
52 
53   SAPI_ASSIGN_OR_RETURN(std::vector<uint8_t> buf_in, ReadFile(in_file_s));
54   SAPI_ASSIGN_OR_RETURN(BrotliDecoderResult ret, dec.Decompress(buf_in));
55   if (ret != BROTLI_DECODER_RESULT_SUCCESS) {
56     return absl::UnavailableError("Compressed file corrupt");
57   }
58   SAPI_ASSIGN_OR_RETURN(std::vector<uint8_t> buf_out, dec.TakeOutput());
59   SAPI_RETURN_IF_ERROR(WriteFile(out_file_s, buf_out));
60 
61   return absl::OkStatus();
62 }
63 
main(int argc,char * argv[])64 int main(int argc, char* argv[]) {
65   std::string prog_name(argv[0]);
66   absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
67   std::vector<char*> args = absl::ParseCommandLine(argc, argv);
68   absl::InitializeLog();
69 
70   if (args.size() != 3) {
71     std::cerr << "Usage:\n  " << prog_name << " INPUT OUTPUT\n";
72     return EXIT_FAILURE;
73   }
74 
75   std::string in_file_s(args[1]);
76   std::string out_file_s(args[2]);
77 
78   BrotliSapiSandbox sandbox;
79   if (!sandbox.Init().ok()) {
80     std::cerr << "Unable to start sandbox\n";
81     return EXIT_FAILURE;
82   }
83 
84   absl::Status status;
85   if (absl::GetFlag(FLAGS_decompress)) {
86     status = DecompressInMemory(sandbox, in_file_s, out_file_s);
87   } else {
88     status = CompressInMemory(sandbox, in_file_s, out_file_s);
89   }
90   if (!status.ok()) {
91     std::cerr << status << "\n";
92     return EXIT_FAILURE;
93   }
94 
95   return EXIT_SUCCESS;
96 }
97