• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 <string>
16 
17 #include "../sandboxed.h"     // NOLINT(build/include)
18 #include "../tests/libpng.h"  // NOLINT(build/include)
19 #include "sandboxed_api/vars.h"
20 
LibPNGMain(const std::string & infile,const std::string & outfile)21 absl::Status LibPNGMain(const std::string& infile, const std::string& outfile) {
22   LibPNGSapiSandbox sandbox;
23   sandbox.AddFile(infile);
24   sandbox.AddFile(outfile);
25 
26   SAPI_RETURN_IF_ERROR(sandbox.Init());
27   LibPNGApi api(&sandbox);
28 
29   sapi::v::Struct<png_image> image;
30   sapi::v::ConstCStr infile_var(infile.c_str());
31   sapi::v::ConstCStr outfile_var(outfile.c_str());
32 
33   image.mutable_data()->version = PNG_IMAGE_VERSION;
34 
35   SAPI_ASSIGN_OR_RETURN(
36       int result, api.png_image_begin_read_from_file(image.PtrBoth(),
37                                                      infile_var.PtrBefore()));
38   if (!result) {
39     return absl::InternalError(
40         absl::StrCat("begin read error: ", image.mutable_data()->message));
41   }
42 
43   image.mutable_data()->format = PNG_FORMAT_RGBA;
44 
45   sapi::v::Array<uint8_t> buffer(PNG_IMAGE_SIZE(*image.mutable_data()));
46 
47   SAPI_ASSIGN_OR_RETURN(
48       result, api.png_image_finish_read(image.PtrBoth(), nullptr,
49                                         buffer.PtrBoth(), 0, nullptr));
50   if (!result) {
51     return absl::InternalError(
52         absl::StrCat("finish read error: ", image.mutable_data()->message));
53   }
54 
55   SAPI_ASSIGN_OR_RETURN(result, api.png_image_write_to_file(
56                                     image.PtrBoth(), outfile_var.PtrBefore(), 0,
57                                     buffer.PtrBoth(), 0, nullptr));
58   if (!result) {
59     return absl::InternalError(
60         absl::StrCat("write error: ", image.mutable_data()->message));
61   }
62 
63   return absl::OkStatus();
64 }
65 
main(int argc,char * argv[])66 int main(int argc, char* argv[]) {
67   if (argc != 3) {
68     LOG(ERROR) << "usage: example input-file output-file";
69     return EXIT_FAILURE;
70   }
71 
72   auto status = LibPNGMain(argv[1], argv[2]);
73   if (!status.ok()) {
74     LOG(ERROR) << "LibPNGMain failed with error:\n"
75                << status.ToString() << '\n';
76     return EXIT_FAILURE;
77   }
78 
79   return EXIT_SUCCESS;
80 }
81