blob: 968d3eef48ec12a9df2e6aa704568d92bc17d58e [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 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_FLATTEN_CHUNKWRITER_H
18#define AAPT_FLATTEN_CHUNKWRITER_H
19
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include "android-base/macros.h"
21#include "androidfw/ResourceTypes.h"
22
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "util/BigBuffer.h"
24#include "util/Util.h"
25
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026namespace aapt {
27
28class ChunkWriter {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 explicit inline ChunkWriter(BigBuffer* buffer) : buffer_(buffer) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 ChunkWriter(ChunkWriter&&) = default;
32 ChunkWriter& operator=(ChunkWriter&&) = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 inline T* StartChunk(uint16_t type) {
36 start_size_ = buffer_->size();
37 T* chunk = buffer_->NextBlock<T>();
38 header_ = &chunk->header;
39 header_->type = util::HostToDevice16(type);
40 header_->headerSize = util::HostToDevice16(sizeof(T));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 return chunk;
42 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 inline T* NextBlock(size_t count = 1) {
46 return buffer_->NextBlock<T>(count);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 inline BigBuffer* buffer() { return buffer_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 inline android::ResChunk_header* chunk_header() { return header_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 inline size_t size() { return buffer_->size() - start_size_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 inline android::ResChunk_header* Finish() {
56 buffer_->Align4();
57 header_->size = util::HostToDevice32(buffer_->size() - start_size_);
58 return header_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060
61 private:
62 DISALLOW_COPY_AND_ASSIGN(ChunkWriter);
63
64 BigBuffer* buffer_;
65 size_t start_size_ = 0;
66 android::ResChunk_header* header_ = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067};
68
69template <>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070inline android::ResChunk_header* ChunkWriter::StartChunk(uint16_t type) {
71 start_size_ = buffer_->size();
72 header_ = buffer_->NextBlock<android::ResChunk_header>();
73 header_->type = util::HostToDevice16(type);
74 header_->headerSize = util::HostToDevice16(sizeof(android::ResChunk_header));
75 return header_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076}
77
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079
80#endif /* AAPT_FLATTEN_CHUNKWRITER_H */