xref: /freebsd/contrib/libcbor/src/cbor/tags.c (revision d411c1d6)
1 /*
2  * Copyright (c) 2014-2020 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 
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 
23 cbor_item_t *cbor_tag_item(const cbor_item_t *item) {
24   CBOR_ASSERT(cbor_isa_tag(item));
25   return cbor_incref(item->metadata.tag_metadata.tagged_item);
26 }
27 
28 uint64_t cbor_tag_value(const cbor_item_t *item) {
29   CBOR_ASSERT(cbor_isa_tag(item));
30   return item->metadata.tag_metadata.value;
31 }
32 
33 void cbor_tag_set_item(cbor_item_t *item, cbor_item_t *tagged_item) {
34   CBOR_ASSERT(cbor_isa_tag(item));
35   cbor_incref(tagged_item);
36   item->metadata.tag_metadata.tagged_item = tagged_item;
37 }
38 
39 cbor_item_t *cbor_build_tag(uint64_t value, cbor_item_t *item) {
40   cbor_item_t *res = cbor_new_tag(value);
41   if (res == NULL) {
42     return NULL;
43   }
44   cbor_tag_set_item(res, item);
45   return res;
46 }
47