libcbor 0.12.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
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#ifndef LIBCBOR_COMMON_H
9#define LIBCBOR_COMMON_H
10
11#include <assert.h>
12#include <stdbool.h>
13#include <stddef.h>
14#include <stdint.h>
15#include <stdlib.h>
16
17#include "cbor/cbor_export.h"
18#include "cbor/configuration.h"
19#include "data.h"
20
21#ifdef __cplusplus
22extern "C" {
23
31#define CBOR_RESTRICT_POINTER
32
33#else
34
35// MSVC + C++ workaround
36#define CBOR_RESTRICT_POINTER CBOR_RESTRICT_SPECIFIER
37
38#endif
39
40static const uint8_t cbor_major_version = CBOR_MAJOR_VERSION;
41static const uint8_t cbor_minor_version = CBOR_MINOR_VERSION;
42static const uint8_t cbor_patch_version = CBOR_PATCH_VERSION;
43
44#define CBOR_VERSION \
45 _CBOR_TO_STR(CBOR_MAJOR_VERSION) \
46 "." _CBOR_TO_STR(CBOR_MINOR_VERSION) "." _CBOR_TO_STR(CBOR_PATCH_VERSION)
47#define CBOR_HEX_VERSION \
48 ((CBOR_MAJOR_VERSION << 16) | (CBOR_MINOR_VERSION << 8) | CBOR_PATCH_VERSION)
49
50/* http://stackoverflow.com/questions/1644868/c-define-macro-for-debug-printing
51 */
52#ifdef DEBUG
53#include <stdio.h>
54#define _cbor_debug_print(fmt, ...) \
55 do { \
56 if (DEBUG) \
57 fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, \
58 __VA_ARGS__); \
59 } while (0)
60extern bool _cbor_enable_assert;
61// Like `assert`, but can be dynamically disabled in tests to allow testing
62// invalid behaviors.
63#define CBOR_ASSERT(e) assert(!_cbor_enable_assert || (e))
64#define _CBOR_TEST_DISABLE_ASSERT(block) \
65 do { \
66 _cbor_enable_assert = false; \
67 block _cbor_enable_assert = true; \
68 } while (0)
69#else
70#define debug_print(fmt, ...) \
71 do { \
72 } while (0)
73#define CBOR_ASSERT(e)
74#define _CBOR_TEST_DISABLE_ASSERT(block) \
75 do { \
76 block \
77 } while (0)
78#endif
79
80#define _CBOR_TO_STR_(x) #x
81#define _CBOR_TO_STR(x) _CBOR_TO_STR_(x) /* enables proper double expansion */
82
83#ifdef __GNUC__
84#define _CBOR_UNUSED __attribute__((__unused__))
85// TODO(https://github.com/PJK/libcbor/issues/247): Prefer [[nodiscard]] if
86// available
87#define _CBOR_NODISCARD __attribute__((warn_unused_result))
88#elif defined(_MSC_VER)
89#define _CBOR_UNUSED __pragma(warning(suppress : 4100 4101))
90#define _CBOR_NODISCARD
91#else
92#define _CBOR_UNUSED
93#define _CBOR_NODISCARD
94#endif
95
96#ifdef CBOR_HAS_BUILTIN_UNREACHABLE
97#define _CBOR_UNREACHABLE __builtin_unreachable()
98#else
99#define _CBOR_UNREACHABLE
100#endif
101
102typedef void* (*_cbor_malloc_t)(size_t);
103typedef void* (*_cbor_realloc_t)(void*, size_t);
104typedef void (*_cbor_free_t)(void*);
105
106CBOR_EXPORT extern _cbor_malloc_t _cbor_malloc;
107CBOR_EXPORT extern _cbor_realloc_t _cbor_realloc;
108CBOR_EXPORT extern _cbor_free_t _cbor_free;
109
110// Macro to short-circuit builder functions when memory allocation fails
111#define _CBOR_NOTNULL(cbor_item) \
112 do { \
113 if (cbor_item == NULL) { \
114 return NULL; \
115 } \
116 } while (0)
117
118// Macro to short-circuit builders when memory allocation of nested data fails
119#define _CBOR_DEPENDENT_NOTNULL(cbor_item, pointer) \
120 do { \
121 if (pointer == NULL) { \
122 _cbor_free(cbor_item); \
123 return NULL; \
124 } \
125 } while (0)
126
147CBOR_EXPORT void cbor_set_allocs(_cbor_malloc_t custom_malloc,
148 _cbor_realloc_t custom_realloc,
149 _cbor_free_t custom_free);
150
151/*
152 * ============================================================================
153 * Type manipulation
154 * ============================================================================
155 */
156
163CBOR_EXPORT cbor_type cbor_typeof(
164 const cbor_item_t* item); /* Will be inlined iff link-time opt is enabled */
165
166/* Standard CBOR Major item types */
167
173CBOR_EXPORT bool cbor_isa_uint(const cbor_item_t* item);
174
180CBOR_EXPORT bool cbor_isa_negint(const cbor_item_t* item);
181
187CBOR_EXPORT bool cbor_isa_bytestring(const cbor_item_t* item);
188
194CBOR_EXPORT bool cbor_isa_string(const cbor_item_t* item);
195
201CBOR_EXPORT bool cbor_isa_array(const cbor_item_t* item);
202
208CBOR_EXPORT bool cbor_isa_map(const cbor_item_t* item);
209
215CBOR_EXPORT bool cbor_isa_tag(const cbor_item_t* item);
216
222CBOR_EXPORT bool cbor_isa_float_ctrl(const cbor_item_t* item);
223
224/* Practical types with respect to their semantics (but not tag values) */
225
231CBOR_EXPORT bool cbor_is_int(const cbor_item_t* item);
232
238CBOR_EXPORT bool cbor_is_float(const cbor_item_t* item);
239
245CBOR_EXPORT bool cbor_is_bool(const cbor_item_t* item);
246
258CBOR_EXPORT bool cbor_is_null(const cbor_item_t* item);
259
271CBOR_EXPORT bool cbor_is_undef(const cbor_item_t* item);
272
273/*
274 * ============================================================================
275 * Memory management
276 * ============================================================================
277 */
278
289CBOR_EXPORT cbor_item_t* cbor_incref(cbor_item_t* item);
290
298CBOR_EXPORT void cbor_decref(cbor_item_t** item);
299
307CBOR_EXPORT void cbor_intermediate_decref(cbor_item_t* item);
308
321CBOR_EXPORT size_t cbor_refcount(const cbor_item_t* item);
322
339CBOR_EXPORT cbor_item_t* cbor_move(cbor_item_t* item);
340
341#ifdef __cplusplus
342}
343#endif
344
345#endif // LIBCBOR_COMMON_H
_cbor_malloc_t _cbor_malloc
Definition allocators.c:10
_cbor_realloc_t _cbor_realloc
Definition allocators.c:11
_cbor_free_t _cbor_free
Definition allocators.c:12
bool cbor_isa_string(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition common.c:34
bool cbor_isa_negint(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition common.c:26
bool cbor_isa_tag(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition common.c:46
void(* _cbor_free_t)(void *)
Definition common.h:104
cbor_item_t * cbor_move(cbor_item_t *item)
Provides CPP-like move construct.
Definition common.c:158
void *(* _cbor_malloc_t)(size_t)
Definition common.h:102
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
bool cbor_is_float(const cbor_item_t *item)
Is the item an a floating point number?
Definition common.c:74
bool cbor_is_int(const cbor_item_t *item)
Is the item an integer, either positive or negative?
Definition common.c:56
void cbor_intermediate_decref(cbor_item_t *item)
Decreases the item's reference count by one, deallocating the item if needed.
Definition common.c:154
bool cbor_is_bool(const cbor_item_t *item)
Is the item an a boolean?
Definition common.c:60
void *(* _cbor_realloc_t)(void *, size_t)
Definition common.h:103
bool cbor_isa_uint(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition common.c:22
bool cbor_isa_float_ctrl(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition common.c:50
bool cbor_isa_array(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition common.c:38
cbor_item_t * cbor_incref(cbor_item_t *item)
Increases the item's reference count by one.
Definition common.c:78
void cbor_decref(cbor_item_t **item)
Decreases the item's reference count by one, deallocating the item if needed.
Definition common.c:83
#define _CBOR_NODISCARD
Definition common.h:93
size_t cbor_refcount(const cbor_item_t *item)
Get the item's reference count.
Definition common.c:156
cbor_type cbor_typeof(const cbor_item_t *item)
Get the type of the item.
Definition common.c:54
bool cbor_is_null(const cbor_item_t *item)
Does this item represent null
Definition common.c:66
bool cbor_isa_bytestring(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition common.c:30
bool cbor_isa_map(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition common.c:42
bool cbor_is_undef(const cbor_item_t *item)
Does this item represent undefined
Definition common.c:70
cbor_type
Specifies the Major type of cbor_item_t.
Definition data.h:24
The item handle.
Definition data.h:171