• 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,android::InputStream * in,std::string_view out_path,uint32_t compression_flags,IArchiveWriter * writer)29 bool CopyInputStreamToArchive(IAaptContext* context, android::InputStream* in,
30                               std::string_view out_path, uint32_t compression_flags,
31                               IArchiveWriter* writer) {
32   TRACE_CALL();
33   if (context->IsVerbose()) {
34     context->GetDiagnostics()->Note(android::DiagMessage()
35                                     << "writing " << out_path << " to archive");
36   }
37 
38   if (!writer->WriteFile(out_path, compression_flags, in)) {
39     context->GetDiagnostics()->Error(android::DiagMessage()
40                                      << "failed to write " << out_path
41                                      << " to archive: " << writer->GetError());
42     return false;
43   }
44   return true;
45 }
46 
CopyFileToArchive(IAaptContext * context,io::IFile * file,std::string_view out_path,uint32_t compression_flags,IArchiveWriter * writer)47 bool CopyFileToArchive(IAaptContext* context, io::IFile* file, std::string_view out_path,
48                        uint32_t compression_flags, IArchiveWriter* writer) {
49   TRACE_CALL();
50   std::unique_ptr<io::IData> data = file->OpenAsData();
51   if (!data) {
52     context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
53                                      << "failed to open file");
54     return false;
55   }
56   return CopyInputStreamToArchive(context, data.get(), out_path, compression_flags, writer);
57 }
58 
CopyFileToArchivePreserveCompression(IAaptContext * context,io::IFile * file,std::string_view out_path,IArchiveWriter * writer)59 bool CopyFileToArchivePreserveCompression(IAaptContext* context, io::IFile* file,
60                                           std::string_view out_path, IArchiveWriter* writer) {
61   uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u;
62   return CopyFileToArchive(context, file, out_path, compression_flags, writer);
63 }
64 
CopyProtoToArchive(IAaptContext * context,::google::protobuf::Message * proto_msg,std::string_view out_path,uint32_t compression_flags,IArchiveWriter * writer)65 bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::Message* proto_msg,
66                         std::string_view out_path, uint32_t compression_flags,
67                         IArchiveWriter* writer) {
68   TRACE_CALL();
69   if (context->IsVerbose()) {
70     context->GetDiagnostics()->Note(android::DiagMessage()
71                                     << "writing " << out_path << " to archive");
72   }
73 
74   if (writer->StartEntry(out_path, compression_flags)) {
75     // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
76     {
77       // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
78       ::google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
79       if (!proto_msg->SerializeToZeroCopyStream(&adaptor)) {
80         context->GetDiagnostics()->Error(android::DiagMessage()
81                                          << "failed to write " << out_path << " to archive");
82         return false;
83       }
84     }
85 
86     if (writer->FinishEntry()) {
87       return true;
88     }
89   }
90   context->GetDiagnostics()->Error(android::DiagMessage() << "failed to write " << out_path
91                                                           << " to archive: " << writer->GetError());
92   return false;
93 }
94 
Copy(android::OutputStream * out,android::InputStream * in)95 bool Copy(android::OutputStream* out, android::InputStream* in) {
96   TRACE_CALL();
97   const void* in_buffer;
98   size_t in_len;
99   while (in->Next(&in_buffer, &in_len)) {
100     void* out_buffer;
101     size_t out_len;
102     if (!out->Next(&out_buffer, &out_len)) {
103       return !out->HadError();
104     }
105 
106     const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
107     memcpy(out_buffer, in_buffer, bytes_to_copy);
108     out->BackUp(out_len - bytes_to_copy);
109     in->BackUp(in_len - bytes_to_copy);
110   }
111   return !in->HadError();
112 }
113 
Copy(android::OutputStream * out,StringPiece in)114 bool Copy(android::OutputStream* out, StringPiece in) {
115   const char* in_buffer = in.data();
116   size_t in_len = in.size();
117   while (in_len != 0) {
118     void* out_buffer;
119     size_t out_len;
120     if (!out->Next(&out_buffer, &out_len)) {
121       return false;
122     }
123 
124     const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
125     memcpy(out_buffer, in_buffer, bytes_to_copy);
126     out->BackUp(out_len - bytes_to_copy);
127     in_buffer += bytes_to_copy;
128     in_len -= bytes_to_copy;
129   }
130   return true;
131 }
132 
Copy(ZeroCopyOutputStream * out,android::InputStream * in)133 bool Copy(ZeroCopyOutputStream* out, android::InputStream* in) {
134   OutputStreamAdaptor adaptor(out);
135   return Copy(&adaptor, in);
136 }
137 
138 }  // namespace io
139 }  // namespace aapt
140