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 #ifndef MEDIA_PROVIDER_FUSE_REDACTIONINFO_H_ 18 #define MEDIA_PROVIDER_FUSE_REDACTIONINFO_H_ 19 20 #include <ostream> 21 #include <vector> 22 23 namespace mediaprovider { 24 namespace fuse { 25 26 /** 27 * Type that represents a single redaction range within a file. Contains 28 * a pair of offsets in the file, [start, end). 29 */ 30 typedef std::pair<off64_t, off64_t> RedactionRange; 31 32 class ReadRange { 33 public: ReadRange(off64_t s,size_t l,bool r)34 ReadRange(off64_t s, size_t l, bool r) : start(s), size(l), is_redaction(r) {} 35 36 const off64_t start; 37 const size_t size; 38 const bool is_redaction; 39 40 bool operator==(const ReadRange& rhs) const { 41 return start == rhs.start && size == rhs.size && is_redaction == rhs.is_redaction; 42 } 43 }; 44 45 class RedactionInfo { 46 public: 47 /** 48 * Constructs a new instance of RedactionInfo based on the given redaction 49 * ranges. 50 * 51 * @param redaction_ranges_num number of redaction ranges, essentially HALF 52 * the size of the array redaction_ranges. If there are no redaction ranges, 53 * this value must be set to 0. 54 * 55 * @param redaction_ranges array that defines the redaction ranges in 56 * the following way: 57 * redaction ranges n = [ redaction_ranges[2n], redaction_ranges[2n+1]] 58 * This means that this array's length should be TWICE the value of 59 * redaction_ranges_num 60 */ 61 RedactionInfo(int redaction_ranges_num, const off64_t* redaction_ranges); 62 /** 63 * Constructs a new instance of RedactionInfo with no redaction ranges. 64 */ 65 RedactionInfo() = default; 66 /** 67 * Calls d'tor for redactionRanges (vector). 68 */ 69 ~RedactionInfo() = default; 70 71 /** 72 * Returns a set of ranges to fulfill a read request starting at |off| of size 73 * |size|. 74 */ 75 void getReadRanges(off64_t off, size_t size, std::vector<ReadRange>* out) const; 76 77 /** 78 * Returns whether any ranges need to be redacted. 79 */ 80 bool isRedactionNeeded() const; 81 /** 82 * Returns number of redaction ranges. 83 */ 84 int size() const; 85 86 private: 87 /** 88 * Calculates the redaction ranges that overlap with a given read request. 89 * The read request is defined by its size and the offset of its first byte. 90 * 91 * <p>The returned ranges are guaranteed to be: 92 * * Non-overlapping (with each other) 93 * * Sorted in an ascending order of offset 94 * 95 * @param size size of the read request 96 * @param off offset of the first byte of the read request 97 * @return unique_ptr to a vector of RedactionRanges. If there are no 98 * relevant redaction ranges, the vector will be empty. 99 */ 100 std::unique_ptr<std::vector<RedactionRange>> getOverlappingRedactionRanges(size_t size, 101 off64_t off) const; 102 std::vector<RedactionRange> redaction_ranges_; 103 void processRedactionRanges(int redaction_ranges_num, const off64_t* redaction_ranges); 104 bool hasOverlapWithReadRequest(size_t size, off64_t off) const; 105 }; 106 107 } // namespace fuse 108 } // namespace mediaprovider 109 110 #endif // MEDIA_PROVIDER_FUSE_REDACTIONINFO_H_ 111