Path: blob/main/contrib/libcbor/examples/cbor2cjson.c
39586 views
/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#include <cjson/cJSON.h>8#include <stdio.h>9#include <string.h>1011#include "cbor.h"1213void usage(void) {14printf("Usage: cbor2cjson [input file]\n");15exit(1);16}1718cJSON* cbor_to_cjson(cbor_item_t* item) {19switch (cbor_typeof(item)) {20case CBOR_TYPE_UINT:21return cJSON_CreateNumber(cbor_get_int(item));22case CBOR_TYPE_NEGINT:23return cJSON_CreateNumber(-1 - cbor_get_int(item));24case CBOR_TYPE_BYTESTRING:25// cJSON only handles null-terminated string -- binary data would have to26// be escaped27return cJSON_CreateString("Unsupported CBOR item: Bytestring");28case CBOR_TYPE_STRING:29if (cbor_string_is_definite(item)) {30// cJSON only handles null-terminated string31char* null_terminated_string = malloc(cbor_string_length(item) + 1);32memcpy(null_terminated_string, cbor_string_handle(item),33cbor_string_length(item));34null_terminated_string[cbor_string_length(item)] = 0;35cJSON* result = cJSON_CreateString(null_terminated_string);36free(null_terminated_string);37return result;38}39return cJSON_CreateString("Unsupported CBOR item: Chunked string");40case CBOR_TYPE_ARRAY: {41cJSON* result = cJSON_CreateArray();42for (size_t i = 0; i < cbor_array_size(item); i++) {43cJSON_AddItemToArray(result, cbor_to_cjson(cbor_array_get(item, i)));44}45return result;46}47case CBOR_TYPE_MAP: {48cJSON* result = cJSON_CreateObject();49for (size_t i = 0; i < cbor_map_size(item); i++) {50char* key = malloc(128);51snprintf(key, 128, "Surrogate key %zu", i);52// JSON only support string keys53if (cbor_isa_string(cbor_map_handle(item)[i].key) &&54cbor_string_is_definite(cbor_map_handle(item)[i].key)) {55size_t key_length = cbor_string_length(cbor_map_handle(item)[i].key);56if (key_length > 127) key_length = 127;57// Null-terminated madness58memcpy(key, cbor_string_handle(cbor_map_handle(item)[i].key),59key_length);60key[key_length] = 0;61}6263cJSON_AddItemToObject(result, key,64cbor_to_cjson(cbor_map_handle(item)[i].value));65free(key);66}67return result;68}69case CBOR_TYPE_TAG:70return cJSON_CreateString("Unsupported CBOR item: Tag");71case CBOR_TYPE_FLOAT_CTRL:72if (cbor_float_ctrl_is_ctrl(item)) {73if (cbor_is_bool(item)) return cJSON_CreateBool(cbor_get_bool(item));74if (cbor_is_null(item)) return cJSON_CreateNull();75return cJSON_CreateString("Unsupported CBOR item: Control value");76}77return cJSON_CreateNumber(cbor_float_get_float(item));78}7980return cJSON_CreateNull();81}8283/*84* Reads CBOR data from a file and outputs JSON using cJSON85* $ ./examples/cbor2cjson examples/data/nested_array.cbor86*/8788int main(int argc, char* argv[]) {89if (argc != 2) usage();90FILE* f = fopen(argv[1], "rb");91if (f == NULL) usage();92fseek(f, 0, SEEK_END);93size_t length = (size_t)ftell(f);94fseek(f, 0, SEEK_SET);95unsigned char* buffer = malloc(length);96fread(buffer, length, 1, f);9798/* Assuming `buffer` contains `length` bytes of input data */99struct cbor_load_result result;100cbor_item_t* item = cbor_load(buffer, length, &result);101free(buffer);102103if (result.error.code != CBOR_ERR_NONE) {104printf(105"There was an error while reading the input near byte %zu (read %zu "106"bytes in total): ",107result.error.position, result.read);108exit(1);109}110111cJSON* cjson_item = cbor_to_cjson(item);112char* json_string = cJSON_Print(cjson_item);113printf("%s\n", json_string);114free(json_string);115fflush(stdout);116117/* Deallocate the result */118cbor_decref(&item);119cJSON_Delete(cjson_item);120121fclose(f);122}123124125