diff options
Diffstat (limited to '3rd_party/libsrp6a-sha512/cstr.c')
-rw-r--r-- | 3rd_party/libsrp6a-sha512/cstr.c | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/3rd_party/libsrp6a-sha512/cstr.c b/3rd_party/libsrp6a-sha512/cstr.c new file mode 100644 index 0000000..9856f46 --- /dev/null +++ b/3rd_party/libsrp6a-sha512/cstr.c | |||
@@ -0,0 +1,226 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <string.h> | ||
3 | |||
4 | #include "config.h" | ||
5 | #include "cstr.h" | ||
6 | |||
7 | #define EXPFACTOR 2 /* Minimum expansion factor */ | ||
8 | #define MINSIZE 4 /* Absolute minimum - one word */ | ||
9 | |||
10 | static char cstr_empty_string[] = { '\0' }; | ||
11 | static 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 | ||
20 | static void * Cmalloc(int n, void * heap) { return malloc(n); } | ||
21 | static void Cfree(void * p, void * heap) { free(p); } | ||
22 | static cstr_allocator malloc_allocator = { Cmalloc, Cfree, NULL }; | ||
23 | #else | ||
24 | static cstr_allocator malloc_allocator = { malloc, free, NULL }; | ||
25 | #endif | ||
26 | |||
27 | _TYPE( void ) | ||
28 | cstr_set_allocator(cstr_allocator * alloc) | ||
29 | { | ||
30 | default_alloc = alloc; | ||
31 | } | ||
32 | |||
33 | _TYPE( cstr * ) | ||
34 | cstr_new_alloc(cstr_allocator * alloc) | ||
35 | { | ||
36 | cstr * str; | ||
37 | |||
38 | if(alloc == NULL) { | ||
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) { | ||
47 | str->data = cstr_empty_string; | ||
48 | str->length = str->cap = 0; | ||
49 | str->ref = 1; | ||
50 | str->allocator = alloc; | ||
51 | } | ||
52 | return str; | ||
53 | } | ||
54 | |||
55 | _TYPE( cstr * ) | ||
56 | cstr_new() | ||
57 | { | ||
58 | return cstr_new_alloc(NULL); | ||
59 | } | ||
60 | |||
61 | _TYPE( cstr * ) | ||
62 | cstr_dup_alloc(const cstr * str, cstr_allocator * alloc) | ||
63 | { | ||
64 | cstr * nstr = cstr_new_alloc(alloc); | ||
65 | if(nstr) | ||
66 | cstr_setn(nstr, str->data, str->length); | ||
67 | return nstr; | ||
68 | } | ||
69 | |||
70 | _TYPE( cstr * ) | ||
71 | cstr_dup(const cstr * str) | ||
72 | { | ||
73 | return cstr_dup_alloc(str, NULL); | ||
74 | } | ||
75 | |||
76 | _TYPE( cstr * ) | ||
77 | cstr_create(const char * s) | ||
78 | { | ||
79 | return cstr_createn(s, strlen(s)); | ||
80 | } | ||
81 | |||
82 | _TYPE( cstr * ) | ||
83 | cstr_createn(const char * s, int len) | ||
84 | { | ||
85 | cstr * str = cstr_new(); | ||
86 | if(str) { | ||
87 | cstr_setn(str, s, len); | ||
88 | } | ||
89 | return str; | ||
90 | } | ||
91 | |||
92 | _TYPE( void ) | ||
93 | cstr_use(cstr * str) | ||
94 | { | ||
95 | ++str->ref; | ||
96 | } | ||
97 | |||
98 | _TYPE( void ) | ||
99 | cstr_clear_free(cstr * str) | ||
100 | { | ||
101 | if(--str->ref == 0) { | ||
102 | if(str->cap > 0) { | ||
103 | memset(str->data, 0, str->cap); | ||
104 | (*str->allocator->free)(str->data, str->allocator->heap); | ||
105 | } | ||
106 | (*str->allocator->free)(str, str->allocator->heap); | ||
107 | } | ||
108 | } | ||
109 | |||
110 | _TYPE( void ) | ||
111 | cstr_free(cstr * str) | ||
112 | { | ||
113 | if(--str->ref == 0) { | ||
114 | if(str->cap > 0) | ||
115 | (*str->allocator->free)(str->data, str->allocator->heap); | ||
116 | (*str->allocator->free)(str, str->allocator->heap); | ||
117 | } | ||
118 | } | ||
119 | |||
120 | _TYPE( void ) | ||
121 | cstr_empty(cstr * str) | ||
122 | { | ||
123 | if(str->cap > 0) | ||
124 | (*str->allocator->free)(str->data, str->allocator->heap); | ||
125 | str->data = cstr_empty_string; | ||
126 | str->length = str->cap = 0; | ||
127 | } | ||
128 | |||
129 | static int | ||
130 | cstr_alloc(cstr * str, int len) | ||
131 | { | ||
132 | char * t; | ||
133 | |||
134 | if(len > str->cap) { | ||
135 | if(len < EXPFACTOR * str->cap) | ||
136 | len = EXPFACTOR * str->cap; | ||
137 | if(len < MINSIZE) | ||
138 | len = MINSIZE; | ||
139 | |||
140 | t = (char *) (*str->allocator->alloc)(len * sizeof(char), | ||
141 | str->allocator->heap); | ||
142 | if(t) { | ||
143 | if(str->data) { | ||
144 | t[str->length] = 0; | ||
145 | if(str->cap > 0) { | ||
146 | if(str->length > 0) | ||
147 | memcpy(t, str->data, str->length); | ||
148 | free(str->data); | ||
149 | } | ||
150 | } | ||
151 | str->data = t; | ||
152 | str->cap = len; | ||
153 | return 1; | ||
154 | } | ||
155 | else | ||
156 | return -1; | ||
157 | } | ||
158 | else | ||
159 | return 0; | ||
160 | } | ||
161 | |||
162 | _TYPE( int ) | ||
163 | cstr_copy(cstr * dst, const cstr * src) | ||
164 | { | ||
165 | return cstr_setn(dst, src->data, src->length); | ||
166 | } | ||
167 | |||
168 | _TYPE( int ) | ||
169 | cstr_set(cstr * str, const char * s) | ||
170 | { | ||
171 | return cstr_setn(str, s, strlen(s)); | ||
172 | } | ||
173 | |||
174 | _TYPE( int ) | ||
175 | cstr_setn(cstr * str, const char * s, int len) | ||
176 | { | ||
177 | if(cstr_alloc(str, len + 1) < 0) | ||
178 | return -1; | ||
179 | str->data[len] = 0; | ||
180 | if(s != NULL && len > 0) | ||
181 | memmove(str->data, s, len); | ||
182 | str->length = len; | ||
183 | return 1; | ||
184 | } | ||
185 | |||
186 | _TYPE( int ) | ||
187 | cstr_set_length(cstr * str, int len) | ||
188 | { | ||
189 | if(len < str->length) { | ||
190 | str->data[len] = 0; | ||
191 | str->length = len; | ||
192 | return 1; | ||
193 | } | ||
194 | else if(len > str->length) { | ||
195 | if(cstr_alloc(str, len + 1) < 0) | ||
196 | return -1; | ||
197 | memset(str->data + str->length, 0, len - str->length + 1); | ||
198 | str->length = len; | ||
199 | return 1; | ||
200 | } | ||
201 | else | ||
202 | return 0; | ||
203 | } | ||
204 | |||
205 | _TYPE( int ) | ||
206 | cstr_append(cstr * str, const char * s) | ||
207 | { | ||
208 | return cstr_appendn(str, s, strlen(s)); | ||
209 | } | ||
210 | |||
211 | _TYPE( int ) | ||
212 | cstr_appendn(cstr * str, const char * s, int len) | ||
213 | { | ||
214 | if(cstr_alloc(str, str->length + len + 1) < 0) | ||
215 | return -1; | ||
216 | memcpy(str->data + str->length, s, len); | ||
217 | str->length += len; | ||
218 | str->data[str->length] = 0; | ||
219 | return 1; | ||
220 | } | ||
221 | |||
222 | _TYPE( int ) | ||
223 | cstr_append_str(cstr * dst, const cstr * src) | ||
224 | { | ||
225 | return cstr_appendn(dst, src->data, src->length); | ||
226 | } | ||