|
1 | | -// SPDX-License-Identifier: BSD-3-Clause |
2 | | -/* |
3 | | - * Copyright(c) 2023 Intel Corporation. All rights reserved. |
4 | | - * |
5 | | - * Author: Karol Trzcinski <karolx.trzcinski@linux.intel.com> |
6 | | - * Marc Herbert <marc.herbert@intel.com> |
7 | | - */ |
8 | | - |
9 | | -#ifndef __TOML_UTILS_H__ |
10 | | -#define __TOML_UTILS_H__ |
11 | | - |
12 | | -#include "toml.h" |
13 | | - |
14 | | -#include <stdint.h> |
15 | | -#include <stdio.h> |
16 | | -#include <stdarg.h> |
17 | | - |
18 | | -/** parser counter, used to assert nothing left unparsed in toml data */ |
19 | | -struct parse_ctx { |
20 | | - int key_cnt; /**< number of parsed key */ |
21 | | - int table_cnt; /**< number of parsed tables */ |
22 | | - int array_cnt; /**< number of parsed arrays */ |
23 | | -}; |
24 | | - |
25 | | -/* macros used to dump values after parsing */ |
26 | | -#define DUMP_KEY_FMT " %20s: " |
27 | | -#define DUMP(fmt, ...) fprintf(stdout, fmt "\n", ##__VA_ARGS__) |
28 | | -#define DUMP_KEY(key, fmt, ...) DUMP(DUMP_KEY_FMT fmt, key, ##__VA_ARGS__) |
29 | | - |
30 | | -void print_bytes(FILE *out, const uint8_t *arr, size_t len); |
31 | | - |
32 | | -#define DUMP_PRINTABLE_BYTES(name, var) _dump_printable_bytes(name, var, sizeof(var)) |
33 | | - |
34 | | -void _dump_printable_bytes(const char *name, const uint8_t *arr, size_t len); |
35 | | - |
36 | | -/** private parser error trace function */ |
37 | | -void vlog_err(const char *msg, va_list vl); |
38 | | - |
39 | | -/** parser error trace function, error code is returned to shorten client code */ |
40 | | -int log_err(int err_code, const char *msg, ...); |
41 | | - |
42 | | -/** log malloc error message for given key */ |
43 | | -int err_malloc(const char *key); |
44 | | - |
45 | | -/** log key not found error */ |
46 | | -int err_key_not_found(const char *key); |
47 | | - |
48 | | -/** error during parsing key value, possible detailed message */ |
49 | | -int err_key_parse(const char *key, const char *extra_msg, ...); |
50 | | - |
51 | | -/** initialize parser context before parsing */ |
52 | | -void parse_ctx_init(struct parse_ctx *ctx); |
53 | | - |
54 | | -/** check nothing left unparsed in given parsing context */ |
55 | | -int assert_everything_parsed(const toml_table_t *table, struct parse_ctx *ctx); |
56 | | - |
57 | | -/** |
58 | | - * Parse hex value from key in given toml table |
59 | | - * @param table toml table where key is specified |
60 | | - * @param ctx parsing context, key counter will be incremented after successful key parse |
61 | | - * @param key field name |
62 | | - * @param def is default value or -1 when value don't have default value |
63 | | - * @param error code, 0 when success |
64 | | - * @return default, parsed, or UINT32_MAX value for error cases |
65 | | - */ |
66 | | -uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx, |
67 | | - const char *key, int64_t def, int *error); |
68 | | - |
69 | | -/** |
70 | | - * Parse integer value from key in given toml table |
71 | | - * @param table toml table where key is specified |
72 | | - * @param ctx parsing context, key counter will be incremented after successful key parse |
73 | | - * @param key field name |
74 | | - * @param def is default value or -1 when value don't have default value |
75 | | - * @param error code, 0 when success |
76 | | - * @return default, parsed, or UINT32_MAX value for error cases |
77 | | - */ |
78 | | -uint32_t parse_uint32_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key, |
79 | | - int64_t def, int *error); |
80 | | - |
81 | | -/** |
82 | | - * Parse string value from key in given toml table to uint8_t array. The |
83 | | - * destination is NOT a string because it is padded with zeros if and |
84 | | - * only if there is some capacity left. For string destinations use |
85 | | - * parse_str_key(). |
86 | | - * |
87 | | - * @param table toml table where key is specified |
88 | | - * @param ctx parsing context, key counter will be incremented after successful key parse |
89 | | - * @param key field name |
90 | | - * @param dst uint8_t[] destination |
91 | | - * @param capacity dst array size |
92 | | - * @param error code, 0 when success |
93 | | - */ |
94 | | -void parse_printable_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key, |
95 | | - uint8_t *dst, int capacity, int *error); |
96 | | - |
97 | | -/** |
98 | | - * Parse string value from key in given toml table to given |
99 | | - * char[]. Destination is padded with zeros. As the only difference with |
100 | | - * parse_printable_key(), dst is guaranteed to be null-terminated when |
101 | | - * there is no error because the last destination byte is reserved for |
102 | | - * that. |
103 | | - * |
104 | | - * @param table toml table where key is specified |
105 | | - * @param ctx parsing context, key counter will be incremented after successful key parse |
106 | | - * @param key field name |
107 | | - * @param dst char[] destination |
108 | | - * @param capacity dst array size including null termination. |
109 | | - * @param error code, 0 when success |
110 | | - */ |
111 | | -void parse_str_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key, |
112 | | - char *dst, int capacity, int *error); |
113 | | - |
114 | | -/** |
115 | | - * Parse UUID hex string into a byte array. The endianness of the output |
116 | | - * is architecture-dependent: do not use in any portable code. |
117 | | - */ |
118 | | -void parse_uuid(const char *buf, void *uuid); |
119 | | - |
120 | | -/** version is stored as toml array with integer number, something like: |
121 | | - * "version = [1, 8]" |
122 | | - */ |
123 | | -int parse_version(toml_table_t *toml, int64_t version[2]); |
124 | | - |
125 | | -#endif /* __TOML_UTILS_H__ */ |
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +/* |
| 3 | + * Copyright(c) 2023 Intel Corporation. All rights reserved. |
| 4 | + * |
| 5 | + * Author: Karol Trzcinski <karolx.trzcinski@linux.intel.com> |
| 6 | + * Marc Herbert <marc.herbert@intel.com> |
| 7 | + */ |
| 8 | + |
| 9 | +#ifndef __TOML_UTILS_H__ |
| 10 | +#define __TOML_UTILS_H__ |
| 11 | + |
| 12 | +#include "toml.h" |
| 13 | + |
| 14 | +#include <stdint.h> |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdarg.h> |
| 17 | + |
| 18 | +/** parser counter, used to assert nothing left unparsed in toml data */ |
| 19 | +struct parse_ctx { |
| 20 | + int key_cnt; /**< number of parsed key */ |
| 21 | + int table_cnt; /**< number of parsed tables */ |
| 22 | + int array_cnt; /**< number of parsed arrays */ |
| 23 | +}; |
| 24 | + |
| 25 | +/* macros used to dump values after parsing */ |
| 26 | +#define DUMP_KEY_FMT " %20s: " |
| 27 | +#define DUMP(fmt, ...) fprintf(stdout, fmt "\n", ##__VA_ARGS__) |
| 28 | +#define DUMP_KEY(key, fmt, ...) DUMP(DUMP_KEY_FMT fmt, key, ##__VA_ARGS__) |
| 29 | + |
| 30 | +void print_bytes(FILE *out, const uint8_t *arr, size_t len); |
| 31 | + |
| 32 | +#define DUMP_PRINTABLE_BYTES(name, var) _dump_printable_bytes(name, var, sizeof(var)) |
| 33 | + |
| 34 | +void _dump_printable_bytes(const char *name, const uint8_t *arr, size_t len); |
| 35 | + |
| 36 | +/** private parser error trace function */ |
| 37 | +void vlog_err(const char *msg, va_list vl); |
| 38 | + |
| 39 | +/** parser error trace function, error code is returned to shorten client code */ |
| 40 | +int log_err(int err_code, const char *msg, ...); |
| 41 | + |
| 42 | +/** log malloc error message for given key */ |
| 43 | +int err_malloc(const char *key); |
| 44 | + |
| 45 | +/** log key not found error */ |
| 46 | +int err_key_not_found(const char *key); |
| 47 | + |
| 48 | +/** error during parsing key value, possible detailed message */ |
| 49 | +int err_key_parse(const char *key, const char *extra_msg, ...); |
| 50 | + |
| 51 | +/** initialize parser context before parsing */ |
| 52 | +void parse_ctx_init(struct parse_ctx *ctx); |
| 53 | + |
| 54 | +/** check nothing left unparsed in given parsing context */ |
| 55 | +int assert_everything_parsed(const toml_table_t *table, struct parse_ctx *ctx); |
| 56 | + |
| 57 | +/** |
| 58 | + * Parse hex value from key in given toml table |
| 59 | + * @param table toml table where key is specified |
| 60 | + * @param ctx parsing context, key counter will be incremented after successful key parse |
| 61 | + * @param key field name |
| 62 | + * @param def is default value or -1 when value don't have default value |
| 63 | + * @param error code, 0 when success |
| 64 | + * @return default, parsed, or UINT32_MAX value for error cases |
| 65 | + */ |
| 66 | +uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx, |
| 67 | + const char *key, int64_t def, int *error); |
| 68 | + |
| 69 | +/** |
| 70 | + * Parse integer value from key in given toml table |
| 71 | + * @param table toml table where key is specified |
| 72 | + * @param ctx parsing context, key counter will be incremented after successful key parse |
| 73 | + * @param key field name |
| 74 | + * @param def is default value or -1 when value don't have default value |
| 75 | + * @param error code, 0 when success |
| 76 | + * @return default, parsed, or UINT32_MAX value for error cases |
| 77 | + */ |
| 78 | +uint32_t parse_uint32_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key, |
| 79 | + int64_t def, int *error); |
| 80 | + |
| 81 | +/** |
| 82 | + * Parse string value from key in given toml table to uint8_t array. The |
| 83 | + * destination is NOT a string because it is padded with zeros if and |
| 84 | + * only if there is some capacity left. For string destinations use |
| 85 | + * parse_str_key(). |
| 86 | + * |
| 87 | + * @param table toml table where key is specified |
| 88 | + * @param ctx parsing context, key counter will be incremented after successful key parse |
| 89 | + * @param key field name |
| 90 | + * @param dst uint8_t[] destination |
| 91 | + * @param capacity dst array size |
| 92 | + * @param error code, 0 when success |
| 93 | + */ |
| 94 | +void parse_printable_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key, |
| 95 | + uint8_t *dst, int capacity, int *error); |
| 96 | + |
| 97 | +/** |
| 98 | + * Parse string value from key in given toml table to given |
| 99 | + * char[]. Destination is padded with zeros. As the only difference with |
| 100 | + * parse_printable_key(), dst is guaranteed to be null-terminated when |
| 101 | + * there is no error because the last destination byte is reserved for |
| 102 | + * that. |
| 103 | + * |
| 104 | + * @param table toml table where key is specified |
| 105 | + * @param ctx parsing context, key counter will be incremented after successful key parse |
| 106 | + * @param key field name |
| 107 | + * @param dst char[] destination |
| 108 | + * @param capacity dst array size including null termination. |
| 109 | + * @param error code, 0 when success |
| 110 | + */ |
| 111 | +void parse_str_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key, |
| 112 | + char *dst, int capacity, int *error); |
| 113 | + |
| 114 | +/** |
| 115 | + * Parse UUID hex string into a byte array. The endianness of the output |
| 116 | + * is architecture-dependent: do not use in any portable code. |
| 117 | + */ |
| 118 | +void parse_uuid(const char *buf, void *uuid); |
| 119 | + |
| 120 | +/** version is stored as toml array with integer number, something like: |
| 121 | + * "version = [1, 8]" |
| 122 | + */ |
| 123 | +int parse_version(toml_table_t *toml, int64_t version[2]); |
| 124 | + |
| 125 | +#endif /* __TOML_UTILS_H__ */ |
0 commit comments