libbtc
bitcoinclibrary
Data Structures | Typedefs | Functions
cstr.h File Reference
#include "btc.h"
#include <stdlib.h>
#include <sys/types.h>

Go to the source code of this file.

Data Structures

struct  cstring
 

Typedefs

typedef struct cstring cstring
 

Functions

LIBBTC_API cstringcstr_new (const char *init_str)
 
LIBBTC_API cstringcstr_new_sz (size_t sz)
 
LIBBTC_API cstringcstr_new_buf (const void *buf, size_t sz)
 
LIBBTC_API void cstr_free (cstring *s, btc_bool free_buf)
 
LIBBTC_API btc_bool cstr_equal (const cstring *a, const cstring *b)
 
LIBBTC_API btc_bool cstr_resize (cstring *s, size_t sz)
 
LIBBTC_API btc_bool cstr_erase (cstring *s, size_t pos, ssize_t len)
 
LIBBTC_API btc_bool cstr_append_buf (cstring *s, const void *buf, size_t sz)
 
static LIBBTC_API btc_bool cstr_append_c (cstring *s, char ch)
 

Typedef Documentation

typedef struct cstring cstring

Function Documentation

LIBBTC_API btc_bool cstr_append_buf ( cstring s,
const void *  buf,
size_t  sz 
)

Definition at line 106 of file cstr.c.

References cstr_alloc_min_sz(), cstring::len, and cstring::str.

Referenced by btc_script_append_op(), btc_script_append_pushdata(), btc_script_build_multisig(), btc_script_copy_without_op_codeseperator(), btc_tx_in_copy(), btc_tx_out_copy(), btc_tx_sighash(), cstr_append_c(), deser_varstr(), ser_bytes(), ser_u16(), ser_u32(), and ser_u64().

107 {
108  if (!cstr_alloc_min_sz(s, s->len + sz))
109  return false;
110 
111  memcpy(s->str + s->len, buf, sz);
112  s->len += sz;
113  s->str[s->len] = 0;
114 
115  return true;
116 }
char * str
Definition: cstr.h:43
static btc_bool cstr_alloc_min_sz(cstring *s, size_t sz)
Definition: cstr.c:10
size_t len
Definition: cstr.h:44
static LIBBTC_API btc_bool cstr_append_c ( cstring s,
char  ch 
)
inlinestatic

Definition at line 59 of file cstr.h.

References cstr_append_buf().

60 {
61  return cstr_append_buf(s, &ch, 1);
62 }
LIBBTC_API btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
LIBBTC_API btc_bool cstr_equal ( const cstring a,
const cstring b 
)

Definition at line 118 of file cstr.c.

References cstring::len, and cstring::str.

119 {
120  if (a == b)
121  return true;
122  if (!a || !b)
123  return false;
124  if (a->len != b->len)
125  return false;
126  return (memcmp(a->str, b->str, a->len) == 0);
127 }
char * str
Definition: cstr.h:43
size_t len
Definition: cstr.h:44
LIBBTC_API btc_bool cstr_erase ( cstring s,
size_t  pos,
ssize_t  len 
)

Definition at line 129 of file cstr.c.

References buffer::len, cstring::len, and cstring::str.

130 {
131  if (pos == s->len && len == 0)
132  return true;
133  if (pos >= s->len)
134  return false;
135 
136  ssize_t old_tail = s->len - pos;
137  if ((len >= 0) && (len > old_tail))
138  return false;
139 
140  memmove(&s->str[pos], &s->str[pos + len], old_tail - len);
141  s->len -= len;
142  s->str[s->len] = 0;
143 
144  return true;
145 }
char * str
Definition: cstr.h:43
size_t len
Definition: buffer.h:22
size_t len
Definition: cstr.h:44
LIBBTC_API void cstr_free ( cstring s,
btc_bool  free_buf 
)

Definition at line 69 of file cstr.c.

References cstring::str.

Referenced by btc_tx_hash(), btc_tx_in_free(), btc_tx_out_free(), btc_tx_sighash(), and deser_varstr().

70 {
71  if (!s)
72  return;
73 
74  if (free_buf)
75  free(s->str);
76 
77  memset(s, 0, sizeof(*s));
78  free(s);
79 }
char * str
Definition: cstr.h:43
LIBBTC_API cstring* cstr_new ( const char *  init_str)

Definition at line 60 of file cstr.c.

References cstr_new_buf(), and cstr_new_sz().

61 {
62  if (!init_str || !*init_str)
63  return cstr_new_sz(0);
64 
65  size_t slen = strlen(init_str);
66  return cstr_new_buf(init_str, slen);
67 }
cstring * cstr_new_sz(size_t sz)
Definition: cstr.c:33
cstring * cstr_new_buf(const void *buf, size_t sz)
Definition: cstr.c:47
LIBBTC_API cstring* cstr_new_buf ( const void *  buf,
size_t  sz 
)

Definition at line 47 of file cstr.c.

References cstr_new_sz(), cstring::len, and cstring::str.

Referenced by cstr_new().

48 {
49  cstring* s = cstr_new_sz(sz);
50  if (!s)
51  return NULL;
52 
53  memcpy(s->str, buf, sz);
54  s->len = sz;
55  s->str[s->len] = 0;
56 
57  return s;
58 }
char * str
Definition: cstr.h:43
cstring * cstr_new_sz(size_t sz)
Definition: cstr.c:33
size_t len
Definition: cstr.h:44
Definition: cstr.h:41
LIBBTC_API cstring* cstr_new_sz ( size_t  sz)

Definition at line 33 of file cstr.c.

References cstr_alloc_min_sz().

Referenced by btc_tx_add_p2pkh_hash160_out(), btc_tx_add_p2sh_hash160_out(), btc_tx_hash(), btc_tx_in_copy(), btc_tx_out_copy(), btc_tx_sighash(), cstr_new(), cstr_new_buf(), and deser_varstr().

34 {
35  cstring* s = calloc(1, sizeof(cstring));
36  if (!s)
37  return NULL;
38 
39  if (!cstr_alloc_min_sz(s, sz)) {
40  free(s);
41  return NULL;
42  }
43 
44  return s;
45 }
static btc_bool cstr_alloc_min_sz(cstring *s, size_t sz)
Definition: cstr.c:10
Definition: cstr.h:41
LIBBTC_API btc_bool cstr_resize ( cstring s,
size_t  sz 
)

Definition at line 81 of file cstr.c.

References cstr_alloc_min_sz(), buffer::len, cstring::len, and cstring::str.

Referenced by btc_script_build_multisig(), btc_script_build_p2pkh(), btc_script_build_p2sh(), and btc_tx_sighash().

82 {
83  // no change
84  if (new_sz == s->len)
85  return true;
86 
87  // truncate string
88  if (new_sz <= s->len) {
89  s->len = new_sz;
90  s->str[s->len] = 0;
91  return true;
92  }
93 
94  // increase string size
95  if (!cstr_alloc_min_sz(s, new_sz))
96  return false;
97 
98  // contents of string tail undefined
99 
100  s->len = new_sz;
101  s->str[s->len] = 0;
102 
103  return true;
104 }
char * str
Definition: cstr.h:43
size_t len
Definition: buffer.h:22
static btc_bool cstr_alloc_min_sz(cstring *s, size_t sz)
Definition: cstr.c:10
size_t len
Definition: cstr.h:44