blob: 25aa8f8d19160fc1c30969bc3182ff4dd6b8e0c0 [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#ifndef AAPT_IO_UTIL_H
18#define AAPT_IO_UTIL_H
19
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070020#include <string_view>
Adam Lesinskid0f492d2017-04-03 18:12:45 -070021
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000022#include "androidfw/Streams.h"
Adam Lesinski46708052017-09-29 14:49:15 -070023#include "format/Archive.h"
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070024#include "google/protobuf/io/coded_stream.h"
25#include "google/protobuf/message.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070026#include "io/File.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070027#include "process/IResourceTableConsumer.h"
28
29namespace aapt {
30namespace io {
31
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000032bool CopyInputStreamToArchive(IAaptContext* context, android::InputStream* in,
33 std::string_view out_path, uint32_t compression_flags,
34 IArchiveWriter* writer);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070035
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070036bool CopyFileToArchive(IAaptContext* context, IFile* file, std::string_view out_path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070037 uint32_t compression_flags, IArchiveWriter* writer);
38
Pierre Lecesned55bef72017-11-10 22:31:01 +000039bool CopyFileToArchivePreserveCompression(IAaptContext* context, IFile* file,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070040 std::string_view out_path, IArchiveWriter* writer);
Pierre Lecesned55bef72017-11-10 22:31:01 +000041
Ryan Mitchell22ead1c2019-05-20 16:54:48 -070042bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::Message* proto_msg,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070043 std::string_view out_path, uint32_t compression_flags,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070044 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 Meyerb4f83ff2023-11-30 19:29:50 +000048bool Copy(android::OutputStream* out, android::InputStream* in);
49bool Copy(android::OutputStream* out, android::StringPiece in);
50bool Copy(::google::protobuf::io::ZeroCopyOutputStream* out, android::InputStream* in);
Adam Lesinski00451162017-10-03 07:44:08 -070051
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000052class OutputStreamAdaptor : public android::OutputStream {
Adam Lesinski00451162017-10-03 07:44:08 -070053 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
86class ZeroCopyInputAdaptor : public ::google::protobuf::io::ZeroCopyInputStream {
87 public:
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000088 explicit ZeroCopyInputAdaptor(android::InputStream* in) : in_(in) {
Adam Lesinski00451162017-10-03 07:44:08 -070089 }
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 Meyerb4f83ff2023-11-30 19:29:50 +0000123 android::InputStream* in_;
Adam Lesinski00451162017-10-03 07:44:08 -0700124};
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700125
Ryan Mitchelle0eba7a2018-09-12 08:54:07 -0700126class ProtoInputStreamReader {
127 public:
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000128 explicit ProtoInputStreamReader(android::InputStream* in) : in_(in) {
129 }
Ryan Mitchelle0eba7a2018-09-12 08:54:07 -0700130
Ryan Mitchell22ead1c2019-05-20 16:54:48 -0700131 /** Deserializes a Message proto from the current position in the input stream.*/
132 template <typename T> bool ReadMessage(T *message) {
Ryan Mitchelle0eba7a2018-09-12 08:54:07 -0700133 ZeroCopyInputAdaptor adapter(in_);
134 google::protobuf::io::CodedInputStream coded_stream(&adapter);
Krzysztof KosiƄski92d26332022-08-06 08:24:47 +0000135 coded_stream.SetTotalBytesLimit(std::numeric_limits<int32_t>::max());
Ryan Mitchell22ead1c2019-05-20 16:54:48 -0700136 return message->ParseFromCodedStream(&coded_stream);
Ryan Mitchelle0eba7a2018-09-12 08:54:07 -0700137 }
138
139 private:
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000140 android::InputStream* in_;
Ryan Mitchelle0eba7a2018-09-12 08:54:07 -0700141};
142
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700143} // namespace io
144} // namespace aapt
145
146#endif /* AAPT_IO_UTIL_H */