libcbor  0.5.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015 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 #ifndef LIBCBOR_COMMON_H
9 #define LIBCBOR_COMMON_H
10 
11 #include <stdlib.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include <assert.h>
16 #include "data.h"
17 #include "cbor/configuration.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 
28 #define CBOR_RESTRICT_POINTER
29 
30 #else
31 
32 // MSVC + C++ workaround
33 #define CBOR_RESTRICT_POINTER CBOR_RESTRICT_SPECIFIER
34 
35 #endif
36 
37 static const uint8_t cbor_major_version = CBOR_MAJOR_VERSION;
38 static const uint8_t cbor_minor_version = CBOR_MINOR_VERSION;
39 static const uint8_t cbor_patch_version = CBOR_PATCH_VERSION;
40 
41 #define CBOR_VERSION TO_STR(CBOR_MAJOR_VERSION) "." TO_STR(CBOR_MINOR_VERSION) "." TO_STR(CBOR_PATCH_VERSION)
42 #define CBOR_HEX_VERSION ((CBOR_MAJOR_VERSION << 16) | (CBOR_MINOR_VERSION << 8) | CBOR_PATCH_VERSION)
43 
44 
45 /* http://stackoverflow.com/questions/1644868/c-define-macro-for-debug-printing */
46 #ifdef DEBUG
47 #include <stdio.h>
48 #define debug_print(fmt, ...) do { \
49  if (DEBUG) \
50  fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, __VA_ARGS__); \
51  } while (0)
52 #else
53 #define debug_print(fmt, ...) do {} while (0)
54 #endif
55 
56 #define TO_STR_(x) #x
57 #define TO_STR(x) TO_STR_(x) /* enables proper double expansion */
58 
59 #if CBOR_CUSTOM_ALLOC
60 
61 typedef void * (* _cbor_malloc_t)(size_t);
62 typedef void * (* _cbor_realloc_t)(void *, size_t);
63 typedef void (* _cbor_free_t)(void *);
64 
68 
82 void cbor_set_allocs(_cbor_malloc_t custom_malloc, _cbor_realloc_t custom_realloc, _cbor_free_t custom_free);
83 
84 #define _CBOR_MALLOC _cbor_malloc
85 #define _CBOR_REALLOC _cbor_realloc
86 #define _CBOR_FREE _cbor_free
87 
88 #else
89 
90 #define _CBOR_MALLOC malloc
91 #define _CBOR_REALLOC realloc
92 #define _CBOR_FREE free
93 
94 #endif
95 
96 /*
97 * ============================================================================
98 * Type manipulation
99 * ============================================================================
100 */
101 
107 cbor_type cbor_typeof(const cbor_item_t * item); /* Will be inlined iff link-time opt is enabled */
108 
109 /* Standard item types as described by the RFC */
110 
115 bool cbor_isa_uint(const cbor_item_t * item);
116 
121 bool cbor_isa_negint(const cbor_item_t * item);
122 
127 bool cbor_isa_bytestring(const cbor_item_t * item);
128 
133 bool cbor_isa_string(const cbor_item_t * item);
134 
139 bool cbor_isa_array(const cbor_item_t * item);
140 
145 bool cbor_isa_map(const cbor_item_t * item);
146 
151 bool cbor_isa_tag(const cbor_item_t * item);
152 
157 bool cbor_isa_float_ctrl(const cbor_item_t * item);
158 
159 /* Practical types with respect to their semantics (but not tag values) */
160 
165 bool cbor_is_int(const cbor_item_t * item);
166 
171 bool cbor_is_float(const cbor_item_t * item);
172 
177 bool cbor_is_bool(const cbor_item_t * item);
178 
186 bool cbor_is_null(const cbor_item_t * item);
187 
188 
196 bool cbor_is_undef(const cbor_item_t * item);
197 
198 
199 /*
200 * ============================================================================
201 * Memory management
202 * ============================================================================
203 */
204 
213 
221 void cbor_decref(cbor_item_t ** item);
222 
230 
240 size_t cbor_refcount(const cbor_item_t * item);
241 
256 
257 #ifdef __cplusplus
258 }
259 #endif
260 
261 #endif //LIBCBOR_COMMON_H
void(* _cbor_free_t)(void *)
Definition: common.h:63
void cbor_decref(cbor_item_t **item)
Decreases the reference count by one, deallocating the item if needed.
Definition: common.c:99
void cbor_intermediate_decref(cbor_item_t *item)
Decreases the reference count by one, deallocating the item if needed.
Definition: common.c:174
bool cbor_isa_string(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:33
cbor_item_t * cbor_move(cbor_item_t *item)
Provides CPP-like move construct.
Definition: common.c:184
bool cbor_is_undef(const cbor_item_t *item)
Does this item represent undefined
Definition: common.c:82
bool cbor_isa_negint(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:23
void *(* _cbor_realloc_t)(void *, size_t)
Definition: common.h:62
cbor_item_t * cbor_incref(cbor_item_t *item)
Increases the reference count by one.
Definition: common.c:93
_cbor_free_t _cbor_free
Definition: allocators.c:12
bool cbor_isa_tag(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:48
bool cbor_isa_float_ctrl(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:53
bool cbor_isa_array(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:38
bool cbor_isa_uint(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:18
bool cbor_isa_map(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:43
void cbor_set_allocs(_cbor_malloc_t custom_malloc, _cbor_realloc_t custom_realloc, _cbor_free_t custom_free)
Sets the memory management routines to use.
Definition: allocators.c:14
void *(* _cbor_malloc_t)(size_t)
Definition: common.h:61
cbor_type
Specifies the Major type of cbor_item_t.
Definition: data.h:24
bool cbor_is_bool(const cbor_item_t *item)
Is the item an a boolean?
Definition: common.c:71
bool cbor_is_float(const cbor_item_t *item)
Is the item an a floating point number?
Definition: common.c:87
_cbor_malloc_t _cbor_malloc
Definition: allocators.c:10
bool cbor_is_int(const cbor_item_t *item)
Is the item an integer, either positive or negative?
Definition: common.c:65
bool cbor_isa_bytestring(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:28
cbor_type cbor_typeof(const cbor_item_t *item)
Get the type of the item.
Definition: common.c:59
bool cbor_is_null(const cbor_item_t *item)
Does this item represent null
Definition: common.c:77
The item handle.
Definition: data.h:149
_cbor_realloc_t _cbor_realloc
Definition: allocators.c:11
size_t cbor_refcount(const cbor_item_t *item)
Get the reference count.
Definition: common.c:179