Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [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 | |
| 17 | #ifndef AAPT_RES_CHUNK_PULL_PARSER_H |
| 18 | #define AAPT_RES_CHUNK_PULL_PARSER_H |
| 19 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "util/Util.h" |
| 21 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include <androidfw/ResourceTypes.h> |
| 23 | #include <string> |
| 24 | |
| 25 | namespace aapt { |
| 26 | |
| 27 | /** |
| 28 | * A pull parser, modeled after XmlPullParser, that reads |
| 29 | * android::ResChunk_header structs from a block of data. |
| 30 | * |
| 31 | * An android::ResChunk_header specifies a type, headerSize, |
| 32 | * and size. The pull parser will verify that the chunk's size |
| 33 | * doesn't extend beyond the available data, and will iterate |
| 34 | * over each chunk in the given block of data. |
| 35 | * |
| 36 | * Processing nested chunks is done by creating a new ResChunkPullParser |
| 37 | * pointing to the data portion of a chunk. |
| 38 | */ |
| 39 | class ResChunkPullParser { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | public: |
| 41 | enum class Event { |
| 42 | StartDocument, |
| 43 | EndDocument, |
| 44 | BadDocument, |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 45 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | Chunk, |
| 47 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Returns false if the event is EndDocument or BadDocument. |
| 51 | */ |
| 52 | static bool isGoodEvent(Event event); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 53 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Create a ResChunkPullParser to read android::ResChunk_headers |
| 56 | * from the memory pointed to by data, of len bytes. |
| 57 | */ |
| 58 | ResChunkPullParser(const void* data, size_t len); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | ResChunkPullParser(const ResChunkPullParser&) = delete; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | Event getEvent() const; |
| 63 | const std::string& getLastError() const; |
| 64 | const android::ResChunk_header* getChunk() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 65 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Move to the next android::ResChunk_header. |
| 68 | */ |
| 69 | Event next(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | private: |
| 72 | Event mEvent; |
| 73 | const android::ResChunk_header* mData; |
| 74 | size_t mLen; |
| 75 | const android::ResChunk_header* mCurrentChunk; |
| 76 | std::string mLastError; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 79 | template <typename T> |
| 80 | inline static const T* convertTo(const android::ResChunk_header* chunk) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 81 | if (util::deviceToHost16(chunk->headerSize) < sizeof(T)) { |
| 82 | return nullptr; |
| 83 | } |
| 84 | return reinterpret_cast<const T*>(chunk); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | inline static const uint8_t* getChunkData( |
| 88 | const android::ResChunk_header* chunk) { |
| 89 | return reinterpret_cast<const uint8_t*>(chunk) + |
| 90 | util::deviceToHost16(chunk->headerSize); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 93 | inline static uint32_t getChunkDataLen(const android::ResChunk_header* chunk) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | return util::deviceToHost32(chunk->size) - |
| 95 | util::deviceToHost16(chunk->headerSize); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 98 | // |
| 99 | // Implementation |
| 100 | // |
| 101 | |
| 102 | inline bool ResChunkPullParser::isGoodEvent(ResChunkPullParser::Event event) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | return event != Event::EndDocument && event != Event::BadDocument; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | inline ResChunkPullParser::ResChunkPullParser(const void* data, size_t len) |
| 107 | : mEvent(Event::StartDocument), |
| 108 | mData(reinterpret_cast<const android::ResChunk_header*>(data)), |
| 109 | mLen(len), |
| 110 | mCurrentChunk(nullptr) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 111 | |
| 112 | inline ResChunkPullParser::Event ResChunkPullParser::getEvent() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | return mEvent; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | inline const std::string& ResChunkPullParser::getLastError() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | return mLastError; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | inline const android::ResChunk_header* ResChunkPullParser::getChunk() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | return mCurrentChunk; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 125 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | #endif // AAPT_RES_CHUNK_PULL_PARSER_H |