keyval.c 1.3 KB

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