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