summaryrefslogtreecommitdiff
path: root/libs/androidfw/Chunk.h
blob: e87b94087450d63f7e15e6ca6f6ca94ce2281253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CHUNK_H_
#define CHUNK_H_

#include "android-base/logging.h"
#include "android-base/macros.h"
#include "utils/ByteOrder.h"

#ifdef _WIN32
#ifdef ERROR
#undef ERROR
#endif
#endif

#include "androidfw/ResourceTypes.h"

namespace android {

// Helpful wrapper around a ResChunk_header that provides getter methods
// that handle endianness conversions and provide access to the data portion
// of the chunk.
class Chunk {
 public:
  explicit Chunk(const ResChunk_header* chunk) : device_chunk_(chunk) {}

  // Returns the type of the chunk. Caller need not worry about endianness.
  inline int type() const { return dtohs(device_chunk_->type); }

  // Returns the size of the entire chunk. This can be useful for skipping
  // over the entire chunk. Caller need not worry about endianness.
  inline size_t size() const { return dtohl(device_chunk_->size); }

  // Returns the size of the header. Caller need not worry about endianness.
  inline size_t header_size() const { return dtohs(device_chunk_->headerSize); }

  template <typename T>
  inline const T* header() const {
    if (header_size() >= sizeof(T)) {
      return reinterpret_cast<const T*>(device_chunk_);
    }
    return nullptr;
  }

  inline const void* data_ptr() const {
    return reinterpret_cast<const uint8_t*>(device_chunk_) + header_size();
  }

  inline size_t data_size() const { return size() - header_size(); }

 private:
  const ResChunk_header* device_chunk_;
};

// Provides a Java style iterator over an array of ResChunk_header's.
// Validation is performed while iterating.
// The caller should check if there was an error during chunk validation
// by calling HadError() and GetLastError() to get the reason for failure.
// Example:
//
//   ChunkIterator iter(data_ptr, data_len);
//   while (iter.HasNext()) {
//     const Chunk chunk = iter.Next();
//     ...
//   }
//
//   if (iter.HadError()) {
//     LOG(ERROR) << iter.GetLastError();
//   }
//
class ChunkIterator {
 public:
  ChunkIterator(const void* data, size_t len)
      : next_chunk_(reinterpret_cast<const ResChunk_header*>(data)),
        len_(len),
        last_error_(nullptr) {
    CHECK(next_chunk_ != nullptr) << "data can't be nullptr";
    VerifyNextChunk();
  }

  Chunk Next();
  inline bool HasNext() const { return !HadError() && len_ != 0; };
  inline bool HadError() const { return last_error_ != nullptr; }
  inline std::string GetLastError() const { return last_error_; }

 private:
  DISALLOW_COPY_AND_ASSIGN(ChunkIterator);

  // Returns false if there was an error.
  bool VerifyNextChunk();

  const ResChunk_header* next_chunk_;
  size_t len_;
  const char* last_error_;
};

}  // namespace android

#endif /* CHUNK_H_ */