blob: 24fa63d6e5b4de56bf80a4f86a4c99c30868c3fc [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinski1ab598f2015-08-14 14:26:04 -070020#include "util/Util.h"
21
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include <androidfw/ResourceTypes.h>
23#include <string>
24
25namespace 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 */
39class ResChunkPullParser {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 public:
41 enum class Event {
42 StartDocument,
43 EndDocument,
44 BadDocument,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 Chunk,
47 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 /**
50 * Returns false if the event is EndDocument or BadDocument.
51 */
52 static bool isGoodEvent(Event event);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 /**
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 Lesinski6f6ceb72014-11-14 14:48:12 -080059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 ResChunkPullParser(const ResChunkPullParser&) = delete;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 Event getEvent() const;
63 const std::string& getLastError() const;
64 const android::ResChunk_header* getChunk() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 /**
67 * Move to the next android::ResChunk_header.
68 */
69 Event next();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 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 Lesinski6f6ceb72014-11-14 14:48:12 -080077};
78
Adam Lesinski769de982015-04-10 19:43:55 -070079template <typename T>
80inline static const T* convertTo(const android::ResChunk_header* chunk) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 if (util::deviceToHost16(chunk->headerSize) < sizeof(T)) {
82 return nullptr;
83 }
84 return reinterpret_cast<const T*>(chunk);
Adam Lesinski769de982015-04-10 19:43:55 -070085}
86
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087inline 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 Lesinski769de982015-04-10 19:43:55 -070091}
92
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093inline static uint32_t getChunkDataLen(const android::ResChunk_header* chunk) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 return util::deviceToHost32(chunk->size) -
95 util::deviceToHost16(chunk->headerSize);
Adam Lesinski769de982015-04-10 19:43:55 -070096}
97
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098//
99// Implementation
100//
101
102inline bool ResChunkPullParser::isGoodEvent(ResChunkPullParser::Event event) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 return event != Event::EndDocument && event != Event::BadDocument;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104}
105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106inline 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 Lesinski6f6ceb72014-11-14 14:48:12 -0800111
112inline ResChunkPullParser::Event ResChunkPullParser::getEvent() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 return mEvent;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114}
115
116inline const std::string& ResChunkPullParser::getLastError() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 return mLastError;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118}
119
120inline const android::ResChunk_header* ResChunkPullParser::getChunk() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 return mCurrentChunk;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122}
123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126#endif // AAPT_RES_CHUNK_PULL_PARSER_H