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 #include "androidfw/Util.h"
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 (android::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) + android::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 android::util::DeviceToHost32(chunk->size) -
84          android::util::DeviceToHost16(chunk->headerSize);
85 }
86 
87 //
88 // Implementation
89 //
90 
IsGoodEvent(ResChunkPullParser::Event event)91 inline bool ResChunkPullParser::IsGoodEvent(ResChunkPullParser::Event event) {
92   return event != Event::kEndDocument && event != Event::kBadDocument;
93 }
94 
ResChunkPullParser(const void * data,size_t len)95 inline ResChunkPullParser::ResChunkPullParser(const void* data, size_t len)
96     : event_(Event::kStartDocument),
97       data_(reinterpret_cast<const android::ResChunk_header*>(data)),
98       len_(len),
99       current_chunk_(nullptr) {
100 }
101 
event()102 inline ResChunkPullParser::Event ResChunkPullParser::event() const {
103   return event_;
104 }
105 
error()106 inline const std::string& ResChunkPullParser::error() const {
107   return error_;
108 }
109 
chunk()110 inline const android::ResChunk_header* ResChunkPullParser::chunk() const {
111   return current_chunk_;
112 }
113 
114 }  // namespace aapt
115 
116 #endif  // AAPT_FORMAT_BINARY_RESCHUNKPULLPARSER_H
117