/src/dovecot/src/lib/strfuncs.c
Line | Count | Source |
1 | | /* Copyright (c) Dovecot authors, see top-level COPYING file */ |
2 | | |
3 | | /* @UNSAFE: whole file */ |
4 | | |
5 | | #include "config.h" |
6 | | #ifdef HAVE_MEMRCHR |
7 | | # define _GNU_SOURCE |
8 | | #endif |
9 | | #include "lib.h" |
10 | | #include "str.h" |
11 | | #include "printf-format-fix.h" |
12 | | #include "strfuncs.h" |
13 | | #include "array.h" |
14 | | #include "hash.h" |
15 | | #include "hmac.h" |
16 | | #include "sha2.h" |
17 | | |
18 | | #include <stdio.h> |
19 | | #include <limits.h> |
20 | | #include <ctype.h> |
21 | | |
22 | | /* Disable our memcpy() safety wrapper. This file is very performance sensitive |
23 | | and it's been checked to work correctly with memcpy(). */ |
24 | | #undef memcpy |
25 | | |
26 | 270 | #define STRCONCAT_BUFSIZE 512 |
27 | | |
28 | | enum _str_trim_sides { |
29 | | STR_TRIM_LEFT = BIT(0), |
30 | | STR_TRIM_RIGHT = BIT(1), |
31 | | }; |
32 | | |
33 | | const unsigned char uchar_nul = '\0'; |
34 | | const unsigned char *uchar_empty_ptr = &uchar_nul; |
35 | | const char *const empty_str_array[] = { NULL }; |
36 | | |
37 | | volatile int timing_safety_unoptimization; |
38 | | |
39 | | int i_snprintf(char *dest, size_t max_chars, const char *format, ...) |
40 | 6.32k | { |
41 | 6.32k | va_list args; |
42 | 6.32k | int ret; |
43 | | |
44 | 6.32k | i_assert(max_chars < INT_MAX); |
45 | | |
46 | 6.32k | va_start(args, format); |
47 | 6.32k | ret = vsnprintf(dest, max_chars, printf_format_fix_unsafe(format), |
48 | 6.32k | args); |
49 | 6.32k | va_end(args); |
50 | | |
51 | 6.32k | i_assert(ret >= 0); |
52 | 6.32k | return (unsigned int)ret < max_chars ? 0 : -1; |
53 | 6.32k | } |
54 | | |
55 | | char *p_strdup(pool_t pool, const char *str) |
56 | 3.66M | { |
57 | 3.66M | void *mem; |
58 | 3.66M | size_t len; |
59 | | |
60 | 3.66M | if (str == NULL) |
61 | 67.2k | return NULL; |
62 | | |
63 | 3.59M | len = strlen(str) + 1; |
64 | 3.59M | mem = p_malloc(pool, len); |
65 | 3.59M | memcpy(mem, str, len); |
66 | 3.59M | return mem; |
67 | 3.66M | } |
68 | | |
69 | | void *p_memdup(pool_t pool, const void *data, size_t size) |
70 | 0 | { |
71 | 0 | void *mem; |
72 | |
|
73 | 0 | mem = p_malloc(pool, size); |
74 | 0 | memcpy(mem, data, size); |
75 | 0 | return mem; |
76 | 0 | } |
77 | | |
78 | | char *p_strdup_empty(pool_t pool, const char *str) |
79 | 6.32k | { |
80 | 6.32k | if (str == NULL || *str == '\0') |
81 | 6.32k | return NULL; |
82 | | |
83 | 0 | return p_strdup(pool, str); |
84 | 6.32k | } |
85 | | |
86 | | char *p_strdup_until(pool_t pool, const void *start, const void *end) |
87 | 204k | { |
88 | 204k | size_t size; |
89 | 204k | char *mem; |
90 | | |
91 | 204k | i_assert((const char *) start <= (const char *) end); |
92 | | |
93 | 204k | size = (size_t) ((const char *) end - (const char *) start); |
94 | | |
95 | 204k | mem = p_malloc(pool, size + 1); |
96 | 204k | memcpy(mem, start, size); |
97 | 204k | return mem; |
98 | 204k | } |
99 | | |
100 | | char *p_strndup(pool_t pool, const void *str, size_t max_chars) |
101 | 31.9k | { |
102 | 31.9k | const char *p; |
103 | 31.9k | char *mem; |
104 | 31.9k | size_t len; |
105 | | |
106 | 31.9k | i_assert(str != NULL); |
107 | 31.9k | i_assert(max_chars != SIZE_MAX); |
108 | | |
109 | 31.9k | p = memchr(str, '\0', max_chars); |
110 | 31.9k | if (p == NULL) |
111 | 31.9k | len = max_chars; |
112 | 0 | else |
113 | 0 | len = p - (const char *)str; |
114 | | |
115 | 31.9k | mem = p_malloc(pool, len+1); |
116 | 31.9k | memcpy(mem, str, len); |
117 | 31.9k | return mem; |
118 | 31.9k | } |
119 | | |
120 | | char *p_strdup_printf(pool_t pool, const char *format, ...) |
121 | 134k | { |
122 | 134k | va_list args; |
123 | 134k | char *ret; |
124 | | |
125 | 134k | va_start(args, format); |
126 | 134k | ret = p_strdup_vprintf(pool, format, args); |
127 | 134k | va_end(args); |
128 | | |
129 | 134k | return ret; |
130 | 134k | } |
131 | | |
132 | | char *t_noalloc_strdup_vprintf(const char *format, va_list args, |
133 | | unsigned int *size_r) |
134 | 617k | { |
135 | 617k | #define SNPRINTF_INITIAL_EXTRA_SIZE 256 |
136 | 617k | va_list args2; |
137 | 617k | char *tmp; |
138 | 617k | size_t init_size; |
139 | 617k | int ret; |
140 | | #ifdef DEBUG_FAST |
141 | | int old_errno = errno; |
142 | | #endif |
143 | | |
144 | 617k | VA_COPY(args2, args); |
145 | | |
146 | | /* the format string is modified only if %m exists in it. it happens |
147 | | only in error conditions, so don't try to t_push() here since it'll |
148 | | just slow down the normal code path. */ |
149 | 617k | format = printf_format_fix_get_len(format, &init_size); |
150 | 617k | init_size += SNPRINTF_INITIAL_EXTRA_SIZE; |
151 | | |
152 | 617k | tmp = t_buffer_get(init_size); |
153 | 617k | ret = vsnprintf(tmp, init_size, format, args); |
154 | 617k | i_assert(ret >= 0); |
155 | | |
156 | 617k | *size_r = ret + 1; |
157 | 617k | if ((unsigned int)ret >= init_size) { |
158 | | /* didn't fit with the first guess. now we know the size, |
159 | | so try again. */ |
160 | 0 | tmp = t_buffer_get(*size_r); |
161 | 0 | ret = vsnprintf(tmp, *size_r, format, args2); |
162 | 0 | i_assert((unsigned int)ret == *size_r-1); |
163 | 0 | } |
164 | | #ifdef DEBUG_FAST |
165 | | /* we rely on errno not changing. it shouldn't. */ |
166 | | i_assert(errno == old_errno); |
167 | | #endif |
168 | 617k | va_end(args2); |
169 | 617k | return tmp; |
170 | 617k | } |
171 | | |
172 | | char *p_strdup_vprintf(pool_t pool, const char *format, va_list args) |
173 | 617k | { |
174 | 617k | char *tmp, *buf; |
175 | 617k | unsigned int size; |
176 | | |
177 | 617k | tmp = t_noalloc_strdup_vprintf(format, args, &size); |
178 | 617k | if (pool->datastack_pool) { |
179 | 457k | t_buffer_alloc(size); |
180 | 457k | return tmp; |
181 | 457k | } else { |
182 | 160k | buf = p_malloc(pool, size); |
183 | 160k | memcpy(buf, tmp, size - 1); |
184 | 160k | return buf; |
185 | 160k | } |
186 | 617k | } |
187 | | |
188 | | char *vstrconcat(const char *str1, va_list args, size_t *ret_len) |
189 | 270 | { |
190 | 270 | const char *str; |
191 | 270 | char *temp; |
192 | 270 | size_t bufsize, i, len; |
193 | | |
194 | 270 | i_assert(str1 != NULL); |
195 | | |
196 | 270 | str = str1; |
197 | 270 | bufsize = STRCONCAT_BUFSIZE; |
198 | 270 | temp = t_buffer_get(bufsize); |
199 | | |
200 | 270 | i = 0; |
201 | 810 | do { |
202 | 810 | len = strlen(str); |
203 | | |
204 | 810 | if (i + len >= bufsize) { |
205 | | /* need more memory */ |
206 | 0 | bufsize = nearest_power(MALLOC_ADD3(i, len, 1)); |
207 | 0 | temp = t_buffer_reget(temp, bufsize); |
208 | 0 | } |
209 | | |
210 | 810 | memcpy(temp + i, str, len); i += len; |
211 | | |
212 | | /* next string */ |
213 | 810 | str = va_arg(args, const char *); |
214 | 810 | } while (str != NULL); |
215 | | |
216 | 270 | i_assert(i < bufsize); |
217 | | |
218 | 270 | temp[i++] = '\0'; |
219 | 270 | *ret_len = i; |
220 | 270 | return temp; |
221 | 270 | } |
222 | | |
223 | | char *p_strconcat(pool_t pool, const char *str1, ...) |
224 | 0 | { |
225 | 0 | va_list args; |
226 | 0 | char *temp, *ret; |
227 | 0 | size_t len; |
228 | |
|
229 | 0 | i_assert(str1 != NULL); |
230 | | |
231 | 0 | va_start(args, str1); |
232 | |
|
233 | 0 | if (pool->datastack_pool) { |
234 | 0 | ret = vstrconcat(str1, args, &len); |
235 | 0 | t_buffer_alloc(len); |
236 | 0 | } else { |
237 | 0 | temp = vstrconcat(str1, args, &len); |
238 | 0 | ret = p_malloc(pool, len); |
239 | 0 | memcpy(ret, temp, len); |
240 | 0 | } |
241 | |
|
242 | 0 | va_end(args); |
243 | 0 | return ret; |
244 | 0 | } |
245 | | |
246 | | void *t_memdup_noconst(const void *data, size_t size) |
247 | 120k | { |
248 | 120k | void *mem = t_malloc_no0(size); |
249 | 120k | memcpy(mem, data, size); |
250 | 120k | return mem; |
251 | 120k | } |
252 | | |
253 | | const void *t_memdup(const void *data, size_t size) |
254 | 0 | { |
255 | 0 | return t_memdup_noconst(data, size); |
256 | 0 | } |
257 | | |
258 | | const char *t_strdup(const char *str) |
259 | 0 | { |
260 | 0 | return t_strdup_noconst(str); |
261 | 0 | } |
262 | | |
263 | | char *t_strdup_noconst(const char *str) |
264 | 120k | { |
265 | 120k | if (str == NULL) |
266 | 0 | return NULL; |
267 | 120k | return t_memdup_noconst(str, strlen(str) + 1); |
268 | 120k | } |
269 | | |
270 | | const char *t_strdup_empty(const char *str) |
271 | 0 | { |
272 | 0 | if (str == NULL || *str == '\0') |
273 | 0 | return NULL; |
274 | | |
275 | 0 | return t_strdup(str); |
276 | 0 | } |
277 | | |
278 | | char *t_strdup_until_noconst(const void *start, const void *end) |
279 | 4.19k | { |
280 | 4.19k | char *mem; |
281 | 4.19k | size_t size; |
282 | | |
283 | 4.19k | i_assert((const char *) start <= (const char *) end); |
284 | | |
285 | 4.19k | size = (size_t)((const char *)end - (const char *)start); |
286 | | |
287 | 4.19k | mem = t_malloc_no0(size + 1); |
288 | 4.19k | memcpy(mem, start, size); |
289 | 4.19k | mem[size] = '\0'; |
290 | 4.19k | return mem; |
291 | 4.19k | } |
292 | | |
293 | | const char *t_strdup_until(const void *start, const void *end) |
294 | 4.19k | { |
295 | 4.19k | return t_strdup_until_noconst(start, end); |
296 | 4.19k | } |
297 | | |
298 | | const char *t_strndup(const void *str, size_t max_chars) |
299 | 31.9k | { |
300 | 31.9k | i_assert(str != NULL); |
301 | 31.9k | return p_strndup(unsafe_data_stack_pool, str, max_chars); |
302 | 31.9k | } |
303 | | |
304 | | const char *t_strdup_printf(const char *format, ...) |
305 | 334k | { |
306 | 334k | va_list args; |
307 | 334k | const char *ret; |
308 | | |
309 | 334k | va_start(args, format); |
310 | 334k | ret = p_strdup_vprintf(unsafe_data_stack_pool, format, args); |
311 | 334k | va_end(args); |
312 | | |
313 | 334k | return ret; |
314 | 334k | } |
315 | | |
316 | | const char *t_strdup_vprintf(const char *format, va_list args) |
317 | 123k | { |
318 | 123k | return p_strdup_vprintf(unsafe_data_stack_pool, format, args); |
319 | 123k | } |
320 | | |
321 | | const char *t_strconcat(const char *str1, ...) |
322 | 270 | { |
323 | 270 | va_list args; |
324 | 270 | const char *ret; |
325 | 270 | size_t len; |
326 | | |
327 | 270 | i_assert(str1 != NULL); |
328 | | |
329 | 270 | va_start(args, str1); |
330 | | |
331 | 270 | ret = vstrconcat(str1, args, &len); |
332 | 270 | t_buffer_alloc(len); |
333 | | |
334 | 270 | va_end(args); |
335 | 270 | return ret; |
336 | 270 | } |
337 | | |
338 | | const char *t_strcut(const char *str, char cutchar) |
339 | 0 | { |
340 | 0 | const char *p; |
341 | |
|
342 | 0 | for (p = str; *p != '\0'; p++) { |
343 | 0 | if (*p == cutchar) |
344 | 0 | return t_strdup_until(str, p); |
345 | 0 | } |
346 | | |
347 | 0 | return str; |
348 | 0 | } |
349 | | |
350 | | const char *t_str_replace(const char *str, char from, char to) |
351 | 0 | { |
352 | 0 | char *out; |
353 | 0 | size_t i, len; |
354 | |
|
355 | 0 | if (strchr(str, from) == NULL) |
356 | 0 | return str; |
357 | | |
358 | 0 | len = strlen(str); |
359 | 0 | out = t_malloc_no0(len + 1); |
360 | 0 | for (i = 0; i < len; i++) { |
361 | 0 | if (str[i] == from) |
362 | 0 | out[i] = to; |
363 | 0 | else |
364 | 0 | out[i] = str[i]; |
365 | 0 | } |
366 | 0 | out[i] = '\0'; |
367 | 0 | return out; |
368 | 0 | } |
369 | | |
370 | | const char *t_str_oneline(const char *str) |
371 | 2.54k | { |
372 | 2.54k | string_t *out; |
373 | 2.54k | size_t len; |
374 | 2.54k | const char *p, *pend, *poff; |
375 | 2.54k | bool new_line; |
376 | | |
377 | 2.54k | if (strpbrk(str, "\r\n") == NULL) |
378 | 2.54k | return str; |
379 | | |
380 | 0 | len = strlen(str); |
381 | 0 | out = t_str_new(len + 1); |
382 | 0 | new_line = TRUE; |
383 | 0 | p = poff = str; |
384 | 0 | pend = str + len; |
385 | 0 | while (p < pend) { |
386 | 0 | switch (*p) { |
387 | 0 | case '\r': |
388 | 0 | if (p > poff) |
389 | 0 | str_append_data(out, poff, p - poff); |
390 | | /* just drop \r */ |
391 | 0 | poff = p + 1; |
392 | 0 | break; |
393 | 0 | case '\n': |
394 | 0 | if (p > poff) |
395 | 0 | str_append_data(out, poff, p - poff); |
396 | 0 | if (new_line) { |
397 | | /* coalesce multiple \n into a single space */ |
398 | 0 | } else { |
399 | | /* first \n after text */ |
400 | 0 | str_append_c(out, ' '); |
401 | 0 | new_line = TRUE; |
402 | 0 | } |
403 | 0 | poff = p + 1; |
404 | 0 | break; |
405 | 0 | default: |
406 | 0 | new_line = FALSE; |
407 | 0 | break; |
408 | 0 | } |
409 | 0 | p++; |
410 | 0 | } |
411 | | |
412 | 0 | if (new_line && str_len(out) > 0) |
413 | 0 | str_truncate(out, str_len(out) - 1); |
414 | 0 | else if (p > poff) |
415 | 0 | str_append_data(out, poff, p - poff); |
416 | 0 | return str_c(out); |
417 | 0 | } |
418 | | |
419 | | int i_strocpy(char *dest, const char *src, size_t dstsize) |
420 | 0 | { |
421 | 0 | if (dstsize == 0) |
422 | 0 | return -1; |
423 | | |
424 | 0 | while (*src != '\0' && dstsize > 1) { |
425 | 0 | *dest++ = *src++; |
426 | 0 | dstsize--; |
427 | 0 | } |
428 | |
|
429 | 0 | *dest = '\0'; |
430 | 0 | return *src == '\0' ? 0 : -1; |
431 | 0 | } |
432 | | |
433 | | char *str_ucase(char *str) |
434 | 120k | { |
435 | 120k | char *p; |
436 | | |
437 | 518k | for (p = str; *p != '\0'; p++) |
438 | 398k | *p = i_toupper(*p); |
439 | 120k | return str; |
440 | 120k | } |
441 | | |
442 | | char *str_lcase(char *str) |
443 | 0 | { |
444 | 0 | char *p; |
445 | |
|
446 | 0 | for (p = str; *p != '\0'; p++) |
447 | 0 | *p = i_tolower(*p); |
448 | 0 | return str; |
449 | 0 | } |
450 | | |
451 | | const char *t_str_lcase(const char *str) |
452 | 0 | { |
453 | 0 | i_assert(str != NULL); |
454 | | |
455 | 0 | return str_lcase(t_strdup_noconst(str)); |
456 | 0 | } |
457 | | |
458 | | const char *t_str_ucase(const char *str) |
459 | 120k | { |
460 | 120k | i_assert(str != NULL); |
461 | | |
462 | 120k | return str_ucase(t_strdup_noconst(str)); |
463 | 120k | } |
464 | | |
465 | | const char *i_strstr_arr(const char *haystack, const char *const *needles) |
466 | 0 | { |
467 | 0 | const char *ptr; |
468 | 0 | for(; *needles != NULL; needles++) |
469 | 0 | if ((ptr = strstr(haystack, *needles)) != NULL) |
470 | 0 | return ptr; |
471 | 0 | return NULL; |
472 | 0 | } |
473 | | |
474 | | static void str_trim_parse(const char *str, |
475 | | const char *chars, enum _str_trim_sides sides, |
476 | | const char **begin_r, const char **end_r) |
477 | 0 | { |
478 | 0 | const char *p, *pend, *begin, *end; |
479 | |
|
480 | 0 | *begin_r = *end_r = NULL; |
481 | |
|
482 | 0 | pend = str + strlen(str); |
483 | 0 | if (pend == str) |
484 | 0 | return; |
485 | | |
486 | 0 | p = str; |
487 | 0 | if ((sides & STR_TRIM_LEFT) != 0) { |
488 | 0 | while (p < pend && strchr(chars, *p) != NULL) |
489 | 0 | p++; |
490 | 0 | if (p == pend) |
491 | 0 | return; |
492 | 0 | } |
493 | 0 | begin = p; |
494 | |
|
495 | 0 | p = pend; |
496 | 0 | if ((sides & STR_TRIM_RIGHT) != 0) { |
497 | 0 | while (p > begin && strchr(chars, *(p-1)) != NULL) |
498 | 0 | p--; |
499 | 0 | if (p == begin) |
500 | 0 | return; |
501 | 0 | } |
502 | 0 | end = p; |
503 | |
|
504 | 0 | *begin_r = begin; |
505 | 0 | *end_r = end; |
506 | 0 | } |
507 | | |
508 | | const char *t_str_trim(const char *str, const char *chars) |
509 | 0 | { |
510 | 0 | const char *begin, *end; |
511 | |
|
512 | 0 | str_trim_parse(str, chars, |
513 | 0 | STR_TRIM_LEFT | STR_TRIM_RIGHT, &begin, &end); |
514 | 0 | if (begin == NULL) |
515 | 0 | return ""; |
516 | 0 | return t_strdup_until(begin, end); |
517 | 0 | } |
518 | | |
519 | | const char *p_str_trim(pool_t pool, const char *str, const char *chars) |
520 | 0 | { |
521 | 0 | const char *begin, *end; |
522 | |
|
523 | 0 | str_trim_parse(str, chars, |
524 | 0 | STR_TRIM_LEFT | STR_TRIM_RIGHT, &begin, &end); |
525 | 0 | if (begin == NULL) |
526 | 0 | return ""; |
527 | 0 | return p_strdup_until(pool, begin, end); |
528 | 0 | } |
529 | | |
530 | | const char *str_ltrim(const char *str, const char *chars) |
531 | 0 | { |
532 | 0 | return str + strspn(str, chars); |
533 | 0 | } |
534 | | |
535 | | const char *t_str_ltrim(const char *str, const char *chars) |
536 | 0 | { |
537 | 0 | return t_strdup(str_ltrim(str, chars)); |
538 | 0 | } |
539 | | |
540 | | const char *p_str_ltrim(pool_t pool, const char *str, const char *chars) |
541 | 0 | { |
542 | 0 | return p_strdup(pool, str_ltrim(str, chars)); |
543 | 0 | } |
544 | | |
545 | | const char *t_str_rtrim(const char *str, const char *chars) |
546 | 0 | { |
547 | 0 | const char *begin, *end; |
548 | |
|
549 | 0 | str_trim_parse(str, chars, STR_TRIM_RIGHT, &begin, &end); |
550 | 0 | if (begin == NULL) |
551 | 0 | return ""; |
552 | 0 | return t_strdup_until(begin, end); |
553 | 0 | } |
554 | | |
555 | | const char *p_str_rtrim(pool_t pool, const char *str, const char *chars) |
556 | 0 | { |
557 | 0 | const char *begin, *end; |
558 | |
|
559 | 0 | str_trim_parse(str, chars, STR_TRIM_RIGHT, &begin, &end); |
560 | 0 | if (begin == NULL) |
561 | 0 | return ""; |
562 | 0 | return p_strdup_until(pool, begin, end); |
563 | 0 | } |
564 | | |
565 | | int null_strcmp(const char *s1, const char *s2) |
566 | 79.1k | { |
567 | 79.1k | if (s1 == NULL) |
568 | 49.8k | return s2 == NULL ? 0 : -1; |
569 | 29.2k | if (s2 == NULL) |
570 | 5.67k | return 1; |
571 | | |
572 | 23.5k | return strcmp(s1, s2); |
573 | 29.2k | } |
574 | | |
575 | | int null_strcasecmp(const char *s1, const char *s2) |
576 | 0 | { |
577 | 0 | if (s1 == NULL) |
578 | 0 | return s2 == NULL ? 0 : -1; |
579 | 0 | if (s2 == NULL) |
580 | 0 | return 1; |
581 | | |
582 | 0 | return strcasecmp(s1, s2); |
583 | 0 | } |
584 | | |
585 | | int i_memcasecmp(const void *p1, const void *p2, size_t size) |
586 | 0 | { |
587 | 0 | const unsigned char *s1 = p1; |
588 | 0 | const unsigned char *s2 = p2; |
589 | 0 | int ret; |
590 | |
|
591 | 0 | while (size > 0) { |
592 | 0 | ret = i_toupper(*s1) - i_toupper(*s2); |
593 | 0 | if (ret != 0) |
594 | 0 | return ret; |
595 | | |
596 | 0 | s1++; s2++; size--; |
597 | 0 | } |
598 | | |
599 | 0 | return 0; |
600 | 0 | } |
601 | | |
602 | | void *i_memrchr(const void *data, int c, size_t size) |
603 | 0 | { |
604 | 0 | #ifdef HAVE_MEMRCHR |
605 | 0 | return memrchr(data, c, size); |
606 | | #else |
607 | | const unsigned char *p = data; |
608 | | |
609 | | while (size > 0) { |
610 | | size--; |
611 | | if (p[size] == (unsigned char)c) |
612 | | return (void *)(p + size); |
613 | | } |
614 | | return NULL; |
615 | | #endif |
616 | 0 | } |
617 | | |
618 | | int i_strcmp_p(const char *const *p1, const char *const *p2) |
619 | 0 | { |
620 | 0 | return strcmp(*p1, *p2); |
621 | 0 | } |
622 | | |
623 | | int i_strcasecmp_p(const char *const *p1, const char *const *p2) |
624 | 0 | { |
625 | 0 | return strcasecmp(*p1, *p2); |
626 | 0 | } |
627 | | |
628 | | bool mem_equals_timing_safe(const void *p1, const void *p2, size_t size) |
629 | 0 | { |
630 | 0 | const unsigned char *s1 = p1, *s2 = p2; |
631 | 0 | size_t i; |
632 | 0 | int ret = 0; |
633 | |
|
634 | 0 | for (i = 0; i < size; i++) |
635 | 0 | ret |= s1[i] ^ s2[i]; |
636 | | |
637 | | /* make sure the compiler optimizer doesn't try to break out of the |
638 | | above loop early. */ |
639 | 0 | timing_safety_unoptimization = ret; |
640 | 0 | return ret == 0; |
641 | 0 | } |
642 | | |
643 | | bool str_equals_timing_almost_safe(const char *s1, const char *s2) |
644 | 0 | { |
645 | 0 | size_t i; |
646 | 0 | int ret = 0; |
647 | |
|
648 | 0 | for (i = 0; s1[i] != '\0' && s2[i] != '\0'; i++) |
649 | 0 | ret |= s1[i] ^ s2[i]; |
650 | 0 | ret |= s1[i] ^ s2[i]; |
651 | | |
652 | | /* make sure the compiler optimizer doesn't try to break out of the |
653 | | above loop early. */ |
654 | 0 | timing_safety_unoptimization = ret; |
655 | 0 | return ret == 0; |
656 | 0 | } |
657 | | |
658 | | bool str_equals_hash_timing_safe(const char *s1, const char *s2) |
659 | 0 | { |
660 | 0 | struct hmac_context ctx; |
661 | 0 | unsigned char digest1[SHA256_RESULTLEN]; |
662 | 0 | unsigned char digest2[SHA256_RESULTLEN]; |
663 | | |
664 | | /* Compare HMAC-SHA256 digests of the inputs rather than the inputs |
665 | | themselves. The digest length is constant, so the subsequent |
666 | | mem_equals_timing_safe() leaks no length information. The HMAC |
667 | | times depend on the input lengths, but each side's length is either |
668 | | a deployment constant (for the secret) or already known to the |
669 | | attacker (for their own input), so neither leaks a useful signal. |
670 | | hash_iv keys the HMAC to prevent precomputation. */ |
671 | 0 | hmac_init(&ctx, (const unsigned char *)&hash_iv, sizeof(hash_iv), |
672 | 0 | &hash_method_sha256); |
673 | 0 | hmac_update(&ctx, s1, strlen(s1)); |
674 | 0 | hmac_final(&ctx, digest1); |
675 | |
|
676 | 0 | hmac_init(&ctx, (const unsigned char *)&hash_iv, sizeof(hash_iv), |
677 | 0 | &hash_method_sha256); |
678 | 0 | hmac_update(&ctx, s2, strlen(s2)); |
679 | 0 | hmac_final(&ctx, digest2); |
680 | |
|
681 | 0 | return mem_equals_timing_safe(digest1, digest2, sizeof(digest1)); |
682 | 0 | } |
683 | | |
684 | | size_t |
685 | | str_match(const char *p1, const char *p2) |
686 | 0 | { |
687 | 0 | size_t i = 0; |
688 | |
|
689 | 0 | while(p1[i] != '\0' && p1[i] == p2[i]) |
690 | 0 | i++; |
691 | |
|
692 | 0 | return i; |
693 | 0 | } |
694 | | |
695 | | size_t str_match_icase(const char *p1, const char *p2) |
696 | 0 | { |
697 | 0 | size_t i = 0; |
698 | |
|
699 | 0 | while (p1[i] != '\0' && i_tolower(p1[i]) == i_tolower(p2[i])) |
700 | 0 | i++; |
701 | |
|
702 | 0 | return i; |
703 | 0 | } |
704 | | |
705 | | bool str_has_dots_component(const char *str, const char *delimiters) |
706 | 0 | { |
707 | 0 | const char *p = str; |
708 | |
|
709 | 0 | while (*p != '\0') { |
710 | 0 | size_t dots = strspn(p, "."); |
711 | |
|
712 | 0 | p += dots; |
713 | 0 | if (dots > 0 && |
714 | 0 | (*p == '\0' || strchr(delimiters, *p) != NULL)) |
715 | 0 | return TRUE; |
716 | | /* skip the rest of the component */ |
717 | 0 | p += strcspn(p, delimiters); |
718 | 0 | if (*p != '\0') |
719 | 0 | p++; |
720 | 0 | } |
721 | 0 | return FALSE; |
722 | 0 | } |
723 | | |
724 | | #undef str_begins |
725 | | bool str_begins(const char *haystack, const char *needle, const char **suffix_r) |
726 | 0 | { |
727 | 0 | size_t prefix_len = str_match(haystack, needle); |
728 | 0 | if (needle[prefix_len] != '\0') |
729 | 0 | return FALSE; |
730 | 0 | *suffix_r = haystack + prefix_len; |
731 | 0 | return TRUE; |
732 | 0 | } |
733 | | |
734 | | #undef str_begins_icase |
735 | | bool str_begins_icase(const char *haystack, const char *needle, |
736 | | const char **suffix_r) |
737 | 0 | { |
738 | 0 | size_t prefix_len = str_match_icase(haystack, needle); |
739 | 0 | if (needle[prefix_len] != '\0') |
740 | 0 | return FALSE; |
741 | 0 | *suffix_r = haystack + prefix_len; |
742 | 0 | return TRUE; |
743 | 0 | } |
744 | | |
745 | | size_t i_memspn(const void *data, size_t data_len, |
746 | | const void *accept, size_t accept_len) |
747 | 0 | { |
748 | 0 | const unsigned char *start = data; |
749 | 0 | i_assert(data != NULL || data_len == 0); |
750 | 0 | i_assert(accept != NULL || accept_len == 0); |
751 | 0 | size_t pos = 0; |
752 | | /* nothing to accept */ |
753 | 0 | if (accept_len == 0) |
754 | 0 | return 0; |
755 | 0 | for (; pos < data_len; pos++) { |
756 | 0 | if (memchr(accept, start[pos], accept_len) == NULL) |
757 | 0 | break; |
758 | 0 | } |
759 | 0 | return pos; |
760 | 0 | } |
761 | | |
762 | | size_t i_memcspn(const void *data, size_t data_len, |
763 | | const void *reject, size_t reject_len) |
764 | 309k | { |
765 | 309k | const unsigned char *start = data; |
766 | 309k | const unsigned char *r = reject; |
767 | 309k | const unsigned char *ptr = CONST_PTR_OFFSET(data, data_len); |
768 | 309k | i_assert(data != NULL || data_len == 0); |
769 | 309k | i_assert(reject != NULL || reject_len == 0); |
770 | | /* nothing to reject */ |
771 | 309k | if (reject_len == 0 || data_len == 0) |
772 | 0 | return data_len; |
773 | | /* Doing repeated memchr's over the data is faster than |
774 | | going over it once byte by byte, as long as reject |
775 | | is reasonably short. */ |
776 | 619k | for (size_t i = 0; i < reject_len; i++) { |
777 | 309k | const unsigned char *kand = |
778 | 309k | memchr(start, r[i], data_len); |
779 | 309k | if (kand != NULL && kand < ptr) |
780 | 307k | ptr = kand; |
781 | 309k | } |
782 | 309k | return ptr - start; |
783 | 309k | } |
784 | | |
785 | | bool t_split_key_value(const char *arg, char separator, |
786 | | const char **key_r, const char **value_r) |
787 | 0 | { |
788 | 0 | *value_r = arg == NULL ? NULL : strchr(arg, separator); |
789 | 0 | if (*value_r != NULL) { |
790 | 0 | *key_r = t_strdup_until(arg, (*value_r)++); |
791 | 0 | return TRUE; |
792 | 0 | } |
793 | 0 | *value_r = ""; |
794 | 0 | *key_r = arg; |
795 | 0 | return FALSE; |
796 | 0 | } |
797 | | |
798 | | static char ** |
799 | | split_str_slow(pool_t pool, const char *data, const char *separators, bool spaces) |
800 | 270 | { |
801 | 270 | char **array; |
802 | 270 | char *str; |
803 | 270 | unsigned int count, alloc_count, new_alloc_count; |
804 | | |
805 | 270 | if (spaces) { |
806 | | /* skip leading separators */ |
807 | 270 | while (*data != '\0' && strchr(separators, *data) != NULL) |
808 | 0 | data++; |
809 | 270 | } |
810 | 270 | if (*data == '\0') |
811 | 0 | return p_new(pool, char *, 1); |
812 | | |
813 | 270 | str = p_strdup(pool, data); |
814 | | |
815 | 270 | alloc_count = 32; |
816 | 270 | array = p_new(pool, char *, alloc_count); |
817 | | |
818 | 270 | array[0] = str; count = 1; |
819 | 7.29k | while (*str != '\0') { |
820 | 7.02k | if (strchr(separators, *str) != NULL) { |
821 | | /* separator found */ |
822 | 0 | if (count+1 >= alloc_count) { |
823 | 0 | new_alloc_count = nearest_power(alloc_count+1); |
824 | 0 | array = p_realloc_type(pool, array, char *, |
825 | 0 | alloc_count, new_alloc_count); |
826 | 0 | alloc_count = new_alloc_count; |
827 | 0 | } |
828 | |
|
829 | 0 | *str = '\0'; |
830 | 0 | if (spaces) { |
831 | 0 | while (str[1] != '\0' && |
832 | 0 | strchr(separators, str[1]) != NULL) |
833 | 0 | str++; |
834 | | |
835 | | /* ignore trailing separators */ |
836 | 0 | if (str[1] == '\0') |
837 | 0 | break; |
838 | 0 | } |
839 | | |
840 | 0 | array[count++] = str+1; |
841 | 0 | } |
842 | | |
843 | 7.02k | str++; |
844 | 7.02k | } |
845 | | |
846 | 270 | i_assert(count < alloc_count); |
847 | 270 | array[count] = NULL; |
848 | | |
849 | 270 | return array; |
850 | 270 | } |
851 | | |
852 | | static char ** |
853 | | split_str_fast(pool_t pool, const char *data, char sep) |
854 | 48.1k | { |
855 | 48.1k | char **array, *str; |
856 | 48.1k | unsigned int count, alloc_count, new_alloc_count; |
857 | | |
858 | 48.1k | if (*data == '\0') |
859 | 21.7k | return p_new(pool, char *, 1); |
860 | | |
861 | 26.3k | str = p_strdup(pool, data); |
862 | | |
863 | 26.3k | alloc_count = 32; |
864 | 26.3k | array = p_new(pool, char *, alloc_count); |
865 | | |
866 | 26.3k | array[0] = str; count = 1; |
867 | 1.59M | while ((str = strchr(str, sep)) != NULL) { |
868 | | /* separator found */ |
869 | 1.56M | if (count+1 >= alloc_count) { |
870 | 7.57k | new_alloc_count = nearest_power(alloc_count+1); |
871 | 7.57k | array = p_realloc_type(pool, array, char *, |
872 | 7.57k | alloc_count, new_alloc_count); |
873 | 7.57k | alloc_count = new_alloc_count; |
874 | 7.57k | } |
875 | 1.56M | *str++ = '\0'; |
876 | 1.56M | array[count++] = str; |
877 | 1.56M | } |
878 | 26.3k | i_assert(count < alloc_count); |
879 | 26.3k | i_assert(array[count] == NULL); |
880 | | |
881 | 26.3k | return array; |
882 | 26.3k | } |
883 | | |
884 | | static char ** |
885 | | split_str(pool_t pool, const char *data, const char *separators, bool spaces) |
886 | 48.3k | { |
887 | 48.3k | i_assert(*separators != '\0'); |
888 | | |
889 | 48.3k | if (separators[1] == '\0' && !spaces) |
890 | 48.1k | return split_str_fast(pool, data, separators[0]); |
891 | 270 | else |
892 | 270 | return split_str_slow(pool, data, separators, spaces); |
893 | 48.3k | } |
894 | | |
895 | | const char **t_strsplit(const char *data, const char *separators) |
896 | 48.1k | { |
897 | 48.1k | return (const char **)split_str(unsafe_data_stack_pool, data, |
898 | 48.1k | separators, FALSE); |
899 | 48.1k | } |
900 | | |
901 | | const char **t_strsplit_spaces(const char *data, const char *separators) |
902 | 270 | { |
903 | 270 | return (const char **)split_str(unsafe_data_stack_pool, data, |
904 | 270 | separators, TRUE); |
905 | 270 | } |
906 | | |
907 | | char **p_strsplit(pool_t pool, const char *data, const char *separators) |
908 | 0 | { |
909 | 0 | return split_str(pool, data, separators, FALSE); |
910 | 0 | } |
911 | | |
912 | | char **p_strsplit_spaces(pool_t pool, const char *data, |
913 | | const char *separators) |
914 | 0 | { |
915 | 0 | return split_str(pool, data, separators, TRUE); |
916 | 0 | } |
917 | | |
918 | | void p_strsplit_free(pool_t pool, char **arr) |
919 | 0 | { |
920 | 0 | p_free(pool, arr[0]); |
921 | 0 | p_free(pool, arr); |
922 | 0 | } |
923 | | |
924 | | unsigned int str_array_length(const char *const *arr) |
925 | 0 | { |
926 | 0 | unsigned int count; |
927 | |
|
928 | 0 | if (arr == NULL) |
929 | 0 | return 0; |
930 | | |
931 | 0 | for (count = 0; *arr != NULL; arr++) |
932 | 0 | count++; |
933 | |
|
934 | 0 | return count; |
935 | 0 | } |
936 | | |
937 | | static char * |
938 | | p_strarray_join_n(pool_t pool, const char *const *arr, unsigned int arr_len, |
939 | | const char *separator) |
940 | 0 | { |
941 | 0 | size_t alloc_len, sep_len, len, pos, needed_space; |
942 | 0 | unsigned int i; |
943 | 0 | char *str; |
944 | |
|
945 | 0 | sep_len = strlen(separator); |
946 | 0 | alloc_len = 64; |
947 | 0 | str = t_buffer_get(alloc_len); |
948 | 0 | pos = 0; |
949 | |
|
950 | 0 | for (i = 0; i < arr_len; i++) { |
951 | 0 | len = strlen(arr[i]); |
952 | 0 | needed_space = MALLOC_ADD(MALLOC_ADD3(pos, len, sep_len), 1); |
953 | 0 | if (needed_space > alloc_len) { |
954 | 0 | alloc_len = nearest_power(needed_space); |
955 | 0 | str = t_buffer_reget(str, alloc_len); |
956 | 0 | } |
957 | |
|
958 | 0 | if (i != 0) { |
959 | 0 | memcpy(str + pos, separator, sep_len); |
960 | 0 | pos += sep_len; |
961 | 0 | } |
962 | |
|
963 | 0 | memcpy(str + pos, arr[i], len); |
964 | 0 | pos += len; |
965 | 0 | } |
966 | 0 | str[pos] = '\0'; |
967 | 0 | if (!pool->datastack_pool) |
968 | 0 | return p_memdup(pool, str, pos + 1); |
969 | 0 | t_buffer_alloc(pos + 1); |
970 | 0 | return str; |
971 | 0 | } |
972 | | |
973 | | const char *t_strarray_join(const char *const *arr, const char *separator) |
974 | 0 | { |
975 | 0 | return p_strarray_join_n(unsafe_data_stack_pool, arr, |
976 | 0 | str_array_length(arr), separator); |
977 | 0 | } |
978 | | |
979 | | bool str_array_remove(const char **arr, const char *value) |
980 | 0 | { |
981 | 0 | const char **dest; |
982 | |
|
983 | 0 | for (; *arr != NULL; arr++) { |
984 | 0 | if (strcmp(*arr, value) == 0) { |
985 | | /* found it. now move the rest. */ |
986 | 0 | for (dest = arr, arr++; *arr != NULL; arr++, dest++) |
987 | 0 | *dest = *arr; |
988 | 0 | *dest = NULL; |
989 | 0 | return TRUE; |
990 | 0 | } |
991 | 0 | } |
992 | 0 | return FALSE; |
993 | 0 | } |
994 | | |
995 | | bool str_array_find(const char *const *arr, const char *value) |
996 | 0 | { |
997 | 0 | for (; *arr != NULL; arr++) { |
998 | 0 | if (strcmp(*arr, value) == 0) |
999 | 0 | return TRUE; |
1000 | 0 | } |
1001 | 0 | return FALSE; |
1002 | 0 | } |
1003 | | |
1004 | | bool str_array_icase_find(const char *const *arr, const char *value) |
1005 | 0 | { |
1006 | 0 | for (; *arr != NULL; arr++) { |
1007 | 0 | if (strcasecmp(*arr, value) == 0) |
1008 | 0 | return TRUE; |
1009 | 0 | } |
1010 | 0 | return FALSE; |
1011 | 0 | } |
1012 | | |
1013 | | const char **p_strarray_dup(pool_t pool, const char *const *arr) |
1014 | 0 | { |
1015 | 0 | unsigned int i; |
1016 | 0 | const char **ret; |
1017 | 0 | char *p; |
1018 | 0 | size_t len, size = sizeof(const char *); |
1019 | |
|
1020 | 0 | for (i = 0; arr[i] != NULL; i++) { |
1021 | 0 | size = MALLOC_ADD3(size, sizeof(const char *) + 1, |
1022 | 0 | strlen(arr[i])); |
1023 | 0 | } |
1024 | |
|
1025 | 0 | ret = p_malloc(pool, size); |
1026 | 0 | p = PTR_OFFSET(ret, sizeof(const char *) * (i + 1)); |
1027 | 0 | for (i = 0; arr[i] != NULL; i++) { |
1028 | 0 | len = strlen(arr[i]) + 1; |
1029 | 0 | memcpy(p, arr[i], len); |
1030 | 0 | ret[i] = p; |
1031 | 0 | p += len; |
1032 | 0 | } |
1033 | 0 | i_assert(PTR_OFFSET(ret, size) == (void *)p); |
1034 | 0 | return ret; |
1035 | 0 | } |
1036 | | |
1037 | | const char *dec2str(uintmax_t number) |
1038 | 0 | { |
1039 | 0 | return dec2str_buf(t_malloc_no0(MAX_INT_STRLEN), number); |
1040 | 0 | } |
1041 | | |
1042 | | char *dec2str_buf(char buffer[STATIC_ARRAY MAX_INT_STRLEN], uintmax_t number) |
1043 | 0 | { |
1044 | 0 | int pos; |
1045 | |
|
1046 | 0 | pos = MAX_INT_STRLEN; |
1047 | 0 | buffer[--pos] = '\0'; |
1048 | 0 | do { |
1049 | 0 | buffer[--pos] = (number % 10) + '0'; |
1050 | 0 | number /= 10; |
1051 | 0 | } while (number != 0 && pos >= 0); |
1052 | |
|
1053 | 0 | i_assert(pos >= 0); |
1054 | 0 | return buffer + pos; |
1055 | 0 | } |
1056 | | |
1057 | | char *p_array_const_string_join(pool_t pool, const ARRAY_TYPE(const_string) *arr, |
1058 | | const char *separator) |
1059 | 0 | { |
1060 | 0 | if (array_is_empty(arr)) |
1061 | 0 | return ""; |
1062 | 0 | return p_strarray_join_n(pool, array_front(arr), array_count(arr), |
1063 | 0 | separator); |
1064 | 0 | } |