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_FORMAT_BINARY_RESCHUNKPULLPARSER_H
18 #define AAPT_FORMAT_BINARY_RESCHUNKPULLPARSER_H
19 
20 #include <string>
21 
22 #include "android-base/macros.h"
23 #include "androidfw/ResourceTypes.h"
24 
25 #include "util/Util.h"
26 
27 namespace aapt {
28 
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.
36 class ResChunkPullParser {
37  public:
38   enum class Event {
39     kStartDocument,
40     kEndDocument,
41     kBadDocument,
42 
43     kChunk,
44   };
45 
46   // Returns false if the event is EndDocument or BadDocument.
47   static bool IsGoodEvent(Event event);
48 
49   // Create a ResChunkPullParser to read android::ResChunk_headers from the memory pointed to by
50   // data, of len bytes.
51   ResChunkPullParser(const void* data, size_t len);
52 
53   Event event() const;
54   const std::string& error() const;
55   const android::ResChunk_header* chunk() const;
56 
57   // Move to the next android::ResChunk_header.
58   Event Next();
59 
60  private:
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_;
68 };
69 
70 template <typename T, size_t MinSize = sizeof(T)>
ConvertTo(const android::ResChunk_header * chunk)71 inline static const T* ConvertTo(const android::ResChunk_header* chunk) {
72   if (util::DeviceToHost16(chunk->headerSize) < MinSize) {
73     return nullptr;
74   }
75   return reinterpret_cast<const T*>(chunk);
76 }
77 
GetChunkData(const android::ResChunk_header * chunk)78 inline static const uint8_t* GetChunkData(const android::ResChunk_header* chunk) {
79   return reinterpret_cast<const uint8_t*>(chunk) + util::DeviceToHost16(chunk->headerSize);
80 }
81 
GetChunkDataLen(const android::ResChunk_header * chunk)82 inline static uint32_t GetChunkDataLen(const android::ResChunk_header* chunk) {
83   return util::DeviceToHost32(chunk->size) - util::DeviceToHost16(chunk->headerSize);
84 }
85 
86 //
87 // Implementation
88 //
89 
IsGoodEvent(ResChunkPullParser::Event event)90 inline bool ResChunkPullParser::IsGoodEvent(ResChunkPullParser::Event event) {
91   return event != Event::kEndDocument && event != Event::kBadDocument;
92 }
93 
ResChunkPullParser(const void * data,size_t len)94 inline ResChunkPullParser::ResChunkPullParser(const void* data, size_t len)
95     : event_(Event::kStartDocument),
96       data_(reinterpret_cast<const android::ResChunk_header*>(data)),
97       len_(len),
98       current_chunk_(nullptr) {
99 }
100 
event()101 inline ResChunkPullParser::Event ResChunkPullParser::event() const {
102   return event_;
103 }
104 
error()105 inline const std::string& ResChunkPullParser::error() const {
106   return error_;
107 }
108 
chunk()109 inline const android::ResChunk_header* ResChunkPullParser::chunk() const {
110   return current_chunk_;
111 }
112 
113 }  // namespace aapt
114 
115 #endif  // AAPT_FORMAT_BINARY_RESCHUNKPULLPARSER_H
116