File: string-utils.c

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (296 lines) | stat: -rw-r--r-- 8,543 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * Copyright (C) 2016-2017 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "string-utils.h"

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "utils.h"

bool sc_streq(const char *a, const char *b) {
    if (!a || !b) {
        return false;
    }

    return strcmp(a, b) == 0;
}

bool sc_endswith(const char *str, const char *suffix) {
    if (!str || !suffix) {
        return false;
    }

    size_t xlen = strlen(suffix);
    size_t slen = strlen(str);

    if (slen < xlen) {
        return false;
    }

    return strncmp(str - xlen + slen, suffix, xlen) == 0;
}

bool sc_startswith(const char *str, const char *prefix) {
    if (!str || !prefix) {
        return false;
    }

    size_t xlen = strlen(prefix);
    return strncmp(str, prefix, xlen) == 0;
}

char *sc_strdup(const char *str) {
    // Set errno in case we die.
    errno = 0;
    size_t len;
    char *copy;
    if (str == NULL) {
        die("cannot duplicate NULL string");
    }
    len = strlen(str);
    copy = malloc(len + 1);
    if (copy == NULL) {
        die("cannot allocate string copy (len: %zd)", len);
    }
    memcpy(copy, str, len + 1);
    return copy;
}

int sc_must_snprintf(char *str, size_t size, const char *format, ...) {
    // Set errno in case we die.
    errno = 0;
    int n;

    va_list va;
    va_start(va, format);
    n = vsnprintf(str, size, format, va);
    va_end(va);

    if (n < 0 || (size_t)n >= size) die("cannot format string: %s", str);

    return n;
}

size_t sc_string_append(char *dst, size_t dst_size, const char *str) {
    // Set errno in case we die.
    errno = 0;
    if (dst == NULL) {
        die("cannot append string: buffer is NULL");
    }
    if (str == NULL) {
        die("cannot append string: string is NULL");
    }
    size_t dst_len = strnlen(dst, dst_size);
    if (dst_len == dst_size) {
        die("cannot append string: dst is unterminated");
    }

    size_t max_str_len = dst_size - dst_len;
    size_t str_len = strnlen(str, max_str_len);
    if (str_len == max_str_len) {
        die("cannot append string: str is too long or unterminated");
    }
    // Append the string
    memcpy(dst + dst_len, str, str_len);
    // Ensure we are terminated
    dst[dst_len + str_len] = '\0';
    // return the new size
    return strlen(dst);
}

size_t sc_string_append_char(char *dst, size_t dst_size, char c) {
    // Set errno in case we die.
    errno = 0;
    if (dst == NULL) {
        die("cannot append character: buffer is NULL");
    }
    size_t dst_len = strnlen(dst, dst_size);
    if (dst_len == dst_size) {
        die("cannot append character: dst is unterminated");
    }
    size_t max_str_len = dst_size - dst_len;
    if (max_str_len < 2) {
        die("cannot append character: not enough space");
    }
    if (c == 0) {
        die("cannot append character: cannot append string terminator");
    }
    // Append the character and terminate the string.
    dst[dst_len + 0] = c;
    dst[dst_len + 1] = '\0';
    // Return the new size
    return dst_len + 1;
}

size_t sc_string_append_char_pair(char *dst, size_t dst_size, char c1, char c2) {
    // Set errno in case we die.
    errno = 0;
    if (dst == NULL) {
        die("cannot append character pair: buffer is NULL");
    }
    size_t dst_len = strnlen(dst, dst_size);
    if (dst_len == dst_size) {
        die("cannot append character pair: dst is unterminated");
    }
    size_t max_str_len = dst_size - dst_len;
    if (max_str_len < 3) {
        die("cannot append character pair: not enough space");
    }
    if (c1 == 0 || c2 == 0) {
        die("cannot append character pair: cannot append string terminator");
    }
    // Append the two characters and terminate the string.
    dst[dst_len + 0] = c1;
    dst[dst_len + 1] = c2;
    dst[dst_len + 2] = '\0';
    // Return the new size
    return dst_len + 2;
}

void sc_string_init(char *buf, size_t buf_size) {
    errno = 0;
    if (buf == NULL) {
        die("cannot initialize string, buffer is NULL");
    }
    if (buf_size == 0) {
        die("cannot initialize string, buffer is too small");
    }
    buf[0] = '\0';
}

void sc_string_quote(char *buf, size_t buf_size, const char *str) {
    // Set errno in case we die.
    errno = 0;
    if (str == NULL) {
        die("cannot quote string: string is NULL");
    }
    const char *hex = "0123456789abcdef";
    // NOTE: this also checks buf/buf_size sanity so that we don't have to.
    sc_string_init(buf, buf_size);
    sc_string_append_char(buf, buf_size, '"');
    for (unsigned char c; (c = *str) != 0; ++str) {
        switch (c) {
                // Pass ASCII letters and digits unmodified.
            case '0' ... '9':
            case 'A' ... 'Z':
            case 'a' ... 'z':
                // Pass most of the punctuation unmodified.
            case ' ':
            case '!':
            case '#':
            case '$':
            case '%':
            case '&':
            case '(':
            case ')':
            case '*':
            case '+':
            case ',':
            case '-':
            case '.':
            case '/':
            case ':':
            case ';':
            case '<':
            case '=':
            case '>':
            case '?':
            case '@':
            case '[':
            case '\'':
            case ']':
            case '^':
            case '_':
            case '`':
            case '{':
            case '|':
            case '}':
            case '~':
                sc_string_append_char(buf, buf_size, c);
                break;
                // Escape special whitespace characters.
            case '\n':
                sc_string_append_char_pair(buf, buf_size, '\\', 'n');
                break;
            case '\r':
                sc_string_append_char_pair(buf, buf_size, '\\', 'r');
                break;
            case '\t':
                sc_string_append_char_pair(buf, buf_size, '\\', 't');
                break;
            case '\v':
                sc_string_append_char_pair(buf, buf_size, '\\', 'v');
                break;
                // Escape the escape character.
            case '\\':
                sc_string_append_char_pair(buf, buf_size, '\\', '\\');
                break;
                // Escape double quote character.
            case '"':
                sc_string_append_char_pair(buf, buf_size, '\\', '"');
                break;
                // Escape everything else as a generic hexadecimal escape string.
            default:
                sc_string_append_char_pair(buf, buf_size, '\\', 'x');
                sc_string_append_char_pair(buf, buf_size, hex[c >> 4], hex[c & 15]);
                break;
        }
    }
    sc_string_append_char(buf, buf_size, '"');
}

void sc_string_split(const char *string, char delimiter, char *prefix_buf, size_t prefix_size, char *suffix_buf,
                     size_t suffix_size) {
    if (string == NULL) {
        die("internal error: cannot split string when it is unset");
    }
    if (prefix_buf == NULL && suffix_buf == NULL) {
        die("internal error: cannot split string when both prefix and suffix are unset");
    }

    const char *pos = strchr(string, delimiter);
    const char *suffix_start = "";
    size_t prefix_len = 0;
    size_t suffix_len = 0;
    if (pos == NULL) {
        prefix_len = strlen(string);
    } else {
        prefix_len = pos - string;
        suffix_start = pos + 1;
        suffix_len = strlen(suffix_start);
    }

    if (prefix_buf != NULL) {
        if (prefix_len >= prefix_size) {
            die("prefix buffer too small");
        }

        memcpy(prefix_buf, string, prefix_len);
        prefix_buf[prefix_len] = '\0';
    }

    if (suffix_buf != NULL) {
        if (suffix_len >= suffix_size) {
            die("suffix buffer too small");
        }
        memcpy(suffix_buf, suffix_start, suffix_len);
        suffix_buf[suffix_len] = '\0';
    }
}