forked from dogecoinfoundation/libdogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.c
More file actions
83 lines (70 loc) · 1.7 KB
/
Copy pathbuffer.c
File metadata and controls
83 lines (70 loc) · 1.7 KB
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
/* Copyright 2012 exMULTI, Inc.
* Copyright 2022 bluezr
* Copyright 2022 The Dogecoin Foundation
* Distributed under the MIT/X11 software license, see the accompanying
* file COPYING or http://www.opensource.org/licenses/mit-license.php.
*/
#include <dogecoin/buffer.h>
#include <dogecoin/mem.h>
/**
* @brief This function compares two buffers to see if
* they are equal.
*
* @param a_ The reference buffer.
* @param b_ The buffer to compare.
*
* @return 1 if buffers are equal, 0 otherwise.
*/
int buffer_equal(const void* a_, const void* b_)
{
const struct buffer* a = a_;
const struct buffer* b = b_;
if (a->len != b->len)
return 0;
return memcmp(a->p, b->p, a->len) == 0;
}
/**
* @brief This function frees the memory allocated for
* the specified buffer.
*
* @param struct_buffer The pointer to the buffer to be freed.
*
* @return Nothing.
*/
void buffer_free(void* struct_buffer)
{
struct buffer* buf = struct_buffer;
if (!buf) {
return;
}
dogecoin_free(buf->p);
dogecoin_free(buf);
}
/**
* @brief This function creates a new empty buffer and
* copies data from the old buffer.
*
* @param data The data to be copied.
* @param data_len The length of the data to be copied.
*
* @return The pointer to the new buffer.
*/
struct buffer* buffer_copy(const void* data, size_t data_len)
{
struct buffer* buf;
buf = dogecoin_calloc(1, sizeof(*buf));
if (!buf) {
goto err_out;
}
buf->p = dogecoin_calloc(1, data_len);
if (!buf->p) {
goto err_out_free;
}
memcpy_safe(buf->p, data, data_len);
buf->len = data_len;
return buf;
err_out_free:
dogecoin_free(buf);
err_out:
return NULL;
}