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