xref: /aosp_15_r20/external/sandboxed-api/contrib/zopfli/utils/utils_zopfli.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2022 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #include "contrib/zopfli/utils/utils_zopfli.h"
16*ec63e07aSXin Li 
17*ec63e07aSXin Li #include <unistd.h>
18*ec63e07aSXin Li 
19*ec63e07aSXin Li #include <fstream>
20*ec63e07aSXin Li 
Compress(ZopfliApi & api,std::ifstream & instream,std::ofstream & outstream,ZopfliFormat format)21*ec63e07aSXin Li absl::Status Compress(ZopfliApi& api, std::ifstream& instream,
22*ec63e07aSXin Li                       std::ofstream& outstream, ZopfliFormat format) {
23*ec63e07aSXin Li   // Get size of Stream
24*ec63e07aSXin Li   instream.seekg(0, std::ios_base::end);
25*ec63e07aSXin Li   std::streamsize ssize = instream.tellg();
26*ec63e07aSXin Li   instream.seekg(0, std::ios_base::beg);
27*ec63e07aSXin Li 
28*ec63e07aSXin Li   // Read data
29*ec63e07aSXin Li   sapi::v::Array<uint8_t> inbuf(ssize);
30*ec63e07aSXin Li   instream.read(reinterpret_cast<char*>(inbuf.GetData()), ssize);
31*ec63e07aSXin Li   if (instream.gcount() != ssize) {
32*ec63e07aSXin Li     return absl::UnavailableError("Unable to read file");
33*ec63e07aSXin Li   }
34*ec63e07aSXin Li 
35*ec63e07aSXin Li   // Compress
36*ec63e07aSXin Li   sapi::v::Struct<ZopfliOptions> options;
37*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(api.ZopfliInitOptions(options.PtrAfter()));
38*ec63e07aSXin Li 
39*ec63e07aSXin Li   sapi::v::GenericPtr outptr;
40*ec63e07aSXin Li   sapi::v::IntBase<size_t> outsize(0);
41*ec63e07aSXin Li 
42*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(
43*ec63e07aSXin Li       api.ZopfliCompress(options.PtrBefore(), format, inbuf.PtrBefore(), ssize,
44*ec63e07aSXin Li                          outptr.PtrAfter(), outsize.PtrBoth()));
45*ec63e07aSXin Li 
46*ec63e07aSXin Li   // Get and save data
47*ec63e07aSXin Li   sapi::v::Array<int8_t> outbuf(outsize.GetValue());
48*ec63e07aSXin Li   outbuf.SetRemote(reinterpret_cast<void*>(outptr.GetValue()));
49*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(api.GetSandbox()->TransferFromSandboxee(&outbuf));
50*ec63e07aSXin Li 
51*ec63e07aSXin Li   outstream.write(reinterpret_cast<char*>(outbuf.GetData()), outbuf.GetSize());
52*ec63e07aSXin Li   if (!outstream.good()) {
53*ec63e07aSXin Li     return absl::UnavailableError("Unable to write file");
54*ec63e07aSXin Li   }
55*ec63e07aSXin Li   return absl::OkStatus();
56*ec63e07aSXin Li }
57*ec63e07aSXin Li 
CompressFD(ZopfliApi & api,sapi::v::Fd & infd,sapi::v::Fd & outfd,ZopfliFormat format)58*ec63e07aSXin Li absl::Status CompressFD(ZopfliApi& api, sapi::v::Fd& infd, sapi::v::Fd& outfd,
59*ec63e07aSXin Li                         ZopfliFormat format) {
60*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(api.GetSandbox()->TransferToSandboxee(&infd));
61*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(api.GetSandbox()->TransferToSandboxee(&outfd));
62*ec63e07aSXin Li 
63*ec63e07aSXin Li   sapi::v::Struct<ZopfliOptions> options;
64*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(api.ZopfliInitOptions(options.PtrAfter()));
65*ec63e07aSXin Li 
66*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(
67*ec63e07aSXin Li       int ret, api.ZopfliCompressFD(options.PtrBefore(), format,
68*ec63e07aSXin Li                                     infd.GetRemoteFd(), outfd.GetRemoteFd()));
69*ec63e07aSXin Li 
70*ec63e07aSXin Li   infd.CloseRemoteFd(api.GetSandbox()->rpc_channel()).IgnoreError();
71*ec63e07aSXin Li   outfd.CloseRemoteFd(api.GetSandbox()->rpc_channel()).IgnoreError();
72*ec63e07aSXin Li 
73*ec63e07aSXin Li   if (ret == -1) {
74*ec63e07aSXin Li     return absl::UnavailableError("Unable to compress file");
75*ec63e07aSXin Li   }
76*ec63e07aSXin Li 
77*ec63e07aSXin Li   return absl::OkStatus();
78*ec63e07aSXin Li }
79