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 | |
| 17 | #define LOG_TAG "RedactionInfo" |
| 18 | |
| 19 | #include "include/libfuse_jni/RedactionInfo.h" |
| 20 | |
| 21 | #include <android-base/logging.h> |
| 22 | |
| 23 | using std::unique_ptr; |
| 24 | using std::vector; |
| 25 | |
| 26 | namespace mediaprovider { |
| 27 | namespace fuse { |
| 28 | |
| 29 | /** |
| 30 | * Merges any overlapping ranges into 1 range. |
| 31 | * |
| 32 | * Given ranges should be sorted, and they remain sorted. |
| 33 | */ |
| 34 | static void mergeOverlappingRedactionRanges(vector<RedactionRange>& ranges) { |
| 35 | int newRangesSize = ranges.size(); |
| 36 | for (int i = 0; i < ranges.size() - 1; ++i) { |
| 37 | if (ranges[i].second >= ranges[i + 1].first) { |
| 38 | ranges[i + 1].first = ranges[i].first; |
| 39 | ranges[i + 1].second = std::max(ranges[i].second, ranges[i + 1].second); |
| 40 | // Invalidate the redundant range |
| 41 | ranges[i].first = LONG_MAX; |
| 42 | ranges[i].second = LONG_MAX; |
| 43 | newRangesSize--; |
| 44 | } |
| 45 | } |
| 46 | if (newRangesSize < ranges.size()) { |
| 47 | // Move invalid ranges to end of array |
| 48 | std::sort(ranges.begin(), ranges.end()); |
| 49 | ranges.resize(newRangesSize); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Determine whether the read request overlaps with the redaction ranges |
| 55 | * defined by the given RedactionInfo. |
| 56 | * |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame^] | 57 | * This function assumes redaction_ranges_ within RedactionInfo is sorted. |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 58 | */ |
| 59 | bool RedactionInfo::hasOverlapWithReadRequest(size_t size, off64_t off) { |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame^] | 60 | if (!isRedactionNeeded() || off > redaction_ranges_.back().second || |
| 61 | off + size < redaction_ranges_.front().first) { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Sets the redaction ranges in RedactionInfo, sort the ranges and merge |
| 69 | * overlapping ranges. |
| 70 | */ |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame^] | 71 | void RedactionInfo::processRedactionRanges(int redaction_ranges_num, |
| 72 | const off64_t* redaction_ranges) { |
| 73 | redaction_ranges_.resize(redaction_ranges_num); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 74 | for (int i = 0; i < redaction_ranges_num; ++i) { |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame^] | 75 | redaction_ranges_[i].first = static_cast<off64_t>(redaction_ranges[2 * i]); |
| 76 | redaction_ranges_[i].second = static_cast<off64_t>(redaction_ranges[2 * i + 1]); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 77 | } |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame^] | 78 | std::sort(redaction_ranges_.begin(), redaction_ranges_.end()); |
| 79 | mergeOverlappingRedactionRanges(redaction_ranges_); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | int RedactionInfo::size() { |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame^] | 83 | return redaction_ranges_.size(); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | bool RedactionInfo::isRedactionNeeded() { |
| 87 | return size() > 0; |
| 88 | } |
| 89 | |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame^] | 90 | RedactionInfo::RedactionInfo(int redaction_ranges_num, const off64_t* redaction_ranges) { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 91 | if (redaction_ranges == 0) return; |
| 92 | processRedactionRanges(redaction_ranges_num, redaction_ranges); |
| 93 | } |
| 94 | |
| 95 | unique_ptr<vector<RedactionRange>> RedactionInfo::getOverlappingRedactionRanges(size_t size, |
| 96 | off64_t off) { |
| 97 | LOG(DEBUG) << "Computing redaction ranges for request: sz = " << size << " off = " << off; |
| 98 | if (hasOverlapWithReadRequest(size, off)) { |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame^] | 99 | auto first_redaction = redaction_ranges_.end(); |
| 100 | auto last_redaction = redaction_ranges_.end(); |
| 101 | for (auto iter = redaction_ranges_.begin(); iter != redaction_ranges_.end(); ++iter) { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 102 | RedactionRange& rr = *iter; |
| 103 | // Look for the first range that overlaps with the read request |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame^] | 104 | if (first_redaction == redaction_ranges_.end() && off <= rr.second && |
| 105 | off + size >= rr.first) { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 106 | first_redaction = iter; |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame^] | 107 | } else if (first_redaction != redaction_ranges_.end() && off + size < rr.first) { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 108 | // Once we're in the read request range, we start checking if |
| 109 | // we're out of it so we can return the result to the caller |
| 110 | break; |
| 111 | } |
| 112 | last_redaction = iter; |
| 113 | } |
shafik | cdb6b2b | 2019-09-30 12:49:26 +0100 | [diff] [blame^] | 114 | if (first_redaction != redaction_ranges_.end()) { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 115 | LOG(DEBUG) << "Returning " << (int)(last_redaction - first_redaction + 1) |
| 116 | << " redaction ranges!"; |
| 117 | return std::make_unique<vector<RedactionRange>>(first_redaction, last_redaction + 1); |
| 118 | } |
| 119 | } |
| 120 | LOG(DEBUG) << "No relevant redaction ranges!"; |
| 121 | return std::make_unique<vector<RedactionRange>>(); |
| 122 | } |
| 123 | } // namespace fuse |
| 124 | } // namespace mediaprovider |