From 30166261bb1e523db1199ea422c9bf09f30de69f Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 28 Jun 2018 11:46:32 -0400 Subject: Move string_is_key_value to lib/encoding --- src/lib/encoding/include.am | 2 ++ src/lib/encoding/keyval.c | 46 +++++++++++++++++++++++++++++++++++++++++++++ src/lib/encoding/keyval.h | 11 +++++++++++ 3 files changed, 59 insertions(+) create mode 100644 src/lib/encoding/keyval.c create mode 100644 src/lib/encoding/keyval.h (limited to 'src/lib') diff --git a/src/lib/encoding/include.am b/src/lib/encoding/include.am index 93f515dd45..cf9fb1b259 100644 --- a/src/lib/encoding/include.am +++ b/src/lib/encoding/include.am @@ -8,6 +8,7 @@ src_lib_libtor_encoding_a_SOURCES = \ src/lib/encoding/binascii.c \ src/lib/encoding/confline.c \ src/lib/encoding/cstring.c \ + src/lib/encoding/keyval.c \ src/lib/encoding/time_fmt.c src_lib_libtor_encoding_testing_a_SOURCES = \ @@ -19,4 +20,5 @@ noinst_HEADERS += \ src/lib/encoding/binascii.h \ src/lib/encoding/confline.h \ src/lib/encoding/cstring.h \ + src/lib/encoding/keyval.h \ src/lib/encoding/time_fmt.h diff --git a/src/lib/encoding/keyval.c b/src/lib/encoding/keyval.c new file mode 100644 index 0000000000..cffd6f6dbc --- /dev/null +++ b/src/lib/encoding/keyval.c @@ -0,0 +1,46 @@ +/* Copyright (c) 2003, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#include "orconfig.h" +#include "lib/encoding/keyval.h" +#include "lib/log/escape.h" +#include "lib/log/torlog.h" +#include "lib/log/util_bug.h" + +#include +#include + +/** Return true if string is a valid 'key=[value]' string. + * "value" is optional, to indicate the empty string. Log at logging + * severity if something ugly happens. */ +int +string_is_key_value(int severity, const char *string) +{ + /* position of equal sign in string */ + const char *equal_sign_pos = NULL; + + tor_assert(string); + + if (strlen(string) < 2) { /* "x=" is shortest args string */ + tor_log(severity, LD_GENERAL, "'%s' is too short to be a k=v value.", + escaped(string)); + return 0; + } + + equal_sign_pos = strchr(string, '='); + if (!equal_sign_pos) { + tor_log(severity, LD_GENERAL, "'%s' is not a k=v value.", escaped(string)); + return 0; + } + + /* validate that the '=' is not in the beginning of the string. */ + if (equal_sign_pos == string) { + tor_log(severity, LD_GENERAL, "'%s' is not a valid k=v value.", + escaped(string)); + return 0; + } + + return 1; +} diff --git a/src/lib/encoding/keyval.h b/src/lib/encoding/keyval.h new file mode 100644 index 0000000000..7458555207 --- /dev/null +++ b/src/lib/encoding/keyval.h @@ -0,0 +1,11 @@ +/* Copyright (c) 2003, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#ifndef TOR_KEYVAL_H +#define TOR_KEYVAL_H + +int string_is_key_value(int severity, const char *string); + +#endif -- cgit v1.2.3-54-g00ecf