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
20 #include "util/Util.h"
21
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 {
40 public:
41 enum class Event {
42 StartDocument,
43 EndDocument,
44 BadDocument,
45
46 Chunk,
47 };
48
49 /**
50 * Returns false if the event is EndDocument or BadDocument.
51 */
52 static bool isGoodEvent(Event event);
53
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);
59
60 ResChunkPullParser(const ResChunkPullParser&) = delete;
61
62 Event getEvent() const;
63 const std::string& getLastError() const;
64 const android::ResChunk_header* getChunk() const;
65
66 /**
67 * Move to the next android::ResChunk_header.
68 */
69 Event next();
70
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;
77 };
78
79 template <typename T>
convertTo(const android::ResChunk_header * chunk)80 inline static const T* convertTo(const android::ResChunk_header* chunk) {
81 if (util::deviceToHost16(chunk->headerSize) < sizeof(T)) {
82 return nullptr;
83 }
84 return reinterpret_cast<const T*>(chunk);
85 }
86
getChunkData(const android::ResChunk_header * chunk)87 inline static const uint8_t* getChunkData(const android::ResChunk_header* chunk) {
88 return reinterpret_cast<const uint8_t*>(chunk) + util::deviceToHost16(chunk->headerSize);
89 }
90
getChunkDataLen(const android::ResChunk_header * chunk)91 inline static uint32_t getChunkDataLen(const android::ResChunk_header* chunk) {
92 return util::deviceToHost32(chunk->size) - util::deviceToHost16(chunk->headerSize);
93 }
94
95 //
96 // Implementation
97 //
98
isGoodEvent(ResChunkPullParser::Event event)99 inline bool ResChunkPullParser::isGoodEvent(ResChunkPullParser::Event event) {
100 return event != Event::EndDocument && event != Event::BadDocument;
101 }
102
ResChunkPullParser(const void * data,size_t len)103 inline ResChunkPullParser::ResChunkPullParser(const void* data, size_t len) :
104 mEvent(Event::StartDocument),
105 mData(reinterpret_cast<const android::ResChunk_header*>(data)),
106 mLen(len),
107 mCurrentChunk(nullptr) {
108 }
109
getEvent()110 inline ResChunkPullParser::Event ResChunkPullParser::getEvent() const {
111 return mEvent;
112 }
113
getLastError()114 inline const std::string& ResChunkPullParser::getLastError() const {
115 return mLastError;
116 }
117
getChunk()118 inline const android::ResChunk_header* ResChunkPullParser::getChunk() const {
119 return mCurrentChunk;
120 }
121
122 } // namespace aapt
123
124 #endif // AAPT_RES_CHUNK_PULL_PARSER_H
125