blob: 34eed63471f878935f92763a6f26da5c38a6bb46 [file] [log] [blame]
Adam Lesinskia40e9722015-11-24 19:11:46 -08001/*
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_IO_DATA_H
18#define AAPT_IO_DATA_H
19
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070020#include <android-base/macros.h>
Adam Lesinskia40e9722015-11-24 19:11:46 -080021#include <memory>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070022#include <utils/FileMap.h>
Adam Lesinskia40e9722015-11-24 19:11:46 -080023
24namespace aapt {
25namespace io {
26
27/**
28 * Interface for a block of contiguous memory. An instance of this interface owns the data.
29 */
30class IData {
31public:
32 virtual ~IData() = default;
33
34 virtual const void* data() const = 0;
35 virtual size_t size() const = 0;
36};
37
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070038class DataSegment : public IData {
39public:
40 explicit DataSegment(std::unique_ptr<IData> data, size_t offset, size_t len) :
41 mData(std::move(data)), mOffset(offset), mLen(len) {
42 }
43
44 const void* data() const override {
45 return static_cast<const uint8_t*>(mData->data()) + mOffset;
46 }
47
48 size_t size() const override {
49 return mLen;
50 }
51
52private:
53 DISALLOW_COPY_AND_ASSIGN(DataSegment);
54
55 std::unique_ptr<IData> mData;
56 size_t mOffset;
57 size_t mLen;
58};
59
Adam Lesinskia40e9722015-11-24 19:11:46 -080060/**
61 * Implementation of IData that exposes a memory mapped file. The mmapped file is owned by this
62 * object.
63 */
64class MmappedData : public IData {
65public:
66 explicit MmappedData(android::FileMap&& map) : mMap(std::forward<android::FileMap>(map)) {
67 }
68
69 const void* data() const override {
70 return mMap.getDataPtr();
71 }
72
73 size_t size() const override {
74 return mMap.getDataLength();
75 }
76
77private:
78 android::FileMap mMap;
79};
80
81/**
82 * Implementation of IData that exposes a block of memory that was malloc'ed (new'ed). The
83 * memory is owned by this object.
84 */
85class MallocData : public IData {
86public:
87 MallocData(std::unique_ptr<const uint8_t[]> data, size_t size) :
88 mData(std::move(data)), mSize(size) {
89 }
90
91 const void* data() const override {
92 return mData.get();
93 }
94
95 size_t size() const override {
96 return mSize;
97 }
98
99private:
100 std::unique_ptr<const uint8_t[]> mData;
101 size_t mSize;
102};
103
Adam Lesinski52364f72016-01-11 13:10:24 -0800104/**
105 * When mmap fails because the file has length 0, we use the EmptyData to simulate data of length 0.
106 */
107class EmptyData : public IData {
108public:
109 const void* data() const override {
110 static const uint8_t d = 0;
111 return &d;
112 }
113
114 size_t size() const override {
115 return 0u;
116 }
117};
118
Adam Lesinskia40e9722015-11-24 19:11:46 -0800119} // namespace io
120} // namespace aapt
121
122#endif /* AAPT_IO_DATA_H */