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