Coverage Report

Created: 2023-03-26 06:28

/src/httpd/srclib/apr/strings/apr_cpystrn.c
Line
Count
Source (jump to first uncovered line)
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
 * contributor license agreements.  See the NOTICE file distributed with
3
 * this work for additional information regarding copyright ownership.
4
 * The ASF licenses this file to You under the Apache License, Version 2.0
5
 * (the "License"); you may not use this file except in compliance with
6
 * the License.  You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include "apr.h"
18
#include "apr_strings.h"
19
#include "apr_private.h"
20
#include "apr_lib.h"
21
22
#if APR_HAVE_SYS_TYPES_H
23
#include <sys/types.h>
24
#endif
25
#if APR_HAVE_STRING_H
26
#include <string.h>
27
#endif
28
#if APR_HAVE_CTYPE_H
29
#include <ctype.h>
30
#endif
31
32
/*
33
 * Apache's "replacement" for the strncpy() function. We roll our
34
 * own to implement these specific changes:
35
 *   (1) strncpy() doesn't always null terminate and we want it to.
36
 *   (2) strncpy() null fills, which is bogus, esp. when copy 8byte
37
 *       strings into 8k blocks.
38
 *   (3) Instead of returning the pointer to the beginning of
39
 *       the destination string, we return a pointer to the
40
 *       terminating '\0' to allow us to "check" for truncation
41
 *   (4) If src is NULL, null terminate dst (empty string copy)
42
 *
43
 * apr_cpystrn() follows the same call structure as strncpy().
44
 */
45
46
APR_DECLARE(char *) apr_cpystrn(char *dst, const char *src, apr_size_t dst_size)
47
1.09M
{
48
49
1.09M
    char *d = dst, *end;
50
51
1.09M
    if (dst_size == 0) {
52
0
        return (dst);
53
0
    }
54
55
1.09M
    if (src) {
56
1.09M
        end = dst + dst_size - 1;
57
58
3.19M
        for (; d < end; ++d, ++src) {
59
2.10M
            if (!(*d = *src)) {
60
0
                return (d);
61
0
            }
62
2.10M
        }
63
1.09M
    }
64
65
1.09M
    *d = '\0';  /* always null terminate */
66
67
1.09M
    return (d);
68
1.09M
}
69
70
71
/*
72
 * This function provides a way to parse a generic argument string
73
 * into a standard argv[] form of argument list. It respects the
74
 * usual "whitespace" and quoteing rules. In the future this could
75
 * be expanded to include support for the apr_call_exec command line
76
 * string processing (including converting '+' to ' ' and doing the
77
 * url processing. It does not currently support this function.
78
 *
79
 *    token_context: Context from which pool allocations will occur.
80
 *    arg_str:       Input argument string for conversion to argv[].
81
 *    argv_out:      Output location. This is a pointer to an array
82
 *                   of pointers to strings (ie. &(char *argv[]).
83
 *                   This value will be allocated from the contexts
84
 *                   pool and filled in with copies of the tokens
85
 *                   found during parsing of the arg_str.
86
 */
87
APR_DECLARE(apr_status_t) apr_tokenize_to_argv(const char *arg_str,
88
                                            char ***argv_out,
89
                                            apr_pool_t *token_context)
90
287
{
91
287
    const char *cp;
92
287
    const char *ct;
93
287
    char *cleaned, *dirty;
94
287
    int escaped;
95
287
    int isquoted, numargs = 0, argnum;
96
97
287
#define SKIP_WHITESPACE(cp) \
98
2.18M
    for ( ; *cp == ' ' || *cp == '\t'; ) { \
99
1.40k
        cp++; \
100
1.40k
    };
101
102
287
#define CHECK_QUOTATION(cp,isquoted) \
103
2.18M
    isquoted = 0; \
104
2.18M
    if (*cp == '"') { \
105
6.87k
        isquoted = 1; \
106
6.87k
        cp++; \
107
6.87k
    } \
108
2.18M
    else if (*cp == '\'') { \
109
2.17M
        isquoted = 2; \
110
2.17M
        cp++; \
111
2.17M
    }
112
113
/* DETERMINE_NEXTSTRING:
114
 * At exit, cp will point to one of the following:  NULL, SPACE, TAB or QUOTE.
115
 * NULL implies the argument string has been fully traversed.
116
 */
117
287
#define DETERMINE_NEXTSTRING(cp,isquoted) \
118
6.38M
    for ( ; *cp != '\0'; cp++) { \
119
6.38M
        if (   (*cp == '\\' && (*(cp+1) == ' ' || *(cp+1) == '\t' || \
120
4.48k
                                *(cp+1) == '"' || *(cp+1) == '\''))) { \
121
1.76k
            cp++; \
122
1.76k
            continue; \
123
1.76k
        } \
124
6.38M
        if (   (!isquoted && (*cp == ' ' || *cp == '\t')) \
125
6.37M
            || (isquoted == 1 && *cp == '"') \
126
6.37M
            || (isquoted == 2 && *cp == '\'')                 ) { \
127
2.18M
            break; \
128
2.18M
        } \
129
6.37M
    }
130
131
/* REMOVE_ESCAPE_CHARS:
132
 * Compresses the arg string to remove all of the '\' escape chars.
133
 * The final argv strings should not have any extra escape chars in it.
134
 */
135
287
#define REMOVE_ESCAPE_CHARS(cleaned, dirty, escaped) \
136
1.09M
    escaped = 0; \
137
3.19M
    while(*dirty) { \
138
2.10M
        if (!escaped && *dirty == '\\') { \
139
1.60k
            escaped = 1; \
140
1.60k
        } \
141
2.10M
        else { \
142
2.09M
            escaped = 0; \
143
2.09M
            *cleaned++ = *dirty; \
144
2.09M
        } \
145
2.10M
        ++dirty; \
146
2.10M
    } \
147
1.09M
    *cleaned = 0;        /* last line of macro... */
148
149
287
    cp = arg_str;
150
287
    SKIP_WHITESPACE(cp);
151
287
    ct = cp;
152
153
    /* This is ugly and expensive, but if anyone wants to figure a
154
     * way to support any number of args without counting and
155
     * allocating, please go ahead and change the code.
156
     *
157
     * Must account for the trailing NULL arg.
158
     */
159
287
    numargs = 1;
160
1.09M
    while (*ct != '\0') {
161
1.09M
        CHECK_QUOTATION(ct, isquoted);
162
1.09M
        DETERMINE_NEXTSTRING(ct, isquoted);
163
1.09M
        if (*ct != '\0') {
164
1.09M
            ct++;
165
1.09M
        }
166
1.09M
        numargs++;
167
1.09M
        SKIP_WHITESPACE(ct);
168
1.09M
    }
169
287
    *argv_out = apr_palloc(token_context, numargs * sizeof(char*));
170
171
    /*  determine first argument */
172
1.09M
    for (argnum = 0; argnum < (numargs-1); argnum++) {
173
1.09M
        SKIP_WHITESPACE(cp);
174
1.09M
        CHECK_QUOTATION(cp, isquoted);
175
1.09M
        ct = cp;
176
1.09M
        DETERMINE_NEXTSTRING(cp, isquoted);
177
1.09M
        cp++;
178
1.09M
        (*argv_out)[argnum] = apr_palloc(token_context, cp - ct);
179
1.09M
        apr_cpystrn((*argv_out)[argnum], ct, cp - ct);
180
1.09M
        cleaned = dirty = (*argv_out)[argnum];
181
1.09M
        REMOVE_ESCAPE_CHARS(cleaned, dirty, escaped);
182
1.09M
    }
183
287
    (*argv_out)[argnum] = NULL;
184
185
287
    return APR_SUCCESS;
186
287
}
187
188
/* Filepath_name_get returns the final element of the pathname.
189
 * Using the current platform's filename syntax.
190
 *   "/foo/bar/gum" -> "gum"
191
 *   "/foo/bar/gum/" -> ""
192
 *   "gum" -> "gum"
193
 *   "wi\\n32\\stuff" -> "stuff
194
 *
195
 * Corrected Win32 to accept "a/b\\stuff", "a:stuff"
196
 */
197
198
APR_DECLARE(const char *) apr_filepath_name_get(const char *pathname)
199
0
{
200
0
    const char path_separator = '/';
201
0
    const char *s = strrchr(pathname, path_separator);
202
203
#ifdef WIN32
204
    const char path_separator_win = '\\';
205
    const char drive_separator_win = ':';
206
    const char *s2 = strrchr(pathname, path_separator_win);
207
208
    if (s2 > s) s = s2;
209
210
    if (!s) s = strrchr(pathname, drive_separator_win);
211
#endif
212
213
0
    return s ? ++s : pathname;
214
0
}
215
216
/* length of dest assumed >= length of src
217
 * collapse in place (src == dest) is legal.
218
 * returns terminating null ptr to dest string.
219
 */
220
APR_DECLARE(char *) apr_collapse_spaces(char *dest, const char *src)
221
0
{
222
0
    while (*src) {
223
0
        if (!apr_isspace(*src))
224
0
            *dest++ = *src;
225
0
        ++src;
226
0
    }
227
0
    *dest = 0;
228
0
    return (dest);
229
0
}
230
231
#if !APR_HAVE_STRDUP
232
char *strdup(const char *str)
233
{
234
    char *sdup;
235
    size_t len = strlen(str) + 1;
236
237
    sdup = (char *) malloc(len);
238
    if (sdup == NULL)
239
        return NULL;
240
    memcpy(sdup, str, len);
241
242
    return sdup;
243
}
244
#endif
245
246
/* The following two routines were donated for SVR4 by Andreas Vogel */
247
#if (!APR_HAVE_STRCASECMP && !APR_HAVE_STRICMP)
248
int strcasecmp(const char *a, const char *b)
249
{
250
    const char *p = a;
251
    const char *q = b;
252
    for (p = a, q = b; *p && *q; p++, q++) {
253
        int diff = apr_tolower(*p) - apr_tolower(*q);
254
        if (diff)
255
            return diff;
256
    }
257
    if (*p)
258
        return 1;               /* p was longer than q */
259
    if (*q)
260
        return -1;              /* p was shorter than q */
261
    return 0;                   /* Exact match */
262
}
263
264
#endif
265
266
#if (!APR_HAVE_STRNCASECMP && !APR_HAVE_STRNICMP)
267
int strncasecmp(const char *a, const char *b, size_t n)
268
{
269
    const char *p = a;
270
    const char *q = b;
271
272
    for (p = a, q = b; /*NOTHING */ ; p++, q++) {
273
        int diff;
274
        if (p == a + n)
275
            return 0;           /*   Match up to n characters */
276
        if (!(*p && *q))
277
            return *p - *q;
278
        diff = apr_tolower(*p) - apr_tolower(*q);
279
        if (diff)
280
            return diff;
281
    }
282
    /*NOTREACHED */
283
}
284
#endif
285
286
/* The following routine was donated for UTS21 by dwd@bell-labs.com */
287
#if (!APR_HAVE_STRSTR)
288
char *strstr(char *s1, char *s2)
289
{
290
    char *p1, *p2;
291
    if (*s2 == '\0') {
292
        /* an empty s2 */
293
        return(s1);
294
    }
295
    while((s1 = strchr(s1, *s2)) != NULL) {
296
        /* found first character of s2, see if the rest matches */
297
        p1 = s1;
298
        p2 = s2;
299
        while (*++p1 == *++p2) {
300
            if (*p1 == '\0') {
301
                /* both strings ended together */
302
                return(s1);
303
            }
304
        }
305
        if (*p2 == '\0') {
306
            /* second string ended, a match */
307
            break;
308
        }
309
        /* didn't find a match here, try starting at next character in s1 */
310
        s1++;
311
    }
312
    return(s1);
313
}
314
#endif
315