| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class ResChunkPullParser { |
| 37 |
public: |
| 38 |
enum class Event { |
| 39 |
kStartDocument, |
| 40 |
kEndDocument, |
| 41 |
kBadDocument, |
| 42 |
|
| 43 |
kChunk, |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
static bool IsGoodEvent(Event event); |
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 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)> |
| 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 |
|
| 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 |
|
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
inline bool ResChunkPullParser::IsGoodEvent(ResChunkPullParser::Event event) { |
| 91 |
return event != Event::kEndDocument && event != Event::kBadDocument; |
| 92 |
} |
| 93 |
|
| 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 |
|
| 101 |
inline ResChunkPullParser::Event ResChunkPullParser::event() const { |
| 102 |
return event_; |
| 103 |
} |
| 104 |
|
| 105 |
inline const std::string& ResChunkPullParser::error() const { |
| 106 |
return error_; |
| 107 |
} |
| 108 |
|
| 109 |
inline const android::ResChunk_header* ResChunkPullParser::chunk() const { |
| 110 |
return current_chunk_; |
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
#endif |