• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "include/libfuse_jni/RedactionInfo.h"
18 
19 #include <android-base/logging.h>
20 
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  */
mergeOverlappingRedactionRanges(vector<RedactionRange> & ranges)32 static void mergeOverlappingRedactionRanges(vector<RedactionRange>& ranges) {
33     if (ranges.size() == 0) return;
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 /**
53  * Removes any range with zero size.
54  *
55  * If ranges are modified, it will be guaranteed to be sorted.
56  */
removeZeroSizeRedactionRanges(vector<RedactionRange> & ranges)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 /**
76  * Determine whether the read request overlaps with the redaction ranges
77  * defined by the given RedactionInfo.
78  *
79  * This function assumes redaction_ranges_ within RedactionInfo is sorted.
80  */
hasOverlapWithReadRequest(size_t size,off64_t off) const81 bool RedactionInfo::hasOverlapWithReadRequest(size_t size, off64_t off) const {
82     if (!isRedactionNeeded() || off >= redaction_ranges_.back().second ||
83         off + size <= redaction_ranges_.front().first) {
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  */
processRedactionRanges(int redaction_ranges_num,const off64_t * redaction_ranges)93 void RedactionInfo::processRedactionRanges(int redaction_ranges_num,
94                                            const off64_t* redaction_ranges) {
95     redaction_ranges_.resize(redaction_ranges_num);
96     for (int i = 0; i < redaction_ranges_num; ++i) {
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]);
99     }
100     std::sort(redaction_ranges_.begin(), redaction_ranges_.end());
101     removeZeroSizeRedactionRanges(redaction_ranges_);
102     mergeOverlappingRedactionRanges(redaction_ranges_);
103 }
104 
size() const105 int RedactionInfo::size() const {
106     return redaction_ranges_.size();
107 }
108 
isRedactionNeeded() const109 bool RedactionInfo::isRedactionNeeded() const {
110     return size() > 0;
111 }
112 
RedactionInfo(int redaction_ranges_num,const off64_t * redaction_ranges)113 RedactionInfo::RedactionInfo(int redaction_ranges_num, const off64_t* redaction_ranges) {
114     if (redaction_ranges == 0) return;
115     processRedactionRanges(redaction_ranges_num, redaction_ranges);
116 }
117 
getOverlappingRedactionRanges(size_t size,off64_t off) const118 unique_ptr<vector<RedactionRange>> RedactionInfo::getOverlappingRedactionRanges(size_t size,
119                                                                                 off64_t off) const {
120     if (hasOverlapWithReadRequest(size, off)) {
121         const off64_t start = off;
122         const off64_t end = static_cast<off64_t>(off + size);
123 
124         auto first_redaction = redaction_ranges_.end();
125         auto last_redaction = redaction_ranges_.begin();
126         for (auto iter = redaction_ranges_.begin(); iter != redaction_ranges_.end(); ++iter) {
127             if (iter->second > start && iter->first < end) {
128                 if (iter < first_redaction) first_redaction = iter;
129                 if (iter > last_redaction) last_redaction = iter;
130             }
131 
132             if (iter->first >= end) {
133                 break;
134             }
135         }
136 
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         }
141     }
142     return std::make_unique<vector<RedactionRange>>();
143 }
144 
getReadRanges(off64_t off,size_t size,std::vector<ReadRange> * out) const145 void RedactionInfo::getReadRanges(off64_t off, size_t size, std::vector<ReadRange>* out) const {
146     const auto rr = getOverlappingRedactionRanges(size, off);
147     const size_t num_ranges = rr->size();
148     if (num_ranges == 0) {
149         return;
150     }
151 
152     const off64_t read_start = off;
153     const off64_t read_end = static_cast<off64_t>(read_start + size);
154 
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;
164 
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     }
178 
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;
203     }
204 }
205 
206 }  // namespace fuse
207 }  // namespace mediaprovider
208