blob: 7ee10161d86f98aa259c5df2d26cfc21c5b00f9b [file] [log] [blame]
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001/*
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
Adam Lesinski93190b72017-11-03 15:20:17 -070021using ::android::StringPiece;
Adam Lesinski00451162017-10-03 07:44:08 -070022using ::google::protobuf::io::ZeroCopyOutputStream;
23
Adam Lesinskid0f492d2017-04-03 18:12:45 -070024namespace aapt {
25namespace io {
26
27bool CopyInputStreamToArchive(IAaptContext* context, InputStream* in, const std::string& out_path,
28 uint32_t compression_flags, IArchiveWriter* writer) {
29 if (context->IsVerbose()) {
30 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
31 }
32
33 if (!writer->WriteFile(out_path, compression_flags, in)) {
34 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
35 << " to archive: " << writer->GetError());
36 return false;
37 }
38 return true;
39}
40
41bool CopyFileToArchive(IAaptContext* context, io::IFile* file, const std::string& out_path,
42 uint32_t compression_flags, IArchiveWriter* writer) {
43 std::unique_ptr<io::IData> data = file->OpenAsData();
44 if (!data) {
45 context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
46 return false;
47 }
48 return CopyInputStreamToArchive(context, data.get(), out_path, compression_flags, writer);
49}
50
51bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::MessageLite* proto_msg,
52 const std::string& out_path, uint32_t compression_flags,
53 IArchiveWriter* writer) {
54 if (context->IsVerbose()) {
55 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
56 }
57
58 if (writer->StartEntry(out_path, compression_flags)) {
59 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
60 {
61 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
62 ::google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
63 if (!proto_msg->SerializeToZeroCopyStream(&adaptor)) {
64 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
65 << " to archive");
66 return false;
67 }
68 }
69
70 if (writer->FinishEntry()) {
71 return true;
72 }
73 }
74 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
75 << " to archive: " << writer->GetError());
76 return false;
77}
78
79bool Copy(OutputStream* out, InputStream* in) {
80 const void* in_buffer;
81 size_t in_len;
82 while (in->Next(&in_buffer, &in_len)) {
83 void* out_buffer;
84 size_t out_len;
85 if (!out->Next(&out_buffer, &out_len)) {
86 return !out->HadError();
87 }
88
89 const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
90 memcpy(out_buffer, in_buffer, bytes_to_copy);
91 out->BackUp(out_len - bytes_to_copy);
92 in->BackUp(in_len - bytes_to_copy);
93 }
94 return !in->HadError();
95}
96
Adam Lesinski93190b72017-11-03 15:20:17 -070097bool Copy(OutputStream* out, const StringPiece& in) {
98 const char* in_buffer = in.data();
99 size_t in_len = in.size();
100 while (in_len != 0) {
101 void* out_buffer;
102 size_t out_len;
103 if (!out->Next(&out_buffer, &out_len)) {
104 return false;
105 }
106
107 const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
108 memcpy(out_buffer, in_buffer, bytes_to_copy);
109 out->BackUp(out_len - bytes_to_copy);
110 in_buffer += bytes_to_copy;
111 in_len -= bytes_to_copy;
112 }
113 return true;
114}
115
Adam Lesinski00451162017-10-03 07:44:08 -0700116bool Copy(ZeroCopyOutputStream* out, InputStream* in) {
117 OutputStreamAdaptor adaptor(out);
118 return Copy(&adaptor, in);
119}
120
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700121} // namespace io
122} // namespace aapt