CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sanitizer.cc
Go to the documentation of this file.
1 
7 #include "cvmfs_config.h"
8 #include "sanitizer.h"
9 
10 #include <cassert>
11 
12 using namespace std; // NOLINT
13 
14 #ifdef CVMFS_NAMESPACE_GUARD
15 namespace CVMFS_NAMESPACE_GUARD {
16 #endif
17 
18 namespace sanitizer {
19 
20 CharRange::CharRange(const char range_begin, const char range_end) {
21  range_begin_ = range_begin;
22  range_end_ = range_end;
23 }
24 
25 
26 CharRange::CharRange(const char single_char) {
27  range_begin_ = range_end_ = single_char;
28 }
29 
30 
31 bool CharRange::InRange(const char c) const {
32  return (c >= range_begin_) && (c <= range_end_);
33 }
34 
35 
36 //------------------------------------------------------------------------------
37 
38 
39 InputSanitizer::InputSanitizer(const string &whitelist) : max_length_(-1) {
40  InitValidRanges(whitelist);
41 }
42 
43 
44 InputSanitizer::InputSanitizer(const string &whitelist, int max_length)
45  : max_length_(max_length)
46 {
47  InitValidRanges(whitelist);
48 }
49 
50 
51 void InputSanitizer::InitValidRanges(const std::string &whitelist) {
52  // Parse the whitelist
53  const unsigned length = whitelist.length();
54  unsigned pickup_pos = 0;
55  for (unsigned i = 0; i < length; ++i) {
56  if ((i+1 >= length) || (whitelist[i+1] == ' ') || (i == length-1)) {
57  const string range = whitelist.substr(pickup_pos, i-pickup_pos+1);
58  switch (range.length()) {
59  case 1:
60  valid_ranges_.push_back(CharRange(range[0]));
61  break;
62  case 2:
63  valid_ranges_.push_back(CharRange(range[0], range[1]));
64  break;
65  default:
66  assert(false);
67  }
68  ++i;
69  pickup_pos = i+1;
70  }
71  }
72 }
73 
74 
76  std::string::const_iterator begin,
77  std::string::const_iterator end,
78  std::string *filtered_output) const {
79  int pos = 0;
80  bool is_sane = true;
81  for (; begin != end; ++begin) {
82  if (CheckRanges(*begin)) {
83  if ((max_length_ >= 0) && (pos >= max_length_)) {
84  is_sane = false;
85  break;
86  }
87  filtered_output->push_back(*begin);
88  pos++;
89  } else {
90  is_sane = false;
91  }
92  }
93  return is_sane;
94 }
95 
96 
97 bool InputSanitizer::CheckRanges(const char chr) const {
98  for (unsigned j = 0; j < valid_ranges_.size(); ++j) {
99  if (valid_ranges_[j].InRange(chr)) {
100  return true;
101  }
102  }
103  return false;
104 }
105 
106 
107 string InputSanitizer::Filter(const std::string &input) const {
108  string filtered_output;
109  Sanitize(input, &filtered_output);
110  return filtered_output;
111 }
112 
113 
114 bool InputSanitizer::IsValid(const std::string &input) const {
115  string dummy;
116  return Sanitize(input, &dummy);
117 }
118 
119 
121  std::string::const_iterator begin,
122  std::string::const_iterator end,
123  std::string *filtered_output) const {
124  if (std::distance(begin, end) == 0) {
125  return false;
126  }
127 
128  if (*begin == '-') {
129  // minus is allowed as the first character!
130  filtered_output->push_back('-');
131  begin++;
132  }
133 
134  return InputSanitizer::Sanitize(begin, end, filtered_output);
135 }
136 
137 
139  std::string::const_iterator begin,
140  std::string::const_iterator end,
141  std::string *filtered_output) const {
142  if (std::distance(begin, end) == 0) {
143  return false;
144  }
145 
146  return InputSanitizer::Sanitize(begin, end, filtered_output);
147 }
148 
149 } // namespace sanitizer
150 
151 #ifdef CVMFS_NAMESPACE_GUARD
152 } // namespace CVMFS_NAMESPACE_GUARD
153 #endif
bool CheckRanges(const char chr) const
Definition: sanitizer.cc:97
void InitValidRanges(const std::string &whitelist)
Definition: sanitizer.cc:51
InputSanitizer(const std::string &whitelist)
virtual bool Sanitize(std::string::const_iterator begin, std::string::const_iterator end, std::string *filtered_output) const
Definition: sanitizer.cc:120
bool Sanitize(const std::string &input, std::string *filtered_output) const
Definition: sanitizer.h:40
assert((mem||(size==0))&&"Out Of Memory")
bool IsValid(const std::string &input) const
Definition: sanitizer.cc:114
std::vector< CharRange > valid_ranges_
Definition: sanitizer.h:52
const whitelist::Whitelist * whitelist() const
Definition: repository.h:124
virtual bool Sanitize(std::string::const_iterator begin, std::string::const_iterator end, std::string *filtered_output) const
Definition: sanitizer.cc:138
std::string Filter(const std::string &input) const
Definition: sanitizer.cc:107