libbtc
bitcoinclibrary
Functions
cstr.c File Reference
#include "btc/cstr.h"
#include <string.h>

Go to the source code of this file.

Functions

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

Function Documentation

static btc_bool cstr_alloc_min_sz ( cstring s,
size_t  sz 
)
static

Definition at line 10 of file cstr.c.

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

Referenced by cstr_append_buf(), cstr_new_sz(), and cstr_resize().

11 {
12  sz++; // NUL overhead
13 
14  if (s->alloc && (s->alloc >= sz))
15  return true;
16 
17  unsigned int shift = 3;
18  unsigned int al_sz;
19  while ((al_sz = (1 << shift)) < sz)
20  shift++;
21 
22  char* new_s = realloc(s->str, al_sz);
23  if (!new_s)
24  return false;
25 
26  s->str = new_s;
27  s->alloc = al_sz;
28  s->str[s->len] = 0;
29 
30  return true;
31 }
char * str
Definition: cstr.h:43
size_t alloc
Definition: cstr.h:45
size_t len
Definition: cstr.h:44
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
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
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:16
size_t len
Definition: cstr.h:44
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
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
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
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
btc_bool cstr_resize ( cstring s,
size_t  new_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:16
static btc_bool cstr_alloc_min_sz(cstring *s, size_t sz)
Definition: cstr.c:10
size_t len
Definition: cstr.h:44