| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 17 | #ifndef AAPT_FORMAT_BINARY_CHUNKWRITER_H |
| 18 | #define AAPT_FORMAT_BINARY_CHUNKWRITER_H |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 19 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include "android-base/macros.h" |
| 21 | #include "androidfw/ResourceTypes.h" |
| 22 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | #include "util/BigBuffer.h" |
| 24 | #include "util/Util.h" |
| 25 | |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | namespace aapt { |
| 27 | |
| 28 | class ChunkWriter { |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 29 | public: |
| Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 30 | explicit inline ChunkWriter(BigBuffer* buffer) : buffer_(buffer) { |
| 31 | } |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 32 | ChunkWriter(ChunkWriter&&) = default; |
| 33 | ChunkWriter& operator=(ChunkWriter&&) = default; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | template <typename T> |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | inline T* StartChunk(uint16_t type) { |
| 37 | start_size_ = buffer_->size(); |
| 38 | T* chunk = buffer_->NextBlock<T>(); |
| 39 | header_ = &chunk->header; |
| 40 | header_->type = util::HostToDevice16(type); |
| 41 | header_->headerSize = util::HostToDevice16(sizeof(T)); |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | return chunk; |
| 43 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | template <typename T> |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | inline T* NextBlock(size_t count = 1) { |
| 47 | return buffer_->NextBlock<T>(count); |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 49 | |
| Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 50 | inline BigBuffer* buffer() { |
| 51 | return buffer_; |
| 52 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | |
| Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 54 | inline android::ResChunk_header* chunk_header() { |
| 55 | return header_; |
| 56 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 57 | |
| Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 58 | inline size_t size() { |
| 59 | return buffer_->size() - start_size_; |
| 60 | } |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 61 | |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | inline android::ResChunk_header* Finish() { |
| 63 | buffer_->Align4(); |
| 64 | header_->size = util::HostToDevice32(buffer_->size() - start_size_); |
| 65 | return header_; |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | } |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | DISALLOW_COPY_AND_ASSIGN(ChunkWriter); |
| 70 | |
| 71 | BigBuffer* buffer_; |
| 72 | size_t start_size_ = 0; |
| 73 | android::ResChunk_header* header_ = nullptr; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | template <> |
| Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | inline android::ResChunk_header* ChunkWriter::StartChunk(uint16_t type) { |
| 78 | start_size_ = buffer_->size(); |
| 79 | header_ = buffer_->NextBlock<android::ResChunk_header>(); |
| 80 | header_->type = util::HostToDevice16(type); |
| 81 | header_->headerSize = util::HostToDevice16(sizeof(android::ResChunk_header)); |
| 82 | return header_; |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | } // namespace aapt |
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 86 | |
| Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 87 | #endif /* AAPT_FORMAT_BINARY_CHUNKWRITER_H */ |