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_FORMAT_CONTAINER_H
18 #define AAPT_FORMAT_CONTAINER_H
19 
20 #include <inttypes.h>
21 
22 #include "Resources.pb.h"
23 #include "ResourcesInternal.pb.h"
24 #include "androidfw/BigBuffer.h"
25 #include "androidfw/Streams.h"
26 #include "google/protobuf/io/coded_stream.h"
27 #include "google/protobuf/io/zero_copy_stream.h"
28 #include "io/Util.h"
29 
30 namespace aapt {
31 
32 enum ContainerEntryType : uint8_t {
33   kResTable = 0x00u,
34   kResFile = 0x01u,
35 };
36 
37 class ContainerWriter {
38  public:
39   explicit ContainerWriter(::google::protobuf::io::ZeroCopyOutputStream* out, size_t entry_count);
40 
41   bool AddResTableEntry(const pb::ResourceTable& table);
42   bool AddResFileEntry(const pb::internal::CompiledFile& file, android::KnownSizeInputStream* in);
43   bool HadError() const;
44   std::string GetError() const;
45 
46  private:
47   DISALLOW_COPY_AND_ASSIGN(ContainerWriter);
48 
49   ::google::protobuf::io::ZeroCopyOutputStream* out_;
50   size_t total_entry_count_;
51   size_t current_entry_count_;
52   std::string error_;
53 };
54 
55 class ContainerReader;
56 
57 class ContainerReaderEntry {
58  public:
59   ContainerEntryType Type() const;
60 
61   bool GetResTable(pb::ResourceTable* out_table);
62   bool GetResFileOffsets(pb::internal::CompiledFile* out_file, off64_t* out_offset,
63                          size_t* out_len);
64 
65   bool HadError() const;
66   std::string GetError() const;
67 
68  private:
69   DISALLOW_COPY_AND_ASSIGN(ContainerReaderEntry);
70 
71   friend class ContainerReader;
72 
73   explicit ContainerReaderEntry(ContainerReader* reader);
74 
75   ContainerReader* reader_;
76   ContainerEntryType type_ = ContainerEntryType::kResTable;
77   size_t length_ = 0u;
78 };
79 
80 class ContainerReader {
81  public:
82   explicit ContainerReader(android::InputStream* in);
83 
84   ContainerReaderEntry* Next();
85 
86   bool HadError() const;
87   std::string GetError() const;
88 
89  private:
90   DISALLOW_COPY_AND_ASSIGN(ContainerReader);
91 
92   friend class ContainerReaderEntry;
93 
94   android::InputStream* in_;
95   io::ZeroCopyInputAdaptor adaptor_;
96   ::google::protobuf::io::CodedInputStream coded_in_;
97   size_t total_entry_count_;
98   size_t current_entry_count_;
99   ContainerReaderEntry entry_;
100   std::string error_;
101 };
102 
103 }  // namespace aapt
104 
105 #endif /* AAPT_FORMAT_CONTAINER_H */
106