libcbor  0.5.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
tags.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2017 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 {
12  cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
13  *item = (cbor_item_t) {
14  .refcount = 1,
15  .type = CBOR_TYPE_TAG,
16  .metadata = {.tag_metadata = {.value = value, .tagged_item = NULL}},
17  .data = NULL /* Never used */
18  };
19  return item;
20 }
21 
23 {
24  assert(cbor_isa_tag(item));
25  return item->metadata.tag_metadata.tagged_item;
26 }
27 
28 uint64_t cbor_tag_value(const cbor_item_t *item)
29 {
30  assert(cbor_isa_tag(item));
31  return item->metadata.tag_metadata.value;
32 }
33 
34 void cbor_tag_set_item(cbor_item_t *item, cbor_item_t *tagged_item)
35 {
36  assert(cbor_isa_tag(item));
37  cbor_incref(tagged_item);
38  item->metadata.tag_metadata.tagged_item = tagged_item;
39 }
40 
41 cbor_item_t * cbor_build_tag(uint64_t value, cbor_item_t * item) {
42  cbor_item_t *res = cbor_new_tag(value);
43  cbor_tag_set_item(res, item);
44  return res;
45 }
struct cbor_item_t cbor_item_t
The item handle.
6 - tags
Definition: data.h:31
union cbor_item_metadata metadata
Discriminated by type.
Definition: data.h:151
cbor_item_t * cbor_new_tag(uint64_t value)
Create a new tag.
Definition: tags.c:10
cbor_item_t * cbor_tag_item(const cbor_item_t *item)
Get the tagged item.
Definition: tags.c:22
cbor_item_t * cbor_build_tag(uint64_t value, cbor_item_t *item)
Build a new tag.
Definition: tags.c:41
#define _CBOR_MALLOC
Definition: common.h:84
size_t refcount
Reference count - initialize to 0.
Definition: data.h:153
cbor_item_t * cbor_incref(cbor_item_t *item)
Increases the reference count by one.
Definition: common.c:93
struct _cbor_tag_metadata tag_metadata
Definition: data.h:144
uint64_t cbor_tag_value(const cbor_item_t *item)
Get tag value.
Definition: tags.c:28
struct cbor_item_t * tagged_item
Definition: data.h:115
bool cbor_isa_tag(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:48
uint64_t value
Definition: data.h:116
void cbor_tag_set_item(cbor_item_t *item, cbor_item_t *tagged_item)
Set the tagged item.
Definition: tags.c:34
The item handle.
Definition: data.h:149