Overpass API regression suite coverage (Nova Scotia) / overpass_api/frontend/hash_request.cc

overpass_api/frontend/hash_request.cc

83 of 84 instrumented lines run by the suite (98.8%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include "hash_request.h"
22std::string sanitize_string(const std::string& input, bool anonymize)
231190595 q{
241190595 q std::string result = input;
251190595 q std::string::size_type src_pos = 0;
261190595 q std::string::size_type target_pos = 0;
27281717595 q while (src_pos < input.size())
28280527595 q {
29280527595 q if (isspace(input[src_pos]))
3031186595 q {
3131186595 q ++src_pos;
3231714595 q while (src_pos < input.size() && isspace(input[src_pos]))
3352899 q ++src_pos;
3431186595 q if (target_pos > 0 && src_pos < input.size()
3531186595 q && (isalpha(result[target_pos-1]) || isdigit(result[target_pos-1]) || result[target_pos-1] == '_')
3631186595 q && (isalpha(input[src_pos]) || isdigit(input[src_pos]) || input[src_pos] == '_' || input[src_pos] == '-'))
3719050577 q result[target_pos++] = ' ';
3831186595 q }
39249341595 q else if (anonymize && (isdigit(input[src_pos])
40105917595 q || (input[src_pos] == '-' && src_pos+1 < input.size() && isdigit(input[src_pos+1]))))
4111436585 q {
4211436585 q result[target_pos++] = '0';
4311436585 q ++src_pos;
4431238585 q while (src_pos < input.size() && (isdigit(input[src_pos]) || input[src_pos] == '.'))
4519802584 q ++src_pos;
4611436585 q }
47237905595 q else if (anonymize && input[src_pos] == '\"' && target_pos >= 5 && result.substr(target_pos-5,5) == "poly:")
482121 q {
492121 q ++src_pos;
501190721 q while (src_pos < input.size() && input[src_pos] != '\"')
511188621 q ++src_pos;
522121 q result[target_pos++] = '\"';
532121 q result[target_pos++] = '\"';
542121 q ++src_pos;
552121 q }
56237884595 q else if (anonymize && input[src_pos] == '\'' && target_pos >= 5 && result.substr(target_pos-5,5) == "poly:")
573333 q {
583333 q ++src_pos;
59760233 q while (src_pos < input.size() && input[src_pos] != '\'')
60756933 q ++src_pos;
613333 q result[target_pos++] = '\'';
623333 q result[target_pos++] = '\'';
633333 q ++src_pos;
643333 q }
65237851595 q else if (input[src_pos] == '/' && src_pos+1 < input.size())
6614627 q {
6714627 q if (input[src_pos+1] == '/')
6961 q src_pos += 2;
701321 q while (src_pos < input.size() && input[src_pos] != '\n')
711261 q ++src_pos;
7314026 q else if (input[src_pos+1] == '*')
7521 q src_pos += 2;
76981 q while (src_pos < input.size() && (input[src_pos] != '/' || input[src_pos-1] != '*'))
77961 q ++src_pos;
7821 q if (src_pos < input.size())
7921 q ++src_pos;
8113825 q else
8213825 q result[target_pos++] = input[src_pos++];
8314627 q }
84237705595 q else if ((input[src_pos] & 0xe0) == 0)
850 ++src_pos;
86237705595 q else
87237705595 q result[target_pos++] = input[src_pos++];
88280527595 q }
891190595 q result.resize(target_pos);
901190595 q return result;
911190595 q}
94uint64_t hash(const std::string& input)
951190595 q{
961190595 q uint64_t result = (input.size()<<32) ^ (input.size()<<48);
971190595 q int pos = 0;
98269627595 q for (decltype(input.size()) i = 0; i < input.size(); ++i)
99268437595 q {
100268437595 q int spread0 = ((5*i)%17)+8;
101268437595 q int spread1 = ((5*i)%23)+33;
102268437595 q uint64_t val = (unsigned char)input[i];
103268437595 q val = val ^ (val<<spread0);
104268437595 q val = val ^ (val<<spread1);
105268437595 q result ^= ((val << pos)|(val >> (64-pos)));
106268437595 q pos = (pos + 19) & 63;
107268437595 q }
1081190595 q return result;