]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-util.c
util: introduce memcmp_safe()
[thirdparty/systemd.git] / src / basic / string-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stdarg.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdio_ext.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "alloc-util.h"
12 #include "escape.h"
13 #include "gunicode.h"
14 #include "locale-util.h"
15 #include "macro.h"
16 #include "string-util.h"
17 #include "terminal-util.h"
18 #include "utf8.h"
19 #include "util.h"
20 #include "fileio.h"
21
22 int strcmp_ptr(const char *a, const char *b) {
23
24 /* Like strcmp(), but tries to make sense of NULL pointers */
25 if (a && b)
26 return strcmp(a, b);
27
28 if (!a && b)
29 return -1;
30
31 if (a && !b)
32 return 1;
33
34 return 0;
35 }
36
37 char* endswith(const char *s, const char *postfix) {
38 size_t sl, pl;
39
40 assert(s);
41 assert(postfix);
42
43 sl = strlen(s);
44 pl = strlen(postfix);
45
46 if (pl == 0)
47 return (char*) s + sl;
48
49 if (sl < pl)
50 return NULL;
51
52 if (memcmp(s + sl - pl, postfix, pl) != 0)
53 return NULL;
54
55 return (char*) s + sl - pl;
56 }
57
58 char* endswith_no_case(const char *s, const char *postfix) {
59 size_t sl, pl;
60
61 assert(s);
62 assert(postfix);
63
64 sl = strlen(s);
65 pl = strlen(postfix);
66
67 if (pl == 0)
68 return (char*) s + sl;
69
70 if (sl < pl)
71 return NULL;
72
73 if (strcasecmp(s + sl - pl, postfix) != 0)
74 return NULL;
75
76 return (char*) s + sl - pl;
77 }
78
79 char* first_word(const char *s, const char *word) {
80 size_t sl, wl;
81 const char *p;
82
83 assert(s);
84 assert(word);
85
86 /* Checks if the string starts with the specified word, either
87 * followed by NUL or by whitespace. Returns a pointer to the
88 * NUL or the first character after the whitespace. */
89
90 sl = strlen(s);
91 wl = strlen(word);
92
93 if (sl < wl)
94 return NULL;
95
96 if (wl == 0)
97 return (char*) s;
98
99 if (memcmp(s, word, wl) != 0)
100 return NULL;
101
102 p = s + wl;
103 if (*p == 0)
104 return (char*) p;
105
106 if (!strchr(WHITESPACE, *p))
107 return NULL;
108
109 p += strspn(p, WHITESPACE);
110 return (char*) p;
111 }
112
113 static size_t strcspn_escaped(const char *s, const char *reject) {
114 bool escaped = false;
115 int n;
116
117 for (n=0; s[n]; n++) {
118 if (escaped)
119 escaped = false;
120 else if (s[n] == '\\')
121 escaped = true;
122 else if (strchr(reject, s[n]))
123 break;
124 }
125
126 /* if s ends in \, return index of previous char */
127 return n - escaped;
128 }
129
130 /* Split a string into words. */
131 const char* split(const char **state, size_t *l, const char *separator, bool quoted) {
132 const char *current;
133
134 current = *state;
135
136 if (!*current) {
137 assert(**state == '\0');
138 return NULL;
139 }
140
141 current += strspn(current, separator);
142 if (!*current) {
143 *state = current;
144 return NULL;
145 }
146
147 if (quoted && strchr("\'\"", *current)) {
148 char quotechars[2] = {*current, '\0'};
149
150 *l = strcspn_escaped(current + 1, quotechars);
151 if (current[*l + 1] == '\0' || current[*l + 1] != quotechars[0] ||
152 (current[*l + 2] && !strchr(separator, current[*l + 2]))) {
153 /* right quote missing or garbage at the end */
154 *state = current;
155 return NULL;
156 }
157 *state = current++ + *l + 2;
158 } else if (quoted) {
159 *l = strcspn_escaped(current, separator);
160 if (current[*l] && !strchr(separator, current[*l])) {
161 /* unfinished escape */
162 *state = current;
163 return NULL;
164 }
165 *state = current + *l;
166 } else {
167 *l = strcspn(current, separator);
168 *state = current + *l;
169 }
170
171 return current;
172 }
173
174 char *strnappend(const char *s, const char *suffix, size_t b) {
175 size_t a;
176 char *r;
177
178 if (!s && !suffix)
179 return strdup("");
180
181 if (!s)
182 return strndup(suffix, b);
183
184 if (!suffix)
185 return strdup(s);
186
187 assert(s);
188 assert(suffix);
189
190 a = strlen(s);
191 if (b > ((size_t) -1) - a)
192 return NULL;
193
194 r = new(char, a+b+1);
195 if (!r)
196 return NULL;
197
198 memcpy(r, s, a);
199 memcpy(r+a, suffix, b);
200 r[a+b] = 0;
201
202 return r;
203 }
204
205 char *strappend(const char *s, const char *suffix) {
206 return strnappend(s, suffix, strlen_ptr(suffix));
207 }
208
209 char *strjoin_real(const char *x, ...) {
210 va_list ap;
211 size_t l;
212 char *r, *p;
213
214 va_start(ap, x);
215
216 if (x) {
217 l = strlen(x);
218
219 for (;;) {
220 const char *t;
221 size_t n;
222
223 t = va_arg(ap, const char *);
224 if (!t)
225 break;
226
227 n = strlen(t);
228 if (n > ((size_t) -1) - l) {
229 va_end(ap);
230 return NULL;
231 }
232
233 l += n;
234 }
235 } else
236 l = 0;
237
238 va_end(ap);
239
240 r = new(char, l+1);
241 if (!r)
242 return NULL;
243
244 if (x) {
245 p = stpcpy(r, x);
246
247 va_start(ap, x);
248
249 for (;;) {
250 const char *t;
251
252 t = va_arg(ap, const char *);
253 if (!t)
254 break;
255
256 p = stpcpy(p, t);
257 }
258
259 va_end(ap);
260 } else
261 r[0] = 0;
262
263 return r;
264 }
265
266 char *strstrip(char *s) {
267 if (!s)
268 return NULL;
269
270 /* Drops trailing whitespace. Modifies the string in place. Returns pointer to first non-space character */
271
272 return delete_trailing_chars(skip_leading_chars(s, WHITESPACE), WHITESPACE);
273 }
274
275 char *delete_chars(char *s, const char *bad) {
276 char *f, *t;
277
278 /* Drops all specified bad characters, regardless where in the string */
279
280 if (!s)
281 return NULL;
282
283 if (!bad)
284 bad = WHITESPACE;
285
286 for (f = s, t = s; *f; f++) {
287 if (strchr(bad, *f))
288 continue;
289
290 *(t++) = *f;
291 }
292
293 *t = 0;
294
295 return s;
296 }
297
298 char *delete_trailing_chars(char *s, const char *bad) {
299 char *p, *c = s;
300
301 /* Drops all specified bad characters, at the end of the string */
302
303 if (!s)
304 return NULL;
305
306 if (!bad)
307 bad = WHITESPACE;
308
309 for (p = s; *p; p++)
310 if (!strchr(bad, *p))
311 c = p + 1;
312
313 *c = 0;
314
315 return s;
316 }
317
318 char *truncate_nl(char *s) {
319 assert(s);
320
321 s[strcspn(s, NEWLINE)] = 0;
322 return s;
323 }
324
325 char ascii_tolower(char x) {
326
327 if (x >= 'A' && x <= 'Z')
328 return x - 'A' + 'a';
329
330 return x;
331 }
332
333 char ascii_toupper(char x) {
334
335 if (x >= 'a' && x <= 'z')
336 return x - 'a' + 'A';
337
338 return x;
339 }
340
341 char *ascii_strlower(char *t) {
342 char *p;
343
344 assert(t);
345
346 for (p = t; *p; p++)
347 *p = ascii_tolower(*p);
348
349 return t;
350 }
351
352 char *ascii_strupper(char *t) {
353 char *p;
354
355 assert(t);
356
357 for (p = t; *p; p++)
358 *p = ascii_toupper(*p);
359
360 return t;
361 }
362
363 char *ascii_strlower_n(char *t, size_t n) {
364 size_t i;
365
366 if (n <= 0)
367 return t;
368
369 for (i = 0; i < n; i++)
370 t[i] = ascii_tolower(t[i]);
371
372 return t;
373 }
374
375 int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
376
377 for (; n > 0; a++, b++, n--) {
378 int x, y;
379
380 x = (int) (uint8_t) ascii_tolower(*a);
381 y = (int) (uint8_t) ascii_tolower(*b);
382
383 if (x != y)
384 return x - y;
385 }
386
387 return 0;
388 }
389
390 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) {
391 int r;
392
393 r = ascii_strcasecmp_n(a, b, MIN(n, m));
394 if (r != 0)
395 return r;
396
397 if (n < m)
398 return -1;
399 else if (n > m)
400 return 1;
401 else
402 return 0;
403 }
404
405 bool chars_intersect(const char *a, const char *b) {
406 const char *p;
407
408 /* Returns true if any of the chars in a are in b. */
409 for (p = a; *p; p++)
410 if (strchr(b, *p))
411 return true;
412
413 return false;
414 }
415
416 bool string_has_cc(const char *p, const char *ok) {
417 const char *t;
418
419 assert(p);
420
421 /*
422 * Check if a string contains control characters. If 'ok' is
423 * non-NULL it may be a string containing additional CCs to be
424 * considered OK.
425 */
426
427 for (t = p; *t; t++) {
428 if (ok && strchr(ok, *t))
429 continue;
430
431 if (*t > 0 && *t < ' ')
432 return true;
433
434 if (*t == 127)
435 return true;
436 }
437
438 return false;
439 }
440
441 static int write_ellipsis(char *buf, bool unicode) {
442 if (unicode || is_locale_utf8()) {
443 buf[0] = 0xe2; /* tri-dot ellipsis: … */
444 buf[1] = 0x80;
445 buf[2] = 0xa6;
446 } else {
447 buf[0] = '.';
448 buf[1] = '.';
449 buf[2] = '.';
450 }
451
452 return 3;
453 }
454
455 static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
456 size_t x, need_space, suffix_len;
457 char *t;
458
459 assert(s);
460 assert(percent <= 100);
461 assert(new_length != (size_t) -1);
462
463 if (old_length <= new_length)
464 return strndup(s, old_length);
465
466 /* Special case short ellipsations */
467 switch (new_length) {
468
469 case 0:
470 return strdup("");
471
472 case 1:
473 if (is_locale_utf8())
474 return strdup("…");
475 else
476 return strdup(".");
477
478 case 2:
479 if (!is_locale_utf8())
480 return strdup("..");
481
482 break;
483
484 default:
485 break;
486 }
487
488 /* Calculate how much space the ellipsis will take up. If we are in UTF-8 mode we only need space for one
489 * character ("…"), otherwise for three characters ("..."). Note that in both cases we need 3 bytes of storage,
490 * either for the UTF-8 encoded character or for three ASCII characters. */
491 need_space = is_locale_utf8() ? 1 : 3;
492
493 t = new(char, new_length+3);
494 if (!t)
495 return NULL;
496
497 assert(new_length >= need_space);
498
499 x = ((new_length - need_space) * percent + 50) / 100;
500 assert(x <= new_length - need_space);
501
502 memcpy(t, s, x);
503 write_ellipsis(t + x, false);
504 suffix_len = new_length - x - need_space;
505 memcpy(t + x + 3, s + old_length - suffix_len, suffix_len);
506 *(t + x + 3 + suffix_len) = '\0';
507
508 return t;
509 }
510
511 char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
512 size_t x, k, len, len2;
513 const char *i, *j;
514 char *e;
515 int r;
516
517 /* Note that 'old_length' refers to bytes in the string, while 'new_length' refers to character cells taken up
518 * on screen. This distinction doesn't matter for ASCII strings, but it does matter for non-ASCII UTF-8
519 * strings.
520 *
521 * Ellipsation is done in a locale-dependent way:
522 * 1. If the string passed in is fully ASCII and the current locale is not UTF-8, three dots are used ("...")
523 * 2. Otherwise, a unicode ellipsis is used ("…")
524 *
525 * In other words: you'll get a unicode ellipsis as soon as either the string contains non-ASCII characters or
526 * the current locale is UTF-8.
527 */
528
529 assert(s);
530 assert(percent <= 100);
531
532 if (new_length == (size_t) -1)
533 return strndup(s, old_length);
534
535 if (new_length == 0)
536 return strdup("");
537
538 /* If no multibyte characters use ascii_ellipsize_mem for speed */
539 if (ascii_is_valid_n(s, old_length))
540 return ascii_ellipsize_mem(s, old_length, new_length, percent);
541
542 x = ((new_length - 1) * percent) / 100;
543 assert(x <= new_length - 1);
544
545 k = 0;
546 for (i = s; i < s + old_length; i = utf8_next_char(i)) {
547 char32_t c;
548 int w;
549
550 r = utf8_encoded_to_unichar(i, &c);
551 if (r < 0)
552 return NULL;
553
554 w = unichar_iswide(c) ? 2 : 1;
555 if (k + w <= x)
556 k += w;
557 else
558 break;
559 }
560
561 for (j = s + old_length; j > i; ) {
562 char32_t c;
563 int w;
564 const char *jj;
565
566 jj = utf8_prev_char(j);
567 r = utf8_encoded_to_unichar(jj, &c);
568 if (r < 0)
569 return NULL;
570
571 w = unichar_iswide(c) ? 2 : 1;
572 if (k + w <= new_length) {
573 k += w;
574 j = jj;
575 } else
576 break;
577 }
578 assert(i <= j);
579
580 /* we don't actually need to ellipsize */
581 if (i == j)
582 return memdup_suffix0(s, old_length);
583
584 /* make space for ellipsis, if possible */
585 if (j < s + old_length)
586 j = utf8_next_char(j);
587 else if (i > s)
588 i = utf8_prev_char(i);
589
590 len = i - s;
591 len2 = s + old_length - j;
592 e = new(char, len + 3 + len2 + 1);
593 if (!e)
594 return NULL;
595
596 /*
597 printf("old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u\n",
598 old_length, new_length, x, len, len2, k);
599 */
600
601 memcpy(e, s, len);
602 write_ellipsis(e + len, true);
603 memcpy(e + len + 3, j, len2);
604 *(e + len + 3 + len2) = '\0';
605
606 return e;
607 }
608
609 char *cellescape(char *buf, size_t len, const char *s) {
610 /* Escape and ellipsize s into buffer buf of size len. Only non-control ASCII
611 * characters are copied as they are, everything else is escaped. The result
612 * is different then if escaping and ellipsization was performed in two
613 * separate steps, because each sequence is either stored in full or skipped.
614 *
615 * This function should be used for logging about strings which expected to
616 * be plain ASCII in a safe way.
617 *
618 * An ellipsis will be used if s is too long. It was always placed at the
619 * very end.
620 */
621
622 size_t i = 0, last_char_width[4] = {}, k = 0, j;
623
624 assert(len > 0); /* at least a terminating NUL */
625
626 for (;;) {
627 char four[4];
628 int w;
629
630 if (*s == 0) /* terminating NUL detected? then we are done! */
631 goto done;
632
633 w = cescape_char(*s, four);
634 if (i + w + 1 > len) /* This character doesn't fit into the buffer anymore? In that case let's
635 * ellipsize at the previous location */
636 break;
637
638 /* OK, there was space, let's add this escaped character to the buffer */
639 memcpy(buf + i, four, w);
640 i += w;
641
642 /* And remember its width in the ring buffer */
643 last_char_width[k] = w;
644 k = (k + 1) % 4;
645
646 s++;
647 }
648
649 /* Ellipsation is necessary. This means we might need to truncate the string again to make space for 4
650 * characters ideally, but the buffer is shorter than that in the first place take what we can get */
651 for (j = 0; j < ELEMENTSOF(last_char_width); j++) {
652
653 if (i + 4 <= len) /* nice, we reached our space goal */
654 break;
655
656 k = k == 0 ? 3 : k - 1;
657 if (last_char_width[k] == 0) /* bummer, we reached the beginning of the strings */
658 break;
659
660 assert(i >= last_char_width[k]);
661 i -= last_char_width[k];
662 }
663
664 if (i + 4 <= len) /* yay, enough space */
665 i += write_ellipsis(buf + i, false);
666 else if (i + 3 <= len) { /* only space for ".." */
667 buf[i++] = '.';
668 buf[i++] = '.';
669 } else if (i + 2 <= len) /* only space for a single "." */
670 buf[i++] = '.';
671 else
672 assert(i + 1 <= len);
673
674 done:
675 buf[i] = '\0';
676 return buf;
677 }
678
679 bool nulstr_contains(const char *nulstr, const char *needle) {
680 const char *i;
681
682 if (!nulstr)
683 return false;
684
685 NULSTR_FOREACH(i, nulstr)
686 if (streq(i, needle))
687 return true;
688
689 return false;
690 }
691
692 char* strshorten(char *s, size_t l) {
693 assert(s);
694
695 if (strnlen(s, l+1) > l)
696 s[l] = 0;
697
698 return s;
699 }
700
701 char *strreplace(const char *text, const char *old_string, const char *new_string) {
702 size_t l, old_len, new_len, allocated = 0;
703 char *t, *ret = NULL;
704 const char *f;
705
706 assert(old_string);
707 assert(new_string);
708
709 if (!text)
710 return NULL;
711
712 old_len = strlen(old_string);
713 new_len = strlen(new_string);
714
715 l = strlen(text);
716 if (!GREEDY_REALLOC(ret, allocated, l+1))
717 return NULL;
718
719 f = text;
720 t = ret;
721 while (*f) {
722 size_t d, nl;
723
724 if (!startswith(f, old_string)) {
725 *(t++) = *(f++);
726 continue;
727 }
728
729 d = t - ret;
730 nl = l - old_len + new_len;
731
732 if (!GREEDY_REALLOC(ret, allocated, nl + 1))
733 return mfree(ret);
734
735 l = nl;
736 t = ret + d;
737
738 t = stpcpy(t, new_string);
739 f += old_len;
740 }
741
742 *t = 0;
743 return ret;
744 }
745
746 static void advance_offsets(ssize_t diff, size_t offsets[2], size_t shift[2], size_t size) {
747 if (!offsets)
748 return;
749
750 if ((size_t) diff < offsets[0])
751 shift[0] += size;
752 if ((size_t) diff < offsets[1])
753 shift[1] += size;
754 }
755
756 char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
757 const char *i, *begin = NULL;
758 enum {
759 STATE_OTHER,
760 STATE_ESCAPE,
761 STATE_CSI,
762 STATE_CSO,
763 } state = STATE_OTHER;
764 char *obuf = NULL;
765 size_t osz = 0, isz, shift[2] = {};
766 FILE *f;
767
768 assert(ibuf);
769 assert(*ibuf);
770
771 /* This does three things:
772 *
773 * 1. Replaces TABs by 8 spaces
774 * 2. Strips ANSI color sequences (a subset of CSI), i.e. ESC '[' … 'm' sequences
775 * 3. Strips ANSI operating system sequences (CSO), i.e. ESC ']' … BEL sequences
776 *
777 * Everything else will be left as it is. In particular other ANSI sequences are left as they are, as are any
778 * other special characters. Truncated ANSI sequences are left-as is too. This call is supposed to suppress the
779 * most basic formatting noise, but nothing else.
780 *
781 * Why care for CSO sequences? Well, to undo what terminal_urlify() and friends generate. */
782
783 isz = _isz ? *_isz : strlen(*ibuf);
784
785 f = open_memstream(&obuf, &osz);
786 if (!f)
787 return NULL;
788
789 /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we created f here
790 * and it doesn't leave our scope. */
791
792 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
793
794 for (i = *ibuf; i < *ibuf + isz + 1; i++) {
795
796 switch (state) {
797
798 case STATE_OTHER:
799 if (i >= *ibuf + isz) /* EOT */
800 break;
801 else if (*i == '\x1B')
802 state = STATE_ESCAPE;
803 else if (*i == '\t') {
804 fputs(" ", f);
805 advance_offsets(i - *ibuf, highlight, shift, 7);
806 } else
807 fputc(*i, f);
808
809 break;
810
811 case STATE_ESCAPE:
812 if (i >= *ibuf + isz) { /* EOT */
813 fputc('\x1B', f);
814 advance_offsets(i - *ibuf, highlight, shift, 1);
815 break;
816 } else if (*i == '[') { /* ANSI CSI */
817 state = STATE_CSI;
818 begin = i + 1;
819 } else if (*i == ']') { /* ANSI CSO */
820 state = STATE_CSO;
821 begin = i + 1;
822 } else {
823 fputc('\x1B', f);
824 fputc(*i, f);
825 advance_offsets(i - *ibuf, highlight, shift, 1);
826 state = STATE_OTHER;
827 }
828
829 break;
830
831 case STATE_CSI:
832
833 if (i >= *ibuf + isz || /* EOT … */
834 !strchr("01234567890;m", *i)) { /* … or invalid chars in sequence */
835 fputc('\x1B', f);
836 fputc('[', f);
837 advance_offsets(i - *ibuf, highlight, shift, 2);
838 state = STATE_OTHER;
839 i = begin-1;
840 } else if (*i == 'm')
841 state = STATE_OTHER;
842
843 break;
844
845 case STATE_CSO:
846
847 if (i >= *ibuf + isz || /* EOT … */
848 (*i != '\a' && (uint8_t) *i < 32U) || (uint8_t) *i > 126U) { /* … or invalid chars in sequence */
849 fputc('\x1B', f);
850 fputc(']', f);
851 advance_offsets(i - *ibuf, highlight, shift, 2);
852 state = STATE_OTHER;
853 i = begin-1;
854 } else if (*i == '\a')
855 state = STATE_OTHER;
856
857 break;
858 }
859 }
860
861 if (fflush_and_check(f) < 0) {
862 fclose(f);
863 return mfree(obuf);
864 }
865
866 fclose(f);
867
868 free(*ibuf);
869 *ibuf = obuf;
870
871 if (_isz)
872 *_isz = osz;
873
874 if (highlight) {
875 highlight[0] += shift[0];
876 highlight[1] += shift[1];
877 }
878
879 return obuf;
880 }
881
882 char *strextend_with_separator(char **x, const char *separator, ...) {
883 bool need_separator;
884 size_t f, l, l_separator;
885 char *r, *p;
886 va_list ap;
887
888 assert(x);
889
890 l = f = strlen_ptr(*x);
891
892 need_separator = !isempty(*x);
893 l_separator = strlen_ptr(separator);
894
895 va_start(ap, separator);
896 for (;;) {
897 const char *t;
898 size_t n;
899
900 t = va_arg(ap, const char *);
901 if (!t)
902 break;
903
904 n = strlen(t);
905
906 if (need_separator)
907 n += l_separator;
908
909 if (n > ((size_t) -1) - l) {
910 va_end(ap);
911 return NULL;
912 }
913
914 l += n;
915 need_separator = true;
916 }
917 va_end(ap);
918
919 need_separator = !isempty(*x);
920
921 r = realloc(*x, l+1);
922 if (!r)
923 return NULL;
924
925 p = r + f;
926
927 va_start(ap, separator);
928 for (;;) {
929 const char *t;
930
931 t = va_arg(ap, const char *);
932 if (!t)
933 break;
934
935 if (need_separator && separator)
936 p = stpcpy(p, separator);
937
938 p = stpcpy(p, t);
939
940 need_separator = true;
941 }
942 va_end(ap);
943
944 assert(p == r + l);
945
946 *p = 0;
947 *x = r;
948
949 return r + l;
950 }
951
952 char *strrep(const char *s, unsigned n) {
953 size_t l;
954 char *r, *p;
955 unsigned i;
956
957 assert(s);
958
959 l = strlen(s);
960 p = r = malloc(l * n + 1);
961 if (!r)
962 return NULL;
963
964 for (i = 0; i < n; i++)
965 p = stpcpy(p, s);
966
967 *p = 0;
968 return r;
969 }
970
971 int split_pair(const char *s, const char *sep, char **l, char **r) {
972 char *x, *a, *b;
973
974 assert(s);
975 assert(sep);
976 assert(l);
977 assert(r);
978
979 if (isempty(sep))
980 return -EINVAL;
981
982 x = strstr(s, sep);
983 if (!x)
984 return -EINVAL;
985
986 a = strndup(s, x - s);
987 if (!a)
988 return -ENOMEM;
989
990 b = strdup(x + strlen(sep));
991 if (!b) {
992 free(a);
993 return -ENOMEM;
994 }
995
996 *l = a;
997 *r = b;
998
999 return 0;
1000 }
1001
1002 int free_and_strdup(char **p, const char *s) {
1003 char *t;
1004
1005 assert(p);
1006
1007 /* Replaces a string pointer with an strdup()ed new string,
1008 * possibly freeing the old one. */
1009
1010 if (streq_ptr(*p, s))
1011 return 0;
1012
1013 if (s) {
1014 t = strdup(s);
1015 if (!t)
1016 return -ENOMEM;
1017 } else
1018 t = NULL;
1019
1020 free(*p);
1021 *p = t;
1022
1023 return 1;
1024 }
1025
1026 #if !HAVE_EXPLICIT_BZERO
1027 /*
1028 * Pointer to memset is volatile so that compiler must de-reference
1029 * the pointer and can't assume that it points to any function in
1030 * particular (such as memset, which it then might further "optimize")
1031 * This approach is inspired by openssl's crypto/mem_clr.c.
1032 */
1033 typedef void *(*memset_t)(void *,int,size_t);
1034
1035 static volatile memset_t memset_func = memset;
1036
1037 void explicit_bzero(void *p, size_t l) {
1038 memset_func(p, '\0', l);
1039 }
1040 #endif
1041
1042 char* string_erase(char *x) {
1043 if (!x)
1044 return NULL;
1045
1046 /* A delicious drop of snake-oil! To be called on memory where
1047 * we stored passphrases or so, after we used them. */
1048 explicit_bzero(x, strlen(x));
1049 return x;
1050 }
1051
1052 char *string_free_erase(char *s) {
1053 return mfree(string_erase(s));
1054 }
1055
1056 bool string_is_safe(const char *p) {
1057 const char *t;
1058
1059 if (!p)
1060 return false;
1061
1062 for (t = p; *t; t++) {
1063 if (*t > 0 && *t < ' ') /* no control characters */
1064 return false;
1065
1066 if (strchr(QUOTES "\\\x7f", *t))
1067 return false;
1068 }
1069
1070 return true;
1071 }