diff options
author | sss <sss@dark-alexandr.net> | 2023-01-17 00:38:19 +0300 |
---|---|---|
committer | sss <sss@dark-alexandr.net> | 2023-01-17 00:38:19 +0300 |
commit | cc3f33db7a8d3c4ad373e646b199808e01bc5d9b (patch) | |
tree | ec09d690c7656ab5f2cc72607e05fb359c24d8b2 /src/core/json_helpers.c |
added webrdp public code
Diffstat (limited to 'src/core/json_helpers.c')
-rw-r--r-- | src/core/json_helpers.c | 275 |
1 files changed, 275 insertions, 0 deletions
diff --git a/src/core/json_helpers.c b/src/core/json_helpers.c new file mode 100644 index 0000000..dfbef34 --- /dev/null +++ b/src/core/json_helpers.c @@ -0,0 +1,275 @@ +/* BSD-2-Clause license + * + * Copyright (c) 2018-2023 NST <www.newinfosec.ru>, sss <sss at dark-alexandr dot net>. + * + */ + +#include <stdint.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + +#include <json.h> + +int64_t +json_option_extract_int64(struct json_object_element_s *json_option) +{ + switch (json_option->value->type) + { + case json_type_null: + { + return 0; + } + break; + case json_type_false: + { + return 0; + } + break; + case json_type_true: + { + return 1; + } + break; + case json_type_number: + { + struct json_number_s *jsopt + = (struct json_number_s *) + json_option->value->payload; + char *out = malloc(jsopt->number_size + 1); + if (!out) + { + perror("malloc"); + return 0; + } + int64_t ret = false; + strncpy(out, jsopt->number, jsopt->number_size); + out[jsopt->number_size] = 0; + ret = atoll(out); + free(out); + return ret; + } + break; + case json_type_string: + { + struct json_string_s *jsopt + = (struct json_string_s *) + json_option->value->payload; + char *out = malloc(jsopt->string_size + 1); + if (!out) + { + perror("malloc"); + return 0; + } + int64_t ret = false; + strncpy(out, jsopt->string, jsopt->string_size); + out[jsopt->string_size] = 0; + ret = atoll(out); + free(out); + return ret; + } + break; + case json_type_object: + { + return 1; + } + break; + default: + { + return 0; + } + break; + } + return 0; +} + +bool +json_option_extract_bool(struct json_object_element_s *json_option) +{ + switch (json_option->value->type) + { + case json_type_null: + { + return false; + } + break; + case json_type_false: + { + return false; + } + break; + case json_type_true: + { + return true; + } + break; + case json_type_number: + { + struct json_number_s *jsopt + = (struct json_number_s *) + json_option->value->payload; + char *out = malloc(jsopt->number_size + 1); + if (!out) + { + perror("malloc"); + return 0; + } + bool ret = false; + strncpy(out, jsopt->number, jsopt->number_size); + out[jsopt->number_size] = 0; + ret = atoi(out); + free(out); + return ret; + } + break; + case json_type_string: + { + struct json_string_s *jsopt + = (struct json_string_s *) + json_option->value->payload; + char *out = malloc(jsopt->string_size + 1); + if (!out) + { + perror("malloc"); + return 0; + } + bool ret = false; + strncpy(out, jsopt->string, jsopt->string_size); + out[jsopt->string_size] = 0; + ret = atoi(out); + free(out); + return ret; + } + break; + case json_type_object: + { + return true; + } + break; + default: + { + return false; + } + break; + } + return false; +} + +char * +json_option_extract_string( + struct json_object_element_s *json_option, char *_out) +{ + char *out = 0; + if (_out) + { + out = _out; + } + switch (json_option->value->type) + { + case json_type_null: + { + if (!out) + { + out = malloc(strlen("null") + 1); + if (!out) + { + perror("malloc"); + return 0; + } + } + strcpy(out, "null"); + return out; + } + break; + case json_type_false: + { + if (!out) + { + out = malloc(strlen("false") + 1); + if (!out) + { + perror("malloc"); + return 0; + } + } + strcpy(out, "false"); + return out; + } + break; + case json_type_true: + { + if (!out) + { + out = malloc(strlen("true") + 1); + if (!out) + { + perror("malloc"); + return 0; + } + } + strcpy(out, "true"); + return out; + } + break; + case json_type_number: + { + struct json_number_s *jsopt + = (struct json_number_s *) + json_option->value->payload; + if (!out) + { + out = malloc(jsopt->number_size + 1); + if (!out) + { + perror("malloc"); + return 0; + } + } + strncpy(out, jsopt->number, jsopt->number_size); + out[jsopt->number_size] = 0; + return out; + } + break; + case json_type_string: + { + struct json_string_s *jsopt + = (struct json_string_s *) + json_option->value->payload; + if (!out) + { + out = malloc(jsopt->string_size + 1); + if (!out) + { + perror("malloc"); + return 0; + } + } + strncpy(out, jsopt->string, jsopt->string_size); + out[jsopt->string_size] = 0; + return out; + } + break; + case json_type_object: + { + if (!out) + { + out = malloc(strlen("object") + 1); + if (!out) + { + perror("malloc"); + return 0; + } + } + strcpy(out, "object"); + return out; + } + break; + default: + { + return out; + } + break; + } + return out; +} |