1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
/* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file
*
* \addtogroup binary
*
* \brief Universal binary data container.
*
* The module provides universal binary data container extensively used by
* a lot of functions provided by the library.
*
* Example of use:
* ~~~~~ {.c}
*
* dnssec_binary_t data = { 0 };
*
* int result = dnssec_binary_alloc(&data, 32);
* if (result != DNSSEC_EOK) {
* return result;
* }
*
* memcpy(&data.data, buffer, data.size);
*
* // ...
*
* dnssec_binary_free(&data);
*
* ~~~~~
*
* @{
*/
#pragma once
#include <stdint.h>
#include <stdlib.h>
/*!
* Universal structure to hold binary data.
*/
typedef struct dnssec_binary {
size_t size; /*!< Size of the binary data. */
uint8_t *data; /*!< Stored data. */
} dnssec_binary_t;
/*!
* Allocate new binary data structure.
*
* \param[out] data Binary to be allocated.
* \param[in] size Requested size of the binary.
*
* \return Error code, DNSSEC_EOK if successful.
*/
int dnssec_binary_alloc(dnssec_binary_t *data, size_t size);
/*!
* Free content of binary structure.
*
* \param binary Binary structure to be freed.
*/
void dnssec_binary_free(dnssec_binary_t *binary);
/*!
* Create a copy of a binary structure.
*
* \param[in] from Source of the copy.
* \param[out] to Target of the copy.
*
* \return Error code, DNSSEC_EOK if successful.
*/
int dnssec_binary_dup(const dnssec_binary_t *from, dnssec_binary_t *to);
/*!
* Resize binary structure to a new size.
*
* Internally uses realloc, which means that this function can be also used
* as a malloc or free.
*
* \param data Binary to be resized.
* \param new_size New size.
*
* \return Error code, DNSSEC_EOK if successful.
*/
int dnssec_binary_resize(dnssec_binary_t *data, size_t new_size);
/*!
* Compare two binary structures (equivalent of memcmp).
*
* \note NULL sorts before data.
*
* \param one First binary.
* \param two Second binary.
*
* \return 0 if one equals two, <0 if one sorts before two, >0 otherwise.
*/
int dnssec_binary_cmp(const dnssec_binary_t *one, const dnssec_binary_t *two);
/*!
* Allocate binary from Base64 encoded string.
*
* \param[in] base64 Base64 encoded data.
* \param[out] binary Decoded binary data.
*
* \return Error code, DNSSEC_EOK if successful.
*/
int dnssec_binary_from_base64(const dnssec_binary_t *base64,
dnssec_binary_t *binary);
/*!
* Create Base64 encoded string from binary data.
*
* \param[in] binary Binary data.
* \param[out] base64 Base64 encode data.
*
* \return Error code, DNSSEC_EOK if successful.
*/
int dnssec_binary_to_base64(const dnssec_binary_t *binary,
dnssec_binary_t *base64);
/*! @} */
|