forked from dogecoinfoundation/libdogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcstr.c
370 lines (311 loc) · 7.94 KB
/
cstr.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* Copyright 2015 BitPay, 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/cstr.h>
#include <dogecoin/mem.h>
/**
* @brief This function takes a cstring and allocates a new
* buffer of the specified size. If the buffer is already
* allocated and is large enough, no change occurs.
*
* @param s The cstring whose buffer is to be reallocated.
* @param sz The new desired size of the buffer.
*
* @return 1 if the buffer is allocated successfully, 0 otherwise.
*/
static int cstr_alloc_min_sz(cstring* s, size_t sz)
{
unsigned int shift;
unsigned int al_sz;
char* new_s;
sz++; /* NULL overhead */
if (s->alloc && (s->alloc >= sz)) {
return 1;
}
shift = 3;
while ((al_sz = (1 << shift)) < sz) {
shift++;
}
new_s = dogecoin_realloc(s->str, al_sz);
if (!new_s) {
return 0;
}
s->str = new_s;
s->alloc = al_sz;
s->str[s->len] = 0;
return 1;
}
/**
* @brief This function takes a cstring and allocates a new
* buffer of the specified size. If the new size is greater than
* the current, it allocates a new buffer of this size and copies
* the buffer's contents to the new location. If the new size is
* smaller, the string will be truncated.
*
* @param s The pointer to the cstring whose buffer is to be resized.
* @param sz The new desired size of the buffer.
*
* @return 1 if the buffer was successfully resized, 0 otherwise.
*/
int cstr_alloc_minsize(cstring* s, size_t new_sz)
{
/* no change */
if (new_sz == s->len) {
return 1;
}
/* truncate string */
if (new_sz <= s->len) {
return 0;
}
/* increase string size */
if (!cstr_alloc_min_sz(s, new_sz)) {
return 0;
}
/* contents of string tail undefined */
// s->len = new_sz;
s->str[s->len] = 0;
return 1;
}
/**
* @brief This function allocates a new cstring of the
* specified size.
*
* @param sz The size of the string to allocate.
*
* @return a pointer to the new cstring object.
*/
cstring* cstr_new_sz(size_t sz)
{
cstring* s = dogecoin_calloc(1, sizeof(cstring));
if (!s) {
return NULL;
}
if (!cstr_alloc_min_sz(s, sz)) {
dogecoin_free(s);
return NULL;
}
return s;
}
/**
* @brief The function creates a new cstring and
* initializes it with the first sz bytes of the
* specified buffer.
*
* @param buf The data to be copied into the cstring.
* @param sz The size of the string to allocate and initialize.
*
* @return A pointer to the new cstring object.
*/
cstring* cstr_new_buf(const void* buf, size_t sz)
{
cstring* s = cstr_new_sz(sz);
if (!s) {
return NULL;
}
memcpy_safe(s->str, buf, sz);
s->len = sz;
s->str[s->len] = 0;
return s;
}
/**
* @brief This function creates a new cstring from
* an existing cstring.
*
* @param copy_str The cstring object to be copied.
*
* @return A pointer to the new cstring object.
*/
cstring* cstr_new_cstr(const cstring* copy_str)
{
return cstr_new_buf(copy_str->str, copy_str->len);
}
cstring* cstr_new(const char* init_str)
{
size_t slen;
if (!init_str || !*init_str) {
return cstr_new_sz(0);
}
slen = strlen(init_str);
return cstr_new_buf(init_str, slen);
}
/**
* @brief This function frees the memory allocated for
* the cstring object.
*
* @param s The pointer to the cstring to be freed.
* @param free_buf Whether the buffer inside the cstring should be freed.
*
* @return Nothing.
*/
void cstr_free(cstring* s, int free_buf)
{
if (!s) {
return;
}
if (free_buf) {
dogecoin_free(s->str);
}
dogecoin_mem_zero(s, sizeof(*s));
dogecoin_free(s);
}
/**
* @brief This function resizes the buffer inside cstring object.
*
* @param s The pointer to the cstring whose buffer is to be resized.
* @param new_sz The new desired size of the buffer.
*
* @return 1 if buffer was resized successfully, 0 otherwise.
*/
int cstr_resize(cstring* s, size_t new_sz)
{
/* no change */
if (new_sz == s->len) {
return 1;
}
/* truncate string */
if (new_sz <= s->len) {
s->len = new_sz;
s->str[s->len] = 0;
return 1;
}
/* increase string size */
if (!cstr_alloc_min_sz(s, new_sz)) {
return 0;
}
/* contents of string tail undefined */
s->len = new_sz;
s->str[s->len] = 0;
return 1;
}
/**
* @brief This function appends the contents of a buffer to the
* buffer of the specified cstring, resizing the destination
* buffer as necessary.
*
* @param s The pointer to the cstring to append to.
* @param buf The buffer to be appended.
* @param sz The size of the buffer to be appended.
*
* @return 1 if the data was appended successfully, 0 otherwise.
*/
int cstr_append_buf(cstring* s, const void* buf, size_t sz)
{
if (!cstr_alloc_min_sz(s, s->len + sz)) {
return 0;
}
memcpy_safe(s->str + s->len, buf, sz);
s->len += sz;
s->str[s->len] = 0;
return 1;
}
/**
* @brief This function appends the contents of one cstring
* to another, resizing the destination buffer as necessary.
*
* @param s The pointer to the cstring to append to.
* @param append The pointer to the cstring whose buffer will be appended.
*
* @return 1 if the cstring was appended successfully, 0 otherwise.
*/
int cstr_append_cstr(cstring* s, cstring* append)
{
return cstr_append_buf(s, append->str, append->len);
}
/**
* @brief This function appends a single character to the
* buffer of a cstring.
*
* @param s The pointer to the cstring to append to.
* @param ch The character to be appended.
*
* @return 1 if the character was appended successfully, 0 otherwise.
*/
int cstr_append_c(cstring* s, char ch)
{
return cstr_append_buf(s, &ch, 1);
}
/**
* @brief This function compares two cstrings and checks
* if their contents are equal.
*
* @param a The pointer to the reference cstring.
* @param b The pointer to the cstring to compare.
*
* @return 1 if the contents are equal, 0 otherwise.
*/
int cstr_equal(const cstring* a, const cstring* b)
{
if (a == b) {
return 1;
}
if (!a || !b) {
return 0;
}
if (a->len != b->len) {
return 0;
}
return (memcmp(a->str, b->str, a->len) == 0);
}
/**
* @brief This function compares the buffers of two cstrings
* and returns which one is greater.
*
* @param a The pointer to the cstring to compare.
* @param b The pointer to the reference cstring.
*
* @return 1 if the string value of a is greater, -1 if the
* string value of b is greater, 0 if the strings are equal.
*/
int cstr_compare(const cstring* a, const cstring* b)
{
unsigned int i;
if (a->len > b->len) {
return (1);
}
if (a->len < b->len) {
return (-1);
}
/* length equal, byte per byte compare */
for (i = 0; i < a->len; i++) {
char a1 = a->str[i];
char b1 = b->str[i];
if (a1 > b1) {
return (1);
}
if (a1 < b1) {
return (-1);
}
}
return (0);
}
/**
* @brief This function erases the characters in the string starting
* at position pos and ending at pos + len.
*
* @param s The pointer to the cstring to modify.
* @param pos The index of the buffer to start erasing from.
* @param len The number of characters to remove.
*
* @return The number of characters successfully erased.
*/
int cstr_erase(cstring* s, size_t pos, ssize_t len)
{
ssize_t old_tail;
if (pos == s->len && len == 0) {
return 1;
}
if (pos >= s->len) {
return 0;
}
old_tail = s->len - pos;
if ((len >= 0) && (len > old_tail)) {
return 0;
}
memmove(&s->str[pos], &s->str[pos + len], old_tail - len);
s->len -= len;
s->str[s->len] = 0;
return 1;
}