/src/tor/src/lib/encoding/keyval.c
Line | Count | Source |
1 | | /* Copyright (c) 2003, Roger Dingledine |
2 | | * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. |
3 | | * Copyright (c) 2007-2021, The Tor Project, Inc. */ |
4 | | /* See LICENSE for licensing information */ |
5 | | |
6 | | /** |
7 | | * \file keyval.c |
8 | | * |
9 | | * \brief Handle data encoded as a key=value pair. |
10 | | **/ |
11 | | |
12 | | #include "orconfig.h" |
13 | | #include "lib/encoding/keyval.h" |
14 | | #include "lib/log/escape.h" |
15 | | #include "lib/log/log.h" |
16 | | #include "lib/log/util_bug.h" |
17 | | |
18 | | #include <stdlib.h> |
19 | | #include <string.h> |
20 | | |
21 | | /** Return true if <b>string</b> is a valid 'key=[value]' string. |
22 | | * "value" is optional, to indicate the empty string. Log at logging |
23 | | * <b>severity</b> if something ugly happens. */ |
24 | | int |
25 | | string_is_key_value(int severity, const char *string) |
26 | 0 | { |
27 | | /* position of equal sign in string */ |
28 | 0 | const char *equal_sign_pos = NULL; |
29 | |
|
30 | 0 | tor_assert(string); |
31 | |
|
32 | 0 | if (strlen(string) < 2) { /* "x=" is shortest args string */ |
33 | 0 | tor_log(severity, LD_GENERAL, "'%s' is too short to be a k=v value.", |
34 | 0 | escaped(string)); |
35 | 0 | return 0; |
36 | 0 | } |
37 | | |
38 | 0 | equal_sign_pos = strchr(string, '='); |
39 | 0 | if (!equal_sign_pos) { |
40 | 0 | tor_log(severity, LD_GENERAL, "'%s' is not a k=v value.", escaped(string)); |
41 | 0 | return 0; |
42 | 0 | } |
43 | | |
44 | | /* validate that the '=' is not in the beginning of the string. */ |
45 | 0 | if (equal_sign_pos == string) { |
46 | 0 | tor_log(severity, LD_GENERAL, "'%s' is not a valid k=v value.", |
47 | 0 | escaped(string)); |
48 | 0 | return 0; |
49 | 0 | } |
50 | | |
51 | 0 | return 1; |
52 | 0 | } |