diff options
Diffstat (limited to 'lib')
34 files changed, 6395 insertions, 1547 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index dc3ee893..27a08278 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am | |||
@@ -4,11 +4,23 @@ SUBDIRS = . tests | |||
4 | 4 | ||
5 | noinst_LIBRARIES = libmonitoringplug.a | 5 | noinst_LIBRARIES = libmonitoringplug.a |
6 | 6 | ||
7 | AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ | 7 | AM_CPPFLAGS = \ |
8 | -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins | 8 | -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins |
9 | 9 | ||
10 | libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c maxfd.c | 10 | libmonitoringplug_a_SOURCES = utils_base.c utils_tcp.c utils_cmd.c maxfd.c output.c perfdata.c output.c thresholds.c vendor/cJSON/cJSON.c |
11 | EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h maxfd.h | 11 | |
12 | EXTRA_DIST = utils_base.h \ | ||
13 | utils_tcp.h \ | ||
14 | utils_cmd.h \ | ||
15 | parse_ini.h \ | ||
16 | extra_opts.h \ | ||
17 | maxfd.h \ | ||
18 | perfdata.h \ | ||
19 | output.h \ | ||
20 | thresholds.h \ | ||
21 | states.h \ | ||
22 | vendor/cJSON/cJSON.h \ | ||
23 | monitoringplug.h | ||
12 | 24 | ||
13 | if USE_PARSE_INI | 25 | if USE_PARSE_INI |
14 | libmonitoringplug_a_SOURCES += parse_ini.c extra_opts.c | 26 | libmonitoringplug_a_SOURCES += parse_ini.c extra_opts.c |
@@ -16,4 +28,3 @@ endif USE_PARSE_INI | |||
16 | 28 | ||
17 | test test-debug: | 29 | test test-debug: |
18 | cd tests && make $@ | 30 | cd tests && make $@ |
19 | |||
diff --git a/lib/extra_opts.c b/lib/extra_opts.c index 88787336..3fe69014 100644 --- a/lib/extra_opts.c +++ b/lib/extra_opts.c | |||
@@ -27,27 +27,32 @@ | |||
27 | 27 | ||
28 | /* FIXME: copied from utils.h; we should move a bunch of libs! */ | 28 | /* FIXME: copied from utils.h; we should move a bunch of libs! */ |
29 | bool is_option2(char *str) { | 29 | bool is_option2(char *str) { |
30 | if (!str) | 30 | if (!str) { |
31 | return false; | 31 | return false; |
32 | else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) | 32 | } |
33 | |||
34 | if (strspn(str, "-") == 1 || strspn(str, "-") == 2) { | ||
33 | return true; | 35 | return true; |
34 | else | 36 | } |
35 | return false; | 37 | |
38 | return false; | ||
36 | } | 39 | } |
37 | 40 | ||
38 | /* this is the externally visible function used by plugins */ | 41 | /* this is the externally visible function used by plugins */ |
39 | char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { | 42 | char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { |
40 | np_arg_list *extra_args = NULL, *ea1 = NULL, *ea_tmp = NULL; | ||
41 | char **argv_new = NULL; | ||
42 | char *argptr = NULL; | ||
43 | int i, j, optfound, argc_new, ea_num = *argc; | ||
44 | |||
45 | if (*argc < 2) { | 43 | if (*argc < 2) { |
46 | /* No arguments provided */ | 44 | /* No arguments provided */ |
47 | return argv; | 45 | return argv; |
48 | } | 46 | } |
49 | 47 | ||
50 | for (i = 1; i < *argc; i++) { | 48 | np_arg_list *extra_args = NULL; |
49 | np_arg_list *ea1 = NULL; | ||
50 | np_arg_list *ea_tmp = NULL; | ||
51 | char *argptr = NULL; | ||
52 | int optfound; | ||
53 | size_t ea_num = (size_t)*argc; | ||
54 | |||
55 | for (int i = 1; i < *argc; i++) { | ||
51 | argptr = NULL; | 56 | argptr = NULL; |
52 | optfound = 0; | 57 | optfound = 0; |
53 | 58 | ||
@@ -56,8 +61,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { | |||
56 | /* It is a single argument with value */ | 61 | /* It is a single argument with value */ |
57 | argptr = argv[i] + 13; | 62 | argptr = argv[i] + 13; |
58 | /* Delete the extra opts argument */ | 63 | /* Delete the extra opts argument */ |
59 | for (j = i; j < *argc; j++) | 64 | for (int j = i; j < *argc; j++) { |
60 | argv[j] = argv[j + 1]; | 65 | argv[j] = argv[j + 1]; |
66 | } | ||
67 | |||
61 | i--; | 68 | i--; |
62 | *argc -= 1; | 69 | *argc -= 1; |
63 | } else if (strcmp(argv[i], "--extra-opts") == 0) { | 70 | } else if (strcmp(argv[i], "--extra-opts") == 0) { |
@@ -65,8 +72,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { | |||
65 | /* It is a argument with separate value */ | 72 | /* It is a argument with separate value */ |
66 | argptr = argv[i + 1]; | 73 | argptr = argv[i + 1]; |
67 | /* Delete the extra-opts argument/value */ | 74 | /* Delete the extra-opts argument/value */ |
68 | for (j = i; j < *argc - 1; j++) | 75 | for (int j = i; j < *argc - 1; j++) { |
69 | argv[j] = argv[j + 2]; | 76 | argv[j] = argv[j + 2]; |
77 | } | ||
78 | |||
70 | i -= 2; | 79 | i -= 2; |
71 | *argc -= 2; | 80 | *argc -= 2; |
72 | ea_num--; | 81 | ea_num--; |
@@ -74,8 +83,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { | |||
74 | /* It has no value */ | 83 | /* It has no value */ |
75 | optfound = 1; | 84 | optfound = 1; |
76 | /* Delete the extra opts argument */ | 85 | /* Delete the extra opts argument */ |
77 | for (j = i; j < *argc; j++) | 86 | for (int j = i; j < *argc; j++) { |
78 | argv[j] = argv[j + 1]; | 87 | argv[j] = argv[j + 1]; |
88 | } | ||
89 | |||
79 | i--; | 90 | i--; |
80 | *argc -= 1; | 91 | *argc -= 1; |
81 | } | 92 | } |
@@ -94,34 +105,37 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { | |||
94 | /* append the list to extra_args */ | 105 | /* append the list to extra_args */ |
95 | if (extra_args == NULL) { | 106 | if (extra_args == NULL) { |
96 | extra_args = ea1; | 107 | extra_args = ea1; |
97 | while ((ea1 = ea1->next)) | 108 | while ((ea1 = ea1->next)) { |
98 | ea_num++; | 109 | ea_num++; |
110 | } | ||
99 | } else { | 111 | } else { |
100 | ea_tmp = extra_args; | 112 | ea_tmp = extra_args; |
101 | while (ea_tmp->next) { | 113 | while (ea_tmp->next) { |
102 | ea_tmp = ea_tmp->next; | 114 | ea_tmp = ea_tmp->next; |
103 | } | 115 | } |
104 | ea_tmp->next = ea1; | 116 | ea_tmp->next = ea1; |
105 | while ((ea1 = ea1->next)) | 117 | while ((ea1 = ea1->next)) { |
106 | ea_num++; | 118 | ea_num++; |
119 | } | ||
107 | } | 120 | } |
108 | ea1 = ea_tmp = NULL; | 121 | ea1 = ea_tmp = NULL; |
109 | } | 122 | } |
110 | } /* lather, rince, repeat */ | 123 | } /* lather, rince, repeat */ |
111 | 124 | ||
112 | if (ea_num == *argc && extra_args == NULL) { | 125 | if (ea_num == (size_t)*argc && extra_args == NULL) { |
113 | /* No extra-opts */ | 126 | /* No extra-opts */ |
114 | return argv; | 127 | return argv; |
115 | } | 128 | } |
116 | 129 | ||
117 | /* done processing arguments. now create a new argv array... */ | 130 | /* done processing arguments. now create a new argv array... */ |
118 | argv_new = (char **)malloc((ea_num + 1) * sizeof(char **)); | 131 | char **argv_new = (char **)malloc((ea_num + 1) * sizeof(char **)); |
119 | if (argv_new == NULL) | 132 | if (argv_new == NULL) { |
120 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | 133 | die(STATE_UNKNOWN, _("malloc() failed!\n")); |
134 | } | ||
121 | 135 | ||
122 | /* starting with program name */ | 136 | /* starting with program name */ |
123 | argv_new[0] = argv[0]; | 137 | argv_new[0] = argv[0]; |
124 | argc_new = 1; | 138 | int argc_new = 1; |
125 | /* then parsed ini opts (frying them up in the same run) */ | 139 | /* then parsed ini opts (frying them up in the same run) */ |
126 | while (extra_args) { | 140 | while (extra_args) { |
127 | argv_new[argc_new++] = extra_args->arg; | 141 | argv_new[argc_new++] = extra_args->arg; |
@@ -130,8 +144,9 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { | |||
130 | free(ea1); | 144 | free(ea1); |
131 | } | 145 | } |
132 | /* finally the rest of the argv array */ | 146 | /* finally the rest of the argv array */ |
133 | for (i = 1; i < *argc; i++) | 147 | for (int i = 1; i < *argc; i++) { |
134 | argv_new[argc_new++] = argv[i]; | 148 | argv_new[argc_new++] = argv[i]; |
149 | } | ||
135 | *argc = argc_new; | 150 | *argc = argc_new; |
136 | /* and terminate. */ | 151 | /* and terminate. */ |
137 | argv_new[argc_new] = NULL; | 152 | argv_new[argc_new] = NULL; |
diff --git a/lib/maxfd.c b/lib/maxfd.c index ca5b6e54..a0f79949 100644 --- a/lib/maxfd.c +++ b/lib/maxfd.c | |||
@@ -19,7 +19,6 @@ | |||
19 | *****************************************************************************/ | 19 | *****************************************************************************/ |
20 | 20 | ||
21 | #include "./maxfd.h" | 21 | #include "./maxfd.h" |
22 | #include <errno.h> | ||
23 | 22 | ||
24 | long mp_open_max(void) { | 23 | long mp_open_max(void) { |
25 | long maxfd = 0L; | 24 | long maxfd = 0L; |
@@ -31,10 +30,11 @@ long mp_open_max(void) { | |||
31 | #ifdef _SC_OPEN_MAX | 30 | #ifdef _SC_OPEN_MAX |
32 | errno = 0; | 31 | errno = 0; |
33 | if ((maxfd = sysconf(_SC_OPEN_MAX)) < 0) { | 32 | if ((maxfd = sysconf(_SC_OPEN_MAX)) < 0) { |
34 | if (errno == 0) | 33 | if (errno == 0) { |
35 | maxfd = DEFAULT_MAXFD; /* it's indeterminate */ | 34 | maxfd = DEFAULT_MAXFD; /* it's indeterminate */ |
36 | else | 35 | } else { |
37 | die(STATE_UNKNOWN, _("sysconf error for _SC_OPEN_MAX\n")); | 36 | die(STATE_UNKNOWN, _("sysconf error for _SC_OPEN_MAX\n")); |
37 | } | ||
38 | } | 38 | } |
39 | #elif defined(OPEN_MAX) | 39 | #elif defined(OPEN_MAX) |
40 | return OPEN_MAX | 40 | return OPEN_MAX |
diff --git a/lib/monitoringplug.h b/lib/monitoringplug.h new file mode 100644 index 00000000..a555d736 --- /dev/null +++ b/lib/monitoringplug.h | |||
@@ -0,0 +1,7 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "./states.h" | ||
4 | #include "./utils_base.h" | ||
5 | #include "./thresholds.h" | ||
6 | #include "./maxfd.h" | ||
7 | #include "./output.h" | ||
diff --git a/lib/output.c b/lib/output.c new file mode 100644 index 00000000..f283969f --- /dev/null +++ b/lib/output.c | |||
@@ -0,0 +1,636 @@ | |||
1 | #include "./output.h" | ||
2 | #include "./utils_base.h" | ||
3 | #include "../plugins/utils.h" | ||
4 | |||
5 | #include <assert.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <string.h> | ||
8 | #include <strings.h> | ||
9 | // #include <cjson/cJSON.h> | ||
10 | #include "./vendor/cJSON/cJSON.h" | ||
11 | #include "perfdata.h" | ||
12 | #include "states.h" | ||
13 | |||
14 | // == Global variables | ||
15 | static mp_output_format output_format = MP_FORMAT_DEFAULT; | ||
16 | static mp_output_detail_level level_of_detail = MP_DETAIL_ALL; | ||
17 | |||
18 | // == Prototypes == | ||
19 | static char *fmt_subcheck_output(mp_output_format output_format, mp_subcheck check, | ||
20 | unsigned int indentation); | ||
21 | static inline cJSON *json_serialize_subcheck(mp_subcheck subcheck); | ||
22 | |||
23 | // == Implementation == | ||
24 | |||
25 | /* | ||
26 | * Generate output string for a mp_subcheck object | ||
27 | */ | ||
28 | static inline char *fmt_subcheck_perfdata(mp_subcheck check) { | ||
29 | char *result = strdup(""); | ||
30 | int added = 0; | ||
31 | |||
32 | if (check.perfdata != NULL) { | ||
33 | added = asprintf(&result, "%s", pd_list_to_string(*check.perfdata)); | ||
34 | } | ||
35 | |||
36 | if (check.subchecks == NULL) { | ||
37 | // No subchecks, return here | ||
38 | return result; | ||
39 | } | ||
40 | |||
41 | mp_subcheck_list *subchecks = check.subchecks; | ||
42 | |||
43 | while (subchecks != NULL) { | ||
44 | if (added > 0) { | ||
45 | added = asprintf(&result, "%s%s", result, fmt_subcheck_perfdata(subchecks->subcheck)); | ||
46 | } else { | ||
47 | // TODO free previous result here? | ||
48 | added = asprintf(&result, "%s", fmt_subcheck_perfdata(subchecks->subcheck)); | ||
49 | } | ||
50 | |||
51 | subchecks = subchecks->next; | ||
52 | } | ||
53 | |||
54 | return result; | ||
55 | } | ||
56 | |||
57 | /* | ||
58 | * Initialiser for a mp_check object. Always use this to get a new one! | ||
59 | * It sets useful defaults | ||
60 | */ | ||
61 | mp_check mp_check_init(void) { | ||
62 | mp_check check = { | ||
63 | .evaluation_function = &mp_eval_check_default, | ||
64 | }; | ||
65 | return check; | ||
66 | } | ||
67 | |||
68 | /* | ||
69 | * Initialiser for a mp_subcheck object. Always use this to get a new one! | ||
70 | * It sets useful defaults | ||
71 | */ | ||
72 | mp_subcheck mp_subcheck_init(void) { | ||
73 | mp_subcheck tmp = {0}; | ||
74 | tmp.default_state = STATE_UNKNOWN; // Default state is unknown | ||
75 | tmp.state_set_explicitly = false; | ||
76 | return tmp; | ||
77 | } | ||
78 | |||
79 | /* | ||
80 | * Add a subcheck to a (the one and only) check object | ||
81 | */ | ||
82 | int mp_add_subcheck_to_check(mp_check check[static 1], mp_subcheck subcheck) { | ||
83 | assert(subcheck.output != NULL); // There must be output in a subcheck | ||
84 | |||
85 | mp_subcheck_list *tmp = NULL; | ||
86 | |||
87 | if (check->subchecks == NULL) { | ||
88 | check->subchecks = (mp_subcheck_list *)calloc(1, sizeof(mp_subcheck_list)); | ||
89 | if (check->subchecks == NULL) { | ||
90 | die(STATE_UNKNOWN, "%s - %s #%d: %s", __FILE__, __func__, __LINE__, "malloc failed"); | ||
91 | } | ||
92 | |||
93 | check->subchecks->subcheck = subcheck; | ||
94 | check->subchecks->next = NULL; | ||
95 | } else { | ||
96 | // Search for the end | ||
97 | tmp = (mp_subcheck_list *)calloc(1, sizeof(mp_subcheck_list)); | ||
98 | if (tmp == NULL) { | ||
99 | die(STATE_UNKNOWN, "%s - %s #%d: %s", __FILE__, __func__, __LINE__, "malloc failed"); | ||
100 | } | ||
101 | |||
102 | tmp->subcheck = subcheck; | ||
103 | tmp->next = check->subchecks; | ||
104 | |||
105 | check->subchecks = tmp; | ||
106 | } | ||
107 | |||
108 | return 0; | ||
109 | } | ||
110 | |||
111 | /* | ||
112 | * Add a mp_perfdata data point to a mp_subcheck object | ||
113 | */ | ||
114 | void mp_add_perfdata_to_subcheck(mp_subcheck check[static 1], const mp_perfdata perfData) { | ||
115 | if (check->perfdata == NULL) { | ||
116 | check->perfdata = pd_list_init(); | ||
117 | } | ||
118 | pd_list_append(check->perfdata, perfData); | ||
119 | } | ||
120 | |||
121 | /* | ||
122 | * Add a mp_subcheck object to another one. The seconde mp_subcheck (argument) is the lower in the | ||
123 | * hierarchy | ||
124 | */ | ||
125 | int mp_add_subcheck_to_subcheck(mp_subcheck check[static 1], mp_subcheck subcheck) { | ||
126 | if (subcheck.output == NULL) { | ||
127 | die(STATE_UNKNOWN, "%s - %s #%d: %s", __FILE__, __func__, __LINE__, | ||
128 | "Sub check output is NULL"); | ||
129 | } | ||
130 | |||
131 | mp_subcheck_list *tmp = NULL; | ||
132 | |||
133 | if (check->subchecks == NULL) { | ||
134 | check->subchecks = (mp_subcheck_list *)calloc(1, sizeof(mp_subcheck_list)); | ||
135 | if (check->subchecks == NULL) { | ||
136 | die(STATE_UNKNOWN, "%s - %s #%d: %s", __FILE__, __func__, __LINE__, "malloc failed"); | ||
137 | } | ||
138 | |||
139 | tmp = check->subchecks; | ||
140 | } else { | ||
141 | // Search for the end | ||
142 | tmp = check->subchecks; | ||
143 | |||
144 | while (tmp->next != NULL) { | ||
145 | tmp = tmp->next; | ||
146 | } | ||
147 | |||
148 | tmp->next = (mp_subcheck_list *)calloc(1, sizeof(mp_subcheck_list)); | ||
149 | if (tmp->next == NULL) { | ||
150 | die(STATE_UNKNOWN, "%s - %s #%d: %s", __FILE__, __func__, __LINE__, "malloc failed"); | ||
151 | } | ||
152 | |||
153 | tmp = tmp->next; | ||
154 | } | ||
155 | |||
156 | tmp->subcheck = subcheck; | ||
157 | |||
158 | return 0; | ||
159 | } | ||
160 | |||
161 | /* | ||
162 | * Add a manual summary to a mp_check object, effectively replacing | ||
163 | * the autogenerated one | ||
164 | */ | ||
165 | void mp_add_summary(mp_check check[static 1], char *summary) { check->summary = summary; } | ||
166 | |||
167 | /* | ||
168 | * Generate the summary string of a mp_check object based on it's subchecks | ||
169 | */ | ||
170 | char *get_subcheck_summary(mp_check check) { | ||
171 | mp_subcheck_list *subchecks = check.subchecks; | ||
172 | |||
173 | unsigned int ok = 0; | ||
174 | unsigned int warning = 0; | ||
175 | unsigned int critical = 0; | ||
176 | unsigned int unknown = 0; | ||
177 | while (subchecks != NULL) { | ||
178 | switch (subchecks->subcheck.state) { | ||
179 | case STATE_OK: | ||
180 | ok++; | ||
181 | break; | ||
182 | case STATE_WARNING: | ||
183 | warning++; | ||
184 | break; | ||
185 | case STATE_CRITICAL: | ||
186 | critical++; | ||
187 | break; | ||
188 | case STATE_UNKNOWN: | ||
189 | unknown++; | ||
190 | break; | ||
191 | default: | ||
192 | die(STATE_UNKNOWN, "Unknown state in get_subcheck_summary"); | ||
193 | } | ||
194 | subchecks = subchecks->next; | ||
195 | } | ||
196 | char *result = NULL; | ||
197 | asprintf(&result, "ok=%d, warning=%d, critical=%d, unknown=%d", ok, warning, critical, unknown); | ||
198 | return result; | ||
199 | } | ||
200 | |||
201 | mp_state_enum mp_compute_subcheck_state(const mp_subcheck subcheck) { | ||
202 | if (subcheck.evaluation_function == NULL) { | ||
203 | return mp_eval_subcheck_default(subcheck); | ||
204 | } | ||
205 | return subcheck.evaluation_function(subcheck); | ||
206 | } | ||
207 | |||
208 | /* | ||
209 | * Generate the result state of a mp_subcheck object based on its own state and its subchecks | ||
210 | * states | ||
211 | */ | ||
212 | mp_state_enum mp_eval_subcheck_default(mp_subcheck subcheck) { | ||
213 | if (subcheck.evaluation_function != NULL) { | ||
214 | return subcheck.evaluation_function(subcheck); | ||
215 | } | ||
216 | |||
217 | if (subcheck.state_set_explicitly) { | ||
218 | return subcheck.state; | ||
219 | } | ||
220 | |||
221 | mp_subcheck_list *scl = subcheck.subchecks; | ||
222 | |||
223 | if (scl == NULL) { | ||
224 | return subcheck.default_state; | ||
225 | } | ||
226 | |||
227 | mp_state_enum result = STATE_OK; | ||
228 | |||
229 | while (scl != NULL) { | ||
230 | result = max_state_alt(result, mp_compute_subcheck_state(scl->subcheck)); | ||
231 | scl = scl->next; | ||
232 | } | ||
233 | |||
234 | return result; | ||
235 | } | ||
236 | |||
237 | mp_state_enum mp_compute_check_state(const mp_check check) { | ||
238 | // just a safety check | ||
239 | if (check.evaluation_function == NULL) { | ||
240 | return mp_eval_check_default(check); | ||
241 | } | ||
242 | return check.evaluation_function(check); | ||
243 | } | ||
244 | |||
245 | /* | ||
246 | * Generate the result state of a mp_check object based on it's own state and it's subchecks states | ||
247 | */ | ||
248 | mp_state_enum mp_eval_check_default(const mp_check check) { | ||
249 | assert(check.subchecks != NULL); // a mp_check without subchecks is invalid, die here | ||
250 | |||
251 | mp_subcheck_list *scl = check.subchecks; | ||
252 | mp_state_enum result = STATE_OK; | ||
253 | |||
254 | while (scl != NULL) { | ||
255 | result = max_state_alt(result, mp_compute_subcheck_state(scl->subcheck)); | ||
256 | scl = scl->next; | ||
257 | } | ||
258 | |||
259 | return result; | ||
260 | } | ||
261 | |||
262 | /* | ||
263 | * Generate output string for a mp_check object | ||
264 | * Non static to be available for testing functions | ||
265 | */ | ||
266 | char *mp_fmt_output(mp_check check) { | ||
267 | char *result = NULL; | ||
268 | |||
269 | switch (output_format) { | ||
270 | case MP_FORMAT_MULTI_LINE: { | ||
271 | if (check.summary == NULL) { | ||
272 | check.summary = get_subcheck_summary(check); | ||
273 | } | ||
274 | |||
275 | asprintf(&result, "[%s] - %s", state_text(mp_compute_check_state(check)), check.summary); | ||
276 | |||
277 | mp_subcheck_list *subchecks = check.subchecks; | ||
278 | |||
279 | while (subchecks != NULL) { | ||
280 | if (level_of_detail == MP_DETAIL_ALL || | ||
281 | mp_compute_subcheck_state(subchecks->subcheck) != STATE_OK) { | ||
282 | asprintf(&result, "%s\n%s", result, | ||
283 | fmt_subcheck_output(MP_FORMAT_MULTI_LINE, subchecks->subcheck, 1)); | ||
284 | } | ||
285 | subchecks = subchecks->next; | ||
286 | } | ||
287 | |||
288 | char *pd_string = NULL; | ||
289 | subchecks = check.subchecks; | ||
290 | |||
291 | while (subchecks != NULL) { | ||
292 | if (pd_string == NULL) { | ||
293 | asprintf(&pd_string, "%s", fmt_subcheck_perfdata(subchecks->subcheck)); | ||
294 | } else { | ||
295 | asprintf(&pd_string, "%s %s", pd_string, | ||
296 | fmt_subcheck_perfdata(subchecks->subcheck)); | ||
297 | } | ||
298 | |||
299 | subchecks = subchecks->next; | ||
300 | } | ||
301 | |||
302 | if (pd_string != NULL && strlen(pd_string) > 0) { | ||
303 | asprintf(&result, "%s|%s", result, pd_string); | ||
304 | } | ||
305 | |||
306 | break; | ||
307 | } | ||
308 | case MP_FORMAT_TEST_JSON: { | ||
309 | cJSON *resultObject = cJSON_CreateObject(); | ||
310 | if (resultObject == NULL) { | ||
311 | die(STATE_UNKNOWN, "cJSON_CreateObject failed"); | ||
312 | } | ||
313 | |||
314 | cJSON *resultState = cJSON_CreateString(state_text(mp_compute_check_state(check))); | ||
315 | cJSON_AddItemToObject(resultObject, "state", resultState); | ||
316 | |||
317 | if (check.summary == NULL) { | ||
318 | check.summary = get_subcheck_summary(check); | ||
319 | } | ||
320 | |||
321 | cJSON *summary = cJSON_CreateString(check.summary); | ||
322 | cJSON_AddItemToObject(resultObject, "summary", summary); | ||
323 | |||
324 | if (check.subchecks != NULL) { | ||
325 | cJSON *subchecks = cJSON_CreateArray(); | ||
326 | |||
327 | mp_subcheck_list *sc = check.subchecks; | ||
328 | |||
329 | while (sc != NULL) { | ||
330 | cJSON *sc_json = json_serialize_subcheck(sc->subcheck); | ||
331 | cJSON_AddItemToArray(subchecks, sc_json); | ||
332 | sc = sc->next; | ||
333 | } | ||
334 | |||
335 | cJSON_AddItemToObject(resultObject, "checks", subchecks); | ||
336 | } | ||
337 | |||
338 | result = cJSON_PrintUnformatted(resultObject); | ||
339 | break; | ||
340 | } | ||
341 | default: | ||
342 | die(STATE_UNKNOWN, "Invalid format"); | ||
343 | } | ||
344 | |||
345 | return result; | ||
346 | } | ||
347 | |||
348 | /* | ||
349 | * Helper function to properly indent the output lines when using multiline | ||
350 | * formats | ||
351 | */ | ||
352 | static char *generate_indentation_string(unsigned int indentation) { | ||
353 | char *result = calloc(indentation + 1, sizeof(char)); | ||
354 | |||
355 | for (unsigned int i = 0; i < indentation; i++) { | ||
356 | result[i] = '\t'; | ||
357 | } | ||
358 | |||
359 | return result; | ||
360 | } | ||
361 | |||
362 | /* | ||
363 | * Helper function to generate the output string of mp_subcheck | ||
364 | */ | ||
365 | static inline char *fmt_subcheck_output(mp_output_format output_format, mp_subcheck check, | ||
366 | unsigned int indentation) { | ||
367 | char *result = NULL; | ||
368 | mp_subcheck_list *subchecks = NULL; | ||
369 | |||
370 | switch (output_format) { | ||
371 | case MP_FORMAT_MULTI_LINE: { | ||
372 | char *tmp_string = NULL; | ||
373 | if ((tmp_string = strchr(check.output, '\n')) != NULL) { | ||
374 | // This is a multiline string, put the correct indentation in before proceeding | ||
375 | char *intermediate_string = ""; | ||
376 | bool have_residual_chars = false; | ||
377 | |||
378 | while (tmp_string != NULL) { | ||
379 | *tmp_string = '\0'; | ||
380 | asprintf(&intermediate_string, "%s%s\n%s", intermediate_string, check.output, | ||
381 | generate_indentation_string( | ||
382 | indentation + 1)); // one more indentation to make it look better | ||
383 | |||
384 | if (*(tmp_string + 1) != '\0') { | ||
385 | check.output = tmp_string + 1; | ||
386 | have_residual_chars = true; | ||
387 | } else { | ||
388 | // Null after the \n, so this is the end | ||
389 | have_residual_chars = false; | ||
390 | break; | ||
391 | } | ||
392 | |||
393 | tmp_string = strchr(check.output, '\n'); | ||
394 | } | ||
395 | |||
396 | // add the rest (if any) | ||
397 | if (have_residual_chars) { | ||
398 | char *tmp = check.output; | ||
399 | xasprintf(&check.output, "%s\n%s%s", intermediate_string, | ||
400 | generate_indentation_string(indentation + 1), tmp); | ||
401 | } else { | ||
402 | check.output = intermediate_string; | ||
403 | } | ||
404 | } | ||
405 | asprintf(&result, "%s\\_[%s] - %s", generate_indentation_string(indentation), | ||
406 | state_text(mp_compute_subcheck_state(check)), check.output); | ||
407 | |||
408 | subchecks = check.subchecks; | ||
409 | |||
410 | while (subchecks != NULL) { | ||
411 | asprintf(&result, "%s\n%s", result, | ||
412 | fmt_subcheck_output(output_format, subchecks->subcheck, indentation + 1)); | ||
413 | subchecks = subchecks->next; | ||
414 | } | ||
415 | return result; | ||
416 | } | ||
417 | default: | ||
418 | die(STATE_UNKNOWN, "Invalid format"); | ||
419 | } | ||
420 | } | ||
421 | |||
422 | static inline cJSON *json_serialise_pd_value(mp_perfdata_value value) { | ||
423 | cJSON *result = cJSON_CreateObject(); | ||
424 | |||
425 | switch (value.type) { | ||
426 | case PD_TYPE_DOUBLE: | ||
427 | cJSON_AddStringToObject(result, "type", "double"); | ||
428 | break; | ||
429 | case PD_TYPE_INT: | ||
430 | cJSON_AddStringToObject(result, "type", "int"); | ||
431 | break; | ||
432 | case PD_TYPE_UINT: | ||
433 | cJSON_AddStringToObject(result, "type", "uint"); | ||
434 | break; | ||
435 | case PD_TYPE_NONE: | ||
436 | die(STATE_UNKNOWN, "Perfdata type was None in json_serialise_pd_value"); | ||
437 | } | ||
438 | cJSON_AddStringToObject(result, "value", pd_value_to_string(value)); | ||
439 | |||
440 | return result; | ||
441 | } | ||
442 | |||
443 | static inline cJSON *json_serialise_range(mp_range range) { | ||
444 | cJSON *result = cJSON_CreateObject(); | ||
445 | |||
446 | if (range.alert_on_inside_range) { | ||
447 | cJSON_AddBoolToObject(result, "alert_on_inside", true); | ||
448 | } else { | ||
449 | cJSON_AddBoolToObject(result, "alert_on_inside", false); | ||
450 | } | ||
451 | |||
452 | if (range.end_infinity) { | ||
453 | cJSON_AddStringToObject(result, "end", "inf"); | ||
454 | } else { | ||
455 | cJSON_AddItemToObject(result, "end", json_serialise_pd_value(range.end)); | ||
456 | } | ||
457 | |||
458 | if (range.start_infinity) { | ||
459 | cJSON_AddStringToObject(result, "start", "inf"); | ||
460 | } else { | ||
461 | cJSON_AddItemToObject(result, "start", json_serialise_pd_value(range.end)); | ||
462 | } | ||
463 | |||
464 | return result; | ||
465 | } | ||
466 | |||
467 | static inline cJSON *json_serialise_pd(mp_perfdata pd_val) { | ||
468 | cJSON *result = cJSON_CreateObject(); | ||
469 | |||
470 | // Label | ||
471 | cJSON_AddStringToObject(result, "label", pd_val.label); | ||
472 | |||
473 | // Value | ||
474 | cJSON_AddItemToObject(result, "value", json_serialise_pd_value(pd_val.value)); | ||
475 | |||
476 | // Uom | ||
477 | cJSON_AddStringToObject(result, "uom", pd_val.uom); | ||
478 | |||
479 | // Warn/Crit | ||
480 | if (pd_val.warn_present) { | ||
481 | cJSON *warn = json_serialise_range(pd_val.warn); | ||
482 | cJSON_AddItemToObject(result, "warn", warn); | ||
483 | } | ||
484 | if (pd_val.crit_present) { | ||
485 | cJSON *crit = json_serialise_range(pd_val.crit); | ||
486 | cJSON_AddItemToObject(result, "crit", crit); | ||
487 | } | ||
488 | |||
489 | if (pd_val.min_present) { | ||
490 | cJSON_AddItemToObject(result, "min", json_serialise_pd_value(pd_val.min)); | ||
491 | } | ||
492 | if (pd_val.max_present) { | ||
493 | cJSON_AddItemToObject(result, "max", json_serialise_pd_value(pd_val.max)); | ||
494 | } | ||
495 | |||
496 | return result; | ||
497 | } | ||
498 | |||
499 | static inline cJSON *json_serialise_pd_list(pd_list *list) { | ||
500 | cJSON *result = cJSON_CreateArray(); | ||
501 | |||
502 | do { | ||
503 | cJSON *pd_value = json_serialise_pd(list->data); | ||
504 | cJSON_AddItemToArray(result, pd_value); | ||
505 | list = list->next; | ||
506 | } while (list != NULL); | ||
507 | |||
508 | return result; | ||
509 | } | ||
510 | |||
511 | static inline cJSON *json_serialize_subcheck(mp_subcheck subcheck) { | ||
512 | cJSON *result = cJSON_CreateObject(); | ||
513 | |||
514 | // Human readable output | ||
515 | cJSON *output = cJSON_CreateString(subcheck.output); | ||
516 | cJSON_AddItemToObject(result, "output", output); | ||
517 | |||
518 | // Test state (aka Exit Code) | ||
519 | cJSON *state = cJSON_CreateString(state_text(mp_compute_subcheck_state(subcheck))); | ||
520 | cJSON_AddItemToObject(result, "state", state); | ||
521 | |||
522 | // Perfdata | ||
523 | if (subcheck.perfdata != NULL) { | ||
524 | cJSON *perfdata = json_serialise_pd_list(subcheck.perfdata); | ||
525 | cJSON_AddItemToObject(result, "perfdata", perfdata); | ||
526 | } | ||
527 | |||
528 | if (subcheck.subchecks != NULL) { | ||
529 | cJSON *subchecks = cJSON_CreateArray(); | ||
530 | |||
531 | mp_subcheck_list *sc = subcheck.subchecks; | ||
532 | |||
533 | while (sc != NULL) { | ||
534 | cJSON *sc_json = json_serialize_subcheck(sc->subcheck); | ||
535 | cJSON_AddItemToArray(subchecks, sc_json); | ||
536 | sc = sc->next; | ||
537 | } | ||
538 | |||
539 | cJSON_AddItemToObject(result, "checks", subchecks); | ||
540 | } | ||
541 | |||
542 | return result; | ||
543 | } | ||
544 | |||
545 | /* | ||
546 | * Wrapper function to print the output string of a mp_check object | ||
547 | * Use this in concrete plugins. | ||
548 | */ | ||
549 | void mp_print_output(mp_check check) { puts(mp_fmt_output(check)); } | ||
550 | |||
551 | /* | ||
552 | * Convenience function to print the output string of a mp_check object and exit | ||
553 | * the program with the resulting state. | ||
554 | * Intended to be used to exit a monitoring plugin. | ||
555 | */ | ||
556 | void mp_exit(mp_check check) { | ||
557 | mp_print_output(check); | ||
558 | if (output_format == MP_FORMAT_TEST_JSON) { | ||
559 | exit(0); | ||
560 | } | ||
561 | |||
562 | exit(mp_compute_check_state(check)); | ||
563 | } | ||
564 | |||
565 | /* | ||
566 | * Function to set the result state of a mp_subcheck object explicitly. | ||
567 | * This will overwrite the default state AND states derived from it's subchecks | ||
568 | */ | ||
569 | mp_subcheck mp_set_subcheck_state(mp_subcheck check, mp_state_enum state) { | ||
570 | check.state = state; | ||
571 | check.state_set_explicitly = true; | ||
572 | return check; | ||
573 | } | ||
574 | |||
575 | /* | ||
576 | * Function to set the default result state of a mp_subcheck object. This state | ||
577 | * will be used if neither an explicit state is set (see *mp_set_subcheck_state*) | ||
578 | * nor does it include other subchecks | ||
579 | */ | ||
580 | mp_subcheck mp_set_subcheck_default_state(mp_subcheck check, mp_state_enum state) { | ||
581 | check.default_state = state; | ||
582 | return check; | ||
583 | } | ||
584 | |||
585 | char *mp_output_format_map[] = { | ||
586 | [MP_FORMAT_MULTI_LINE] = "multi-line", | ||
587 | [MP_FORMAT_TEST_JSON] = "mp-test-json", | ||
588 | }; | ||
589 | |||
590 | /* | ||
591 | * Function to parse the output from a string | ||
592 | */ | ||
593 | parsed_output_format mp_parse_output_format(char *format_string) { | ||
594 | parsed_output_format result = { | ||
595 | .parsing_success = false, | ||
596 | .output_format = MP_FORMAT_DEFAULT, | ||
597 | }; | ||
598 | |||
599 | for (mp_output_format i = 0; i < (sizeof(mp_output_format_map) / sizeof(char *)); i++) { | ||
600 | if (strcasecmp(mp_output_format_map[i], format_string) == 0) { | ||
601 | result.parsing_success = true; | ||
602 | result.output_format = i; | ||
603 | break; | ||
604 | } | ||
605 | } | ||
606 | |||
607 | return result; | ||
608 | } | ||
609 | |||
610 | void mp_set_format(mp_output_format format) { output_format = format; } | ||
611 | |||
612 | mp_output_format mp_get_format(void) { return output_format; } | ||
613 | |||
614 | void mp_set_level_of_detail(mp_output_detail_level level) { level_of_detail = level; } | ||
615 | |||
616 | mp_output_detail_level mp_get_level_of_detail(void) { return level_of_detail; } | ||
617 | |||
618 | mp_state_enum mp_eval_ok(mp_check overall) { | ||
619 | (void)overall; | ||
620 | return STATE_OK; | ||
621 | } | ||
622 | |||
623 | mp_state_enum mp_eval_warning(mp_check overall) { | ||
624 | (void)overall; | ||
625 | return STATE_WARNING; | ||
626 | } | ||
627 | |||
628 | mp_state_enum mp_eval_critical(mp_check overall) { | ||
629 | (void)overall; | ||
630 | return STATE_CRITICAL; | ||
631 | } | ||
632 | |||
633 | mp_state_enum mp_eval_unknown(mp_check overall) { | ||
634 | (void)overall; | ||
635 | return STATE_UNKNOWN; | ||
636 | } | ||
diff --git a/lib/output.h b/lib/output.h new file mode 100644 index 00000000..c63c8e3f --- /dev/null +++ b/lib/output.h | |||
@@ -0,0 +1,117 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../config.h" | ||
4 | #include "./perfdata.h" | ||
5 | #include "./states.h" | ||
6 | |||
7 | /* | ||
8 | * A partial check result | ||
9 | */ | ||
10 | typedef struct mp_subcheck mp_subcheck; | ||
11 | struct mp_subcheck { | ||
12 | mp_state_enum state; // OK, Warning, Critical ... set explicitly | ||
13 | mp_state_enum default_state; // OK, Warning, Critical .. if not set explicitly | ||
14 | bool state_set_explicitly; // was the state set explicitly (or should it be derived from | ||
15 | // subchecks) | ||
16 | |||
17 | char *output; // Text output for humans ("Filesystem xyz is fine", "Could not create TCP | ||
18 | // connection to..") | ||
19 | pd_list *perfdata; // Performance data for this check | ||
20 | struct subcheck_list *subchecks; // subchecks deeper in the hierarchy | ||
21 | |||
22 | // the evaluation_functions computes the state of subcheck | ||
23 | mp_state_enum (*evaluation_function)(mp_subcheck); | ||
24 | }; | ||
25 | |||
26 | /* | ||
27 | * A list of subchecks, used in subchecks and the main check | ||
28 | */ | ||
29 | typedef struct subcheck_list { | ||
30 | mp_subcheck subcheck; | ||
31 | struct subcheck_list *next; | ||
32 | } mp_subcheck_list; | ||
33 | |||
34 | /* | ||
35 | * Possible output formats | ||
36 | */ | ||
37 | typedef enum output_format { | ||
38 | MP_FORMAT_MULTI_LINE, | ||
39 | MP_FORMAT_TEST_JSON, | ||
40 | } mp_output_format; | ||
41 | |||
42 | #define MP_FORMAT_DEFAULT MP_FORMAT_MULTI_LINE | ||
43 | |||
44 | /* | ||
45 | * Format related functions | ||
46 | */ | ||
47 | void mp_set_format(mp_output_format format); | ||
48 | mp_output_format mp_get_format(void); | ||
49 | |||
50 | // Output detail level | ||
51 | |||
52 | typedef enum output_detail_level { | ||
53 | MP_DETAIL_ALL, | ||
54 | MP_DETAIL_NON_OK_ONLY, | ||
55 | } mp_output_detail_level; | ||
56 | |||
57 | void mp_set_level_of_detail(mp_output_detail_level level); | ||
58 | mp_output_detail_level mp_get_level_of_detail(void); | ||
59 | |||
60 | /* | ||
61 | * The main state object of a plugin. Exists only ONCE per plugin. | ||
62 | * This is the "root" of a tree of singular checks. | ||
63 | * The final result is always derived from the children and the "worst" state | ||
64 | * in the first layer of subchecks | ||
65 | */ | ||
66 | typedef struct mp_check mp_check; | ||
67 | struct mp_check { | ||
68 | char *summary; // Overall summary, if not set a summary will be automatically generated | ||
69 | mp_subcheck_list *subchecks; | ||
70 | |||
71 | // the evaluation_functions computes the state of check | ||
72 | mp_state_enum (*evaluation_function)(mp_check); | ||
73 | }; | ||
74 | |||
75 | mp_check mp_check_init(void); | ||
76 | mp_subcheck mp_subcheck_init(void); | ||
77 | |||
78 | mp_subcheck mp_set_subcheck_state(mp_subcheck, mp_state_enum); | ||
79 | mp_subcheck mp_set_subcheck_default_state(mp_subcheck, mp_state_enum); | ||
80 | |||
81 | int mp_add_subcheck_to_check(mp_check check[static 1], mp_subcheck); | ||
82 | int mp_add_subcheck_to_subcheck(mp_subcheck check[static 1], mp_subcheck); | ||
83 | |||
84 | void mp_add_perfdata_to_subcheck(mp_subcheck check[static 1], mp_perfdata); | ||
85 | |||
86 | void mp_add_summary(mp_check check[static 1], char *summary); | ||
87 | |||
88 | mp_state_enum mp_compute_check_state(mp_check); | ||
89 | mp_state_enum mp_compute_subcheck_state(mp_subcheck); | ||
90 | |||
91 | mp_state_enum mp_eval_ok(mp_check overall); | ||
92 | mp_state_enum mp_eval_warning(mp_check overall); | ||
93 | mp_state_enum mp_eval_critical(mp_check overall); | ||
94 | mp_state_enum mp_eval_unknown(mp_check overall); | ||
95 | mp_state_enum mp_eval_check_default(mp_check check); | ||
96 | mp_state_enum mp_eval_subcheck_default(mp_subcheck subcheck); | ||
97 | |||
98 | typedef struct { | ||
99 | bool parsing_success; | ||
100 | mp_output_format output_format; | ||
101 | } parsed_output_format; | ||
102 | parsed_output_format mp_parse_output_format(char *format_string); | ||
103 | |||
104 | // TODO free and stuff | ||
105 | // void mp_cleanup_check(mp_check check[static 1]); | ||
106 | |||
107 | char *mp_fmt_output(mp_check); | ||
108 | |||
109 | void mp_print_output(mp_check); | ||
110 | |||
111 | /* | ||
112 | * ================== | ||
113 | * Exit functionality | ||
114 | * ================== | ||
115 | */ | ||
116 | |||
117 | void mp_exit(mp_check) __attribute__((noreturn)); | ||
diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 1289aae2..db337622 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c | |||
@@ -40,26 +40,29 @@ typedef struct { | |||
40 | char *stanza; | 40 | char *stanza; |
41 | } np_ini_info; | 41 | } np_ini_info; |
42 | 42 | ||
43 | static char *default_ini_file_names[] = {"monitoring-plugins.ini", "plugins.ini", "nagios-plugins.ini", NULL}; | 43 | static char *default_ini_file_names[] = {"monitoring-plugins.ini", "plugins.ini", |
44 | "nagios-plugins.ini", NULL}; | ||
44 | 45 | ||
45 | static char *default_ini_path_names[] = { | 46 | static char *default_ini_path_names[] = { |
46 | "/usr/local/etc/monitoring-plugins/monitoring-plugins.ini", "/usr/local/etc/monitoring-plugins.ini", | 47 | "/usr/local/etc/monitoring-plugins/monitoring-plugins.ini", |
47 | "/etc/monitoring-plugins/monitoring-plugins.ini", "/etc/monitoring-plugins.ini", | 48 | "/usr/local/etc/monitoring-plugins.ini", "/etc/monitoring-plugins/monitoring-plugins.ini", |
49 | "/etc/monitoring-plugins.ini", | ||
48 | /* deprecated path names (for backward compatibility): */ | 50 | /* deprecated path names (for backward compatibility): */ |
49 | "/etc/nagios/plugins.ini", "/usr/local/nagios/etc/plugins.ini", "/usr/local/etc/nagios/plugins.ini", "/etc/opt/nagios/plugins.ini", | 51 | "/etc/nagios/plugins.ini", "/usr/local/nagios/etc/plugins.ini", |
50 | "/etc/nagios-plugins.ini", "/usr/local/etc/nagios-plugins.ini", "/etc/opt/nagios-plugins.ini", NULL}; | 52 | "/usr/local/etc/nagios/plugins.ini", "/etc/opt/nagios/plugins.ini", "/etc/nagios-plugins.ini", |
53 | "/usr/local/etc/nagios-plugins.ini", "/etc/opt/nagios-plugins.ini", NULL}; | ||
51 | 54 | ||
52 | /* eat all characters from a FILE pointer until n is encountered */ | 55 | /* eat all characters from a FILE pointer until n is encountered */ |
53 | #define GOBBLE_TO(f, c, n) \ | 56 | #define GOBBLE_TO(f, c, n) \ |
54 | do { \ | 57 | do { \ |
55 | (c) = fgetc((f)); \ | 58 | (c) = fgetc((f)); \ |
56 | } while ((c) != EOF && (c) != (n)) | 59 | } while ((c) != EOF && (c) != (n)) |
57 | 60 | ||
58 | /* internal function that returns the constructed defaults options */ | 61 | /* internal function that returns the constructed defaults options */ |
59 | static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); | 62 | static bool read_defaults(FILE *defaults_file, const char *stanza, np_arg_list **opts); |
60 | 63 | ||
61 | /* internal function that converts a single line into options format */ | 64 | /* internal function that converts a single line into options format */ |
62 | static int add_option(FILE *f, np_arg_list **optlst); | 65 | static int add_option(FILE *filePointer, np_arg_list **optlst); |
63 | 66 | ||
64 | /* internal functions to find default file */ | 67 | /* internal functions to find default file */ |
65 | static char *default_file(void); | 68 | static char *default_file(void); |
@@ -71,7 +74,8 @@ static char *default_file_in_path(void); | |||
71 | * into its separate parts. | 74 | * into its separate parts. |
72 | */ | 75 | */ |
73 | static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) { | 76 | static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) { |
74 | size_t locator_len = 0, stanza_len = 0; | 77 | size_t locator_len = 0; |
78 | size_t stanza_len = 0; | ||
75 | 79 | ||
76 | /* if locator is NULL we'll use default values */ | 80 | /* if locator is NULL we'll use default values */ |
77 | if (locator != NULL) { | 81 | if (locator != NULL) { |
@@ -87,8 +91,9 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in | |||
87 | i->stanza = strdup(def_stanza); | 91 | i->stanza = strdup(def_stanza); |
88 | } | 92 | } |
89 | 93 | ||
90 | if (i->stanza == NULL) | 94 | if (i->stanza == NULL) { |
91 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | 95 | die(STATE_UNKNOWN, _("malloc() failed!\n")); |
96 | } | ||
92 | 97 | ||
93 | /* check whether there's an @file part */ | 98 | /* check whether there's an @file part */ |
94 | if (stanza_len == locator_len) { | 99 | if (stanza_len == locator_len) { |
@@ -99,39 +104,46 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in | |||
99 | i->file_string_on_heap = true; | 104 | i->file_string_on_heap = true; |
100 | } | 105 | } |
101 | 106 | ||
102 | if (i->file == NULL || i->file[0] == '\0') | 107 | if (i->file == NULL || i->file[0] == '\0') { |
103 | die(STATE_UNKNOWN, _("Cannot find config file in any standard location.\n")); | 108 | die(STATE_UNKNOWN, _("Cannot find config file in any standard location.\n")); |
109 | } | ||
104 | } | 110 | } |
105 | 111 | ||
106 | /* | 112 | /* |
107 | * This is the externally visible function used by extra_opts. | 113 | * This is the externally visible function used by extra_opts. |
108 | */ | 114 | */ |
109 | np_arg_list *np_get_defaults(const char *locator, const char *default_section) { | 115 | np_arg_list *np_get_defaults(const char *locator, const char *default_section) { |
110 | FILE *inifile = NULL; | ||
111 | np_arg_list *defaults = NULL; | ||
112 | np_ini_info i; | ||
113 | int is_suid_plugin = mp_suid(); | 116 | int is_suid_plugin = mp_suid(); |
114 | 117 | ||
115 | if (is_suid_plugin && idpriv_temp_drop() == -1) | 118 | if (is_suid_plugin && idpriv_temp_drop() == -1) { |
116 | die(STATE_UNKNOWN, _("Cannot drop privileges: %s\n"), strerror(errno)); | 119 | die(STATE_UNKNOWN, _("Cannot drop privileges: %s\n"), strerror(errno)); |
120 | } | ||
117 | 121 | ||
118 | parse_locator(locator, default_section, &i); | 122 | FILE *inifile = NULL; |
119 | inifile = strcmp(i.file, "-") == 0 ? stdin : fopen(i.file, "r"); | 123 | np_ini_info ini_info; |
124 | parse_locator(locator, default_section, &ini_info); | ||
125 | inifile = strcmp(ini_info.file, "-") == 0 ? stdin : fopen(ini_info.file, "r"); | ||
120 | 126 | ||
121 | if (inifile == NULL) | 127 | if (inifile == NULL) { |
122 | die(STATE_UNKNOWN, _("Can't read config file: %s\n"), strerror(errno)); | 128 | die(STATE_UNKNOWN, _("Can't read config file: %s\n"), strerror(errno)); |
123 | if (!read_defaults(inifile, i.stanza, &defaults)) | 129 | } |
124 | die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file); | ||
125 | 130 | ||
126 | if (i.file_string_on_heap) { | 131 | np_arg_list *defaults = NULL; |
127 | free(i.file); | 132 | if (!read_defaults(inifile, ini_info.stanza, &defaults)) { |
133 | die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), ini_info.stanza, ini_info.file); | ||
134 | } | ||
135 | |||
136 | if (ini_info.file_string_on_heap) { | ||
137 | free(ini_info.file); | ||
128 | } | 138 | } |
129 | 139 | ||
130 | if (inifile != stdin) | 140 | if (inifile != stdin) { |
131 | fclose(inifile); | 141 | fclose(inifile); |
132 | free(i.stanza); | 142 | } |
133 | if (is_suid_plugin && idpriv_temp_restore() == -1) | 143 | free(ini_info.stanza); |
144 | if (is_suid_plugin && idpriv_temp_restore() == -1) { | ||
134 | die(STATE_UNKNOWN, _("Cannot restore privileges: %s\n"), strerror(errno)); | 145 | die(STATE_UNKNOWN, _("Cannot restore privileges: %s\n"), strerror(errno)); |
146 | } | ||
135 | 147 | ||
136 | return defaults; | 148 | return defaults; |
137 | } | 149 | } |
@@ -143,54 +155,58 @@ np_arg_list *np_get_defaults(const char *locator, const char *default_section) { | |||
143 | * be extra careful about user-supplied input (i.e. avoiding possible | 155 | * be extra careful about user-supplied input (i.e. avoiding possible |
144 | * format string vulnerabilities, etc). | 156 | * format string vulnerabilities, etc). |
145 | */ | 157 | */ |
146 | static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts) { | 158 | static bool read_defaults(FILE *defaults_file, const char *stanza, np_arg_list **opts) { |
147 | int c = 0; | ||
148 | bool status = false; | 159 | bool status = false; |
149 | size_t i, stanza_len; | ||
150 | enum { | 160 | enum { |
151 | NOSTANZA, | 161 | NOSTANZA, |
152 | WRONGSTANZA, | 162 | WRONGSTANZA, |
153 | RIGHTSTANZA | 163 | RIGHTSTANZA |
154 | } stanzastate = NOSTANZA; | 164 | } stanzastate = NOSTANZA; |
155 | 165 | ||
156 | stanza_len = strlen(stanza); | 166 | size_t stanza_len = strlen(stanza); |
157 | 167 | ||
158 | /* our little stanza-parsing state machine */ | 168 | /* our little stanza-parsing state machine */ |
159 | while ((c = fgetc(f)) != EOF) { | 169 | int current_char = 0; |
170 | while ((current_char = fgetc(defaults_file)) != EOF) { | ||
160 | /* gobble up leading whitespace */ | 171 | /* gobble up leading whitespace */ |
161 | if (isspace(c)) | 172 | if (isspace(current_char)) { |
162 | continue; | 173 | continue; |
163 | switch (c) { | 174 | } |
175 | switch (current_char) { | ||
164 | /* globble up comment lines */ | 176 | /* globble up comment lines */ |
165 | case ';': | 177 | case ';': |
166 | case '#': | 178 | case '#': |
167 | GOBBLE_TO(f, c, '\n'); | 179 | GOBBLE_TO(defaults_file, current_char, '\n'); |
168 | break; | 180 | break; |
169 | /* start of a stanza, check to see if it matches */ | 181 | /* start of a stanza, check to see if it matches */ |
170 | case '[': | 182 | case '[': { |
171 | stanzastate = WRONGSTANZA; | 183 | stanzastate = WRONGSTANZA; |
184 | size_t i; | ||
172 | for (i = 0; i < stanza_len; i++) { | 185 | for (i = 0; i < stanza_len; i++) { |
173 | c = fgetc(f); | 186 | current_char = fgetc(defaults_file); |
174 | /* strip leading whitespace */ | 187 | /* strip leading whitespace */ |
175 | if (i == 0) | 188 | if (i == 0) { |
176 | for (; isspace(c); c = fgetc(f)) | 189 | for (; isspace(current_char); current_char = fgetc(defaults_file)) { |
177 | continue; | 190 | } |
191 | } | ||
178 | /* nope, read to the end of the line */ | 192 | /* nope, read to the end of the line */ |
179 | if (c != stanza[i]) { | 193 | if (current_char != stanza[i]) { |
180 | GOBBLE_TO(f, c, '\n'); | 194 | GOBBLE_TO(defaults_file, current_char, '\n'); |
181 | break; | 195 | break; |
182 | } | 196 | } |
183 | } | 197 | } |
198 | |||
184 | /* if it matched up to here and the next char is ']'... */ | 199 | /* if it matched up to here and the next char is ']'... */ |
185 | if (i == stanza_len) { | 200 | if (i == stanza_len) { |
186 | c = fgetc(f); | 201 | current_char = fgetc(defaults_file); |
187 | /* strip trailing whitespace */ | 202 | /* strip trailing whitespace */ |
188 | for (; isspace(c); c = fgetc(f)) | 203 | for (; isspace(current_char); current_char = fgetc(defaults_file)) { |
189 | continue; | 204 | } |
190 | if (c == ']') | 205 | if (current_char == ']') { |
191 | stanzastate = RIGHTSTANZA; | 206 | stanzastate = RIGHTSTANZA; |
207 | } | ||
192 | } | 208 | } |
193 | break; | 209 | } break; |
194 | /* otherwise, we're in the body of a stanza or a parse error */ | 210 | /* otherwise, we're in the body of a stanza or a parse error */ |
195 | default: | 211 | default: |
196 | switch (stanzastate) { | 212 | switch (stanzastate) { |
@@ -201,12 +217,12 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts) { | |||
201 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); | 217 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); |
202 | /* we're in a stanza, but for a different plugin */ | 218 | /* we're in a stanza, but for a different plugin */ |
203 | case WRONGSTANZA: | 219 | case WRONGSTANZA: |
204 | GOBBLE_TO(f, c, '\n'); | 220 | GOBBLE_TO(defaults_file, current_char, '\n'); |
205 | break; | 221 | break; |
206 | /* okay, this is where we start taking the config */ | 222 | /* okay, this is where we start taking the config */ |
207 | case RIGHTSTANZA: | 223 | case RIGHTSTANZA: |
208 | ungetc(c, f); | 224 | ungetc(current_char, defaults_file); |
209 | if (add_option(f, opts)) { | 225 | if (add_option(defaults_file, opts)) { |
210 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); | 226 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); |
211 | } | 227 | } |
212 | status = true; | 228 | status = true; |
@@ -225,13 +241,12 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts) { | |||
225 | * --option[=value] | 241 | * --option[=value] |
226 | * appending it to the linked list optbuf. | 242 | * appending it to the linked list optbuf. |
227 | */ | 243 | */ |
228 | static int add_option(FILE *f, np_arg_list **optlst) { | 244 | static int add_option(FILE *filePointer, np_arg_list **optlst) { |
229 | np_arg_list *opttmp = *optlst, *optnew; | 245 | char *linebuf = NULL; |
230 | char *linebuf = NULL, *lineend = NULL, *optptr = NULL, *optend = NULL; | 246 | bool done_reading = false; |
231 | char *eqptr = NULL, *valptr = NULL, *valend = NULL; | 247 | const size_t read_sz = 8; |
232 | short done_reading = 0, equals = 0, value = 0; | 248 | size_t linebuf_sz = 0; |
233 | size_t cfg_len = 0, read_sz = 8, linebuf_sz = 0, read_pos = 0; | 249 | size_t read_pos = 0; |
234 | size_t opt_len = 0, val_len = 0; | ||
235 | 250 | ||
236 | /* read one line from the file */ | 251 | /* read one line from the file */ |
237 | while (!done_reading) { | 252 | while (!done_reading) { |
@@ -239,74 +254,101 @@ static int add_option(FILE *f, np_arg_list **optlst) { | |||
239 | if (linebuf == NULL || read_pos + read_sz >= linebuf_sz) { | 254 | if (linebuf == NULL || read_pos + read_sz >= linebuf_sz) { |
240 | linebuf_sz = linebuf_sz > 0 ? linebuf_sz << 1 : read_sz; | 255 | linebuf_sz = linebuf_sz > 0 ? linebuf_sz << 1 : read_sz; |
241 | linebuf = realloc(linebuf, linebuf_sz); | 256 | linebuf = realloc(linebuf, linebuf_sz); |
242 | if (linebuf == NULL) | 257 | if (linebuf == NULL) { |
243 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | 258 | die(STATE_UNKNOWN, _("malloc() failed!\n")); |
259 | } | ||
244 | } | 260 | } |
245 | if (fgets(&linebuf[read_pos], (int)read_sz, f) == NULL) | 261 | |
246 | done_reading = 1; | 262 | if (fgets(&linebuf[read_pos], (int)read_sz, filePointer) == NULL) { |
247 | else { | 263 | done_reading = true; |
264 | } else { | ||
248 | read_pos = strlen(linebuf); | 265 | read_pos = strlen(linebuf); |
249 | if (linebuf[read_pos - 1] == '\n') { | 266 | if (linebuf[read_pos - 1] == '\n') { |
250 | linebuf[--read_pos] = '\0'; | 267 | linebuf[--read_pos] = '\0'; |
251 | done_reading = 1; | 268 | done_reading = true; |
252 | } | 269 | } |
253 | } | 270 | } |
254 | } | 271 | } |
255 | lineend = &linebuf[read_pos]; | 272 | |
273 | char *lineend = &linebuf[read_pos]; | ||
256 | /* all that to read one line, isn't C fun? :) now comes the parsing :/ */ | 274 | /* all that to read one line, isn't C fun? :) now comes the parsing :/ */ |
257 | 275 | ||
258 | /* skip leading whitespace */ | 276 | /* skip leading whitespace */ |
259 | for (optptr = linebuf; optptr < lineend && isspace(*optptr); optptr++) | 277 | char *optptr = NULL; |
260 | continue; | 278 | for (optptr = linebuf; optptr < lineend && isspace(*optptr); optptr++) { |
279 | } | ||
280 | |||
261 | /* continue to '=' or EOL, watching for spaces that might precede it */ | 281 | /* continue to '=' or EOL, watching for spaces that might precede it */ |
282 | char *eqptr = NULL; | ||
283 | char *optend = NULL; | ||
262 | for (eqptr = optptr; eqptr < lineend && *eqptr != '='; eqptr++) { | 284 | for (eqptr = optptr; eqptr < lineend && *eqptr != '='; eqptr++) { |
263 | if (isspace(*eqptr) && optend == NULL) | 285 | if (isspace(*eqptr) && optend == NULL) { |
264 | optend = eqptr; | 286 | optend = eqptr; |
265 | else | 287 | } else { |
266 | optend = NULL; | 288 | optend = NULL; |
289 | } | ||
267 | } | 290 | } |
268 | if (optend == NULL) | 291 | |
292 | if (optend == NULL) { | ||
269 | optend = eqptr; | 293 | optend = eqptr; |
294 | } | ||
295 | |||
270 | --optend; | 296 | --optend; |
297 | |||
271 | /* ^[[:space:]]*=foo is a syntax error */ | 298 | /* ^[[:space:]]*=foo is a syntax error */ |
272 | if (optptr == eqptr) | 299 | if (optptr == eqptr) { |
273 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); | 300 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); |
301 | } | ||
302 | |||
274 | /* continue from '=' to start of value or EOL */ | 303 | /* continue from '=' to start of value or EOL */ |
275 | for (valptr = eqptr + 1; valptr < lineend && isspace(*valptr); valptr++) | 304 | char *valptr = NULL; |
276 | continue; | 305 | for (valptr = eqptr + 1; valptr < lineend && isspace(*valptr); valptr++) { |
306 | } | ||
307 | |||
277 | /* continue to the end of value */ | 308 | /* continue to the end of value */ |
278 | for (valend = valptr; valend < lineend; valend++) | 309 | char *valend = NULL; |
279 | continue; | 310 | for (valend = valptr; valend < lineend; valend++) { |
311 | } | ||
312 | |||
280 | --valend; | 313 | --valend; |
314 | |||
281 | /* finally trim off trailing spaces */ | 315 | /* finally trim off trailing spaces */ |
282 | for (; isspace(*valend); valend--) | 316 | for (; isspace(*valend); valend--) { |
283 | continue; | 317 | } |
318 | |||
284 | /* calculate the length of "--foo" */ | 319 | /* calculate the length of "--foo" */ |
285 | opt_len = (size_t)(1 + optend - optptr); | 320 | size_t opt_len = (size_t)(1 + optend - optptr); |
286 | /* 1-character params needs only one dash */ | 321 | /* 1-character params needs only one dash */ |
287 | if (opt_len == 1) | 322 | size_t cfg_len = 0; |
323 | if (opt_len == 1) { | ||
288 | cfg_len = 1 + (opt_len); | 324 | cfg_len = 1 + (opt_len); |
289 | else | 325 | } else { |
290 | cfg_len = 2 + (opt_len); | 326 | cfg_len = 2 + (opt_len); |
327 | } | ||
328 | |||
329 | size_t val_len = 0; | ||
330 | bool equals = false; | ||
331 | bool value = false; | ||
291 | /* if valptr<lineend then we have to also allocate space for "=bar" */ | 332 | /* if valptr<lineend then we have to also allocate space for "=bar" */ |
292 | if (valptr < lineend) { | 333 | if (valptr < lineend) { |
293 | equals = value = 1; | 334 | equals = value = true; |
294 | val_len = (size_t)(1 + valend - valptr); | 335 | val_len = (size_t)(1 + valend - valptr); |
295 | cfg_len += 1 + val_len; | 336 | cfg_len += 1 + val_len; |
296 | } | 337 | } else if (valptr == lineend) { |
297 | /* if valptr==valend then we have "=" but no "bar" */ | 338 | /* if valptr==valend then we have "=" but no "bar" */ |
298 | else if (valptr == lineend) { | 339 | equals = true; |
299 | equals = 1; | ||
300 | cfg_len += 1; | 340 | cfg_len += 1; |
301 | } | 341 | } |
342 | |||
302 | /* a line with no equal sign isn't valid */ | 343 | /* a line with no equal sign isn't valid */ |
303 | if (equals == 0) | 344 | if (!equals) { |
304 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); | 345 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); |
346 | } | ||
305 | 347 | ||
306 | /* okay, now we have all the info we need, so we create a new np_arg_list | 348 | /* okay, now we have all the info we need, so we create a new np_arg_list |
307 | * element and set the argument... | 349 | * element and set the argument... |
308 | */ | 350 | */ |
309 | optnew = malloc(sizeof(np_arg_list)); | 351 | np_arg_list *optnew = malloc(sizeof(np_arg_list)); |
310 | optnew->next = NULL; | 352 | optnew->next = NULL; |
311 | 353 | ||
312 | read_pos = 0; | 354 | read_pos = 0; |
@@ -329,11 +371,13 @@ static int add_option(FILE *f, np_arg_list **optlst) { | |||
329 | optnew->arg[read_pos] = '\0'; | 371 | optnew->arg[read_pos] = '\0'; |
330 | 372 | ||
331 | /* ...and put that to the end of the list */ | 373 | /* ...and put that to the end of the list */ |
332 | if (*optlst == NULL) | 374 | if (*optlst == NULL) { |
333 | *optlst = optnew; | 375 | *optlst = optnew; |
334 | else { | 376 | } else { |
335 | while (opttmp->next != NULL) | 377 | np_arg_list *opttmp = *optlst; |
378 | while (opttmp->next != NULL) { | ||
336 | opttmp = opttmp->next; | 379 | opttmp = opttmp->next; |
380 | } | ||
337 | opttmp->next = optnew; | 381 | opttmp->next = optnew; |
338 | } | 382 | } |
339 | 383 | ||
@@ -344,7 +388,8 @@ static int add_option(FILE *f, np_arg_list **optlst) { | |||
344 | static char *default_file(void) { | 388 | static char *default_file(void) { |
345 | char *ini_file; | 389 | char *ini_file; |
346 | 390 | ||
347 | if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || (ini_file = default_file_in_path()) != NULL) { | 391 | if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || |
392 | (ini_file = default_file_in_path()) != NULL) { | ||
348 | return ini_file; | 393 | return ini_file; |
349 | } | 394 | } |
350 | 395 | ||
@@ -357,19 +402,25 @@ static char *default_file(void) { | |||
357 | } | 402 | } |
358 | 403 | ||
359 | static char *default_file_in_path(void) { | 404 | static char *default_file_in_path(void) { |
360 | char *config_path, **file; | 405 | char *config_path; |
361 | char *dir, *ini_file, *tokens; | 406 | char **file; |
407 | char *dir; | ||
408 | char *ini_file; | ||
409 | char *tokens; | ||
362 | 410 | ||
363 | if ((config_path = getenv("NAGIOS_CONFIG_PATH")) == NULL) | 411 | if ((config_path = getenv("NAGIOS_CONFIG_PATH")) == NULL) { |
364 | return NULL; | 412 | return NULL; |
413 | } | ||
365 | /* shall we spit out a warning that NAGIOS_CONFIG_PATH is deprecated? */ | 414 | /* shall we spit out a warning that NAGIOS_CONFIG_PATH is deprecated? */ |
366 | 415 | ||
367 | if ((tokens = strdup(config_path)) == NULL) | 416 | if ((tokens = strdup(config_path)) == NULL) { |
368 | die(STATE_UNKNOWN, "%s\n", _("Insufficient Memory")); | 417 | die(STATE_UNKNOWN, "%s\n", _("Insufficient Memory")); |
418 | } | ||
369 | for (dir = strtok(tokens, ":"); dir != NULL; dir = strtok(NULL, ":")) { | 419 | for (dir = strtok(tokens, ":"); dir != NULL; dir = strtok(NULL, ":")) { |
370 | for (file = default_ini_file_names; *file != NULL; file++) { | 420 | for (file = default_ini_file_names; *file != NULL; file++) { |
371 | if ((asprintf(&ini_file, "%s/%s", dir, *file)) < 0) | 421 | if ((asprintf(&ini_file, "%s/%s", dir, *file)) < 0) { |
372 | die(STATE_UNKNOWN, "%s\n", _("Insufficient Memory")); | 422 | die(STATE_UNKNOWN, "%s\n", _("Insufficient Memory")); |
423 | } | ||
373 | if (access(ini_file, F_OK) == 0) { | 424 | if (access(ini_file, F_OK) == 0) { |
374 | free(tokens); | 425 | free(tokens); |
375 | return ini_file; | 426 | return ini_file; |
diff --git a/lib/perfdata.c b/lib/perfdata.c new file mode 100644 index 00000000..2930a8bc --- /dev/null +++ b/lib/perfdata.c | |||
@@ -0,0 +1,649 @@ | |||
1 | #include "./perfdata.h" | ||
2 | #include "../plugins/common.h" | ||
3 | #include "../plugins/utils.h" | ||
4 | #include "utils_base.h" | ||
5 | |||
6 | #include <assert.h> | ||
7 | #include <limits.h> | ||
8 | #include <stdlib.h> | ||
9 | |||
10 | char *pd_value_to_string(const mp_perfdata_value pd) { | ||
11 | char *result = NULL; | ||
12 | |||
13 | assert(pd.type != PD_TYPE_NONE); | ||
14 | |||
15 | switch (pd.type) { | ||
16 | case PD_TYPE_INT: | ||
17 | asprintf(&result, "%lli", pd.pd_int); | ||
18 | break; | ||
19 | case PD_TYPE_UINT: | ||
20 | asprintf(&result, "%llu", pd.pd_int); | ||
21 | break; | ||
22 | case PD_TYPE_DOUBLE: | ||
23 | asprintf(&result, "%f", pd.pd_double); | ||
24 | break; | ||
25 | default: | ||
26 | // die here | ||
27 | die(STATE_UNKNOWN, "Invalid mp_perfdata mode\n"); | ||
28 | } | ||
29 | |||
30 | return result; | ||
31 | } | ||
32 | |||
33 | char *pd_to_string(mp_perfdata pd) { | ||
34 | assert(pd.label != NULL); | ||
35 | char *result = NULL; | ||
36 | |||
37 | if (strchr(pd.label, '\'') == NULL) { | ||
38 | asprintf(&result, "'%s'=", pd.label); | ||
39 | } else { | ||
40 | // we have a illegal single quote in the string | ||
41 | // replace it silently instead of complaining | ||
42 | for (char *ptr = pd.label; *ptr == '\0'; ptr++) { | ||
43 | if (*ptr == '\'') { | ||
44 | *ptr = '_'; | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | asprintf(&result, "%s%s", result, pd_value_to_string(pd.value)); | ||
50 | |||
51 | if (pd.uom != NULL) { | ||
52 | asprintf(&result, "%s%s", result, pd.uom); | ||
53 | } | ||
54 | |||
55 | if (pd.warn_present) { | ||
56 | asprintf(&result, "%s;%s", result, mp_range_to_string(pd.warn)); | ||
57 | } else { | ||
58 | asprintf(&result, "%s;", result); | ||
59 | } | ||
60 | |||
61 | if (pd.crit_present) { | ||
62 | asprintf(&result, "%s;%s", result, mp_range_to_string(pd.crit)); | ||
63 | } else { | ||
64 | asprintf(&result, "%s;", result); | ||
65 | } | ||
66 | if (pd.min_present) { | ||
67 | asprintf(&result, "%s;%s", result, pd_value_to_string(pd.min)); | ||
68 | } else { | ||
69 | asprintf(&result, "%s;", result); | ||
70 | } | ||
71 | |||
72 | if (pd.max_present) { | ||
73 | asprintf(&result, "%s;%s", result, pd_value_to_string(pd.max)); | ||
74 | } | ||
75 | |||
76 | /*printf("pd_to_string: %s\n", result); */ | ||
77 | |||
78 | return result; | ||
79 | } | ||
80 | |||
81 | char *pd_list_to_string(const pd_list pd) { | ||
82 | char *result = pd_to_string(pd.data); | ||
83 | |||
84 | for (pd_list *elem = pd.next; elem != NULL; elem = elem->next) { | ||
85 | asprintf(&result, "%s %s", result, pd_to_string(elem->data)); | ||
86 | } | ||
87 | |||
88 | return result; | ||
89 | } | ||
90 | |||
91 | mp_perfdata perfdata_init() { | ||
92 | mp_perfdata pd = {}; | ||
93 | return pd; | ||
94 | } | ||
95 | |||
96 | pd_list *pd_list_init() { | ||
97 | pd_list *tmp = (pd_list *)calloc(1, sizeof(pd_list)); | ||
98 | if (tmp == NULL) { | ||
99 | die(STATE_UNKNOWN, "calloc failed\n"); | ||
100 | } | ||
101 | tmp->next = NULL; | ||
102 | return tmp; | ||
103 | } | ||
104 | |||
105 | mp_range mp_range_init() { | ||
106 | mp_range result = { | ||
107 | .alert_on_inside_range = OUTSIDE, | ||
108 | .start = {}, | ||
109 | .start_infinity = true, | ||
110 | .end = {}, | ||
111 | .end_infinity = true, | ||
112 | }; | ||
113 | |||
114 | return result; | ||
115 | } | ||
116 | |||
117 | mp_range mp_range_set_start(mp_range input, mp_perfdata_value perf_val) { | ||
118 | input.start = perf_val; | ||
119 | input.start_infinity = false; | ||
120 | return input; | ||
121 | } | ||
122 | |||
123 | mp_range mp_range_set_end(mp_range input, mp_perfdata_value perf_val) { | ||
124 | input.end = perf_val; | ||
125 | input.end_infinity = false; | ||
126 | return input; | ||
127 | } | ||
128 | |||
129 | void pd_list_append(pd_list pdl[1], const mp_perfdata pd) { | ||
130 | assert(pdl != NULL); | ||
131 | |||
132 | if (pdl->data.value.type == PD_TYPE_NONE) { | ||
133 | // first entry is still empty | ||
134 | pdl->data = pd; | ||
135 | } else { | ||
136 | // find last element in the list | ||
137 | pd_list *curr = pdl; | ||
138 | pd_list *next = pdl->next; | ||
139 | |||
140 | while (next != NULL) { | ||
141 | curr = next; | ||
142 | next = next->next; | ||
143 | } | ||
144 | |||
145 | if (curr->data.value.type == PD_TYPE_NONE) { | ||
146 | // still empty | ||
147 | curr->data = pd; | ||
148 | } else { | ||
149 | // new a new one | ||
150 | curr->next = pd_list_init(); | ||
151 | curr->next->data = pd; | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | |||
156 | void pd_list_free(pd_list pdl[1]) { | ||
157 | while (pdl != NULL) { | ||
158 | pd_list *old = pdl; | ||
159 | pdl = pdl->next; | ||
160 | free(old); | ||
161 | } | ||
162 | } | ||
163 | |||
164 | /* | ||
165 | * returns -1 if a < b, 0 if a == b, 1 if a > b | ||
166 | */ | ||
167 | int cmp_perfdata_value(const mp_perfdata_value a, const mp_perfdata_value b) { | ||
168 | // Test if types are different | ||
169 | if (a.type == b.type) { | ||
170 | |||
171 | switch (a.type) { | ||
172 | case PD_TYPE_UINT: | ||
173 | if (a.pd_uint < b.pd_uint) { | ||
174 | return -1; | ||
175 | } else if (a.pd_uint == b.pd_uint) { | ||
176 | return 0; | ||
177 | } else { | ||
178 | return 1; | ||
179 | } | ||
180 | break; | ||
181 | case PD_TYPE_INT: | ||
182 | if (a.pd_int < b.pd_int) { | ||
183 | return -1; | ||
184 | } else if (a.pd_int == b.pd_int) { | ||
185 | return 0; | ||
186 | } else { | ||
187 | return 1; | ||
188 | } | ||
189 | break; | ||
190 | case PD_TYPE_DOUBLE: | ||
191 | if (a.pd_int < b.pd_int) { | ||
192 | return -1; | ||
193 | } else if (a.pd_int == b.pd_int) { | ||
194 | return 0; | ||
195 | } else { | ||
196 | return 1; | ||
197 | } | ||
198 | break; | ||
199 | default: | ||
200 | die(STATE_UNKNOWN, "Error in %s line: %d!", __FILE__, __LINE__); | ||
201 | } | ||
202 | } | ||
203 | |||
204 | // Get dirty here | ||
205 | long double floating_a = 0; | ||
206 | |||
207 | switch (a.type) { | ||
208 | case PD_TYPE_UINT: | ||
209 | floating_a = a.pd_uint; | ||
210 | break; | ||
211 | case PD_TYPE_INT: | ||
212 | floating_a = a.pd_int; | ||
213 | break; | ||
214 | case PD_TYPE_DOUBLE: | ||
215 | floating_a = a.pd_double; | ||
216 | break; | ||
217 | default: | ||
218 | die(STATE_UNKNOWN, "Error in %s line: %d!", __FILE__, __LINE__); | ||
219 | } | ||
220 | |||
221 | long double floating_b = 0; | ||
222 | switch (b.type) { | ||
223 | case PD_TYPE_UINT: | ||
224 | floating_b = b.pd_uint; | ||
225 | break; | ||
226 | case PD_TYPE_INT: | ||
227 | floating_b = b.pd_int; | ||
228 | break; | ||
229 | case PD_TYPE_DOUBLE: | ||
230 | floating_b = b.pd_double; | ||
231 | break; | ||
232 | default: | ||
233 | die(STATE_UNKNOWN, "Error in %s line: %d!", __FILE__, __LINE__); | ||
234 | } | ||
235 | |||
236 | if (floating_a < floating_b) { | ||
237 | return -1; | ||
238 | } | ||
239 | if (floating_a == floating_b) { | ||
240 | return 0; | ||
241 | } | ||
242 | return 1; | ||
243 | } | ||
244 | |||
245 | char *mp_range_to_string(const mp_range input) { | ||
246 | char *result = ""; | ||
247 | if (input.alert_on_inside_range == INSIDE) { | ||
248 | asprintf(&result, "@"); | ||
249 | } | ||
250 | |||
251 | if (input.start_infinity) { | ||
252 | asprintf(&result, "%s~:", result); | ||
253 | } else { | ||
254 | asprintf(&result, "%s%s:", result, pd_value_to_string(input.start)); | ||
255 | } | ||
256 | |||
257 | if (!input.end_infinity) { | ||
258 | asprintf(&result, "%s%s", result, pd_value_to_string(input.end)); | ||
259 | } | ||
260 | return result; | ||
261 | } | ||
262 | |||
263 | mp_perfdata mp_set_pd_value_float(mp_perfdata pd, float value) { | ||
264 | return mp_set_pd_value_double(pd, value); | ||
265 | } | ||
266 | |||
267 | mp_perfdata mp_set_pd_value_double(mp_perfdata pd, double value) { | ||
268 | pd.value.pd_double = value; | ||
269 | pd.value.type = PD_TYPE_DOUBLE; | ||
270 | return pd; | ||
271 | } | ||
272 | |||
273 | mp_perfdata mp_set_pd_value_char(mp_perfdata pd, char value) { | ||
274 | return mp_set_pd_value_long_long(pd, (long long)value); | ||
275 | } | ||
276 | |||
277 | mp_perfdata mp_set_pd_value_u_char(mp_perfdata pd, unsigned char value) { | ||
278 | return mp_set_pd_value_u_long_long(pd, (unsigned long long)value); | ||
279 | } | ||
280 | |||
281 | mp_perfdata mp_set_pd_value_int(mp_perfdata pd, int value) { | ||
282 | return mp_set_pd_value_long_long(pd, (long long)value); | ||
283 | } | ||
284 | |||
285 | mp_perfdata mp_set_pd_value_u_int(mp_perfdata pd, unsigned int value) { | ||
286 | return mp_set_pd_value_u_long_long(pd, (unsigned long long)value); | ||
287 | } | ||
288 | |||
289 | mp_perfdata mp_set_pd_value_long(mp_perfdata pd, long value) { | ||
290 | return mp_set_pd_value_long_long(pd, (long long)value); | ||
291 | } | ||
292 | |||
293 | mp_perfdata mp_set_pd_value_u_long(mp_perfdata pd, unsigned long value) { | ||
294 | return mp_set_pd_value_u_long_long(pd, (unsigned long long)value); | ||
295 | } | ||
296 | |||
297 | mp_perfdata mp_set_pd_value_long_long(mp_perfdata pd, long long value) { | ||
298 | pd.value.pd_int = value; | ||
299 | pd.value.type = PD_TYPE_INT; | ||
300 | return pd; | ||
301 | } | ||
302 | |||
303 | mp_perfdata mp_set_pd_value_u_long_long(mp_perfdata pd, unsigned long long value) { | ||
304 | pd.value.pd_uint = value; | ||
305 | pd.value.type = PD_TYPE_UINT; | ||
306 | return pd; | ||
307 | } | ||
308 | |||
309 | mp_perfdata_value mp_create_pd_value_double(double value) { | ||
310 | mp_perfdata_value res = {0}; | ||
311 | res.type = PD_TYPE_DOUBLE; | ||
312 | res.pd_double = value; | ||
313 | return res; | ||
314 | } | ||
315 | |||
316 | mp_perfdata_value mp_create_pd_value_float(float value) { | ||
317 | return mp_create_pd_value_double((double)value); | ||
318 | } | ||
319 | |||
320 | mp_perfdata_value mp_create_pd_value_char(char value) { | ||
321 | return mp_create_pd_value_long_long((long long)value); | ||
322 | } | ||
323 | |||
324 | mp_perfdata_value mp_create_pd_value_u_char(unsigned char value) { | ||
325 | return mp_create_pd_value_u_long_long((unsigned long long)value); | ||
326 | } | ||
327 | |||
328 | mp_perfdata_value mp_create_pd_value_int(int value) { | ||
329 | return mp_create_pd_value_long_long((long long)value); | ||
330 | } | ||
331 | |||
332 | mp_perfdata_value mp_create_pd_value_u_int(unsigned int value) { | ||
333 | return mp_create_pd_value_u_long_long((unsigned long long)value); | ||
334 | } | ||
335 | |||
336 | mp_perfdata_value mp_create_pd_value_long(long value) { | ||
337 | return mp_create_pd_value_long_long((long long)value); | ||
338 | } | ||
339 | |||
340 | mp_perfdata_value mp_create_pd_value_u_long(unsigned long value) { | ||
341 | return mp_create_pd_value_u_long_long((unsigned long long)value); | ||
342 | } | ||
343 | |||
344 | mp_perfdata_value mp_create_pd_value_long_long(long long value) { | ||
345 | mp_perfdata_value res = {0}; | ||
346 | res.type = PD_TYPE_INT; | ||
347 | res.pd_int = value; | ||
348 | return res; | ||
349 | } | ||
350 | |||
351 | mp_perfdata_value mp_create_pd_value_u_long_long(unsigned long long value) { | ||
352 | mp_perfdata_value res = {0}; | ||
353 | res.type = PD_TYPE_UINT; | ||
354 | res.pd_uint = value; | ||
355 | return res; | ||
356 | } | ||
357 | |||
358 | char *fmt_range(range foo) { return foo.text; } | ||
359 | |||
360 | typedef struct integer_parser_wrapper { | ||
361 | int error; | ||
362 | mp_perfdata_value value; | ||
363 | } integer_parser_wrapper; | ||
364 | |||
365 | typedef struct double_parser_wrapper { | ||
366 | int error; | ||
367 | mp_perfdata_value value; | ||
368 | } double_parser_wrapper; | ||
369 | |||
370 | typedef struct perfdata_value_parser_wrapper { | ||
371 | int error; | ||
372 | mp_perfdata_value value; | ||
373 | } perfdata_value_parser_wrapper; | ||
374 | |||
375 | double_parser_wrapper parse_double(const char *input); | ||
376 | integer_parser_wrapper parse_integer(const char *input); | ||
377 | perfdata_value_parser_wrapper parse_pd_value(const char *input); | ||
378 | |||
379 | mp_range_parsed mp_parse_range_string(const char *input) { | ||
380 | if (input == NULL) { | ||
381 | mp_range_parsed result = { | ||
382 | .error = MP_RANGE_PARSING_FAILURE, | ||
383 | }; | ||
384 | return result; | ||
385 | } | ||
386 | |||
387 | if (strlen(input) == 0) { | ||
388 | mp_range_parsed result = { | ||
389 | .error = MP_RANGE_PARSING_FAILURE, | ||
390 | }; | ||
391 | return result; | ||
392 | } | ||
393 | |||
394 | mp_range_parsed result = { | ||
395 | .range = mp_range_init(), | ||
396 | .error = MP_PARSING_SUCCES, | ||
397 | }; | ||
398 | |||
399 | if (input[0] == '@') { | ||
400 | // found an '@' at beginning, so invert the range logic | ||
401 | result.range.alert_on_inside_range = INSIDE; | ||
402 | |||
403 | // advance the pointer one symbol | ||
404 | input++; | ||
405 | } | ||
406 | |||
407 | char *working_copy = strdup(input); | ||
408 | if (working_copy == NULL) { | ||
409 | // strdup error, probably | ||
410 | mp_range_parsed result = { | ||
411 | .error = MP_RANGE_PARSING_FAILURE, | ||
412 | }; | ||
413 | return result; | ||
414 | } | ||
415 | input = working_copy; | ||
416 | |||
417 | char *separator = index(working_copy, ':'); | ||
418 | if (separator != NULL) { | ||
419 | // Found a separator | ||
420 | // set the separator to 0, so we have two different strings | ||
421 | *separator = '\0'; | ||
422 | |||
423 | if (input[0] == '~') { | ||
424 | // the beginning starts with '~', so it might be infinity | ||
425 | if (&input[1] != separator) { | ||
426 | // the next symbol after '~' is not the separator! | ||
427 | // so input is probably wrong | ||
428 | result.error = MP_RANGE_PARSING_FAILURE; | ||
429 | free(working_copy); | ||
430 | return result; | ||
431 | } | ||
432 | |||
433 | result.range.start_infinity = true; | ||
434 | } else { | ||
435 | // No '~' at the beginning, so this should be a number | ||
436 | result.range.start_infinity = false; | ||
437 | perfdata_value_parser_wrapper parsed_pd = parse_pd_value(input); | ||
438 | |||
439 | if (parsed_pd.error != MP_PARSING_SUCCES) { | ||
440 | result.error = parsed_pd.error; | ||
441 | free(working_copy); | ||
442 | return result; | ||
443 | } | ||
444 | |||
445 | result.range.start = parsed_pd.value; | ||
446 | result.range.start_infinity = false; | ||
447 | } | ||
448 | // got the first part now | ||
449 | // advance the pointer | ||
450 | input = separator + 1; | ||
451 | } | ||
452 | |||
453 | // End part or no separator | ||
454 | if (input[0] == '\0') { | ||
455 | // the end is infinite | ||
456 | result.range.end_infinity = true; | ||
457 | } else { | ||
458 | perfdata_value_parser_wrapper parsed_pd = parse_pd_value(input); | ||
459 | |||
460 | if (parsed_pd.error != MP_PARSING_SUCCES) { | ||
461 | result.error = parsed_pd.error; | ||
462 | return result; | ||
463 | } | ||
464 | result.range.end = parsed_pd.value; | ||
465 | result.range.end_infinity = false; | ||
466 | } | ||
467 | free(working_copy); | ||
468 | return result; | ||
469 | } | ||
470 | |||
471 | double_parser_wrapper parse_double(const char *input) { | ||
472 | double_parser_wrapper result = { | ||
473 | .error = MP_PARSING_SUCCES, | ||
474 | }; | ||
475 | |||
476 | if (input == NULL) { | ||
477 | result.error = MP_PARSING_FAILURE; | ||
478 | return result; | ||
479 | } | ||
480 | |||
481 | char *endptr = NULL; | ||
482 | errno = 0; | ||
483 | double tmp = strtod(input, &endptr); | ||
484 | |||
485 | if (input == endptr) { | ||
486 | // man 3 strtod says, no conversion performed | ||
487 | result.error = MP_PARSING_FAILURE; | ||
488 | return result; | ||
489 | } | ||
490 | |||
491 | if (errno) { | ||
492 | // some other error | ||
493 | // TODO maybe differentiate a little bit | ||
494 | result.error = MP_PARSING_FAILURE; | ||
495 | return result; | ||
496 | } | ||
497 | |||
498 | result.value = mp_create_pd_value(tmp); | ||
499 | return result; | ||
500 | } | ||
501 | |||
502 | integer_parser_wrapper parse_integer(const char *input) { | ||
503 | integer_parser_wrapper result = { | ||
504 | .error = MP_PARSING_SUCCES, | ||
505 | }; | ||
506 | |||
507 | if (input == NULL) { | ||
508 | result.error = MP_PARSING_FAILURE; | ||
509 | return result; | ||
510 | } | ||
511 | |||
512 | char *endptr = NULL; | ||
513 | errno = 0; | ||
514 | long long tmp = strtoll(input, &endptr, 0); | ||
515 | |||
516 | // validating *sigh* | ||
517 | if (*endptr != '\0') { | ||
518 | // something went wrong in strtoll | ||
519 | if (tmp == LLONG_MIN) { | ||
520 | // underflow | ||
521 | result.error = MP_RANGE_PARSING_UNDERFLOW; | ||
522 | return result; | ||
523 | } | ||
524 | |||
525 | if (tmp == LLONG_MAX) { | ||
526 | // overflow | ||
527 | result.error = MP_RANGE_PARSING_OVERFLOW; | ||
528 | return result; | ||
529 | } | ||
530 | |||
531 | // still wrong, but not sure why, probably invalid characters | ||
532 | if (errno == EINVAL) { | ||
533 | result.error = MP_RANGE_PARSING_INVALID_CHAR; | ||
534 | return result; | ||
535 | } | ||
536 | |||
537 | // some other error, do catch all here | ||
538 | result.error = MP_RANGE_PARSING_FAILURE; | ||
539 | return result; | ||
540 | } | ||
541 | |||
542 | // no error, should be fine | ||
543 | result.value = mp_create_pd_value(tmp); | ||
544 | return result; | ||
545 | } | ||
546 | |||
547 | perfdata_value_parser_wrapper parse_pd_value(const char *input) { | ||
548 | // try integer first | ||
549 | integer_parser_wrapper tmp_int = parse_integer(input); | ||
550 | |||
551 | if (tmp_int.error == MP_PARSING_SUCCES) { | ||
552 | perfdata_value_parser_wrapper result = { | ||
553 | .error = tmp_int.error, | ||
554 | .value = tmp_int.value, | ||
555 | }; | ||
556 | return result; | ||
557 | } | ||
558 | |||
559 | double_parser_wrapper tmp_double = parse_double(input); | ||
560 | perfdata_value_parser_wrapper result = {}; | ||
561 | if (tmp_double.error == MP_PARSING_SUCCES) { | ||
562 | result.error = tmp_double.error; | ||
563 | result.value = tmp_double.value; | ||
564 | } else { | ||
565 | result.error = tmp_double.error; | ||
566 | } | ||
567 | return result; | ||
568 | } | ||
569 | |||
570 | mp_perfdata mp_set_pd_max_value(mp_perfdata perfdata, mp_perfdata_value value) { | ||
571 | perfdata.max = value; | ||
572 | perfdata.max_present = true; | ||
573 | return perfdata; | ||
574 | } | ||
575 | |||
576 | mp_perfdata mp_set_pd_min_value(mp_perfdata perfdata, mp_perfdata_value value) { | ||
577 | perfdata.min = value; | ||
578 | perfdata.min_present = true; | ||
579 | return perfdata; | ||
580 | } | ||
581 | |||
582 | double mp_get_pd_value(mp_perfdata_value value) { | ||
583 | assert(value.type != PD_TYPE_NONE); | ||
584 | switch (value.type) { | ||
585 | case PD_TYPE_DOUBLE: | ||
586 | return value.pd_double; | ||
587 | case PD_TYPE_INT: | ||
588 | return (double)value.pd_int; | ||
589 | case PD_TYPE_UINT: | ||
590 | return (double)value.pd_uint; | ||
591 | default: | ||
592 | return 0; // just to make the compiler happy | ||
593 | } | ||
594 | } | ||
595 | |||
596 | mp_perfdata_value mp_pd_value_multiply(mp_perfdata_value left, mp_perfdata_value right) { | ||
597 | if (left.type == right.type) { | ||
598 | switch (left.type) { | ||
599 | case PD_TYPE_DOUBLE: | ||
600 | left.pd_double *= right.pd_double; | ||
601 | return left; | ||
602 | case PD_TYPE_INT: | ||
603 | left.pd_int *= right.pd_int; | ||
604 | return left; | ||
605 | case PD_TYPE_UINT: | ||
606 | left.pd_uint *= right.pd_uint; | ||
607 | return left; | ||
608 | default: | ||
609 | // what to here? | ||
610 | return left; | ||
611 | } | ||
612 | } | ||
613 | |||
614 | // Different types, oh boy, just do the lazy thing for now and switch to double | ||
615 | switch (left.type) { | ||
616 | case PD_TYPE_INT: | ||
617 | left.pd_double = (double)left.pd_int; | ||
618 | left.type = PD_TYPE_DOUBLE; | ||
619 | break; | ||
620 | case PD_TYPE_UINT: | ||
621 | left.pd_double = (double)left.pd_uint; | ||
622 | left.type = PD_TYPE_DOUBLE; | ||
623 | break; | ||
624 | } | ||
625 | |||
626 | switch (right.type) { | ||
627 | case PD_TYPE_INT: | ||
628 | right.pd_double = (double)right.pd_int; | ||
629 | right.type = PD_TYPE_DOUBLE; | ||
630 | break; | ||
631 | case PD_TYPE_UINT: | ||
632 | right.pd_double = (double)right.pd_uint; | ||
633 | right.type = PD_TYPE_DOUBLE; | ||
634 | break; | ||
635 | } | ||
636 | |||
637 | left.pd_double *= right.pd_double; | ||
638 | return left; | ||
639 | } | ||
640 | |||
641 | mp_range mp_range_multiply(mp_range range, mp_perfdata_value factor) { | ||
642 | if (!range.end_infinity) { | ||
643 | range.end = mp_pd_value_multiply(range.end, factor); | ||
644 | } | ||
645 | if (!range.start_infinity) { | ||
646 | range.start = mp_pd_value_multiply(range.start, factor); | ||
647 | } | ||
648 | return range; | ||
649 | } | ||
diff --git a/lib/perfdata.h b/lib/perfdata.h new file mode 100644 index 00000000..e51ef5fd --- /dev/null +++ b/lib/perfdata.h | |||
@@ -0,0 +1,219 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../config.h" | ||
4 | |||
5 | #include <inttypes.h> | ||
6 | #include <stdbool.h> | ||
7 | |||
8 | // Enum for the specific type of a perfdata_value | ||
9 | typedef enum pd_value_type { | ||
10 | PD_TYPE_NONE = 0, | ||
11 | PD_TYPE_INT, | ||
12 | PD_TYPE_UINT, | ||
13 | PD_TYPE_DOUBLE | ||
14 | } pd_value_type; | ||
15 | |||
16 | typedef struct { | ||
17 | enum pd_value_type type; | ||
18 | union { | ||
19 | long long pd_int; | ||
20 | unsigned long long pd_uint; | ||
21 | double pd_double; | ||
22 | }; | ||
23 | } mp_perfdata_value; | ||
24 | |||
25 | #define MP_OUTSIDE false | ||
26 | #define MP_INSIDE true | ||
27 | |||
28 | /* | ||
29 | * New range type with generic numerical values | ||
30 | */ | ||
31 | typedef struct { | ||
32 | mp_perfdata_value start; | ||
33 | bool start_infinity; /* false (default) or true */ | ||
34 | |||
35 | mp_perfdata_value end; | ||
36 | bool end_infinity; | ||
37 | |||
38 | bool alert_on_inside_range; /* OUTSIDE (default) or INSIDE */ | ||
39 | } mp_range; | ||
40 | |||
41 | /* | ||
42 | * Old range type with floating point values | ||
43 | */ | ||
44 | typedef struct { | ||
45 | double start; | ||
46 | bool start_infinity; | ||
47 | double end; | ||
48 | bool end_infinity; | ||
49 | int alert_on; /* OUTSIDE (default) or INSIDE */ | ||
50 | char *text; /* original unparsed text input */ | ||
51 | } range; | ||
52 | |||
53 | /* | ||
54 | * Perfdata type for storing perfdata output | ||
55 | */ | ||
56 | typedef struct { | ||
57 | char *label; | ||
58 | char *uom; | ||
59 | mp_perfdata_value value; | ||
60 | |||
61 | bool warn_present; | ||
62 | mp_range warn; | ||
63 | |||
64 | bool crit_present; | ||
65 | mp_range crit; | ||
66 | |||
67 | bool min_present; | ||
68 | mp_perfdata_value min; | ||
69 | |||
70 | bool max_present; | ||
71 | mp_perfdata_value max; | ||
72 | } mp_perfdata; | ||
73 | |||
74 | /* | ||
75 | * List of mp_perfdata values | ||
76 | */ | ||
77 | typedef struct pd_list_struct { | ||
78 | mp_perfdata data; | ||
79 | struct pd_list_struct *next; | ||
80 | } pd_list; | ||
81 | |||
82 | /* | ||
83 | * ============ | ||
84 | * Initializers | ||
85 | * ============ | ||
86 | */ | ||
87 | /* | ||
88 | * Initialize mp_perfdata value. Always use this to generate a new one | ||
89 | */ | ||
90 | mp_perfdata perfdata_init(void); | ||
91 | |||
92 | /* | ||
93 | * Initialize pd_list value. Always use this to generate a new one | ||
94 | */ | ||
95 | pd_list *pd_list_init(void); | ||
96 | |||
97 | /* | ||
98 | * Initialize a new mp_range value, with unset values (start and end are infinite | ||
99 | */ | ||
100 | mp_range mp_range_init(void); | ||
101 | |||
102 | /* | ||
103 | * Worker functions | ||
104 | */ | ||
105 | |||
106 | mp_range mp_range_set_start(mp_range, mp_perfdata_value); | ||
107 | mp_range mp_range_set_end(mp_range, mp_perfdata_value); | ||
108 | |||
109 | /* | ||
110 | * Parsing a range from a string | ||
111 | */ | ||
112 | |||
113 | typedef enum { | ||
114 | MP_PARSING_SUCCES = 0, | ||
115 | MP_PARSING_FAILURE, | ||
116 | MP_RANGE_PARSING_FAILURE, | ||
117 | MP_RANGE_PARSING_UNDERFLOW, | ||
118 | MP_RANGE_PARSING_OVERFLOW, | ||
119 | MP_RANGE_PARSING_INVALID_CHAR, | ||
120 | } mp_range_parser_error; | ||
121 | |||
122 | typedef struct mp_range_parsed { | ||
123 | mp_range_parser_error error; | ||
124 | mp_range range; | ||
125 | } mp_range_parsed; | ||
126 | |||
127 | mp_range_parsed mp_parse_range_string(const char * /*input*/); | ||
128 | |||
129 | /* | ||
130 | * Appends a mp_perfdata value to a pd_list | ||
131 | */ | ||
132 | void pd_list_append(pd_list[1], mp_perfdata); | ||
133 | |||
134 | #define mp_set_pd_value(P, V) \ | ||
135 | _Generic((V), \ | ||
136 | float: mp_set_pd_value_float, \ | ||
137 | double: mp_set_pd_value_double, \ | ||
138 | int: mp_set_pd_value_int, \ | ||
139 | unsigned int: mp_set_pd_value_u_int, \ | ||
140 | long: mp_set_pd_value_long, \ | ||
141 | unsigned long: mp_set_pd_value_u_long, \ | ||
142 | long long: mp_set_pd_value_long_long, \ | ||
143 | unsigned long long: mp_set_pd_value_u_long_long)(P, V) | ||
144 | |||
145 | mp_perfdata mp_set_pd_value_float(mp_perfdata, float); | ||
146 | mp_perfdata mp_set_pd_value_double(mp_perfdata, double); | ||
147 | mp_perfdata mp_set_pd_value_int(mp_perfdata, int); | ||
148 | mp_perfdata mp_set_pd_value_u_int(mp_perfdata, unsigned int); | ||
149 | mp_perfdata mp_set_pd_value_long(mp_perfdata, long); | ||
150 | mp_perfdata mp_set_pd_value_u_long(mp_perfdata, unsigned long); | ||
151 | mp_perfdata mp_set_pd_value_long_long(mp_perfdata, long long); | ||
152 | mp_perfdata mp_set_pd_value_u_long_long(mp_perfdata, unsigned long long); | ||
153 | |||
154 | #define mp_create_pd_value(V) \ | ||
155 | _Generic((V), \ | ||
156 | float: mp_create_pd_value_float, \ | ||
157 | double: mp_create_pd_value_double, \ | ||
158 | char: mp_create_pd_value_char, \ | ||
159 | unsigned char: mp_create_pd_value_u_char, \ | ||
160 | int: mp_create_pd_value_int, \ | ||
161 | unsigned int: mp_create_pd_value_u_int, \ | ||
162 | long: mp_create_pd_value_long, \ | ||
163 | unsigned long: mp_create_pd_value_u_long, \ | ||
164 | long long: mp_create_pd_value_long_long, \ | ||
165 | unsigned long long: mp_create_pd_value_u_long_long)(V) | ||
166 | |||
167 | mp_perfdata_value mp_create_pd_value_float(float); | ||
168 | mp_perfdata_value mp_create_pd_value_double(double); | ||
169 | mp_perfdata_value mp_create_pd_value_char(char); | ||
170 | mp_perfdata_value mp_create_pd_value_u_char(unsigned char); | ||
171 | mp_perfdata_value mp_create_pd_value_int(int); | ||
172 | mp_perfdata_value mp_create_pd_value_u_int(unsigned int); | ||
173 | mp_perfdata_value mp_create_pd_value_long(long); | ||
174 | mp_perfdata_value mp_create_pd_value_u_long(unsigned long); | ||
175 | mp_perfdata_value mp_create_pd_value_long_long(long long); | ||
176 | mp_perfdata_value mp_create_pd_value_u_long_long(unsigned long long); | ||
177 | |||
178 | mp_perfdata mp_set_pd_max_value(mp_perfdata perfdata, mp_perfdata_value value); | ||
179 | mp_perfdata mp_set_pd_min_value(mp_perfdata perfdata, mp_perfdata_value value); | ||
180 | |||
181 | double mp_get_pd_value(mp_perfdata_value value); | ||
182 | |||
183 | /* | ||
184 | * Free the memory used by a pd_list | ||
185 | */ | ||
186 | void pd_list_free(pd_list[1]); | ||
187 | |||
188 | int cmp_perfdata_value(mp_perfdata_value, mp_perfdata_value); | ||
189 | |||
190 | // ================ | ||
191 | // Helper functions | ||
192 | // ================ | ||
193 | |||
194 | mp_perfdata_value mp_pd_value_multiply(mp_perfdata_value left, mp_perfdata_value right); | ||
195 | mp_range mp_range_multiply(mp_range range, mp_perfdata_value factor); | ||
196 | |||
197 | // ================= | ||
198 | // String formatters | ||
199 | // ================= | ||
200 | /* | ||
201 | * Generate string from mp_perfdata value | ||
202 | */ | ||
203 | char *pd_to_string(mp_perfdata); | ||
204 | |||
205 | /* | ||
206 | * Generate string from perfdata_value value | ||
207 | */ | ||
208 | char *pd_value_to_string(mp_perfdata_value); | ||
209 | |||
210 | /* | ||
211 | * Generate string from pd_list value for the final output | ||
212 | */ | ||
213 | char *pd_list_to_string(pd_list); | ||
214 | |||
215 | /* | ||
216 | * Generate string from a mp_range value | ||
217 | */ | ||
218 | char *mp_range_to_string(mp_range); | ||
219 | char *fmt_range(range); | ||
diff --git a/lib/states.h b/lib/states.h new file mode 100644 index 00000000..4a170caa --- /dev/null +++ b/lib/states.h | |||
@@ -0,0 +1,71 @@ | |||
1 | #ifndef _MP_STATES_ | ||
2 | #define _MP_STATES_ | ||
3 | |||
4 | #include "../config.h" | ||
5 | #include <sys/param.h> | ||
6 | |||
7 | typedef enum state_enum { | ||
8 | STATE_OK, | ||
9 | STATE_WARNING, | ||
10 | STATE_CRITICAL, | ||
11 | STATE_UNKNOWN, | ||
12 | STATE_DEPENDENT | ||
13 | } mp_state_enum; | ||
14 | |||
15 | /* ************************************************************************** | ||
16 | * max_state(STATE_x, STATE_y) | ||
17 | * compares STATE_x to STATE_y and returns result based on the following | ||
18 | * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL | ||
19 | * | ||
20 | * Note that numerically the above does not hold | ||
21 | ****************************************************************************/ | ||
22 | |||
23 | static inline mp_state_enum max_state(mp_state_enum a, mp_state_enum b) { | ||
24 | if (a == STATE_CRITICAL || b == STATE_CRITICAL) { | ||
25 | return STATE_CRITICAL; | ||
26 | } | ||
27 | if (a == STATE_WARNING || b == STATE_WARNING) { | ||
28 | return STATE_WARNING; | ||
29 | } | ||
30 | if (a == STATE_OK || b == STATE_OK) { | ||
31 | return STATE_OK; | ||
32 | } | ||
33 | if (a == STATE_UNKNOWN || b == STATE_UNKNOWN) { | ||
34 | return STATE_UNKNOWN; | ||
35 | } | ||
36 | if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) { | ||
37 | return STATE_DEPENDENT; | ||
38 | } | ||
39 | return MAX(a, b); | ||
40 | } | ||
41 | |||
42 | /* ************************************************************************** | ||
43 | * max_state_alt(STATE_x, STATE_y) | ||
44 | * compares STATE_x to STATE_y and returns result based on the following | ||
45 | * STATE_OK < STATE_DEPENDENT < STATE_UNKNOWN < STATE_WARNING < STATE_CRITICAL | ||
46 | * | ||
47 | * The main difference between max_state_alt and max_state it that it doesn't | ||
48 | * allow setting a default to UNKNOWN. It will instead prioritixe any valid | ||
49 | * non-OK state. | ||
50 | ****************************************************************************/ | ||
51 | |||
52 | static inline mp_state_enum max_state_alt(mp_state_enum a, mp_state_enum b) { | ||
53 | if (a == STATE_CRITICAL || b == STATE_CRITICAL) { | ||
54 | return STATE_CRITICAL; | ||
55 | } | ||
56 | if (a == STATE_WARNING || b == STATE_WARNING) { | ||
57 | return STATE_WARNING; | ||
58 | } | ||
59 | if (a == STATE_UNKNOWN || b == STATE_UNKNOWN) { | ||
60 | return STATE_UNKNOWN; | ||
61 | } | ||
62 | if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) { | ||
63 | return STATE_DEPENDENT; | ||
64 | } | ||
65 | if (a == STATE_OK || b == STATE_OK) { | ||
66 | return STATE_OK; | ||
67 | } | ||
68 | return MAX(a, b); | ||
69 | } | ||
70 | |||
71 | #endif | ||
diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am index 31d79df6..7798a72e 100644 --- a/lib/tests/Makefile.am +++ b/lib/tests/Makefile.am | |||
@@ -8,9 +8,9 @@ check_PROGRAMS = @EXTRA_TEST@ | |||
8 | AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ | 8 | AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ |
9 | -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins | 9 | -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins |
10 | 10 | ||
11 | EXTRA_PROGRAMS = test_utils test_disk test_tcp test_cmd test_base64 test_ini1 test_ini3 test_opts1 test_opts2 test_opts3 | 11 | EXTRA_PROGRAMS = test_utils test_tcp test_cmd test_base64 test_ini1 test_ini3 test_opts1 test_opts2 test_opts3 test_generic_output |
12 | 12 | ||
13 | np_test_scripts = test_base64.t test_cmd.t test_disk.t test_ini1.t test_ini3.t test_opts1.t test_opts2.t test_opts3.t test_tcp.t test_utils.t | 13 | np_test_scripts = test_base64.t test_cmd.t test_ini1.t test_ini3.t test_opts1.t test_opts2.t test_opts3.t test_tcp.t test_utils.t test_generic_output.t |
14 | np_test_files = config-dos.ini config-opts.ini config-tiny.ini plugin.ini plugins.ini | 14 | np_test_files = config-dos.ini config-opts.ini config-tiny.ini plugin.ini plugins.ini |
15 | EXTRA_DIST = $(np_test_scripts) $(np_test_files) var | 15 | EXTRA_DIST = $(np_test_scripts) $(np_test_files) var |
16 | 16 | ||
@@ -29,7 +29,7 @@ AM_CFLAGS = -g -I$(top_srcdir)/lib -I$(top_srcdir)/gl $(tap_cflags) | |||
29 | AM_LDFLAGS = $(tap_ldflags) -ltap | 29 | AM_LDFLAGS = $(tap_ldflags) -ltap |
30 | LDADD = $(top_srcdir)/lib/libmonitoringplug.a $(top_srcdir)/gl/libgnu.a $(LIB_CRYPTO) | 30 | LDADD = $(top_srcdir)/lib/libmonitoringplug.a $(top_srcdir)/gl/libgnu.a $(LIB_CRYPTO) |
31 | 31 | ||
32 | SOURCES = test_utils.c test_disk.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c | 32 | SOURCES = test_utils.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c test_generic_output.c |
33 | 33 | ||
34 | test: ${noinst_PROGRAMS} | 34 | test: ${noinst_PROGRAMS} |
35 | perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map {$$_ .= ".t"} @ARGV)' $(EXTRA_PROGRAMS) | 35 | perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map {$$_ .= ".t"} @ARGV)' $(EXTRA_PROGRAMS) |
diff --git a/lib/tests/test_base64.c b/lib/tests/test_base64.c index 94cb5aa9..798244da 100644 --- a/lib/tests/test_base64.c +++ b/lib/tests/test_base64.c | |||
@@ -180,117 +180,168 @@ int main(int argc, char **argv) { | |||
180 | #endif | 180 | #endif |
181 | 181 | ||
182 | char random[1024] = { | 182 | char random[1024] = { |
183 | 0x0b, 0x30, 0x44, 0x62, 0x7c, 0x22, 0x1f, 0x0d, 0x05, 0x67, 0x2c, 0x2a, 0x39, 0x21, 0x46, 0x08, 0x50, 0x66, 0x34, 0x37, 0x0b, 0x45, | 183 | 0x0b, 0x30, 0x44, 0x62, 0x7c, 0x22, 0x1f, 0x0d, 0x05, 0x67, 0x2c, 0x2a, 0x39, 0x21, 0x46, |
184 | 0x4b, 0x38, 0x32, 0x06, 0x7a, 0x3e, 0x7f, 0x0c, 0x40, 0x18, 0x6b, 0x2d, 0x60, 0x4c, 0x60, 0x0c, 0x23, 0x43, 0x3b, 0x3e, 0x1b, 0x16, | 184 | 0x08, 0x50, 0x66, 0x34, 0x37, 0x0b, 0x45, 0x4b, 0x38, 0x32, 0x06, 0x7a, 0x3e, 0x7f, 0x0c, |
185 | 0x04, 0x46, 0x58, 0x3f, 0x40, 0x6a, 0x11, 0x05, 0x63, 0x71, 0x14, 0x35, 0x47, 0x79, 0x13, 0x6f, 0x6b, 0x27, 0x18, 0x5b, 0x48, 0x27, | 185 | 0x40, 0x18, 0x6b, 0x2d, 0x60, 0x4c, 0x60, 0x0c, 0x23, 0x43, 0x3b, 0x3e, 0x1b, 0x16, 0x04, |
186 | 0x3e, 0x6f, 0x15, 0x33, 0x4f, 0x3e, 0x5e, 0x51, 0x73, 0x68, 0x25, 0x0f, 0x06, 0x5b, 0x7c, 0x72, 0x75, 0x3e, 0x3f, 0x1b, 0x5c, 0x6d, | 186 | 0x46, 0x58, 0x3f, 0x40, 0x6a, 0x11, 0x05, 0x63, 0x71, 0x14, 0x35, 0x47, 0x79, 0x13, 0x6f, |
187 | 0x6a, 0x39, 0x7c, 0x63, 0x63, 0x60, 0x6c, 0x7a, 0x33, 0x76, 0x52, 0x13, 0x25, 0x33, 0x7d, 0x65, 0x23, 0x27, 0x11, 0x06, 0x06, 0x47, | 187 | 0x6b, 0x27, 0x18, 0x5b, 0x48, 0x27, 0x3e, 0x6f, 0x15, 0x33, 0x4f, 0x3e, 0x5e, 0x51, 0x73, |
188 | 0x71, 0x1e, 0x14, 0x74, 0x63, 0x70, 0x2d, 0x15, 0x27, 0x18, 0x51, 0x06, 0x05, 0x33, 0x11, 0x2c, 0x6b, 0x00, 0x2d, 0x77, 0x20, 0x48, | 188 | 0x68, 0x25, 0x0f, 0x06, 0x5b, 0x7c, 0x72, 0x75, 0x3e, 0x3f, 0x1b, 0x5c, 0x6d, 0x6a, 0x39, |
189 | 0x0d, 0x73, 0x51, 0x45, 0x25, 0x7f, 0x7f, 0x35, 0x26, 0x2e, 0x26, 0x53, 0x24, 0x68, 0x1e, 0x0e, 0x58, 0x3a, 0x59, 0x50, 0x56, 0x37, | 189 | 0x7c, 0x63, 0x63, 0x60, 0x6c, 0x7a, 0x33, 0x76, 0x52, 0x13, 0x25, 0x33, 0x7d, 0x65, 0x23, |
190 | 0x5f, 0x66, 0x01, 0x4c, 0x5a, 0x64, 0x32, 0x50, 0x7b, 0x6a, 0x20, 0x72, 0x2b, 0x1d, 0x7e, 0x43, 0x7b, 0x61, 0x42, 0x0b, 0x61, 0x73, | 190 | 0x27, 0x11, 0x06, 0x06, 0x47, 0x71, 0x1e, 0x14, 0x74, 0x63, 0x70, 0x2d, 0x15, 0x27, 0x18, |
191 | 0x24, 0x79, 0x3a, 0x6b, 0x4a, 0x79, 0x6e, 0x09, 0x0f, 0x27, 0x2d, 0x0c, 0x5e, 0x32, 0x4b, 0x0d, 0x79, 0x46, 0x39, 0x21, 0x0a, 0x26, | 191 | 0x51, 0x06, 0x05, 0x33, 0x11, 0x2c, 0x6b, 0x00, 0x2d, 0x77, 0x20, 0x48, 0x0d, 0x73, 0x51, |
192 | 0x5f, 0x3a, 0x00, 0x26, 0x3f, 0x13, 0x2e, 0x7e, 0x50, 0x2b, 0x67, 0x46, 0x72, 0x3f, 0x3b, 0x01, 0x46, 0x1b, 0x0b, 0x35, 0x49, 0x39, | 192 | 0x45, 0x25, 0x7f, 0x7f, 0x35, 0x26, 0x2e, 0x26, 0x53, 0x24, 0x68, 0x1e, 0x0e, 0x58, 0x3a, |
193 | 0x19, 0x70, 0x3d, 0x02, 0x41, 0x0e, 0x38, 0x05, 0x76, 0x65, 0x4f, 0x31, 0x6c, 0x5e, 0x17, 0x04, 0x15, 0x36, 0x26, 0x64, 0x34, 0x14, | 193 | 0x59, 0x50, 0x56, 0x37, 0x5f, 0x66, 0x01, 0x4c, 0x5a, 0x64, 0x32, 0x50, 0x7b, 0x6a, 0x20, |
194 | 0x17, 0x7c, 0x0e, 0x0b, 0x5b, 0x55, 0x53, 0x6b, 0x00, 0x42, 0x41, 0x4f, 0x02, 0x5c, 0x13, 0x0a, 0x2c, 0x2c, 0x3e, 0x10, 0x14, 0x33, | 194 | 0x72, 0x2b, 0x1d, 0x7e, 0x43, 0x7b, 0x61, 0x42, 0x0b, 0x61, 0x73, 0x24, 0x79, 0x3a, 0x6b, |
195 | 0x45, 0x7c, 0x7a, 0x5a, 0x31, 0x61, 0x39, 0x08, 0x22, 0x6a, 0x1e, 0x0f, 0x6f, 0x1b, 0x6c, 0x13, 0x5e, 0x79, 0x20, 0x79, 0x50, 0x62, | 195 | 0x4a, 0x79, 0x6e, 0x09, 0x0f, 0x27, 0x2d, 0x0c, 0x5e, 0x32, 0x4b, 0x0d, 0x79, 0x46, 0x39, |
196 | 0x06, 0x2c, 0x76, 0x17, 0x04, 0x2b, 0x2a, 0x75, 0x1f, 0x0c, 0x37, 0x4e, 0x0f, 0x7b, 0x2d, 0x34, 0x75, 0x60, 0x31, 0x74, 0x2e, 0x0a, | 196 | 0x21, 0x0a, 0x26, 0x5f, 0x3a, 0x00, 0x26, 0x3f, 0x13, 0x2e, 0x7e, 0x50, 0x2b, 0x67, 0x46, |
197 | 0x4a, 0x11, 0x6c, 0x49, 0x25, 0x01, 0x3a, 0x3d, 0x22, 0x1e, 0x6d, 0x18, 0x51, 0x78, 0x2d, 0x62, 0x31, 0x4c, 0x50, 0x40, 0x17, 0x4b, | 197 | 0x72, 0x3f, 0x3b, 0x01, 0x46, 0x1b, 0x0b, 0x35, 0x49, 0x39, 0x19, 0x70, 0x3d, 0x02, 0x41, |
198 | 0x6f, 0x22, 0x00, 0x7f, 0x61, 0x2a, 0x34, 0x3e, 0x00, 0x5f, 0x2f, 0x5f, 0x2f, 0x14, 0x2a, 0x55, 0x27, 0x1f, 0x46, 0x1f, 0x12, 0x46, | 198 | 0x0e, 0x38, 0x05, 0x76, 0x65, 0x4f, 0x31, 0x6c, 0x5e, 0x17, 0x04, 0x15, 0x36, 0x26, 0x64, |
199 | 0x5e, 0x1e, 0x0c, 0x7c, 0x38, 0x01, 0x61, 0x64, 0x76, 0x22, 0x6e, 0x08, 0x20, 0x38, 0x4f, 0x73, 0x72, 0x55, 0x12, 0x42, 0x19, 0x50, | 199 | 0x34, 0x14, 0x17, 0x7c, 0x0e, 0x0b, 0x5b, 0x55, 0x53, 0x6b, 0x00, 0x42, 0x41, 0x4f, 0x02, |
200 | 0x61, 0x43, 0x77, 0x7d, 0x41, 0x2e, 0x35, 0x4f, 0x3d, 0x31, 0x28, 0x58, 0x67, 0x1b, 0x03, 0x51, 0x20, 0x32, 0x1c, 0x08, 0x6e, 0x37, | 200 | 0x5c, 0x13, 0x0a, 0x2c, 0x2c, 0x3e, 0x10, 0x14, 0x33, 0x45, 0x7c, 0x7a, 0x5a, 0x31, 0x61, |
201 | 0x75, 0x37, 0x44, 0x4f, 0x68, 0x19, 0x07, 0x64, 0x14, 0x28, 0x25, 0x2b, 0x69, 0x35, 0x18, 0x27, 0x26, 0x14, 0x13, 0x70, 0x42, 0x19, | 201 | 0x39, 0x08, 0x22, 0x6a, 0x1e, 0x0f, 0x6f, 0x1b, 0x6c, 0x13, 0x5e, 0x79, 0x20, 0x79, 0x50, |
202 | 0x12, 0x75, 0x3e, 0x02, 0x5d, 0x7c, 0x13, 0x1f, 0x16, 0x53, 0x3b, 0x74, 0x48, 0x3c, 0x5e, 0x39, 0x6c, 0x1c, 0x1c, 0x74, 0x39, 0x1f, | 202 | 0x62, 0x06, 0x2c, 0x76, 0x17, 0x04, 0x2b, 0x2a, 0x75, 0x1f, 0x0c, 0x37, 0x4e, 0x0f, 0x7b, |
203 | 0x00, 0x1b, 0x06, 0x0a, 0x68, 0x3b, 0x52, 0x4f, 0x1e, 0x6e, 0x3c, 0x35, 0x0c, 0x38, 0x0e, 0x0b, 0x3b, 0x1a, 0x76, 0x23, 0x29, 0x53, | 203 | 0x2d, 0x34, 0x75, 0x60, 0x31, 0x74, 0x2e, 0x0a, 0x4a, 0x11, 0x6c, 0x49, 0x25, 0x01, 0x3a, |
204 | 0x1e, 0x5f, 0x41, 0x0c, 0x4b, 0x0a, 0x65, 0x28, 0x78, 0x67, 0x48, 0x59, 0x26, 0x6d, 0x31, 0x76, 0x23, 0x70, 0x61, 0x64, 0x3b, 0x38, | 204 | 0x3d, 0x22, 0x1e, 0x6d, 0x18, 0x51, 0x78, 0x2d, 0x62, 0x31, 0x4c, 0x50, 0x40, 0x17, 0x4b, |
205 | 0x79, 0x66, 0x74, 0x53, 0x2c, 0x64, 0x64, 0x54, 0x03, 0x54, 0x65, 0x44, 0x4c, 0x18, 0x4f, 0x48, 0x20, 0x4f, 0x72, 0x10, 0x3f, 0x0c, | 205 | 0x6f, 0x22, 0x00, 0x7f, 0x61, 0x2a, 0x34, 0x3e, 0x00, 0x5f, 0x2f, 0x5f, 0x2f, 0x14, 0x2a, |
206 | 0x52, 0x2d, 0x03, 0x14, 0x03, 0x51, 0x42, 0x10, 0x77, 0x6a, 0x34, 0x06, 0x32, 0x03, 0x72, 0x14, 0x7c, 0x08, 0x5d, 0x52, 0x1a, 0x62, | 206 | 0x55, 0x27, 0x1f, 0x46, 0x1f, 0x12, 0x46, 0x5e, 0x1e, 0x0c, 0x7c, 0x38, 0x01, 0x61, 0x64, |
207 | 0x7c, 0x3e, 0x30, 0x7e, 0x5f, 0x7f, 0x54, 0x0f, 0x44, 0x49, 0x5d, 0x5e, 0x10, 0x6a, 0x06, 0x2b, 0x06, 0x53, 0x10, 0x39, 0x37, 0x32, | 207 | 0x76, 0x22, 0x6e, 0x08, 0x20, 0x38, 0x4f, 0x73, 0x72, 0x55, 0x12, 0x42, 0x19, 0x50, 0x61, |
208 | 0x4a, 0x4e, 0x3d, 0x2b, 0x65, 0x38, 0x39, 0x07, 0x72, 0x54, 0x64, 0x4d, 0x56, 0x6a, 0x03, 0x22, 0x70, 0x7b, 0x5f, 0x60, 0x0b, 0x2a, | 208 | 0x43, 0x77, 0x7d, 0x41, 0x2e, 0x35, 0x4f, 0x3d, 0x31, 0x28, 0x58, 0x67, 0x1b, 0x03, 0x51, |
209 | 0x0b, 0x6b, 0x10, 0x64, 0x14, 0x05, 0x22, 0x00, 0x73, 0x40, 0x23, 0x5b, 0x51, 0x1f, 0x2b, 0x1a, 0x5d, 0x69, 0x7a, 0x46, 0x0c, 0x5f, | 209 | 0x20, 0x32, 0x1c, 0x08, 0x6e, 0x37, 0x75, 0x37, 0x44, 0x4f, 0x68, 0x19, 0x07, 0x64, 0x14, |
210 | 0x32, 0x4b, 0x4a, 0x28, 0x52, 0x79, 0x5b, 0x12, 0x42, 0x18, 0x00, 0x5d, 0x27, 0x31, 0x53, 0x3c, 0x4c, 0x36, 0x4e, 0x38, 0x3f, 0x72, | 210 | 0x28, 0x25, 0x2b, 0x69, 0x35, 0x18, 0x27, 0x26, 0x14, 0x13, 0x70, 0x42, 0x19, 0x12, 0x75, |
211 | 0x03, 0x71, 0x02, 0x5b, 0x36, 0x59, 0x7f, 0x75, 0x6e, 0x08, 0x54, 0x0d, 0x34, 0x1c, 0x34, 0x57, 0x5d, 0x69, 0x48, 0x00, 0x3b, 0x05, | 211 | 0x3e, 0x02, 0x5d, 0x7c, 0x13, 0x1f, 0x16, 0x53, 0x3b, 0x74, 0x48, 0x3c, 0x5e, 0x39, 0x6c, |
212 | 0x07, 0x6e, 0x27, 0x65, 0x6e, 0x40, 0x3d, 0x3a, 0x4f, 0x72, 0x5d, 0x39, 0x16, 0x0f, 0x63, 0x12, 0x12, 0x15, 0x3a, 0x70, 0x0d, 0x57, | 212 | 0x1c, 0x1c, 0x74, 0x39, 0x1f, 0x00, 0x1b, 0x06, 0x0a, 0x68, 0x3b, 0x52, 0x4f, 0x1e, 0x6e, |
213 | 0x18, 0x0d, 0x5e, 0x3d, 0x22, 0x68, 0x68, 0x7c, 0x6d, 0x4f, 0x0c, 0x7b, 0x09, 0x2d, 0x4a, 0x73, 0x20, 0x47, 0x07, 0x57, 0x75, 0x5d, | 213 | 0x3c, 0x35, 0x0c, 0x38, 0x0e, 0x0b, 0x3b, 0x1a, 0x76, 0x23, 0x29, 0x53, 0x1e, 0x5f, 0x41, |
214 | 0x53, 0x70, 0x34, 0x21, 0x40, 0x57, 0x51, 0x5e, 0x49, 0x44, 0x00, 0x54, 0x27, 0x04, 0x68, 0x7e, 0x59, 0x56, 0x58, 0x74, 0x14, 0x3c, | 214 | 0x0c, 0x4b, 0x0a, 0x65, 0x28, 0x78, 0x67, 0x48, 0x59, 0x26, 0x6d, 0x31, 0x76, 0x23, 0x70, |
215 | 0x16, 0x33, 0x41, 0x16, 0x4b, 0x2f, 0x49, 0x37, 0x0a, 0x54, 0x08, 0x08, 0x1f, 0x39, 0x67, 0x76, 0x28, 0x28, 0x07, 0x1d, 0x61, 0x47, | 215 | 0x61, 0x64, 0x3b, 0x38, 0x79, 0x66, 0x74, 0x53, 0x2c, 0x64, 0x64, 0x54, 0x03, 0x54, 0x65, |
216 | 0x51, 0x4d, 0x75, 0x26, 0x52, 0x47, 0x47, 0x0c, 0x57, 0x58, 0x74, 0x3e, 0x62, 0x6c, 0x58, 0x3a, 0x44, 0x1e, 0x16, 0x2e, 0x21, 0x1c, | 216 | 0x44, 0x4c, 0x18, 0x4f, 0x48, 0x20, 0x4f, 0x72, 0x10, 0x3f, 0x0c, 0x52, 0x2d, 0x03, 0x14, |
217 | 0x73, 0x45, 0x67, 0x74, 0x4f, 0x33, 0x66, 0x0e, 0x74, 0x66, 0x26, 0x1f, 0x2e, 0x38, 0x44, 0x40, 0x7e, 0x2a, 0x50, 0x52, 0x5e, 0x43, | 217 | 0x03, 0x51, 0x42, 0x10, 0x77, 0x6a, 0x34, 0x06, 0x32, 0x03, 0x72, 0x14, 0x7c, 0x08, 0x5d, |
218 | 0x01, 0x7a, 0x38, 0x49, 0x3c, 0x55, 0x4d, 0x5a, 0x44, 0x08, 0x26, 0x59, 0x4d, 0x45, 0x0b, 0x48, 0x0a, 0x33, 0x5e, 0x4a, 0x4d, 0x75, | 218 | 0x52, 0x1a, 0x62, 0x7c, 0x3e, 0x30, 0x7e, 0x5f, 0x7f, 0x54, 0x0f, 0x44, 0x49, 0x5d, 0x5e, |
219 | 0x16, 0x17, 0x63, 0x46, 0x01, 0x2a, 0x55, 0x7b, 0x0f, 0x02, 0x73, 0x6a, 0x4b, 0x7f, 0x75, 0x65, 0x3c, 0x4c, 0x33, 0x39, 0x6c, 0x74, | 219 | 0x10, 0x6a, 0x06, 0x2b, 0x06, 0x53, 0x10, 0x39, 0x37, 0x32, 0x4a, 0x4e, 0x3d, 0x2b, 0x65, |
220 | 0x05, 0x60, 0x0f, 0x7f, 0x2d, 0x41, 0x4d, 0x4d, 0x46, 0x71, 0x09, 0x6f, 0x4f, 0x60, 0x15, 0x0f, 0x46, 0x73, 0x63, 0x4c, 0x5e, 0x74, | 220 | 0x38, 0x39, 0x07, 0x72, 0x54, 0x64, 0x4d, 0x56, 0x6a, 0x03, 0x22, 0x70, 0x7b, 0x5f, 0x60, |
221 | 0x30, 0x0d, 0x28, 0x43, 0x08, 0x72, 0x32, 0x04, 0x2e, 0x31, 0x29, 0x27, 0x44, 0x6d, 0x13, 0x17, 0x48, 0x0f, 0x49, 0x52, 0x10, 0x13, | 221 | 0x0b, 0x2a, 0x0b, 0x6b, 0x10, 0x64, 0x14, 0x05, 0x22, 0x00, 0x73, 0x40, 0x23, 0x5b, 0x51, |
222 | 0x7f, 0x17, 0x16, 0x62, 0x79, 0x35, 0x78, 0x3e, 0x01, 0x7c, 0x2e, 0x0f, 0x76, 0x3e, 0x5e, 0x53, 0x6c, 0x5b, 0x5f, 0x7c, 0x19, 0x41, | 222 | 0x1f, 0x2b, 0x1a, 0x5d, 0x69, 0x7a, 0x46, 0x0c, 0x5f, 0x32, 0x4b, 0x4a, 0x28, 0x52, 0x79, |
223 | 0x02, 0x2f, 0x17, 0x64, 0x41, 0x75, 0x10, 0x04, 0x47, 0x7c, 0x3d, 0x4b, 0x52, 0x00, 0x10, 0x5d, 0x51, 0x4e, 0x7a, 0x27, 0x25, 0x55, | 223 | 0x5b, 0x12, 0x42, 0x18, 0x00, 0x5d, 0x27, 0x31, 0x53, 0x3c, 0x4c, 0x36, 0x4e, 0x38, 0x3f, |
224 | 0x40, 0x12, 0x35, 0x60, 0x05, 0x1b, 0x34, 0x2d, 0x04, 0x7a, 0x6a, 0x69, 0x02, 0x79, 0x03, 0x3a, 0x2f, 0x06, 0x0a, 0x79, 0x7b, 0x12, | 224 | 0x72, 0x03, 0x71, 0x02, 0x5b, 0x36, 0x59, 0x7f, 0x75, 0x6e, 0x08, 0x54, 0x0d, 0x34, 0x1c, |
225 | 0x5d, 0x7c, 0x52, 0x29, 0x47, 0x58, 0x12, 0x73, 0x3f, 0x27, 0x56, 0x05, 0x0c, 0x48, 0x32, 0x58, 0x6b, 0x57, 0x5c, 0x03, 0x64, 0x56, | 225 | 0x34, 0x57, 0x5d, 0x69, 0x48, 0x00, 0x3b, 0x05, 0x07, 0x6e, 0x27, 0x65, 0x6e, 0x40, 0x3d, |
226 | 0x11, 0x52, 0x7a, 0x30, 0x36, 0x29, 0x17, 0x3b, 0x68, 0x7a, 0x7c, 0x05, 0x6b, 0x6b, 0x13, 0x6a, 0x24, 0x5c, 0x68, 0x42, 0x18, 0x32, | 226 | 0x3a, 0x4f, 0x72, 0x5d, 0x39, 0x16, 0x0f, 0x63, 0x12, 0x12, 0x15, 0x3a, 0x70, 0x0d, 0x57, |
227 | 0x03, 0x73, 0x6e, 0x04, 0x21, 0x2e, 0x01, 0x04, 0x63, 0x7d, 0x44, 0x41, 0x12, 0x31, 0x0b, 0x15, 0x1f, 0x70, 0x00, 0x2e, 0x66, 0x14, | 227 | 0x18, 0x0d, 0x5e, 0x3d, 0x22, 0x68, 0x68, 0x7c, 0x6d, 0x4f, 0x0c, 0x7b, 0x09, 0x2d, 0x4a, |
228 | 0x3c, 0x7f, 0x2b, 0x00, 0x1f, 0x0c, 0x28, 0x59, 0x0a, 0x16, 0x49, 0x5a, 0x5c, 0x64, 0x65, 0x4b, 0x11, 0x29, 0x15, 0x36, 0x5a, 0x65, | 228 | 0x73, 0x20, 0x47, 0x07, 0x57, 0x75, 0x5d, 0x53, 0x70, 0x34, 0x21, 0x40, 0x57, 0x51, 0x5e, |
229 | 0x19, 0x4f, 0x60, 0x23, 0x3a, 0x3a, 0x13, 0x25, 0x02, 0x78, 0x4c, 0x54}; | 229 | 0x49, 0x44, 0x00, 0x54, 0x27, 0x04, 0x68, 0x7e, 0x59, 0x56, 0x58, 0x74, 0x14, 0x3c, 0x16, |
230 | 0x33, 0x41, 0x16, 0x4b, 0x2f, 0x49, 0x37, 0x0a, 0x54, 0x08, 0x08, 0x1f, 0x39, 0x67, 0x76, | ||
231 | 0x28, 0x28, 0x07, 0x1d, 0x61, 0x47, 0x51, 0x4d, 0x75, 0x26, 0x52, 0x47, 0x47, 0x0c, 0x57, | ||
232 | 0x58, 0x74, 0x3e, 0x62, 0x6c, 0x58, 0x3a, 0x44, 0x1e, 0x16, 0x2e, 0x21, 0x1c, 0x73, 0x45, | ||
233 | 0x67, 0x74, 0x4f, 0x33, 0x66, 0x0e, 0x74, 0x66, 0x26, 0x1f, 0x2e, 0x38, 0x44, 0x40, 0x7e, | ||
234 | 0x2a, 0x50, 0x52, 0x5e, 0x43, 0x01, 0x7a, 0x38, 0x49, 0x3c, 0x55, 0x4d, 0x5a, 0x44, 0x08, | ||
235 | 0x26, 0x59, 0x4d, 0x45, 0x0b, 0x48, 0x0a, 0x33, 0x5e, 0x4a, 0x4d, 0x75, 0x16, 0x17, 0x63, | ||
236 | 0x46, 0x01, 0x2a, 0x55, 0x7b, 0x0f, 0x02, 0x73, 0x6a, 0x4b, 0x7f, 0x75, 0x65, 0x3c, 0x4c, | ||
237 | 0x33, 0x39, 0x6c, 0x74, 0x05, 0x60, 0x0f, 0x7f, 0x2d, 0x41, 0x4d, 0x4d, 0x46, 0x71, 0x09, | ||
238 | 0x6f, 0x4f, 0x60, 0x15, 0x0f, 0x46, 0x73, 0x63, 0x4c, 0x5e, 0x74, 0x30, 0x0d, 0x28, 0x43, | ||
239 | 0x08, 0x72, 0x32, 0x04, 0x2e, 0x31, 0x29, 0x27, 0x44, 0x6d, 0x13, 0x17, 0x48, 0x0f, 0x49, | ||
240 | 0x52, 0x10, 0x13, 0x7f, 0x17, 0x16, 0x62, 0x79, 0x35, 0x78, 0x3e, 0x01, 0x7c, 0x2e, 0x0f, | ||
241 | 0x76, 0x3e, 0x5e, 0x53, 0x6c, 0x5b, 0x5f, 0x7c, 0x19, 0x41, 0x02, 0x2f, 0x17, 0x64, 0x41, | ||
242 | 0x75, 0x10, 0x04, 0x47, 0x7c, 0x3d, 0x4b, 0x52, 0x00, 0x10, 0x5d, 0x51, 0x4e, 0x7a, 0x27, | ||
243 | 0x25, 0x55, 0x40, 0x12, 0x35, 0x60, 0x05, 0x1b, 0x34, 0x2d, 0x04, 0x7a, 0x6a, 0x69, 0x02, | ||
244 | 0x79, 0x03, 0x3a, 0x2f, 0x06, 0x0a, 0x79, 0x7b, 0x12, 0x5d, 0x7c, 0x52, 0x29, 0x47, 0x58, | ||
245 | 0x12, 0x73, 0x3f, 0x27, 0x56, 0x05, 0x0c, 0x48, 0x32, 0x58, 0x6b, 0x57, 0x5c, 0x03, 0x64, | ||
246 | 0x56, 0x11, 0x52, 0x7a, 0x30, 0x36, 0x29, 0x17, 0x3b, 0x68, 0x7a, 0x7c, 0x05, 0x6b, 0x6b, | ||
247 | 0x13, 0x6a, 0x24, 0x5c, 0x68, 0x42, 0x18, 0x32, 0x03, 0x73, 0x6e, 0x04, 0x21, 0x2e, 0x01, | ||
248 | 0x04, 0x63, 0x7d, 0x44, 0x41, 0x12, 0x31, 0x0b, 0x15, 0x1f, 0x70, 0x00, 0x2e, 0x66, 0x14, | ||
249 | 0x3c, 0x7f, 0x2b, 0x00, 0x1f, 0x0c, 0x28, 0x59, 0x0a, 0x16, 0x49, 0x5a, 0x5c, 0x64, 0x65, | ||
250 | 0x4b, 0x11, 0x29, 0x15, 0x36, 0x5a, 0x65, 0x19, 0x4f, 0x60, 0x23, 0x3a, 0x3a, 0x13, 0x25, | ||
251 | 0x02, 0x78, 0x4c, 0x54}; | ||
230 | char b64_known[1369] = { | 252 | char b64_known[1369] = { |
231 | 0x43, 0x7a, 0x42, 0x45, 0x59, 0x6e, 0x77, 0x69, 0x48, 0x77, 0x30, 0x46, 0x5a, 0x79, 0x77, 0x71, 0x4f, 0x53, 0x46, 0x47, 0x43, 0x46, | 253 | 0x43, 0x7a, 0x42, 0x45, 0x59, 0x6e, 0x77, 0x69, 0x48, 0x77, 0x30, 0x46, 0x5a, 0x79, 0x77, |
232 | 0x42, 0x6d, 0x4e, 0x44, 0x63, 0x4c, 0x52, 0x55, 0x73, 0x34, 0x4d, 0x67, 0x5a, 0x36, 0x50, 0x6e, 0x38, 0x4d, 0x51, 0x42, 0x68, 0x72, | 254 | 0x71, 0x4f, 0x53, 0x46, 0x47, 0x43, 0x46, 0x42, 0x6d, 0x4e, 0x44, 0x63, 0x4c, 0x52, 0x55, |
233 | 0x4c, 0x57, 0x42, 0x4d, 0x59, 0x41, 0x77, 0x6a, 0x51, 0x7a, 0x73, 0x2b, 0x47, 0x78, 0x59, 0x45, 0x52, 0x6c, 0x67, 0x2f, 0x51, 0x47, | 255 | 0x73, 0x34, 0x4d, 0x67, 0x5a, 0x36, 0x50, 0x6e, 0x38, 0x4d, 0x51, 0x42, 0x68, 0x72, 0x4c, |
234 | 0x6f, 0x52, 0x42, 0x57, 0x4e, 0x78, 0x46, 0x44, 0x56, 0x48, 0x65, 0x52, 0x4e, 0x76, 0x61, 0x79, 0x63, 0x59, 0x57, 0x30, 0x67, 0x6e, | 256 | 0x57, 0x42, 0x4d, 0x59, 0x41, 0x77, 0x6a, 0x51, 0x7a, 0x73, 0x2b, 0x47, 0x78, 0x59, 0x45, |
235 | 0x50, 0x6d, 0x38, 0x56, 0x4d, 0x30, 0x38, 0x2b, 0x58, 0x6c, 0x46, 0x7a, 0x61, 0x43, 0x55, 0x50, 0x42, 0x6c, 0x74, 0x38, 0x63, 0x6e, | 257 | 0x52, 0x6c, 0x67, 0x2f, 0x51, 0x47, 0x6f, 0x52, 0x42, 0x57, 0x4e, 0x78, 0x46, 0x44, 0x56, |
236 | 0x55, 0x2b, 0x50, 0x78, 0x74, 0x63, 0x62, 0x57, 0x6f, 0x35, 0x66, 0x47, 0x4e, 0x6a, 0x59, 0x47, 0x78, 0x36, 0x4d, 0x33, 0x5a, 0x53, | 258 | 0x48, 0x65, 0x52, 0x4e, 0x76, 0x61, 0x79, 0x63, 0x59, 0x57, 0x30, 0x67, 0x6e, 0x50, 0x6d, |
237 | 0x45, 0x79, 0x55, 0x7a, 0x66, 0x57, 0x55, 0x6a, 0x4a, 0x78, 0x45, 0x47, 0x42, 0x6b, 0x64, 0x78, 0x48, 0x68, 0x52, 0x30, 0x59, 0x33, | 259 | 0x38, 0x56, 0x4d, 0x30, 0x38, 0x2b, 0x58, 0x6c, 0x46, 0x7a, 0x61, 0x43, 0x55, 0x50, 0x42, |
238 | 0x41, 0x74, 0x46, 0x53, 0x63, 0x59, 0x55, 0x51, 0x59, 0x46, 0x4d, 0x78, 0x45, 0x73, 0x61, 0x77, 0x41, 0x74, 0x64, 0x79, 0x42, 0x49, | 260 | 0x6c, 0x74, 0x38, 0x63, 0x6e, 0x55, 0x2b, 0x50, 0x78, 0x74, 0x63, 0x62, 0x57, 0x6f, 0x35, |
239 | 0x44, 0x58, 0x4e, 0x52, 0x52, 0x53, 0x56, 0x2f, 0x66, 0x7a, 0x55, 0x6d, 0x4c, 0x69, 0x5a, 0x54, 0x4a, 0x47, 0x67, 0x65, 0x44, 0x6c, | 261 | 0x66, 0x47, 0x4e, 0x6a, 0x59, 0x47, 0x78, 0x36, 0x4d, 0x33, 0x5a, 0x53, 0x45, 0x79, 0x55, |
240 | 0x67, 0x36, 0x57, 0x56, 0x42, 0x57, 0x4e, 0x31, 0x39, 0x6d, 0x41, 0x55, 0x78, 0x61, 0x5a, 0x44, 0x4a, 0x51, 0x65, 0x32, 0x6f, 0x67, | 262 | 0x7a, 0x66, 0x57, 0x55, 0x6a, 0x4a, 0x78, 0x45, 0x47, 0x42, 0x6b, 0x64, 0x78, 0x48, 0x68, |
241 | 0x63, 0x69, 0x73, 0x64, 0x66, 0x6b, 0x4e, 0x37, 0x59, 0x55, 0x49, 0x4c, 0x59, 0x58, 0x4d, 0x6b, 0x65, 0x54, 0x70, 0x72, 0x53, 0x6e, | 263 | 0x52, 0x30, 0x59, 0x33, 0x41, 0x74, 0x46, 0x53, 0x63, 0x59, 0x55, 0x51, 0x59, 0x46, 0x4d, |
242 | 0x6c, 0x75, 0x43, 0x51, 0x38, 0x6e, 0x4c, 0x51, 0x78, 0x65, 0x4d, 0x6b, 0x73, 0x4e, 0x65, 0x55, 0x59, 0x35, 0x49, 0x51, 0x6f, 0x6d, | 264 | 0x78, 0x45, 0x73, 0x61, 0x77, 0x41, 0x74, 0x64, 0x79, 0x42, 0x49, 0x44, 0x58, 0x4e, 0x52, |
243 | 0x58, 0x7a, 0x6f, 0x41, 0x4a, 0x6a, 0x38, 0x54, 0x4c, 0x6e, 0x35, 0x51, 0x4b, 0x32, 0x64, 0x47, 0x63, 0x6a, 0x38, 0x37, 0x41, 0x55, | 265 | 0x52, 0x53, 0x56, 0x2f, 0x66, 0x7a, 0x55, 0x6d, 0x4c, 0x69, 0x5a, 0x54, 0x4a, 0x47, 0x67, |
244 | 0x59, 0x62, 0x43, 0x7a, 0x56, 0x4a, 0x4f, 0x52, 0x6c, 0x77, 0x50, 0x51, 0x4a, 0x42, 0x44, 0x6a, 0x67, 0x46, 0x64, 0x6d, 0x56, 0x50, | 266 | 0x65, 0x44, 0x6c, 0x67, 0x36, 0x57, 0x56, 0x42, 0x57, 0x4e, 0x31, 0x39, 0x6d, 0x41, 0x55, |
245 | 0x4d, 0x57, 0x78, 0x65, 0x46, 0x77, 0x51, 0x56, 0x4e, 0x69, 0x5a, 0x6b, 0x4e, 0x42, 0x51, 0x58, 0x66, 0x41, 0x34, 0x4c, 0x57, 0x31, | 267 | 0x78, 0x61, 0x5a, 0x44, 0x4a, 0x51, 0x65, 0x32, 0x6f, 0x67, 0x63, 0x69, 0x73, 0x64, 0x66, |
246 | 0x56, 0x54, 0x61, 0x77, 0x42, 0x43, 0x51, 0x55, 0x38, 0x43, 0x58, 0x42, 0x4d, 0x4b, 0x4c, 0x43, 0x77, 0x2b, 0x45, 0x42, 0x51, 0x7a, | 268 | 0x6b, 0x4e, 0x37, 0x59, 0x55, 0x49, 0x4c, 0x59, 0x58, 0x4d, 0x6b, 0x65, 0x54, 0x70, 0x72, |
247 | 0x52, 0x58, 0x78, 0x36, 0x57, 0x6a, 0x46, 0x68, 0x4f, 0x51, 0x67, 0x69, 0x61, 0x68, 0x34, 0x50, 0x62, 0x78, 0x74, 0x73, 0x45, 0x31, | 269 | 0x53, 0x6e, 0x6c, 0x75, 0x43, 0x51, 0x38, 0x6e, 0x4c, 0x51, 0x78, 0x65, 0x4d, 0x6b, 0x73, |
248 | 0x35, 0x35, 0x49, 0x48, 0x6c, 0x51, 0x59, 0x67, 0x59, 0x73, 0x64, 0x68, 0x63, 0x45, 0x4b, 0x79, 0x70, 0x31, 0x48, 0x77, 0x77, 0x33, | 270 | 0x4e, 0x65, 0x55, 0x59, 0x35, 0x49, 0x51, 0x6f, 0x6d, 0x58, 0x7a, 0x6f, 0x41, 0x4a, 0x6a, |
249 | 0x54, 0x67, 0x39, 0x37, 0x4c, 0x54, 0x52, 0x31, 0x59, 0x44, 0x46, 0x30, 0x4c, 0x67, 0x70, 0x4b, 0x45, 0x57, 0x78, 0x4a, 0x4a, 0x51, | 271 | 0x38, 0x54, 0x4c, 0x6e, 0x35, 0x51, 0x4b, 0x32, 0x64, 0x47, 0x63, 0x6a, 0x38, 0x37, 0x41, |
250 | 0x45, 0x36, 0x50, 0x53, 0x49, 0x65, 0x62, 0x52, 0x68, 0x52, 0x65, 0x43, 0x31, 0x69, 0x4d, 0x55, 0x78, 0x51, 0x51, 0x42, 0x64, 0x4c, | 272 | 0x55, 0x59, 0x62, 0x43, 0x7a, 0x56, 0x4a, 0x4f, 0x52, 0x6c, 0x77, 0x50, 0x51, 0x4a, 0x42, |
251 | 0x62, 0x79, 0x49, 0x41, 0x66, 0x32, 0x45, 0x71, 0x4e, 0x44, 0x34, 0x41, 0x58, 0x79, 0x39, 0x66, 0x4c, 0x78, 0x51, 0x71, 0x56, 0x53, | 273 | 0x44, 0x6a, 0x67, 0x46, 0x64, 0x6d, 0x56, 0x50, 0x4d, 0x57, 0x78, 0x65, 0x46, 0x77, 0x51, |
252 | 0x63, 0x66, 0x52, 0x68, 0x38, 0x53, 0x52, 0x6c, 0x34, 0x65, 0x44, 0x48, 0x77, 0x34, 0x41, 0x57, 0x46, 0x6b, 0x64, 0x69, 0x4a, 0x75, | 274 | 0x56, 0x4e, 0x69, 0x5a, 0x6b, 0x4e, 0x42, 0x51, 0x58, 0x66, 0x41, 0x34, 0x4c, 0x57, 0x31, |
253 | 0x43, 0x43, 0x41, 0x34, 0x54, 0x33, 0x4e, 0x79, 0x56, 0x52, 0x4a, 0x43, 0x47, 0x56, 0x42, 0x68, 0x51, 0x33, 0x64, 0x39, 0x51, 0x53, | 275 | 0x56, 0x54, 0x61, 0x77, 0x42, 0x43, 0x51, 0x55, 0x38, 0x43, 0x58, 0x42, 0x4d, 0x4b, 0x4c, |
254 | 0x34, 0x31, 0x54, 0x7a, 0x30, 0x78, 0x4b, 0x46, 0x68, 0x6e, 0x47, 0x77, 0x4e, 0x52, 0x49, 0x44, 0x49, 0x63, 0x43, 0x47, 0x34, 0x33, | 276 | 0x43, 0x77, 0x2b, 0x45, 0x42, 0x51, 0x7a, 0x52, 0x58, 0x78, 0x36, 0x57, 0x6a, 0x46, 0x68, |
255 | 0x64, 0x54, 0x64, 0x45, 0x54, 0x32, 0x67, 0x5a, 0x42, 0x32, 0x51, 0x55, 0x4b, 0x43, 0x55, 0x72, 0x61, 0x54, 0x55, 0x59, 0x4a, 0x79, | 277 | 0x4f, 0x51, 0x67, 0x69, 0x61, 0x68, 0x34, 0x50, 0x62, 0x78, 0x74, 0x73, 0x45, 0x31, 0x35, |
256 | 0x59, 0x55, 0x45, 0x33, 0x42, 0x43, 0x47, 0x52, 0x4a, 0x31, 0x50, 0x67, 0x4a, 0x64, 0x66, 0x42, 0x4d, 0x66, 0x46, 0x6c, 0x4d, 0x37, | 278 | 0x35, 0x49, 0x48, 0x6c, 0x51, 0x59, 0x67, 0x59, 0x73, 0x64, 0x68, 0x63, 0x45, 0x4b, 0x79, |
257 | 0x64, 0x45, 0x67, 0x38, 0x58, 0x6a, 0x6c, 0x73, 0x48, 0x42, 0x78, 0x30, 0x4f, 0x52, 0x38, 0x41, 0x47, 0x77, 0x59, 0x4b, 0x61, 0x44, | 279 | 0x70, 0x31, 0x48, 0x77, 0x77, 0x33, 0x54, 0x67, 0x39, 0x37, 0x4c, 0x54, 0x52, 0x31, 0x59, |
258 | 0x74, 0x53, 0x54, 0x78, 0x35, 0x75, 0x50, 0x44, 0x55, 0x4d, 0x4f, 0x41, 0x34, 0x4c, 0x4f, 0x78, 0x70, 0x32, 0x49, 0x79, 0x6c, 0x54, | 280 | 0x44, 0x46, 0x30, 0x4c, 0x67, 0x70, 0x4b, 0x45, 0x57, 0x78, 0x4a, 0x4a, 0x51, 0x45, 0x36, |
259 | 0x48, 0x6c, 0x39, 0x42, 0x44, 0x45, 0x73, 0x4b, 0x5a, 0x53, 0x68, 0x34, 0x5a, 0x30, 0x68, 0x5a, 0x4a, 0x6d, 0x30, 0x78, 0x64, 0x69, | 281 | 0x50, 0x53, 0x49, 0x65, 0x62, 0x52, 0x68, 0x52, 0x65, 0x43, 0x31, 0x69, 0x4d, 0x55, 0x78, |
260 | 0x4e, 0x77, 0x59, 0x57, 0x51, 0x37, 0x4f, 0x48, 0x6c, 0x6d, 0x64, 0x46, 0x4d, 0x73, 0x5a, 0x47, 0x52, 0x55, 0x41, 0x31, 0x52, 0x6c, | 282 | 0x51, 0x51, 0x42, 0x64, 0x4c, 0x62, 0x79, 0x49, 0x41, 0x66, 0x32, 0x45, 0x71, 0x4e, 0x44, |
261 | 0x52, 0x45, 0x77, 0x59, 0x54, 0x30, 0x67, 0x67, 0x54, 0x33, 0x49, 0x51, 0x50, 0x77, 0x78, 0x53, 0x4c, 0x51, 0x4d, 0x55, 0x41, 0x31, | 283 | 0x34, 0x41, 0x58, 0x79, 0x39, 0x66, 0x4c, 0x78, 0x51, 0x71, 0x56, 0x53, 0x63, 0x66, 0x52, |
262 | 0x46, 0x43, 0x45, 0x48, 0x64, 0x71, 0x4e, 0x41, 0x59, 0x79, 0x41, 0x33, 0x49, 0x55, 0x66, 0x41, 0x68, 0x64, 0x55, 0x68, 0x70, 0x69, | 284 | 0x68, 0x38, 0x53, 0x52, 0x6c, 0x34, 0x65, 0x44, 0x48, 0x77, 0x34, 0x41, 0x57, 0x46, 0x6b, |
263 | 0x66, 0x44, 0x34, 0x77, 0x66, 0x6c, 0x39, 0x2f, 0x56, 0x41, 0x39, 0x45, 0x53, 0x56, 0x31, 0x65, 0x45, 0x47, 0x6f, 0x47, 0x4b, 0x77, | 285 | 0x64, 0x69, 0x4a, 0x75, 0x43, 0x43, 0x41, 0x34, 0x54, 0x33, 0x4e, 0x79, 0x56, 0x52, 0x4a, |
264 | 0x5a, 0x54, 0x45, 0x44, 0x6b, 0x33, 0x4d, 0x6b, 0x70, 0x4f, 0x50, 0x53, 0x74, 0x6c, 0x4f, 0x44, 0x6b, 0x48, 0x63, 0x6c, 0x52, 0x6b, | 286 | 0x43, 0x47, 0x56, 0x42, 0x68, 0x51, 0x33, 0x64, 0x39, 0x51, 0x53, 0x34, 0x31, 0x54, 0x7a, |
265 | 0x54, 0x56, 0x5a, 0x71, 0x41, 0x79, 0x4a, 0x77, 0x65, 0x31, 0x39, 0x67, 0x43, 0x79, 0x6f, 0x4c, 0x61, 0x78, 0x42, 0x6b, 0x46, 0x41, | 287 | 0x30, 0x78, 0x4b, 0x46, 0x68, 0x6e, 0x47, 0x77, 0x4e, 0x52, 0x49, 0x44, 0x49, 0x63, 0x43, |
266 | 0x55, 0x69, 0x41, 0x48, 0x4e, 0x41, 0x49, 0x31, 0x74, 0x52, 0x48, 0x79, 0x73, 0x61, 0x58, 0x57, 0x6c, 0x36, 0x52, 0x67, 0x78, 0x66, | 288 | 0x47, 0x34, 0x33, 0x64, 0x54, 0x64, 0x45, 0x54, 0x32, 0x67, 0x5a, 0x42, 0x32, 0x51, 0x55, |
267 | 0x4d, 0x6b, 0x74, 0x4b, 0x4b, 0x46, 0x4a, 0x35, 0x57, 0x78, 0x4a, 0x43, 0x47, 0x41, 0x42, 0x64, 0x4a, 0x7a, 0x46, 0x54, 0x50, 0x45, | 289 | 0x4b, 0x43, 0x55, 0x72, 0x61, 0x54, 0x55, 0x59, 0x4a, 0x79, 0x59, 0x55, 0x45, 0x33, 0x42, |
268 | 0x77, 0x32, 0x54, 0x6a, 0x67, 0x2f, 0x63, 0x67, 0x4e, 0x78, 0x41, 0x6c, 0x73, 0x32, 0x57, 0x58, 0x39, 0x31, 0x62, 0x67, 0x68, 0x55, | 290 | 0x43, 0x47, 0x52, 0x4a, 0x31, 0x50, 0x67, 0x4a, 0x64, 0x66, 0x42, 0x4d, 0x66, 0x46, 0x6c, |
269 | 0x44, 0x54, 0x51, 0x63, 0x4e, 0x46, 0x64, 0x64, 0x61, 0x55, 0x67, 0x41, 0x4f, 0x77, 0x55, 0x48, 0x62, 0x69, 0x64, 0x6c, 0x62, 0x6b, | 291 | 0x4d, 0x37, 0x64, 0x45, 0x67, 0x38, 0x58, 0x6a, 0x6c, 0x73, 0x48, 0x42, 0x78, 0x30, 0x4f, |
270 | 0x41, 0x39, 0x4f, 0x6b, 0x39, 0x79, 0x58, 0x54, 0x6b, 0x57, 0x44, 0x32, 0x4d, 0x53, 0x45, 0x68, 0x55, 0x36, 0x63, 0x41, 0x31, 0x58, | 292 | 0x52, 0x38, 0x41, 0x47, 0x77, 0x59, 0x4b, 0x61, 0x44, 0x74, 0x53, 0x54, 0x78, 0x35, 0x75, |
271 | 0x47, 0x41, 0x31, 0x65, 0x50, 0x53, 0x4a, 0x6f, 0x61, 0x48, 0x78, 0x74, 0x54, 0x77, 0x78, 0x37, 0x43, 0x53, 0x31, 0x4b, 0x63, 0x79, | 293 | 0x50, 0x44, 0x55, 0x4d, 0x4f, 0x41, 0x34, 0x4c, 0x4f, 0x78, 0x70, 0x32, 0x49, 0x79, 0x6c, |
272 | 0x42, 0x48, 0x42, 0x31, 0x64, 0x31, 0x58, 0x56, 0x4e, 0x77, 0x4e, 0x43, 0x46, 0x41, 0x56, 0x31, 0x46, 0x65, 0x53, 0x55, 0x51, 0x41, | 294 | 0x54, 0x48, 0x6c, 0x39, 0x42, 0x44, 0x45, 0x73, 0x4b, 0x5a, 0x53, 0x68, 0x34, 0x5a, 0x30, |
273 | 0x56, 0x43, 0x63, 0x45, 0x61, 0x48, 0x35, 0x5a, 0x56, 0x6c, 0x68, 0x30, 0x46, 0x44, 0x77, 0x57, 0x4d, 0x30, 0x45, 0x57, 0x53, 0x79, | 295 | 0x68, 0x5a, 0x4a, 0x6d, 0x30, 0x78, 0x64, 0x69, 0x4e, 0x77, 0x59, 0x57, 0x51, 0x37, 0x4f, |
274 | 0x39, 0x4a, 0x4e, 0x77, 0x70, 0x55, 0x43, 0x41, 0x67, 0x66, 0x4f, 0x57, 0x64, 0x32, 0x4b, 0x43, 0x67, 0x48, 0x48, 0x57, 0x46, 0x48, | 296 | 0x48, 0x6c, 0x6d, 0x64, 0x46, 0x4d, 0x73, 0x5a, 0x47, 0x52, 0x55, 0x41, 0x31, 0x52, 0x6c, |
275 | 0x55, 0x55, 0x31, 0x31, 0x4a, 0x6c, 0x4a, 0x48, 0x52, 0x77, 0x78, 0x58, 0x57, 0x48, 0x51, 0x2b, 0x59, 0x6d, 0x78, 0x59, 0x4f, 0x6b, | 297 | 0x52, 0x45, 0x77, 0x59, 0x54, 0x30, 0x67, 0x67, 0x54, 0x33, 0x49, 0x51, 0x50, 0x77, 0x78, |
276 | 0x51, 0x65, 0x46, 0x69, 0x34, 0x68, 0x48, 0x48, 0x4e, 0x46, 0x5a, 0x33, 0x52, 0x50, 0x4d, 0x32, 0x59, 0x4f, 0x64, 0x47, 0x59, 0x6d, | 298 | 0x53, 0x4c, 0x51, 0x4d, 0x55, 0x41, 0x31, 0x46, 0x43, 0x45, 0x48, 0x64, 0x71, 0x4e, 0x41, |
277 | 0x48, 0x79, 0x34, 0x34, 0x52, 0x45, 0x42, 0x2b, 0x4b, 0x6c, 0x42, 0x53, 0x58, 0x6b, 0x4d, 0x42, 0x65, 0x6a, 0x68, 0x4a, 0x50, 0x46, | 299 | 0x59, 0x79, 0x41, 0x33, 0x49, 0x55, 0x66, 0x41, 0x68, 0x64, 0x55, 0x68, 0x70, 0x69, 0x66, |
278 | 0x56, 0x4e, 0x57, 0x6b, 0x51, 0x49, 0x4a, 0x6c, 0x6c, 0x4e, 0x52, 0x51, 0x74, 0x49, 0x43, 0x6a, 0x4e, 0x65, 0x53, 0x6b, 0x31, 0x31, | 300 | 0x44, 0x34, 0x77, 0x66, 0x6c, 0x39, 0x2f, 0x56, 0x41, 0x39, 0x45, 0x53, 0x56, 0x31, 0x65, |
279 | 0x46, 0x68, 0x64, 0x6a, 0x52, 0x67, 0x45, 0x71, 0x56, 0x58, 0x73, 0x50, 0x41, 0x6e, 0x4e, 0x71, 0x53, 0x33, 0x39, 0x31, 0x5a, 0x54, | 301 | 0x45, 0x47, 0x6f, 0x47, 0x4b, 0x77, 0x5a, 0x54, 0x45, 0x44, 0x6b, 0x33, 0x4d, 0x6b, 0x70, |
280 | 0x78, 0x4d, 0x4d, 0x7a, 0x6c, 0x73, 0x64, 0x41, 0x56, 0x67, 0x44, 0x33, 0x38, 0x74, 0x51, 0x55, 0x31, 0x4e, 0x52, 0x6e, 0x45, 0x4a, | 302 | 0x4f, 0x50, 0x53, 0x74, 0x6c, 0x4f, 0x44, 0x6b, 0x48, 0x63, 0x6c, 0x52, 0x6b, 0x54, 0x56, |
281 | 0x62, 0x30, 0x39, 0x67, 0x46, 0x51, 0x39, 0x47, 0x63, 0x32, 0x4e, 0x4d, 0x58, 0x6e, 0x51, 0x77, 0x44, 0x53, 0x68, 0x44, 0x43, 0x48, | 303 | 0x5a, 0x71, 0x41, 0x79, 0x4a, 0x77, 0x65, 0x31, 0x39, 0x67, 0x43, 0x79, 0x6f, 0x4c, 0x61, |
282 | 0x49, 0x79, 0x42, 0x43, 0x34, 0x78, 0x4b, 0x53, 0x64, 0x45, 0x62, 0x52, 0x4d, 0x58, 0x53, 0x41, 0x39, 0x4a, 0x55, 0x68, 0x41, 0x54, | 304 | 0x78, 0x42, 0x6b, 0x46, 0x41, 0x55, 0x69, 0x41, 0x48, 0x4e, 0x41, 0x49, 0x31, 0x74, 0x52, |
283 | 0x66, 0x78, 0x63, 0x57, 0x59, 0x6e, 0x6b, 0x31, 0x65, 0x44, 0x34, 0x42, 0x66, 0x43, 0x34, 0x50, 0x64, 0x6a, 0x35, 0x65, 0x55, 0x32, | 305 | 0x48, 0x79, 0x73, 0x61, 0x58, 0x57, 0x6c, 0x36, 0x52, 0x67, 0x78, 0x66, 0x4d, 0x6b, 0x74, |
284 | 0x78, 0x62, 0x58, 0x33, 0x77, 0x5a, 0x51, 0x51, 0x49, 0x76, 0x46, 0x32, 0x52, 0x42, 0x64, 0x52, 0x41, 0x45, 0x52, 0x33, 0x77, 0x39, | 306 | 0x4b, 0x4b, 0x46, 0x4a, 0x35, 0x57, 0x78, 0x4a, 0x43, 0x47, 0x41, 0x42, 0x64, 0x4a, 0x7a, |
285 | 0x53, 0x31, 0x49, 0x41, 0x45, 0x46, 0x31, 0x52, 0x54, 0x6e, 0x6f, 0x6e, 0x4a, 0x56, 0x56, 0x41, 0x45, 0x6a, 0x56, 0x67, 0x42, 0x52, | 307 | 0x46, 0x54, 0x50, 0x45, 0x77, 0x32, 0x54, 0x6a, 0x67, 0x2f, 0x63, 0x67, 0x4e, 0x78, 0x41, |
286 | 0x73, 0x30, 0x4c, 0x51, 0x52, 0x36, 0x61, 0x6d, 0x6b, 0x43, 0x65, 0x51, 0x4d, 0x36, 0x4c, 0x77, 0x59, 0x4b, 0x65, 0x58, 0x73, 0x53, | 308 | 0x6c, 0x73, 0x32, 0x57, 0x58, 0x39, 0x31, 0x62, 0x67, 0x68, 0x55, 0x44, 0x54, 0x51, 0x63, |
287 | 0x58, 0x58, 0x78, 0x53, 0x4b, 0x55, 0x64, 0x59, 0x45, 0x6e, 0x4d, 0x2f, 0x4a, 0x31, 0x59, 0x46, 0x44, 0x45, 0x67, 0x79, 0x57, 0x47, | 309 | 0x4e, 0x46, 0x64, 0x64, 0x61, 0x55, 0x67, 0x41, 0x4f, 0x77, 0x55, 0x48, 0x62, 0x69, 0x64, |
288 | 0x74, 0x58, 0x58, 0x41, 0x4e, 0x6b, 0x56, 0x68, 0x46, 0x53, 0x65, 0x6a, 0x41, 0x32, 0x4b, 0x52, 0x63, 0x37, 0x61, 0x48, 0x70, 0x38, | 310 | 0x6c, 0x62, 0x6b, 0x41, 0x39, 0x4f, 0x6b, 0x39, 0x79, 0x58, 0x54, 0x6b, 0x57, 0x44, 0x32, |
289 | 0x42, 0x57, 0x74, 0x72, 0x45, 0x32, 0x6f, 0x6b, 0x58, 0x47, 0x68, 0x43, 0x47, 0x44, 0x49, 0x44, 0x63, 0x32, 0x34, 0x45, 0x49, 0x53, | 311 | 0x4d, 0x53, 0x45, 0x68, 0x55, 0x36, 0x63, 0x41, 0x31, 0x58, 0x47, 0x41, 0x31, 0x65, 0x50, |
290 | 0x34, 0x42, 0x42, 0x47, 0x4e, 0x39, 0x52, 0x45, 0x45, 0x53, 0x4d, 0x51, 0x73, 0x56, 0x48, 0x33, 0x41, 0x41, 0x4c, 0x6d, 0x59, 0x55, | 312 | 0x53, 0x4a, 0x6f, 0x61, 0x48, 0x78, 0x74, 0x54, 0x77, 0x78, 0x37, 0x43, 0x53, 0x31, 0x4b, |
291 | 0x50, 0x48, 0x38, 0x72, 0x41, 0x42, 0x38, 0x4d, 0x4b, 0x46, 0x6b, 0x4b, 0x46, 0x6b, 0x6c, 0x61, 0x58, 0x47, 0x52, 0x6c, 0x53, 0x78, | 313 | 0x63, 0x79, 0x42, 0x48, 0x42, 0x31, 0x64, 0x31, 0x58, 0x56, 0x4e, 0x77, 0x4e, 0x43, 0x46, |
292 | 0x45, 0x70, 0x46, 0x54, 0x5a, 0x61, 0x5a, 0x52, 0x6c, 0x50, 0x59, 0x43, 0x4d, 0x36, 0x4f, 0x68, 0x4d, 0x6c, 0x41, 0x6e, 0x68, 0x4d, | 314 | 0x41, 0x56, 0x31, 0x46, 0x65, 0x53, 0x55, 0x51, 0x41, 0x56, 0x43, 0x63, 0x45, 0x61, 0x48, |
293 | 0x56, 0x41, 0x3d, 0x3d, 0x00}; | 315 | 0x35, 0x5a, 0x56, 0x6c, 0x68, 0x30, 0x46, 0x44, 0x77, 0x57, 0x4d, 0x30, 0x45, 0x57, 0x53, |
316 | 0x79, 0x39, 0x4a, 0x4e, 0x77, 0x70, 0x55, 0x43, 0x41, 0x67, 0x66, 0x4f, 0x57, 0x64, 0x32, | ||
317 | 0x4b, 0x43, 0x67, 0x48, 0x48, 0x57, 0x46, 0x48, 0x55, 0x55, 0x31, 0x31, 0x4a, 0x6c, 0x4a, | ||
318 | 0x48, 0x52, 0x77, 0x78, 0x58, 0x57, 0x48, 0x51, 0x2b, 0x59, 0x6d, 0x78, 0x59, 0x4f, 0x6b, | ||
319 | 0x51, 0x65, 0x46, 0x69, 0x34, 0x68, 0x48, 0x48, 0x4e, 0x46, 0x5a, 0x33, 0x52, 0x50, 0x4d, | ||
320 | 0x32, 0x59, 0x4f, 0x64, 0x47, 0x59, 0x6d, 0x48, 0x79, 0x34, 0x34, 0x52, 0x45, 0x42, 0x2b, | ||
321 | 0x4b, 0x6c, 0x42, 0x53, 0x58, 0x6b, 0x4d, 0x42, 0x65, 0x6a, 0x68, 0x4a, 0x50, 0x46, 0x56, | ||
322 | 0x4e, 0x57, 0x6b, 0x51, 0x49, 0x4a, 0x6c, 0x6c, 0x4e, 0x52, 0x51, 0x74, 0x49, 0x43, 0x6a, | ||
323 | 0x4e, 0x65, 0x53, 0x6b, 0x31, 0x31, 0x46, 0x68, 0x64, 0x6a, 0x52, 0x67, 0x45, 0x71, 0x56, | ||
324 | 0x58, 0x73, 0x50, 0x41, 0x6e, 0x4e, 0x71, 0x53, 0x33, 0x39, 0x31, 0x5a, 0x54, 0x78, 0x4d, | ||
325 | 0x4d, 0x7a, 0x6c, 0x73, 0x64, 0x41, 0x56, 0x67, 0x44, 0x33, 0x38, 0x74, 0x51, 0x55, 0x31, | ||
326 | 0x4e, 0x52, 0x6e, 0x45, 0x4a, 0x62, 0x30, 0x39, 0x67, 0x46, 0x51, 0x39, 0x47, 0x63, 0x32, | ||
327 | 0x4e, 0x4d, 0x58, 0x6e, 0x51, 0x77, 0x44, 0x53, 0x68, 0x44, 0x43, 0x48, 0x49, 0x79, 0x42, | ||
328 | 0x43, 0x34, 0x78, 0x4b, 0x53, 0x64, 0x45, 0x62, 0x52, 0x4d, 0x58, 0x53, 0x41, 0x39, 0x4a, | ||
329 | 0x55, 0x68, 0x41, 0x54, 0x66, 0x78, 0x63, 0x57, 0x59, 0x6e, 0x6b, 0x31, 0x65, 0x44, 0x34, | ||
330 | 0x42, 0x66, 0x43, 0x34, 0x50, 0x64, 0x6a, 0x35, 0x65, 0x55, 0x32, 0x78, 0x62, 0x58, 0x33, | ||
331 | 0x77, 0x5a, 0x51, 0x51, 0x49, 0x76, 0x46, 0x32, 0x52, 0x42, 0x64, 0x52, 0x41, 0x45, 0x52, | ||
332 | 0x33, 0x77, 0x39, 0x53, 0x31, 0x49, 0x41, 0x45, 0x46, 0x31, 0x52, 0x54, 0x6e, 0x6f, 0x6e, | ||
333 | 0x4a, 0x56, 0x56, 0x41, 0x45, 0x6a, 0x56, 0x67, 0x42, 0x52, 0x73, 0x30, 0x4c, 0x51, 0x52, | ||
334 | 0x36, 0x61, 0x6d, 0x6b, 0x43, 0x65, 0x51, 0x4d, 0x36, 0x4c, 0x77, 0x59, 0x4b, 0x65, 0x58, | ||
335 | 0x73, 0x53, 0x58, 0x58, 0x78, 0x53, 0x4b, 0x55, 0x64, 0x59, 0x45, 0x6e, 0x4d, 0x2f, 0x4a, | ||
336 | 0x31, 0x59, 0x46, 0x44, 0x45, 0x67, 0x79, 0x57, 0x47, 0x74, 0x58, 0x58, 0x41, 0x4e, 0x6b, | ||
337 | 0x56, 0x68, 0x46, 0x53, 0x65, 0x6a, 0x41, 0x32, 0x4b, 0x52, 0x63, 0x37, 0x61, 0x48, 0x70, | ||
338 | 0x38, 0x42, 0x57, 0x74, 0x72, 0x45, 0x32, 0x6f, 0x6b, 0x58, 0x47, 0x68, 0x43, 0x47, 0x44, | ||
339 | 0x49, 0x44, 0x63, 0x32, 0x34, 0x45, 0x49, 0x53, 0x34, 0x42, 0x42, 0x47, 0x4e, 0x39, 0x52, | ||
340 | 0x45, 0x45, 0x53, 0x4d, 0x51, 0x73, 0x56, 0x48, 0x33, 0x41, 0x41, 0x4c, 0x6d, 0x59, 0x55, | ||
341 | 0x50, 0x48, 0x38, 0x72, 0x41, 0x42, 0x38, 0x4d, 0x4b, 0x46, 0x6b, 0x4b, 0x46, 0x6b, 0x6c, | ||
342 | 0x61, 0x58, 0x47, 0x52, 0x6c, 0x53, 0x78, 0x45, 0x70, 0x46, 0x54, 0x5a, 0x61, 0x5a, 0x52, | ||
343 | 0x6c, 0x50, 0x59, 0x43, 0x4d, 0x36, 0x4f, 0x68, 0x4d, 0x6c, 0x41, 0x6e, 0x68, 0x4d, 0x56, | ||
344 | 0x41, 0x3d, 0x3d, 0x00}; | ||
294 | char *b64_test; | 345 | char *b64_test; |
295 | 346 | ||
296 | plan_tests(1); | 347 | plan_tests(1); |
diff --git a/lib/tests/test_cmd.c b/lib/tests/test_cmd.c index c8867dfb..d51016cc 100644 --- a/lib/tests/test_cmd.c +++ b/lib/tests/test_cmd.c | |||
@@ -38,36 +38,35 @@ char *get_command(char *const *line) { | |||
38 | } | 38 | } |
39 | 39 | ||
40 | int main(int argc, char **argv) { | 40 | int main(int argc, char **argv) { |
41 | char **command_line = malloc(sizeof(char *) * COMMAND_LINE); | ||
42 | char *command = NULL; | ||
43 | char *perl; | ||
44 | output chld_out, chld_err; | ||
45 | int c; | ||
46 | int result = UNSET; | ||
47 | |||
48 | plan_tests(51); | 41 | plan_tests(51); |
49 | 42 | ||
50 | diag("Running plain echo command, set one"); | 43 | diag("Running plain echo command, set one"); |
51 | 44 | ||
52 | /* ensure everything is empty before we begin */ | 45 | /* ensure everything is empty before we begin */ |
46 | |||
47 | output chld_out; | ||
53 | memset(&chld_out, 0, sizeof(output)); | 48 | memset(&chld_out, 0, sizeof(output)); |
49 | output chld_err; | ||
54 | memset(&chld_err, 0, sizeof(output)); | 50 | memset(&chld_err, 0, sizeof(output)); |
55 | ok(chld_out.lines == 0, "(initialised) Checking stdout is reset"); | 51 | ok(chld_out.lines == 0, "(initialised) Checking stdout is reset"); |
56 | ok(chld_err.lines == 0, "(initialised) Checking stderr is reset"); | 52 | ok(chld_err.lines == 0, "(initialised) Checking stderr is reset"); |
53 | int result = UNSET; | ||
57 | ok(result == UNSET, "(initialised) Checking exit code is reset"); | 54 | ok(result == UNSET, "(initialised) Checking exit code is reset"); |
58 | 55 | ||
56 | char **command_line = malloc(sizeof(char *) * COMMAND_LINE); | ||
59 | command_line[0] = strdup("/bin/echo"); | 57 | command_line[0] = strdup("/bin/echo"); |
60 | command_line[1] = strdup("this"); | 58 | command_line[1] = strdup("this"); |
61 | command_line[2] = strdup("is"); | 59 | command_line[2] = strdup("is"); |
62 | command_line[3] = strdup("test"); | 60 | command_line[3] = strdup("test"); |
63 | command_line[4] = strdup("one"); | 61 | command_line[4] = strdup("one"); |
64 | 62 | ||
65 | command = get_command(command_line); | 63 | char *command = get_command(command_line); |
66 | 64 | ||
67 | result = cmd_run_array(command_line, &chld_out, &chld_err, 0); | 65 | result = cmd_run_array(command_line, &chld_out, &chld_err, 0); |
68 | ok(chld_out.lines == 1, "(array) Check for expected number of stdout lines"); | 66 | ok(chld_out.lines == 1, "(array) Check for expected number of stdout lines"); |
69 | ok(chld_err.lines == 0, "(array) Check for expected number of stderr lines"); | 67 | ok(chld_err.lines == 0, "(array) Check for expected number of stderr lines"); |
70 | ok(strcmp(chld_out.line[0], "this is test one") == 0, "(array) Check for expected stdout output"); | 68 | ok(strcmp(chld_out.line[0], "this is test one") == 0, |
69 | "(array) Check for expected stdout output"); | ||
71 | ok(result == 0, "(array) Checking exit code"); | 70 | ok(result == 0, "(array) Checking exit code"); |
72 | 71 | ||
73 | /* ensure everything is empty again */ | 72 | /* ensure everything is empty again */ |
@@ -82,7 +81,8 @@ int main(int argc, char **argv) { | |||
82 | 81 | ||
83 | ok(chld_out.lines == 1, "(string) Check for expected number of stdout lines"); | 82 | ok(chld_out.lines == 1, "(string) Check for expected number of stdout lines"); |
84 | ok(chld_err.lines == 0, "(string) Check for expected number of stderr lines"); | 83 | ok(chld_err.lines == 0, "(string) Check for expected number of stderr lines"); |
85 | ok(strcmp(chld_out.line[0], "this is test one") == 0, "(string) Check for expected stdout output"); | 84 | ok(strcmp(chld_out.line[0], "this is test one") == 0, |
85 | "(string) Check for expected stdout output"); | ||
86 | ok(result == 0, "(string) Checking exit code"); | 86 | ok(result == 0, "(string) Checking exit code"); |
87 | 87 | ||
88 | diag("Running plain echo command, set two"); | 88 | diag("Running plain echo command, set two"); |
@@ -104,7 +104,8 @@ int main(int argc, char **argv) { | |||
104 | result = cmd_run_array(command_line, &chld_out, &chld_err, 0); | 104 | result = cmd_run_array(command_line, &chld_out, &chld_err, 0); |
105 | ok(chld_out.lines == 1, "(array) Check for expected number of stdout lines"); | 105 | ok(chld_out.lines == 1, "(array) Check for expected number of stdout lines"); |
106 | ok(chld_err.lines == 0, "(array) Check for expected number of stderr lines"); | 106 | ok(chld_err.lines == 0, "(array) Check for expected number of stderr lines"); |
107 | ok(strcmp(chld_out.line[0], "this is test two") == 0, "(array) Check for expected stdout output"); | 107 | ok(strcmp(chld_out.line[0], "this is test two") == 0, |
108 | "(array) Check for expected stdout output"); | ||
108 | ok(result == 0, "(array) Checking exit code"); | 109 | ok(result == 0, "(array) Checking exit code"); |
109 | 110 | ||
110 | /* ensure everything is empty again */ | 111 | /* ensure everything is empty again */ |
@@ -119,7 +120,8 @@ int main(int argc, char **argv) { | |||
119 | 120 | ||
120 | ok(chld_out.lines == 1, "(string) Check for expected number of stdout lines"); | 121 | ok(chld_out.lines == 1, "(string) Check for expected number of stdout lines"); |
121 | ok(chld_err.lines == 0, "(string) Check for expected number of stderr lines"); | 122 | ok(chld_err.lines == 0, "(string) Check for expected number of stderr lines"); |
122 | ok(strcmp(chld_out.line[0], "this is test one") == 0, "(string) Check for expected stdout output"); | 123 | ok(strcmp(chld_out.line[0], "this is test one") == 0, |
124 | "(string) Check for expected stdout output"); | ||
123 | ok(result == 0, "(string) Checking exit code"); | 125 | ok(result == 0, "(string) Checking exit code"); |
124 | 126 | ||
125 | /* ensure everything is empty again */ | 127 | /* ensure everything is empty again */ |
@@ -130,7 +132,8 @@ int main(int argc, char **argv) { | |||
130 | ok(chld_err.lines == 0, "(initialised) Checking stderr is reset"); | 132 | ok(chld_err.lines == 0, "(initialised) Checking stderr is reset"); |
131 | ok(result == UNSET, "(initialised) Checking exit code is reset"); | 133 | ok(result == UNSET, "(initialised) Checking exit code is reset"); |
132 | 134 | ||
133 | /* Pass linefeeds via parameters through - those should be evaluated by echo to give multi line output */ | 135 | /* Pass linefeeds via parameters through - those should be evaluated by echo to give multi line |
136 | * output */ | ||
134 | command_line[0] = strdup("/bin/echo"); | 137 | command_line[0] = strdup("/bin/echo"); |
135 | command_line[1] = strdup("this is a test via echo\nline two\nit's line 3"); | 138 | command_line[1] = strdup("this is a test via echo\nline two\nit's line 3"); |
136 | command_line[2] = strdup("and (note space between '3' and 'and') $$ will not get evaluated"); | 139 | command_line[2] = strdup("and (note space between '3' and 'and') $$ will not get evaluated"); |
@@ -138,9 +141,12 @@ int main(int argc, char **argv) { | |||
138 | result = cmd_run_array(command_line, &chld_out, &chld_err, 0); | 141 | result = cmd_run_array(command_line, &chld_out, &chld_err, 0); |
139 | ok(chld_out.lines == 3, "(array) Check for expected number of stdout lines"); | 142 | ok(chld_out.lines == 3, "(array) Check for expected number of stdout lines"); |
140 | ok(chld_err.lines == 0, "(array) Check for expected number of stderr lines"); | 143 | ok(chld_err.lines == 0, "(array) Check for expected number of stderr lines"); |
141 | ok(strcmp(chld_out.line[0], "this is a test via echo") == 0, "(array) Check line 1 for expected stdout output"); | 144 | ok(strcmp(chld_out.line[0], "this is a test via echo") == 0, |
142 | ok(strcmp(chld_out.line[1], "line two") == 0, "(array) Check line 2 for expected stdout output"); | 145 | "(array) Check line 1 for expected stdout output"); |
143 | ok(strcmp(chld_out.line[2], "it's line 3 and (note space between '3' and 'and') $$ will not get evaluated") == 0, | 146 | ok(strcmp(chld_out.line[1], "line two") == 0, |
147 | "(array) Check line 2 for expected stdout output"); | ||
148 | ok(strcmp(chld_out.line[2], | ||
149 | "it's line 3 and (note space between '3' and 'and') $$ will not get evaluated") == 0, | ||
144 | "(array) Check line 3 for expected stdout output"); | 150 | "(array) Check line 3 for expected stdout output"); |
145 | ok(result == 0, "(array) Checking exit code"); | 151 | ok(result == 0, "(array) Checking exit code"); |
146 | 152 | ||
@@ -171,7 +177,8 @@ int main(int argc, char **argv) { | |||
171 | 177 | ||
172 | ok(chld_out.lines == 0, "/bin/sh returns no stdout when file is missing..."); | 178 | ok(chld_out.lines == 0, "/bin/sh returns no stdout when file is missing..."); |
173 | ok(chld_err.lines == 1, "...but does give an error line"); | 179 | ok(chld_err.lines == 1, "...but does give an error line"); |
174 | ok(strstr(chld_err.line[0], "non-existent-file") != NULL, "And missing filename is in error message"); | 180 | ok(strstr(chld_err.line[0], "non-existent-file") != NULL, |
181 | "And missing filename is in error message"); | ||
175 | ok(result != 0, "Get non-zero return code from /bin/sh"); | 182 | ok(result != 0, "Get non-zero return code from /bin/sh"); |
176 | 183 | ||
177 | /* ensure everything is empty again */ | 184 | /* ensure everything is empty again */ |
diff --git a/lib/tests/test_disk.c b/lib/tests/test_disk.c deleted file mode 100644 index c18db7a4..00000000 --- a/lib/tests/test_disk.c +++ /dev/null | |||
@@ -1,192 +0,0 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 3 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | * | ||
16 | * | ||
17 | *****************************************************************************/ | ||
18 | |||
19 | #include "common.h" | ||
20 | #include "utils_disk.h" | ||
21 | #include "tap.h" | ||
22 | #include "regex.h" | ||
23 | |||
24 | void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc); | ||
25 | |||
26 | int main(int argc, char **argv) { | ||
27 | struct name_list *exclude_filesystem = NULL; | ||
28 | struct name_list *exclude_fstype = NULL; | ||
29 | struct name_list *dummy_mountlist = NULL; | ||
30 | struct name_list *temp_name; | ||
31 | struct parameter_list *paths = NULL; | ||
32 | struct parameter_list *p, *prev = NULL, *last = NULL; | ||
33 | |||
34 | struct mount_entry *dummy_mount_list; | ||
35 | struct mount_entry *me; | ||
36 | struct mount_entry **mtail = &dummy_mount_list; | ||
37 | int cflags = REG_NOSUB | REG_EXTENDED; | ||
38 | int found = 0, count = 0; | ||
39 | |||
40 | plan_tests(33); | ||
41 | |||
42 | ok(np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); | ||
43 | np_add_name(&exclude_filesystem, "/var/log"); | ||
44 | ok(np_find_name(exclude_filesystem, "/var/log") == true, "is in list now"); | ||
45 | ok(np_find_name(exclude_filesystem, "/home") == false, "/home not in list"); | ||
46 | np_add_name(&exclude_filesystem, "/home"); | ||
47 | ok(np_find_name(exclude_filesystem, "/home") == true, "is in list now"); | ||
48 | ok(np_find_name(exclude_filesystem, "/var/log") == true, "/var/log still in list"); | ||
49 | |||
50 | ok(np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); | ||
51 | np_add_name(&exclude_fstype, "iso9660"); | ||
52 | ok(np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); | ||
53 | |||
54 | ok(np_find_name(exclude_filesystem, "iso9660") == false, "Make sure no clashing in variables"); | ||
55 | |||
56 | /* | ||
57 | for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { | ||
58 | printf("Name: %s\n", temp_name->name); | ||
59 | } | ||
60 | */ | ||
61 | |||
62 | me = (struct mount_entry *)malloc(sizeof *me); | ||
63 | me->me_devname = strdup("/dev/c0t0d0s0"); | ||
64 | me->me_mountdir = strdup("/"); | ||
65 | *mtail = me; | ||
66 | mtail = &me->me_next; | ||
67 | |||
68 | me = (struct mount_entry *)malloc(sizeof *me); | ||
69 | me->me_devname = strdup("/dev/c1t0d1s0"); | ||
70 | me->me_mountdir = strdup("/var"); | ||
71 | *mtail = me; | ||
72 | mtail = &me->me_next; | ||
73 | |||
74 | me = (struct mount_entry *)malloc(sizeof *me); | ||
75 | me->me_devname = strdup("/dev/c2t0d0s0"); | ||
76 | me->me_mountdir = strdup("/home"); | ||
77 | *mtail = me; | ||
78 | mtail = &me->me_next; | ||
79 | |||
80 | np_test_mount_entry_regex(dummy_mount_list, strdup("/"), cflags, 3, strdup("a")); | ||
81 | np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"), cflags, 3, strdup("regex on dev names:")); | ||
82 | np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"), cflags, 0, strdup("regex on non existent dev/path:")); | ||
83 | np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"), cflags | REG_ICASE, 0, strdup("regi on non existent dev/path:")); | ||
84 | np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"), cflags, 3, strdup("partial devname regex match:")); | ||
85 | np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"), cflags, 1, strdup("partial devname regex match:")); | ||
86 | np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"), cflags | REG_ICASE, 1, strdup("partial devname regi match:")); | ||
87 | np_test_mount_entry_regex(dummy_mount_list, strdup("home"), cflags, 1, strdup("partial pathname regex match:")); | ||
88 | np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"), cflags | REG_ICASE, 1, strdup("partial pathname regi match:")); | ||
89 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"), cflags, 2, strdup("grouped regex pathname match:")); | ||
90 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"), cflags | REG_ICASE, 2, strdup("grouped regi pathname match:")); | ||
91 | |||
92 | np_add_parameter(&paths, "/home/groups"); | ||
93 | np_add_parameter(&paths, "/var"); | ||
94 | np_add_parameter(&paths, "/tmp"); | ||
95 | np_add_parameter(&paths, "/home/tonvoon"); | ||
96 | np_add_parameter(&paths, "/dev/c2t0d0s0"); | ||
97 | |||
98 | np_set_best_match(paths, dummy_mount_list, false); | ||
99 | for (p = paths; p; p = p->name_next) { | ||
100 | struct mount_entry *temp_me; | ||
101 | temp_me = p->best_match; | ||
102 | if (!strcmp(p->name, "/home/groups")) { | ||
103 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home"); | ||
104 | } else if (!strcmp(p->name, "/var")) { | ||
105 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var"); | ||
106 | } else if (!strcmp(p->name, "/tmp")) { | ||
107 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /"); | ||
108 | } else if (!strcmp(p->name, "/home/tonvoon")) { | ||
109 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home"); | ||
110 | } else if (!strcmp(p->name, "/dev/c2t0d0s0")) { | ||
111 | ok(temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0"); | ||
112 | } | ||
113 | } | ||
114 | |||
115 | paths = NULL; /* Bad boy - should free, but this is a test suite */ | ||
116 | np_add_parameter(&paths, "/home/groups"); | ||
117 | np_add_parameter(&paths, "/var"); | ||
118 | np_add_parameter(&paths, "/tmp"); | ||
119 | np_add_parameter(&paths, "/home/tonvoon"); | ||
120 | np_add_parameter(&paths, "/home"); | ||
121 | |||
122 | np_set_best_match(paths, dummy_mount_list, true); | ||
123 | for (p = paths; p; p = p->name_next) { | ||
124 | if (!strcmp(p->name, "/home/groups")) { | ||
125 | ok(!p->best_match, "/home/groups correctly not found"); | ||
126 | } else if (!strcmp(p->name, "/var")) { | ||
127 | ok(p->best_match, "/var found"); | ||
128 | } else if (!strcmp(p->name, "/tmp")) { | ||
129 | ok(!p->best_match, "/tmp correctly not found"); | ||
130 | } else if (!strcmp(p->name, "/home/tonvoon")) { | ||
131 | ok(!p->best_match, "/home/tonvoon not found"); | ||
132 | } else if (!strcmp(p->name, "/home")) { | ||
133 | ok(p->best_match, "/home found"); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | /* test deleting first element in paths */ | ||
138 | paths = np_del_parameter(paths, NULL); | ||
139 | for (p = paths; p; p = p->name_next) { | ||
140 | if (!strcmp(p->name, "/home/groups")) | ||
141 | found = 1; | ||
142 | } | ||
143 | ok(found == 0, "first element successfully deleted"); | ||
144 | found = 0; | ||
145 | |||
146 | p = paths; | ||
147 | while (p) { | ||
148 | if (!strcmp(p->name, "/tmp")) | ||
149 | p = np_del_parameter(p, prev); | ||
150 | else { | ||
151 | prev = p; | ||
152 | p = p->name_next; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | for (p = paths; p; p = p->name_next) { | ||
157 | if (!strcmp(p->name, "/tmp")) | ||
158 | found = 1; | ||
159 | if (p->name_next) | ||
160 | prev = p; | ||
161 | else | ||
162 | last = p; | ||
163 | } | ||
164 | ok(found == 0, "/tmp element successfully deleted"); | ||
165 | |||
166 | p = np_del_parameter(last, prev); | ||
167 | for (p = paths; p; p = p->name_next) { | ||
168 | if (!strcmp(p->name, "/home")) | ||
169 | found = 1; | ||
170 | last = p; | ||
171 | count++; | ||
172 | } | ||
173 | ok(found == 0, "last (/home) element successfully deleted"); | ||
174 | ok(count == 2, "two elements remaining"); | ||
175 | |||
176 | return exit_status(); | ||
177 | } | ||
178 | |||
179 | void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc) { | ||
180 | int matches = 0; | ||
181 | regex_t re; | ||
182 | struct mount_entry *me; | ||
183 | if (regcomp(&re, regstr, cflags) == 0) { | ||
184 | for (me = dummy_mount_list; me; me = me->me_next) { | ||
185 | if (np_regex_match_mount_entry(me, &re)) | ||
186 | matches++; | ||
187 | } | ||
188 | ok(matches == expect, "%s '%s' matched %i/3 entries. ok: %i/3", desc, regstr, expect, matches); | ||
189 | |||
190 | } else | ||
191 | ok(false, "regex '%s' not compilable", regstr); | ||
192 | } | ||
diff --git a/lib/tests/test_disk.t b/lib/tests/test_disk.t deleted file mode 100755 index da84dfdf..00000000 --- a/lib/tests/test_disk.t +++ /dev/null | |||
@@ -1,6 +0,0 @@ | |||
1 | #!/usr/bin/perl | ||
2 | use Test::More; | ||
3 | if (! -e "./test_disk") { | ||
4 | plan skip_all => "./test_disk not compiled - please enable libtap library to test"; | ||
5 | } | ||
6 | exec "./test_disk"; | ||
diff --git a/lib/tests/test_generic_output.c b/lib/tests/test_generic_output.c new file mode 100644 index 00000000..e4a78bcd --- /dev/null +++ b/lib/tests/test_generic_output.c | |||
@@ -0,0 +1,317 @@ | |||
1 | #include "../lib/output.h" | ||
2 | #include "../../tap/tap.h" | ||
3 | #include "./states.h" | ||
4 | |||
5 | #include <string.h> | ||
6 | |||
7 | void test_one_subcheck(void); | ||
8 | void test_two_subchecks(void); | ||
9 | |||
10 | void test_perfdata_formatting(void); | ||
11 | void test_perfdata_formatting2(void); | ||
12 | |||
13 | void test_deep_check_hierarchy(void); | ||
14 | void test_deep_check_hierarchy2(void); | ||
15 | |||
16 | void test_default_states1(void); | ||
17 | void test_default_states2(void); | ||
18 | |||
19 | int main(void) { | ||
20 | plan_tests(19); | ||
21 | |||
22 | diag("Simple test with one subcheck"); | ||
23 | test_one_subcheck(); | ||
24 | |||
25 | diag("Test with two subchecks"); | ||
26 | test_two_subchecks(); | ||
27 | |||
28 | diag("Test for performance data formatting"); | ||
29 | test_perfdata_formatting(); | ||
30 | |||
31 | diag("Another test for performance data formatting"); | ||
32 | test_perfdata_formatting2(); | ||
33 | |||
34 | diag("Test for deeper hierarchies"); | ||
35 | test_deep_check_hierarchy(); | ||
36 | |||
37 | diag("Another test for deeper hierarchies"); | ||
38 | test_deep_check_hierarchy2(); | ||
39 | |||
40 | diag("Testing the default state logic"); | ||
41 | test_default_states1(); | ||
42 | |||
43 | diag("Testing the default state logic #2"); | ||
44 | test_default_states2(); | ||
45 | |||
46 | return exit_status(); | ||
47 | } | ||
48 | |||
49 | void test_one_subcheck(void) { | ||
50 | mp_subcheck sc1 = mp_subcheck_init(); | ||
51 | |||
52 | sc1.output = "foobar"; | ||
53 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); | ||
54 | |||
55 | mp_check check = mp_check_init(); | ||
56 | mp_add_subcheck_to_check(&check, sc1); | ||
57 | |||
58 | ok(mp_compute_check_state(check) == STATE_WARNING, "Main state should be warning"); | ||
59 | |||
60 | char *output = mp_fmt_output(check); | ||
61 | |||
62 | // diag("Formatted output"); | ||
63 | // diag(output); | ||
64 | |||
65 | char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n" | ||
66 | "\t\\_[WARNING] - foobar\n"; | ||
67 | |||
68 | // diag("Expected output"); | ||
69 | // diag(expected); | ||
70 | |||
71 | ok(strcmp(output, expected) == 0, "Simple output test"); | ||
72 | } | ||
73 | |||
74 | void test_perfdata_formatting2(void) { | ||
75 | mp_perfdata pd1 = perfdata_init(); | ||
76 | mp_perfdata pd2 = perfdata_init(); | ||
77 | |||
78 | pd1.label = "foo"; | ||
79 | pd2.label = "bar"; | ||
80 | |||
81 | pd1 = mp_set_pd_value(pd1, 23); | ||
82 | pd2 = mp_set_pd_value(pd2, 1LL); | ||
83 | |||
84 | pd_list *tmp = pd_list_init(); | ||
85 | |||
86 | pd_list_append(tmp, pd1); | ||
87 | pd_list_append(tmp, pd2); | ||
88 | |||
89 | char *result = pd_list_to_string(*tmp); | ||
90 | |||
91 | ok(strcmp(result, "foo=23;;; bar=1;;;") == 0, "Perfdata string formatting"); | ||
92 | } | ||
93 | |||
94 | void test_perfdata_formatting(void) { | ||
95 | mp_perfdata pd1 = perfdata_init(); | ||
96 | |||
97 | pd1.uom = "s"; | ||
98 | pd1.label = "foo"; | ||
99 | |||
100 | pd1 = mp_set_pd_value(pd1, 23); | ||
101 | |||
102 | char *pd_string = pd_to_string(pd1); | ||
103 | |||
104 | ok(strcmp(pd_string, "foo=23s;;;") == 0, "Perfdata string formatting"); | ||
105 | } | ||
106 | |||
107 | void test_two_subchecks(void) { | ||
108 | mp_subcheck sc1 = mp_subcheck_init(); | ||
109 | |||
110 | sc1.output = "foobar"; | ||
111 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); | ||
112 | |||
113 | ok(mp_compute_subcheck_state(sc1) == STATE_WARNING, | ||
114 | "Test subcheck state directly after setting it"); | ||
115 | |||
116 | mp_perfdata pd1 = perfdata_init(); | ||
117 | |||
118 | pd1 = mp_set_pd_value(pd1, 23); | ||
119 | |||
120 | pd1.uom = "s"; | ||
121 | pd1.label = "foo"; | ||
122 | |||
123 | mp_add_perfdata_to_subcheck(&sc1, pd1); | ||
124 | |||
125 | mp_subcheck sc2 = mp_subcheck_init(); | ||
126 | sc2.output = "baz"; | ||
127 | sc2 = mp_set_subcheck_state(sc2, STATE_OK); | ||
128 | |||
129 | ok(mp_compute_subcheck_state(sc2) == STATE_OK, "Test subcheck 2 state after setting it"); | ||
130 | |||
131 | mp_add_subcheck_to_subcheck(&sc1, sc2); | ||
132 | |||
133 | ok(mp_compute_subcheck_state(sc1) == STATE_WARNING, | ||
134 | "Test subcheck state after adding a subcheck"); | ||
135 | |||
136 | mp_check check = mp_check_init(); | ||
137 | mp_add_subcheck_to_check(&check, sc1); | ||
138 | |||
139 | ok(mp_compute_check_state(check) == STATE_WARNING, "Test main check result"); | ||
140 | |||
141 | char *output = mp_fmt_output(check); | ||
142 | |||
143 | // diag("Formatted output. Length: %u", strlen(output)); | ||
144 | // diag(output); | ||
145 | |||
146 | ok(output != NULL, "Output should not be NULL"); | ||
147 | |||
148 | char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n" | ||
149 | "\t\\_[WARNING] - foobar\n" | ||
150 | "\t\t\\_[OK] - baz\n" | ||
151 | "|foo=23s;;; \n"; | ||
152 | |||
153 | // diag("Expected output. Length: %u", strlen(expected)); | ||
154 | // diag(expected); | ||
155 | |||
156 | ok(strcmp(output, expected) == 0, "Output is as expected"); | ||
157 | } | ||
158 | |||
159 | void test_deep_check_hierarchy(void) { | ||
160 | // level 4 | ||
161 | mp_subcheck sc4 = mp_subcheck_init(); | ||
162 | sc4.output = "level4"; | ||
163 | sc4 = mp_set_subcheck_state(sc4, STATE_OK); | ||
164 | |||
165 | // level 3 | ||
166 | mp_subcheck sc3 = mp_subcheck_init(); | ||
167 | sc3.output = "level3"; | ||
168 | sc3 = mp_set_subcheck_state(sc3, STATE_OK); | ||
169 | |||
170 | // level 2 | ||
171 | mp_subcheck sc2 = mp_subcheck_init(); | ||
172 | sc2.output = "baz"; | ||
173 | sc2 = mp_set_subcheck_state(sc2, STATE_OK); | ||
174 | |||
175 | // level 1 | ||
176 | mp_subcheck sc1 = mp_subcheck_init(); | ||
177 | |||
178 | sc1.output = "foobar"; | ||
179 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); | ||
180 | |||
181 | mp_perfdata pd1 = perfdata_init(); | ||
182 | |||
183 | pd1.uom = "s"; | ||
184 | pd1.label = "foo"; | ||
185 | pd1 = mp_set_pd_value(pd1, 23); | ||
186 | |||
187 | mp_add_perfdata_to_subcheck(&sc1, pd1); | ||
188 | |||
189 | // main check | ||
190 | mp_check check = mp_check_init(); | ||
191 | |||
192 | mp_add_subcheck_to_subcheck(&sc3, sc4); | ||
193 | mp_add_subcheck_to_subcheck(&sc2, sc3); | ||
194 | mp_add_subcheck_to_subcheck(&sc1, sc2); | ||
195 | mp_add_subcheck_to_check(&check, sc1); | ||
196 | |||
197 | char *output = mp_fmt_output(check); | ||
198 | |||
199 | size_t output_length = strlen(output); | ||
200 | |||
201 | // diag("Formatted output of length %i", output_length); | ||
202 | // diag(output); | ||
203 | |||
204 | ok(output != NULL, "Output should not be NULL"); | ||
205 | |||
206 | char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n" | ||
207 | "\t\\_[WARNING] - foobar\n" | ||
208 | "\t\t\\_[OK] - baz\n" | ||
209 | "\t\t\t\\_[OK] - level3\n" | ||
210 | "\t\t\t\t\\_[OK] - level4\n" | ||
211 | "|foo=23s;;; \n"; | ||
212 | |||
213 | size_t expected_length = strlen(expected); | ||
214 | |||
215 | // diag("Expected output of length: %i", expected_length); | ||
216 | // diag(expected); | ||
217 | |||
218 | ok(output_length == expected_length, "Outputs are of equal length"); | ||
219 | ok(strcmp(output, expected) == 0, "Output is as expected"); | ||
220 | } | ||
221 | |||
222 | void test_deep_check_hierarchy2(void) { | ||
223 | // level 1 | ||
224 | mp_subcheck sc1 = mp_subcheck_init(); | ||
225 | |||
226 | sc1.output = "foobar"; | ||
227 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); | ||
228 | |||
229 | mp_perfdata pd1 = perfdata_init(); | ||
230 | pd1.uom = "s"; | ||
231 | pd1.label = "foo"; | ||
232 | pd1 = mp_set_pd_value(pd1, 23); | ||
233 | |||
234 | mp_add_perfdata_to_subcheck(&sc1, pd1); | ||
235 | |||
236 | // level 2 | ||
237 | mp_subcheck sc2 = mp_subcheck_init(); | ||
238 | sc2.output = "baz"; | ||
239 | sc2 = mp_set_subcheck_state(sc2, STATE_OK); | ||
240 | |||
241 | mp_perfdata pd2 = perfdata_init(); | ||
242 | pd2.uom = "B"; | ||
243 | pd2.label = "baz"; | ||
244 | pd2 = mp_set_pd_value(pd2, 1024); | ||
245 | mp_add_perfdata_to_subcheck(&sc2, pd2); | ||
246 | |||
247 | // level 3 | ||
248 | mp_subcheck sc3 = mp_subcheck_init(); | ||
249 | sc3.output = "level3"; | ||
250 | sc3 = mp_set_subcheck_state(sc3, STATE_OK); | ||
251 | |||
252 | mp_perfdata pd3 = perfdata_init(); | ||
253 | pd3.label = "floatMe"; | ||
254 | pd3 = mp_set_pd_value(pd3, 1024.1024); | ||
255 | mp_add_perfdata_to_subcheck(&sc3, pd3); | ||
256 | |||
257 | // level 4 | ||
258 | mp_subcheck sc4 = mp_subcheck_init(); | ||
259 | sc4.output = "level4"; | ||
260 | sc4 = mp_set_subcheck_state(sc4, STATE_OK); | ||
261 | |||
262 | mp_check check = mp_check_init(); | ||
263 | |||
264 | mp_add_subcheck_to_subcheck(&sc3, sc4); | ||
265 | mp_add_subcheck_to_subcheck(&sc2, sc3); | ||
266 | mp_add_subcheck_to_subcheck(&sc1, sc2); | ||
267 | mp_add_subcheck_to_check(&check, sc1); | ||
268 | |||
269 | char *output = mp_fmt_output(check); | ||
270 | |||
271 | // diag("Formatted output of length: %i", strlen(output)); | ||
272 | // diag(output); | ||
273 | |||
274 | ok(output != NULL, "Output should not be NULL"); | ||
275 | |||
276 | char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n" | ||
277 | "\t\\_[WARNING] - foobar\n" | ||
278 | "\t\t\\_[OK] - baz\n" | ||
279 | "\t\t\t\\_[OK] - level3\n" | ||
280 | "\t\t\t\t\\_[OK] - level4\n" | ||
281 | "|foo=23s;;; baz=1024B;;; floatMe=1024.102400;;; \n"; | ||
282 | |||
283 | // diag("Expected output of length: %i", strlen(expected)); | ||
284 | // diag(expected); | ||
285 | |||
286 | ok(strcmp(output, expected) == 0, "Output is as expected"); | ||
287 | } | ||
288 | |||
289 | void test_default_states1(void) { | ||
290 | mp_subcheck sc = mp_subcheck_init(); | ||
291 | |||
292 | mp_state_enum state1 = mp_compute_subcheck_state(sc); | ||
293 | ok(state1 == STATE_UNKNOWN, "Default default state is Unknown"); | ||
294 | |||
295 | sc = mp_set_subcheck_default_state(sc, STATE_CRITICAL); | ||
296 | |||
297 | mp_state_enum state2 = mp_compute_subcheck_state(sc); | ||
298 | ok(state2 == STATE_CRITICAL, "Default state is Critical"); | ||
299 | |||
300 | sc = mp_set_subcheck_state(sc, STATE_OK); | ||
301 | |||
302 | mp_state_enum state3 = mp_compute_subcheck_state(sc); | ||
303 | ok(state3 == STATE_OK, "Default state is Critical"); | ||
304 | } | ||
305 | |||
306 | void test_default_states2(void) { | ||
307 | mp_check check = mp_check_init(); | ||
308 | |||
309 | mp_subcheck sc = mp_subcheck_init(); | ||
310 | sc.output = "placeholder"; | ||
311 | sc = mp_set_subcheck_default_state(sc, STATE_CRITICAL); | ||
312 | |||
313 | mp_add_subcheck_to_check(&check, sc); | ||
314 | |||
315 | mp_state_enum result_state = mp_compute_check_state(check); | ||
316 | ok(result_state == STATE_CRITICAL, "Derived state is the proper default state"); | ||
317 | } | ||
diff --git a/lib/tests/test_generic_output.t b/lib/tests/test_generic_output.t new file mode 100644 index 00000000..48c2ddf4 --- /dev/null +++ b/lib/tests/test_generic_output.t | |||
@@ -0,0 +1,6 @@ | |||
1 | #!/usr/bin/perl | ||
2 | use Test::More; | ||
3 | if (! -e "./test_generic_output") { | ||
4 | plan skip_all => "./test_generic_output not compiled - please enable libtap library to test"; | ||
5 | } | ||
6 | exec "./test_generic_output"; | ||
diff --git a/lib/tests/test_ini1.c b/lib/tests/test_ini1.c index 246c1250..de983764 100644 --- a/lib/tests/test_ini1.c +++ b/lib/tests/test_ini1.c | |||
@@ -42,27 +42,30 @@ char *list2str(np_arg_list *optlst) { | |||
42 | free(optltmp); | 42 | free(optltmp); |
43 | } | 43 | } |
44 | /* Strip last whitespace */ | 44 | /* Strip last whitespace */ |
45 | if (strlen(optstr) > 1) | 45 | if (strlen(optstr) > 1) { |
46 | optstr[strlen(optstr) - 1] = '\0'; | 46 | optstr[strlen(optstr) - 1] = '\0'; |
47 | } | ||
47 | 48 | ||
48 | return optstr; | 49 | return optstr; |
49 | } | 50 | } |
50 | 51 | ||
51 | int main(int argc, char **argv) { | 52 | int main(int argc, char **argv) { |
52 | char *optstr = NULL; | ||
53 | 53 | ||
54 | plan_tests(12); | 54 | plan_tests(12); |
55 | 55 | ||
56 | optstr = list2str(np_get_defaults("section@./config-tiny.ini", "check_disk")); | 56 | char *optstr = list2str(np_get_defaults("section@./config-tiny.ini", "check_disk")); |
57 | ok(!strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "config-tiny.ini's section as expected"); | 57 | ok(!strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), |
58 | "config-tiny.ini's section as expected"); | ||
58 | my_free(optstr); | 59 | my_free(optstr); |
59 | 60 | ||
60 | optstr = list2str(np_get_defaults("@./config-tiny.ini", "section")); | 61 | optstr = list2str(np_get_defaults("@./config-tiny.ini", "section")); |
61 | ok(!strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "Used default section name, without specific"); | 62 | ok(!strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), |
63 | "Used default section name, without specific"); | ||
62 | my_free(optstr); | 64 | my_free(optstr); |
63 | 65 | ||
64 | optstr = list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk")); | 66 | optstr = list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk")); |
65 | ok(!strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected"); | 67 | ok(!strcmp(optstr, "--something else=blah --remove=whitespace"), |
68 | "config-tiny.ini's Section Two as expected"); | ||
66 | my_free(optstr); | 69 | my_free(optstr); |
67 | 70 | ||
68 | optstr = list2str(np_get_defaults("/path/to/file.txt@./config-tiny.ini", "check_disk")); | 71 | optstr = list2str(np_get_defaults("/path/to/file.txt@./config-tiny.ini", "check_disk")); |
@@ -70,15 +73,18 @@ int main(int argc, char **argv) { | |||
70 | my_free(optstr); | 73 | my_free(optstr); |
71 | 74 | ||
72 | optstr = list2str(np_get_defaults("section2@./config-tiny.ini", "check_disk")); | 75 | optstr = list2str(np_get_defaults("section2@./config-tiny.ini", "check_disk")); |
73 | ok(!strcmp(optstr, "--this=that"), "config-tiny.ini's section2 with whitespace before section name"); | 76 | ok(!strcmp(optstr, "--this=that"), |
77 | "config-tiny.ini's section2 with whitespace before section name"); | ||
74 | my_free(optstr); | 78 | my_free(optstr); |
75 | 79 | ||
76 | optstr = list2str(np_get_defaults("section3@./config-tiny.ini", "check_disk")); | 80 | optstr = list2str(np_get_defaults("section3@./config-tiny.ini", "check_disk")); |
77 | ok(!strcmp(optstr, "--this=that"), "config-tiny.ini's section3 with whitespace after section name"); | 81 | ok(!strcmp(optstr, "--this=that"), |
82 | "config-tiny.ini's section3 with whitespace after section name"); | ||
78 | my_free(optstr); | 83 | my_free(optstr); |
79 | 84 | ||
80 | optstr = list2str(np_get_defaults("check_mysql@./plugin.ini", "check_disk")); | 85 | optstr = list2str(np_get_defaults("check_mysql@./plugin.ini", "check_disk")); |
81 | ok(!strcmp(optstr, "--username=operator --password=secret"), "plugin.ini's check_mysql as expected"); | 86 | ok(!strcmp(optstr, "--username=operator --password=secret"), |
87 | "plugin.ini's check_mysql as expected"); | ||
82 | my_free(optstr); | 88 | my_free(optstr); |
83 | 89 | ||
84 | optstr = list2str(np_get_defaults("check_mysql2@./plugin.ini", "check_disk")); | 90 | optstr = list2str(np_get_defaults("check_mysql2@./plugin.ini", "check_disk")); |
@@ -90,29 +96,39 @@ int main(int argc, char **argv) { | |||
90 | my_free(optstr); | 96 | my_free(optstr); |
91 | 97 | ||
92 | optstr = list2str(np_get_defaults("Section Two@./config-dos.ini", "check_disk")); | 98 | optstr = list2str(np_get_defaults("Section Two@./config-dos.ini", "check_disk")); |
93 | ok(!strcmp(optstr, "--something else=blah --remove=whitespace"), "config-dos.ini's Section Two as expected"); | 99 | ok(!strcmp(optstr, "--something else=blah --remove=whitespace"), |
100 | "config-dos.ini's Section Two as expected"); | ||
94 | my_free(optstr); | 101 | my_free(optstr); |
95 | 102 | ||
96 | optstr = list2str(np_get_defaults("section_twice@./plugin.ini", "check_disk")); | 103 | optstr = list2str(np_get_defaults("section_twice@./plugin.ini", "check_disk")); |
97 | ok(!strcmp(optstr, "--foo=bar --bar=foo"), "plugin.ini's section_twice defined twice in the file"); | 104 | ok(!strcmp(optstr, "--foo=bar --bar=foo"), |
105 | "plugin.ini's section_twice defined twice in the file"); | ||
98 | my_free(optstr); | 106 | my_free(optstr); |
99 | 107 | ||
100 | optstr = list2str(np_get_defaults("tcp_long_lines@plugins.ini", "check_tcp")); | 108 | optstr = list2str(np_get_defaults("tcp_long_lines@plugins.ini", "check_tcp")); |
101 | ok(!strcmp(optstr, "--escape --send=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar " | 109 | ok(!strcmp(optstr, "--escape --send=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
110 | "yadda Foo bar BAZ yadda yadda yadda Foo bar " | ||
102 | "BAZ yadda yadda yadda Foo bar BAZ yadda yadda " | 111 | "BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
103 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda " | 112 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar " |
113 | "BAZ yadda yadda yadda Foo bar BAZ yadda " | ||
104 | "yadda yadda Foo bar BAZ yadda yadda yadda Foo " | 114 | "yadda yadda Foo bar BAZ yadda yadda yadda Foo " |
105 | "bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " | 115 | "bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda " |
116 | "yadda yadda Foo bar BAZ yadda yadda " | ||
106 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ " | 117 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ " |
107 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda --expect=Foo bar BAZ yadda yadda " | 118 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
119 | "yadda --expect=Foo bar BAZ yadda yadda " | ||
108 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ " | 120 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ " |
109 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo " | 121 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
122 | "yadda Foo bar BAZ yadda yadda yadda Foo " | ||
110 | "bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " | 123 | "bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
111 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda " | 124 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar " |
125 | "BAZ yadda yadda yadda Foo bar BAZ yadda " | ||
112 | "yadda yadda Foo bar BAZ yadda yadda yadda Foo " | 126 | "yadda yadda Foo bar BAZ yadda yadda yadda Foo " |
113 | "bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " | 127 | "bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda " |
128 | "yadda yadda Foo bar BAZ yadda yadda " | ||
114 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ " | 129 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ " |
115 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo " | 130 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
131 | "yadda Foo bar BAZ yadda yadda yadda Foo " | ||
116 | "bar BAZ yadda yadda yadda --jail"), | 132 | "bar BAZ yadda yadda yadda --jail"), |
117 | "Long options"); | 133 | "Long options"); |
118 | my_free(optstr); | 134 | my_free(optstr); |
diff --git a/lib/tests/test_opts1.c b/lib/tests/test_opts1.c index 984183d3..fa95c4d4 100644 --- a/lib/tests/test_opts1.c +++ b/lib/tests/test_opts1.c | |||
@@ -40,37 +40,40 @@ void my_free(int *argc, char **newargv, char **argv) { | |||
40 | #else | 40 | #else |
41 | void my_free(int *argc, char **newargv, char **argv) { | 41 | void my_free(int *argc, char **newargv, char **argv) { |
42 | /* Free stuff (and print while we're at it) */ | 42 | /* Free stuff (and print while we're at it) */ |
43 | int i, freeflag = 1; | 43 | bool freeflag = true; |
44 | printf(" Arg(%i): ", *argc + 1); | 44 | printf(" Arg(%i): ", *argc + 1); |
45 | printf("'%s' ", newargv[0]); | 45 | printf("'%s' ", newargv[0]); |
46 | for (i = 1; i < *argc; i++) { | 46 | |
47 | for (int i = 1; i < *argc; i++) { | ||
47 | printf("'%s' ", newargv[i]); | 48 | printf("'%s' ", newargv[i]); |
48 | /* Stop freeing when we get to the start of the original array */ | 49 | /* Stop freeing when we get to the start of the original array */ |
49 | if (freeflag) { | 50 | if (freeflag) { |
50 | if (newargv[i] == argv[1]) | 51 | if (newargv[i] == argv[1]) { |
51 | freeflag = 0; | 52 | freeflag = false; |
52 | else | 53 | } else { |
53 | free(newargv[i]); | 54 | free(newargv[i]); |
55 | } | ||
54 | } | 56 | } |
55 | } | 57 | } |
56 | printf("\n"); | 58 | printf("\n"); |
57 | /* Free only if it's a different array */ | 59 | /* Free only if it's a different array */ |
58 | if (newargv != argv) | 60 | if (newargv != argv) { |
59 | free(newargv); | 61 | free(newargv); |
62 | } | ||
60 | *argc = 0; | 63 | *argc = 0; |
61 | } | 64 | } |
62 | #endif | 65 | #endif |
63 | 66 | ||
64 | int array_diff(int i1, char **a1, int i2, char **a2) { | 67 | int array_diff(int i1, char **a1, int i2, char **a2) { |
65 | int i; | ||
66 | |||
67 | if (i1 != i2) { | 68 | if (i1 != i2) { |
68 | printf(" Argument count doesn't match!\n"); | 69 | printf(" Argument count doesn't match!\n"); |
69 | return 0; | 70 | return 0; |
70 | } | 71 | } |
71 | for (i = 0; i <= i1; i++) { | 72 | |
72 | if (a1[i] == NULL && a2[i] == NULL) | 73 | for (int i = 0; i <= i1; i++) { |
74 | if (a1[i] == NULL && a2[i] == NULL) { | ||
73 | continue; | 75 | continue; |
76 | } | ||
74 | if (a1[i] == NULL || a2[i] == NULL) { | 77 | if (a1[i] == NULL || a2[i] == NULL) { |
75 | printf(" Argument # %i null in one array!\n", i); | 78 | printf(" Argument # %i null in one array!\n", i); |
76 | return 0; | 79 | return 0; |
@@ -84,11 +87,10 @@ int array_diff(int i1, char **a1, int i2, char **a2) { | |||
84 | } | 87 | } |
85 | 88 | ||
86 | int main(int argc, char **argv) { | 89 | int main(int argc, char **argv) { |
87 | char **argv_new = NULL; | ||
88 | int i, argc_test; | ||
89 | |||
90 | plan_tests(5); | 90 | plan_tests(5); |
91 | 91 | ||
92 | char **argv_new = NULL; | ||
93 | int argc_test; | ||
92 | { | 94 | { |
93 | char *argv_test[] = {"prog_name", (char *)NULL}; | 95 | char *argv_test[] = {"prog_name", (char *)NULL}; |
94 | argc_test = 1; | 96 | argc_test = 1; |
@@ -110,27 +112,36 @@ int main(int argc, char **argv) { | |||
110 | { | 112 | { |
111 | char *argv_test[] = {"prog_name", "--extra-opts=@./config-opts.ini", (char *)NULL}; | 113 | char *argv_test[] = {"prog_name", "--extra-opts=@./config-opts.ini", (char *)NULL}; |
112 | argc_test = 2; | 114 | argc_test = 2; |
113 | char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", "--blank", (char *)NULL}; | 115 | char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", "--blank", |
116 | (char *)NULL}; | ||
114 | argv_new = np_extra_opts(&argc_test, argv_test, "check_disk"); | 117 | argv_new = np_extra_opts(&argc_test, argv_test, "check_disk"); |
115 | ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts using default section"); | 118 | ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts using default section"); |
116 | my_free(&argc_test, argv_new, argv_test); | 119 | my_free(&argc_test, argv_new, argv_test); |
117 | } | 120 | } |
118 | 121 | ||
119 | { | 122 | { |
120 | char *argv_test[] = {"prog_name", "--extra-opts=sect1@./config-opts.ini", "--extra-opts", "sect2@./config-opts.ini", (char *)NULL}; | 123 | char *argv_test[] = {"prog_name", "--extra-opts=sect1@./config-opts.ini", "--extra-opts", |
124 | "sect2@./config-opts.ini", (char *)NULL}; | ||
121 | argc_test = 4; | 125 | argc_test = 4; |
122 | char *argv_known[] = {"prog_name", "--one=two", "--something else=oops", "--this=that", (char *)NULL}; | 126 | char *argv_known[] = {"prog_name", "--one=two", "--something else=oops", "--this=that", |
127 | (char *)NULL}; | ||
123 | argv_new = np_extra_opts(&argc_test, argv_test, "check_disk"); | 128 | argv_new = np_extra_opts(&argc_test, argv_test, "check_disk"); |
124 | ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts specified twice"); | 129 | ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts specified twice"); |
125 | my_free(&argc_test, argv_new, argv_test); | 130 | my_free(&argc_test, argv_new, argv_test); |
126 | } | 131 | } |
127 | 132 | ||
128 | { | 133 | { |
129 | char *argv_test[] = {"prog_name", "--arg1=val1", "--extra-opts=@./config-opts.ini", "--extra-opts", "sect1@./config-opts.ini", | 134 | char *argv_test[] = {"prog_name", |
130 | "--arg2", (char *)NULL}; | 135 | "--arg1=val1", |
136 | "--extra-opts=@./config-opts.ini", | ||
137 | "--extra-opts", | ||
138 | "sect1@./config-opts.ini", | ||
139 | "--arg2", | ||
140 | (char *)NULL}; | ||
131 | argc_test = 6; | 141 | argc_test = 6; |
132 | char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", "--blank", "--one=two", | 142 | char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", |
133 | "--arg1=val1", "--arg2", (char *)NULL}; | 143 | "--blank", "--one=two", "--arg1=val1", |
144 | "--arg2", (char *)NULL}; | ||
134 | argv_new = np_extra_opts(&argc_test, argv_test, "check_disk"); | 145 | argv_new = np_extra_opts(&argc_test, argv_test, "check_disk"); |
135 | ok(array_diff(argc_test, argv_new, 7, argv_known), "twice extra opts using two sections"); | 146 | ok(array_diff(argc_test, argv_new, 7, argv_known), "twice extra opts using two sections"); |
136 | my_free(&argc_test, argv_new, argv_test); | 147 | my_free(&argc_test, argv_new, argv_test); |
diff --git a/lib/tests/test_opts2.c b/lib/tests/test_opts2.c index 23496617..3dd1b039 100644 --- a/lib/tests/test_opts2.c +++ b/lib/tests/test_opts2.c | |||
@@ -23,36 +23,39 @@ | |||
23 | 23 | ||
24 | void my_free(int *argc, char **newargv, char **argv) { | 24 | void my_free(int *argc, char **newargv, char **argv) { |
25 | /* Free stuff (and print while we're at it) */ | 25 | /* Free stuff (and print while we're at it) */ |
26 | int i, freeflag = 1; | 26 | bool freeflag = true; |
27 | |||
27 | printf(" Arg(%i): ", *argc + 1); | 28 | printf(" Arg(%i): ", *argc + 1); |
28 | printf("'%s' ", newargv[0]); | 29 | printf("'%s' ", newargv[0]); |
29 | for (i = 1; i < *argc; i++) { | 30 | for (int i = 1; i < *argc; i++) { |
30 | printf("'%s' ", newargv[i]); | 31 | printf("'%s' ", newargv[i]); |
31 | /* Stop freeing when we get to the start of the original array */ | 32 | /* Stop freeing when we get to the start of the original array */ |
32 | if (freeflag) { | 33 | if (freeflag) { |
33 | if (newargv[i] == argv[1]) | 34 | if (newargv[i] == argv[1]) { |
34 | freeflag = 0; | 35 | freeflag = false; |
35 | else | 36 | } else { |
36 | free(newargv[i]); | 37 | free(newargv[i]); |
38 | } | ||
37 | } | 39 | } |
38 | } | 40 | } |
39 | printf("\n"); | 41 | printf("\n"); |
40 | /* Free only if it's a different array */ | 42 | /* Free only if it's a different array */ |
41 | if (newargv != argv) | 43 | if (newargv != argv) { |
42 | free(newargv); | 44 | free(newargv); |
45 | } | ||
43 | *argc = 0; | 46 | *argc = 0; |
44 | } | 47 | } |
45 | 48 | ||
46 | int array_diff(int i1, char **a1, int i2, char **a2) { | 49 | int array_diff(int i1, char **a1, int i2, char **a2) { |
47 | int i; | ||
48 | |||
49 | if (i1 != i2) { | 50 | if (i1 != i2) { |
50 | printf(" Argument count doesn't match!\n"); | 51 | printf(" Argument count doesn't match!\n"); |
51 | return 0; | 52 | return 0; |
52 | } | 53 | } |
53 | for (i = 0; i <= i1; i++) { | 54 | |
54 | if (a1[i] == NULL && a2[i] == NULL) | 55 | for (int i = 0; i <= i1; i++) { |
56 | if (a1[i] == NULL && a2[i] == NULL) { | ||
55 | continue; | 57 | continue; |
58 | } | ||
56 | if (a1[i] == NULL || a2[i] == NULL) { | 59 | if (a1[i] == NULL || a2[i] == NULL) { |
57 | printf(" Argument # %i null in one array!\n", i); | 60 | printf(" Argument # %i null in one array!\n", i); |
58 | return 0; | 61 | return 0; |
@@ -66,11 +69,10 @@ int array_diff(int i1, char **a1, int i2, char **a2) { | |||
66 | } | 69 | } |
67 | 70 | ||
68 | int main(int argc, char **argv) { | 71 | int main(int argc, char **argv) { |
69 | char **argv_new = NULL; | ||
70 | int i, argc_test; | ||
71 | |||
72 | plan_tests(5); | 72 | plan_tests(5); |
73 | 73 | ||
74 | char **argv_new = NULL; | ||
75 | int argc_test; | ||
74 | { | 76 | { |
75 | char *argv_test[] = {"prog_name", "arg1", "--extra-opts", "--arg3", "val2", (char *)NULL}; | 77 | char *argv_test[] = {"prog_name", "arg1", "--extra-opts", "--arg3", "val2", (char *)NULL}; |
76 | argc_test = 5; | 78 | argc_test = 5; |
@@ -90,7 +92,8 @@ int main(int argc, char **argv) { | |||
90 | } | 92 | } |
91 | 93 | ||
92 | { | 94 | { |
93 | char *argv_test[] = {"prog_name", "arg1", "--extra-opts=section1", "--arg3", "val2", (char *)NULL}; | 95 | char *argv_test[] = {"prog_name", "arg1", "--extra-opts=section1", |
96 | "--arg3", "val2", (char *)NULL}; | ||
94 | argc_test = 5; | 97 | argc_test = 5; |
95 | char *argv_known[] = {"prog_name", "--foobar=baz", "arg1", "--arg3", "val2", (char *)NULL}; | 98 | char *argv_known[] = {"prog_name", "--foobar=baz", "arg1", "--arg3", "val2", (char *)NULL}; |
96 | argv_new = np_extra_opts(&argc_test, argv_test, "check_disk"); | 99 | argv_new = np_extra_opts(&argc_test, argv_test, "check_disk"); |
@@ -108,30 +111,39 @@ int main(int argc, char **argv) { | |||
108 | } | 111 | } |
109 | 112 | ||
110 | { | 113 | { |
111 | char *argv_test[] = {"check_tcp", "--extra-opts", "--extra-opts=tcp_long_lines", (char *)NULL}; | 114 | char *argv_test[] = {"check_tcp", "--extra-opts", "--extra-opts=tcp_long_lines", |
115 | (char *)NULL}; | ||
112 | argc_test = 3; | 116 | argc_test = 3; |
113 | char *argv_known[] = { | 117 | char *argv_known[] = {"check_tcp", |
114 | "check_tcp", | 118 | "--timeout=10", |
115 | "--timeout=10", | 119 | "--escape", |
116 | "--escape", | 120 | "--send=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda " |
117 | "--send=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " | 121 | "Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
118 | "yadda Foo bar BAZ yadda " | 122 | "yadda Foo bar BAZ yadda " |
119 | "yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " | 123 | "yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
120 | "yadda Foo bar BAZ " | 124 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
121 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda " | 125 | "yadda Foo bar BAZ " |
122 | "yadda yadda Foo bar " | 126 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda " |
123 | "BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda", | 127 | "yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda " |
124 | "--expect=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " | 128 | "yadda yadda Foo bar " |
125 | "yadda Foo bar BAZ yadda " | 129 | "BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ " |
126 | "yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " | 130 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda", |
127 | "yadda Foo bar BAZ " | 131 | "--expect=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
128 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda " | 132 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
129 | "yadda yadda Foo bar " | 133 | "yadda Foo bar BAZ yadda " |
130 | "BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ " | 134 | "yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
131 | "yadda yadda yadda Foo " | 135 | "yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda " |
132 | "bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda", | 136 | "yadda Foo bar BAZ " |
133 | "--jail", | 137 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda " |
134 | (char *)NULL}; | 138 | "yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda " |
139 | "yadda yadda Foo bar " | ||
140 | "BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ " | ||
141 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ " | ||
142 | "yadda yadda yadda Foo " | ||
143 | "bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ " | ||
144 | "yadda yadda yadda Foo bar BAZ yadda yadda yadda", | ||
145 | "--jail", | ||
146 | (char *)NULL}; | ||
135 | argv_new = np_extra_opts(&argc_test, argv_test, "check_tcp"); | 147 | argv_new = np_extra_opts(&argc_test, argv_test, "check_tcp"); |
136 | ok(array_diff(argc_test, argv_new, 6, argv_known), "Long lines test"); | 148 | ok(array_diff(argc_test, argv_new, 6, argv_known), "Long lines test"); |
137 | my_free(&argc_test, argv_new, argv_test); | 149 | my_free(&argc_test, argv_new, argv_test); |
diff --git a/lib/tests/test_tcp.c b/lib/tests/test_tcp.c index 1b3003e9..37c818c9 100644 --- a/lib/tests/test_tcp.c +++ b/lib/tests/test_tcp.c | |||
@@ -21,30 +21,38 @@ | |||
21 | #include "tap.h" | 21 | #include "tap.h" |
22 | 22 | ||
23 | int main(void) { | 23 | int main(void) { |
24 | char **server_expect; | ||
25 | int server_expect_count = 3; | ||
26 | |||
27 | plan_tests(9); | 24 | plan_tests(9); |
28 | 25 | ||
26 | char **server_expect; | ||
27 | const int server_expect_count = 3; | ||
29 | server_expect = malloc(sizeof(char *) * server_expect_count); | 28 | server_expect = malloc(sizeof(char *) * server_expect_count); |
30 | 29 | ||
31 | server_expect[0] = strdup("AA"); | 30 | server_expect[0] = strdup("AA"); |
32 | server_expect[1] = strdup("bb"); | 31 | server_expect[1] = strdup("bb"); |
33 | server_expect[2] = strdup("CC"); | 32 | server_expect[2] = strdup("CC"); |
34 | 33 | ||
35 | ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_SUCCESS, | 34 | ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == |
35 | NP_MATCH_SUCCESS, | ||
36 | "Test matching any string at the beginning (first expect string)"); | 36 | "Test matching any string at the beginning (first expect string)"); |
37 | ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_SUCCESS, | 37 | ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == |
38 | NP_MATCH_SUCCESS, | ||
38 | "Test matching any string at the beginning (second expect string)"); | 39 | "Test matching any string at the beginning (second expect string)"); |
39 | ok(np_expect_match("b", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_RETRY, | 40 | ok(np_expect_match("b", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_RETRY, |
40 | "Test matching any string at the beginning (substring match)"); | 41 | "Test matching any string at the beginning (substring match)"); |
41 | ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_FAILURE, | 42 | ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == |
43 | NP_MATCH_FAILURE, | ||
42 | "Test with strings not matching at the beginning"); | 44 | "Test with strings not matching at the beginning"); |
43 | ok(np_expect_match("XX CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_FAILURE, "Test matching any string"); | 45 | ok(np_expect_match("XX CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == |
44 | ok(np_expect_match("XX", server_expect, server_expect_count, 0) == NP_MATCH_RETRY, "Test not matching any string"); | 46 | NP_MATCH_FAILURE, |
45 | ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_SUCCESS, | 47 | "Test matching any string"); |
48 | ok(np_expect_match("XX", server_expect, server_expect_count, 0) == NP_MATCH_RETRY, | ||
49 | "Test not matching any string"); | ||
50 | ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == | ||
51 | NP_MATCH_SUCCESS, | ||
46 | "Test matching all strings"); | 52 | "Test matching all strings"); |
47 | ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_RETRY, "Test not matching all strings"); | 53 | ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == |
54 | NP_MATCH_RETRY, | ||
55 | "Test not matching all strings"); | ||
48 | ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_RETRY, | 56 | ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_RETRY, |
49 | "Test not matching any string (testing all)"); | 57 | "Test not matching any string (testing all)"); |
50 | 58 | ||
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index c3150f00..8040dec8 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c | |||
@@ -28,17 +28,7 @@ | |||
28 | #include "utils_base.c" | 28 | #include "utils_base.c" |
29 | 29 | ||
30 | int main(int argc, char **argv) { | 30 | int main(int argc, char **argv) { |
31 | char state_path[1024]; | 31 | plan_tests(155); |
32 | range *range; | ||
33 | double temp; | ||
34 | thresholds *thresholds = NULL; | ||
35 | int i, rc; | ||
36 | char *temp_string; | ||
37 | state_key *temp_state_key = NULL; | ||
38 | state_data *temp_state_data; | ||
39 | time_t current_time; | ||
40 | |||
41 | plan_tests(185); | ||
42 | 32 | ||
43 | ok(this_monitoring_plugin == NULL, "monitoring_plugin not initialised"); | 33 | ok(this_monitoring_plugin == NULL, "monitoring_plugin not initialised"); |
44 | 34 | ||
@@ -57,7 +47,7 @@ int main(int argc, char **argv) { | |||
57 | 47 | ||
58 | np_set_args(argc, argv); | 48 | np_set_args(argc, argv); |
59 | 49 | ||
60 | range = parse_range_string("6"); | 50 | range *range = parse_range_string("6"); |
61 | ok(range != NULL, "'6' is valid range"); | 51 | ok(range != NULL, "'6' is valid range"); |
62 | ok(range->start == 0, "Start correct"); | 52 | ok(range->start == 0, "Start correct"); |
63 | ok(range->start_infinity == false, "Not using negative infinity"); | 53 | ok(range->start_infinity == false, "Not using negative infinity"); |
@@ -97,7 +87,7 @@ int main(int argc, char **argv) { | |||
97 | free(range); | 87 | free(range); |
98 | 88 | ||
99 | range = parse_range_string("12345678901234567890:"); | 89 | range = parse_range_string("12345678901234567890:"); |
100 | temp = atof("12345678901234567890"); /* Can't just use this because number too large */ | 90 | double temp = atof("12345678901234567890"); /* Can't just use this because number too large */ |
101 | ok(range != NULL, "'12345678901234567890:' is valid range"); | 91 | ok(range != NULL, "'12345678901234567890:' is valid range"); |
102 | ok(range->start == temp, "Start correct"); | 92 | ok(range->start == temp, "Start correct"); |
103 | ok(range->start_infinity == false, "Not using negative infinity"); | 93 | ok(range->start_infinity == false, "Not using negative infinity"); |
@@ -158,32 +148,34 @@ int main(int argc, char **argv) { | |||
158 | range = parse_range_string("2:1"); | 148 | range = parse_range_string("2:1"); |
159 | ok(range == NULL, "'2:1' rejected"); | 149 | ok(range == NULL, "'2:1' rejected"); |
160 | 150 | ||
161 | rc = _set_thresholds(&thresholds, NULL, NULL); | 151 | thresholds *thresholds = NULL; |
162 | ok(rc == 0, "Thresholds (NULL, NULL) set"); | 152 | int returnCode; |
153 | returnCode = _set_thresholds(&thresholds, NULL, NULL); | ||
154 | ok(returnCode == 0, "Thresholds (NULL, NULL) set"); | ||
163 | ok(thresholds->warning == NULL, "Warning not set"); | 155 | ok(thresholds->warning == NULL, "Warning not set"); |
164 | ok(thresholds->critical == NULL, "Critical not set"); | 156 | ok(thresholds->critical == NULL, "Critical not set"); |
165 | 157 | ||
166 | rc = _set_thresholds(&thresholds, NULL, "80"); | 158 | returnCode = _set_thresholds(&thresholds, NULL, "80"); |
167 | ok(rc == 0, "Thresholds (NULL, '80') set"); | 159 | ok(returnCode == 0, "Thresholds (NULL, '80') set"); |
168 | ok(thresholds->warning == NULL, "Warning not set"); | 160 | ok(thresholds->warning == NULL, "Warning not set"); |
169 | ok(thresholds->critical->end == 80, "Critical set correctly"); | 161 | ok(thresholds->critical->end == 80, "Critical set correctly"); |
170 | 162 | ||
171 | rc = _set_thresholds(&thresholds, "5:33", NULL); | 163 | returnCode = _set_thresholds(&thresholds, "5:33", NULL); |
172 | ok(rc == 0, "Thresholds ('5:33', NULL) set"); | 164 | ok(returnCode == 0, "Thresholds ('5:33', NULL) set"); |
173 | ok(thresholds->warning->start == 5, "Warning start set"); | 165 | ok(thresholds->warning->start == 5, "Warning start set"); |
174 | ok(thresholds->warning->end == 33, "Warning end set"); | 166 | ok(thresholds->warning->end == 33, "Warning end set"); |
175 | ok(thresholds->critical == NULL, "Critical not set"); | 167 | ok(thresholds->critical == NULL, "Critical not set"); |
176 | 168 | ||
177 | rc = _set_thresholds(&thresholds, "30", "60"); | 169 | returnCode = _set_thresholds(&thresholds, "30", "60"); |
178 | ok(rc == 0, "Thresholds ('30', '60') set"); | 170 | ok(returnCode == 0, "Thresholds ('30', '60') set"); |
179 | ok(thresholds->warning->end == 30, "Warning set correctly"); | 171 | ok(thresholds->warning->end == 30, "Warning set correctly"); |
180 | ok(thresholds->critical->end == 60, "Critical set correctly"); | 172 | ok(thresholds->critical->end == 60, "Critical set correctly"); |
181 | ok(get_status(15.3, thresholds) == STATE_OK, "15.3 - ok"); | 173 | ok(get_status(15.3, thresholds) == STATE_OK, "15.3 - ok"); |
182 | ok(get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); | 174 | ok(get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); |
183 | ok(get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); | 175 | ok(get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); |
184 | 176 | ||
185 | rc = _set_thresholds(&thresholds, "-10:-2", "-30:20"); | 177 | returnCode = _set_thresholds(&thresholds, "-10:-2", "-30:20"); |
186 | ok(rc == 0, "Thresholds ('-30:20', '-10:-2') set"); | 178 | ok(returnCode == 0, "Thresholds ('-30:20', '-10:-2') set"); |
187 | ok(thresholds->warning->start == -10, "Warning start set correctly"); | 179 | ok(thresholds->warning->start == -10, "Warning start set correctly"); |
188 | ok(thresholds->warning->end == -2, "Warning end set correctly"); | 180 | ok(thresholds->warning->end == -2, "Warning end set correctly"); |
189 | ok(thresholds->critical->start == -30, "Critical start set correctly"); | 181 | ok(thresholds->critical->start == -30, "Critical start set correctly"); |
@@ -304,164 +296,28 @@ int main(int argc, char **argv) { | |||
304 | test = np_extract_ntpvar("", "foo"); | 296 | test = np_extract_ntpvar("", "foo"); |
305 | ok(!test, "Empty string return NULL"); | 297 | ok(!test, "Empty string return NULL"); |
306 | 298 | ||
307 | /* This is the result of running ./test_utils */ | ||
308 | temp_string = (char *)_np_state_generate_key(); | ||
309 | ok(!strcmp(temp_string, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got hash with exe and no parameters") || | ||
310 | diag("You are probably running in wrong directory. Must run as ./test_utils"); | ||
311 | |||
312 | this_monitoring_plugin->argc = 4; | ||
313 | this_monitoring_plugin->argv[0] = "./test_utils"; | ||
314 | this_monitoring_plugin->argv[1] = "here"; | ||
315 | this_monitoring_plugin->argv[2] = "--and"; | ||
316 | this_monitoring_plugin->argv[3] = "now"; | ||
317 | temp_string = (char *)_np_state_generate_key(); | ||
318 | ok(!strcmp(temp_string, "bd72da9f78ff1419fad921ea5e43ce56508aef6c"), "Got based on expected argv"); | ||
319 | |||
320 | unsetenv("MP_STATE_PATH"); | ||
321 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
322 | ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory"); | ||
323 | |||
324 | setenv("MP_STATE_PATH", "", 1); | ||
325 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
326 | ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string"); | ||
327 | |||
328 | setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1); | ||
329 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
330 | ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory"); | ||
331 | |||
332 | ok(temp_state_key == NULL, "temp_state_key initially empty"); | ||
333 | |||
334 | this_monitoring_plugin->argc = 1; | ||
335 | this_monitoring_plugin->argv[0] = "./test_utils"; | ||
336 | np_enable_state(NULL, 51); | ||
337 | temp_state_key = this_monitoring_plugin->state; | ||
338 | ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name"); | ||
339 | ok(!strcmp(temp_state_key->name, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got generated filename"); | ||
340 | |||
341 | np_enable_state("allowedchars_in_keyname", 77); | ||
342 | temp_state_key = this_monitoring_plugin->state; | ||
343 | sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/allowedchars_in_keyname", (unsigned long)geteuid()); | ||
344 | ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name"); | ||
345 | ok(!strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars"); | ||
346 | ok(!strcmp(temp_state_key->_filename, state_path), "Got internal filename"); | ||
347 | |||
348 | /* Don't do this test just yet. Will die */ | ||
349 | /* | ||
350 | np_enable_state("bad^chars$in@here", 77); | ||
351 | temp_state_key = this_monitoring_plugin->state; | ||
352 | ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" ); | ||
353 | */ | ||
354 | |||
355 | np_enable_state("funnykeyname", 54); | ||
356 | temp_state_key = this_monitoring_plugin->state; | ||
357 | sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/funnykeyname", (unsigned long)geteuid()); | ||
358 | ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name"); | ||
359 | ok(!strcmp(temp_state_key->name, "funnykeyname"), "Got key name"); | ||
360 | |||
361 | ok(!strcmp(temp_state_key->_filename, state_path), "Got internal filename"); | ||
362 | ok(temp_state_key->data_version == 54, "Version set"); | ||
363 | |||
364 | temp_state_data = np_state_read(); | ||
365 | ok(temp_state_data == NULL, "Got no state data as file does not exist"); | ||
366 | |||
367 | /* | ||
368 | temp_fp = fopen("var/statefile", "r"); | ||
369 | if (temp_fp==NULL) | ||
370 | printf("Error opening. errno=%d\n", errno); | ||
371 | printf("temp_fp=%s\n", temp_fp); | ||
372 | ok( _np_state_read_file(temp_fp) == true, "Can read state file" ); | ||
373 | fclose(temp_fp); | ||
374 | */ | ||
375 | |||
376 | temp_state_key->_filename = "var/statefile"; | ||
377 | temp_state_data = np_state_read(); | ||
378 | ok(this_monitoring_plugin->state->state_data != NULL, "Got state data now") || | ||
379 | diag("Are you running in right directory? Will get coredump next if not"); | ||
380 | ok(this_monitoring_plugin->state->state_data->time == 1234567890, "Got time"); | ||
381 | ok(!strcmp((char *)this_monitoring_plugin->state->state_data->data, "String to read"), "Data as expected"); | ||
382 | |||
383 | temp_state_key->data_version = 53; | ||
384 | temp_state_data = np_state_read(); | ||
385 | ok(temp_state_data == NULL, "Older data version gives NULL"); | ||
386 | temp_state_key->data_version = 54; | ||
387 | |||
388 | temp_state_key->_filename = "var/nonexistent"; | ||
389 | temp_state_data = np_state_read(); | ||
390 | ok(temp_state_data == NULL, "Missing file gives NULL"); | ||
391 | ok(this_monitoring_plugin->state->state_data == NULL, "No state information"); | ||
392 | |||
393 | temp_state_key->_filename = "var/oldformat"; | ||
394 | temp_state_data = np_state_read(); | ||
395 | ok(temp_state_data == NULL, "Old file format gives NULL"); | ||
396 | |||
397 | temp_state_key->_filename = "var/baddate"; | ||
398 | temp_state_data = np_state_read(); | ||
399 | ok(temp_state_data == NULL, "Bad date gives NULL"); | ||
400 | |||
401 | temp_state_key->_filename = "var/missingdataline"; | ||
402 | temp_state_data = np_state_read(); | ||
403 | ok(temp_state_data == NULL, "Missing data line gives NULL"); | ||
404 | |||
405 | unlink("var/generated"); | ||
406 | temp_state_key->_filename = "var/generated"; | ||
407 | current_time = 1234567890; | ||
408 | np_state_write_string(current_time, "String to read"); | ||
409 | ok(system("cmp var/generated var/statefile") == 0, "Generated file same as expected"); | ||
410 | |||
411 | unlink("var/generated_directory/statefile"); | ||
412 | unlink("var/generated_directory"); | ||
413 | temp_state_key->_filename = "var/generated_directory/statefile"; | ||
414 | current_time = 1234567890; | ||
415 | np_state_write_string(current_time, "String to read"); | ||
416 | ok(system("cmp var/generated_directory/statefile var/statefile") == 0, "Have created directory"); | ||
417 | |||
418 | /* This test to check cannot write to dir - can't automate yet */ | ||
419 | /* | ||
420 | unlink("var/generated_bad_dir"); | ||
421 | mkdir("var/generated_bad_dir", S_IRUSR); | ||
422 | np_state_write_string(current_time, "String to read"); | ||
423 | */ | ||
424 | |||
425 | temp_state_key->_filename = "var/generated"; | ||
426 | time(¤t_time); | ||
427 | np_state_write_string(0, "String to read"); | ||
428 | temp_state_data = np_state_read(); | ||
429 | /* Check time is set to current_time */ | ||
430 | ok(system("cmp var/generated var/statefile > /dev/null") != 0, "Generated file should be different this time"); | ||
431 | ok(this_monitoring_plugin->state->state_data->time - current_time <= 1, "Has time generated from current time"); | ||
432 | |||
433 | /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ | ||
434 | /* | ||
435 | temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write"; | ||
436 | np_state_write_string(0, "Bad file"); | ||
437 | */ | ||
438 | |||
439 | np_cleanup(); | ||
440 | |||
441 | ok(this_monitoring_plugin == NULL, "Free'd this_monitoring_plugin"); | ||
442 | |||
443 | ok(mp_suid() == false, "Test aren't suid"); | 299 | ok(mp_suid() == false, "Test aren't suid"); |
444 | 300 | ||
445 | /* base states with random case */ | 301 | /* base states with random case */ |
446 | char *states[] = {"Ok", "wArnINg", "cRiTIcaL", "UnKNoWN", NULL}; | 302 | char *states[] = {"Ok", "wArnINg", "cRiTIcaL", "UnKNoWN", NULL}; |
447 | 303 | ||
448 | for (i = 0; states[i] != NULL; i++) { | 304 | for (int i = 0; states[i] != NULL; i++) { |
449 | /* out of the random case states, create the lower and upper versions + numeric string one */ | 305 | /* out of the random case states, create the lower and upper versions + numeric string one |
306 | */ | ||
450 | char *statelower = strdup(states[i]); | 307 | char *statelower = strdup(states[i]); |
451 | char *stateupper = strdup(states[i]); | 308 | char *stateupper = strdup(states[i]); |
452 | char statenum[2]; | 309 | char statenum[2]; |
453 | char *temp_ptr; | 310 | for (char *temp_ptr = statelower; *temp_ptr; temp_ptr++) { |
454 | for (temp_ptr = statelower; *temp_ptr; temp_ptr++) { | 311 | *temp_ptr = (char)tolower(*temp_ptr); |
455 | *temp_ptr = tolower(*temp_ptr); | ||
456 | } | 312 | } |
457 | for (temp_ptr = stateupper; *temp_ptr; temp_ptr++) { | 313 | for (char *temp_ptr = stateupper; *temp_ptr; temp_ptr++) { |
458 | *temp_ptr = toupper(*temp_ptr); | 314 | *temp_ptr = (char)toupper(*temp_ptr); |
459 | } | 315 | } |
460 | snprintf(statenum, 2, "%i", i); | 316 | snprintf(statenum, 2, "%i", i); |
461 | 317 | ||
462 | /* Base test names, we'll append the state string */ | 318 | /* Base test names, we'll append the state string */ |
463 | char testname[64] = "Translate state string: "; | 319 | char testname[64] = "Translate state string: "; |
464 | int tlen = strlen(testname); | 320 | size_t tlen = strlen(testname); |
465 | 321 | ||
466 | strcpy(testname + tlen, states[i]); | 322 | strcpy(testname + tlen, states[i]); |
467 | ok(i == mp_translate_state(states[i]), testname); | 323 | ok(i == mp_translate_state(states[i]), testname); |
diff --git a/lib/thresholds.c b/lib/thresholds.c new file mode 100644 index 00000000..de2b9315 --- /dev/null +++ b/lib/thresholds.c | |||
@@ -0,0 +1,71 @@ | |||
1 | #include "./thresholds.h" | ||
2 | #include "./utils_base.h" | ||
3 | #include "perfdata.h" | ||
4 | |||
5 | #include <stddef.h> | ||
6 | |||
7 | mp_thresholds mp_thresholds_init() { | ||
8 | mp_thresholds tmp = { | ||
9 | .critical = {}, | ||
10 | .critical_is_set = false, | ||
11 | .warning = {}, | ||
12 | .warning_is_set = false, | ||
13 | }; | ||
14 | return tmp; | ||
15 | } | ||
16 | |||
17 | char *fmt_threshold_warning(const thresholds th) { | ||
18 | if (th.warning == NULL) { | ||
19 | return ""; | ||
20 | } | ||
21 | |||
22 | return fmt_range(*th.warning); | ||
23 | } | ||
24 | |||
25 | char *fmt_threshold_critical(const thresholds th) { | ||
26 | if (th.critical == NULL) { | ||
27 | return ""; | ||
28 | } | ||
29 | return fmt_range(*th.critical); | ||
30 | } | ||
31 | |||
32 | mp_perfdata mp_pd_set_thresholds(mp_perfdata perfdata, mp_thresholds threshold) { | ||
33 | if (threshold.critical_is_set) { | ||
34 | perfdata.crit = threshold.critical; | ||
35 | perfdata.crit_present = true; | ||
36 | } | ||
37 | |||
38 | if (threshold.warning_is_set) { | ||
39 | perfdata.warn = threshold.warning; | ||
40 | perfdata.warn_present = true; | ||
41 | } | ||
42 | |||
43 | return perfdata; | ||
44 | } | ||
45 | |||
46 | mp_state_enum mp_get_pd_status(mp_perfdata perfdata) { | ||
47 | if (perfdata.crit_present) { | ||
48 | if (mp_check_range(perfdata.value, perfdata.crit)) { | ||
49 | return STATE_CRITICAL; | ||
50 | } | ||
51 | } | ||
52 | if (perfdata.warn_present) { | ||
53 | if (mp_check_range(perfdata.value, perfdata.warn)) { | ||
54 | return STATE_WARNING; | ||
55 | } | ||
56 | } | ||
57 | |||
58 | return STATE_OK; | ||
59 | } | ||
60 | |||
61 | mp_thresholds mp_thresholds_set_warn(mp_thresholds thlds, mp_range warn) { | ||
62 | thlds.warning = warn; | ||
63 | thlds.warning_is_set = true; | ||
64 | return thlds; | ||
65 | } | ||
66 | |||
67 | mp_thresholds mp_thresholds_set_crit(mp_thresholds thlds, mp_range crit) { | ||
68 | thlds.critical = crit; | ||
69 | thlds.critical_is_set = true; | ||
70 | return thlds; | ||
71 | } | ||
diff --git a/lib/thresholds.h b/lib/thresholds.h new file mode 100644 index 00000000..f8647681 --- /dev/null +++ b/lib/thresholds.h | |||
@@ -0,0 +1,31 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "./perfdata.h" | ||
4 | #include "states.h" | ||
5 | |||
6 | /* | ||
7 | * Old threshold type using the old range type | ||
8 | */ | ||
9 | typedef struct { | ||
10 | range *warning; | ||
11 | range *critical; | ||
12 | } thresholds; | ||
13 | |||
14 | typedef struct { | ||
15 | bool warning_is_set; | ||
16 | mp_range warning; | ||
17 | bool critical_is_set; | ||
18 | mp_range critical; | ||
19 | } mp_thresholds; | ||
20 | |||
21 | mp_thresholds mp_thresholds_init(void); | ||
22 | |||
23 | mp_perfdata mp_pd_set_thresholds(mp_perfdata /* pd */, mp_thresholds /* th */); | ||
24 | |||
25 | mp_state_enum mp_get_pd_status(mp_perfdata /* pd */); | ||
26 | |||
27 | mp_thresholds mp_thresholds_set_warn(mp_thresholds thlds, mp_range warn); | ||
28 | mp_thresholds mp_thresholds_set_crit(mp_thresholds thlds, mp_range crit); | ||
29 | |||
30 | char *fmt_threshold_warning(thresholds th); | ||
31 | char *fmt_threshold_critical(thresholds th); | ||
diff --git a/lib/utils_base.c b/lib/utils_base.c index 90a4aaa5..28e6dc47 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
@@ -25,6 +25,7 @@ | |||
25 | *****************************************************************************/ | 25 | *****************************************************************************/ |
26 | 26 | ||
27 | #include "../plugins/common.h" | 27 | #include "../plugins/common.h" |
28 | #include "states.h" | ||
28 | #include <stdarg.h> | 29 | #include <stdarg.h> |
29 | #include "utils_base.h" | 30 | #include "utils_base.h" |
30 | #include <ctype.h> | 31 | #include <ctype.h> |
@@ -33,20 +34,20 @@ | |||
33 | #include <unistd.h> | 34 | #include <unistd.h> |
34 | #include <sys/types.h> | 35 | #include <sys/types.h> |
35 | 36 | ||
36 | #define np_free(ptr) \ | 37 | #define np_free(ptr) \ |
37 | { \ | 38 | { \ |
38 | if (ptr) { \ | 39 | if (ptr) { \ |
39 | free(ptr); \ | 40 | free(ptr); \ |
40 | ptr = NULL; \ | 41 | ptr = NULL; \ |
41 | } \ | 42 | } \ |
42 | } | 43 | } |
43 | 44 | ||
44 | monitoring_plugin *this_monitoring_plugin = NULL; | 45 | monitoring_plugin *this_monitoring_plugin = NULL; |
45 | 46 | ||
46 | int timeout_state = STATE_CRITICAL; | 47 | mp_state_enum timeout_state = STATE_CRITICAL; |
47 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; | 48 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; |
48 | 49 | ||
49 | bool _np_state_read_file(FILE *); | 50 | bool _np_state_read_file(FILE *state_file); |
50 | 51 | ||
51 | void np_init(char *plugin_name, int argc, char **argv) { | 52 | void np_init(char *plugin_name, int argc, char **argv) { |
52 | if (this_monitoring_plugin == NULL) { | 53 | if (this_monitoring_plugin == NULL) { |
@@ -55,31 +56,25 @@ void np_init(char *plugin_name, int argc, char **argv) { | |||
55 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | 56 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); |
56 | } | 57 | } |
57 | this_monitoring_plugin->plugin_name = strdup(plugin_name); | 58 | this_monitoring_plugin->plugin_name = strdup(plugin_name); |
58 | if (this_monitoring_plugin->plugin_name == NULL) | 59 | if (this_monitoring_plugin->plugin_name == NULL) { |
59 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | 60 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); |
61 | } | ||
60 | this_monitoring_plugin->argc = argc; | 62 | this_monitoring_plugin->argc = argc; |
61 | this_monitoring_plugin->argv = argv; | 63 | this_monitoring_plugin->argv = argv; |
62 | } | 64 | } |
63 | } | 65 | } |
64 | 66 | ||
65 | void np_set_args(int argc, char **argv) { | 67 | void np_set_args(int argc, char **argv) { |
66 | if (this_monitoring_plugin == NULL) | 68 | if (this_monitoring_plugin == NULL) { |
67 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | 69 | die(STATE_UNKNOWN, _("This requires np_init to be called")); |
70 | } | ||
68 | 71 | ||
69 | this_monitoring_plugin->argc = argc; | 72 | this_monitoring_plugin->argc = argc; |
70 | this_monitoring_plugin->argv = argv; | 73 | this_monitoring_plugin->argv = argv; |
71 | } | 74 | } |
72 | 75 | ||
73 | void np_cleanup() { | 76 | void np_cleanup(void) { |
74 | if (this_monitoring_plugin != NULL) { | 77 | if (this_monitoring_plugin != NULL) { |
75 | if (this_monitoring_plugin->state != NULL) { | ||
76 | if (this_monitoring_plugin->state->state_data) { | ||
77 | np_free(this_monitoring_plugin->state->state_data->data); | ||
78 | np_free(this_monitoring_plugin->state->state_data); | ||
79 | } | ||
80 | np_free(this_monitoring_plugin->state->name); | ||
81 | np_free(this_monitoring_plugin->state); | ||
82 | } | ||
83 | np_free(this_monitoring_plugin->plugin_name); | 78 | np_free(this_monitoring_plugin->plugin_name); |
84 | np_free(this_monitoring_plugin); | 79 | np_free(this_monitoring_plugin); |
85 | } | 80 | } |
@@ -151,7 +146,8 @@ range *parse_range_string(char *str) { | |||
151 | set_range_end(temp_range, end); | 146 | set_range_end(temp_range, end); |
152 | } | 147 | } |
153 | 148 | ||
154 | if (temp_range->start_infinity == true || temp_range->end_infinity == true || temp_range->start <= temp_range->end) { | 149 | if (temp_range->start_infinity || temp_range->end_infinity || |
150 | temp_range->start <= temp_range->end) { | ||
155 | return temp_range; | 151 | return temp_range; |
156 | } | 152 | } |
157 | free(temp_range); | 153 | free(temp_range); |
@@ -162,8 +158,9 @@ range *parse_range_string(char *str) { | |||
162 | int _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string) { | 158 | int _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string) { |
163 | thresholds *temp_thresholds = NULL; | 159 | thresholds *temp_thresholds = NULL; |
164 | 160 | ||
165 | if ((temp_thresholds = calloc(1, sizeof(thresholds))) == NULL) | 161 | if ((temp_thresholds = calloc(1, sizeof(thresholds))) == NULL) { |
166 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | 162 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); |
163 | } | ||
167 | 164 | ||
168 | temp_thresholds->warning = NULL; | 165 | temp_thresholds->warning = NULL; |
169 | temp_thresholds->critical = NULL; | 166 | temp_thresholds->critical = NULL; |
@@ -202,12 +199,14 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) { | |||
202 | printf("Threshold not set"); | 199 | printf("Threshold not set"); |
203 | } else { | 200 | } else { |
204 | if (my_threshold->warning) { | 201 | if (my_threshold->warning) { |
205 | printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end); | 202 | printf("Warning: start=%g end=%g; ", my_threshold->warning->start, |
203 | my_threshold->warning->end); | ||
206 | } else { | 204 | } else { |
207 | printf("Warning not set; "); | 205 | printf("Warning not set; "); |
208 | } | 206 | } |
209 | if (my_threshold->critical) { | 207 | if (my_threshold->critical) { |
210 | printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end); | 208 | printf("Critical: start=%g end=%g", my_threshold->critical->start, |
209 | my_threshold->critical->end); | ||
211 | } else { | 210 | } else { |
212 | printf("Critical not set"); | 211 | printf("Critical not set"); |
213 | } | 212 | } |
@@ -215,6 +214,36 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) { | |||
215 | printf("\n"); | 214 | printf("\n"); |
216 | } | 215 | } |
217 | 216 | ||
217 | /* Returns true if alert should be raised based on the range, false otherwise */ | ||
218 | bool mp_check_range(const mp_perfdata_value value, const mp_range my_range) { | ||
219 | bool is_inside = false; | ||
220 | |||
221 | if (!my_range.end_infinity && !my_range.start_infinity) { | ||
222 | // range: .........|---inside---|........... | ||
223 | // value | ||
224 | is_inside = ((cmp_perfdata_value(value, my_range.start) >= 0) && | ||
225 | (cmp_perfdata_value(value, my_range.end) <= 0)); | ||
226 | } else if (!my_range.start_infinity && my_range.end_infinity) { | ||
227 | // range: .........|---inside--------- | ||
228 | // value | ||
229 | is_inside = (cmp_perfdata_value(value, my_range.start) >= 0); | ||
230 | } else if (my_range.start_infinity && !my_range.end_infinity) { | ||
231 | // range: -inside--------|.................... | ||
232 | // value | ||
233 | is_inside = (cmp_perfdata_value(value, my_range.end) == -1); | ||
234 | } else { | ||
235 | // range from -inf to inf, so always inside | ||
236 | is_inside = true; | ||
237 | } | ||
238 | |||
239 | if ((is_inside && my_range.alert_on_inside_range == INSIDE) || | ||
240 | (!is_inside && my_range.alert_on_inside_range == OUTSIDE)) { | ||
241 | return true; | ||
242 | } | ||
243 | |||
244 | return false; | ||
245 | } | ||
246 | |||
218 | /* Returns true if alert should be raised based on the range */ | 247 | /* Returns true if alert should be raised based on the range */ |
219 | bool check_range(double value, range *my_range) { | 248 | bool check_range(double value, range *my_range) { |
220 | bool no = false; | 249 | bool no = false; |
@@ -225,38 +254,38 @@ bool check_range(double value, range *my_range) { | |||
225 | yes = false; | 254 | yes = false; |
226 | } | 255 | } |
227 | 256 | ||
228 | if (my_range->end_infinity == false && my_range->start_infinity == false) { | 257 | if (!my_range->end_infinity && !my_range->start_infinity) { |
229 | if ((my_range->start <= value) && (value <= my_range->end)) { | 258 | if ((my_range->start <= value) && (value <= my_range->end)) { |
230 | return no; | 259 | return no; |
231 | } else { | ||
232 | return yes; | ||
233 | } | 260 | } |
234 | } else if (my_range->start_infinity == false && my_range->end_infinity == true) { | 261 | return yes; |
262 | } | ||
263 | |||
264 | if (!my_range->start_infinity && my_range->end_infinity) { | ||
235 | if (my_range->start <= value) { | 265 | if (my_range->start <= value) { |
236 | return no; | 266 | return no; |
237 | } else { | ||
238 | return yes; | ||
239 | } | 267 | } |
240 | } else if (my_range->start_infinity == true && my_range->end_infinity == false) { | 268 | return yes; |
269 | } | ||
270 | |||
271 | if (my_range->start_infinity && !my_range->end_infinity) { | ||
241 | if (value <= my_range->end) { | 272 | if (value <= my_range->end) { |
242 | return no; | 273 | return no; |
243 | } else { | ||
244 | return yes; | ||
245 | } | 274 | } |
246 | } else { | 275 | return yes; |
247 | return no; | ||
248 | } | 276 | } |
277 | return no; | ||
249 | } | 278 | } |
250 | 279 | ||
251 | /* Returns status */ | 280 | /* Returns status */ |
252 | int get_status(double value, thresholds *my_thresholds) { | 281 | mp_state_enum get_status(double value, thresholds *my_thresholds) { |
253 | if (my_thresholds->critical != NULL) { | 282 | if (my_thresholds->critical != NULL) { |
254 | if (check_range(value, my_thresholds->critical) == true) { | 283 | if (check_range(value, my_thresholds->critical)) { |
255 | return STATE_CRITICAL; | 284 | return STATE_CRITICAL; |
256 | } | 285 | } |
257 | } | 286 | } |
258 | if (my_thresholds->warning != NULL) { | 287 | if (my_thresholds->warning != NULL) { |
259 | if (check_range(value, my_thresholds->warning) == true) { | 288 | if (check_range(value, my_thresholds->warning)) { |
260 | return STATE_WARNING; | 289 | return STATE_WARNING; |
261 | } | 290 | } |
262 | } | 291 | } |
@@ -265,31 +294,31 @@ int get_status(double value, thresholds *my_thresholds) { | |||
265 | 294 | ||
266 | char *np_escaped_string(const char *string) { | 295 | char *np_escaped_string(const char *string) { |
267 | char *data; | 296 | char *data; |
268 | int i, j = 0; | 297 | int write_index = 0; |
269 | data = strdup(string); | 298 | data = strdup(string); |
270 | for (i = 0; data[i]; i++) { | 299 | for (int i = 0; data[i]; i++) { |
271 | if (data[i] == '\\') { | 300 | if (data[i] == '\\') { |
272 | switch (data[++i]) { | 301 | switch (data[++i]) { |
273 | case 'n': | 302 | case 'n': |
274 | data[j++] = '\n'; | 303 | data[write_index++] = '\n'; |
275 | break; | 304 | break; |
276 | case 'r': | 305 | case 'r': |
277 | data[j++] = '\r'; | 306 | data[write_index++] = '\r'; |
278 | break; | 307 | break; |
279 | case 't': | 308 | case 't': |
280 | data[j++] = '\t'; | 309 | data[write_index++] = '\t'; |
281 | break; | 310 | break; |
282 | case '\\': | 311 | case '\\': |
283 | data[j++] = '\\'; | 312 | data[write_index++] = '\\'; |
284 | break; | 313 | break; |
285 | default: | 314 | default: |
286 | data[j++] = data[i]; | 315 | data[write_index++] = data[i]; |
287 | } | 316 | } |
288 | } else { | 317 | } else { |
289 | data[j++] = data[i]; | 318 | data[write_index++] = data[i]; |
290 | } | 319 | } |
291 | } | 320 | } |
292 | data[j] = '\0'; | 321 | data[write_index] = '\0'; |
293 | return data; | 322 | return data; |
294 | } | 323 | } |
295 | 324 | ||
@@ -302,38 +331,43 @@ int np_check_if_root(void) { return (geteuid() == 0); } | |||
302 | * data strings. | 331 | * data strings. |
303 | */ | 332 | */ |
304 | char *np_extract_value(const char *varlist, const char *name, char sep) { | 333 | char *np_extract_value(const char *varlist, const char *name, char sep) { |
305 | char *tmp = NULL, *value = NULL; | 334 | char *tmp = NULL; |
306 | int i; | 335 | char *value = NULL; |
307 | 336 | ||
308 | while (1) { | 337 | while (true) { |
309 | /* Strip any leading space */ | 338 | /* Strip any leading space */ |
310 | for (; isspace(varlist[0]); varlist++) | 339 | for (; isspace(varlist[0]); varlist++) { |
311 | ; | 340 | ; |
341 | } | ||
312 | 342 | ||
313 | if (strncmp(name, varlist, strlen(name)) == 0) { | 343 | if (strncmp(name, varlist, strlen(name)) == 0) { |
314 | varlist += strlen(name); | 344 | varlist += strlen(name); |
315 | /* strip trailing spaces */ | 345 | /* strip trailing spaces */ |
316 | for (; isspace(varlist[0]); varlist++) | 346 | for (; isspace(varlist[0]); varlist++) { |
317 | ; | 347 | ; |
348 | } | ||
318 | 349 | ||
319 | if (varlist[0] == '=') { | 350 | if (varlist[0] == '=') { |
320 | /* We matched the key, go past the = sign */ | 351 | /* We matched the key, go past the = sign */ |
321 | varlist++; | 352 | varlist++; |
322 | /* strip leading spaces */ | 353 | /* strip leading spaces */ |
323 | for (; isspace(varlist[0]); varlist++) | 354 | for (; isspace(varlist[0]); varlist++) { |
324 | ; | 355 | ; |
356 | } | ||
325 | 357 | ||
326 | if ((tmp = index(varlist, sep))) { | 358 | if ((tmp = index(varlist, sep))) { |
327 | /* Value is delimited by a comma */ | 359 | /* Value is delimited by a comma */ |
328 | if (tmp - varlist == 0) | 360 | if (tmp - varlist == 0) { |
329 | continue; | 361 | continue; |
330 | value = (char *)calloc(1, tmp - varlist + 1); | 362 | } |
331 | strncpy(value, varlist, tmp - varlist); | 363 | value = (char *)calloc(1, (unsigned long)(tmp - varlist + 1)); |
364 | strncpy(value, varlist, (unsigned long)(tmp - varlist)); | ||
332 | value[tmp - varlist] = '\0'; | 365 | value[tmp - varlist] = '\0'; |
333 | } else { | 366 | } else { |
334 | /* Value is delimited by a \0 */ | 367 | /* Value is delimited by a \0 */ |
335 | if (strlen(varlist) == 0) | 368 | if (strlen(varlist) == 0) { |
336 | continue; | 369 | continue; |
370 | } | ||
337 | value = (char *)calloc(1, strlen(varlist) + 1); | 371 | value = (char *)calloc(1, strlen(varlist) + 1); |
338 | strncpy(value, varlist, strlen(varlist)); | 372 | strncpy(value, varlist, strlen(varlist)); |
339 | value[strlen(varlist)] = '\0'; | 373 | value[strlen(varlist)] = '\0'; |
@@ -351,14 +385,16 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { | |||
351 | } | 385 | } |
352 | 386 | ||
353 | /* Clean-up trailing spaces/newlines */ | 387 | /* Clean-up trailing spaces/newlines */ |
354 | if (value) | 388 | if (value) { |
355 | for (i = strlen(value) - 1; isspace(value[i]); i--) | 389 | for (unsigned long i = strlen(value) - 1; isspace(value[i]); i--) { |
356 | value[i] = '\0'; | 390 | value[i] = '\0'; |
391 | } | ||
392 | } | ||
357 | 393 | ||
358 | return value; | 394 | return value; |
359 | } | 395 | } |
360 | 396 | ||
361 | const char *state_text(int result) { | 397 | const char *state_text(mp_state_enum result) { |
362 | switch (result) { | 398 | switch (result) { |
363 | case STATE_OK: | 399 | case STATE_OK: |
364 | return "OK"; | 400 | return "OK"; |
@@ -378,343 +414,17 @@ const char *state_text(int result) { | |||
378 | * return the corresponding STATE_ value or ERROR) | 414 | * return the corresponding STATE_ value or ERROR) |
379 | */ | 415 | */ |
380 | int mp_translate_state(char *state_text) { | 416 | int mp_translate_state(char *state_text) { |
381 | if (!strcasecmp(state_text, "OK") || !strcmp(state_text, "0")) | 417 | if (!strcasecmp(state_text, "OK") || !strcmp(state_text, "0")) { |
382 | return STATE_OK; | 418 | return STATE_OK; |
383 | if (!strcasecmp(state_text, "WARNING") || !strcmp(state_text, "1")) | ||
384 | return STATE_WARNING; | ||
385 | if (!strcasecmp(state_text, "CRITICAL") || !strcmp(state_text, "2")) | ||
386 | return STATE_CRITICAL; | ||
387 | if (!strcasecmp(state_text, "UNKNOWN") || !strcmp(state_text, "3")) | ||
388 | return STATE_UNKNOWN; | ||
389 | return ERROR; | ||
390 | } | ||
391 | |||
392 | /* | ||
393 | * Returns a string to use as a keyname, based on an md5 hash of argv, thus | ||
394 | * hopefully a unique key per service/plugin invocation. Use the extra-opts | ||
395 | * parse of argv, so that uniqueness in parameters are reflected there. | ||
396 | */ | ||
397 | char *_np_state_generate_key() { | ||
398 | int i; | ||
399 | char **argv = this_monitoring_plugin->argv; | ||
400 | char keyname[41]; | ||
401 | char *p = NULL; | ||
402 | |||
403 | unsigned char result[256]; | ||
404 | |||
405 | #ifdef USE_OPENSSL | ||
406 | /* | ||
407 | * This code path is chosen if openssl is available (which should be the most common | ||
408 | * scenario). Alternatively, the gnulib implementation/ | ||
409 | * | ||
410 | */ | ||
411 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | ||
412 | |||
413 | EVP_DigestInit(ctx, EVP_sha256()); | ||
414 | |||
415 | for (i = 0; i < this_monitoring_plugin->argc; i++) { | ||
416 | EVP_DigestUpdate(ctx, argv[i], strlen(argv[i])); | ||
417 | } | ||
418 | |||
419 | EVP_DigestFinal(ctx, result, NULL); | ||
420 | #else | ||
421 | |||
422 | struct sha256_ctx ctx; | ||
423 | |||
424 | for (i = 0; i < this_monitoring_plugin->argc; i++) { | ||
425 | sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); | ||
426 | } | ||
427 | |||
428 | sha256_finish_ctx(&ctx, result); | ||
429 | #endif // FOUNDOPENSSL | ||
430 | |||
431 | for (i = 0; i < 20; ++i) { | ||
432 | sprintf(&keyname[2 * i], "%02x", result[i]); | ||
433 | } | ||
434 | |||
435 | keyname[40] = '\0'; | ||
436 | |||
437 | p = strdup(keyname); | ||
438 | if (p == NULL) { | ||
439 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
440 | } | ||
441 | return p; | ||
442 | } | ||
443 | |||
444 | void _cleanup_state_data() { | ||
445 | if (this_monitoring_plugin->state->state_data != NULL) { | ||
446 | np_free(this_monitoring_plugin->state->state_data->data); | ||
447 | np_free(this_monitoring_plugin->state->state_data); | ||
448 | } | ||
449 | } | ||
450 | |||
451 | /* | ||
452 | * Internal function. Returns either: | ||
453 | * envvar NAGIOS_PLUGIN_STATE_DIRECTORY | ||
454 | * statically compiled shared state directory | ||
455 | */ | ||
456 | char *_np_state_calculate_location_prefix() { | ||
457 | char *env_dir; | ||
458 | |||
459 | /* Do not allow passing MP_STATE_PATH in setuid plugins | ||
460 | * for security reasons */ | ||
461 | if (!mp_suid()) { | ||
462 | env_dir = getenv("MP_STATE_PATH"); | ||
463 | if (env_dir && env_dir[0] != '\0') | ||
464 | return env_dir; | ||
465 | /* This is the former ENV, for backward-compatibility */ | ||
466 | env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); | ||
467 | if (env_dir && env_dir[0] != '\0') | ||
468 | return env_dir; | ||
469 | } | ||
470 | |||
471 | return NP_STATE_DIR_PREFIX; | ||
472 | } | ||
473 | |||
474 | /* | ||
475 | * Initiatializer for state routines. | ||
476 | * Sets variables. Generates filename. Returns np_state_key. die with | ||
477 | * UNKNOWN if exception | ||
478 | */ | ||
479 | void np_enable_state(char *keyname, int expected_data_version) { | ||
480 | state_key *this_state = NULL; | ||
481 | char *temp_filename = NULL; | ||
482 | char *temp_keyname = NULL; | ||
483 | char *p = NULL; | ||
484 | int ret; | ||
485 | |||
486 | if (this_monitoring_plugin == NULL) | ||
487 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | ||
488 | |||
489 | this_state = (state_key *)calloc(1, sizeof(state_key)); | ||
490 | if (this_state == NULL) | ||
491 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
492 | |||
493 | if (keyname == NULL) { | ||
494 | temp_keyname = _np_state_generate_key(); | ||
495 | } else { | ||
496 | temp_keyname = strdup(keyname); | ||
497 | if (temp_keyname == NULL) | ||
498 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
499 | } | 419 | } |
500 | /* Die if invalid characters used for keyname */ | 420 | if (!strcasecmp(state_text, "WARNING") || !strcmp(state_text, "1")) { |
501 | p = temp_keyname; | 421 | return STATE_WARNING; |
502 | while (*p != '\0') { | ||
503 | if (!(isalnum(*p) || *p == '_')) { | ||
504 | die(STATE_UNKNOWN, _("Invalid character for keyname - only alphanumerics or '_'")); | ||
505 | } | ||
506 | p++; | ||
507 | } | ||
508 | this_state->name = temp_keyname; | ||
509 | this_state->plugin_name = this_monitoring_plugin->plugin_name; | ||
510 | this_state->data_version = expected_data_version; | ||
511 | this_state->state_data = NULL; | ||
512 | |||
513 | /* Calculate filename */ | ||
514 | ret = asprintf(&temp_filename, "%s/%lu/%s/%s", _np_state_calculate_location_prefix(), (unsigned long)geteuid(), | ||
515 | this_monitoring_plugin->plugin_name, this_state->name); | ||
516 | if (ret < 0) | ||
517 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
518 | |||
519 | this_state->_filename = temp_filename; | ||
520 | |||
521 | this_monitoring_plugin->state = this_state; | ||
522 | } | ||
523 | |||
524 | /* | ||
525 | * Will return NULL if no data is available (first run). If key currently | ||
526 | * exists, read data. If state file format version is not expected, return | ||
527 | * as if no data. Get state data version number and compares to expected. | ||
528 | * If numerically lower, then return as no previous state. die with UNKNOWN | ||
529 | * if exceptional error. | ||
530 | */ | ||
531 | state_data *np_state_read() { | ||
532 | state_data *this_state_data = NULL; | ||
533 | FILE *statefile; | ||
534 | bool rc = false; | ||
535 | |||
536 | if (this_monitoring_plugin == NULL) | ||
537 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | ||
538 | |||
539 | /* Open file. If this fails, no previous state found */ | ||
540 | statefile = fopen(this_monitoring_plugin->state->_filename, "r"); | ||
541 | if (statefile != NULL) { | ||
542 | |||
543 | this_state_data = (state_data *)calloc(1, sizeof(state_data)); | ||
544 | if (this_state_data == NULL) | ||
545 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
546 | |||
547 | this_state_data->data = NULL; | ||
548 | this_monitoring_plugin->state->state_data = this_state_data; | ||
549 | |||
550 | rc = _np_state_read_file(statefile); | ||
551 | |||
552 | fclose(statefile); | ||
553 | } | ||
554 | |||
555 | if (!rc) { | ||
556 | _cleanup_state_data(); | ||
557 | } | ||
558 | |||
559 | return this_monitoring_plugin->state->state_data; | ||
560 | } | ||
561 | |||
562 | /* | ||
563 | * Read the state file | ||
564 | */ | ||
565 | bool _np_state_read_file(FILE *f) { | ||
566 | bool status = false; | ||
567 | size_t pos; | ||
568 | char *line; | ||
569 | int i; | ||
570 | int failure = 0; | ||
571 | time_t current_time, data_time; | ||
572 | enum { | ||
573 | STATE_FILE_VERSION, | ||
574 | STATE_DATA_VERSION, | ||
575 | STATE_DATA_TIME, | ||
576 | STATE_DATA_TEXT, | ||
577 | STATE_DATA_END | ||
578 | } expected = STATE_FILE_VERSION; | ||
579 | |||
580 | time(¤t_time); | ||
581 | |||
582 | /* Note: This introduces a limit of 1024 bytes in the string data */ | ||
583 | line = (char *)calloc(1, 1024); | ||
584 | if (line == NULL) | ||
585 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
586 | |||
587 | while (!failure && (fgets(line, 1024, f)) != NULL) { | ||
588 | pos = strlen(line); | ||
589 | if (line[pos - 1] == '\n') { | ||
590 | line[pos - 1] = '\0'; | ||
591 | } | ||
592 | |||
593 | if (line[0] == '#') | ||
594 | continue; | ||
595 | |||
596 | switch (expected) { | ||
597 | case STATE_FILE_VERSION: | ||
598 | i = atoi(line); | ||
599 | if (i != NP_STATE_FORMAT_VERSION) | ||
600 | failure++; | ||
601 | else | ||
602 | expected = STATE_DATA_VERSION; | ||
603 | break; | ||
604 | case STATE_DATA_VERSION: | ||
605 | i = atoi(line); | ||
606 | if (i != this_monitoring_plugin->state->data_version) | ||
607 | failure++; | ||
608 | else | ||
609 | expected = STATE_DATA_TIME; | ||
610 | break; | ||
611 | case STATE_DATA_TIME: | ||
612 | /* If time > now, error */ | ||
613 | data_time = strtoul(line, NULL, 10); | ||
614 | if (data_time > current_time) | ||
615 | failure++; | ||
616 | else { | ||
617 | this_monitoring_plugin->state->state_data->time = data_time; | ||
618 | expected = STATE_DATA_TEXT; | ||
619 | } | ||
620 | break; | ||
621 | case STATE_DATA_TEXT: | ||
622 | this_monitoring_plugin->state->state_data->data = strdup(line); | ||
623 | if (this_monitoring_plugin->state->state_data->data == NULL) | ||
624 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
625 | expected = STATE_DATA_END; | ||
626 | status = true; | ||
627 | break; | ||
628 | case STATE_DATA_END:; | ||
629 | } | ||
630 | } | ||
631 | |||
632 | np_free(line); | ||
633 | return status; | ||
634 | } | ||
635 | |||
636 | /* | ||
637 | * If time=NULL, use current time. Create state file, with state format | ||
638 | * version, default text. Writes version, time, and data. Avoid locking | ||
639 | * problems - use mv to write and then swap. Possible loss of state data if | ||
640 | * two things writing to same key at same time. | ||
641 | * Will die with UNKNOWN if errors | ||
642 | */ | ||
643 | void np_state_write_string(time_t data_time, char *data_string) { | ||
644 | FILE *fp; | ||
645 | char *temp_file = NULL; | ||
646 | int fd = 0, result = 0; | ||
647 | time_t current_time; | ||
648 | char *directories = NULL; | ||
649 | char *p = NULL; | ||
650 | |||
651 | if (data_time == 0) | ||
652 | time(¤t_time); | ||
653 | else | ||
654 | current_time = data_time; | ||
655 | |||
656 | /* If file doesn't currently exist, create directories */ | ||
657 | if (access(this_monitoring_plugin->state->_filename, F_OK) != 0) { | ||
658 | result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); | ||
659 | if (result < 0) | ||
660 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
661 | |||
662 | for (p = directories + 1; *p; p++) { | ||
663 | if (*p == '/') { | ||
664 | *p = '\0'; | ||
665 | if ((access(directories, F_OK) != 0) && (mkdir(directories, S_IRWXU) != 0)) { | ||
666 | /* Can't free this! Otherwise error message is wrong! */ | ||
667 | /* np_free(directories); */ | ||
668 | die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories); | ||
669 | } | ||
670 | *p = '/'; | ||
671 | } | ||
672 | } | ||
673 | np_free(directories); | ||
674 | } | ||
675 | |||
676 | result = asprintf(&temp_file, "%s.XXXXXX", this_monitoring_plugin->state->_filename); | ||
677 | if (result < 0) | ||
678 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
679 | |||
680 | if ((fd = mkstemp(temp_file)) == -1) { | ||
681 | np_free(temp_file); | ||
682 | die(STATE_UNKNOWN, _("Cannot create temporary filename")); | ||
683 | } | ||
684 | |||
685 | fp = (FILE *)fdopen(fd, "w"); | ||
686 | if (fp == NULL) { | ||
687 | close(fd); | ||
688 | unlink(temp_file); | ||
689 | np_free(temp_file); | ||
690 | die(STATE_UNKNOWN, _("Unable to open temporary state file")); | ||
691 | } | 422 | } |
692 | 423 | if (!strcasecmp(state_text, "CRITICAL") || !strcmp(state_text, "2")) { | |
693 | fprintf(fp, "# NP State file\n"); | 424 | return STATE_CRITICAL; |
694 | fprintf(fp, "%d\n", NP_STATE_FORMAT_VERSION); | ||
695 | fprintf(fp, "%d\n", this_monitoring_plugin->state->data_version); | ||
696 | fprintf(fp, "%lu\n", current_time); | ||
697 | fprintf(fp, "%s\n", data_string); | ||
698 | |||
699 | fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP); | ||
700 | |||
701 | fflush(fp); | ||
702 | |||
703 | result = fclose(fp); | ||
704 | |||
705 | fsync(fd); | ||
706 | |||
707 | if (result != 0) { | ||
708 | unlink(temp_file); | ||
709 | np_free(temp_file); | ||
710 | die(STATE_UNKNOWN, _("Error writing temp file")); | ||
711 | } | 425 | } |
712 | 426 | if (!strcasecmp(state_text, "UNKNOWN") || !strcmp(state_text, "3")) { | |
713 | if (rename(temp_file, this_monitoring_plugin->state->_filename) != 0) { | 427 | return STATE_UNKNOWN; |
714 | unlink(temp_file); | ||
715 | np_free(temp_file); | ||
716 | die(STATE_UNKNOWN, _("Cannot rename state temp file")); | ||
717 | } | 428 | } |
718 | 429 | return ERROR; | |
719 | np_free(temp_file); | ||
720 | } | 430 | } |
diff --git a/lib/utils_base.h b/lib/utils_base.h index a209cb6d..27884bf0 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h | |||
@@ -2,6 +2,13 @@ | |||
2 | #define _UTILS_BASE_ | 2 | #define _UTILS_BASE_ |
3 | /* Header file for Monitoring Plugins utils_base.c */ | 3 | /* Header file for Monitoring Plugins utils_base.c */ |
4 | 4 | ||
5 | #include "../config.h" | ||
6 | #include <time.h> | ||
7 | |||
8 | #include "./perfdata.h" | ||
9 | #include "./thresholds.h" | ||
10 | #include "states.h" | ||
11 | |||
5 | #ifndef USE_OPENSSL | 12 | #ifndef USE_OPENSSL |
6 | # include "sha256.h" | 13 | # include "sha256.h" |
7 | #endif | 14 | #endif |
@@ -19,39 +26,8 @@ | |||
19 | #define OUTSIDE 0 | 26 | #define OUTSIDE 0 |
20 | #define INSIDE 1 | 27 | #define INSIDE 1 |
21 | 28 | ||
22 | typedef struct range_struct { | ||
23 | double start; | ||
24 | bool start_infinity; | ||
25 | double end; | ||
26 | int end_infinity; | ||
27 | int alert_on; /* OUTSIDE (default) or INSIDE */ | ||
28 | char *text; /* original unparsed text input */ | ||
29 | } range; | ||
30 | |||
31 | typedef struct thresholds_struct { | ||
32 | range *warning; | ||
33 | range *critical; | ||
34 | } thresholds; | ||
35 | |||
36 | #define NP_STATE_FORMAT_VERSION 1 | ||
37 | |||
38 | typedef struct state_data_struct { | ||
39 | time_t time; | ||
40 | void *data; | ||
41 | int length; /* Of binary data */ | ||
42 | } state_data; | ||
43 | |||
44 | typedef struct state_key_struct { | ||
45 | char *name; | ||
46 | char *plugin_name; | ||
47 | int data_version; | ||
48 | char *_filename; | ||
49 | state_data *state_data; | ||
50 | } state_key; | ||
51 | |||
52 | typedef struct np_struct { | 29 | typedef struct np_struct { |
53 | char *plugin_name; | 30 | char *plugin_name; |
54 | state_key *state; | ||
55 | int argc; | 31 | int argc; |
56 | char **argv; | 32 | char **argv; |
57 | } monitoring_plugin; | 33 | } monitoring_plugin; |
@@ -61,10 +37,11 @@ int _set_thresholds(thresholds **, char *, char *); | |||
61 | void set_thresholds(thresholds **, char *, char *); | 37 | void set_thresholds(thresholds **, char *, char *); |
62 | void print_thresholds(const char *, thresholds *); | 38 | void print_thresholds(const char *, thresholds *); |
63 | bool check_range(double, range *); | 39 | bool check_range(double, range *); |
64 | int get_status(double, thresholds *); | 40 | bool mp_check_range(mp_perfdata_value, mp_range); |
41 | mp_state_enum get_status(double, thresholds *); | ||
65 | 42 | ||
66 | /* Handle timeouts */ | 43 | /* Handle timeouts */ |
67 | extern int timeout_state; | 44 | extern mp_state_enum timeout_state; |
68 | extern unsigned int timeout_interval; | 45 | extern unsigned int timeout_interval; |
69 | 46 | ||
70 | /* All possible characters in a threshold range */ | 47 | /* All possible characters in a threshold range */ |
@@ -106,13 +83,9 @@ char *np_extract_value(const char *, const char *, char); | |||
106 | */ | 83 | */ |
107 | int mp_translate_state(char *); | 84 | int mp_translate_state(char *); |
108 | 85 | ||
109 | void np_enable_state(char *, int); | ||
110 | state_data *np_state_read(); | ||
111 | void np_state_write_string(time_t, char *); | ||
112 | |||
113 | void np_init(char *, int argc, char **argv); | 86 | void np_init(char *, int argc, char **argv); |
114 | void np_set_args(int argc, char **argv); | 87 | void np_set_args(int argc, char **argv); |
115 | void np_cleanup(); | 88 | void np_cleanup(void); |
116 | const char *state_text(int); | 89 | const char *state_text(mp_state_enum); |
117 | 90 | ||
118 | #endif /* _UTILS_BASE_ */ | 91 | #endif /* _UTILS_BASE_ */ |
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 18350ac0..35b83297 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c | |||
@@ -40,7 +40,6 @@ | |||
40 | 40 | ||
41 | /** includes **/ | 41 | /** includes **/ |
42 | #include "common.h" | 42 | #include "common.h" |
43 | #include "utils.h" | ||
44 | #include "utils_cmd.h" | 43 | #include "utils_cmd.h" |
45 | /* This variable must be global, since there's no way the caller | 44 | /* This variable must be global, since there's no way the caller |
46 | * can forcibly slay a dead or ungainly running program otherwise. | 45 | * can forcibly slay a dead or ungainly running program otherwise. |
@@ -62,16 +61,13 @@ static pid_t *_cmd_pids = NULL; | |||
62 | # include <sys/wait.h> | 61 | # include <sys/wait.h> |
63 | #endif | 62 | #endif |
64 | 63 | ||
65 | /* used in _cmd_open to pass the environment to commands */ | ||
66 | extern char **environ; | ||
67 | |||
68 | /** macros **/ | 64 | /** macros **/ |
69 | #ifndef WEXITSTATUS | 65 | #ifndef WEXITSTATUS |
70 | # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) | 66 | # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) |
71 | #endif | 67 | #endif |
72 | 68 | ||
73 | #ifndef WIFEXITED | 69 | #ifndef WIFEXITED |
74 | # define WIFEXITED(stat_val) (((stat_val)&255) == 0) | 70 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) |
75 | #endif | 71 | #endif |
76 | 72 | ||
77 | /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ | 73 | /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ |
@@ -80,14 +76,12 @@ extern char **environ; | |||
80 | #endif | 76 | #endif |
81 | 77 | ||
82 | /** prototypes **/ | 78 | /** prototypes **/ |
83 | static int _cmd_open(char *const *, int *, int *) __attribute__((__nonnull__(1, 2, 3))); | 79 | static int _cmd_open(char *const *argv, int *pfd, int *pfderr) |
84 | 80 | __attribute__((__nonnull__(1, 2, 3))); | |
85 | static int _cmd_fetch_output(int, output *, int) __attribute__((__nonnull__(2))); | ||
86 | 81 | ||
87 | static int _cmd_close(int); | 82 | static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) __attribute__((__nonnull__(2))); |
88 | 83 | ||
89 | /* prototype imported from utils.h */ | 84 | static int _cmd_close(int fileDescriptor); |
90 | extern void die(int, const char *, ...) __attribute__((__noreturn__, __format__(__printf__, 2, 3))); | ||
91 | 85 | ||
92 | /* this function is NOT async-safe. It is exported so multithreaded | 86 | /* this function is NOT async-safe. It is exported so multithreaded |
93 | * plugins (or other apps) can call it prior to running any commands | 87 | * plugins (or other apps) can call it prior to running any commands |
@@ -103,26 +97,29 @@ void cmd_init(void) { | |||
103 | maxfd = MAXFD_LIMIT; | 97 | maxfd = MAXFD_LIMIT; |
104 | } | 98 | } |
105 | 99 | ||
106 | if (!_cmd_pids) | 100 | if (!_cmd_pids) { |
107 | _cmd_pids = calloc(maxfd, sizeof(pid_t)); | 101 | _cmd_pids = calloc(maxfd, sizeof(pid_t)); |
102 | } | ||
108 | } | 103 | } |
109 | 104 | ||
110 | /* Start running a command, array style */ | 105 | /* Start running a command, array style */ |
111 | static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { | 106 | static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { |
112 | pid_t pid; | ||
113 | #ifdef RLIMIT_CORE | 107 | #ifdef RLIMIT_CORE |
114 | struct rlimit limit; | 108 | struct rlimit limit; |
115 | #endif | 109 | #endif |
116 | 110 | ||
117 | int i = 0; | 111 | int i = 0; |
118 | 112 | ||
119 | if (!_cmd_pids) | 113 | if (!_cmd_pids) { |
120 | CMD_INIT; | 114 | CMD_INIT; |
115 | } | ||
121 | 116 | ||
122 | setenv("LC_ALL", "C", 1); | 117 | setenv("LC_ALL", "C", 1); |
123 | 118 | ||
124 | if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) | 119 | pid_t pid; |
120 | if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) { | ||
125 | return -1; /* errno set by the failing function */ | 121 | return -1; /* errno set by the failing function */ |
122 | } | ||
126 | 123 | ||
127 | /* child runs exceve() and _exit. */ | 124 | /* child runs exceve() and _exit. */ |
128 | if (pid == 0) { | 125 | if (pid == 0) { |
@@ -147,9 +144,11 @@ static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { | |||
147 | * This is executed in a separate address space (pure child), | 144 | * This is executed in a separate address space (pure child), |
148 | * so we don't have to worry about async safety */ | 145 | * so we don't have to worry about async safety */ |
149 | long maxfd = mp_open_max(); | 146 | long maxfd = mp_open_max(); |
150 | for (i = 0; i < maxfd; i++) | 147 | for (i = 0; i < maxfd; i++) { |
151 | if (_cmd_pids[i] > 0) | 148 | if (_cmd_pids[i] > 0) { |
152 | close(i); | 149 | close(i); |
150 | } | ||
151 | } | ||
153 | 152 | ||
154 | execve(argv[0], argv, environ); | 153 | execve(argv[0], argv, environ); |
155 | _exit(STATE_UNKNOWN); | 154 | _exit(STATE_UNKNOWN); |
@@ -166,87 +165,94 @@ static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { | |||
166 | return pfd[0]; | 165 | return pfd[0]; |
167 | } | 166 | } |
168 | 167 | ||
169 | static int _cmd_close(int fd) { | 168 | static int _cmd_close(int fileDescriptor) { |
170 | int status; | ||
171 | pid_t pid; | 169 | pid_t pid; |
172 | 170 | ||
173 | /* make sure the provided fd was opened */ | 171 | /* make sure the provided fd was opened */ |
174 | long maxfd = mp_open_max(); | 172 | long maxfd = mp_open_max(); |
175 | if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) | 173 | if (fileDescriptor < 0 || fileDescriptor > maxfd || !_cmd_pids || |
174 | (pid = _cmd_pids[fileDescriptor]) == 0) { | ||
176 | return -1; | 175 | return -1; |
176 | } | ||
177 | 177 | ||
178 | _cmd_pids[fd] = 0; | 178 | _cmd_pids[fileDescriptor] = 0; |
179 | if (close(fd) == -1) | 179 | if (close(fileDescriptor) == -1) { |
180 | return -1; | 180 | return -1; |
181 | } | ||
181 | 182 | ||
182 | /* EINTR is ok (sort of), everything else is bad */ | 183 | /* EINTR is ok (sort of), everything else is bad */ |
183 | while (waitpid(pid, &status, 0) < 0) | 184 | int status; |
184 | if (errno != EINTR) | 185 | while (waitpid(pid, &status, 0) < 0) { |
186 | if (errno != EINTR) { | ||
185 | return -1; | 187 | return -1; |
188 | } | ||
189 | } | ||
186 | 190 | ||
187 | /* return child's termination status */ | 191 | /* return child's termination status */ |
188 | return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; | 192 | return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; |
189 | } | 193 | } |
190 | 194 | ||
191 | static int _cmd_fetch_output(int fd, output *op, int flags) { | 195 | static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) { |
192 | size_t len = 0, i = 0, lineno = 0; | ||
193 | size_t rsf = 6, ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ | ||
194 | char *buf = NULL; | ||
195 | int ret; | ||
196 | char tmpbuf[4096]; | 196 | char tmpbuf[4096]; |
197 | 197 | cmd_output->buf = NULL; | |
198 | op->buf = NULL; | 198 | cmd_output->buflen = 0; |
199 | op->buflen = 0; | 199 | ssize_t ret; |
200 | while ((ret = read(fd, tmpbuf, sizeof(tmpbuf))) > 0) { | 200 | while ((ret = read(fileDescriptor, tmpbuf, sizeof(tmpbuf))) > 0) { |
201 | len = (size_t)ret; | 201 | size_t len = (size_t)ret; |
202 | op->buf = realloc(op->buf, op->buflen + len + 1); | 202 | cmd_output->buf = realloc(cmd_output->buf, cmd_output->buflen + len + 1); |
203 | memcpy(op->buf + op->buflen, tmpbuf, len); | 203 | memcpy(cmd_output->buf + cmd_output->buflen, tmpbuf, len); |
204 | op->buflen += len; | 204 | cmd_output->buflen += len; |
205 | i++; | ||
206 | } | 205 | } |
207 | 206 | ||
208 | if (ret < 0) { | 207 | if (ret < 0) { |
209 | printf("read() returned %d: %s\n", ret, strerror(errno)); | 208 | printf("read() returned %zd: %s\n", ret, strerror(errno)); |
210 | return ret; | 209 | return ret; |
211 | } | 210 | } |
212 | 211 | ||
213 | /* some plugins may want to keep output unbroken, and some commands | 212 | /* some plugins may want to keep output unbroken, and some commands |
214 | * will yield no output, so return here for those */ | 213 | * will yield no output, so return here for those */ |
215 | if (flags & CMD_NO_ARRAYS || !op->buf || !op->buflen) | 214 | if (flags & CMD_NO_ARRAYS || !cmd_output->buf || !cmd_output->buflen) { |
216 | return op->buflen; | 215 | return cmd_output->buflen; |
216 | } | ||
217 | 217 | ||
218 | /* and some may want both */ | 218 | /* and some may want both */ |
219 | char *buf = NULL; | ||
219 | if (flags & CMD_NO_ASSOC) { | 220 | if (flags & CMD_NO_ASSOC) { |
220 | buf = malloc(op->buflen); | 221 | buf = malloc(cmd_output->buflen); |
221 | memcpy(buf, op->buf, op->buflen); | 222 | memcpy(buf, cmd_output->buf, cmd_output->buflen); |
222 | } else | 223 | } else { |
223 | buf = op->buf; | 224 | buf = cmd_output->buf; |
224 | 225 | } | |
225 | op->line = NULL; | 226 | |
226 | op->lens = NULL; | 227 | cmd_output->line = NULL; |
227 | i = 0; | 228 | cmd_output->lens = NULL; |
228 | while (i < op->buflen) { | 229 | size_t i = 0; |
230 | size_t ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ | ||
231 | size_t rsf = 6; | ||
232 | size_t lineno = 0; | ||
233 | while (i < cmd_output->buflen) { | ||
229 | /* make sure we have enough memory */ | 234 | /* make sure we have enough memory */ |
230 | if (lineno >= ary_size) { | 235 | if (lineno >= ary_size) { |
231 | /* ary_size must never be zero */ | 236 | /* ary_size must never be zero */ |
232 | do { | 237 | do { |
233 | ary_size = op->buflen >> --rsf; | 238 | ary_size = cmd_output->buflen >> --rsf; |
234 | } while (!ary_size); | 239 | } while (!ary_size); |
235 | 240 | ||
236 | op->line = realloc(op->line, ary_size * sizeof(char *)); | 241 | cmd_output->line = realloc(cmd_output->line, ary_size * sizeof(char *)); |
237 | op->lens = realloc(op->lens, ary_size * sizeof(size_t)); | 242 | cmd_output->lens = realloc(cmd_output->lens, ary_size * sizeof(size_t)); |
238 | } | 243 | } |
239 | 244 | ||
240 | /* set the pointer to the string */ | 245 | /* set the pointer to the string */ |
241 | op->line[lineno] = &buf[i]; | 246 | cmd_output->line[lineno] = &buf[i]; |
242 | 247 | ||
243 | /* hop to next newline or end of buffer */ | 248 | /* hop to next newline or end of buffer */ |
244 | while (buf[i] != '\n' && i < op->buflen) | 249 | while (buf[i] != '\n' && i < cmd_output->buflen) { |
245 | i++; | 250 | i++; |
251 | } | ||
246 | buf[i] = '\0'; | 252 | buf[i] = '\0'; |
247 | 253 | ||
248 | /* calculate the string length using pointer difference */ | 254 | /* calculate the string length using pointer difference */ |
249 | op->lens[lineno] = (size_t)&buf[i] - (size_t)op->line[lineno]; | 255 | cmd_output->lens[lineno] = (size_t)&buf[i] - (size_t)cmd_output->line[lineno]; |
250 | 256 | ||
251 | lineno++; | 257 | lineno++; |
252 | i++; | 258 | i++; |
@@ -256,41 +262,42 @@ static int _cmd_fetch_output(int fd, output *op, int flags) { | |||
256 | } | 262 | } |
257 | 263 | ||
258 | int cmd_run(const char *cmdstring, output *out, output *err, int flags) { | 264 | int cmd_run(const char *cmdstring, output *out, output *err, int flags) { |
259 | int i = 0, argc; | 265 | if (cmdstring == NULL) { |
260 | size_t cmdlen; | ||
261 | char **argv = NULL; | ||
262 | char *cmd = NULL; | ||
263 | char *str = NULL; | ||
264 | |||
265 | if (cmdstring == NULL) | ||
266 | return -1; | 266 | return -1; |
267 | } | ||
267 | 268 | ||
268 | /* initialize the structs */ | 269 | /* initialize the structs */ |
269 | if (out) | 270 | if (out) { |
270 | memset(out, 0, sizeof(output)); | 271 | memset(out, 0, sizeof(output)); |
271 | if (err) | 272 | } |
273 | if (err) { | ||
272 | memset(err, 0, sizeof(output)); | 274 | memset(err, 0, sizeof(output)); |
275 | } | ||
273 | 276 | ||
274 | /* make copy of command string so strtok() doesn't silently modify it */ | 277 | /* make copy of command string so strtok() doesn't silently modify it */ |
275 | /* (the calling program may want to access it later) */ | 278 | /* (the calling program may want to access it later) */ |
276 | cmdlen = strlen(cmdstring); | 279 | size_t cmdlen = strlen(cmdstring); |
277 | if ((cmd = malloc(cmdlen + 1)) == NULL) | 280 | char *cmd = NULL; |
281 | if ((cmd = malloc(cmdlen + 1)) == NULL) { | ||
278 | return -1; | 282 | return -1; |
283 | } | ||
279 | memcpy(cmd, cmdstring, cmdlen); | 284 | memcpy(cmd, cmdstring, cmdlen); |
280 | cmd[cmdlen] = '\0'; | 285 | cmd[cmdlen] = '\0'; |
281 | 286 | ||
282 | /* This is not a shell, so we don't handle "???" */ | 287 | /* This is not a shell, so we don't handle "???" */ |
283 | if (strstr(cmdstring, "\"")) | 288 | if (strstr(cmdstring, "\"")) { |
284 | return -1; | 289 | return -1; |
290 | } | ||
285 | 291 | ||
286 | /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ | 292 | /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ |
287 | if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) | 293 | if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) { |
288 | return -1; | 294 | return -1; |
295 | } | ||
289 | 296 | ||
290 | /* each arg must be whitespace-separated, so args can be a maximum | 297 | /* each arg must be whitespace-separated, so args can be a maximum |
291 | * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ | 298 | * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ |
292 | argc = (cmdlen >> 1) + 2; | 299 | int argc = (cmdlen >> 1) + 2; |
293 | argv = calloc((size_t)argc, sizeof(char *)); | 300 | char **argv = calloc((size_t)argc, sizeof(char *)); |
294 | 301 | ||
295 | if (argv == NULL) { | 302 | if (argv == NULL) { |
296 | printf("%s\n", _("Could not malloc argv array in popen()")); | 303 | printf("%s\n", _("Could not malloc argv array in popen()")); |
@@ -298,14 +305,16 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) { | |||
298 | } | 305 | } |
299 | 306 | ||
300 | /* get command arguments (stupidly, but fairly quickly) */ | 307 | /* get command arguments (stupidly, but fairly quickly) */ |
308 | int i = 0; | ||
301 | while (cmd) { | 309 | while (cmd) { |
302 | str = cmd; | 310 | char *str = cmd; |
303 | str += strspn(str, " \t\r\n"); /* trim any leading whitespace */ | 311 | str += strspn(str, " \t\r\n"); /* trim any leading whitespace */ |
304 | 312 | ||
305 | if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ | 313 | if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ |
306 | str++; | 314 | str++; |
307 | if (!strstr(str, "'")) | 315 | if (!strstr(str, "'")) { |
308 | return -1; /* balanced? */ | 316 | return -1; /* balanced? */ |
317 | } | ||
309 | cmd = 1 + strstr(str, "'"); | 318 | cmd = 1 + strstr(str, "'"); |
310 | str[strcspn(str, "'")] = 0; | 319 | str[strcspn(str, "'")] = 0; |
311 | } else { | 320 | } else { |
@@ -317,8 +326,9 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) { | |||
317 | } | 326 | } |
318 | } | 327 | } |
319 | 328 | ||
320 | if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) | 329 | if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) { |
321 | cmd = NULL; | 330 | cmd = NULL; |
331 | } | ||
322 | 332 | ||
323 | argv[i++] = str; | 333 | argv[i++] = str; |
324 | } | 334 | } |
@@ -327,53 +337,65 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) { | |||
327 | } | 337 | } |
328 | 338 | ||
329 | int cmd_run_array(char *const *argv, output *out, output *err, int flags) { | 339 | int cmd_run_array(char *const *argv, output *out, output *err, int flags) { |
330 | int fd, pfd_out[2], pfd_err[2]; | ||
331 | |||
332 | /* initialize the structs */ | 340 | /* initialize the structs */ |
333 | if (out) | 341 | if (out) { |
334 | memset(out, 0, sizeof(output)); | 342 | memset(out, 0, sizeof(output)); |
335 | if (err) | 343 | } |
344 | if (err) { | ||
336 | memset(err, 0, sizeof(output)); | 345 | memset(err, 0, sizeof(output)); |
346 | } | ||
337 | 347 | ||
338 | if ((fd = _cmd_open(argv, pfd_out, pfd_err)) == -1) | 348 | int fd; |
349 | int pfd_out[2]; | ||
350 | int pfd_err[2]; | ||
351 | if ((fd = _cmd_open(argv, pfd_out, pfd_err)) == -1) { | ||
339 | die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), argv[0]); | 352 | die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), argv[0]); |
353 | } | ||
340 | 354 | ||
341 | if (out) | 355 | if (out) { |
342 | out->lines = _cmd_fetch_output(pfd_out[0], out, flags); | 356 | out->lines = _cmd_fetch_output(pfd_out[0], out, flags); |
343 | if (err) | 357 | } |
358 | if (err) { | ||
344 | err->lines = _cmd_fetch_output(pfd_err[0], err, flags); | 359 | err->lines = _cmd_fetch_output(pfd_err[0], err, flags); |
360 | } | ||
345 | 361 | ||
346 | return _cmd_close(fd); | 362 | return _cmd_close(fd); |
347 | } | 363 | } |
348 | 364 | ||
349 | int cmd_file_read(char *filename, output *out, int flags) { | 365 | int cmd_file_read(const char *filename, output *out, int flags) { |
350 | int fd; | 366 | int fd; |
351 | if (out) | 367 | if (out) { |
352 | memset(out, 0, sizeof(output)); | 368 | memset(out, 0, sizeof(output)); |
369 | } | ||
353 | 370 | ||
354 | if ((fd = open(filename, O_RDONLY)) == -1) { | 371 | if ((fd = open(filename, O_RDONLY)) == -1) { |
355 | die(STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno)); | 372 | die(STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno)); |
356 | } | 373 | } |
357 | 374 | ||
358 | if (out) | 375 | if (out) { |
359 | out->lines = _cmd_fetch_output(fd, out, flags); | 376 | out->lines = _cmd_fetch_output(fd, out, flags); |
377 | } | ||
360 | 378 | ||
361 | if (close(fd) == -1) | 379 | if (close(fd) == -1) { |
362 | die(STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno)); | 380 | die(STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno)); |
381 | } | ||
363 | 382 | ||
364 | return 0; | 383 | return 0; |
365 | } | 384 | } |
366 | 385 | ||
367 | void timeout_alarm_handler(int signo) { | 386 | void timeout_alarm_handler(int signo) { |
368 | if (signo == SIGALRM) { | 387 | if (signo == SIGALRM) { |
369 | printf(_("%s - Plugin timed out after %d seconds\n"), state_text(timeout_state), timeout_interval); | 388 | printf(_("%s - Plugin timed out after %d seconds\n"), state_text(timeout_state), |
389 | timeout_interval); | ||
370 | 390 | ||
371 | long maxfd = mp_open_max(); | 391 | long maxfd = mp_open_max(); |
372 | if (_cmd_pids) | 392 | if (_cmd_pids) { |
373 | for (long int i = 0; i < maxfd; i++) { | 393 | for (long int i = 0; i < maxfd; i++) { |
374 | if (_cmd_pids[i] != 0) | 394 | if (_cmd_pids[i] != 0) { |
375 | kill(_cmd_pids[i], SIGKILL); | 395 | kill(_cmd_pids[i], SIGKILL); |
396 | } | ||
376 | } | 397 | } |
398 | } | ||
377 | 399 | ||
378 | exit(timeout_state); | 400 | exit(timeout_state); |
379 | } | 401 | } |
diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h index d00069c9..3672cdc9 100644 --- a/lib/utils_cmd.h +++ b/lib/utils_cmd.h | |||
@@ -5,22 +5,22 @@ | |||
5 | * Header file for Monitoring Plugins utils_cmd.c | 5 | * Header file for Monitoring Plugins utils_cmd.c |
6 | * | 6 | * |
7 | */ | 7 | */ |
8 | #include "../config.h" | ||
9 | #include <stddef.h> | ||
8 | 10 | ||
9 | /** types **/ | 11 | /** types **/ |
10 | struct output { | 12 | typedef struct { |
11 | char *buf; /* output buffer */ | 13 | char *buf; /* output buffer */ |
12 | size_t buflen; /* output buffer content length */ | 14 | size_t buflen; /* output buffer content length */ |
13 | char **line; /* array of lines (points to buf) */ | 15 | char **line; /* array of lines (points to buf) */ |
14 | size_t *lens; /* string lengths */ | 16 | size_t *lens; /* string lengths */ |
15 | size_t lines; /* lines of output */ | 17 | size_t lines; /* lines of output */ |
16 | }; | 18 | } output; |
17 | |||
18 | typedef struct output output; | ||
19 | 19 | ||
20 | /** prototypes **/ | 20 | /** prototypes **/ |
21 | int cmd_run(const char *, output *, output *, int); | 21 | int cmd_run(const char *, output *, output *, int); |
22 | int cmd_run_array(char *const *, output *, output *, int); | 22 | int cmd_run_array(char *const *, output *, output *, int); |
23 | int cmd_file_read(char *, output *, int); | 23 | int cmd_file_read(const char *, output *, int); |
24 | 24 | ||
25 | /* only multi-threaded plugins need to bother with this */ | 25 | /* only multi-threaded plugins need to bother with this */ |
26 | void cmd_init(void); | 26 | void cmd_init(void); |
diff --git a/lib/utils_disk.c b/lib/utils_disk.c deleted file mode 100644 index 2b761f5e..00000000 --- a/lib/utils_disk.c +++ /dev/null | |||
@@ -1,255 +0,0 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * Library for check_disk | ||
4 | * | ||
5 | * License: GPL | ||
6 | * Copyright (c) 1999-2024 Monitoring Plugins Development Team | ||
7 | * | ||
8 | * Description: | ||
9 | * | ||
10 | * This file contains utilities for check_disk. These are tested by libtap | ||
11 | * | ||
12 | * | ||
13 | * This program is free software: you can redistribute it and/or modify | ||
14 | * it under the terms of the GNU General Public License as published by | ||
15 | * the Free Software Foundation, either version 3 of the License, or | ||
16 | * (at your option) any later version. | ||
17 | * | ||
18 | * This program is distributed in the hope that it will be useful, | ||
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
21 | * GNU General Public License for more details. | ||
22 | * | ||
23 | * You should have received a copy of the GNU General Public License | ||
24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
25 | * | ||
26 | * | ||
27 | *****************************************************************************/ | ||
28 | |||
29 | #include "common.h" | ||
30 | #include "utils_disk.h" | ||
31 | #include "gl/fsusage.h" | ||
32 | #include <string.h> | ||
33 | |||
34 | void np_add_name(struct name_list **list, const char *name) { | ||
35 | struct name_list *new_entry; | ||
36 | new_entry = (struct name_list *)malloc(sizeof *new_entry); | ||
37 | new_entry->name = (char *)name; | ||
38 | new_entry->next = *list; | ||
39 | *list = new_entry; | ||
40 | } | ||
41 | |||
42 | /* @brief Initialises a new regex at the begin of list via regcomp(3) | ||
43 | * | ||
44 | * @details if the regex fails to compile the error code of regcomp(3) is returned | ||
45 | * and list is not modified, otherwise list is modified to point to the new | ||
46 | * element | ||
47 | * @param list Pointer to a linked list of regex_list elements | ||
48 | * @param regex the string containing the regex which should be inserted into the list | ||
49 | * @param clags the cflags parameter for regcomp(3) | ||
50 | */ | ||
51 | int np_add_regex(struct regex_list **list, const char *regex, int cflags) { | ||
52 | struct regex_list *new_entry = (struct regex_list *)malloc(sizeof *new_entry); | ||
53 | |||
54 | if (new_entry == NULL) { | ||
55 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
56 | } | ||
57 | |||
58 | int regcomp_result = regcomp(&new_entry->regex, regex, cflags); | ||
59 | |||
60 | if (!regcomp_result) { | ||
61 | // regcomp succeeded | ||
62 | new_entry->next = *list; | ||
63 | *list = new_entry; | ||
64 | |||
65 | return 0; | ||
66 | } else { | ||
67 | // regcomp failed | ||
68 | free(new_entry); | ||
69 | |||
70 | return regcomp_result; | ||
71 | } | ||
72 | } | ||
73 | |||
74 | /* Initialises a new parameter at the end of list */ | ||
75 | struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name) { | ||
76 | struct parameter_list *current = *list; | ||
77 | struct parameter_list *new_path; | ||
78 | new_path = (struct parameter_list *)malloc(sizeof *new_path); | ||
79 | new_path->name = (char *)malloc(strlen(name) + 1); | ||
80 | new_path->best_match = NULL; | ||
81 | new_path->name_next = NULL; | ||
82 | new_path->name_prev = NULL; | ||
83 | new_path->freespace_bytes = NULL; | ||
84 | new_path->freespace_units = NULL; | ||
85 | new_path->freespace_percent = NULL; | ||
86 | new_path->usedspace_bytes = NULL; | ||
87 | new_path->usedspace_units = NULL; | ||
88 | new_path->usedspace_percent = NULL; | ||
89 | new_path->usedinodes_percent = NULL; | ||
90 | new_path->freeinodes_percent = NULL; | ||
91 | new_path->group = NULL; | ||
92 | new_path->dfree_pct = -1; | ||
93 | new_path->dused_pct = -1; | ||
94 | new_path->total = 0; | ||
95 | new_path->available = 0; | ||
96 | new_path->available_to_root = 0; | ||
97 | new_path->used = 0; | ||
98 | new_path->dused_units = 0; | ||
99 | new_path->dfree_units = 0; | ||
100 | new_path->dtotal_units = 0; | ||
101 | new_path->inodes_total = 0; | ||
102 | new_path->inodes_free = 0; | ||
103 | new_path->inodes_free_to_root = 0; | ||
104 | new_path->inodes_used = 0; | ||
105 | new_path->dused_inodes_percent = 0; | ||
106 | new_path->dfree_inodes_percent = 0; | ||
107 | |||
108 | strcpy(new_path->name, name); | ||
109 | |||
110 | if (current == NULL) { | ||
111 | *list = new_path; | ||
112 | new_path->name_prev = NULL; | ||
113 | } else { | ||
114 | while (current->name_next) { | ||
115 | current = current->name_next; | ||
116 | } | ||
117 | current->name_next = new_path; | ||
118 | new_path->name_prev = current; | ||
119 | } | ||
120 | return new_path; | ||
121 | } | ||
122 | |||
123 | /* Delete a given parameter from list and return pointer to next element*/ | ||
124 | struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev) { | ||
125 | if (item == NULL) { | ||
126 | return NULL; | ||
127 | } | ||
128 | struct parameter_list *next; | ||
129 | |||
130 | if (item->name_next) | ||
131 | next = item->name_next; | ||
132 | else | ||
133 | next = NULL; | ||
134 | |||
135 | if (next) | ||
136 | next->name_prev = prev; | ||
137 | |||
138 | if (prev) | ||
139 | prev->name_next = next; | ||
140 | |||
141 | if (item->name) { | ||
142 | free(item->name); | ||
143 | } | ||
144 | free(item); | ||
145 | |||
146 | return next; | ||
147 | } | ||
148 | |||
149 | /* returns a pointer to the struct found in the list */ | ||
150 | struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name) { | ||
151 | struct parameter_list *temp_list; | ||
152 | for (temp_list = list; temp_list; temp_list = temp_list->name_next) { | ||
153 | if (!strcmp(temp_list->name, name)) | ||
154 | return temp_list; | ||
155 | } | ||
156 | |||
157 | return NULL; | ||
158 | } | ||
159 | |||
160 | void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) { | ||
161 | struct parameter_list *d; | ||
162 | for (d = desired; d; d = d->name_next) { | ||
163 | if (!d->best_match) { | ||
164 | struct mount_entry *me; | ||
165 | size_t name_len = strlen(d->name); | ||
166 | size_t best_match_len = 0; | ||
167 | struct mount_entry *best_match = NULL; | ||
168 | struct fs_usage fsp; | ||
169 | |||
170 | /* set best match if path name exactly matches a mounted device name */ | ||
171 | for (me = mount_list; me; me = me->me_next) { | ||
172 | if (strcmp(me->me_devname, d->name) == 0) { | ||
173 | if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { | ||
174 | best_match = me; | ||
175 | } | ||
176 | } | ||
177 | } | ||
178 | |||
179 | /* set best match by directory name if no match was found by devname */ | ||
180 | if (!best_match) { | ||
181 | for (me = mount_list; me; me = me->me_next) { | ||
182 | size_t len = strlen(me->me_mountdir); | ||
183 | if ((!exact && | ||
184 | (best_match_len <= len && len <= name_len && (len == 1 || strncmp(me->me_mountdir, d->name, len) == 0))) || | ||
185 | (exact && strcmp(me->me_mountdir, d->name) == 0)) { | ||
186 | if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { | ||
187 | best_match = me; | ||
188 | best_match_len = len; | ||
189 | } | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | |||
194 | if (best_match) { | ||
195 | d->best_match = best_match; | ||
196 | } else { | ||
197 | d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */ | ||
198 | } | ||
199 | } | ||
200 | } | ||
201 | } | ||
202 | |||
203 | /* Returns true if name is in list */ | ||
204 | bool np_find_name(struct name_list *list, const char *name) { | ||
205 | const struct name_list *n; | ||
206 | |||
207 | if (list == NULL || name == NULL) { | ||
208 | return false; | ||
209 | } | ||
210 | for (n = list; n; n = n->next) { | ||
211 | if (!strcmp(name, n->name)) { | ||
212 | return true; | ||
213 | } | ||
214 | } | ||
215 | return false; | ||
216 | } | ||
217 | |||
218 | /* Returns true if name is in list */ | ||
219 | bool np_find_regmatch(struct regex_list *list, const char *name) { | ||
220 | int len; | ||
221 | regmatch_t m; | ||
222 | |||
223 | if (name == NULL) { | ||
224 | return false; | ||
225 | } | ||
226 | |||
227 | len = strlen(name); | ||
228 | |||
229 | for (; list; list = list->next) { | ||
230 | /* Emulate a full match as if surrounded with ^( )$ | ||
231 | by checking whether the match spans the whole name */ | ||
232 | if (!regexec(&list->regex, name, 1, &m, 0) && m.rm_so == 0 && m.rm_eo == len) { | ||
233 | return true; | ||
234 | } | ||
235 | } | ||
236 | |||
237 | return false; | ||
238 | } | ||
239 | |||
240 | bool np_seen_name(struct name_list *list, const char *name) { | ||
241 | const struct name_list *s; | ||
242 | for (s = list; s; s = s->next) { | ||
243 | if (!strcmp(s->name, name)) { | ||
244 | return true; | ||
245 | } | ||
246 | } | ||
247 | return false; | ||
248 | } | ||
249 | |||
250 | bool np_regex_match_mount_entry(struct mount_entry *me, regex_t *re) { | ||
251 | if (regexec(re, me->me_devname, (size_t)0, NULL, 0) == 0 || regexec(re, me->me_mountdir, (size_t)0, NULL, 0) == 0) { | ||
252 | return true; | ||
253 | } | ||
254 | return false; | ||
255 | } | ||
diff --git a/lib/utils_disk.h b/lib/utils_disk.h deleted file mode 100644 index c5e81dc1..00000000 --- a/lib/utils_disk.h +++ /dev/null | |||
@@ -1,48 +0,0 @@ | |||
1 | /* Header file for utils_disk */ | ||
2 | |||
3 | #include "mountlist.h" | ||
4 | #include "utils_base.h" | ||
5 | #include "regex.h" | ||
6 | |||
7 | struct name_list { | ||
8 | char *name; | ||
9 | struct name_list *next; | ||
10 | }; | ||
11 | |||
12 | struct regex_list { | ||
13 | regex_t regex; | ||
14 | struct regex_list *next; | ||
15 | }; | ||
16 | |||
17 | struct parameter_list { | ||
18 | char *name; | ||
19 | thresholds *freespace_bytes; | ||
20 | thresholds *freespace_units; | ||
21 | thresholds *freespace_percent; | ||
22 | thresholds *usedspace_bytes; | ||
23 | thresholds *usedspace_units; | ||
24 | thresholds *usedspace_percent; | ||
25 | thresholds *usedinodes_percent; | ||
26 | thresholds *freeinodes_percent; | ||
27 | char *group; | ||
28 | struct mount_entry *best_match; | ||
29 | struct parameter_list *name_next; | ||
30 | struct parameter_list *name_prev; | ||
31 | uintmax_t total, available, available_to_root, used, inodes_free, inodes_free_to_root, inodes_used, inodes_total; | ||
32 | double dfree_pct, dused_pct; | ||
33 | uint64_t dused_units, dfree_units, dtotal_units; | ||
34 | double dused_inodes_percent, dfree_inodes_percent; | ||
35 | }; | ||
36 | |||
37 | void np_add_name(struct name_list **list, const char *name); | ||
38 | bool np_find_name(struct name_list *list, const char *name); | ||
39 | bool np_seen_name(struct name_list *list, const char *name); | ||
40 | int np_add_regex(struct regex_list **list, const char *regex, int cflags); | ||
41 | bool np_find_regmatch(struct regex_list *list, const char *name); | ||
42 | struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); | ||
43 | struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name); | ||
44 | struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev); | ||
45 | |||
46 | int search_parameter_list(struct parameter_list *list, const char *name); | ||
47 | void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact); | ||
48 | bool np_regex_match_mount_entry(struct mount_entry *me, regex_t *re); | ||
diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c index daae1d54..a82d5a3f 100644 --- a/lib/utils_tcp.c +++ b/lib/utils_tcp.c | |||
@@ -26,28 +26,35 @@ | |||
26 | * | 26 | * |
27 | *****************************************************************************/ | 27 | *****************************************************************************/ |
28 | 28 | ||
29 | #include "common.h" | 29 | #include "../config.h" |
30 | #include "utils_tcp.h" | 30 | #include "utils_tcp.h" |
31 | #include <stdio.h> | ||
32 | #include <string.h> | ||
31 | 33 | ||
32 | #define VERBOSE(message) \ | 34 | #define VERBOSE(message) \ |
33 | do { \ | 35 | do { \ |
34 | if (flags & NP_MATCH_VERBOSE) \ | 36 | if (flags & NP_MATCH_VERBOSE) \ |
35 | puts(message); \ | 37 | puts(message); \ |
36 | } while (0) | 38 | } while (0) |
37 | 39 | ||
38 | enum np_match_result np_expect_match(char *status, char **server_expect, int expect_count, int flags) { | 40 | enum np_match_result np_expect_match(char *status, char **server_expect, int expect_count, |
39 | int i, match = 0, partial = 0; | 41 | int flags) { |
40 | 42 | int match = 0; | |
41 | for (i = 0; i < expect_count; i++) { | 43 | int partial = 0; |
42 | if (flags & NP_MATCH_VERBOSE) | 44 | for (int i = 0; i < expect_count; i++) { |
43 | printf("looking for [%s] %s [%s]\n", server_expect[i], (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in", status); | 45 | if (flags & NP_MATCH_VERBOSE) { |
46 | printf("looking for [%s] %s [%s]\n", server_expect[i], | ||
47 | (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in", status); | ||
48 | } | ||
44 | 49 | ||
45 | if (flags & NP_MATCH_EXACT) { | 50 | if (flags & NP_MATCH_EXACT) { |
46 | if (strncmp(status, server_expect[i], strlen(server_expect[i])) == 0) { | 51 | if (strncmp(status, server_expect[i], strlen(server_expect[i])) == 0) { |
47 | VERBOSE("found it"); | 52 | VERBOSE("found it"); |
48 | match++; | 53 | match++; |
49 | continue; | 54 | continue; |
50 | } else if (strncmp(status, server_expect[i], strlen(status)) == 0) { | 55 | } |
56 | |||
57 | if (strncmp(status, server_expect[i], strlen(status)) == 0) { | ||
51 | VERBOSE("found a substring"); | 58 | VERBOSE("found a substring"); |
52 | partial++; | 59 | partial++; |
53 | continue; | 60 | continue; |
@@ -60,10 +67,12 @@ enum np_match_result np_expect_match(char *status, char **server_expect, int exp | |||
60 | VERBOSE("couldn't find it"); | 67 | VERBOSE("couldn't find it"); |
61 | } | 68 | } |
62 | 69 | ||
63 | if ((flags & NP_MATCH_ALL && match == expect_count) || (!(flags & NP_MATCH_ALL) && match >= 1)) | 70 | if ((flags & NP_MATCH_ALL && match == expect_count) || |
71 | (!(flags & NP_MATCH_ALL) && match >= 1)) { | ||
64 | return NP_MATCH_SUCCESS; | 72 | return NP_MATCH_SUCCESS; |
65 | else if (partial > 0 || !(flags & NP_MATCH_EXACT)) | 73 | } |
74 | if (partial > 0 || !(flags & NP_MATCH_EXACT)) { | ||
66 | return NP_MATCH_RETRY; | 75 | return NP_MATCH_RETRY; |
67 | else | 76 | } |
68 | return NP_MATCH_FAILURE; | 77 | return NP_MATCH_FAILURE; |
69 | } | 78 | } |
diff --git a/lib/utils_tcp.h b/lib/utils_tcp.h index d5999e9b..e5cdbb82 100644 --- a/lib/utils_tcp.h +++ b/lib/utils_tcp.h | |||
@@ -11,9 +11,11 @@ | |||
11 | * server. | 11 | * server. |
12 | */ | 12 | */ |
13 | enum np_match_result { | 13 | enum np_match_result { |
14 | NP_MATCH_NONE, | ||
14 | NP_MATCH_FAILURE, | 15 | NP_MATCH_FAILURE, |
15 | NP_MATCH_SUCCESS, | 16 | NP_MATCH_SUCCESS, |
16 | NP_MATCH_RETRY | 17 | NP_MATCH_RETRY |
17 | }; | 18 | }; |
18 | 19 | ||
19 | enum np_match_result np_expect_match(char *status, char **server_expect, int server_expect_count, int flags); | 20 | enum np_match_result np_expect_match(char *status, char **server_expect, int server_expect_count, |
21 | int flags); | ||
diff --git a/lib/vendor/cJSON/cJSON.c b/lib/vendor/cJSON/cJSON.c new file mode 100644 index 00000000..12076e92 --- /dev/null +++ b/lib/vendor/cJSON/cJSON.c | |||
@@ -0,0 +1,3165 @@ | |||
1 | /* | ||
2 | Copyright (c) 2009-2017 Dave Gamble and cJSON contributors | ||
3 | |||
4 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
5 | of this software and associated documentation files (the "Software"), to deal | ||
6 | in the Software without restriction, including without limitation the rights | ||
7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
8 | copies of the Software, and to permit persons to whom the Software is | ||
9 | furnished to do so, subject to the following conditions: | ||
10 | |||
11 | The above copyright notice and this permission notice shall be included in | ||
12 | all copies or substantial portions of the Software. | ||
13 | |||
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
20 | THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | /* cJSON */ | ||
24 | /* JSON parser in C. */ | ||
25 | |||
26 | /* disable warnings about old C89 functions in MSVC */ | ||
27 | #if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) | ||
28 | #define _CRT_SECURE_NO_DEPRECATE | ||
29 | #endif | ||
30 | |||
31 | #ifdef __GNUC__ | ||
32 | #pragma GCC visibility push(default) | ||
33 | #endif | ||
34 | #if defined(_MSC_VER) | ||
35 | #pragma warning (push) | ||
36 | /* disable warning about single line comments in system headers */ | ||
37 | #pragma warning (disable : 4001) | ||
38 | #endif | ||
39 | |||
40 | #include "../../../config.h" | ||
41 | #include <string.h> | ||
42 | #include <stdio.h> | ||
43 | #include <math.h> | ||
44 | #include <stdlib.h> | ||
45 | #include <limits.h> | ||
46 | #include <ctype.h> | ||
47 | #include <float.h> | ||
48 | |||
49 | #ifdef ENABLE_LOCALES | ||
50 | #include <locale.h> | ||
51 | #endif | ||
52 | |||
53 | #if defined(_MSC_VER) | ||
54 | #pragma warning (pop) | ||
55 | #endif | ||
56 | #ifdef __GNUC__ | ||
57 | #pragma GCC visibility pop | ||
58 | #endif | ||
59 | |||
60 | #include "cJSON.h" | ||
61 | |||
62 | /* define our own boolean type */ | ||
63 | #ifdef true | ||
64 | #undef true | ||
65 | #endif | ||
66 | #define true ((cJSON_bool)1) | ||
67 | |||
68 | #ifdef false | ||
69 | #undef false | ||
70 | #endif | ||
71 | #define false ((cJSON_bool)0) | ||
72 | |||
73 | /* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */ | ||
74 | #ifndef isinf | ||
75 | #define isinf(d) (isnan((d - d)) && !isnan(d)) | ||
76 | #endif | ||
77 | #ifndef isnan | ||
78 | #define isnan(d) (d != d) | ||
79 | #endif | ||
80 | |||
81 | #ifndef NAN | ||
82 | #ifdef _WIN32 | ||
83 | #define NAN sqrt(-1.0) | ||
84 | #else | ||
85 | #define NAN 0.0/0.0 | ||
86 | #endif | ||
87 | #endif | ||
88 | |||
89 | typedef struct { | ||
90 | const unsigned char *json; | ||
91 | size_t position; | ||
92 | } error; | ||
93 | static error global_error = { NULL, 0 }; | ||
94 | |||
95 | CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) | ||
96 | { | ||
97 | return (const char*) (global_error.json + global_error.position); | ||
98 | } | ||
99 | |||
100 | CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item) | ||
101 | { | ||
102 | if (!cJSON_IsString(item)) | ||
103 | { | ||
104 | return NULL; | ||
105 | } | ||
106 | |||
107 | return item->valuestring; | ||
108 | } | ||
109 | |||
110 | CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) | ||
111 | { | ||
112 | if (!cJSON_IsNumber(item)) | ||
113 | { | ||
114 | return (double) NAN; | ||
115 | } | ||
116 | |||
117 | return item->valuedouble; | ||
118 | } | ||
119 | |||
120 | /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ | ||
121 | #if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 18) | ||
122 | #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. | ||
123 | #endif | ||
124 | |||
125 | CJSON_PUBLIC(const char*) cJSON_Version(void) | ||
126 | { | ||
127 | static char version[15]; | ||
128 | sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); | ||
129 | |||
130 | return version; | ||
131 | } | ||
132 | |||
133 | /* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ | ||
134 | static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) | ||
135 | { | ||
136 | if ((string1 == NULL) || (string2 == NULL)) | ||
137 | { | ||
138 | return 1; | ||
139 | } | ||
140 | |||
141 | if (string1 == string2) | ||
142 | { | ||
143 | return 0; | ||
144 | } | ||
145 | |||
146 | for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) | ||
147 | { | ||
148 | if (*string1 == '\0') | ||
149 | { | ||
150 | return 0; | ||
151 | } | ||
152 | } | ||
153 | |||
154 | return tolower(*string1) - tolower(*string2); | ||
155 | } | ||
156 | |||
157 | typedef struct internal_hooks | ||
158 | { | ||
159 | void *(CJSON_CDECL *allocate)(size_t size); | ||
160 | void (CJSON_CDECL *deallocate)(void *pointer); | ||
161 | void *(CJSON_CDECL *reallocate)(void *pointer, size_t size); | ||
162 | } internal_hooks; | ||
163 | |||
164 | #if defined(_MSC_VER) | ||
165 | /* work around MSVC error C2322: '...' address of dllimport '...' is not static */ | ||
166 | static void * CJSON_CDECL internal_malloc(size_t size) | ||
167 | { | ||
168 | return malloc(size); | ||
169 | } | ||
170 | static void CJSON_CDECL internal_free(void *pointer) | ||
171 | { | ||
172 | free(pointer); | ||
173 | } | ||
174 | static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) | ||
175 | { | ||
176 | return realloc(pointer, size); | ||
177 | } | ||
178 | #else | ||
179 | #define internal_malloc malloc | ||
180 | #define internal_free free | ||
181 | #define internal_realloc realloc | ||
182 | #endif | ||
183 | |||
184 | /* strlen of character literals resolved at compile time */ | ||
185 | #define static_strlen(string_literal) (sizeof(string_literal) - sizeof("")) | ||
186 | |||
187 | static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; | ||
188 | |||
189 | static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) | ||
190 | { | ||
191 | size_t length = 0; | ||
192 | unsigned char *copy = NULL; | ||
193 | |||
194 | if (string == NULL) | ||
195 | { | ||
196 | return NULL; | ||
197 | } | ||
198 | |||
199 | length = strlen((const char*)string) + sizeof(""); | ||
200 | copy = (unsigned char*)hooks->allocate(length); | ||
201 | if (copy == NULL) | ||
202 | { | ||
203 | return NULL; | ||
204 | } | ||
205 | memcpy(copy, string, length); | ||
206 | |||
207 | return copy; | ||
208 | } | ||
209 | |||
210 | CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) | ||
211 | { | ||
212 | if (hooks == NULL) | ||
213 | { | ||
214 | /* Reset hooks */ | ||
215 | global_hooks.allocate = malloc; | ||
216 | global_hooks.deallocate = free; | ||
217 | global_hooks.reallocate = realloc; | ||
218 | return; | ||
219 | } | ||
220 | |||
221 | global_hooks.allocate = malloc; | ||
222 | if (hooks->malloc_fn != NULL) | ||
223 | { | ||
224 | global_hooks.allocate = hooks->malloc_fn; | ||
225 | } | ||
226 | |||
227 | global_hooks.deallocate = free; | ||
228 | if (hooks->free_fn != NULL) | ||
229 | { | ||
230 | global_hooks.deallocate = hooks->free_fn; | ||
231 | } | ||
232 | |||
233 | /* use realloc only if both free and malloc are used */ | ||
234 | global_hooks.reallocate = NULL; | ||
235 | if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) | ||
236 | { | ||
237 | global_hooks.reallocate = realloc; | ||
238 | } | ||
239 | } | ||
240 | |||
241 | /* Internal constructor. */ | ||
242 | static cJSON *cJSON_New_Item(const internal_hooks * const hooks) | ||
243 | { | ||
244 | cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON)); | ||
245 | if (node) | ||
246 | { | ||
247 | memset(node, '\0', sizeof(cJSON)); | ||
248 | } | ||
249 | |||
250 | return node; | ||
251 | } | ||
252 | |||
253 | /* Delete a cJSON structure. */ | ||
254 | CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) | ||
255 | { | ||
256 | cJSON *next = NULL; | ||
257 | while (item != NULL) | ||
258 | { | ||
259 | next = item->next; | ||
260 | if (!(item->type & cJSON_IsReference) && (item->child != NULL)) | ||
261 | { | ||
262 | cJSON_Delete(item->child); | ||
263 | } | ||
264 | if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) | ||
265 | { | ||
266 | global_hooks.deallocate(item->valuestring); | ||
267 | item->valuestring = NULL; | ||
268 | } | ||
269 | if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) | ||
270 | { | ||
271 | global_hooks.deallocate(item->string); | ||
272 | item->string = NULL; | ||
273 | } | ||
274 | global_hooks.deallocate(item); | ||
275 | item = next; | ||
276 | } | ||
277 | } | ||
278 | |||
279 | /* get the decimal point character of the current locale */ | ||
280 | static unsigned char get_decimal_point(void) | ||
281 | { | ||
282 | #ifdef ENABLE_LOCALES | ||
283 | struct lconv *lconv = localeconv(); | ||
284 | return (unsigned char) lconv->decimal_point[0]; | ||
285 | #else | ||
286 | return '.'; | ||
287 | #endif | ||
288 | } | ||
289 | |||
290 | typedef struct | ||
291 | { | ||
292 | const unsigned char *content; | ||
293 | size_t length; | ||
294 | size_t offset; | ||
295 | size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ | ||
296 | internal_hooks hooks; | ||
297 | } parse_buffer; | ||
298 | |||
299 | /* check if the given size is left to read in a given parse buffer (starting with 1) */ | ||
300 | #define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) | ||
301 | /* check if the buffer can be accessed at the given index (starting with 0) */ | ||
302 | #define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) | ||
303 | #define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) | ||
304 | /* get a pointer to the buffer at the position */ | ||
305 | #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) | ||
306 | |||
307 | /* Parse the input text to generate a number, and populate the result into item. */ | ||
308 | static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) | ||
309 | { | ||
310 | double number = 0; | ||
311 | unsigned char *after_end = NULL; | ||
312 | unsigned char number_c_string[64]; | ||
313 | unsigned char decimal_point = get_decimal_point(); | ||
314 | size_t i = 0; | ||
315 | |||
316 | if ((input_buffer == NULL) || (input_buffer->content == NULL)) | ||
317 | { | ||
318 | return false; | ||
319 | } | ||
320 | |||
321 | /* copy the number into a temporary buffer and replace '.' with the decimal point | ||
322 | * of the current locale (for strtod) | ||
323 | * This also takes care of '\0' not necessarily being available for marking the end of the input */ | ||
324 | for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) | ||
325 | { | ||
326 | switch (buffer_at_offset(input_buffer)[i]) | ||
327 | { | ||
328 | case '0': | ||
329 | case '1': | ||
330 | case '2': | ||
331 | case '3': | ||
332 | case '4': | ||
333 | case '5': | ||
334 | case '6': | ||
335 | case '7': | ||
336 | case '8': | ||
337 | case '9': | ||
338 | case '+': | ||
339 | case '-': | ||
340 | case 'e': | ||
341 | case 'E': | ||
342 | number_c_string[i] = buffer_at_offset(input_buffer)[i]; | ||
343 | break; | ||
344 | |||
345 | case '.': | ||
346 | number_c_string[i] = decimal_point; | ||
347 | break; | ||
348 | |||
349 | default: | ||
350 | goto loop_end; | ||
351 | } | ||
352 | } | ||
353 | loop_end: | ||
354 | number_c_string[i] = '\0'; | ||
355 | |||
356 | number = strtod((const char*)number_c_string, (char**)&after_end); | ||
357 | if (number_c_string == after_end) | ||
358 | { | ||
359 | return false; /* parse_error */ | ||
360 | } | ||
361 | |||
362 | item->valuedouble = number; | ||
363 | |||
364 | /* use saturation in case of overflow */ | ||
365 | if (number >= INT_MAX) | ||
366 | { | ||
367 | item->valueint = INT_MAX; | ||
368 | } | ||
369 | else if (number <= (double)INT_MIN) | ||
370 | { | ||
371 | item->valueint = INT_MIN; | ||
372 | } | ||
373 | else | ||
374 | { | ||
375 | item->valueint = (int)number; | ||
376 | } | ||
377 | |||
378 | item->type = cJSON_Number; | ||
379 | |||
380 | input_buffer->offset += (size_t)(after_end - number_c_string); | ||
381 | return true; | ||
382 | } | ||
383 | |||
384 | /* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */ | ||
385 | CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) | ||
386 | { | ||
387 | if (number >= INT_MAX) | ||
388 | { | ||
389 | object->valueint = INT_MAX; | ||
390 | } | ||
391 | else if (number <= (double)INT_MIN) | ||
392 | { | ||
393 | object->valueint = INT_MIN; | ||
394 | } | ||
395 | else | ||
396 | { | ||
397 | object->valueint = (int)number; | ||
398 | } | ||
399 | |||
400 | return object->valuedouble = number; | ||
401 | } | ||
402 | |||
403 | /* Note: when passing a NULL valuestring, cJSON_SetValuestring treats this as an error and return NULL */ | ||
404 | CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) | ||
405 | { | ||
406 | char *copy = NULL; | ||
407 | size_t v1_len; | ||
408 | size_t v2_len; | ||
409 | /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */ | ||
410 | if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference)) | ||
411 | { | ||
412 | return NULL; | ||
413 | } | ||
414 | /* return NULL if the object is corrupted or valuestring is NULL */ | ||
415 | if (object->valuestring == NULL || valuestring == NULL) | ||
416 | { | ||
417 | return NULL; | ||
418 | } | ||
419 | |||
420 | v1_len = strlen(valuestring); | ||
421 | v2_len = strlen(object->valuestring); | ||
422 | |||
423 | if (v1_len <= v2_len) | ||
424 | { | ||
425 | /* strcpy does not handle overlapping string: [X1, X2] [Y1, Y2] => X2 < Y1 or Y2 < X1 */ | ||
426 | if (!( valuestring + v1_len < object->valuestring || object->valuestring + v2_len < valuestring )) | ||
427 | { | ||
428 | return NULL; | ||
429 | } | ||
430 | strcpy(object->valuestring, valuestring); | ||
431 | return object->valuestring; | ||
432 | } | ||
433 | copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); | ||
434 | if (copy == NULL) | ||
435 | { | ||
436 | return NULL; | ||
437 | } | ||
438 | if (object->valuestring != NULL) | ||
439 | { | ||
440 | cJSON_free(object->valuestring); | ||
441 | } | ||
442 | object->valuestring = copy; | ||
443 | |||
444 | return copy; | ||
445 | } | ||
446 | |||
447 | typedef struct | ||
448 | { | ||
449 | unsigned char *buffer; | ||
450 | size_t length; | ||
451 | size_t offset; | ||
452 | size_t depth; /* current nesting depth (for formatted printing) */ | ||
453 | cJSON_bool noalloc; | ||
454 | cJSON_bool format; /* is this print a formatted print */ | ||
455 | internal_hooks hooks; | ||
456 | } printbuffer; | ||
457 | |||
458 | /* realloc printbuffer if necessary to have at least "needed" bytes more */ | ||
459 | static unsigned char* ensure(printbuffer * const p, size_t needed) | ||
460 | { | ||
461 | unsigned char *newbuffer = NULL; | ||
462 | size_t newsize = 0; | ||
463 | |||
464 | if ((p == NULL) || (p->buffer == NULL)) | ||
465 | { | ||
466 | return NULL; | ||
467 | } | ||
468 | |||
469 | if ((p->length > 0) && (p->offset >= p->length)) | ||
470 | { | ||
471 | /* make sure that offset is valid */ | ||
472 | return NULL; | ||
473 | } | ||
474 | |||
475 | if (needed > INT_MAX) | ||
476 | { | ||
477 | /* sizes bigger than INT_MAX are currently not supported */ | ||
478 | return NULL; | ||
479 | } | ||
480 | |||
481 | needed += p->offset + 1; | ||
482 | if (needed <= p->length) | ||
483 | { | ||
484 | return p->buffer + p->offset; | ||
485 | } | ||
486 | |||
487 | if (p->noalloc) { | ||
488 | return NULL; | ||
489 | } | ||
490 | |||
491 | /* calculate new buffer size */ | ||
492 | if (needed > (INT_MAX / 2)) | ||
493 | { | ||
494 | /* overflow of int, use INT_MAX if possible */ | ||
495 | if (needed <= INT_MAX) | ||
496 | { | ||
497 | newsize = INT_MAX; | ||
498 | } | ||
499 | else | ||
500 | { | ||
501 | return NULL; | ||
502 | } | ||
503 | } | ||
504 | else | ||
505 | { | ||
506 | newsize = needed * 2; | ||
507 | } | ||
508 | |||
509 | if (p->hooks.reallocate != NULL) | ||
510 | { | ||
511 | /* reallocate with realloc if available */ | ||
512 | newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); | ||
513 | if (newbuffer == NULL) | ||
514 | { | ||
515 | p->hooks.deallocate(p->buffer); | ||
516 | p->length = 0; | ||
517 | p->buffer = NULL; | ||
518 | |||
519 | return NULL; | ||
520 | } | ||
521 | } | ||
522 | else | ||
523 | { | ||
524 | /* otherwise reallocate manually */ | ||
525 | newbuffer = (unsigned char*)p->hooks.allocate(newsize); | ||
526 | if (!newbuffer) | ||
527 | { | ||
528 | p->hooks.deallocate(p->buffer); | ||
529 | p->length = 0; | ||
530 | p->buffer = NULL; | ||
531 | |||
532 | return NULL; | ||
533 | } | ||
534 | |||
535 | memcpy(newbuffer, p->buffer, p->offset + 1); | ||
536 | p->hooks.deallocate(p->buffer); | ||
537 | } | ||
538 | p->length = newsize; | ||
539 | p->buffer = newbuffer; | ||
540 | |||
541 | return newbuffer + p->offset; | ||
542 | } | ||
543 | |||
544 | /* calculate the new length of the string in a printbuffer and update the offset */ | ||
545 | static void update_offset(printbuffer * const buffer) | ||
546 | { | ||
547 | const unsigned char *buffer_pointer = NULL; | ||
548 | if ((buffer == NULL) || (buffer->buffer == NULL)) | ||
549 | { | ||
550 | return; | ||
551 | } | ||
552 | buffer_pointer = buffer->buffer + buffer->offset; | ||
553 | |||
554 | buffer->offset += strlen((const char*)buffer_pointer); | ||
555 | } | ||
556 | |||
557 | /* securely comparison of floating-point variables */ | ||
558 | static cJSON_bool compare_double(double a, double b) | ||
559 | { | ||
560 | double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); | ||
561 | return (fabs(a - b) <= maxVal * DBL_EPSILON); | ||
562 | } | ||
563 | |||
564 | /* Render the number nicely from the given item into a string. */ | ||
565 | static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) | ||
566 | { | ||
567 | unsigned char *output_pointer = NULL; | ||
568 | double d = item->valuedouble; | ||
569 | int length = 0; | ||
570 | size_t i = 0; | ||
571 | unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ | ||
572 | unsigned char decimal_point = get_decimal_point(); | ||
573 | double test = 0.0; | ||
574 | |||
575 | if (output_buffer == NULL) | ||
576 | { | ||
577 | return false; | ||
578 | } | ||
579 | |||
580 | /* This checks for NaN and Infinity */ | ||
581 | if (isnan(d) || isinf(d)) | ||
582 | { | ||
583 | length = sprintf((char*)number_buffer, "null"); | ||
584 | } | ||
585 | else if(d == (double)item->valueint) | ||
586 | { | ||
587 | length = sprintf((char*)number_buffer, "%d", item->valueint); | ||
588 | } | ||
589 | else | ||
590 | { | ||
591 | /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ | ||
592 | length = sprintf((char*)number_buffer, "%1.15g", d); | ||
593 | |||
594 | /* Check whether the original double can be recovered */ | ||
595 | if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d)) | ||
596 | { | ||
597 | /* If not, print with 17 decimal places of precision */ | ||
598 | length = sprintf((char*)number_buffer, "%1.17g", d); | ||
599 | } | ||
600 | } | ||
601 | |||
602 | /* sprintf failed or buffer overrun occurred */ | ||
603 | if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) | ||
604 | { | ||
605 | return false; | ||
606 | } | ||
607 | |||
608 | /* reserve appropriate space in the output */ | ||
609 | output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); | ||
610 | if (output_pointer == NULL) | ||
611 | { | ||
612 | return false; | ||
613 | } | ||
614 | |||
615 | /* copy the printed number to the output and replace locale | ||
616 | * dependent decimal point with '.' */ | ||
617 | for (i = 0; i < ((size_t)length); i++) | ||
618 | { | ||
619 | if (number_buffer[i] == decimal_point) | ||
620 | { | ||
621 | output_pointer[i] = '.'; | ||
622 | continue; | ||
623 | } | ||
624 | |||
625 | output_pointer[i] = number_buffer[i]; | ||
626 | } | ||
627 | output_pointer[i] = '\0'; | ||
628 | |||
629 | output_buffer->offset += (size_t)length; | ||
630 | |||
631 | return true; | ||
632 | } | ||
633 | |||
634 | /* parse 4 digit hexadecimal number */ | ||
635 | static unsigned parse_hex4(const unsigned char * const input) | ||
636 | { | ||
637 | unsigned int h = 0; | ||
638 | size_t i = 0; | ||
639 | |||
640 | for (i = 0; i < 4; i++) | ||
641 | { | ||
642 | /* parse digit */ | ||
643 | if ((input[i] >= '0') && (input[i] <= '9')) | ||
644 | { | ||
645 | h += (unsigned int) input[i] - '0'; | ||
646 | } | ||
647 | else if ((input[i] >= 'A') && (input[i] <= 'F')) | ||
648 | { | ||
649 | h += (unsigned int) 10 + input[i] - 'A'; | ||
650 | } | ||
651 | else if ((input[i] >= 'a') && (input[i] <= 'f')) | ||
652 | { | ||
653 | h += (unsigned int) 10 + input[i] - 'a'; | ||
654 | } | ||
655 | else /* invalid */ | ||
656 | { | ||
657 | return 0; | ||
658 | } | ||
659 | |||
660 | if (i < 3) | ||
661 | { | ||
662 | /* shift left to make place for the next nibble */ | ||
663 | h = h << 4; | ||
664 | } | ||
665 | } | ||
666 | |||
667 | return h; | ||
668 | } | ||
669 | |||
670 | /* converts a UTF-16 literal to UTF-8 | ||
671 | * A literal can be one or two sequences of the form \uXXXX */ | ||
672 | static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer) | ||
673 | { | ||
674 | long unsigned int codepoint = 0; | ||
675 | unsigned int first_code = 0; | ||
676 | const unsigned char *first_sequence = input_pointer; | ||
677 | unsigned char utf8_length = 0; | ||
678 | unsigned char utf8_position = 0; | ||
679 | unsigned char sequence_length = 0; | ||
680 | unsigned char first_byte_mark = 0; | ||
681 | |||
682 | if ((input_end - first_sequence) < 6) | ||
683 | { | ||
684 | /* input ends unexpectedly */ | ||
685 | goto fail; | ||
686 | } | ||
687 | |||
688 | /* get the first utf16 sequence */ | ||
689 | first_code = parse_hex4(first_sequence + 2); | ||
690 | |||
691 | /* check that the code is valid */ | ||
692 | if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) | ||
693 | { | ||
694 | goto fail; | ||
695 | } | ||
696 | |||
697 | /* UTF16 surrogate pair */ | ||
698 | if ((first_code >= 0xD800) && (first_code <= 0xDBFF)) | ||
699 | { | ||
700 | const unsigned char *second_sequence = first_sequence + 6; | ||
701 | unsigned int second_code = 0; | ||
702 | sequence_length = 12; /* \uXXXX\uXXXX */ | ||
703 | |||
704 | if ((input_end - second_sequence) < 6) | ||
705 | { | ||
706 | /* input ends unexpectedly */ | ||
707 | goto fail; | ||
708 | } | ||
709 | |||
710 | if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u')) | ||
711 | { | ||
712 | /* missing second half of the surrogate pair */ | ||
713 | goto fail; | ||
714 | } | ||
715 | |||
716 | /* get the second utf16 sequence */ | ||
717 | second_code = parse_hex4(second_sequence + 2); | ||
718 | /* check that the code is valid */ | ||
719 | if ((second_code < 0xDC00) || (second_code > 0xDFFF)) | ||
720 | { | ||
721 | /* invalid second half of the surrogate pair */ | ||
722 | goto fail; | ||
723 | } | ||
724 | |||
725 | |||
726 | /* calculate the unicode codepoint from the surrogate pair */ | ||
727 | codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); | ||
728 | } | ||
729 | else | ||
730 | { | ||
731 | sequence_length = 6; /* \uXXXX */ | ||
732 | codepoint = first_code; | ||
733 | } | ||
734 | |||
735 | /* encode as UTF-8 | ||
736 | * takes at maximum 4 bytes to encode: | ||
737 | * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ | ||
738 | if (codepoint < 0x80) | ||
739 | { | ||
740 | /* normal ascii, encoding 0xxxxxxx */ | ||
741 | utf8_length = 1; | ||
742 | } | ||
743 | else if (codepoint < 0x800) | ||
744 | { | ||
745 | /* two bytes, encoding 110xxxxx 10xxxxxx */ | ||
746 | utf8_length = 2; | ||
747 | first_byte_mark = 0xC0; /* 11000000 */ | ||
748 | } | ||
749 | else if (codepoint < 0x10000) | ||
750 | { | ||
751 | /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */ | ||
752 | utf8_length = 3; | ||
753 | first_byte_mark = 0xE0; /* 11100000 */ | ||
754 | } | ||
755 | else if (codepoint <= 0x10FFFF) | ||
756 | { | ||
757 | /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */ | ||
758 | utf8_length = 4; | ||
759 | first_byte_mark = 0xF0; /* 11110000 */ | ||
760 | } | ||
761 | else | ||
762 | { | ||
763 | /* invalid unicode codepoint */ | ||
764 | goto fail; | ||
765 | } | ||
766 | |||
767 | /* encode as utf8 */ | ||
768 | for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) | ||
769 | { | ||
770 | /* 10xxxxxx */ | ||
771 | (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF); | ||
772 | codepoint >>= 6; | ||
773 | } | ||
774 | /* encode first byte */ | ||
775 | if (utf8_length > 1) | ||
776 | { | ||
777 | (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF); | ||
778 | } | ||
779 | else | ||
780 | { | ||
781 | (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F); | ||
782 | } | ||
783 | |||
784 | *output_pointer += utf8_length; | ||
785 | |||
786 | return sequence_length; | ||
787 | |||
788 | fail: | ||
789 | return 0; | ||
790 | } | ||
791 | |||
792 | /* Parse the input text into an unescaped cinput, and populate item. */ | ||
793 | static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) | ||
794 | { | ||
795 | const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; | ||
796 | const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; | ||
797 | unsigned char *output_pointer = NULL; | ||
798 | unsigned char *output = NULL; | ||
799 | |||
800 | /* not a string */ | ||
801 | if (buffer_at_offset(input_buffer)[0] != '\"') | ||
802 | { | ||
803 | goto fail; | ||
804 | } | ||
805 | |||
806 | { | ||
807 | /* calculate approximate size of the output (overestimate) */ | ||
808 | size_t allocation_length = 0; | ||
809 | size_t skipped_bytes = 0; | ||
810 | while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) | ||
811 | { | ||
812 | /* is escape sequence */ | ||
813 | if (input_end[0] == '\\') | ||
814 | { | ||
815 | if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) | ||
816 | { | ||
817 | /* prevent buffer overflow when last input character is a backslash */ | ||
818 | goto fail; | ||
819 | } | ||
820 | skipped_bytes++; | ||
821 | input_end++; | ||
822 | } | ||
823 | input_end++; | ||
824 | } | ||
825 | if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) | ||
826 | { | ||
827 | goto fail; /* string ended unexpectedly */ | ||
828 | } | ||
829 | |||
830 | /* This is at most how much we need for the output */ | ||
831 | allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; | ||
832 | output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); | ||
833 | if (output == NULL) | ||
834 | { | ||
835 | goto fail; /* allocation failure */ | ||
836 | } | ||
837 | } | ||
838 | |||
839 | output_pointer = output; | ||
840 | /* loop through the string literal */ | ||
841 | while (input_pointer < input_end) | ||
842 | { | ||
843 | if (*input_pointer != '\\') | ||
844 | { | ||
845 | *output_pointer++ = *input_pointer++; | ||
846 | } | ||
847 | /* escape sequence */ | ||
848 | else | ||
849 | { | ||
850 | unsigned char sequence_length = 2; | ||
851 | if ((input_end - input_pointer) < 1) | ||
852 | { | ||
853 | goto fail; | ||
854 | } | ||
855 | |||
856 | switch (input_pointer[1]) | ||
857 | { | ||
858 | case 'b': | ||
859 | *output_pointer++ = '\b'; | ||
860 | break; | ||
861 | case 'f': | ||
862 | *output_pointer++ = '\f'; | ||
863 | break; | ||
864 | case 'n': | ||
865 | *output_pointer++ = '\n'; | ||
866 | break; | ||
867 | case 'r': | ||
868 | *output_pointer++ = '\r'; | ||
869 | break; | ||
870 | case 't': | ||
871 | *output_pointer++ = '\t'; | ||
872 | break; | ||
873 | case '\"': | ||
874 | case '\\': | ||
875 | case '/': | ||
876 | *output_pointer++ = input_pointer[1]; | ||
877 | break; | ||
878 | |||
879 | /* UTF-16 literal */ | ||
880 | case 'u': | ||
881 | sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); | ||
882 | if (sequence_length == 0) | ||
883 | { | ||
884 | /* failed to convert UTF16-literal to UTF-8 */ | ||
885 | goto fail; | ||
886 | } | ||
887 | break; | ||
888 | |||
889 | default: | ||
890 | goto fail; | ||
891 | } | ||
892 | input_pointer += sequence_length; | ||
893 | } | ||
894 | } | ||
895 | |||
896 | /* zero terminate the output */ | ||
897 | *output_pointer = '\0'; | ||
898 | |||
899 | item->type = cJSON_String; | ||
900 | item->valuestring = (char*)output; | ||
901 | |||
902 | input_buffer->offset = (size_t) (input_end - input_buffer->content); | ||
903 | input_buffer->offset++; | ||
904 | |||
905 | return true; | ||
906 | |||
907 | fail: | ||
908 | if (output != NULL) | ||
909 | { | ||
910 | input_buffer->hooks.deallocate(output); | ||
911 | output = NULL; | ||
912 | } | ||
913 | |||
914 | if (input_pointer != NULL) | ||
915 | { | ||
916 | input_buffer->offset = (size_t)(input_pointer - input_buffer->content); | ||
917 | } | ||
918 | |||
919 | return false; | ||
920 | } | ||
921 | |||
922 | /* Render the cstring provided to an escaped version that can be printed. */ | ||
923 | static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) | ||
924 | { | ||
925 | const unsigned char *input_pointer = NULL; | ||
926 | unsigned char *output = NULL; | ||
927 | unsigned char *output_pointer = NULL; | ||
928 | size_t output_length = 0; | ||
929 | /* numbers of additional characters needed for escaping */ | ||
930 | size_t escape_characters = 0; | ||
931 | |||
932 | if (output_buffer == NULL) | ||
933 | { | ||
934 | return false; | ||
935 | } | ||
936 | |||
937 | /* empty string */ | ||
938 | if (input == NULL) | ||
939 | { | ||
940 | output = ensure(output_buffer, sizeof("\"\"")); | ||
941 | if (output == NULL) | ||
942 | { | ||
943 | return false; | ||
944 | } | ||
945 | strcpy((char*)output, "\"\""); | ||
946 | |||
947 | return true; | ||
948 | } | ||
949 | |||
950 | /* set "flag" to 1 if something needs to be escaped */ | ||
951 | for (input_pointer = input; *input_pointer; input_pointer++) | ||
952 | { | ||
953 | switch (*input_pointer) | ||
954 | { | ||
955 | case '\"': | ||
956 | case '\\': | ||
957 | case '\b': | ||
958 | case '\f': | ||
959 | case '\n': | ||
960 | case '\r': | ||
961 | case '\t': | ||
962 | /* one character escape sequence */ | ||
963 | escape_characters++; | ||
964 | break; | ||
965 | default: | ||
966 | if (*input_pointer < 32) | ||
967 | { | ||
968 | /* UTF-16 escape sequence uXXXX */ | ||
969 | escape_characters += 5; | ||
970 | } | ||
971 | break; | ||
972 | } | ||
973 | } | ||
974 | output_length = (size_t)(input_pointer - input) + escape_characters; | ||
975 | |||
976 | output = ensure(output_buffer, output_length + sizeof("\"\"")); | ||
977 | if (output == NULL) | ||
978 | { | ||
979 | return false; | ||
980 | } | ||
981 | |||
982 | /* no characters have to be escaped */ | ||
983 | if (escape_characters == 0) | ||
984 | { | ||
985 | output[0] = '\"'; | ||
986 | memcpy(output + 1, input, output_length); | ||
987 | output[output_length + 1] = '\"'; | ||
988 | output[output_length + 2] = '\0'; | ||
989 | |||
990 | return true; | ||
991 | } | ||
992 | |||
993 | output[0] = '\"'; | ||
994 | output_pointer = output + 1; | ||
995 | /* copy the string */ | ||
996 | for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++) | ||
997 | { | ||
998 | if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\')) | ||
999 | { | ||
1000 | /* normal character, copy */ | ||
1001 | *output_pointer = *input_pointer; | ||
1002 | } | ||
1003 | else | ||
1004 | { | ||
1005 | /* character needs to be escaped */ | ||
1006 | *output_pointer++ = '\\'; | ||
1007 | switch (*input_pointer) | ||
1008 | { | ||
1009 | case '\\': | ||
1010 | *output_pointer = '\\'; | ||
1011 | break; | ||
1012 | case '\"': | ||
1013 | *output_pointer = '\"'; | ||
1014 | break; | ||
1015 | case '\b': | ||
1016 | *output_pointer = 'b'; | ||
1017 | break; | ||
1018 | case '\f': | ||
1019 | *output_pointer = 'f'; | ||
1020 | break; | ||
1021 | case '\n': | ||
1022 | *output_pointer = 'n'; | ||
1023 | break; | ||
1024 | case '\r': | ||
1025 | *output_pointer = 'r'; | ||
1026 | break; | ||
1027 | case '\t': | ||
1028 | *output_pointer = 't'; | ||
1029 | break; | ||
1030 | default: | ||
1031 | /* escape and print as unicode codepoint */ | ||
1032 | sprintf((char*)output_pointer, "u%04x", *input_pointer); | ||
1033 | output_pointer += 4; | ||
1034 | break; | ||
1035 | } | ||
1036 | } | ||
1037 | } | ||
1038 | output[output_length + 1] = '\"'; | ||
1039 | output[output_length + 2] = '\0'; | ||
1040 | |||
1041 | return true; | ||
1042 | } | ||
1043 | |||
1044 | /* Invoke print_string_ptr (which is useful) on an item. */ | ||
1045 | static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) | ||
1046 | { | ||
1047 | return print_string_ptr((unsigned char*)item->valuestring, p); | ||
1048 | } | ||
1049 | |||
1050 | /* Predeclare these prototypes. */ | ||
1051 | static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer); | ||
1052 | static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer); | ||
1053 | static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer); | ||
1054 | static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer); | ||
1055 | static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer); | ||
1056 | static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer); | ||
1057 | |||
1058 | /* Utility to jump whitespace and cr/lf */ | ||
1059 | static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) | ||
1060 | { | ||
1061 | if ((buffer == NULL) || (buffer->content == NULL)) | ||
1062 | { | ||
1063 | return NULL; | ||
1064 | } | ||
1065 | |||
1066 | if (cannot_access_at_index(buffer, 0)) | ||
1067 | { | ||
1068 | return buffer; | ||
1069 | } | ||
1070 | |||
1071 | while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) | ||
1072 | { | ||
1073 | buffer->offset++; | ||
1074 | } | ||
1075 | |||
1076 | if (buffer->offset == buffer->length) | ||
1077 | { | ||
1078 | buffer->offset--; | ||
1079 | } | ||
1080 | |||
1081 | return buffer; | ||
1082 | } | ||
1083 | |||
1084 | /* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */ | ||
1085 | static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) | ||
1086 | { | ||
1087 | if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) | ||
1088 | { | ||
1089 | return NULL; | ||
1090 | } | ||
1091 | |||
1092 | if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) | ||
1093 | { | ||
1094 | buffer->offset += 3; | ||
1095 | } | ||
1096 | |||
1097 | return buffer; | ||
1098 | } | ||
1099 | |||
1100 | CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated) | ||
1101 | { | ||
1102 | size_t buffer_length; | ||
1103 | |||
1104 | if (NULL == value) | ||
1105 | { | ||
1106 | return NULL; | ||
1107 | } | ||
1108 | |||
1109 | /* Adding null character size due to require_null_terminated. */ | ||
1110 | buffer_length = strlen(value) + sizeof(""); | ||
1111 | |||
1112 | return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated); | ||
1113 | } | ||
1114 | |||
1115 | /* Parse an object - create a new root, and populate. */ | ||
1116 | CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated) | ||
1117 | { | ||
1118 | parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; | ||
1119 | cJSON *item = NULL; | ||
1120 | |||
1121 | /* reset error position */ | ||
1122 | global_error.json = NULL; | ||
1123 | global_error.position = 0; | ||
1124 | |||
1125 | if (value == NULL || 0 == buffer_length) | ||
1126 | { | ||
1127 | goto fail; | ||
1128 | } | ||
1129 | |||
1130 | buffer.content = (const unsigned char*)value; | ||
1131 | buffer.length = buffer_length; | ||
1132 | buffer.offset = 0; | ||
1133 | buffer.hooks = global_hooks; | ||
1134 | |||
1135 | item = cJSON_New_Item(&global_hooks); | ||
1136 | if (item == NULL) /* memory fail */ | ||
1137 | { | ||
1138 | goto fail; | ||
1139 | } | ||
1140 | |||
1141 | if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) | ||
1142 | { | ||
1143 | /* parse failure. ep is set. */ | ||
1144 | goto fail; | ||
1145 | } | ||
1146 | |||
1147 | /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ | ||
1148 | if (require_null_terminated) | ||
1149 | { | ||
1150 | buffer_skip_whitespace(&buffer); | ||
1151 | if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') | ||
1152 | { | ||
1153 | goto fail; | ||
1154 | } | ||
1155 | } | ||
1156 | if (return_parse_end) | ||
1157 | { | ||
1158 | *return_parse_end = (const char*)buffer_at_offset(&buffer); | ||
1159 | } | ||
1160 | |||
1161 | return item; | ||
1162 | |||
1163 | fail: | ||
1164 | if (item != NULL) | ||
1165 | { | ||
1166 | cJSON_Delete(item); | ||
1167 | } | ||
1168 | |||
1169 | if (value != NULL) | ||
1170 | { | ||
1171 | error local_error; | ||
1172 | local_error.json = (const unsigned char*)value; | ||
1173 | local_error.position = 0; | ||
1174 | |||
1175 | if (buffer.offset < buffer.length) | ||
1176 | { | ||
1177 | local_error.position = buffer.offset; | ||
1178 | } | ||
1179 | else if (buffer.length > 0) | ||
1180 | { | ||
1181 | local_error.position = buffer.length - 1; | ||
1182 | } | ||
1183 | |||
1184 | if (return_parse_end != NULL) | ||
1185 | { | ||
1186 | *return_parse_end = (const char*)local_error.json + local_error.position; | ||
1187 | } | ||
1188 | |||
1189 | global_error = local_error; | ||
1190 | } | ||
1191 | |||
1192 | return NULL; | ||
1193 | } | ||
1194 | |||
1195 | /* Default options for cJSON_Parse */ | ||
1196 | CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) | ||
1197 | { | ||
1198 | return cJSON_ParseWithOpts(value, 0, 0); | ||
1199 | } | ||
1200 | |||
1201 | CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length) | ||
1202 | { | ||
1203 | return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0); | ||
1204 | } | ||
1205 | |||
1206 | #define cjson_min(a, b) (((a) < (b)) ? (a) : (b)) | ||
1207 | |||
1208 | static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks) | ||
1209 | { | ||
1210 | static const size_t default_buffer_size = 256; | ||
1211 | printbuffer buffer[1]; | ||
1212 | unsigned char *printed = NULL; | ||
1213 | |||
1214 | memset(buffer, 0, sizeof(buffer)); | ||
1215 | |||
1216 | /* create buffer */ | ||
1217 | buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size); | ||
1218 | buffer->length = default_buffer_size; | ||
1219 | buffer->format = format; | ||
1220 | buffer->hooks = *hooks; | ||
1221 | if (buffer->buffer == NULL) | ||
1222 | { | ||
1223 | goto fail; | ||
1224 | } | ||
1225 | |||
1226 | /* print the value */ | ||
1227 | if (!print_value(item, buffer)) | ||
1228 | { | ||
1229 | goto fail; | ||
1230 | } | ||
1231 | update_offset(buffer); | ||
1232 | |||
1233 | /* check if reallocate is available */ | ||
1234 | if (hooks->reallocate != NULL) | ||
1235 | { | ||
1236 | printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1); | ||
1237 | if (printed == NULL) { | ||
1238 | goto fail; | ||
1239 | } | ||
1240 | buffer->buffer = NULL; | ||
1241 | } | ||
1242 | else /* otherwise copy the JSON over to a new buffer */ | ||
1243 | { | ||
1244 | printed = (unsigned char*) hooks->allocate(buffer->offset + 1); | ||
1245 | if (printed == NULL) | ||
1246 | { | ||
1247 | goto fail; | ||
1248 | } | ||
1249 | memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); | ||
1250 | printed[buffer->offset] = '\0'; /* just to be sure */ | ||
1251 | |||
1252 | /* free the buffer */ | ||
1253 | hooks->deallocate(buffer->buffer); | ||
1254 | buffer->buffer = NULL; | ||
1255 | } | ||
1256 | |||
1257 | return printed; | ||
1258 | |||
1259 | fail: | ||
1260 | if (buffer->buffer != NULL) | ||
1261 | { | ||
1262 | hooks->deallocate(buffer->buffer); | ||
1263 | buffer->buffer = NULL; | ||
1264 | } | ||
1265 | |||
1266 | if (printed != NULL) | ||
1267 | { | ||
1268 | hooks->deallocate(printed); | ||
1269 | printed = NULL; | ||
1270 | } | ||
1271 | |||
1272 | return NULL; | ||
1273 | } | ||
1274 | |||
1275 | /* Render a cJSON item/entity/structure to text. */ | ||
1276 | CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) | ||
1277 | { | ||
1278 | return (char*)print(item, true, &global_hooks); | ||
1279 | } | ||
1280 | |||
1281 | CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) | ||
1282 | { | ||
1283 | return (char*)print(item, false, &global_hooks); | ||
1284 | } | ||
1285 | |||
1286 | CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) | ||
1287 | { | ||
1288 | printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; | ||
1289 | |||
1290 | if (prebuffer < 0) | ||
1291 | { | ||
1292 | return NULL; | ||
1293 | } | ||
1294 | |||
1295 | p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer); | ||
1296 | if (!p.buffer) | ||
1297 | { | ||
1298 | return NULL; | ||
1299 | } | ||
1300 | |||
1301 | p.length = (size_t)prebuffer; | ||
1302 | p.offset = 0; | ||
1303 | p.noalloc = false; | ||
1304 | p.format = fmt; | ||
1305 | p.hooks = global_hooks; | ||
1306 | |||
1307 | if (!print_value(item, &p)) | ||
1308 | { | ||
1309 | global_hooks.deallocate(p.buffer); | ||
1310 | p.buffer = NULL; | ||
1311 | return NULL; | ||
1312 | } | ||
1313 | |||
1314 | return (char*)p.buffer; | ||
1315 | } | ||
1316 | |||
1317 | CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format) | ||
1318 | { | ||
1319 | printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; | ||
1320 | |||
1321 | if ((length < 0) || (buffer == NULL)) | ||
1322 | { | ||
1323 | return false; | ||
1324 | } | ||
1325 | |||
1326 | p.buffer = (unsigned char*)buffer; | ||
1327 | p.length = (size_t)length; | ||
1328 | p.offset = 0; | ||
1329 | p.noalloc = true; | ||
1330 | p.format = format; | ||
1331 | p.hooks = global_hooks; | ||
1332 | |||
1333 | return print_value(item, &p); | ||
1334 | } | ||
1335 | |||
1336 | /* Parser core - when encountering text, process appropriately. */ | ||
1337 | static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer) | ||
1338 | { | ||
1339 | if ((input_buffer == NULL) || (input_buffer->content == NULL)) | ||
1340 | { | ||
1341 | return false; /* no input */ | ||
1342 | } | ||
1343 | |||
1344 | /* parse the different types of values */ | ||
1345 | /* null */ | ||
1346 | if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0)) | ||
1347 | { | ||
1348 | item->type = cJSON_NULL; | ||
1349 | input_buffer->offset += 4; | ||
1350 | return true; | ||
1351 | } | ||
1352 | /* false */ | ||
1353 | if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0)) | ||
1354 | { | ||
1355 | item->type = cJSON_False; | ||
1356 | input_buffer->offset += 5; | ||
1357 | return true; | ||
1358 | } | ||
1359 | /* true */ | ||
1360 | if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0)) | ||
1361 | { | ||
1362 | item->type = cJSON_True; | ||
1363 | item->valueint = 1; | ||
1364 | input_buffer->offset += 4; | ||
1365 | return true; | ||
1366 | } | ||
1367 | /* string */ | ||
1368 | if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) | ||
1369 | { | ||
1370 | return parse_string(item, input_buffer); | ||
1371 | } | ||
1372 | /* number */ | ||
1373 | if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) | ||
1374 | { | ||
1375 | return parse_number(item, input_buffer); | ||
1376 | } | ||
1377 | /* array */ | ||
1378 | if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) | ||
1379 | { | ||
1380 | return parse_array(item, input_buffer); | ||
1381 | } | ||
1382 | /* object */ | ||
1383 | if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) | ||
1384 | { | ||
1385 | return parse_object(item, input_buffer); | ||
1386 | } | ||
1387 | |||
1388 | return false; | ||
1389 | } | ||
1390 | |||
1391 | /* Render a value to text. */ | ||
1392 | static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer) | ||
1393 | { | ||
1394 | unsigned char *output = NULL; | ||
1395 | |||
1396 | if ((item == NULL) || (output_buffer == NULL)) | ||
1397 | { | ||
1398 | return false; | ||
1399 | } | ||
1400 | |||
1401 | switch ((item->type) & 0xFF) | ||
1402 | { | ||
1403 | case cJSON_NULL: | ||
1404 | output = ensure(output_buffer, 5); | ||
1405 | if (output == NULL) | ||
1406 | { | ||
1407 | return false; | ||
1408 | } | ||
1409 | strcpy((char*)output, "null"); | ||
1410 | return true; | ||
1411 | |||
1412 | case cJSON_False: | ||
1413 | output = ensure(output_buffer, 6); | ||
1414 | if (output == NULL) | ||
1415 | { | ||
1416 | return false; | ||
1417 | } | ||
1418 | strcpy((char*)output, "false"); | ||
1419 | return true; | ||
1420 | |||
1421 | case cJSON_True: | ||
1422 | output = ensure(output_buffer, 5); | ||
1423 | if (output == NULL) | ||
1424 | { | ||
1425 | return false; | ||
1426 | } | ||
1427 | strcpy((char*)output, "true"); | ||
1428 | return true; | ||
1429 | |||
1430 | case cJSON_Number: | ||
1431 | return print_number(item, output_buffer); | ||
1432 | |||
1433 | case cJSON_Raw: | ||
1434 | { | ||
1435 | size_t raw_length = 0; | ||
1436 | if (item->valuestring == NULL) | ||
1437 | { | ||
1438 | return false; | ||
1439 | } | ||
1440 | |||
1441 | raw_length = strlen(item->valuestring) + sizeof(""); | ||
1442 | output = ensure(output_buffer, raw_length); | ||
1443 | if (output == NULL) | ||
1444 | { | ||
1445 | return false; | ||
1446 | } | ||
1447 | memcpy(output, item->valuestring, raw_length); | ||
1448 | return true; | ||
1449 | } | ||
1450 | |||
1451 | case cJSON_String: | ||
1452 | return print_string(item, output_buffer); | ||
1453 | |||
1454 | case cJSON_Array: | ||
1455 | return print_array(item, output_buffer); | ||
1456 | |||
1457 | case cJSON_Object: | ||
1458 | return print_object(item, output_buffer); | ||
1459 | |||
1460 | default: | ||
1461 | return false; | ||
1462 | } | ||
1463 | } | ||
1464 | |||
1465 | /* Build an array from input text. */ | ||
1466 | static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer) | ||
1467 | { | ||
1468 | cJSON *head = NULL; /* head of the linked list */ | ||
1469 | cJSON *current_item = NULL; | ||
1470 | |||
1471 | if (input_buffer->depth >= CJSON_NESTING_LIMIT) | ||
1472 | { | ||
1473 | return false; /* to deeply nested */ | ||
1474 | } | ||
1475 | input_buffer->depth++; | ||
1476 | |||
1477 | if (buffer_at_offset(input_buffer)[0] != '[') | ||
1478 | { | ||
1479 | /* not an array */ | ||
1480 | goto fail; | ||
1481 | } | ||
1482 | |||
1483 | input_buffer->offset++; | ||
1484 | buffer_skip_whitespace(input_buffer); | ||
1485 | if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) | ||
1486 | { | ||
1487 | /* empty array */ | ||
1488 | goto success; | ||
1489 | } | ||
1490 | |||
1491 | /* check if we skipped to the end of the buffer */ | ||
1492 | if (cannot_access_at_index(input_buffer, 0)) | ||
1493 | { | ||
1494 | input_buffer->offset--; | ||
1495 | goto fail; | ||
1496 | } | ||
1497 | |||
1498 | /* step back to character in front of the first element */ | ||
1499 | input_buffer->offset--; | ||
1500 | /* loop through the comma separated array elements */ | ||
1501 | do | ||
1502 | { | ||
1503 | /* allocate next item */ | ||
1504 | cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); | ||
1505 | if (new_item == NULL) | ||
1506 | { | ||
1507 | goto fail; /* allocation failure */ | ||
1508 | } | ||
1509 | |||
1510 | /* attach next item to list */ | ||
1511 | if (head == NULL) | ||
1512 | { | ||
1513 | /* start the linked list */ | ||
1514 | current_item = head = new_item; | ||
1515 | } | ||
1516 | else | ||
1517 | { | ||
1518 | /* add to the end and advance */ | ||
1519 | current_item->next = new_item; | ||
1520 | new_item->prev = current_item; | ||
1521 | current_item = new_item; | ||
1522 | } | ||
1523 | |||
1524 | /* parse next value */ | ||
1525 | input_buffer->offset++; | ||
1526 | buffer_skip_whitespace(input_buffer); | ||
1527 | if (!parse_value(current_item, input_buffer)) | ||
1528 | { | ||
1529 | goto fail; /* failed to parse value */ | ||
1530 | } | ||
1531 | buffer_skip_whitespace(input_buffer); | ||
1532 | } | ||
1533 | while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); | ||
1534 | |||
1535 | if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') | ||
1536 | { | ||
1537 | goto fail; /* expected end of array */ | ||
1538 | } | ||
1539 | |||
1540 | success: | ||
1541 | input_buffer->depth--; | ||
1542 | |||
1543 | if (head != NULL) { | ||
1544 | head->prev = current_item; | ||
1545 | } | ||
1546 | |||
1547 | item->type = cJSON_Array; | ||
1548 | item->child = head; | ||
1549 | |||
1550 | input_buffer->offset++; | ||
1551 | |||
1552 | return true; | ||
1553 | |||
1554 | fail: | ||
1555 | if (head != NULL) | ||
1556 | { | ||
1557 | cJSON_Delete(head); | ||
1558 | } | ||
1559 | |||
1560 | return false; | ||
1561 | } | ||
1562 | |||
1563 | /* Render an array to text */ | ||
1564 | static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer) | ||
1565 | { | ||
1566 | unsigned char *output_pointer = NULL; | ||
1567 | size_t length = 0; | ||
1568 | cJSON *current_element = item->child; | ||
1569 | |||
1570 | if (output_buffer == NULL) | ||
1571 | { | ||
1572 | return false; | ||
1573 | } | ||
1574 | |||
1575 | /* Compose the output array. */ | ||
1576 | /* opening square bracket */ | ||
1577 | output_pointer = ensure(output_buffer, 1); | ||
1578 | if (output_pointer == NULL) | ||
1579 | { | ||
1580 | return false; | ||
1581 | } | ||
1582 | |||
1583 | *output_pointer = '['; | ||
1584 | output_buffer->offset++; | ||
1585 | output_buffer->depth++; | ||
1586 | |||
1587 | while (current_element != NULL) | ||
1588 | { | ||
1589 | if (!print_value(current_element, output_buffer)) | ||
1590 | { | ||
1591 | return false; | ||
1592 | } | ||
1593 | update_offset(output_buffer); | ||
1594 | if (current_element->next) | ||
1595 | { | ||
1596 | length = (size_t) (output_buffer->format ? 2 : 1); | ||
1597 | output_pointer = ensure(output_buffer, length + 1); | ||
1598 | if (output_pointer == NULL) | ||
1599 | { | ||
1600 | return false; | ||
1601 | } | ||
1602 | *output_pointer++ = ','; | ||
1603 | if(output_buffer->format) | ||
1604 | { | ||
1605 | *output_pointer++ = ' '; | ||
1606 | } | ||
1607 | *output_pointer = '\0'; | ||
1608 | output_buffer->offset += length; | ||
1609 | } | ||
1610 | current_element = current_element->next; | ||
1611 | } | ||
1612 | |||
1613 | output_pointer = ensure(output_buffer, 2); | ||
1614 | if (output_pointer == NULL) | ||
1615 | { | ||
1616 | return false; | ||
1617 | } | ||
1618 | *output_pointer++ = ']'; | ||
1619 | *output_pointer = '\0'; | ||
1620 | output_buffer->depth--; | ||
1621 | |||
1622 | return true; | ||
1623 | } | ||
1624 | |||
1625 | /* Build an object from the text. */ | ||
1626 | static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer) | ||
1627 | { | ||
1628 | cJSON *head = NULL; /* linked list head */ | ||
1629 | cJSON *current_item = NULL; | ||
1630 | |||
1631 | if (input_buffer->depth >= CJSON_NESTING_LIMIT) | ||
1632 | { | ||
1633 | return false; /* to deeply nested */ | ||
1634 | } | ||
1635 | input_buffer->depth++; | ||
1636 | |||
1637 | if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{')) | ||
1638 | { | ||
1639 | goto fail; /* not an object */ | ||
1640 | } | ||
1641 | |||
1642 | input_buffer->offset++; | ||
1643 | buffer_skip_whitespace(input_buffer); | ||
1644 | if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) | ||
1645 | { | ||
1646 | goto success; /* empty object */ | ||
1647 | } | ||
1648 | |||
1649 | /* check if we skipped to the end of the buffer */ | ||
1650 | if (cannot_access_at_index(input_buffer, 0)) | ||
1651 | { | ||
1652 | input_buffer->offset--; | ||
1653 | goto fail; | ||
1654 | } | ||
1655 | |||
1656 | /* step back to character in front of the first element */ | ||
1657 | input_buffer->offset--; | ||
1658 | /* loop through the comma separated array elements */ | ||
1659 | do | ||
1660 | { | ||
1661 | /* allocate next item */ | ||
1662 | cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); | ||
1663 | if (new_item == NULL) | ||
1664 | { | ||
1665 | goto fail; /* allocation failure */ | ||
1666 | } | ||
1667 | |||
1668 | /* attach next item to list */ | ||
1669 | if (head == NULL) | ||
1670 | { | ||
1671 | /* start the linked list */ | ||
1672 | current_item = head = new_item; | ||
1673 | } | ||
1674 | else | ||
1675 | { | ||
1676 | /* add to the end and advance */ | ||
1677 | current_item->next = new_item; | ||
1678 | new_item->prev = current_item; | ||
1679 | current_item = new_item; | ||
1680 | } | ||
1681 | |||
1682 | if (cannot_access_at_index(input_buffer, 1)) | ||
1683 | { | ||
1684 | goto fail; /* nothing comes after the comma */ | ||
1685 | } | ||
1686 | |||
1687 | /* parse the name of the child */ | ||
1688 | input_buffer->offset++; | ||
1689 | buffer_skip_whitespace(input_buffer); | ||
1690 | if (!parse_string(current_item, input_buffer)) | ||
1691 | { | ||
1692 | goto fail; /* failed to parse name */ | ||
1693 | } | ||
1694 | buffer_skip_whitespace(input_buffer); | ||
1695 | |||
1696 | /* swap valuestring and string, because we parsed the name */ | ||
1697 | current_item->string = current_item->valuestring; | ||
1698 | current_item->valuestring = NULL; | ||
1699 | |||
1700 | if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':')) | ||
1701 | { | ||
1702 | goto fail; /* invalid object */ | ||
1703 | } | ||
1704 | |||
1705 | /* parse the value */ | ||
1706 | input_buffer->offset++; | ||
1707 | buffer_skip_whitespace(input_buffer); | ||
1708 | if (!parse_value(current_item, input_buffer)) | ||
1709 | { | ||
1710 | goto fail; /* failed to parse value */ | ||
1711 | } | ||
1712 | buffer_skip_whitespace(input_buffer); | ||
1713 | } | ||
1714 | while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); | ||
1715 | |||
1716 | if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) | ||
1717 | { | ||
1718 | goto fail; /* expected end of object */ | ||
1719 | } | ||
1720 | |||
1721 | success: | ||
1722 | input_buffer->depth--; | ||
1723 | |||
1724 | if (head != NULL) { | ||
1725 | head->prev = current_item; | ||
1726 | } | ||
1727 | |||
1728 | item->type = cJSON_Object; | ||
1729 | item->child = head; | ||
1730 | |||
1731 | input_buffer->offset++; | ||
1732 | return true; | ||
1733 | |||
1734 | fail: | ||
1735 | if (head != NULL) | ||
1736 | { | ||
1737 | cJSON_Delete(head); | ||
1738 | } | ||
1739 | |||
1740 | return false; | ||
1741 | } | ||
1742 | |||
1743 | /* Render an object to text. */ | ||
1744 | static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer) | ||
1745 | { | ||
1746 | unsigned char *output_pointer = NULL; | ||
1747 | size_t length = 0; | ||
1748 | cJSON *current_item = item->child; | ||
1749 | |||
1750 | if (output_buffer == NULL) | ||
1751 | { | ||
1752 | return false; | ||
1753 | } | ||
1754 | |||
1755 | /* Compose the output: */ | ||
1756 | length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ | ||
1757 | output_pointer = ensure(output_buffer, length + 1); | ||
1758 | if (output_pointer == NULL) | ||
1759 | { | ||
1760 | return false; | ||
1761 | } | ||
1762 | |||
1763 | *output_pointer++ = '{'; | ||
1764 | output_buffer->depth++; | ||
1765 | if (output_buffer->format) | ||
1766 | { | ||
1767 | *output_pointer++ = '\n'; | ||
1768 | } | ||
1769 | output_buffer->offset += length; | ||
1770 | |||
1771 | while (current_item) | ||
1772 | { | ||
1773 | if (output_buffer->format) | ||
1774 | { | ||
1775 | size_t i; | ||
1776 | output_pointer = ensure(output_buffer, output_buffer->depth); | ||
1777 | if (output_pointer == NULL) | ||
1778 | { | ||
1779 | return false; | ||
1780 | } | ||
1781 | for (i = 0; i < output_buffer->depth; i++) | ||
1782 | { | ||
1783 | *output_pointer++ = '\t'; | ||
1784 | } | ||
1785 | output_buffer->offset += output_buffer->depth; | ||
1786 | } | ||
1787 | |||
1788 | /* print key */ | ||
1789 | if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) | ||
1790 | { | ||
1791 | return false; | ||
1792 | } | ||
1793 | update_offset(output_buffer); | ||
1794 | |||
1795 | length = (size_t) (output_buffer->format ? 2 : 1); | ||
1796 | output_pointer = ensure(output_buffer, length); | ||
1797 | if (output_pointer == NULL) | ||
1798 | { | ||
1799 | return false; | ||
1800 | } | ||
1801 | *output_pointer++ = ':'; | ||
1802 | if (output_buffer->format) | ||
1803 | { | ||
1804 | *output_pointer++ = '\t'; | ||
1805 | } | ||
1806 | output_buffer->offset += length; | ||
1807 | |||
1808 | /* print value */ | ||
1809 | if (!print_value(current_item, output_buffer)) | ||
1810 | { | ||
1811 | return false; | ||
1812 | } | ||
1813 | update_offset(output_buffer); | ||
1814 | |||
1815 | /* print comma if not last */ | ||
1816 | length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); | ||
1817 | output_pointer = ensure(output_buffer, length + 1); | ||
1818 | if (output_pointer == NULL) | ||
1819 | { | ||
1820 | return false; | ||
1821 | } | ||
1822 | if (current_item->next) | ||
1823 | { | ||
1824 | *output_pointer++ = ','; | ||
1825 | } | ||
1826 | |||
1827 | if (output_buffer->format) | ||
1828 | { | ||
1829 | *output_pointer++ = '\n'; | ||
1830 | } | ||
1831 | *output_pointer = '\0'; | ||
1832 | output_buffer->offset += length; | ||
1833 | |||
1834 | current_item = current_item->next; | ||
1835 | } | ||
1836 | |||
1837 | output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); | ||
1838 | if (output_pointer == NULL) | ||
1839 | { | ||
1840 | return false; | ||
1841 | } | ||
1842 | if (output_buffer->format) | ||
1843 | { | ||
1844 | size_t i; | ||
1845 | for (i = 0; i < (output_buffer->depth - 1); i++) | ||
1846 | { | ||
1847 | *output_pointer++ = '\t'; | ||
1848 | } | ||
1849 | } | ||
1850 | *output_pointer++ = '}'; | ||
1851 | *output_pointer = '\0'; | ||
1852 | output_buffer->depth--; | ||
1853 | |||
1854 | return true; | ||
1855 | } | ||
1856 | |||
1857 | /* Get Array size/item / object item. */ | ||
1858 | CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) | ||
1859 | { | ||
1860 | cJSON *child = NULL; | ||
1861 | size_t size = 0; | ||
1862 | |||
1863 | if (array == NULL) | ||
1864 | { | ||
1865 | return 0; | ||
1866 | } | ||
1867 | |||
1868 | child = array->child; | ||
1869 | |||
1870 | while(child != NULL) | ||
1871 | { | ||
1872 | size++; | ||
1873 | child = child->next; | ||
1874 | } | ||
1875 | |||
1876 | /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ | ||
1877 | |||
1878 | return (int)size; | ||
1879 | } | ||
1880 | |||
1881 | static cJSON* get_array_item(const cJSON *array, size_t index) | ||
1882 | { | ||
1883 | cJSON *current_child = NULL; | ||
1884 | |||
1885 | if (array == NULL) | ||
1886 | { | ||
1887 | return NULL; | ||
1888 | } | ||
1889 | |||
1890 | current_child = array->child; | ||
1891 | while ((current_child != NULL) && (index > 0)) | ||
1892 | { | ||
1893 | index--; | ||
1894 | current_child = current_child->next; | ||
1895 | } | ||
1896 | |||
1897 | return current_child; | ||
1898 | } | ||
1899 | |||
1900 | CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) | ||
1901 | { | ||
1902 | if (index < 0) | ||
1903 | { | ||
1904 | return NULL; | ||
1905 | } | ||
1906 | |||
1907 | return get_array_item(array, (size_t)index); | ||
1908 | } | ||
1909 | |||
1910 | static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive) | ||
1911 | { | ||
1912 | cJSON *current_element = NULL; | ||
1913 | |||
1914 | if ((object == NULL) || (name == NULL)) | ||
1915 | { | ||
1916 | return NULL; | ||
1917 | } | ||
1918 | |||
1919 | current_element = object->child; | ||
1920 | if (case_sensitive) | ||
1921 | { | ||
1922 | while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0)) | ||
1923 | { | ||
1924 | current_element = current_element->next; | ||
1925 | } | ||
1926 | } | ||
1927 | else | ||
1928 | { | ||
1929 | while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) | ||
1930 | { | ||
1931 | current_element = current_element->next; | ||
1932 | } | ||
1933 | } | ||
1934 | |||
1935 | if ((current_element == NULL) || (current_element->string == NULL)) { | ||
1936 | return NULL; | ||
1937 | } | ||
1938 | |||
1939 | return current_element; | ||
1940 | } | ||
1941 | |||
1942 | CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) | ||
1943 | { | ||
1944 | return get_object_item(object, string, false); | ||
1945 | } | ||
1946 | |||
1947 | CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string) | ||
1948 | { | ||
1949 | return get_object_item(object, string, true); | ||
1950 | } | ||
1951 | |||
1952 | CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string) | ||
1953 | { | ||
1954 | return cJSON_GetObjectItem(object, string) ? 1 : 0; | ||
1955 | } | ||
1956 | |||
1957 | /* Utility for array list handling. */ | ||
1958 | static void suffix_object(cJSON *prev, cJSON *item) | ||
1959 | { | ||
1960 | prev->next = item; | ||
1961 | item->prev = prev; | ||
1962 | } | ||
1963 | |||
1964 | /* Utility for handling references. */ | ||
1965 | static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks) | ||
1966 | { | ||
1967 | cJSON *reference = NULL; | ||
1968 | if (item == NULL) | ||
1969 | { | ||
1970 | return NULL; | ||
1971 | } | ||
1972 | |||
1973 | reference = cJSON_New_Item(hooks); | ||
1974 | if (reference == NULL) | ||
1975 | { | ||
1976 | return NULL; | ||
1977 | } | ||
1978 | |||
1979 | memcpy(reference, item, sizeof(cJSON)); | ||
1980 | reference->string = NULL; | ||
1981 | reference->type |= cJSON_IsReference; | ||
1982 | reference->next = reference->prev = NULL; | ||
1983 | return reference; | ||
1984 | } | ||
1985 | |||
1986 | static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) | ||
1987 | { | ||
1988 | cJSON *child = NULL; | ||
1989 | |||
1990 | if ((item == NULL) || (array == NULL) || (array == item)) | ||
1991 | { | ||
1992 | return false; | ||
1993 | } | ||
1994 | |||
1995 | child = array->child; | ||
1996 | /* | ||
1997 | * To find the last item in array quickly, we use prev in array | ||
1998 | */ | ||
1999 | if (child == NULL) | ||
2000 | { | ||
2001 | /* list is empty, start new one */ | ||
2002 | array->child = item; | ||
2003 | item->prev = item; | ||
2004 | item->next = NULL; | ||
2005 | } | ||
2006 | else | ||
2007 | { | ||
2008 | /* append to the end */ | ||
2009 | if (child->prev) | ||
2010 | { | ||
2011 | suffix_object(child->prev, item); | ||
2012 | array->child->prev = item; | ||
2013 | } | ||
2014 | } | ||
2015 | |||
2016 | return true; | ||
2017 | } | ||
2018 | |||
2019 | /* Add item to array/object. */ | ||
2020 | CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) | ||
2021 | { | ||
2022 | return add_item_to_array(array, item); | ||
2023 | } | ||
2024 | |||
2025 | #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) | ||
2026 | #pragma GCC diagnostic push | ||
2027 | #endif | ||
2028 | #ifdef __GNUC__ | ||
2029 | #pragma GCC diagnostic ignored "-Wcast-qual" | ||
2030 | #endif | ||
2031 | /* helper function to cast away const */ | ||
2032 | static void* cast_away_const(const void* string) | ||
2033 | { | ||
2034 | return (void*)string; | ||
2035 | } | ||
2036 | #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) | ||
2037 | #pragma GCC diagnostic pop | ||
2038 | #endif | ||
2039 | |||
2040 | |||
2041 | static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key) | ||
2042 | { | ||
2043 | char *new_key = NULL; | ||
2044 | int new_type = cJSON_Invalid; | ||
2045 | |||
2046 | if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) | ||
2047 | { | ||
2048 | return false; | ||
2049 | } | ||
2050 | |||
2051 | if (constant_key) | ||
2052 | { | ||
2053 | new_key = (char*)cast_away_const(string); | ||
2054 | new_type = item->type | cJSON_StringIsConst; | ||
2055 | } | ||
2056 | else | ||
2057 | { | ||
2058 | new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks); | ||
2059 | if (new_key == NULL) | ||
2060 | { | ||
2061 | return false; | ||
2062 | } | ||
2063 | |||
2064 | new_type = item->type & ~cJSON_StringIsConst; | ||
2065 | } | ||
2066 | |||
2067 | if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) | ||
2068 | { | ||
2069 | hooks->deallocate(item->string); | ||
2070 | } | ||
2071 | |||
2072 | item->string = new_key; | ||
2073 | item->type = new_type; | ||
2074 | |||
2075 | return add_item_to_array(object, item); | ||
2076 | } | ||
2077 | |||
2078 | CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) | ||
2079 | { | ||
2080 | return add_item_to_object(object, string, item, &global_hooks, false); | ||
2081 | } | ||
2082 | |||
2083 | /* Add an item to an object with constant string as key */ | ||
2084 | CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) | ||
2085 | { | ||
2086 | return add_item_to_object(object, string, item, &global_hooks, true); | ||
2087 | } | ||
2088 | |||
2089 | CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) | ||
2090 | { | ||
2091 | if (array == NULL) | ||
2092 | { | ||
2093 | return false; | ||
2094 | } | ||
2095 | |||
2096 | return add_item_to_array(array, create_reference(item, &global_hooks)); | ||
2097 | } | ||
2098 | |||
2099 | CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) | ||
2100 | { | ||
2101 | if ((object == NULL) || (string == NULL)) | ||
2102 | { | ||
2103 | return false; | ||
2104 | } | ||
2105 | |||
2106 | return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); | ||
2107 | } | ||
2108 | |||
2109 | CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name) | ||
2110 | { | ||
2111 | cJSON *null = cJSON_CreateNull(); | ||
2112 | if (add_item_to_object(object, name, null, &global_hooks, false)) | ||
2113 | { | ||
2114 | return null; | ||
2115 | } | ||
2116 | |||
2117 | cJSON_Delete(null); | ||
2118 | return NULL; | ||
2119 | } | ||
2120 | |||
2121 | CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name) | ||
2122 | { | ||
2123 | cJSON *true_item = cJSON_CreateTrue(); | ||
2124 | if (add_item_to_object(object, name, true_item, &global_hooks, false)) | ||
2125 | { | ||
2126 | return true_item; | ||
2127 | } | ||
2128 | |||
2129 | cJSON_Delete(true_item); | ||
2130 | return NULL; | ||
2131 | } | ||
2132 | |||
2133 | CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name) | ||
2134 | { | ||
2135 | cJSON *false_item = cJSON_CreateFalse(); | ||
2136 | if (add_item_to_object(object, name, false_item, &global_hooks, false)) | ||
2137 | { | ||
2138 | return false_item; | ||
2139 | } | ||
2140 | |||
2141 | cJSON_Delete(false_item); | ||
2142 | return NULL; | ||
2143 | } | ||
2144 | |||
2145 | CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean) | ||
2146 | { | ||
2147 | cJSON *bool_item = cJSON_CreateBool(boolean); | ||
2148 | if (add_item_to_object(object, name, bool_item, &global_hooks, false)) | ||
2149 | { | ||
2150 | return bool_item; | ||
2151 | } | ||
2152 | |||
2153 | cJSON_Delete(bool_item); | ||
2154 | return NULL; | ||
2155 | } | ||
2156 | |||
2157 | CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number) | ||
2158 | { | ||
2159 | cJSON *number_item = cJSON_CreateNumber(number); | ||
2160 | if (add_item_to_object(object, name, number_item, &global_hooks, false)) | ||
2161 | { | ||
2162 | return number_item; | ||
2163 | } | ||
2164 | |||
2165 | cJSON_Delete(number_item); | ||
2166 | return NULL; | ||
2167 | } | ||
2168 | |||
2169 | CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string) | ||
2170 | { | ||
2171 | cJSON *string_item = cJSON_CreateString(string); | ||
2172 | if (add_item_to_object(object, name, string_item, &global_hooks, false)) | ||
2173 | { | ||
2174 | return string_item; | ||
2175 | } | ||
2176 | |||
2177 | cJSON_Delete(string_item); | ||
2178 | return NULL; | ||
2179 | } | ||
2180 | |||
2181 | CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw) | ||
2182 | { | ||
2183 | cJSON *raw_item = cJSON_CreateRaw(raw); | ||
2184 | if (add_item_to_object(object, name, raw_item, &global_hooks, false)) | ||
2185 | { | ||
2186 | return raw_item; | ||
2187 | } | ||
2188 | |||
2189 | cJSON_Delete(raw_item); | ||
2190 | return NULL; | ||
2191 | } | ||
2192 | |||
2193 | CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name) | ||
2194 | { | ||
2195 | cJSON *object_item = cJSON_CreateObject(); | ||
2196 | if (add_item_to_object(object, name, object_item, &global_hooks, false)) | ||
2197 | { | ||
2198 | return object_item; | ||
2199 | } | ||
2200 | |||
2201 | cJSON_Delete(object_item); | ||
2202 | return NULL; | ||
2203 | } | ||
2204 | |||
2205 | CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name) | ||
2206 | { | ||
2207 | cJSON *array = cJSON_CreateArray(); | ||
2208 | if (add_item_to_object(object, name, array, &global_hooks, false)) | ||
2209 | { | ||
2210 | return array; | ||
2211 | } | ||
2212 | |||
2213 | cJSON_Delete(array); | ||
2214 | return NULL; | ||
2215 | } | ||
2216 | |||
2217 | CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) | ||
2218 | { | ||
2219 | if ((parent == NULL) || (item == NULL) || (item != parent->child && item->prev == NULL)) | ||
2220 | { | ||
2221 | return NULL; | ||
2222 | } | ||
2223 | |||
2224 | if (item != parent->child) | ||
2225 | { | ||
2226 | /* not the first element */ | ||
2227 | item->prev->next = item->next; | ||
2228 | } | ||
2229 | if (item->next != NULL) | ||
2230 | { | ||
2231 | /* not the last element */ | ||
2232 | item->next->prev = item->prev; | ||
2233 | } | ||
2234 | |||
2235 | if (item == parent->child) | ||
2236 | { | ||
2237 | /* first element */ | ||
2238 | parent->child = item->next; | ||
2239 | } | ||
2240 | else if (item->next == NULL) | ||
2241 | { | ||
2242 | /* last element */ | ||
2243 | parent->child->prev = item->prev; | ||
2244 | } | ||
2245 | |||
2246 | /* make sure the detached item doesn't point anywhere anymore */ | ||
2247 | item->prev = NULL; | ||
2248 | item->next = NULL; | ||
2249 | |||
2250 | return item; | ||
2251 | } | ||
2252 | |||
2253 | CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) | ||
2254 | { | ||
2255 | if (which < 0) | ||
2256 | { | ||
2257 | return NULL; | ||
2258 | } | ||
2259 | |||
2260 | return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); | ||
2261 | } | ||
2262 | |||
2263 | CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) | ||
2264 | { | ||
2265 | cJSON_Delete(cJSON_DetachItemFromArray(array, which)); | ||
2266 | } | ||
2267 | |||
2268 | CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) | ||
2269 | { | ||
2270 | cJSON *to_detach = cJSON_GetObjectItem(object, string); | ||
2271 | |||
2272 | return cJSON_DetachItemViaPointer(object, to_detach); | ||
2273 | } | ||
2274 | |||
2275 | CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) | ||
2276 | { | ||
2277 | cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); | ||
2278 | |||
2279 | return cJSON_DetachItemViaPointer(object, to_detach); | ||
2280 | } | ||
2281 | |||
2282 | CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) | ||
2283 | { | ||
2284 | cJSON_Delete(cJSON_DetachItemFromObject(object, string)); | ||
2285 | } | ||
2286 | |||
2287 | CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) | ||
2288 | { | ||
2289 | cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); | ||
2290 | } | ||
2291 | |||
2292 | /* Replace array/object items with new ones. */ | ||
2293 | CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) | ||
2294 | { | ||
2295 | cJSON *after_inserted = NULL; | ||
2296 | |||
2297 | if (which < 0 || newitem == NULL) | ||
2298 | { | ||
2299 | return false; | ||
2300 | } | ||
2301 | |||
2302 | after_inserted = get_array_item(array, (size_t)which); | ||
2303 | if (after_inserted == NULL) | ||
2304 | { | ||
2305 | return add_item_to_array(array, newitem); | ||
2306 | } | ||
2307 | |||
2308 | if (after_inserted != array->child && after_inserted->prev == NULL) { | ||
2309 | /* return false if after_inserted is a corrupted array item */ | ||
2310 | return false; | ||
2311 | } | ||
2312 | |||
2313 | newitem->next = after_inserted; | ||
2314 | newitem->prev = after_inserted->prev; | ||
2315 | after_inserted->prev = newitem; | ||
2316 | if (after_inserted == array->child) | ||
2317 | { | ||
2318 | array->child = newitem; | ||
2319 | } | ||
2320 | else | ||
2321 | { | ||
2322 | newitem->prev->next = newitem; | ||
2323 | } | ||
2324 | return true; | ||
2325 | } | ||
2326 | |||
2327 | CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) | ||
2328 | { | ||
2329 | if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL)) | ||
2330 | { | ||
2331 | return false; | ||
2332 | } | ||
2333 | |||
2334 | if (replacement == item) | ||
2335 | { | ||
2336 | return true; | ||
2337 | } | ||
2338 | |||
2339 | replacement->next = item->next; | ||
2340 | replacement->prev = item->prev; | ||
2341 | |||
2342 | if (replacement->next != NULL) | ||
2343 | { | ||
2344 | replacement->next->prev = replacement; | ||
2345 | } | ||
2346 | if (parent->child == item) | ||
2347 | { | ||
2348 | if (parent->child->prev == parent->child) | ||
2349 | { | ||
2350 | replacement->prev = replacement; | ||
2351 | } | ||
2352 | parent->child = replacement; | ||
2353 | } | ||
2354 | else | ||
2355 | { /* | ||
2356 | * To find the last item in array quickly, we use prev in array. | ||
2357 | * We can't modify the last item's next pointer where this item was the parent's child | ||
2358 | */ | ||
2359 | if (replacement->prev != NULL) | ||
2360 | { | ||
2361 | replacement->prev->next = replacement; | ||
2362 | } | ||
2363 | if (replacement->next == NULL) | ||
2364 | { | ||
2365 | parent->child->prev = replacement; | ||
2366 | } | ||
2367 | } | ||
2368 | |||
2369 | item->next = NULL; | ||
2370 | item->prev = NULL; | ||
2371 | cJSON_Delete(item); | ||
2372 | |||
2373 | return true; | ||
2374 | } | ||
2375 | |||
2376 | CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) | ||
2377 | { | ||
2378 | if (which < 0) | ||
2379 | { | ||
2380 | return false; | ||
2381 | } | ||
2382 | |||
2383 | return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); | ||
2384 | } | ||
2385 | |||
2386 | static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive) | ||
2387 | { | ||
2388 | if ((replacement == NULL) || (string == NULL)) | ||
2389 | { | ||
2390 | return false; | ||
2391 | } | ||
2392 | |||
2393 | /* replace the name in the replacement */ | ||
2394 | if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) | ||
2395 | { | ||
2396 | cJSON_free(replacement->string); | ||
2397 | } | ||
2398 | replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); | ||
2399 | if (replacement->string == NULL) | ||
2400 | { | ||
2401 | return false; | ||
2402 | } | ||
2403 | |||
2404 | replacement->type &= ~cJSON_StringIsConst; | ||
2405 | |||
2406 | return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); | ||
2407 | } | ||
2408 | |||
2409 | CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) | ||
2410 | { | ||
2411 | return replace_item_in_object(object, string, newitem, false); | ||
2412 | } | ||
2413 | |||
2414 | CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) | ||
2415 | { | ||
2416 | return replace_item_in_object(object, string, newitem, true); | ||
2417 | } | ||
2418 | |||
2419 | /* Create basic types: */ | ||
2420 | CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) | ||
2421 | { | ||
2422 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2423 | if(item) | ||
2424 | { | ||
2425 | item->type = cJSON_NULL; | ||
2426 | } | ||
2427 | |||
2428 | return item; | ||
2429 | } | ||
2430 | |||
2431 | CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void) | ||
2432 | { | ||
2433 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2434 | if(item) | ||
2435 | { | ||
2436 | item->type = cJSON_True; | ||
2437 | } | ||
2438 | |||
2439 | return item; | ||
2440 | } | ||
2441 | |||
2442 | CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void) | ||
2443 | { | ||
2444 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2445 | if(item) | ||
2446 | { | ||
2447 | item->type = cJSON_False; | ||
2448 | } | ||
2449 | |||
2450 | return item; | ||
2451 | } | ||
2452 | |||
2453 | CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean) | ||
2454 | { | ||
2455 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2456 | if(item) | ||
2457 | { | ||
2458 | item->type = boolean ? cJSON_True : cJSON_False; | ||
2459 | } | ||
2460 | |||
2461 | return item; | ||
2462 | } | ||
2463 | |||
2464 | CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) | ||
2465 | { | ||
2466 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2467 | if(item) | ||
2468 | { | ||
2469 | item->type = cJSON_Number; | ||
2470 | item->valuedouble = num; | ||
2471 | |||
2472 | /* use saturation in case of overflow */ | ||
2473 | if (num >= INT_MAX) | ||
2474 | { | ||
2475 | item->valueint = INT_MAX; | ||
2476 | } | ||
2477 | else if (num <= (double)INT_MIN) | ||
2478 | { | ||
2479 | item->valueint = INT_MIN; | ||
2480 | } | ||
2481 | else | ||
2482 | { | ||
2483 | item->valueint = (int)num; | ||
2484 | } | ||
2485 | } | ||
2486 | |||
2487 | return item; | ||
2488 | } | ||
2489 | |||
2490 | CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) | ||
2491 | { | ||
2492 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2493 | if(item) | ||
2494 | { | ||
2495 | item->type = cJSON_String; | ||
2496 | item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); | ||
2497 | if(!item->valuestring) | ||
2498 | { | ||
2499 | cJSON_Delete(item); | ||
2500 | return NULL; | ||
2501 | } | ||
2502 | } | ||
2503 | |||
2504 | return item; | ||
2505 | } | ||
2506 | |||
2507 | CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string) | ||
2508 | { | ||
2509 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2510 | if (item != NULL) | ||
2511 | { | ||
2512 | item->type = cJSON_String | cJSON_IsReference; | ||
2513 | item->valuestring = (char*)cast_away_const(string); | ||
2514 | } | ||
2515 | |||
2516 | return item; | ||
2517 | } | ||
2518 | |||
2519 | CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child) | ||
2520 | { | ||
2521 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2522 | if (item != NULL) { | ||
2523 | item->type = cJSON_Object | cJSON_IsReference; | ||
2524 | item->child = (cJSON*)cast_away_const(child); | ||
2525 | } | ||
2526 | |||
2527 | return item; | ||
2528 | } | ||
2529 | |||
2530 | CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) { | ||
2531 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2532 | if (item != NULL) { | ||
2533 | item->type = cJSON_Array | cJSON_IsReference; | ||
2534 | item->child = (cJSON*)cast_away_const(child); | ||
2535 | } | ||
2536 | |||
2537 | return item; | ||
2538 | } | ||
2539 | |||
2540 | CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) | ||
2541 | { | ||
2542 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2543 | if(item) | ||
2544 | { | ||
2545 | item->type = cJSON_Raw; | ||
2546 | item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks); | ||
2547 | if(!item->valuestring) | ||
2548 | { | ||
2549 | cJSON_Delete(item); | ||
2550 | return NULL; | ||
2551 | } | ||
2552 | } | ||
2553 | |||
2554 | return item; | ||
2555 | } | ||
2556 | |||
2557 | CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) | ||
2558 | { | ||
2559 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2560 | if(item) | ||
2561 | { | ||
2562 | item->type=cJSON_Array; | ||
2563 | } | ||
2564 | |||
2565 | return item; | ||
2566 | } | ||
2567 | |||
2568 | CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) | ||
2569 | { | ||
2570 | cJSON *item = cJSON_New_Item(&global_hooks); | ||
2571 | if (item) | ||
2572 | { | ||
2573 | item->type = cJSON_Object; | ||
2574 | } | ||
2575 | |||
2576 | return item; | ||
2577 | } | ||
2578 | |||
2579 | /* Create Arrays: */ | ||
2580 | CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) | ||
2581 | { | ||
2582 | size_t i = 0; | ||
2583 | cJSON *n = NULL; | ||
2584 | cJSON *p = NULL; | ||
2585 | cJSON *a = NULL; | ||
2586 | |||
2587 | if ((count < 0) || (numbers == NULL)) | ||
2588 | { | ||
2589 | return NULL; | ||
2590 | } | ||
2591 | |||
2592 | a = cJSON_CreateArray(); | ||
2593 | |||
2594 | for(i = 0; a && (i < (size_t)count); i++) | ||
2595 | { | ||
2596 | n = cJSON_CreateNumber(numbers[i]); | ||
2597 | if (!n) | ||
2598 | { | ||
2599 | cJSON_Delete(a); | ||
2600 | return NULL; | ||
2601 | } | ||
2602 | if(!i) | ||
2603 | { | ||
2604 | a->child = n; | ||
2605 | } | ||
2606 | else | ||
2607 | { | ||
2608 | suffix_object(p, n); | ||
2609 | } | ||
2610 | p = n; | ||
2611 | } | ||
2612 | |||
2613 | if (a && a->child) { | ||
2614 | a->child->prev = n; | ||
2615 | } | ||
2616 | |||
2617 | return a; | ||
2618 | } | ||
2619 | |||
2620 | CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) | ||
2621 | { | ||
2622 | size_t i = 0; | ||
2623 | cJSON *n = NULL; | ||
2624 | cJSON *p = NULL; | ||
2625 | cJSON *a = NULL; | ||
2626 | |||
2627 | if ((count < 0) || (numbers == NULL)) | ||
2628 | { | ||
2629 | return NULL; | ||
2630 | } | ||
2631 | |||
2632 | a = cJSON_CreateArray(); | ||
2633 | |||
2634 | for(i = 0; a && (i < (size_t)count); i++) | ||
2635 | { | ||
2636 | n = cJSON_CreateNumber((double)numbers[i]); | ||
2637 | if(!n) | ||
2638 | { | ||
2639 | cJSON_Delete(a); | ||
2640 | return NULL; | ||
2641 | } | ||
2642 | if(!i) | ||
2643 | { | ||
2644 | a->child = n; | ||
2645 | } | ||
2646 | else | ||
2647 | { | ||
2648 | suffix_object(p, n); | ||
2649 | } | ||
2650 | p = n; | ||
2651 | } | ||
2652 | |||
2653 | if (a && a->child) { | ||
2654 | a->child->prev = n; | ||
2655 | } | ||
2656 | |||
2657 | return a; | ||
2658 | } | ||
2659 | |||
2660 | CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) | ||
2661 | { | ||
2662 | size_t i = 0; | ||
2663 | cJSON *n = NULL; | ||
2664 | cJSON *p = NULL; | ||
2665 | cJSON *a = NULL; | ||
2666 | |||
2667 | if ((count < 0) || (numbers == NULL)) | ||
2668 | { | ||
2669 | return NULL; | ||
2670 | } | ||
2671 | |||
2672 | a = cJSON_CreateArray(); | ||
2673 | |||
2674 | for(i = 0; a && (i < (size_t)count); i++) | ||
2675 | { | ||
2676 | n = cJSON_CreateNumber(numbers[i]); | ||
2677 | if(!n) | ||
2678 | { | ||
2679 | cJSON_Delete(a); | ||
2680 | return NULL; | ||
2681 | } | ||
2682 | if(!i) | ||
2683 | { | ||
2684 | a->child = n; | ||
2685 | } | ||
2686 | else | ||
2687 | { | ||
2688 | suffix_object(p, n); | ||
2689 | } | ||
2690 | p = n; | ||
2691 | } | ||
2692 | |||
2693 | if (a && a->child) { | ||
2694 | a->child->prev = n; | ||
2695 | } | ||
2696 | |||
2697 | return a; | ||
2698 | } | ||
2699 | |||
2700 | CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count) | ||
2701 | { | ||
2702 | size_t i = 0; | ||
2703 | cJSON *n = NULL; | ||
2704 | cJSON *p = NULL; | ||
2705 | cJSON *a = NULL; | ||
2706 | |||
2707 | if ((count < 0) || (strings == NULL)) | ||
2708 | { | ||
2709 | return NULL; | ||
2710 | } | ||
2711 | |||
2712 | a = cJSON_CreateArray(); | ||
2713 | |||
2714 | for (i = 0; a && (i < (size_t)count); i++) | ||
2715 | { | ||
2716 | n = cJSON_CreateString(strings[i]); | ||
2717 | if(!n) | ||
2718 | { | ||
2719 | cJSON_Delete(a); | ||
2720 | return NULL; | ||
2721 | } | ||
2722 | if(!i) | ||
2723 | { | ||
2724 | a->child = n; | ||
2725 | } | ||
2726 | else | ||
2727 | { | ||
2728 | suffix_object(p,n); | ||
2729 | } | ||
2730 | p = n; | ||
2731 | } | ||
2732 | |||
2733 | if (a && a->child) { | ||
2734 | a->child->prev = n; | ||
2735 | } | ||
2736 | |||
2737 | return a; | ||
2738 | } | ||
2739 | |||
2740 | /* Duplication */ | ||
2741 | cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse); | ||
2742 | |||
2743 | CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) | ||
2744 | { | ||
2745 | return cJSON_Duplicate_rec(item, 0, recurse ); | ||
2746 | } | ||
2747 | |||
2748 | cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse) | ||
2749 | { | ||
2750 | cJSON *newitem = NULL; | ||
2751 | cJSON *child = NULL; | ||
2752 | cJSON *next = NULL; | ||
2753 | cJSON *newchild = NULL; | ||
2754 | |||
2755 | /* Bail on bad ptr */ | ||
2756 | if (!item) | ||
2757 | { | ||
2758 | goto fail; | ||
2759 | } | ||
2760 | /* Create new item */ | ||
2761 | newitem = cJSON_New_Item(&global_hooks); | ||
2762 | if (!newitem) | ||
2763 | { | ||
2764 | goto fail; | ||
2765 | } | ||
2766 | /* Copy over all vars */ | ||
2767 | newitem->type = item->type & (~cJSON_IsReference); | ||
2768 | newitem->valueint = item->valueint; | ||
2769 | newitem->valuedouble = item->valuedouble; | ||
2770 | if (item->valuestring) | ||
2771 | { | ||
2772 | newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); | ||
2773 | if (!newitem->valuestring) | ||
2774 | { | ||
2775 | goto fail; | ||
2776 | } | ||
2777 | } | ||
2778 | if (item->string) | ||
2779 | { | ||
2780 | newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks); | ||
2781 | if (!newitem->string) | ||
2782 | { | ||
2783 | goto fail; | ||
2784 | } | ||
2785 | } | ||
2786 | /* If non-recursive, then we're done! */ | ||
2787 | if (!recurse) | ||
2788 | { | ||
2789 | return newitem; | ||
2790 | } | ||
2791 | /* Walk the ->next chain for the child. */ | ||
2792 | child = item->child; | ||
2793 | while (child != NULL) | ||
2794 | { | ||
2795 | if(depth >= CJSON_CIRCULAR_LIMIT) { | ||
2796 | goto fail; | ||
2797 | } | ||
2798 | newchild = cJSON_Duplicate_rec(child, depth + 1, true); /* Duplicate (with recurse) each item in the ->next chain */ | ||
2799 | if (!newchild) | ||
2800 | { | ||
2801 | goto fail; | ||
2802 | } | ||
2803 | if (next != NULL) | ||
2804 | { | ||
2805 | /* If newitem->child already set, then crosswire ->prev and ->next and move on */ | ||
2806 | next->next = newchild; | ||
2807 | newchild->prev = next; | ||
2808 | next = newchild; | ||
2809 | } | ||
2810 | else | ||
2811 | { | ||
2812 | /* Set newitem->child and move to it */ | ||
2813 | newitem->child = newchild; | ||
2814 | next = newchild; | ||
2815 | } | ||
2816 | child = child->next; | ||
2817 | } | ||
2818 | if (newitem && newitem->child) | ||
2819 | { | ||
2820 | newitem->child->prev = newchild; | ||
2821 | } | ||
2822 | |||
2823 | return newitem; | ||
2824 | |||
2825 | fail: | ||
2826 | if (newitem != NULL) | ||
2827 | { | ||
2828 | cJSON_Delete(newitem); | ||
2829 | } | ||
2830 | |||
2831 | return NULL; | ||
2832 | } | ||
2833 | |||
2834 | static void skip_oneline_comment(char **input) | ||
2835 | { | ||
2836 | *input += static_strlen("//"); | ||
2837 | |||
2838 | for (; (*input)[0] != '\0'; ++(*input)) | ||
2839 | { | ||
2840 | if ((*input)[0] == '\n') { | ||
2841 | *input += static_strlen("\n"); | ||
2842 | return; | ||
2843 | } | ||
2844 | } | ||
2845 | } | ||
2846 | |||
2847 | static void skip_multiline_comment(char **input) | ||
2848 | { | ||
2849 | *input += static_strlen("/*"); | ||
2850 | |||
2851 | for (; (*input)[0] != '\0'; ++(*input)) | ||
2852 | { | ||
2853 | if (((*input)[0] == '*') && ((*input)[1] == '/')) | ||
2854 | { | ||
2855 | *input += static_strlen("*/"); | ||
2856 | return; | ||
2857 | } | ||
2858 | } | ||
2859 | } | ||
2860 | |||
2861 | static void minify_string(char **input, char **output) { | ||
2862 | (*output)[0] = (*input)[0]; | ||
2863 | *input += static_strlen("\""); | ||
2864 | *output += static_strlen("\""); | ||
2865 | |||
2866 | |||
2867 | for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) { | ||
2868 | (*output)[0] = (*input)[0]; | ||
2869 | |||
2870 | if ((*input)[0] == '\"') { | ||
2871 | (*output)[0] = '\"'; | ||
2872 | *input += static_strlen("\""); | ||
2873 | *output += static_strlen("\""); | ||
2874 | return; | ||
2875 | } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) { | ||
2876 | (*output)[1] = (*input)[1]; | ||
2877 | *input += static_strlen("\""); | ||
2878 | *output += static_strlen("\""); | ||
2879 | } | ||
2880 | } | ||
2881 | } | ||
2882 | |||
2883 | CJSON_PUBLIC(void) cJSON_Minify(char *json) | ||
2884 | { | ||
2885 | char *into = json; | ||
2886 | |||
2887 | if (json == NULL) | ||
2888 | { | ||
2889 | return; | ||
2890 | } | ||
2891 | |||
2892 | while (json[0] != '\0') | ||
2893 | { | ||
2894 | switch (json[0]) | ||
2895 | { | ||
2896 | case ' ': | ||
2897 | case '\t': | ||
2898 | case '\r': | ||
2899 | case '\n': | ||
2900 | json++; | ||
2901 | break; | ||
2902 | |||
2903 | case '/': | ||
2904 | if (json[1] == '/') | ||
2905 | { | ||
2906 | skip_oneline_comment(&json); | ||
2907 | } | ||
2908 | else if (json[1] == '*') | ||
2909 | { | ||
2910 | skip_multiline_comment(&json); | ||
2911 | } else { | ||
2912 | json++; | ||
2913 | } | ||
2914 | break; | ||
2915 | |||
2916 | case '\"': | ||
2917 | minify_string(&json, (char**)&into); | ||
2918 | break; | ||
2919 | |||
2920 | default: | ||
2921 | into[0] = json[0]; | ||
2922 | json++; | ||
2923 | into++; | ||
2924 | } | ||
2925 | } | ||
2926 | |||
2927 | /* and null-terminate. */ | ||
2928 | *into = '\0'; | ||
2929 | } | ||
2930 | |||
2931 | CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item) | ||
2932 | { | ||
2933 | if (item == NULL) | ||
2934 | { | ||
2935 | return false; | ||
2936 | } | ||
2937 | |||
2938 | return (item->type & 0xFF) == cJSON_Invalid; | ||
2939 | } | ||
2940 | |||
2941 | CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item) | ||
2942 | { | ||
2943 | if (item == NULL) | ||
2944 | { | ||
2945 | return false; | ||
2946 | } | ||
2947 | |||
2948 | return (item->type & 0xFF) == cJSON_False; | ||
2949 | } | ||
2950 | |||
2951 | CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item) | ||
2952 | { | ||
2953 | if (item == NULL) | ||
2954 | { | ||
2955 | return false; | ||
2956 | } | ||
2957 | |||
2958 | return (item->type & 0xff) == cJSON_True; | ||
2959 | } | ||
2960 | |||
2961 | |||
2962 | CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item) | ||
2963 | { | ||
2964 | if (item == NULL) | ||
2965 | { | ||
2966 | return false; | ||
2967 | } | ||
2968 | |||
2969 | return (item->type & (cJSON_True | cJSON_False)) != 0; | ||
2970 | } | ||
2971 | CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item) | ||
2972 | { | ||
2973 | if (item == NULL) | ||
2974 | { | ||
2975 | return false; | ||
2976 | } | ||
2977 | |||
2978 | return (item->type & 0xFF) == cJSON_NULL; | ||
2979 | } | ||
2980 | |||
2981 | CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) | ||
2982 | { | ||
2983 | if (item == NULL) | ||
2984 | { | ||
2985 | return false; | ||
2986 | } | ||
2987 | |||
2988 | return (item->type & 0xFF) == cJSON_Number; | ||
2989 | } | ||
2990 | |||
2991 | CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) | ||
2992 | { | ||
2993 | if (item == NULL) | ||
2994 | { | ||
2995 | return false; | ||
2996 | } | ||
2997 | |||
2998 | return (item->type & 0xFF) == cJSON_String; | ||
2999 | } | ||
3000 | |||
3001 | CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) | ||
3002 | { | ||
3003 | if (item == NULL) | ||
3004 | { | ||
3005 | return false; | ||
3006 | } | ||
3007 | |||
3008 | return (item->type & 0xFF) == cJSON_Array; | ||
3009 | } | ||
3010 | |||
3011 | CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item) | ||
3012 | { | ||
3013 | if (item == NULL) | ||
3014 | { | ||
3015 | return false; | ||
3016 | } | ||
3017 | |||
3018 | return (item->type & 0xFF) == cJSON_Object; | ||
3019 | } | ||
3020 | |||
3021 | CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) | ||
3022 | { | ||
3023 | if (item == NULL) | ||
3024 | { | ||
3025 | return false; | ||
3026 | } | ||
3027 | |||
3028 | return (item->type & 0xFF) == cJSON_Raw; | ||
3029 | } | ||
3030 | |||
3031 | CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive) | ||
3032 | { | ||
3033 | if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF))) | ||
3034 | { | ||
3035 | return false; | ||
3036 | } | ||
3037 | |||
3038 | /* check if type is valid */ | ||
3039 | switch (a->type & 0xFF) | ||
3040 | { | ||
3041 | case cJSON_False: | ||
3042 | case cJSON_True: | ||
3043 | case cJSON_NULL: | ||
3044 | case cJSON_Number: | ||
3045 | case cJSON_String: | ||
3046 | case cJSON_Raw: | ||
3047 | case cJSON_Array: | ||
3048 | case cJSON_Object: | ||
3049 | break; | ||
3050 | |||
3051 | default: | ||
3052 | return false; | ||
3053 | } | ||
3054 | |||
3055 | /* identical objects are equal */ | ||
3056 | if (a == b) | ||
3057 | { | ||
3058 | return true; | ||
3059 | } | ||
3060 | |||
3061 | switch (a->type & 0xFF) | ||
3062 | { | ||
3063 | /* in these cases and equal type is enough */ | ||
3064 | case cJSON_False: | ||
3065 | case cJSON_True: | ||
3066 | case cJSON_NULL: | ||
3067 | return true; | ||
3068 | |||
3069 | case cJSON_Number: | ||
3070 | if (compare_double(a->valuedouble, b->valuedouble)) | ||
3071 | { | ||
3072 | return true; | ||
3073 | } | ||
3074 | return false; | ||
3075 | |||
3076 | case cJSON_String: | ||
3077 | case cJSON_Raw: | ||
3078 | if ((a->valuestring == NULL) || (b->valuestring == NULL)) | ||
3079 | { | ||
3080 | return false; | ||
3081 | } | ||
3082 | if (strcmp(a->valuestring, b->valuestring) == 0) | ||
3083 | { | ||
3084 | return true; | ||
3085 | } | ||
3086 | |||
3087 | return false; | ||
3088 | |||
3089 | case cJSON_Array: | ||
3090 | { | ||
3091 | cJSON *a_element = a->child; | ||
3092 | cJSON *b_element = b->child; | ||
3093 | |||
3094 | for (; (a_element != NULL) && (b_element != NULL);) | ||
3095 | { | ||
3096 | if (!cJSON_Compare(a_element, b_element, case_sensitive)) | ||
3097 | { | ||
3098 | return false; | ||
3099 | } | ||
3100 | |||
3101 | a_element = a_element->next; | ||
3102 | b_element = b_element->next; | ||
3103 | } | ||
3104 | |||
3105 | /* one of the arrays is longer than the other */ | ||
3106 | if (a_element != b_element) { | ||
3107 | return false; | ||
3108 | } | ||
3109 | |||
3110 | return true; | ||
3111 | } | ||
3112 | |||
3113 | case cJSON_Object: | ||
3114 | { | ||
3115 | cJSON *a_element = NULL; | ||
3116 | cJSON *b_element = NULL; | ||
3117 | cJSON_ArrayForEach(a_element, a) | ||
3118 | { | ||
3119 | /* TODO This has O(n^2) runtime, which is horrible! */ | ||
3120 | b_element = get_object_item(b, a_element->string, case_sensitive); | ||
3121 | if (b_element == NULL) | ||
3122 | { | ||
3123 | return false; | ||
3124 | } | ||
3125 | |||
3126 | if (!cJSON_Compare(a_element, b_element, case_sensitive)) | ||
3127 | { | ||
3128 | return false; | ||
3129 | } | ||
3130 | } | ||
3131 | |||
3132 | /* doing this twice, once on a and b to prevent true comparison if a subset of b | ||
3133 | * TODO: Do this the proper way, this is just a fix for now */ | ||
3134 | cJSON_ArrayForEach(b_element, b) | ||
3135 | { | ||
3136 | a_element = get_object_item(a, b_element->string, case_sensitive); | ||
3137 | if (a_element == NULL) | ||
3138 | { | ||
3139 | return false; | ||
3140 | } | ||
3141 | |||
3142 | if (!cJSON_Compare(b_element, a_element, case_sensitive)) | ||
3143 | { | ||
3144 | return false; | ||
3145 | } | ||
3146 | } | ||
3147 | |||
3148 | return true; | ||
3149 | } | ||
3150 | |||
3151 | default: | ||
3152 | return false; | ||
3153 | } | ||
3154 | } | ||
3155 | |||
3156 | CJSON_PUBLIC(void *) cJSON_malloc(size_t size) | ||
3157 | { | ||
3158 | return global_hooks.allocate(size); | ||
3159 | } | ||
3160 | |||
3161 | CJSON_PUBLIC(void) cJSON_free(void *object) | ||
3162 | { | ||
3163 | global_hooks.deallocate(object); | ||
3164 | object = NULL; | ||
3165 | } | ||
diff --git a/lib/vendor/cJSON/cJSON.h b/lib/vendor/cJSON/cJSON.h new file mode 100644 index 00000000..37520bbc --- /dev/null +++ b/lib/vendor/cJSON/cJSON.h | |||
@@ -0,0 +1,306 @@ | |||
1 | /* | ||
2 | Copyright (c) 2009-2017 Dave Gamble and cJSON contributors | ||
3 | |||
4 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
5 | of this software and associated documentation files (the "Software"), to deal | ||
6 | in the Software without restriction, including without limitation the rights | ||
7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
8 | copies of the Software, and to permit persons to whom the Software is | ||
9 | furnished to do so, subject to the following conditions: | ||
10 | |||
11 | The above copyright notice and this permission notice shall be included in | ||
12 | all copies or substantial portions of the Software. | ||
13 | |||
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
20 | THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #ifndef cJSON__h | ||
24 | #define cJSON__h | ||
25 | |||
26 | #ifdef __cplusplus | ||
27 | extern "C" | ||
28 | { | ||
29 | #endif | ||
30 | |||
31 | #if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)) | ||
32 | #define __WINDOWS__ | ||
33 | #endif | ||
34 | |||
35 | #ifdef __WINDOWS__ | ||
36 | |||
37 | /* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options: | ||
38 | |||
39 | CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols | ||
40 | CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default) | ||
41 | CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol | ||
42 | |||
43 | For *nix builds that support visibility attribute, you can define similar behavior by | ||
44 | |||
45 | setting default visibility to hidden by adding | ||
46 | -fvisibility=hidden (for gcc) | ||
47 | or | ||
48 | -xldscope=hidden (for sun cc) | ||
49 | to CFLAGS | ||
50 | |||
51 | then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does | ||
52 | |||
53 | */ | ||
54 | |||
55 | #define CJSON_CDECL __cdecl | ||
56 | #define CJSON_STDCALL __stdcall | ||
57 | |||
58 | /* export symbols by default, this is necessary for copy pasting the C and header file */ | ||
59 | #if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS) | ||
60 | #define CJSON_EXPORT_SYMBOLS | ||
61 | #endif | ||
62 | |||
63 | #if defined(CJSON_HIDE_SYMBOLS) | ||
64 | #define CJSON_PUBLIC(type) type CJSON_STDCALL | ||
65 | #elif defined(CJSON_EXPORT_SYMBOLS) | ||
66 | #define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL | ||
67 | #elif defined(CJSON_IMPORT_SYMBOLS) | ||
68 | #define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL | ||
69 | #endif | ||
70 | #else /* !__WINDOWS__ */ | ||
71 | #define CJSON_CDECL | ||
72 | #define CJSON_STDCALL | ||
73 | |||
74 | #if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY) | ||
75 | #define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type | ||
76 | #else | ||
77 | #define CJSON_PUBLIC(type) type | ||
78 | #endif | ||
79 | #endif | ||
80 | |||
81 | /* project version */ | ||
82 | #define CJSON_VERSION_MAJOR 1 | ||
83 | #define CJSON_VERSION_MINOR 7 | ||
84 | #define CJSON_VERSION_PATCH 18 | ||
85 | |||
86 | #include <stddef.h> | ||
87 | |||
88 | /* cJSON Types: */ | ||
89 | #define cJSON_Invalid (0) | ||
90 | #define cJSON_False (1 << 0) | ||
91 | #define cJSON_True (1 << 1) | ||
92 | #define cJSON_NULL (1 << 2) | ||
93 | #define cJSON_Number (1 << 3) | ||
94 | #define cJSON_String (1 << 4) | ||
95 | #define cJSON_Array (1 << 5) | ||
96 | #define cJSON_Object (1 << 6) | ||
97 | #define cJSON_Raw (1 << 7) /* raw json */ | ||
98 | |||
99 | #define cJSON_IsReference 256 | ||
100 | #define cJSON_StringIsConst 512 | ||
101 | |||
102 | /* The cJSON structure: */ | ||
103 | typedef struct cJSON | ||
104 | { | ||
105 | /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ | ||
106 | struct cJSON *next; | ||
107 | struct cJSON *prev; | ||
108 | /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ | ||
109 | struct cJSON *child; | ||
110 | |||
111 | /* The type of the item, as above. */ | ||
112 | int type; | ||
113 | |||
114 | /* The item's string, if type==cJSON_String and type == cJSON_Raw */ | ||
115 | char *valuestring; | ||
116 | /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */ | ||
117 | int valueint; | ||
118 | /* The item's number, if type==cJSON_Number */ | ||
119 | double valuedouble; | ||
120 | |||
121 | /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ | ||
122 | char *string; | ||
123 | } cJSON; | ||
124 | |||
125 | typedef struct cJSON_Hooks | ||
126 | { | ||
127 | /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */ | ||
128 | void *(CJSON_CDECL *malloc_fn)(size_t sz); | ||
129 | void (CJSON_CDECL *free_fn)(void *ptr); | ||
130 | } cJSON_Hooks; | ||
131 | |||
132 | typedef int cJSON_bool; | ||
133 | |||
134 | /* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them. | ||
135 | * This is to prevent stack overflows. */ | ||
136 | #ifndef CJSON_NESTING_LIMIT | ||
137 | #define CJSON_NESTING_LIMIT 1000 | ||
138 | #endif | ||
139 | |||
140 | /* Limits the length of circular references can be before cJSON rejects to parse them. | ||
141 | * This is to prevent stack overflows. */ | ||
142 | #ifndef CJSON_CIRCULAR_LIMIT | ||
143 | #define CJSON_CIRCULAR_LIMIT 10000 | ||
144 | #endif | ||
145 | |||
146 | /* returns the version of cJSON as a string */ | ||
147 | CJSON_PUBLIC(const char*) cJSON_Version(void); | ||
148 | |||
149 | /* Supply malloc, realloc and free functions to cJSON */ | ||
150 | CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); | ||
151 | |||
152 | /* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */ | ||
153 | /* Supply a block of JSON, and this returns a cJSON object you can interrogate. */ | ||
154 | CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value); | ||
155 | CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length); | ||
156 | /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ | ||
157 | /* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */ | ||
158 | CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated); | ||
159 | CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated); | ||
160 | |||
161 | /* Render a cJSON entity to text for transfer/storage. */ | ||
162 | CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); | ||
163 | /* Render a cJSON entity to text for transfer/storage without any formatting. */ | ||
164 | CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); | ||
165 | /* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ | ||
166 | CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt); | ||
167 | /* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */ | ||
168 | /* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */ | ||
169 | CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format); | ||
170 | /* Delete a cJSON entity and all subentities. */ | ||
171 | CJSON_PUBLIC(void) cJSON_Delete(cJSON *item); | ||
172 | |||
173 | /* Returns the number of items in an array (or object). */ | ||
174 | CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array); | ||
175 | /* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */ | ||
176 | CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); | ||
177 | /* Get item "string" from object. Case insensitive. */ | ||
178 | CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string); | ||
179 | CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string); | ||
180 | CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string); | ||
181 | /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ | ||
182 | CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void); | ||
183 | |||
184 | /* Check item type and return its value */ | ||
185 | CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item); | ||
186 | CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item); | ||
187 | |||
188 | /* These functions check the type of an item */ | ||
189 | CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item); | ||
190 | CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item); | ||
191 | CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item); | ||
192 | CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item); | ||
193 | CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item); | ||
194 | CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item); | ||
195 | CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item); | ||
196 | CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item); | ||
197 | CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item); | ||
198 | CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item); | ||
199 | |||
200 | /* These calls create a cJSON item of the appropriate type. */ | ||
201 | CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); | ||
202 | CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void); | ||
203 | CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void); | ||
204 | CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean); | ||
205 | CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num); | ||
206 | CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); | ||
207 | /* raw json */ | ||
208 | CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw); | ||
209 | CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); | ||
210 | CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); | ||
211 | |||
212 | /* Create a string where valuestring references a string so | ||
213 | * it will not be freed by cJSON_Delete */ | ||
214 | CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string); | ||
215 | /* Create an object/array that only references it's elements so | ||
216 | * they will not be freed by cJSON_Delete */ | ||
217 | CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child); | ||
218 | CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child); | ||
219 | |||
220 | /* These utilities create an Array of count items. | ||
221 | * The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/ | ||
222 | CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count); | ||
223 | CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count); | ||
224 | CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count); | ||
225 | CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count); | ||
226 | |||
227 | /* Append item to the specified array/object. */ | ||
228 | CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); | ||
229 | CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); | ||
230 | /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object. | ||
231 | * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before | ||
232 | * writing to `item->string` */ | ||
233 | CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); | ||
234 | /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ | ||
235 | CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); | ||
236 | CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); | ||
237 | |||
238 | /* Remove/Detach items from Arrays/Objects. */ | ||
239 | CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); | ||
240 | CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); | ||
241 | CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); | ||
242 | CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); | ||
243 | CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); | ||
244 | CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); | ||
245 | CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); | ||
246 | |||
247 | /* Update array items. */ | ||
248 | CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */ | ||
249 | CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement); | ||
250 | CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); | ||
251 | CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); | ||
252 | CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem); | ||
253 | |||
254 | /* Duplicate a cJSON item */ | ||
255 | CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); | ||
256 | /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will | ||
257 | * need to be released. With recurse!=0, it will duplicate any children connected to the item. | ||
258 | * The item->next and ->prev pointers are always zero on return from Duplicate. */ | ||
259 | /* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal. | ||
260 | * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */ | ||
261 | CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive); | ||
262 | |||
263 | /* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings. | ||
264 | * The input pointer json cannot point to a read-only address area, such as a string constant, | ||
265 | * but should point to a readable and writable address area. */ | ||
266 | CJSON_PUBLIC(void) cJSON_Minify(char *json); | ||
267 | |||
268 | /* Helper functions for creating and adding items to an object at the same time. | ||
269 | * They return the added item or NULL on failure. */ | ||
270 | CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name); | ||
271 | CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name); | ||
272 | CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name); | ||
273 | CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean); | ||
274 | CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number); | ||
275 | CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string); | ||
276 | CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw); | ||
277 | CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name); | ||
278 | CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name); | ||
279 | |||
280 | /* When assigning an integer value, it needs to be propagated to valuedouble too. */ | ||
281 | #define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) | ||
282 | /* helper for the cJSON_SetNumberValue macro */ | ||
283 | CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); | ||
284 | #define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number)) | ||
285 | /* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */ | ||
286 | CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring); | ||
287 | |||
288 | /* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/ | ||
289 | #define cJSON_SetBoolValue(object, boolValue) ( \ | ||
290 | (object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \ | ||
291 | (object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \ | ||
292 | cJSON_Invalid\ | ||
293 | ) | ||
294 | |||
295 | /* Macro for iterating over an array or object */ | ||
296 | #define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next) | ||
297 | |||
298 | /* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */ | ||
299 | CJSON_PUBLIC(void *) cJSON_malloc(size_t size); | ||
300 | CJSON_PUBLIC(void) cJSON_free(void *object); | ||
301 | |||
302 | #ifdef __cplusplus | ||
303 | } | ||
304 | #endif | ||
305 | |||
306 | #endif | ||