blob: 58277531034c8fdfa651d57cb8ebe7027af15a3c [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 Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <string>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "android-base/macros.h"
23#include "androidfw/ResourceTypes.h"
24
25#include "util/Util.h"
26
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027namespace 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 */
41class ResChunkPullParser {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 public:
43 enum class Event {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 kStartDocument,
45 kEndDocument,
46 kBadDocument,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 kChunk,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 /**
52 * Returns false if the event is EndDocument or BadDocument.
53 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 static bool IsGoodEvent(Event event);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 /**
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 Lesinski6f6ceb72014-11-14 14:48:12 -080061
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 Event event() const;
63 const std::string& error() const;
64 const android::ResChunk_header* chunk() 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 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 Event Next();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 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 Lesinski6f6ceb72014-11-14 14:48:12 -080079};
80
Adam Lesinski136fd072017-03-03 13:50:21 -080081template <typename T, size_t MinSize = sizeof(T)>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082inline static const T* ConvertTo(const android::ResChunk_header* chunk) {
Adam Lesinski136fd072017-03-03 13:50:21 -080083 if (util::DeviceToHost16(chunk->headerSize) < MinSize) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return nullptr;
85 }
86 return reinterpret_cast<const T*>(chunk);
Adam Lesinski769de982015-04-10 19:43:55 -070087}
88
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089inline static const uint8_t* GetChunkData(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 const android::ResChunk_header* chunk) {
91 return reinterpret_cast<const uint8_t*>(chunk) +
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 util::DeviceToHost16(chunk->headerSize);
Adam Lesinski769de982015-04-10 19:43:55 -070093}
94
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095inline static uint32_t GetChunkDataLen(const android::ResChunk_header* chunk) {
96 return util::DeviceToHost32(chunk->size) -
97 util::DeviceToHost16(chunk->headerSize);
Adam Lesinski769de982015-04-10 19:43:55 -070098}
99
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100//
101// Implementation
102//
103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104inline bool ResChunkPullParser::IsGoodEvent(ResChunkPullParser::Event event) {
105 return event != Event::kEndDocument && event != Event::kBadDocument;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106}
107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108inline ResChunkPullParser::ResChunkPullParser(const void* data, size_t len)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 : event_(Event::kStartDocument),
110 data_(reinterpret_cast<const android::ResChunk_header*>(data)),
111 len_(len),
112 current_chunk_(nullptr) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114inline ResChunkPullParser::Event ResChunkPullParser::event() const {
115 return event_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116}
117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118inline const std::string& ResChunkPullParser::error() const { return error_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120inline const android::ResChunk_header* ResChunkPullParser::chunk() const {
121 return current_chunk_;
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