Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 21 | using ::android::StringPiece; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 22 | using ::google::protobuf::io::ZeroCopyOutputStream; |
| 23 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 24 | namespace aapt { |
| 25 | namespace io { |
| 26 | |
| 27 | bool 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 | |
| 41 | bool 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 | |
Pierre Lecesne | d55bef7 | 2017-11-10 22:31:01 +0000 | [diff] [blame] | 51 | bool 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 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 57 | bool 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 | |
| 85 | bool 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 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 103 | bool 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 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 122 | bool Copy(ZeroCopyOutputStream* out, InputStream* in) { |
| 123 | OutputStreamAdaptor adaptor(out); |
| 124 | return Copy(&adaptor, in); |
| 125 | } |
| 126 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 127 | } // namespace io |
| 128 | } // namespace aapt |