libbtc
bitcoinclibrary
buffer.c
Go to the documentation of this file.
1 /* Copyright 2012 exMULTI, Inc.
2  * Distributed under the MIT/X11 software license, see the accompanying
3  * file COPYING or http://www.opensource.org/licenses/mit-license.php.
4  */
5 
6 #include "buffer.h"
7 
8 #include <stdlib.h>
9 #include <string.h>
10 
11 btc_bool buffer_equal(const void* a_, const void* b_)
12 {
13  const struct buffer* a = a_;
14  const struct buffer* b = b_;
15 
16  if (a->len != b->len)
17  return false;
18  return memcmp(a->p, b->p, a->len) == 0;
19 }
20 
21 void buffer_free(void* struct_buffer)
22 {
23  struct buffer* buf = struct_buffer;
24  if (!buf)
25  return;
26 
27  free(buf->p);
28  free(buf);
29 }
30 
31 struct buffer* buffer_copy(const void* data, size_t data_len)
32 {
33  struct buffer* buf;
34  buf = malloc(sizeof(*buf));
35  if (!buf)
36  goto err_out;
37 
38  buf->p = malloc(data_len);
39  if (!buf->p)
40  goto err_out_free;
41 
42  memcpy(buf->p, data, data_len);
43  buf->len = data_len;
44 
45  return buf;
46 
47 err_out_free:
48  free(buf);
49 err_out:
50  return NULL;
51 }
uint8_t btc_bool
Definition: btc.h:34
struct buffer * buffer_copy(const void *data, size_t data_len)
Definition: buffer.c:31
void * p
Definition: buffer.h:15
size_t len
Definition: buffer.h:16
void buffer_free(void *struct_buffer)
Definition: buffer.c:21
btc_bool buffer_equal(const void *a_, const void *b_)
Definition: buffer.c:11
Definition: buffer.h:13