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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* Copyright (C) 2015 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 AAPT_IO_DATA_H
#define AAPT_IO_DATA_H
#include <memory>
#include "android-base/macros.h"
#include "utils/FileMap.h"
#include "io/Io.h"
namespace aapt {
namespace io {
// Interface for a block of contiguous memory. An instance of this interface owns the data.
class IData : public InputStream {
public:
virtual ~IData() = default;
virtual const void* data() const = 0;
virtual size_t size() const = 0;
};
class DataSegment : public IData {
public:
explicit DataSegment(std::unique_ptr<IData> data, size_t offset, size_t len)
: data_(std::move(data)), offset_(offset), len_(len), next_read_(offset) {}
virtual ~DataSegment() = default;
const void* data() const override {
return static_cast<const uint8_t*>(data_->data()) + offset_;
}
size_t size() const override { return len_; }
bool Next(const void** data, size_t* size) override {
if (next_read_ == offset_ + len_) {
return false;
}
*data = static_cast<const uint8_t*>(data_->data()) + next_read_;
*size = len_ - (next_read_ - offset_);
next_read_ = offset_ + len_;
return true;
}
void BackUp(size_t count) override {
if (count > next_read_ - offset_) {
next_read_ = offset_;
} else {
next_read_ -= count;
}
}
bool CanRewind() const override { return true; }
bool Rewind() override {
next_read_ = offset_;
return true;
}
size_t ByteCount() const override { return next_read_ - offset_; }
bool HadError() const override { return false; }
private:
DISALLOW_COPY_AND_ASSIGN(DataSegment);
std::unique_ptr<IData> data_;
size_t offset_;
size_t len_;
size_t next_read_;
};
// Implementation of IData that exposes a memory mapped file.
// The mmapped file is owned by this object.
class MmappedData : public IData {
public:
explicit MmappedData(android::FileMap&& map) : map_(std::forward<android::FileMap>(map)) {}
virtual ~MmappedData() = default;
const void* data() const override { return map_.getDataPtr(); }
size_t size() const override { return map_.getDataLength(); }
bool Next(const void** data, size_t* size) override {
if (next_read_ == map_.getDataLength()) {
return false;
}
*data = reinterpret_cast<const uint8_t*>(map_.getDataPtr()) + next_read_;
*size = map_.getDataLength() - next_read_;
next_read_ = map_.getDataLength();
return true;
}
void BackUp(size_t count) override {
if (count > next_read_) {
next_read_ = 0;
} else {
next_read_ -= count;
}
}
bool CanRewind() const override { return true; }
bool Rewind() override {
next_read_ = 0;
return true;
}
size_t ByteCount() const override { return next_read_; }
bool HadError() const override { return false; }
private:
DISALLOW_COPY_AND_ASSIGN(MmappedData);
android::FileMap map_;
size_t next_read_ = 0;
};
// Implementation of IData that exposes a block of memory that was malloc'ed (new'ed).
// The memory is owned by this object.
class MallocData : public IData {
public:
MallocData(std::unique_ptr<const uint8_t[]> data, size_t size)
: data_(std::move(data)), size_(size) {}
virtual ~MallocData() = default;
const void* data() const override { return data_.get(); }
size_t size() const override { return size_; }
bool Next(const void** data, size_t* size) override {
if (next_read_ == size_) {
return false;
}
*data = data_.get() + next_read_;
*size = size_ - next_read_;
next_read_ = size_;
return true;
}
void BackUp(size_t count) override {
if (count > next_read_) {
next_read_ = 0;
} else {
next_read_ -= count;
}
}
bool CanRewind() const override { return true; }
bool Rewind() override {
next_read_ = 0;
return true;
}
size_t ByteCount() const override { return next_read_; }
bool HadError() const override { return false; }
private:
DISALLOW_COPY_AND_ASSIGN(MallocData);
std::unique_ptr<const uint8_t[]> data_;
size_t size_;
size_t next_read_ = 0;
};
// When mmap fails because the file has length 0, we use the EmptyData to simulate data of length 0.
class EmptyData : public IData {
public:
virtual ~EmptyData() = default;
const void* data() const override {
static const uint8_t d = 0;
return &d;
}
size_t size() const override { return 0u; }
bool Next(const void** /*data*/, size_t* /*size*/) override { return false; }
void BackUp(size_t /*count*/) override {}
bool CanRewind() const override { return true; }
bool Rewind() override { return true; }
size_t ByteCount() const override { return 0u; }
bool HadError() const override { return false; }
};
} // namespace io
} // namespace aapt
#endif /* AAPT_IO_DATA_H */
|