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