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 | #ifndef AAPT_IO_UTIL_H |
| 18 | #define AAPT_IO_UTIL_H |
| 19 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 20 | #include <string_view> |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 21 | |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 22 | #include "androidfw/Streams.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 23 | #include "format/Archive.h" |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 24 | #include "google/protobuf/io/coded_stream.h" |
| 25 | #include "google/protobuf/message.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 26 | #include "io/File.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 27 | #include "process/IResourceTableConsumer.h" |
| 28 | |
| 29 | namespace aapt { |
| 30 | namespace io { |
| 31 | |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 32 | bool CopyInputStreamToArchive(IAaptContext* context, android::InputStream* in, |
| 33 | std::string_view out_path, uint32_t compression_flags, |
| 34 | IArchiveWriter* writer); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 35 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 36 | bool CopyFileToArchive(IAaptContext* context, IFile* file, std::string_view out_path, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 37 | uint32_t compression_flags, IArchiveWriter* writer); |
| 38 | |
Pierre Lecesne | d55bef7 | 2017-11-10 22:31:01 +0000 | [diff] [blame] | 39 | bool CopyFileToArchivePreserveCompression(IAaptContext* context, IFile* file, |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 40 | std::string_view out_path, IArchiveWriter* writer); |
Pierre Lecesne | d55bef7 | 2017-11-10 22:31:01 +0000 | [diff] [blame] | 41 | |
Ryan Mitchell | 22ead1c | 2019-05-20 16:54:48 -0700 | [diff] [blame] | 42 | bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::Message* proto_msg, |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 43 | std::string_view out_path, uint32_t compression_flags, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 44 | IArchiveWriter* writer); |
| 45 | |
| 46 | // Copies the data from in to out. Returns false if there was an error. |
| 47 | // If there was an error, check the individual streams' HadError/GetError methods. |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 48 | bool Copy(android::OutputStream* out, android::InputStream* in); |
| 49 | bool Copy(android::OutputStream* out, android::StringPiece in); |
| 50 | bool Copy(::google::protobuf::io::ZeroCopyOutputStream* out, android::InputStream* in); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 51 | |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 52 | class OutputStreamAdaptor : public android::OutputStream { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 53 | public: |
| 54 | explicit OutputStreamAdaptor(::google::protobuf::io::ZeroCopyOutputStream* out) : out_(out) { |
| 55 | } |
| 56 | |
| 57 | bool Next(void** data, size_t* size) override { |
| 58 | int out_size; |
| 59 | bool result = out_->Next(data, &out_size); |
| 60 | *size = static_cast<size_t>(out_size); |
| 61 | if (!result) { |
| 62 | error_ocurred_ = true; |
| 63 | } |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | void BackUp(size_t count) override { |
| 68 | out_->BackUp(static_cast<int>(count)); |
| 69 | } |
| 70 | |
| 71 | size_t ByteCount() const override { |
| 72 | return static_cast<size_t>(out_->ByteCount()); |
| 73 | } |
| 74 | |
| 75 | bool HadError() const override { |
| 76 | return error_ocurred_; |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | DISALLOW_COPY_AND_ASSIGN(OutputStreamAdaptor); |
| 81 | |
| 82 | ::google::protobuf::io::ZeroCopyOutputStream* out_; |
| 83 | bool error_ocurred_ = false; |
| 84 | }; |
| 85 | |
| 86 | class ZeroCopyInputAdaptor : public ::google::protobuf::io::ZeroCopyInputStream { |
| 87 | public: |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 88 | explicit ZeroCopyInputAdaptor(android::InputStream* in) : in_(in) { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool Next(const void** data, int* size) override { |
| 92 | size_t out_size; |
| 93 | bool result = in_->Next(data, &out_size); |
| 94 | *size = static_cast<int>(out_size); |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | void BackUp(int count) override { |
| 99 | in_->BackUp(static_cast<size_t>(count)); |
| 100 | } |
| 101 | |
| 102 | bool Skip(int count) override { |
| 103 | const void* data; |
| 104 | int size; |
| 105 | while (Next(&data, &size)) { |
| 106 | if (size > count) { |
| 107 | BackUp(size - count); |
| 108 | return true; |
| 109 | } else { |
| 110 | count -= size; |
| 111 | } |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | ::google::protobuf::int64 ByteCount() const override { |
| 117 | return static_cast<::google::protobuf::int64>(in_->ByteCount()); |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | DISALLOW_COPY_AND_ASSIGN(ZeroCopyInputAdaptor); |
| 122 | |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 123 | android::InputStream* in_; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 124 | }; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 125 | |
Ryan Mitchell | e0eba7a | 2018-09-12 08:54:07 -0700 | [diff] [blame] | 126 | class ProtoInputStreamReader { |
| 127 | public: |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 128 | explicit ProtoInputStreamReader(android::InputStream* in) : in_(in) { |
| 129 | } |
Ryan Mitchell | e0eba7a | 2018-09-12 08:54:07 -0700 | [diff] [blame] | 130 | |
Ryan Mitchell | 22ead1c | 2019-05-20 16:54:48 -0700 | [diff] [blame] | 131 | /** Deserializes a Message proto from the current position in the input stream.*/ |
| 132 | template <typename T> bool ReadMessage(T *message) { |
Ryan Mitchell | e0eba7a | 2018-09-12 08:54:07 -0700 | [diff] [blame] | 133 | ZeroCopyInputAdaptor adapter(in_); |
| 134 | google::protobuf::io::CodedInputStream coded_stream(&adapter); |
Krzysztof KosiĆski | 92d2633 | 2022-08-06 08:24:47 +0000 | [diff] [blame] | 135 | coded_stream.SetTotalBytesLimit(std::numeric_limits<int32_t>::max()); |
Ryan Mitchell | 22ead1c | 2019-05-20 16:54:48 -0700 | [diff] [blame] | 136 | return message->ParseFromCodedStream(&coded_stream); |
Ryan Mitchell | e0eba7a | 2018-09-12 08:54:07 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | private: |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 140 | android::InputStream* in_; |
Ryan Mitchell | e0eba7a | 2018-09-12 08:54:07 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 143 | } // namespace io |
| 144 | } // namespace aapt |
| 145 | |
| 146 | #endif /* AAPT_IO_UTIL_H */ |