Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0-only */
2 : /*
3 : * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
4 : */
5 :
6 : #include "core/verdict.h"
7 :
8 : #include <errno.h>
9 : #include <stddef.h>
10 :
11 : #include "core/helper.h"
12 :
13 : static const char *_bf_verdict_strs[] = {
14 : [BF_VERDICT_ACCEPT] = "ACCEPT",
15 : [BF_VERDICT_DROP] = "DROP",
16 : [BF_VERDICT_CONTINUE] = "CONTINUE",
17 : };
18 :
19 : static_assert(ARRAY_SIZE(_bf_verdict_strs) == _BF_VERDICT_MAX,
20 : "missing entries in the verdict array");
21 :
22 5 : const char *bf_verdict_to_str(enum bf_verdict verdict)
23 : {
24 5 : bf_assert(0 <= verdict && verdict < _BF_VERDICT_MAX);
25 :
26 3 : return _bf_verdict_strs[verdict];
27 : }
28 :
29 7 : int bf_verdict_from_str(const char *str, enum bf_verdict *verdict)
30 : {
31 7 : bf_assert(str);
32 6 : bf_assert(verdict);
33 :
34 14 : for (size_t i = 0; i < _BF_VERDICT_MAX; ++i) {
35 12 : if (bf_streq(_bf_verdict_strs[i], str)) {
36 3 : *verdict = i;
37 3 : return 0;
38 : }
39 : }
40 :
41 : return -EINVAL;
42 : }
|