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 | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "android-base/macros.h" |
| 23 | #include "androidfw/ResourceTypes.h" |
| 24 | |
| 25 | #include "util/Util.h" |
| 26 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 27 | namespace aapt { |
| 28 | |
| 29 | /** |
| 30 | * A pull parser, modeled after XmlPullParser, that reads |
| 31 | * android::ResChunk_header structs from a block of data. |
| 32 | * |
| 33 | * An android::ResChunk_header specifies a type, headerSize, |
| 34 | * and size. The pull parser will verify that the chunk's size |
| 35 | * doesn't extend beyond the available data, and will iterate |
| 36 | * over each chunk in the given block of data. |
| 37 | * |
| 38 | * Processing nested chunks is done by creating a new ResChunkPullParser |
| 39 | * pointing to the data portion of a chunk. |
| 40 | */ |
| 41 | class ResChunkPullParser { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | public: |
| 43 | enum class Event { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | kStartDocument, |
| 45 | kEndDocument, |
| 46 | kBadDocument, |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | kChunk, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 50 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Returns false if the event is EndDocument or BadDocument. |
| 53 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | static bool IsGoodEvent(Event event); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 55 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Create a ResChunkPullParser to read android::ResChunk_headers |
| 58 | * from the memory pointed to by data, of len bytes. |
| 59 | */ |
| 60 | ResChunkPullParser(const void* data, size_t len); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | Event event() const; |
| 63 | const std::string& error() const; |
| 64 | const android::ResChunk_header* chunk() 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 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | DISALLOW_COPY_AND_ASSIGN(ResChunkPullParser); |
| 73 | |
| 74 | Event event_; |
| 75 | const android::ResChunk_header* data_; |
| 76 | size_t len_; |
| 77 | const android::ResChunk_header* current_chunk_; |
| 78 | std::string error_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
Adam Lesinski | 136fd07 | 2017-03-03 13:50:21 -0800 | [diff] [blame] | 81 | template <typename T, size_t MinSize = sizeof(T)> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | inline static const T* ConvertTo(const android::ResChunk_header* chunk) { |
Adam Lesinski | 136fd07 | 2017-03-03 13:50:21 -0800 | [diff] [blame] | 83 | if (util::DeviceToHost16(chunk->headerSize) < MinSize) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | return nullptr; |
| 85 | } |
| 86 | return reinterpret_cast<const T*>(chunk); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | inline static const uint8_t* GetChunkData( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | const android::ResChunk_header* chunk) { |
| 91 | return reinterpret_cast<const uint8_t*>(chunk) + |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | util::DeviceToHost16(chunk->headerSize); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | inline static uint32_t GetChunkDataLen(const android::ResChunk_header* chunk) { |
| 96 | return util::DeviceToHost32(chunk->size) - |
| 97 | util::DeviceToHost16(chunk->headerSize); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 100 | // |
| 101 | // Implementation |
| 102 | // |
| 103 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | inline bool ResChunkPullParser::IsGoodEvent(ResChunkPullParser::Event event) { |
| 105 | return event != Event::kEndDocument && event != Event::kBadDocument; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | inline ResChunkPullParser::ResChunkPullParser(const void* data, size_t len) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | : event_(Event::kStartDocument), |
| 110 | data_(reinterpret_cast<const android::ResChunk_header*>(data)), |
| 111 | len_(len), |
| 112 | current_chunk_(nullptr) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 113 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | inline ResChunkPullParser::Event ResChunkPullParser::event() const { |
| 115 | return event_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | inline const std::string& ResChunkPullParser::error() const { return error_; } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 119 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | inline const android::ResChunk_header* ResChunkPullParser::chunk() const { |
| 121 | return current_chunk_; |
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 |