Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright © 2017 Red Hat, Inc |
3 |
|
|
* |
4 |
|
|
* This program is free software; you can redistribute it and/or |
5 |
|
|
* modify it under the terms of the GNU Lesser General Public |
6 |
|
|
* License as published by the Free Software Foundation; either |
7 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
8 |
|
|
* |
9 |
|
|
* This library is distributed in the hope that it will be useful, |
10 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
|
|
* Lesser General Public License for more details. |
13 |
|
|
* |
14 |
|
|
* You should have received a copy of the GNU Lesser General Public |
15 |
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 |
|
|
* |
17 |
|
|
* Authors: |
18 |
|
|
* Christian J. Kellner <christian@kellner.me> |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include "config.h" |
22 |
|
|
|
23 |
|
|
#include "bolt-str.h" |
24 |
|
|
|
25 |
|
|
#include <string.h> |
26 |
|
|
|
27 |
|
|
typedef void (* zero_fn_t) (void *s, |
28 |
|
|
size_t n); |
29 |
|
|
void |
30 |
|
✗ |
bolt_erase_n (void *data, gsize n) |
31 |
|
|
{ |
32 |
|
|
#if !HAVE_FN_EXPLICIT_BZERO |
33 |
|
|
#warning no explicit bzero, using fallback |
34 |
|
|
static volatile zero_fn_t explicit_bzero = bzero; |
35 |
|
|
#endif |
36 |
|
|
|
37 |
|
✗ |
explicit_bzero (data, n); |
38 |
|
✗ |
} |
39 |
|
|
|
40 |
|
|
void |
41 |
|
✗ |
bolt_str_erase (char *str) |
42 |
|
|
{ |
43 |
|
✗ |
if (str == NULL) |
44 |
|
✗ |
return; |
45 |
|
|
|
46 |
|
✗ |
bolt_erase_n (str, strlen (str)); |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
void |
50 |
|
✗ |
bolt_str_erase_clear (char **str) |
51 |
|
|
{ |
52 |
|
✗ |
g_return_if_fail (str != NULL); |
53 |
|
✗ |
if (*str == NULL) |
54 |
|
✗ |
return; |
55 |
|
|
|
56 |
|
✗ |
bolt_str_erase (*str); |
57 |
|
✗ |
g_free (*str); |
58 |
|
✗ |
*str = NULL; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
GStrv |
62 |
|
✗ |
bolt_strv_from_ptr_array (GPtrArray **array) |
63 |
|
|
{ |
64 |
|
|
GPtrArray *a; |
65 |
|
|
|
66 |
|
✗ |
if (array == NULL || *array == NULL) |
67 |
|
✗ |
return NULL; |
68 |
|
|
|
69 |
|
✗ |
a = *array; |
70 |
|
|
|
71 |
|
✗ |
if (a->len == 0 || a->pdata[a->len - 1] != NULL) |
72 |
|
✗ |
g_ptr_array_add (a, NULL); |
73 |
|
|
|
74 |
|
✗ |
*array = NULL; |
75 |
|
✗ |
return (GStrv) g_ptr_array_free (a, FALSE); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
char * |
79 |
|
✗ |
bolt_strdup_validate (const char *string) |
80 |
|
|
{ |
81 |
|
✗ |
g_autofree char *str = NULL; |
82 |
|
|
gboolean ok; |
83 |
|
|
gsize l; |
84 |
|
|
|
85 |
|
✗ |
if (string == NULL) |
86 |
|
✗ |
return NULL; |
87 |
|
|
|
88 |
|
✗ |
str = g_strdup (string); |
89 |
|
✗ |
str = g_strstrip (str); |
90 |
|
|
|
91 |
|
✗ |
l = strlen (str); |
92 |
|
✗ |
if (l == 0) |
93 |
|
✗ |
return NULL; |
94 |
|
|
|
95 |
|
✗ |
ok = g_utf8_validate (str, l, NULL); |
96 |
|
|
|
97 |
|
✗ |
if (!ok) |
98 |
|
✗ |
return NULL; |
99 |
|
|
|
100 |
|
✗ |
return g_steal_pointer (&str); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
char * |
104 |
|
✗ |
bolt_strstrip (char *string) |
105 |
|
|
{ |
106 |
|
|
char *str; |
107 |
|
|
|
108 |
|
✗ |
if (string == NULL) |
109 |
|
✗ |
return NULL; |
110 |
|
|
|
111 |
|
✗ |
str = g_strstrip (string); |
112 |
|
|
|
113 |
|
✗ |
if (strlen (str) == 0) |
114 |
|
✗ |
g_clear_pointer (&str, g_free); |
115 |
|
|
|
116 |
|
✗ |
return str; |
117 |
|
|
} |
118 |
|
|
|