MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / ParseTorReplyMapping

Function ParseTorReplyMapping

src/torcontrol.cpp:259–350  ·  view source on GitHub ↗

Parse reply arguments in the form 'METHODS=COOKIE,SAFECOOKIE COOKIEFILE=".../control_auth_cookie"'. * Returns a map of keys to values, or an empty map if there was an error. * Grammar is implicitly defined in https://spec.torproject.org/control-spec by * the server reply formats for PROTOCOLINFO (S3.21), AUTHCHALLENGE (S3.24), * and ADD_ONION (S3.27). See also sections 2.1 and 2.3. */

Source from the content-addressed store, hash-verified

257 * and ADD_ONION (S3.27). See also sections 2.1 and 2.3.
258 */
259std::map<std::string,std::string> ParseTorReplyMapping(const std::string &s)
260{
261 std::map<std::string,std::string> mapping;
262 size_t ptr=0;
263 while (ptr < s.size()) {
264 std::string key, value;
265 while (ptr < s.size() && s[ptr] != '=' && s[ptr] != ' ') {
266 key.push_back(s[ptr]);
267 ++ptr;
268 }
269 if (ptr == s.size()) // unexpected end of line
270 return std::map<std::string,std::string>();
271 if (s[ptr] == ' ') // The remaining string is an OptArguments
272 break;
273 ++ptr; // skip '='
274 if (ptr < s.size() && s[ptr] == '"') { // Quoted string
275 ++ptr; // skip opening '"'
276 bool escape_next = false;
277 while (ptr < s.size() && (escape_next || s[ptr] != '"')) {
278 // Repeated backslashes must be interpreted as pairs
279 escape_next = (s[ptr] == '\\' && !escape_next);
280 value.push_back(s[ptr]);
281 ++ptr;
282 }
283 if (ptr == s.size()) // unexpected end of line
284 return std::map<std::string,std::string>();
285 ++ptr; // skip closing '"'
286 /**
287 * Unescape value. Per https://spec.torproject.org/control-spec section 2.1.1:
288 *
289 * For future-proofing, controller implementers MAY use the following
290 * rules to be compatible with buggy Tor implementations and with
291 * future ones that implement the spec as intended:
292 *
293 * Read \n \t \r and \0 ... \377 as C escapes.
294 * Treat a backslash followed by any other character as that character.
295 */
296 std::string escaped_value;
297 for (size_t i = 0; i < value.size(); ++i) {
298 if (value[i] == '\\') {
299 // This will always be valid, because if the QuotedString
300 // ended in an odd number of backslashes, then the parser
301 // would already have returned above, due to a missing
302 // terminating double-quote.
303 ++i;
304 if (value[i] == 'n') {
305 escaped_value.push_back('\n');
306 } else if (value[i] == 't') {
307 escaped_value.push_back('\t');
308 } else if (value[i] == 'r') {
309 escaped_value.push_back('\r');
310 } else if ('0' <= value[i] && value[i] <= '7') {
311 size_t j;
312 // Octal escape sequences have a limit of three octal digits,
313 // but terminate at the first character that is not a valid
314 // octal digit if encountered sooner.
315 for (j = 1; j < 3 && (i+j) < value.size() && '0' <= value[i+j] && value[i+j] <= '7'; ++j) {}
316 // Tor restricts first digit to 0-3 for three-digit octals.

Callers 5

add_onion_cbMethod · 0.85
authchallenge_cbMethod · 0.85
protocolinfo_cbMethod · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 2

sizeMethod · 0.45
push_backMethod · 0.45

Tested by 2

BOOST_AUTO_TEST_CASEFunction · 0.68