• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "io/Util.h"
18 
19 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
20 
21 #include "trace/TraceBuffer.h"
22 
23 using ::android::StringPiece;
24 using ::google::protobuf::io::ZeroCopyOutputStream;
25 
26 namespace aapt {
27 namespace io {
28 
CopyInputStreamToArchive(IAaptContext * context,InputStream * in,const std::string & out_path,uint32_t compression_flags,IArchiveWriter * writer)29 bool CopyInputStreamToArchive(IAaptContext* context, InputStream* in, const std::string& out_path,
30                               uint32_t compression_flags, IArchiveWriter* writer) {
31   TRACE_CALL();
32   if (context->IsVerbose()) {
33     context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
34   }
35 
36   if (!writer->WriteFile(out_path, compression_flags, in)) {
37     context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
38                                                    << " to archive: " << writer->GetError());
39     return false;
40   }
41   return true;
42 }
43 
CopyFileToArchive(IAaptContext * context,io::IFile * file,const std::string & out_path,uint32_t compression_flags,IArchiveWriter * writer)44 bool CopyFileToArchive(IAaptContext* context, io::IFile* file, const std::string& out_path,
45                        uint32_t compression_flags, IArchiveWriter* writer) {
46   TRACE_CALL();
47   std::unique_ptr<io::IData> data = file->OpenAsData();
48   if (!data) {
49     context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
50     return false;
51   }
52   return CopyInputStreamToArchive(context, data.get(), out_path, compression_flags, writer);
53 }
54 
CopyFileToArchivePreserveCompression(IAaptContext * context,io::IFile * file,const std::string & out_path,IArchiveWriter * writer)55 bool CopyFileToArchivePreserveCompression(IAaptContext* context, io::IFile* file,
56                                           const std::string& out_path, IArchiveWriter* writer) {
57   uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u;
58   return CopyFileToArchive(context, file, out_path, compression_flags, writer);
59 }
60 
CopyProtoToArchive(IAaptContext * context,::google::protobuf::Message * proto_msg,const std::string & out_path,uint32_t compression_flags,IArchiveWriter * writer)61 bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::Message* proto_msg,
62                         const std::string& out_path, uint32_t compression_flags,
63                         IArchiveWriter* writer) {
64   TRACE_CALL();
65   if (context->IsVerbose()) {
66     context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
67   }
68 
69   if (writer->StartEntry(out_path, compression_flags)) {
70     // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
71     {
72       // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
73       ::google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
74       if (!proto_msg->SerializeToZeroCopyStream(&adaptor)) {
75         context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
76                                                        << " to archive");
77         return false;
78       }
79     }
80 
81     if (writer->FinishEntry()) {
82       return true;
83     }
84   }
85   context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
86                                                  << " to archive: " << writer->GetError());
87   return false;
88 }
89 
Copy(OutputStream * out,InputStream * in)90 bool Copy(OutputStream* out, InputStream* in) {
91   TRACE_CALL();
92   const void* in_buffer;
93   size_t in_len;
94   while (in->Next(&in_buffer, &in_len)) {
95     void* out_buffer;
96     size_t out_len;
97     if (!out->Next(&out_buffer, &out_len)) {
98       return !out->HadError();
99     }
100 
101     const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
102     memcpy(out_buffer, in_buffer, bytes_to_copy);
103     out->BackUp(out_len - bytes_to_copy);
104     in->BackUp(in_len - bytes_to_copy);
105   }
106   return !in->HadError();
107 }
108 
Copy(OutputStream * out,const StringPiece & in)109 bool Copy(OutputStream* out, const StringPiece& in) {
110   const char* in_buffer = in.data();
111   size_t in_len = in.size();
112   while (in_len != 0) {
113     void* out_buffer;
114     size_t out_len;
115     if (!out->Next(&out_buffer, &out_len)) {
116       return false;
117     }
118 
119     const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
120     memcpy(out_buffer, in_buffer, bytes_to_copy);
121     out->BackUp(out_len - bytes_to_copy);
122     in_buffer += bytes_to_copy;
123     in_len -= bytes_to_copy;
124   }
125   return true;
126 }
127 
Copy(ZeroCopyOutputStream * out,InputStream * in)128 bool Copy(ZeroCopyOutputStream* out, InputStream* in) {
129   OutputStreamAdaptor adaptor(out);
130   return Copy(&adaptor, in);
131 }
132 
133 }  // namespace io
134 }  // namespace aapt
135