Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
Expand Down
17 changes: 15 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ else()
set(CBOR_RESTRICT_SPECIFIER "restrict")

set(CMAKE_C_FLAGS_DEBUG
"${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -g -ggdb -DDEBUG=true")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Wall -DNDEBUG")
"${CMAKE_C_FLAGS_DEBUG} -O0 -pedantic -Wall -Wextra -g -ggdb -DDEBUG=true")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -pedantic -Wall -Wextra -DNDEBUG")

if(SANITIZE)
set(CMAKE_C_FLAGS_DEBUG
Expand All @@ -124,6 +124,19 @@ else()
add_definitions(-DEIGHT_BYTE_SIZE_T)
endif()

include(CheckCSourceCompiles)

check_c_source_compiles("
int main() {
__builtin_unreachable();
return 0;
}
" HAS_BUILTIN_UNREACHABLE)

if (HAS_BUILTIN_UNREACHABLE)
add_definitions(-D_CBOR_HAS_BUILTIN_UNREACHABLE)
endif()

enable_testing()

set(CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind")
Expand Down
32 changes: 16 additions & 16 deletions examples/cjson2cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#include "cbor/internal/builder_callbacks.h"
#include "cbor/internal/loaders.h"

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

cbor_item_t *cjson_cbor_load(void *source,
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,
Expand All @@ -51,9 +51,9 @@ cbor_item_t *cjson_cbor_load(void *source,
return context.root;
}

void cjson_cbor_stream_decode(cJSON *source,
const struct cbor_callbacks *callbacks,
void *context) {
void cjson_cbor_stream_decode(cJSON* source,
const struct cbor_callbacks* callbacks,
void* context) {
switch (source->type) {
case cJSON_False: {
callbacks->boolean(context, false);
Expand Down Expand Up @@ -83,13 +83,13 @@ void cjson_cbor_stream_decode(cJSON *source,
}
case cJSON_String: {
// XXX: Assume cJSON handled unicode correctly
callbacks->string(context, (unsigned char *)source->valuestring,
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;
cJSON* item = source->child;
while (item != NULL) {
cjson_cbor_stream_decode(item, callbacks, context);
item = item->next;
Expand All @@ -98,9 +98,9 @@ void cjson_cbor_stream_decode(cJSON *source,
}
case cJSON_Object: {
callbacks->map_start(context, cJSON_GetArraySize(source));
cJSON *item = source->child;
cJSON* item = source->child;
while (item != NULL) {
callbacks->string(context, (unsigned char *)item->string,
callbacks->string(context, (unsigned char*)item->string,
strlen(item->string));
cjson_cbor_stream_decode(item, callbacks, context);
item = item->next;
Expand All @@ -115,24 +115,24 @@ void usage(void) {
exit(1);
}

int main(int argc, char *argv[]) {
int main(int argc, char* argv[]) {
if (argc != 2) usage();
FILE *f = fopen(argv[1], "rb");
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);
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);
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;
unsigned char* buffer;
size_t buffer_size;
cbor_serialize_alloc(cbor, &buffer, &buffer_size);

Expand Down
12 changes: 6 additions & 6 deletions examples/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* standard library functions.
*/

int compareUint(const void *a, const void *b) {
uint8_t av = cbor_get_uint8(*(cbor_item_t **)a),
bv = cbor_get_uint8(*(cbor_item_t **)b);
int compare_uint(const void* a, const void* b) {
uint8_t av = cbor_get_uint8(*(cbor_item_t**)a),
bv = cbor_get_uint8(*(cbor_item_t**)b);

if (av < bv)
return -1;
Expand All @@ -27,15 +27,15 @@ int compareUint(const void *a, const void *b) {
}

int main(void) {
cbor_item_t *array = cbor_new_definite_array(4);
cbor_item_t* array = cbor_new_definite_array(4);
bool success = cbor_array_push(array, cbor_move(cbor_build_uint8(4)));
success &= cbor_array_push(array, cbor_move(cbor_build_uint8(3)));
success &= cbor_array_push(array, cbor_move(cbor_build_uint8(1)));
success &= cbor_array_push(array, cbor_move(cbor_build_uint8(2)));
if (!success) return 1;

qsort(cbor_array_handle(array), cbor_array_size(array), sizeof(cbor_item_t *),
compareUint);
qsort(cbor_array_handle(array), cbor_array_size(array), sizeof(cbor_item_t*),
compare_uint);

cbor_describe(array, stdout);
fflush(stdout);
Expand Down
3 changes: 2 additions & 1 deletion examples/streaming_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void flush(size_t bytes) {
*/
int main(int argc, char* argv[]) {
if (argc != 2) usage();
long n = strtol(argv[1], NULL, 10);
size_t n;
scanf(argv[1], "%zu", &n);
out = freopen(NULL, "wb", stdout);
if (!out) exit(1);

Expand Down
8 changes: 1 addition & 7 deletions examples/streaming_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
#include <string.h>
#include "cbor.h"

#ifdef __GNUC__
#define UNUSED(x) __attribute__((__unused__)) x
#else
#define UNUSED(x) x
#endif

void usage(void) {
printf("Usage: streaming_parser [input file]\n");
exit(1);
Expand All @@ -30,7 +24,7 @@ void usage(void) {
const char* key = "a secret key";
bool key_found = false;

void find_string(void* UNUSED(_ctx), cbor_data buffer, uint64_t len) {
void find_string(void* _ctx _CBOR_UNUSED, cbor_data buffer, uint64_t len) {
if (key_found) {
printf("Found the value: %.*s\n", (int)len, buffer);
key_found = false;
Expand Down
54 changes: 29 additions & 25 deletions src/cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "cbor/internal/builder_callbacks.h"
#include "cbor/internal/loaders.h"

cbor_item_t *cbor_load(cbor_data source, size_t source_size,
struct cbor_load_result *result) {
cbor_item_t* cbor_load(cbor_data source, size_t source_size,
struct cbor_load_result* result) {
/* Context stack */
static struct cbor_callbacks callbacks = {
.uint8 = &cbor_builder_uint8_callback,
Expand Down Expand Up @@ -114,8 +114,8 @@
return NULL;
}

static cbor_item_t *_cbor_copy_int(cbor_item_t *item, bool negative) {
cbor_item_t *res = NULL;
static cbor_item_t* _cbor_copy_int(cbor_item_t* item, bool negative) {
cbor_item_t* res = NULL;
switch (cbor_int_get_width(item)) {
case CBOR_INT_8:
res = cbor_build_uint8(cbor_get_uint8(item));
Expand All @@ -136,8 +136,7 @@
return res;
}

static cbor_item_t *_cbor_copy_float_ctrl(cbor_item_t *item) {
// cppcheck-suppress missingReturn
static cbor_item_t* _cbor_copy_float_ctrl(cbor_item_t* item) {
switch (cbor_float_get_width(item)) {
case CBOR_FLOAT_0:
return cbor_build_ctrl(cbor_ctrl_value(item));
Expand All @@ -147,11 +146,13 @@
return cbor_build_float4(cbor_float_get_float4(item));
case CBOR_FLOAT_64:
return cbor_build_float8(cbor_float_get_float8(item));
default:

Check warning on line 149 in src/cbor.c

View check run for this annotation

Codecov / codecov/patch

src/cbor.c#L149

Added line #L149 was not covered by tests
_CBOR_UNREACHABLE;
return NULL;

Check warning on line 151 in src/cbor.c

View check run for this annotation

Codecov / codecov/patch

src/cbor.c#L151

Added line #L151 was not covered by tests
}
}

cbor_item_t *cbor_copy(cbor_item_t *item) {
// cppcheck-suppress missingReturn
cbor_item_t* cbor_copy(cbor_item_t* item) {
switch (cbor_typeof(item)) {
case CBOR_TYPE_UINT:
return _cbor_copy_int(item, false);
Expand All @@ -162,13 +163,13 @@
return cbor_build_bytestring(cbor_bytestring_handle(item),
cbor_bytestring_length(item));
} else {
cbor_item_t *res = cbor_new_indefinite_bytestring();
cbor_item_t* res = cbor_new_indefinite_bytestring();
if (res == NULL) {
return NULL;
}

for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++) {
cbor_item_t *chunk_copy =
cbor_item_t* chunk_copy =
cbor_copy(cbor_bytestring_chunks_handle(item)[i]);
if (chunk_copy == NULL) {
cbor_decref(&res);
Expand All @@ -185,16 +186,16 @@
}
case CBOR_TYPE_STRING:
if (cbor_string_is_definite(item)) {
return cbor_build_stringn((const char *)cbor_string_handle(item),
return cbor_build_stringn((const char*)cbor_string_handle(item),
cbor_string_length(item));
} else {
cbor_item_t *res = cbor_new_indefinite_string();
cbor_item_t* res = cbor_new_indefinite_string();
if (res == NULL) {
return NULL;
}

for (size_t i = 0; i < cbor_string_chunk_count(item); i++) {
cbor_item_t *chunk_copy =
cbor_item_t* chunk_copy =
cbor_copy(cbor_string_chunks_handle(item)[i]);
if (chunk_copy == NULL) {
cbor_decref(&res);
Expand All @@ -210,7 +211,7 @@
return res;
}
case CBOR_TYPE_ARRAY: {
cbor_item_t *res;
cbor_item_t* res;
if (cbor_array_is_definite(item)) {
res = cbor_new_definite_array(cbor_array_size(item));
} else {
Expand All @@ -221,7 +222,7 @@
}

for (size_t i = 0; i < cbor_array_size(item); i++) {
cbor_item_t *entry_copy = cbor_copy(cbor_move(cbor_array_get(item, i)));
cbor_item_t* entry_copy = cbor_copy(cbor_move(cbor_array_get(item, i)));
if (entry_copy == NULL) {
cbor_decref(&res);
return NULL;
Expand All @@ -236,7 +237,7 @@
return res;
}
case CBOR_TYPE_MAP: {
cbor_item_t *res;
cbor_item_t* res;
if (cbor_map_is_definite(item)) {
res = cbor_new_definite_map(cbor_map_size(item));
} else {
Expand All @@ -246,14 +247,14 @@
return NULL;
}

struct cbor_pair *it = cbor_map_handle(item);
struct cbor_pair* it = cbor_map_handle(item);
for (size_t i = 0; i < cbor_map_size(item); i++) {
cbor_item_t *key_copy = cbor_copy(it[i].key);
cbor_item_t* key_copy = cbor_copy(it[i].key);
if (key_copy == NULL) {
cbor_decref(&res);
return NULL;
}
cbor_item_t *value_copy = cbor_copy(it[i].value);
cbor_item_t* value_copy = cbor_copy(it[i].value);
if (value_copy == NULL) {
cbor_decref(&res);
cbor_decref(&key_copy);
Expand All @@ -272,16 +273,19 @@
return res;
}
case CBOR_TYPE_TAG: {
cbor_item_t *item_copy = cbor_copy(cbor_move(cbor_tag_item(item)));
cbor_item_t* item_copy = cbor_copy(cbor_move(cbor_tag_item(item)));
if (item_copy == NULL) {
return NULL;
}
cbor_item_t *tag = cbor_build_tag(cbor_tag_value(item), item_copy);
cbor_item_t* tag = cbor_build_tag(cbor_tag_value(item), item_copy);
cbor_decref(&item_copy);
return tag;
}
case CBOR_TYPE_FLOAT_CTRL:
return _cbor_copy_float_ctrl(item);
default:

Check warning on line 286 in src/cbor.c

View check run for this annotation

Codecov / codecov/patch

src/cbor.c#L286

Added line #L286 was not covered by tests
_CBOR_UNREACHABLE;
return NULL;

Check warning on line 288 in src/cbor.c

View check run for this annotation

Codecov / codecov/patch

src/cbor.c#L288

Added line #L288 was not covered by tests
}
}

Expand All @@ -300,11 +304,11 @@
return res;
}

static void _cbor_type_marquee(FILE *out, char *label, int indent) {
static void _cbor_type_marquee(FILE* out, char* label, int indent) {
fprintf(out, "%*.*s[%s] ", indent, indent, " ", label);
}

static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) {
static void _cbor_nested_describe(cbor_item_t* item, FILE* out, int indent) {
const int indent_offset = 4;
switch (cbor_typeof(item)) {
case CBOR_TYPE_UINT: {
Expand All @@ -328,7 +332,7 @@
_cbor_nested_describe(cbor_bytestring_chunks_handle(item)[i], out,
indent + indent_offset);
} else {
const unsigned char *data = cbor_bytestring_handle(item);
const unsigned char* data = cbor_bytestring_handle(item);
fprintf(out, "Definite, Length: %zuB, Data:\n",
cbor_bytestring_length(item));
fprintf(out, "%*s", indent + indent_offset, " ");
Expand Down Expand Up @@ -417,7 +421,7 @@
}
}

void cbor_describe(cbor_item_t *item, FILE *out) {
void cbor_describe(cbor_item_t* item, FILE* out) {
_cbor_nested_describe(item, out, 0);
}

Expand Down
Loading