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