1 /* 2 * Copyright (C) 2023 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 SRC_PROTOZERO_FILTERING_STRING_FILTER_H_ 18 #define SRC_PROTOZERO_FILTERING_STRING_FILTER_H_ 19 20 #include <regex> 21 #include <string> 22 #include <string_view> 23 24 namespace protozero { 25 26 // Performs filtering of strings in an "iptables" style. See the comments in 27 // |TraceConfig.TraceFilter| for information on how this class works. 28 class StringFilter { 29 public: 30 enum class Policy { 31 kMatchRedactGroups = 1, 32 kAtraceMatchRedactGroups = 2, 33 kMatchBreak = 3, 34 kAtraceMatchBreak = 4, 35 kAtraceRepeatedSearchRedactGroups = 5, 36 }; 37 38 // Adds a new rule for filtering strings. 39 void AddRule(Policy policy, 40 std::string_view pattern, 41 std::string atrace_payload_starts_with); 42 43 // Tries to filter the given string. Returns true if the string was modified 44 // in any way, false otherwise. MaybeFilter(char * ptr,size_t len)45 bool MaybeFilter(char* ptr, size_t len) const { 46 if (len == 0 || rules_.empty()) { 47 return false; 48 } 49 return MaybeFilterInternal(ptr, len); 50 } 51 52 private: 53 struct Rule { 54 Policy policy; 55 std::regex pattern; 56 std::string atrace_payload_starts_with; 57 }; 58 59 bool MaybeFilterInternal(char* ptr, size_t len) const; 60 61 std::vector<Rule> rules_; 62 }; 63 64 } // namespace protozero 65 66 #endif // SRC_PROTOZERO_FILTERING_STRING_FILTER_H_ 67