libbtc
bitcoinclibrary
Functions
buffer.c File Reference
#include "buffer.h"
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Functions

btc_bool buffer_equal (const void *a_, const void *b_)
 
void buffer_free (void *struct_buffer)
 
struct bufferbuffer_copy (const void *data, size_t data_len)
 

Function Documentation

struct buffer* buffer_copy ( const void *  data,
size_t  data_len 
)

Definition at line 31 of file buffer.c.

References buffer::len, and buffer::p.

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 }
void * p
Definition: buffer.h:15
size_t len
Definition: buffer.h:16
Definition: buffer.h:13
btc_bool buffer_equal ( const void *  a_,
const void *  b_ 
)

Definition at line 11 of file buffer.c.

References buffer::len, and buffer::p.

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 }
void * p
Definition: buffer.h:15
size_t len
Definition: buffer.h:16
Definition: buffer.h:13
void buffer_free ( void *  struct_buffer)

Definition at line 21 of file buffer.c.

References buffer::p.

22 {
23  struct buffer* buf = struct_buffer;
24  if (!buf)
25  return;
26 
27  free(buf->p);
28  free(buf);
29 }
void * p
Definition: buffer.h:15
Definition: buffer.h:13