shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 specic language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 17 | #include "include/libfuse_jni/RedactionInfo.h" |
| 18 | |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 21 | using std::unique_ptr; |
| 22 | using std::vector; |
| 23 | |
| 24 | namespace mediaprovider { |
| 25 | namespace fuse { |
| 26 | |
| 27 | /** |
| 28 | * Merges any overlapping ranges into 1 range. |
| 29 | * |
| 30 | * Given ranges should be sorted, and they remain sorted. |
| 31 | */ |
| 32 | static void mergeOverlappingRedactionRanges(vector<RedactionRange>& ranges) { |
Sahana Rao | 8a1db67 | 2020-10-25 00:00:11 +0100 | [diff] [blame] | 33 | if (ranges.size() == 0) return; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 34 | int newRangesSize = ranges.size(); |
| 35 | for (int i = 0; i < ranges.size() - 1; ++i) { |
| 36 | if (ranges[i].second >= ranges[i + 1].first) { |
| 37 | ranges[i + 1].first = ranges[i].first; |
| 38 | ranges[i + 1].second = std::max(ranges[i].second, ranges[i + 1].second); |
| 39 | // Invalidate the redundant range |
| 40 | ranges[i].first = LONG_MAX; |
| 41 | ranges[i].second = LONG_MAX; |
| 42 | newRangesSize--; |
| 43 | } |
| 44 | } |
| 45 | if (newRangesSize < ranges.size()) { |
| 46 | // Move invalid ranges to end of array |
| 47 | std::sort(ranges.begin(), ranges.end()); |
| 48 | ranges.resize(newRangesSize); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
Sahana Rao | 8a1db67 | 2020-10-25 00:00:11 +0100 | [diff] [blame] | 53 | * Removes any range with zero size. |
| 54 | * |
| 55 | * If ranges are modified, it will be guaranteed to be sorted. |
| 56 | */ |
| 57 | static void removeZeroSizeRedactionRanges(vector<RedactionRange>& ranges) { |
| 58 | int newRangesSize = ranges.size(); |
| 59 | for (int i = 0; i < ranges.size(); ++i) { |
| 60 | if (ranges[i].first == ranges[i].second) { |
| 61 | // This redaction range is of length zero, hence we don't have anything |
| 62 | // to redact in this range, so remove it from the redaction_ranges_. |
| 63 | ranges[i].first = LONG_MAX; |
| 64 | ranges[i].second = LONG_MAX; |
| 65 | newRangesSize--; |
| 66 | } |
| 67 | } |
| 68 | if (newRangesSize < ranges.size()) { |
| 69 | // Move invalid ranges to end of array |
| 70 | std::sort(ranges.begin(), ranges.end()); |
| 71 | ranges.resize(newRangesSize); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 76 | * Determine whether the read request overlaps with the redaction ranges |
| 77 | * defined by the given RedactionInfo. |
| 78 | * |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 79 | * This function assumes redaction_ranges_ within RedactionInfo is sorted. |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 80 | */ |
Narayan Kamath | bd22bb0 | 2020-01-08 16:02:50 +0000 | [diff] [blame] | 81 | bool RedactionInfo::hasOverlapWithReadRequest(size_t size, off64_t off) const { |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 82 | if (!isRedactionNeeded() || off >= redaction_ranges_.back().second || |
| 83 | off + size <= redaction_ranges_.front().first) { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sets the redaction ranges in RedactionInfo, sort the ranges and merge |
| 91 | * overlapping ranges. |
| 92 | */ |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 93 | void RedactionInfo::processRedactionRanges(int redaction_ranges_num, |
| 94 | const off64_t* redaction_ranges) { |
| 95 | redaction_ranges_.resize(redaction_ranges_num); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 96 | for (int i = 0; i < redaction_ranges_num; ++i) { |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 97 | redaction_ranges_[i].first = static_cast<off64_t>(redaction_ranges[2 * i]); |
| 98 | redaction_ranges_[i].second = static_cast<off64_t>(redaction_ranges[2 * i + 1]); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 99 | } |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 100 | std::sort(redaction_ranges_.begin(), redaction_ranges_.end()); |
Sahana Rao | 8a1db67 | 2020-10-25 00:00:11 +0100 | [diff] [blame] | 101 | removeZeroSizeRedactionRanges(redaction_ranges_); |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 102 | mergeOverlappingRedactionRanges(redaction_ranges_); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Narayan Kamath | bd22bb0 | 2020-01-08 16:02:50 +0000 | [diff] [blame] | 105 | int RedactionInfo::size() const { |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 106 | return redaction_ranges_.size(); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Narayan Kamath | bd22bb0 | 2020-01-08 16:02:50 +0000 | [diff] [blame] | 109 | bool RedactionInfo::isRedactionNeeded() const { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 110 | return size() > 0; |
| 111 | } |
| 112 | |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 113 | RedactionInfo::RedactionInfo(int redaction_ranges_num, const off64_t* redaction_ranges) { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 114 | if (redaction_ranges == 0) return; |
| 115 | processRedactionRanges(redaction_ranges_num, redaction_ranges); |
| 116 | } |
| 117 | |
| 118 | unique_ptr<vector<RedactionRange>> RedactionInfo::getOverlappingRedactionRanges(size_t size, |
Narayan Kamath | bd22bb0 | 2020-01-08 16:02:50 +0000 | [diff] [blame] | 119 | off64_t off) const { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 120 | if (hasOverlapWithReadRequest(size, off)) { |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 121 | const off64_t start = off; |
| 122 | const off64_t end = static_cast<off64_t>(off + size); |
| 123 | |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 124 | auto first_redaction = redaction_ranges_.end(); |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 125 | auto last_redaction = redaction_ranges_.begin(); |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame] | 126 | for (auto iter = redaction_ranges_.begin(); iter != redaction_ranges_.end(); ++iter) { |
Sahana Rao | 8a1db67 | 2020-10-25 00:00:11 +0100 | [diff] [blame] | 127 | if (iter->second > start && iter->first < end) { |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 128 | if (iter < first_redaction) first_redaction = iter; |
| 129 | if (iter > last_redaction) last_redaction = iter; |
| 130 | } |
| 131 | |
| 132 | if (iter->first >= end) { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 133 | break; |
| 134 | } |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 135 | } |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 136 | |
Hyoungho Choi | 446de54 | 2020-10-22 23:07:57 +0900 | [diff] [blame] | 137 | if (first_redaction != redaction_ranges_.end()) { |
| 138 | CHECK(first_redaction <= last_redaction); |
| 139 | return std::make_unique<vector<RedactionRange>>(first_redaction, last_redaction + 1); |
| 140 | } |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 141 | } |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 142 | return std::make_unique<vector<RedactionRange>>(); |
| 143 | } |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 144 | |
| 145 | void RedactionInfo::getReadRanges(off64_t off, size_t size, std::vector<ReadRange>* out) const { |
Sahana Rao | 8a1db67 | 2020-10-25 00:00:11 +0100 | [diff] [blame] | 146 | const auto rr = getOverlappingRedactionRanges(size, off); |
| 147 | const size_t num_ranges = rr->size(); |
| 148 | if (num_ranges == 0) { |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
Sahana Rao | 8a1db67 | 2020-10-25 00:00:11 +0100 | [diff] [blame] | 152 | const off64_t read_start = off; |
| 153 | const off64_t read_end = static_cast<off64_t>(read_start + size); |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 154 | |
Sahana Rao | 8a1db67 | 2020-10-25 00:00:11 +0100 | [diff] [blame] | 155 | // The algorithm for computing redaction ranges is very simple. |
| 156 | // Given a set of overlapping redaction ranges [s1, e1) [s2, e2) .. [sN, eN) for a read |
| 157 | // [s, e) |
| 158 | // |
| 159 | // We can construct a series of indices that we know will be the starts of every read range |
| 160 | // that we intend to return. Then, it's relatively simple to compute the lengths of the ranges. |
| 161 | // Also note that the read ranges we return always alternate in whether they're redacting or |
| 162 | // not. i.e, we will never return two consecutive redacting ranges or non redacting ranges. |
| 163 | std::vector<off64_t> sorted_indices; |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 164 | |
Sahana Rao | 8a1db67 | 2020-10-25 00:00:11 +0100 | [diff] [blame] | 165 | // Compute the list of indices -- this list will always contain { e1, s2, e2... sN } |
| 166 | // In addition, it may contain s or both (s and s1), depending on the start index. |
| 167 | // In addition, it may contain e or both (e and eN), depending on the end index. |
| 168 | // |
| 169 | // For a concrete example, consider ranges [10, 20) and [30, 40) |
| 170 | // For a read [0, 60) : sorted_indices will be { 0, 10, 20, 30, 40, 60 } is_first = false |
| 171 | // For a read [15, 60) : sorted_indices will be { 15, 20, 30, 40, 60 } is_first = true |
| 172 | // For a read [0, 35) : sorted_indices will be { 0, 10, 20, 30, 35 } is_first = false |
| 173 | // For a read [15, 35) : sorted_indices will be { 15, 20, 30, 35 } is_first = true |
| 174 | for (int i = 0; i < num_ranges; ++i) { |
| 175 | sorted_indices.push_back(rr->at(i).first); |
| 176 | sorted_indices.push_back(rr->at(i).second); |
| 177 | } |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 178 | |
Sahana Rao | 8a1db67 | 2020-10-25 00:00:11 +0100 | [diff] [blame] | 179 | // Find the right position for read_start in sorted_indices |
| 180 | // Either insert at the beginning or replace s1 with read_start |
| 181 | bool is_first_range_redaction = true; |
| 182 | if (read_start < rr->at(0).first) { |
| 183 | is_first_range_redaction = false; |
| 184 | sorted_indices.insert(sorted_indices.begin(), read_start); |
| 185 | } else { |
| 186 | sorted_indices.front() = read_start; |
| 187 | } |
| 188 | |
| 189 | // Find the right position for read_end in sorted_indices |
| 190 | // Either insert at the end or replace eN with read_end |
| 191 | if (read_end > rr->at(num_ranges - 1).second) { |
| 192 | sorted_indices.push_back(read_end); |
| 193 | } else { |
| 194 | sorted_indices.back() = read_end; |
| 195 | } |
| 196 | |
| 197 | bool is_redaction = is_first_range_redaction; |
| 198 | for (int i = 0; i < (sorted_indices.size() - 1); ++i) { |
| 199 | const off64_t read_size = sorted_indices[i + 1] - sorted_indices[i]; |
| 200 | CHECK(read_size > 0); |
| 201 | out->push_back(ReadRange(sorted_indices[i], read_size, is_redaction)); |
| 202 | is_redaction = !is_redaction; |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 206 | } // namespace fuse |
Narayan Kamath | bd22bb0 | 2020-01-08 16:02:50 +0000 | [diff] [blame] | 207 | } // namespace mediaprovider |