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
21using ::android::StringPiece;
22using ::google::protobuf::io::ZeroCopyOutputStream;
23
24namespace 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 CopyFileToArchivePreserveCompression(IAaptContext* context, io::IFile* file,
52                                          const std::string& out_path, IArchiveWriter* writer) {
53  uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u;
54  return CopyFileToArchive(context, file, out_path, compression_flags, writer);
55}
56
57bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::MessageLite* proto_msg,
58                        const std::string& out_path, uint32_t compression_flags,
59                        IArchiveWriter* writer) {
60  if (context->IsVerbose()) {
61    context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
62  }
63
64  if (writer->StartEntry(out_path, compression_flags)) {
65    // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
66    {
67      // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
68      ::google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
69      if (!proto_msg->SerializeToZeroCopyStream(&adaptor)) {
70        context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
71                                                       << " to archive");
72        return false;
73      }
74    }
75
76    if (writer->FinishEntry()) {
77      return true;
78    }
79  }
80  context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
81                                                 << " to archive: " << writer->GetError());
82  return false;
83}
84
85bool Copy(OutputStream* out, InputStream* in) {
86  const void* in_buffer;
87  size_t in_len;
88  while (in->Next(&in_buffer, &in_len)) {
89    void* out_buffer;
90    size_t out_len;
91    if (!out->Next(&out_buffer, &out_len)) {
92      return !out->HadError();
93    }
94
95    const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
96    memcpy(out_buffer, in_buffer, bytes_to_copy);
97    out->BackUp(out_len - bytes_to_copy);
98    in->BackUp(in_len - bytes_to_copy);
99  }
100  return !in->HadError();
101}
102
103bool Copy(OutputStream* out, const StringPiece& in) {
104  const char* in_buffer = in.data();
105  size_t in_len = in.size();
106  while (in_len != 0) {
107    void* out_buffer;
108    size_t out_len;
109    if (!out->Next(&out_buffer, &out_len)) {
110      return false;
111    }
112
113    const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
114    memcpy(out_buffer, in_buffer, bytes_to_copy);
115    out->BackUp(out_len - bytes_to_copy);
116    in_buffer += bytes_to_copy;
117    in_len -= bytes_to_copy;
118  }
119  return true;
120}
121
122bool Copy(ZeroCopyOutputStream* out, InputStream* in) {
123  OutputStreamAdaptor adaptor(out);
124  return Copy(&adaptor, in);
125}
126
127}  // namespace io
128}  // namespace aapt
129