From cc3f33db7a8d3c4ad373e646b199808e01bc5d9b Mon Sep 17 00:00:00 2001 From: sss Date: Tue, 17 Jan 2023 00:38:19 +0300 Subject: added webrdp public code --- src/core/json_helpers.c | 275 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 275 insertions(+) create mode 100644 src/core/json_helpers.c (limited to 'src/core/json_helpers.c') 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 , sss . + * + */ + +#include +#include +#include +#include +#include + +#include + +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; +} -- cgit v1.2.3