xref: /netbsd-src/external/mit/libcbor/dist/src/cbor/tags.c (revision 5dd36a3bc8bf2a9dec29ceb6349550414570c447)
1 /*
2  * Copyright (c) 2014-2019 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #include "tags.h"
9 
cbor_new_tag(uint64_t value)10 cbor_item_t *cbor_new_tag(uint64_t value) {
11   cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
12   _CBOR_NOTNULL(item);
13 
14   *item = (cbor_item_t){
15       .refcount = 1,
16       .type = CBOR_TYPE_TAG,
17       .metadata = {.tag_metadata = {.value = value, .tagged_item = NULL}},
18       .data = NULL /* Never used */
19   };
20   return item;
21 }
22 
cbor_tag_item(const cbor_item_t * item)23 cbor_item_t *cbor_tag_item(const cbor_item_t *item) {
24   assert(cbor_isa_tag(item));
25   return item->metadata.tag_metadata.tagged_item;
26 }
27 
cbor_tag_value(const cbor_item_t * item)28 uint64_t cbor_tag_value(const cbor_item_t *item) {
29   assert(cbor_isa_tag(item));
30   return item->metadata.tag_metadata.value;
31 }
32 
cbor_tag_set_item(cbor_item_t * item,cbor_item_t * tagged_item)33 void cbor_tag_set_item(cbor_item_t *item, cbor_item_t *tagged_item) {
34   assert(cbor_isa_tag(item));
35   cbor_incref(tagged_item);
36   item->metadata.tag_metadata.tagged_item = tagged_item;
37 }
38 
cbor_build_tag(uint64_t value,cbor_item_t * item)39 cbor_item_t *cbor_build_tag(uint64_t value, cbor_item_t *item) {
40   cbor_item_t *res = cbor_new_tag(value);
41   cbor_tag_set_item(res, item);
42   return res;
43 }
44