Home | History | Annotate | Download | only in unflatten
      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 <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 /**
     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  */
     41 class ResChunkPullParser {
     42  public:
     43   enum class Event {
     44     kStartDocument,
     45     kEndDocument,
     46     kBadDocument,
     47 
     48     kChunk,
     49   };
     50 
     51   /**
     52    * Returns false if the event is EndDocument or BadDocument.
     53    */
     54   static bool IsGoodEvent(Event event);
     55 
     56   /**
     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);
     61 
     62   Event event() const;
     63   const std::string& error() const;
     64   const android::ResChunk_header* chunk() const;
     65 
     66   /**
     67    * Move to the next android::ResChunk_header.
     68    */
     69   Event Next();
     70 
     71  private:
     72   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_;
     79 };
     80 
     81 template <typename T, size_t MinSize = sizeof(T)>
     82 inline static const T* ConvertTo(const android::ResChunk_header* chunk) {
     83   if (util::DeviceToHost16(chunk->headerSize) < MinSize) {
     84     return nullptr;
     85   }
     86   return reinterpret_cast<const T*>(chunk);
     87 }
     88 
     89 inline static const uint8_t* GetChunkData(
     90     const android::ResChunk_header* chunk) {
     91   return reinterpret_cast<const uint8_t*>(chunk) +
     92          util::DeviceToHost16(chunk->headerSize);
     93 }
     94 
     95 inline static uint32_t GetChunkDataLen(const android::ResChunk_header* chunk) {
     96   return util::DeviceToHost32(chunk->size) -
     97          util::DeviceToHost16(chunk->headerSize);
     98 }
     99 
    100 //
    101 // Implementation
    102 //
    103 
    104 inline bool ResChunkPullParser::IsGoodEvent(ResChunkPullParser::Event event) {
    105   return event != Event::kEndDocument && event != Event::kBadDocument;
    106 }
    107 
    108 inline ResChunkPullParser::ResChunkPullParser(const void* data, size_t len)
    109     : event_(Event::kStartDocument),
    110       data_(reinterpret_cast<const android::ResChunk_header*>(data)),
    111       len_(len),
    112       current_chunk_(nullptr) {}
    113 
    114 inline ResChunkPullParser::Event ResChunkPullParser::event() const {
    115   return event_;
    116 }
    117 
    118 inline const std::string& ResChunkPullParser::error() const { return error_; }
    119 
    120 inline const android::ResChunk_header* ResChunkPullParser::chunk() const {
    121   return current_chunk_;
    122 }
    123 
    124 }  // namespace aapt
    125 
    126 #endif  // AAPT_RES_CHUNK_PULL_PARSER_H
    127