diff options
author | 2019-11-14 21:11:34 +0000 | |
---|---|---|
committer | 2019-11-14 21:11:34 +0000 | |
commit | da0d961c2c28322a016c9e47e9898b8a67695a06 (patch) | |
tree | 98cc58b7052570a785fb26702de946f8f6519f3a /lib/libcbor/src/cbor/common.c | |
parent | Add missing cross-reference to NOTES section. (diff) | |
download | wireguard-openbsd-da0d961c2c28322a016c9e47e9898b8a67695a06.tar.xz wireguard-openbsd-da0d961c2c28322a016c9e47e9898b8a67695a06.zip |
Add libcbor; an implementation of the Concise Binary Object
Representation (CBOR) encoding format defined in RFC7049.
This is a dependency of libfido2, that we'll use for U2F/FIDO
support in OpenSSH.
feedback and "Looks good enough to me" deraadt@
Diffstat (limited to 'lib/libcbor/src/cbor/common.c')
-rw-r--r-- | lib/libcbor/src/cbor/common.c | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/lib/libcbor/src/cbor/common.c b/lib/libcbor/src/cbor/common.c new file mode 100644 index 00000000000..77544ae8107 --- /dev/null +++ b/lib/libcbor/src/cbor/common.c @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2014-2017 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 "cbor/common.h" +#include "arrays.h" +#include "bytestrings.h" +#include "data.h" +#include "floats_ctrls.h" +#include "ints.h" +#include "maps.h" +#include "strings.h" +#include "tags.h" + +bool cbor_isa_uint(const cbor_item_t *item) +{ + return item->type == CBOR_TYPE_UINT; +} + +bool cbor_isa_negint(const cbor_item_t *item) +{ + return item->type == CBOR_TYPE_NEGINT; +} + +bool cbor_isa_bytestring(const cbor_item_t *item) +{ + return item->type == CBOR_TYPE_BYTESTRING; +} + +bool cbor_isa_string(const cbor_item_t *item) +{ + return item->type == CBOR_TYPE_STRING; +} + +bool cbor_isa_array(const cbor_item_t *item) +{ + return item->type == CBOR_TYPE_ARRAY; +} + +bool cbor_isa_map(const cbor_item_t *item) +{ + return item->type == CBOR_TYPE_MAP; +} + +bool cbor_isa_tag(const cbor_item_t *item) +{ + return item->type == CBOR_TYPE_TAG; +} + +bool cbor_isa_float_ctrl(const cbor_item_t *item) +{ + return item->type == CBOR_TYPE_FLOAT_CTRL; +} + + +cbor_type cbor_typeof(const cbor_item_t *item) +{ + return item->type; +} + + +bool cbor_is_int(const cbor_item_t *item) +{ + return cbor_isa_uint(item) || cbor_isa_negint(item); +} + + +bool cbor_is_bool(const cbor_item_t *item) +{ + return cbor_isa_float_ctrl(item) && + (cbor_ctrl_value(item) == CBOR_CTRL_FALSE || cbor_ctrl_value(item) == CBOR_CTRL_TRUE); +} + +bool cbor_is_null(const cbor_item_t *item) +{ + return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_NULL; +} + +bool cbor_is_undef(const cbor_item_t *item) +{ + return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_UNDEF; +} + +bool cbor_is_float(const cbor_item_t *item) +{ + return cbor_isa_float_ctrl(item) && !cbor_float_ctrl_is_ctrl(item); +} + + +cbor_item_t * cbor_incref(cbor_item_t *item) +{ + item->refcount++; + return item; +} + +void cbor_decref(cbor_item_t **item_ref) +{ + cbor_item_t * item = *item_ref; + if (--item->refcount == 0) { + switch (item->type) { + case CBOR_TYPE_UINT: + /* Fallthrough */ + case CBOR_TYPE_NEGINT: + /* Combined allocation, freeing the item suffices */ + { + break; + } + case CBOR_TYPE_BYTESTRING: { + if (cbor_bytestring_is_definite(item)) { + _CBOR_FREE(item->data); + } else { + /* We need to decref all chunks */ + cbor_item_t **handle = cbor_bytestring_chunks_handle(item); + for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++) + cbor_decref(&handle[i]); + _CBOR_FREE(((struct cbor_indefinite_string_data *) item->data)->chunks); + _CBOR_FREE(item->data); + } + break; + } + case CBOR_TYPE_STRING: { + if (cbor_string_is_definite(item)) { + _CBOR_FREE(item->data); + } else { + /* We need to decref all chunks */ + cbor_item_t **handle = cbor_string_chunks_handle(item); + for (size_t i = 0; i < cbor_string_chunk_count(item); i++) + cbor_decref(&handle[i]); + _CBOR_FREE(((struct cbor_indefinite_string_data *) item->data)->chunks); + _CBOR_FREE(item->data); + } + break; + } + case CBOR_TYPE_ARRAY: { + /* Get all items and decref them */ + cbor_item_t **handle = cbor_array_handle(item); + size_t size = cbor_array_size(item); + for (size_t i = 0; i < size; i++) + if (handle[i] != NULL) + cbor_decref(&handle[i]); + _CBOR_FREE(item->data); + break; + } + case CBOR_TYPE_MAP: { + struct cbor_pair *handle = cbor_map_handle(item); + for (size_t i = 0; i < item->metadata.map_metadata.end_ptr; i++, handle++) { + cbor_decref(&handle->key); + if (handle->value != NULL) + cbor_decref(&handle->value); + } + _CBOR_FREE(item->data); + break; + }; + case CBOR_TYPE_TAG: { + if (item->metadata.tag_metadata.tagged_item != NULL) + cbor_decref(&item->metadata.tag_metadata.tagged_item); + _CBOR_FREE(item->data); + break; + } + case CBOR_TYPE_FLOAT_CTRL: { + /* Floats have combined allocation */ + break; + } + } + _CBOR_FREE(item); + //TODO + *item_ref = NULL; + } +} + +void cbor_intermediate_decref(cbor_item_t * item) +{ + cbor_decref(&item); +} + +size_t cbor_refcount(const cbor_item_t * item) +{ + return item->refcount; +} + +cbor_item_t * cbor_move(cbor_item_t * item) +{ + item->refcount--; + return item; +} |