diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | examples/bazel/third_party/libcbor/cbor/configuration.h | 4 | ||||
| -rw-r--r-- | examples/cbor2cjson.c | 123 | ||||
| -rw-r--r-- | examples/cjson2cbor.c | 4 | ||||
| -rw-r--r-- | examples/data/all_types.cbor | bin | 0 -> 354 bytes | |||
| -rw-r--r-- | examples/readfile.c | 2 |
6 files changed, 132 insertions, 5 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index eaf985ba32cd..b1f2ec290e7a 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -22,6 +22,10 @@ if(CJSON_FOUND) add_executable(cjson2cbor cjson2cbor.c) target_include_directories(cjson2cbor PUBLIC ${CJSON_INCLUDE_DIRS}) target_link_libraries(cjson2cbor cbor ${CJSON_LIBRARY}) + + add_executable(cbor2cjson cbor2cjson.c) + target_include_directories(cbor2cjson PUBLIC ${CJSON_INCLUDE_DIRS}) + target_link_libraries(cbor2cjson cbor ${CJSON_LIBRARY}) endif() file(COPY data DESTINATION .) diff --git a/examples/bazel/third_party/libcbor/cbor/configuration.h b/examples/bazel/third_party/libcbor/cbor/configuration.h index 3472cf49d8fe..ddf6b9dc5f2b 100644 --- a/examples/bazel/third_party/libcbor/cbor/configuration.h +++ b/examples/bazel/third_party/libcbor/cbor/configuration.h @@ -2,8 +2,8 @@ #define LIBCBOR_CONFIGURATION_H #define CBOR_MAJOR_VERSION 0 -#define CBOR_MINOR_VERSION 10 -#define CBOR_PATCH_VERSION 2 +#define CBOR_MINOR_VERSION 11 +#define CBOR_PATCH_VERSION 0 #define CBOR_BUFFER_GROWTH 2 #define CBOR_MAX_STACK_SIZE 2048 diff --git a/examples/cbor2cjson.c b/examples/cbor2cjson.c new file mode 100644 index 000000000000..99ddc9ee09e1 --- /dev/null +++ b/examples/cbor2cjson.c @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com> + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include <cjson/cJSON.h> +#include <stdio.h> +#include <string.h> + +#include "cbor.h" + +void usage(void) { + printf("Usage: cbor2cjson [input file]\n"); + exit(1); +} + +cJSON* cbor_to_cjson(cbor_item_t* item) { + switch (cbor_typeof(item)) { + case CBOR_TYPE_UINT: + return cJSON_CreateNumber(cbor_get_int(item)); + case CBOR_TYPE_NEGINT: + return cJSON_CreateNumber(-1 - cbor_get_int(item)); + case CBOR_TYPE_BYTESTRING: + // cJSON only handles null-terminated string -- binary data would have to + // be escaped + return cJSON_CreateString("Unsupported CBOR item: Bytestring"); + case CBOR_TYPE_STRING: + if (cbor_string_is_definite(item)) { + // cJSON only handles null-terminated string + char* null_terminated_string = malloc(cbor_string_length(item) + 1); + memcpy(null_terminated_string, cbor_string_handle(item), + cbor_string_length(item)); + null_terminated_string[cbor_string_length(item)] = 0; + cJSON* result = cJSON_CreateString(null_terminated_string); + free(null_terminated_string); + return result; + } + return cJSON_CreateString("Unsupported CBOR item: Chunked string"); + case CBOR_TYPE_ARRAY: { + cJSON* result = cJSON_CreateArray(); + for (size_t i = 0; i < cbor_array_size(item); i++) { + cJSON_AddItemToArray(result, cbor_to_cjson(cbor_array_get(item, i))); + } + return result; + } + case CBOR_TYPE_MAP: { + cJSON* result = cJSON_CreateObject(); + for (size_t i = 0; i < cbor_map_size(item); i++) { + char* key = malloc(128); + snprintf(key, 128, "Surrogate key %zu", i); + // JSON only support string keys + if (cbor_isa_string(cbor_map_handle(item)[i].key) && + cbor_string_is_definite(cbor_map_handle(item)[i].key)) { + size_t key_length = cbor_string_length(cbor_map_handle(item)[i].key); + if (key_length > 127) key_length = 127; + // Null-terminated madness + memcpy(key, cbor_string_handle(cbor_map_handle(item)[i].key), + key_length); + key[key_length] = 0; + } + + cJSON_AddItemToObject(result, key, + cbor_to_cjson(cbor_map_handle(item)[i].value)); + free(key); + } + return result; + } + case CBOR_TYPE_TAG: + return cJSON_CreateString("Unsupported CBOR item: Tag"); + case CBOR_TYPE_FLOAT_CTRL: + if (cbor_float_ctrl_is_ctrl(item)) { + if (cbor_is_bool(item)) return cJSON_CreateBool(cbor_get_bool(item)); + if (cbor_is_null(item)) return cJSON_CreateNull(); + return cJSON_CreateString("Unsupported CBOR item: Control value"); + } + return cJSON_CreateNumber(cbor_float_get_float(item)); + } + + return cJSON_CreateNull(); +} + +/* + * Reads CBOR data from a file and outputs JSON using cJSON + * $ ./examples/cbor2cjson examples/data/nested_array.cbor + */ + +int main(int argc, char* argv[]) { + if (argc != 2) usage(); + FILE* f = fopen(argv[1], "rb"); + if (f == NULL) usage(); + fseek(f, 0, SEEK_END); + size_t length = (size_t)ftell(f); + fseek(f, 0, SEEK_SET); + unsigned char* buffer = malloc(length); + fread(buffer, length, 1, f); + + /* Assuming `buffer` contains `length` bytes of input data */ + struct cbor_load_result result; + cbor_item_t* item = cbor_load(buffer, length, &result); + free(buffer); + + if (result.error.code != CBOR_ERR_NONE) { + printf( + "There was an error while reading the input near byte %zu (read %zu " + "bytes in total): ", + result.error.position, result.read); + exit(1); + } + + cJSON* cjson_item = cbor_to_cjson(item); + char* json_string = cJSON_Print(cjson_item); + printf("%s\n", json_string); + free(json_string); + fflush(stdout); + + /* Deallocate the result */ + cbor_decref(&item); + cJSON_Delete(cjson_item); + + fclose(f); +} diff --git a/examples/cjson2cbor.c b/examples/cjson2cbor.c index eae78e1cf7a6..b67439902442 100644 --- a/examples/cjson2cbor.c +++ b/examples/cjson2cbor.c @@ -7,7 +7,7 @@ /** * This code demonstrates how cJSON (https://github.com/DaveGamble/cJSON) - * callbacks can be used in conjuction with the streaming parser to translate + * callbacks can be used in conjunction with the streaming parser to translate * JSON to CBOR. Please note that cbor_builder_* APIs are internal and thus * subject to change. * @@ -111,7 +111,7 @@ void cjson_cbor_stream_decode(cJSON *source, } void usage(void) { - printf("Usage: cjson [input JSON file]\n"); + printf("Usage: cjson2cbor [input JSON file]\n"); exit(1); } diff --git a/examples/data/all_types.cbor b/examples/data/all_types.cbor Binary files differnew file mode 100644 index 000000000000..00d66a0e667e --- /dev/null +++ b/examples/data/all_types.cbor diff --git a/examples/readfile.c b/examples/readfile.c index 3f8db6152800..c6f4a3342d08 100644 --- a/examples/readfile.c +++ b/examples/readfile.c @@ -58,7 +58,7 @@ int main(int argc, char* argv[]) { case CBOR_ERR_SYNTAXERROR: { printf( "Syntactically malformed data -- see " - "http://tools.ietf.org/html/rfc7049\n"); + "https://www.rfc-editor.org/info/std94\n"); break; } case CBOR_ERR_NONE: { |
