diff options
Diffstat (limited to 'strbuf.c')
-rw-r--r-- | strbuf.c | 110 |
1 files changed, 29 insertions, 81 deletions
@@ -26,6 +26,7 @@ | |||
26 | #include <stdlib.h> | 26 | #include <stdlib.h> |
27 | #include <stdarg.h> | 27 | #include <stdarg.h> |
28 | #include <string.h> | 28 | #include <string.h> |
29 | #include <stdint.h> | ||
29 | 30 | ||
30 | #include "strbuf.h" | 31 | #include "strbuf.h" |
31 | 32 | ||
@@ -38,22 +39,22 @@ static void die(const char *fmt, ...) | |||
38 | va_end(arg); | 39 | va_end(arg); |
39 | fprintf(stderr, "\n"); | 40 | fprintf(stderr, "\n"); |
40 | 41 | ||
41 | exit(-1); | 42 | abort(); |
42 | } | 43 | } |
43 | 44 | ||
44 | void strbuf_init(strbuf_t *s, int len) | 45 | void strbuf_init(strbuf_t *s, size_t len) |
45 | { | 46 | { |
46 | int size; | 47 | size_t size; |
47 | 48 | ||
48 | if (len <= 0) | 49 | if (!len) |
49 | size = STRBUF_DEFAULT_SIZE; | 50 | size = STRBUF_DEFAULT_SIZE; |
50 | else | 51 | else |
51 | size = len + 1; /* \0 terminator */ | 52 | size = len + 1; |
52 | 53 | if (size < len) | |
54 | die("Overflow, len: %zu", len); | ||
53 | s->buf = NULL; | 55 | s->buf = NULL; |
54 | s->size = size; | 56 | s->size = size; |
55 | s->length = 0; | 57 | s->length = 0; |
56 | s->increment = STRBUF_DEFAULT_INCREMENT; | ||
57 | s->dynamic = 0; | 58 | s->dynamic = 0; |
58 | s->reallocs = 0; | 59 | s->reallocs = 0; |
59 | s->debug = 0; | 60 | s->debug = 0; |
@@ -65,7 +66,7 @@ void strbuf_init(strbuf_t *s, int len) | |||
65 | strbuf_ensure_null(s); | 66 | strbuf_ensure_null(s); |
66 | } | 67 | } |
67 | 68 | ||
68 | strbuf_t *strbuf_new(int len) | 69 | strbuf_t *strbuf_new(size_t len) |
69 | { | 70 | { |
70 | strbuf_t *s; | 71 | strbuf_t *s; |
71 | 72 | ||
@@ -81,20 +82,10 @@ strbuf_t *strbuf_new(int len) | |||
81 | return s; | 82 | return s; |
82 | } | 83 | } |
83 | 84 | ||
84 | void strbuf_set_increment(strbuf_t *s, int increment) | ||
85 | { | ||
86 | /* Increment > 0: Linear buffer growth rate | ||
87 | * Increment < -1: Exponential buffer growth rate */ | ||
88 | if (increment == 0 || increment == -1) | ||
89 | die("BUG: Invalid string increment"); | ||
90 | |||
91 | s->increment = increment; | ||
92 | } | ||
93 | |||
94 | static inline void debug_stats(strbuf_t *s) | 85 | static inline void debug_stats(strbuf_t *s) |
95 | { | 86 | { |
96 | if (s->debug) { | 87 | if (s->debug) { |
97 | fprintf(stderr, "strbuf(%lx) reallocs: %d, length: %d, size: %d\n", | 88 | fprintf(stderr, "strbuf(%lx) reallocs: %d, length: %zd, size: %zd\n", |
98 | (long)s, s->reallocs, s->length, s->size); | 89 | (long)s, s->reallocs, s->length, s->size); |
99 | } | 90 | } |
100 | } | 91 | } |
@@ -113,7 +104,7 @@ void strbuf_free(strbuf_t *s) | |||
113 | free(s); | 104 | free(s); |
114 | } | 105 | } |
115 | 106 | ||
116 | char *strbuf_free_to_string(strbuf_t *s, int *len) | 107 | char *strbuf_free_to_string(strbuf_t *s, size_t *len) |
117 | { | 108 | { |
118 | char *buf; | 109 | char *buf; |
119 | 110 | ||
@@ -131,57 +122,63 @@ char *strbuf_free_to_string(strbuf_t *s, int *len) | |||
131 | return buf; | 122 | return buf; |
132 | } | 123 | } |
133 | 124 | ||
134 | static int calculate_new_size(strbuf_t *s, int len) | 125 | static size_t calculate_new_size(strbuf_t *s, size_t len) |
135 | { | 126 | { |
136 | int reqsize, newsize; | 127 | size_t reqsize, newsize; |
137 | 128 | ||
138 | if (len <= 0) | 129 | if (len <= 0) |
139 | die("BUG: Invalid strbuf length requested"); | 130 | die("BUG: Invalid strbuf length requested"); |
140 | 131 | ||
141 | /* Ensure there is room for optional NULL termination */ | 132 | /* Ensure there is room for optional NULL termination */ |
142 | reqsize = len + 1; | 133 | reqsize = len + 1; |
134 | if (reqsize < len) | ||
135 | die("Overflow, len: %zu", len); | ||
143 | 136 | ||
144 | /* If the user has requested to shrink the buffer, do it exactly */ | 137 | /* If the user has requested to shrink the buffer, do it exactly */ |
145 | if (s->size > reqsize) | 138 | if (s->size > reqsize) |
146 | return reqsize; | 139 | return reqsize; |
147 | 140 | ||
148 | newsize = s->size; | 141 | newsize = s->size; |
149 | if (s->increment < 0) { | 142 | if (reqsize >= SIZE_MAX / 2) { |
143 | newsize = reqsize; | ||
144 | } else { | ||
150 | /* Exponential sizing */ | 145 | /* Exponential sizing */ |
151 | while (newsize < reqsize) | 146 | while (newsize < reqsize) |
152 | newsize *= -s->increment; | 147 | newsize *= 2; |
153 | } else if (s->increment != 0) { | ||
154 | /* Linear sizing */ | ||
155 | newsize = ((newsize + s->increment - 1) / s->increment) * s->increment; | ||
156 | } | 148 | } |
157 | 149 | ||
150 | if (newsize < reqsize) | ||
151 | die("BUG: strbuf length would overflow, len: %zu", len); | ||
152 | |||
153 | |||
158 | return newsize; | 154 | return newsize; |
159 | } | 155 | } |
160 | 156 | ||
161 | 157 | ||
162 | /* Ensure strbuf can handle a string length bytes long (ignoring NULL | 158 | /* Ensure strbuf can handle a string length bytes long (ignoring NULL |
163 | * optional termination). */ | 159 | * optional termination). */ |
164 | void strbuf_resize(strbuf_t *s, int len) | 160 | void strbuf_resize(strbuf_t *s, size_t len) |
165 | { | 161 | { |
166 | int newsize; | 162 | size_t newsize; |
167 | 163 | ||
168 | newsize = calculate_new_size(s, len); | 164 | newsize = calculate_new_size(s, len); |
169 | 165 | ||
170 | if (s->debug > 1) { | 166 | if (s->debug > 1) { |
171 | fprintf(stderr, "strbuf(%lx) resize: %d => %d\n", | 167 | fprintf(stderr, "strbuf(%lx) resize: %zd => %zd\n", |
172 | (long)s, s->size, newsize); | 168 | (long)s, s->size, newsize); |
173 | } | 169 | } |
174 | 170 | ||
175 | s->size = newsize; | 171 | s->size = newsize; |
176 | s->buf = realloc(s->buf, s->size); | 172 | s->buf = realloc(s->buf, s->size); |
177 | if (!s->buf) | 173 | if (!s->buf) |
178 | die("Out of memory"); | 174 | die("Out of memory, len: %zu", len); |
179 | s->reallocs++; | 175 | s->reallocs++; |
180 | } | 176 | } |
181 | 177 | ||
182 | void strbuf_append_string(strbuf_t *s, const char *str) | 178 | void strbuf_append_string(strbuf_t *s, const char *str) |
183 | { | 179 | { |
184 | int space, i; | 180 | int i; |
181 | size_t space; | ||
185 | 182 | ||
186 | space = strbuf_empty_length(s); | 183 | space = strbuf_empty_length(s); |
187 | 184 | ||
@@ -197,55 +194,6 @@ void strbuf_append_string(strbuf_t *s, const char *str) | |||
197 | } | 194 | } |
198 | } | 195 | } |
199 | 196 | ||
200 | /* strbuf_append_fmt() should only be used when an upper bound | ||
201 | * is known for the output string. */ | ||
202 | void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...) | ||
203 | { | ||
204 | va_list arg; | ||
205 | int fmt_len; | ||
206 | |||
207 | strbuf_ensure_empty_length(s, len); | ||
208 | |||
209 | va_start(arg, fmt); | ||
210 | fmt_len = vsnprintf(s->buf + s->length, len, fmt, arg); | ||
211 | va_end(arg); | ||
212 | |||
213 | if (fmt_len < 0) | ||
214 | die("BUG: Unable to convert number"); /* This should never happen.. */ | ||
215 | |||
216 | s->length += fmt_len; | ||
217 | } | ||
218 | |||
219 | /* strbuf_append_fmt_retry() can be used when the there is no known | ||
220 | * upper bound for the output string. */ | ||
221 | void strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...) | ||
222 | { | ||
223 | va_list arg; | ||
224 | int fmt_len, try; | ||
225 | int empty_len; | ||
226 | |||
227 | /* If the first attempt to append fails, resize the buffer appropriately | ||
228 | * and try again */ | ||
229 | for (try = 0; ; try++) { | ||
230 | va_start(arg, fmt); | ||
231 | /* Append the new formatted string */ | ||
232 | /* fmt_len is the length of the string required, excluding the | ||
233 | * trailing NULL */ | ||
234 | empty_len = strbuf_empty_length(s); | ||
235 | /* Add 1 since there is also space to store the terminating NULL. */ | ||
236 | fmt_len = vsnprintf(s->buf + s->length, empty_len + 1, fmt, arg); | ||
237 | va_end(arg); | ||
238 | |||
239 | if (fmt_len <= empty_len) | ||
240 | break; /* SUCCESS */ | ||
241 | if (try > 0) | ||
242 | die("BUG: length of formatted string changed"); | ||
243 | |||
244 | strbuf_resize(s, s->length + fmt_len); | ||
245 | } | ||
246 | |||
247 | s->length += fmt_len; | ||
248 | } | ||
249 | 197 | ||
250 | /* vi:ai et sw=4 ts=4: | 198 | /* vi:ai et sw=4 ts=4: |
251 | */ | 199 | */ |