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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_private.h"
#include "apr_lib.h"
#if APR_HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if APR_HAVE_STRING_H
#include <string.h>
#endif
#if APR_HAVE_CTYPE_H
#include <ctype.h>
#endif
/*
* Apache's "replacement" for the strncpy() function. We roll our
* own to implement these specific changes:
* (1) strncpy() doesn't always null terminate and we want it to.
* (2) strncpy() null fills, which is bogus, esp. when copy 8byte
* strings into 8k blocks.
* (3) Instead of returning the pointer to the beginning of
* the destination string, we return a pointer to the
* terminating '\0' to allow us to "check" for truncation
* (4) If src is NULL, null terminate dst (empty string copy)
*
* apr_cpystrn() follows the same call structure as strncpy().
*/
APR_DECLARE(char *) apr_cpystrn(char *dst, const char *src, apr_size_t dst_size)
{
char *d = dst, *end;
if (dst_size == 0) {
return (dst);
}
if (src) {
end = dst + dst_size - 1;
for (; d < end; ++d, ++src) {
if (!(*d = *src)) {
return (d);
}
}
}
*d = '\0'; /* always null terminate */
return (d);
}
/*
* This function provides a way to parse a generic argument string
* into a standard argv[] form of argument list. It respects the
* usual "whitespace" and quoteing rules. In the future this could
* be expanded to include support for the apr_call_exec command line
* string processing (including converting '+' to ' ' and doing the
* url processing. It does not currently support this function.
*
* token_context: Context from which pool allocations will occur.
* arg_str: Input argument string for conversion to argv[].
* argv_out: Output location. This is a pointer to an array
* of pointers to strings (ie. &(char *argv[]).
* This value will be allocated from the contexts
* pool and filled in with copies of the tokens
* found during parsing of the arg_str.
*/
APR_DECLARE(apr_status_t) apr_tokenize_to_argv(const char *arg_str,
char ***argv_out,
apr_pool_t *token_context)
{
const char *cp;
const char *ct;
char *cleaned, *dirty;
int escaped;
int isquoted, numargs = 0, argnum;
#define SKIP_WHITESPACE(cp) \
for ( ; *cp == ' ' || *cp == '\t'; ) { \
cp++; \
};
#define CHECK_QUOTATION(cp,isquoted) \
isquoted = 0; \
if (*cp == '"') { \
isquoted = 1; \
cp++; \
} \
else if (*cp == '\'') { \
isquoted = 2; \
cp++; \
}
/* DETERMINE_NEXTSTRING:
* At exit, cp will point to one of the following: NULL, SPACE, TAB or QUOTE.
* NULL implies the argument string has been fully traversed.
*/
#define DETERMINE_NEXTSTRING(cp,isquoted) \
for ( ; *cp != '\0'; cp++) { \
if ( (*cp == '\\' && (*(cp+1) == ' ' || *(cp+1) == '\t' || \
*(cp+1) == '"' || *(cp+1) == '\''))) { \
cp++; \
continue; \
} \
if ( (!isquoted && (*cp == ' ' || *cp == '\t')) \
|| (isquoted == 1 && *cp == '"') \
|| (isquoted == 2 && *cp == '\'') ) { \
break; \
} \
}
/* REMOVE_ESCAPE_CHARS:
* Compresses the arg string to remove all of the '\' escape chars.
* The final argv strings should not have any extra escape chars in it.
*/
#define REMOVE_ESCAPE_CHARS(cleaned, dirty, escaped) \
escaped = 0; \
while(*dirty) { \
if (!escaped && *dirty == '\\') { \
escaped = 1; \
} \
else { \
escaped = 0; \
*cleaned++ = *dirty; \
} \
++dirty; \
} \
*cleaned = 0; /* last line of macro... */
cp = arg_str;
SKIP_WHITESPACE(cp);
ct = cp;
/* This is ugly and expensive, but if anyone wants to figure a
* way to support any number of args without counting and
* allocating, please go ahead and change the code.
*
* Must account for the trailing NULL arg.
*/
numargs = 1;
while (*ct != '\0') {
CHECK_QUOTATION(ct, isquoted);
DETERMINE_NEXTSTRING(ct, isquoted);
if (*ct != '\0') {
ct++;
}
numargs++;
SKIP_WHITESPACE(ct);
}
*argv_out = apr_palloc(token_context, numargs * sizeof(char*));
/* determine first argument */
for (argnum = 0; argnum < (numargs-1); argnum++) {
SKIP_WHITESPACE(cp);
CHECK_QUOTATION(cp, isquoted);
ct = cp;
DETERMINE_NEXTSTRING(cp, isquoted);
cp++;
(*argv_out)[argnum] = apr_palloc(token_context, cp - ct);
apr_cpystrn((*argv_out)[argnum], ct, cp - ct);
cleaned = dirty = (*argv_out)[argnum];
REMOVE_ESCAPE_CHARS(cleaned, dirty, escaped);
}
(*argv_out)[argnum] = NULL;
return APR_SUCCESS;
}
/* Filepath_name_get returns the final element of the pathname.
* Using the current platform's filename syntax.
* "/foo/bar/gum" -> "gum"
* "/foo/bar/gum/" -> ""
* "gum" -> "gum"
* "wi\\n32\\stuff" -> "stuff
*
* Corrected Win32 to accept "a/b\\stuff", "a:stuff"
*/
APR_DECLARE(const char *) apr_filepath_name_get(const char *pathname)
{
const char path_separator = '/';
const char *s = strrchr(pathname, path_separator);
#ifdef WIN32
const char path_separator_win = '\\';
const char drive_separator_win = ':';
const char *s2 = strrchr(pathname, path_separator_win);
if (s2 > s) s = s2;
if (!s) s = strrchr(pathname, drive_separator_win);
#endif
return s ? ++s : pathname;
}
/* length of dest assumed >= length of src
* collapse in place (src == dest) is legal.
* returns terminating null ptr to dest string.
*/
APR_DECLARE(char *) apr_collapse_spaces(char *dest, const char *src)
{
while (*src) {
if (!apr_isspace(*src))
*dest++ = *src;
++src;
}
*dest = 0;
return (dest);
}
#if !APR_HAVE_STRDUP
char *strdup(const char *str)
{
char *sdup;
size_t len = strlen(str) + 1;
sdup = (char *) malloc(len);
if (sdup == NULL)
return NULL;
memcpy(sdup, str, len);
return sdup;
}
#endif
/* The following two routines were donated for SVR4 by Andreas Vogel */
#if (!APR_HAVE_STRCASECMP && !APR_HAVE_STRICMP)
int strcasecmp(const char *a, const char *b)
{
const char *p = a;
const char *q = b;
for (p = a, q = b; *p && *q; p++, q++) {
int diff = apr_tolower(*p) - apr_tolower(*q);
if (diff)
return diff;
}
if (*p)
return 1; /* p was longer than q */
if (*q)
return -1; /* p was shorter than q */
return 0; /* Exact match */
}
#endif
#if (!APR_HAVE_STRNCASECMP && !APR_HAVE_STRNICMP)
int strncasecmp(const char *a, const char *b, size_t n)
{
const char *p = a;
const char *q = b;
for (p = a, q = b; /*NOTHING */ ; p++, q++) {
int diff;
if (p == a + n)
return 0; /* Match up to n characters */
if (!(*p && *q))
return *p - *q;
diff = apr_tolower(*p) - apr_tolower(*q);
if (diff)
return diff;
}
/*NOTREACHED */
}
#endif
/* The following routine was donated for UTS21 by dwd@bell-labs.com */
#if (!APR_HAVE_STRSTR)
char *strstr(char *s1, char *s2)
{
char *p1, *p2;
if (*s2 == '\0') {
/* an empty s2 */
return(s1);
}
while((s1 = strchr(s1, *s2)) != NULL) {
/* found first character of s2, see if the rest matches */
p1 = s1;
p2 = s2;
while (*++p1 == *++p2) {
if (*p1 == '\0') {
/* both strings ended together */
return(s1);
}
}
if (*p2 == '\0') {
/* second string ended, a match */
break;
}
/* didn't find a match here, try starting at next character in s1 */
s1++;
}
return(s1);
}
#endif
|