keyval.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. /**
  6. * \file keyval.c
  7. *
  8. * \brief Handle data encoded as a key=value pair.
  9. **/
  10. #include "orconfig.h"
  11. #include "lib/encoding/keyval.h"
  12. #include "lib/log/escape.h"
  13. #include "lib/log/torlog.h"
  14. #include "lib/log/util_bug.h"
  15. #include <stdlib.h>
  16. #include <string.h>
  17. /** Return true if <b>string</b> is a valid 'key=[value]' string.
  18. * "value" is optional, to indicate the empty string. Log at logging
  19. * <b>severity</b> if something ugly happens. */
  20. int
  21. string_is_key_value(int severity, const char *string)
  22. {
  23. /* position of equal sign in string */
  24. const char *equal_sign_pos = NULL;
  25. tor_assert(string);
  26. if (strlen(string) < 2) { /* "x=" is shortest args string */
  27. tor_log(severity, LD_GENERAL, "'%s' is too short to be a k=v value.",
  28. escaped(string));
  29. return 0;
  30. }
  31. equal_sign_pos = strchr(string, '=');
  32. if (!equal_sign_pos) {
  33. tor_log(severity, LD_GENERAL, "'%s' is not a k=v value.", escaped(string));
  34. return 0;
  35. }
  36. /* validate that the '=' is not in the beginning of the string. */
  37. if (equal_sign_pos == string) {
  38. tor_log(severity, LD_GENERAL, "'%s' is not a valid k=v value.",
  39. escaped(string));
  40. return 0;
  41. }
  42. return 1;
  43. }