tor  0.4.0.0-alpha-dev
keyval.c
Go to the documentation of this file.
1 /* Copyright (c) 2003, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2018, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
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 
24 int
25 string_is_key_value(int severity, const char *string)
26 {
27  /* position of equal sign in string */
28  const char *equal_sign_pos = NULL;
29 
30  tor_assert(string);
31 
32  if (strlen(string) < 2) { /* "x=" is shortest args string */
33  tor_log(severity, LD_GENERAL, "'%s' is too short to be a k=v value.",
34  escaped(string));
35  return 0;
36  }
37 
38  equal_sign_pos = strchr(string, '=');
39  if (!equal_sign_pos) {
40  tor_log(severity, LD_GENERAL, "'%s' is not a k=v value.", escaped(string));
41  return 0;
42  }
43 
44  /* validate that the '=' is not in the beginning of the string. */
45  if (equal_sign_pos == string) {
46  tor_log(severity, LD_GENERAL, "'%s' is not a valid k=v value.",
47  escaped(string));
48  return 0;
49  }
50 
51  return 1;
52 }
#define LD_GENERAL
Definition: log.h:58
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:631
tor_assert(buffer)
const char * escaped(const char *s)
Definition: escape.c:126
Header for keyval.c.
Header for escape.c.
Headers for log.c.
Macros to manage assertions, fatal and non-fatal.
int string_is_key_value(int severity, const char *string)
Definition: keyval.c:25