Merge branch 'maint-0.4.8' into release-0.4.8
[tor.git] / src / feature / dirauth / recommend_pkg.c
blob5d7e53c6d9624e0b9469954b752a3f69a3322b46
1 /* Copyright (c) 2001-2004, 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 */
6 /**
7 * \file recommend_pkg.c
8 * \brief Code related to the recommended-packages subsystem.
10 * Currently unused.
11 **/
13 #include "core/or/or.h"
14 #include "feature/dirauth/recommend_pkg.h"
16 /** Return true iff <b>line</b> is a valid RecommendedPackages line.
19 The grammar is:
21 "package" SP PACKAGENAME SP VERSION SP URL SP DIGESTS NL
23 PACKAGENAME = NONSPACE
24 VERSION = NONSPACE
25 URL = NONSPACE
26 DIGESTS = DIGEST | DIGESTS SP DIGEST
27 DIGEST = DIGESTTYPE "=" DIGESTVAL
29 NONSPACE = one or more non-space printing characters
31 DIGESTVAL = DIGESTTYPE = one or more non-=, non-" " characters.
33 SP = " "
34 NL = a newline
37 int
38 validate_recommended_package_line(const char *line)
40 const char *cp = line;
42 #define WORD() \
43 do { \
44 if (*cp == ' ') \
45 return 0; \
46 cp = strchr(cp, ' '); \
47 if (!cp) \
48 return 0; \
49 } while (0)
51 WORD(); /* skip packagename */
52 ++cp;
53 WORD(); /* skip version */
54 ++cp;
55 WORD(); /* Skip URL */
56 ++cp;
58 /* Skip digesttype=digestval + */
59 int n_entries = 0;
60 while (1) {
61 const char *start_of_word = cp;
62 const char *end_of_word = strchr(cp, ' ');
63 if (! end_of_word)
64 end_of_word = cp + strlen(cp);
66 if (start_of_word == end_of_word)
67 return 0;
69 const char *eq = memchr(start_of_word, '=', end_of_word - start_of_word);
71 if (!eq)
72 return 0;
73 if (eq == start_of_word)
74 return 0;
75 if (eq == end_of_word - 1)
76 return 0;
77 if (memchr(eq+1, '=', end_of_word - (eq+1)))
78 return 0;
80 ++n_entries;
81 if (0 == *end_of_word)
82 break;
84 cp = end_of_word + 1;
87 /* If we reach this point, we have at least 1 entry. */
88 tor_assert(n_entries > 0);
89 return 1;