summaryrefslogtreecommitdiffstats
path: root/3rd_party/libsrp6a-sha512/cstr.c
diff options
context:
space:
mode:
Diffstat (limited to '3rd_party/libsrp6a-sha512/cstr.c')
-rw-r--r--3rd_party/libsrp6a-sha512/cstr.c62
1 files changed, 10 insertions, 52 deletions
diff --git a/3rd_party/libsrp6a-sha512/cstr.c b/3rd_party/libsrp6a-sha512/cstr.c
index 9856f46..58a5638 100644
--- a/3rd_party/libsrp6a-sha512/cstr.c
+++ b/3rd_party/libsrp6a-sha512/cstr.c
@@ -8,72 +8,31 @@
8#define MINSIZE 4 /* Absolute minimum - one word */ 8#define MINSIZE 4 /* Absolute minimum - one word */
9 9
10static char cstr_empty_string[] = { '\0' }; 10static char cstr_empty_string[] = { '\0' };
11static cstr_allocator * default_alloc = NULL;
12
13/*
14 * It is assumed, for efficiency, that it is okay to pass more arguments
15 * to a function than are called for, as long as the required arguments
16 * are in proper form. If extra arguments to malloc() and free() cause
17 * problems, define PEDANTIC_ARGS below.
18 */
19#ifdef PEDANTIC_ARGS
20static void * Cmalloc(int n, void * heap) { return malloc(n); }
21static void Cfree(void * p, void * heap) { free(p); }
22static cstr_allocator malloc_allocator = { Cmalloc, Cfree, NULL };
23#else
24static cstr_allocator malloc_allocator = { malloc, free, NULL };
25#endif
26
27_TYPE( void )
28cstr_set_allocator(cstr_allocator * alloc)
29{
30 default_alloc = alloc;
31}
32 11
33_TYPE( cstr * ) 12_TYPE( cstr * )
34cstr_new_alloc(cstr_allocator * alloc) 13cstr_new()
35{ 14{
36 cstr * str; 15 cstr * str;
37 16
38 if(alloc == NULL) { 17 str = (cstr *) malloc(sizeof(cstr));
39 if(default_alloc == NULL) {
40 default_alloc = &malloc_allocator;
41 }
42 alloc = default_alloc;
43 }
44
45 str = (cstr *) (*alloc->alloc)(sizeof(cstr), alloc->heap);
46 if(str) { 18 if(str) {
47 str->data = cstr_empty_string; 19 str->data = cstr_empty_string;
48 str->length = str->cap = 0; 20 str->length = str->cap = 0;
49 str->ref = 1; 21 str->ref = 1;
50 str->allocator = alloc;
51 } 22 }
52 return str; 23 return str;
53} 24}
54 25
55_TYPE( cstr * ) 26_TYPE( cstr * )
56cstr_new() 27cstr_dup(const cstr * str)
57{
58 return cstr_new_alloc(NULL);
59}
60
61_TYPE( cstr * )
62cstr_dup_alloc(const cstr * str, cstr_allocator * alloc)
63{ 28{
64 cstr * nstr = cstr_new_alloc(alloc); 29 cstr * nstr = cstr_new();
65 if(nstr) 30 if(nstr)
66 cstr_setn(nstr, str->data, str->length); 31 cstr_setn(nstr, str->data, str->length);
67 return nstr; 32 return nstr;
68} 33}
69 34
70_TYPE( cstr * ) 35_TYPE( cstr * )
71cstr_dup(const cstr * str)
72{
73 return cstr_dup_alloc(str, NULL);
74}
75
76_TYPE( cstr * )
77cstr_create(const char * s) 36cstr_create(const char * s)
78{ 37{
79 return cstr_createn(s, strlen(s)); 38 return cstr_createn(s, strlen(s));
@@ -101,9 +60,9 @@ cstr_clear_free(cstr * str)
101 if(--str->ref == 0) { 60 if(--str->ref == 0) {
102 if(str->cap > 0) { 61 if(str->cap > 0) {
103 memset(str->data, 0, str->cap); 62 memset(str->data, 0, str->cap);
104 (*str->allocator->free)(str->data, str->allocator->heap); 63 free(str->data);
105 } 64 }
106 (*str->allocator->free)(str, str->allocator->heap); 65 free(str);
107 } 66 }
108} 67}
109 68
@@ -112,8 +71,8 @@ cstr_free(cstr * str)
112{ 71{
113 if(--str->ref == 0) { 72 if(--str->ref == 0) {
114 if(str->cap > 0) 73 if(str->cap > 0)
115 (*str->allocator->free)(str->data, str->allocator->heap); 74 free(str->data);
116 (*str->allocator->free)(str, str->allocator->heap); 75 free(str);
117 } 76 }
118} 77}
119 78
@@ -121,7 +80,7 @@ _TYPE( void )
121cstr_empty(cstr * str) 80cstr_empty(cstr * str)
122{ 81{
123 if(str->cap > 0) 82 if(str->cap > 0)
124 (*str->allocator->free)(str->data, str->allocator->heap); 83 free(str->data);
125 str->data = cstr_empty_string; 84 str->data = cstr_empty_string;
126 str->length = str->cap = 0; 85 str->length = str->cap = 0;
127} 86}
@@ -137,8 +96,7 @@ cstr_alloc(cstr * str, int len)
137 if(len < MINSIZE) 96 if(len < MINSIZE)
138 len = MINSIZE; 97 len = MINSIZE;
139 98
140 t = (char *) (*str->allocator->alloc)(len * sizeof(char), 99 t = (char *) malloc(len * sizeof(char));
141 str->allocator->heap);
142 if(t) { 100 if(t) {
143 if(str->data) { 101 if(str->data) {
144 t[str->length] = 0; 102 t[str->length] = 0;