blob: 685f522a2e7146d4e0424c2796f9f5707aa34e61 [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
Adam Lesinski46708052017-09-29 14:49:15 -070022#include "format/Archive.h"
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070023#include "google/protobuf/io/coded_stream.h"
24#include "google/protobuf/message.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070025#include "io/File.h"
26#include "io/Io.h"
27#include "process/IResourceTableConsumer.h"
28
29namespace aapt {
30namespace io {
31
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070032bool CopyInputStreamToArchive(IAaptContext* context, InputStream* in, std::string_view out_path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070033 uint32_t compression_flags, IArchiveWriter* writer);
34
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070035bool CopyFileToArchive(IAaptContext* context, IFile* file, std::string_view out_path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070036 uint32_t compression_flags, IArchiveWriter* writer);
37
Pierre Lecesned55bef72017-11-10 22:31:01 +000038bool CopyFileToArchivePreserveCompression(IAaptContext* context, IFile* file,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070039 std::string_view out_path, IArchiveWriter* writer);
Pierre Lecesned55bef72017-11-10 22:31:01 +000040
Ryan Mitchell22ead1c2019-05-20 16:54:48 -070041bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::Message* proto_msg,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070042 std::string_view out_path, uint32_t compression_flags,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070043 IArchiveWriter* writer);
44
45// Copies the data from in to out. Returns false if there was an error.
46// If there was an error, check the individual streams' HadError/GetError methods.
47bool Copy(OutputStream* out, InputStream* in);
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070048bool Copy(OutputStream* out, android::StringPiece in);
Adam Lesinski00451162017-10-03 07:44:08 -070049bool Copy(::google::protobuf::io::ZeroCopyOutputStream* out, InputStream* in);
50
51class OutputStreamAdaptor : public io::OutputStream {
52 public:
53 explicit OutputStreamAdaptor(::google::protobuf::io::ZeroCopyOutputStream* out) : out_(out) {
54 }
55
56 bool Next(void** data, size_t* size) override {
57 int out_size;
58 bool result = out_->Next(data, &out_size);
59 *size = static_cast<size_t>(out_size);
60 if (!result) {
61 error_ocurred_ = true;
62 }
63 return result;
64 }
65
66 void BackUp(size_t count) override {
67 out_->BackUp(static_cast<int>(count));
68 }
69
70 size_t ByteCount() const override {
71 return static_cast<size_t>(out_->ByteCount());
72 }
73
74 bool HadError() const override {
75 return error_ocurred_;
76 }
77
78 private:
79 DISALLOW_COPY_AND_ASSIGN(OutputStreamAdaptor);
80
81 ::google::protobuf::io::ZeroCopyOutputStream* out_;
82 bool error_ocurred_ = false;
83};
84
85class ZeroCopyInputAdaptor : public ::google::protobuf::io::ZeroCopyInputStream {
86 public:
87 explicit ZeroCopyInputAdaptor(io::InputStream* in) : in_(in) {
88 }
89
90 bool Next(const void** data, int* size) override {
91 size_t out_size;
92 bool result = in_->Next(data, &out_size);
93 *size = static_cast<int>(out_size);
94 return result;
95 }
96
97 void BackUp(int count) override {
98 in_->BackUp(static_cast<size_t>(count));
99 }
100
101 bool Skip(int count) override {
102 const void* data;
103 int size;
104 while (Next(&data, &size)) {
105 if (size > count) {
106 BackUp(size - count);
107 return true;
108 } else {
109 count -= size;
110 }
111 }
112 return false;
113 }
114
115 ::google::protobuf::int64 ByteCount() const override {
116 return static_cast<::google::protobuf::int64>(in_->ByteCount());
117 }
118
119 private:
120 DISALLOW_COPY_AND_ASSIGN(ZeroCopyInputAdaptor);
121
122 io::InputStream* in_;
123};
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700124
Ryan Mitchelle0eba7a2018-09-12 08:54:07 -0700125class ProtoInputStreamReader {
126 public:
127 explicit ProtoInputStreamReader(io::InputStream* in) : in_(in) { }
128
Ryan Mitchell22ead1c2019-05-20 16:54:48 -0700129 /** Deserializes a Message proto from the current position in the input stream.*/
130 template <typename T> bool ReadMessage(T *message) {
Ryan Mitchelle0eba7a2018-09-12 08:54:07 -0700131 ZeroCopyInputAdaptor adapter(in_);
132 google::protobuf::io::CodedInputStream coded_stream(&adapter);
Krzysztof KosiƄski92d26332022-08-06 08:24:47 +0000133 coded_stream.SetTotalBytesLimit(std::numeric_limits<int32_t>::max());
Ryan Mitchell22ead1c2019-05-20 16:54:48 -0700134 return message->ParseFromCodedStream(&coded_stream);
Ryan Mitchelle0eba7a2018-09-12 08:54:07 -0700135 }
136
137 private:
138 io::InputStream* in_;
139};
140
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700141} // namespace io
142} // namespace aapt
143
144#endif /* AAPT_IO_UTIL_H */