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