Skip to content
Permalink
Browse files

Reformat the world

  • Loading branch information...
PJK committed Feb 2, 2019
1 parent f9363ce commit af70f0b7369f2e1c79b0ca6c676c39b7ee09e602
Showing with 6,335 additions and 6,908 deletions.
  1. +1 −2 demo/hello_cbor.c
  2. +121 −130 examples/cjson2cbor.c
  3. +21 −22 examples/create_items.c
  4. +6 −7 examples/hello.c
  5. +57 −62 examples/readfile.c
  6. +20 −23 examples/sort.c
  7. +34 −41 examples/streaming_parser.c
  8. +5 −5 src/allocators.c
  9. +315 −359 src/cbor.c
  10. +13 −14 src/cbor.h
  11. +103 −122 src/cbor/arrays.c
  12. +16 −15 src/cbor/arrays.h
  13. +86 −92 src/cbor/bytestrings.c
  14. +23 −17 src/cbor/bytestrings.h
  15. +72 −60 src/cbor/callbacks.c
  16. +67 −68 src/cbor/callbacks.h
  17. +133 −162 src/cbor/common.c
  18. +89 −74 src/cbor/common.h
  19. +111 −94 src/cbor/data.h
  20. +126 −120 src/cbor/encoding.c
  21. +11 −11 src/cbor/encoding.h
  22. +138 −164 src/cbor/floats_ctrls.c
  23. +25 −26 src/cbor/floats_ctrls.h
  24. +305 −317 src/cbor/internal/builder_callbacks.c
  25. +8 −8 src/cbor/internal/builder_callbacks.h
  26. +76 −75 src/cbor/internal/encoders.c
  27. +11 −7 src/cbor/internal/encoders.h
  28. +51 −61 src/cbor/internal/loaders.c
  29. +1 −1 src/cbor/internal/loaders.h
  30. +24 −26 src/cbor/internal/memory_utils.c
  31. +9 −7 src/cbor/internal/memory_utils.h
  32. +17 −18 src/cbor/internal/stack.c
  33. +8 −7 src/cbor/internal/stack.h
  34. +67 −42 src/cbor/internal/unicode.c
  35. +6 −8 src/cbor/internal/unicode.h
  36. +176 −205 src/cbor/ints.c
  37. +13 −10 src/cbor/ints.h
  38. +95 −109 src/cbor/maps.c
  39. +15 −11 src/cbor/maps.h
  40. +235 −236 src/cbor/serialization.c
  41. +17 −14 src/cbor/serialization.h
  42. +652 −671 src/cbor/streaming.c
  43. +7 −9 src/cbor/streaming.h
  44. +97 −105 src/cbor/strings.c
  45. +24 −17 src/cbor/strings.h
  46. +24 −28 src/cbor/tags.c
  47. +6 −6 src/cbor/tags.h
  48. +26 −30 test/assertions.c
  49. +8 −7 test/assertions.h
  50. +70 −80 test/bad_inputs_test.c
  51. +22 −18 test/callbacks_test.c
  52. +241 −252 test/cbor_serialize_test.c
  53. +520 −691 test/cbor_stream_decode_test.c
  54. +162 −206 test/copy_test.c
  55. +1 −5 test/cpp_linkage_test.cpp
  56. +45 −58 test/fuzz_test.c
  57. +109 −130 test/memory_allocation_test.c
  58. +22 −22 test/pretty_printer_test.c
  59. +254 −306 test/stream_expectations.c
  60. +55 −55 test/stream_expectations.h
  61. +47 −49 test/type_0_encoders_test.c
  62. +106 −119 test/type_0_test.c
  63. +48 −49 test/type_1_encoders_test.c
  64. +71 −82 test/type_1_test.c
  65. +20 −25 test/type_2_encoders_test.c
  66. +269 −249 test/type_2_test.c
  67. +20 −25 test/type_3_encoders_test.c
  68. +177 −175 test/type_3_test.c
  69. +32 −38 test/type_4_encoders_test.c
  70. +93 −102 test/type_4_test.c
  71. +19 −25 test/type_5_encoders_test.c
  72. +140 −157 test/type_5_test.c
  73. +16 −19 test/type_6_encoders_test.c
  74. +56 −71 test/type_6_test.c
  75. +65 −76 test/type_7_encoders_test.c
  76. +67 −77 test/type_7_test.c
  77. +17 −22 test/unicode_test.c
@@ -8,7 +8,6 @@
#include <cbor.h>
#include <stdio.h>

int main(int argc, char * argv[])
{
int main(int argc, char* argv[]) {
printf("Hello from libcbor %s\n", CBOR_VERSION);
}
@@ -5,150 +5,141 @@
* it under the terms of the MIT license. See LICENSE for details.
*/


/**
* This code demonstrates how cJSON (https://github.com/DaveGamble/cJSON) callbacks
* can be used in conjuction with the streaming parser to translate JSON to CBOR.
* Please note that cbor_builder_* APIs are internal and thus subject to change.
* This code demonstrates how cJSON (https://github.com/DaveGamble/cJSON)
* callbacks can be used in conjuction with the streaming parser to translate
* JSON to CBOR. Please note that cbor_builder_* APIs are internal and thus
* subject to change.
*
* The example will only be compiled when cJSON is available
*/

#include <string.h>
#include <math.h>
#include <float.h>
#include <cjson/cJSON.h>
#include <float.h>
#include <math.h>
#include <string.h>
#include "cbor.h"
#include "cbor/internal/builder_callbacks.h"
#include "cbor/internal/loaders.h"

typedef void(*cbor_load_callback_t)(cJSON *, const struct cbor_callbacks *, void *);

cbor_item_t * cjson_cbor_load(void *source, cbor_load_callback_t cbor_load_callback)
{
static struct cbor_callbacks callbacks = {
.uint64 = &cbor_builder_uint64_callback,
.negint64 = &cbor_builder_negint64_callback,
.string = &cbor_builder_string_callback,
.array_start = &cbor_builder_array_start_callback,
.map_start = &cbor_builder_map_start_callback,
.null = &cbor_builder_null_callback,
.boolean = &cbor_builder_boolean_callback,
.float4 = &cbor_builder_float4_callback,
};

/* Context stack */
struct _cbor_stack stack = _cbor_stack_init();

/* Target for callbacks */
struct _cbor_decoder_context context = (struct _cbor_decoder_context) {
.stack = &stack,
};

cbor_load_callback(source, &callbacks, &context);

return context.root;
typedef void (*cbor_load_callback_t)(cJSON *, const struct cbor_callbacks *,
void *);

cbor_item_t *cjson_cbor_load(void *source,
cbor_load_callback_t cbor_load_callback) {
static struct cbor_callbacks callbacks = {
.uint64 = &cbor_builder_uint64_callback,
.negint64 = &cbor_builder_negint64_callback,
.string = &cbor_builder_string_callback,
.array_start = &cbor_builder_array_start_callback,
.map_start = &cbor_builder_map_start_callback,
.null = &cbor_builder_null_callback,
.boolean = &cbor_builder_boolean_callback,
.float4 = &cbor_builder_float4_callback,
};

/* Context stack */
struct _cbor_stack stack = _cbor_stack_init();

/* Target for callbacks */
struct _cbor_decoder_context context = (struct _cbor_decoder_context){
.stack = &stack,
};

cbor_load_callback(source, &callbacks, &context);

return context.root;
}

void cjson_cbor_stream_decode(cJSON * source,
const struct cbor_callbacks *callbacks,
void * context)
{
switch (source->type) {
case cJSON_False:
{
callbacks->boolean(context, false);
return;
}
case cJSON_True:
{
callbacks->boolean(context, true);
return;
}
case cJSON_NULL:
{
callbacks->null(context);
return;
}
case cJSON_Number:
{
// This is stupid -- ints and doubles cannot are not distinguished
if (fabs(source->valuedouble - source->valueint) > DBL_EPSILON) {
callbacks->float4(context, source->valuedouble);
} else {
// XXX: This is not portable
if (source->valueint >= 0) {
callbacks->uint64(context, source->valueint);
} else {
callbacks->negint64(context, source->valueint + 1);
}
}
return;
}
case cJSON_String:
{
// XXX: Assume cJSON handled unicode correctly
callbacks->string(context, (unsigned char *)source->valuestring, strlen(source->valuestring));
return;
}
case cJSON_Array:
{
callbacks->array_start(context, cJSON_GetArraySize(source));
cJSON *item = source->child;
while (item != NULL) {
cjson_cbor_stream_decode(item, callbacks, context);
item = item->next;
}
return;
}
case cJSON_Object:
{
callbacks->map_start(context, cJSON_GetArraySize(source));
cJSON *item = source->child;
while (item != NULL) {
callbacks->string(context, (unsigned char *) item->string, strlen(item->string));
cjson_cbor_stream_decode(item, callbacks, context);
item = item->next;
}
return;
}
}
void cjson_cbor_stream_decode(cJSON *source,
const struct cbor_callbacks *callbacks,
void *context) {
switch (source->type) {
case cJSON_False: {
callbacks->boolean(context, false);
return;
}
case cJSON_True: {
callbacks->boolean(context, true);
return;
}
case cJSON_NULL: {
callbacks->null(context);
return;
}
case cJSON_Number: {
// This is stupid -- ints and doubles cannot are not distinguished
if (fabs(source->valuedouble - source->valueint) > DBL_EPSILON) {
callbacks->float4(context, source->valuedouble);
} else {
// XXX: This is not portable
if (source->valueint >= 0) {
callbacks->uint64(context, source->valueint);
} else {
callbacks->negint64(context, source->valueint + 1);
}
}
return;
}
case cJSON_String: {
// XXX: Assume cJSON handled unicode correctly
callbacks->string(context, (unsigned char *)source->valuestring,
strlen(source->valuestring));
return;
}
case cJSON_Array: {
callbacks->array_start(context, cJSON_GetArraySize(source));
cJSON *item = source->child;
while (item != NULL) {
cjson_cbor_stream_decode(item, callbacks, context);
item = item->next;
}
return;
}
case cJSON_Object: {
callbacks->map_start(context, cJSON_GetArraySize(source));
cJSON *item = source->child;
while (item != NULL) {
callbacks->string(context, (unsigned char *)item->string,
strlen(item->string));
cjson_cbor_stream_decode(item, callbacks, context);
item = item->next;
}
return;
}
}
}

void usage()
{
printf("Usage: cjson [input JSON file]\n");
exit(1);
void usage() {
printf("Usage: cjson [input JSON file]\n");
exit(1);
}

int main(int argc, char * argv[])
{
if (argc != 2)
usage();
FILE * f = fopen(argv[1], "rb");
if (f == NULL)
usage();
/* Read input file into a buffer (cJSON doesn't work with streams) */
fseek(f, 0, SEEK_END);
size_t length = (size_t)ftell(f);
fseek(f, 0, SEEK_SET);
char * json_buffer = malloc(length + 1);
fread(json_buffer, length, 1, f);
json_buffer[length] = '\0';

/* Convert between JSON and CBOR */
cJSON * json = cJSON_Parse(json_buffer);
cbor_item_t * cbor = cjson_cbor_load(json, cjson_cbor_stream_decode);

/* Print out CBOR bytes */
unsigned char * buffer;
size_t buffer_size,
cbor_length = cbor_serialize_alloc(cbor, &buffer, &buffer_size);

fwrite(buffer, 1, cbor_length, stdout);

free(buffer);
fflush(stdout);
cJSON_Delete(json);
cbor_decref(&cbor);
int main(int argc, char *argv[]) {
if (argc != 2) usage();
FILE *f = fopen(argv[1], "rb");
if (f == NULL) usage();
/* Read input file into a buffer (cJSON doesn't work with streams) */
fseek(f, 0, SEEK_END);
size_t length = (size_t)ftell(f);
fseek(f, 0, SEEK_SET);
char *json_buffer = malloc(length + 1);
fread(json_buffer, length, 1, f);
json_buffer[length] = '\0';

/* Convert between JSON and CBOR */
cJSON *json = cJSON_Parse(json_buffer);
cbor_item_t *cbor = cjson_cbor_load(json, cjson_cbor_stream_decode);

/* Print out CBOR bytes */
unsigned char *buffer;
size_t buffer_size,
cbor_length = cbor_serialize_alloc(cbor, &buffer, &buffer_size);

fwrite(buffer, 1, cbor_length, stdout);

free(buffer);
fflush(stdout);
cJSON_Delete(json);
cbor_decref(&cbor);
}
@@ -5,30 +5,29 @@
* it under the terms of the MIT license. See LICENSE for details.
*/

#include "cbor.h"
#include <stdio.h>
#include "cbor.h"

int main(int argc, char * argv[])
{
/* Preallocate the map structure */
cbor_item_t * root = cbor_new_definite_map(2);
/* Add the content */
cbor_map_add(root, (struct cbor_pair) {
.key = cbor_move(cbor_build_string("Is CBOR awesome?")),
.value = cbor_move(cbor_build_bool(true))
});
cbor_map_add(root, (struct cbor_pair) {
.key = cbor_move(cbor_build_uint8(42)),
.value = cbor_move(cbor_build_string("Is the answer"))
});
/* Output: `length` bytes of data in the `buffer` */
unsigned char * buffer;
size_t buffer_size,
length = cbor_serialize_alloc(root, &buffer, &buffer_size);
int main(int argc, char* argv[]) {
/* Preallocate the map structure */
cbor_item_t* root = cbor_new_definite_map(2);
/* Add the content */
cbor_map_add(root,
(struct cbor_pair){
.key = cbor_move(cbor_build_string("Is CBOR awesome?")),
.value = cbor_move(cbor_build_bool(true))});
cbor_map_add(root,
(struct cbor_pair){
.key = cbor_move(cbor_build_uint8(42)),
.value = cbor_move(cbor_build_string("Is the answer"))});
/* Output: `length` bytes of data in the `buffer` */
unsigned char* buffer;
size_t buffer_size,
length = cbor_serialize_alloc(root, &buffer, &buffer_size);

fwrite(buffer, 1, length, stdout);
free(buffer);
fwrite(buffer, 1, length, stdout);
free(buffer);

fflush(stdout);
cbor_decref(&root);
fflush(stdout);
cbor_decref(&root);
}
@@ -5,13 +5,12 @@
* it under the terms of the MIT license. See LICENSE for details.
*/

#include "cbor.h"
#include <stdio.h>
#include "cbor.h"

int main(int argc, char * argv[])
{
printf("Hello from libcbor %s\n", CBOR_VERSION);
printf("Custom allocation support: %s\n", CBOR_CUSTOM_ALLOC ? "yes" : "no");
printf("Pretty-printer support: %s\n", CBOR_PRETTY_PRINTER ? "yes" : "no");
printf("Buffer growth factor: %f\n", (float) CBOR_BUFFER_GROWTH);
int main(int argc, char* argv[]) {
printf("Hello from libcbor %s\n", CBOR_VERSION);
printf("Custom allocation support: %s\n", CBOR_CUSTOM_ALLOC ? "yes" : "no");
printf("Pretty-printer support: %s\n", CBOR_PRETTY_PRINTER ? "yes" : "no");
printf("Buffer growth factor: %f\n", (float)CBOR_BUFFER_GROWTH);
}
Oops, something went wrong.

0 comments on commit af70f0b

Please sign in to comment.
You can’t perform that action at this time.