summaryrefslogtreecommitdiff
path: root/strings
diff options
context:
space:
mode:
authorLorry <lorry@roadtrain.codethink.co.uk>2012-08-28 17:19:35 +0100
committerLorry <lorry@roadtrain.codethink.co.uk>2012-08-28 17:19:35 +0100
commitd3c52eb4b4402f0afbebc3f36b9ef8c0994a6c1a (patch)
treeb5ed37757fd9e0f821d103c26692e35ddf2976cb /strings
downloadlibapr-tarball-d3c52eb4b4402f0afbebc3f36b9ef8c0994a6c1a.tar.gz
Tarball conversion
Diffstat (limited to 'strings')
-rw-r--r--strings/apr_cpystrn.c311
-rw-r--r--strings/apr_fnmatch.c482
-rw-r--r--strings/apr_snprintf.c1408
-rw-r--r--strings/apr_strings.c468
-rw-r--r--strings/apr_strnatcmp.c149
-rw-r--r--strings/apr_strtok.c56
6 files changed, 2874 insertions, 0 deletions
diff --git a/strings/apr_cpystrn.c b/strings/apr_cpystrn.c
new file mode 100644
index 0000000..6311c29
--- /dev/null
+++ b/strings/apr_cpystrn.c
@@ -0,0 +1,311 @@
+/* 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
+ *
+ * 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, *end;
+
+ if (dst_size == 0) {
+ return (dst);
+ }
+
+ d = dst;
+ 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);
+ 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
+
diff --git a/strings/apr_fnmatch.c b/strings/apr_fnmatch.c
new file mode 100644
index 0000000..c1c0e4a
--- /dev/null
+++ b/strings/apr_fnmatch.c
@@ -0,0 +1,482 @@
+/* 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.
+ */
+
+
+/* Derived from The Open Group Base Specifications Issue 7, IEEE Std 1003.1-2008
+ * as described in;
+ * http://pubs.opengroup.org/onlinepubs/9699919799/functions/fnmatch.html
+ *
+ * Filename pattern matches defined in section 2.13, "Pattern Matching Notation"
+ * from chapter 2. "Shell Command Language"
+ * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13
+ * where; 1. A bracket expression starting with an unquoted <circumflex> '^'
+ * character CONTINUES to specify a non-matching list; 2. an explicit <period> '.'
+ * in a bracket expression matching list, e.g. "[.abc]" does NOT match a leading
+ * <period> in a filename; 3. a <left-square-bracket> '[' which does not introduce
+ * a valid bracket expression is treated as an ordinary character; 4. a differing
+ * number of consecutive slashes within pattern and string will NOT match;
+ * 5. a trailing '\' in FNM_ESCAPE mode is treated as an ordinary '\' character.
+ *
+ * Bracket expansion defined in section 9.3.5, "RE Bracket Expression",
+ * from chapter 9, "Regular Expressions"
+ * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03_05
+ * with no support for collating symbols, equivalence class expressions or
+ * character class expressions. A partial range expression with a leading
+ * hyphen following a valid range expression will match only the ordinary
+ * <hyphen> and the ending character (e.g. "[a-m-z]" will match characters
+ * 'a' through 'm', a <hyphen> '-', or a 'z').
+ *
+ * NOTE: Only POSIX/C single byte locales are correctly supported at this time.
+ * Notably, non-POSIX locales with FNM_CASEFOLD produce undefined results,
+ * particularly in ranges of mixed case (e.g. "[A-z]") or spanning alpha and
+ * nonalpha characters within a range.
+ *
+ * XXX comments below indicate porting required for multi-byte character sets
+ * and non-POSIX locale collation orders; requires mbr* APIs to track shift
+ * state of pattern and string (rewinding pattern and string repeatedly).
+ *
+ * Certain parts of the code assume 0x00-0x3F are unique with any MBCS (e.g.
+ * UTF-8, SHIFT-JIS, etc). Any implementation allowing '\' as an alternate
+ * path delimiter must be aware that 0x5C is NOT unique within SHIFT-JIS.
+ */
+
+#include "apr_file_info.h"
+#include "apr_fnmatch.h"
+#include "apr_tables.h"
+#include "apr_lib.h"
+#include "apr_strings.h"
+#include <string.h>
+#if APR_HAVE_CTYPE_H
+# include <ctype.h>
+#endif
+
+
+/* Most MBCS/collation/case issues handled here. Wildcard '*' is not handled.
+ * EOS '\0' and the FNM_PATHNAME '/' delimiters are not advanced over,
+ * however the "\/" sequence is advanced to '/'.
+ *
+ * Both pattern and string are **char to support pointer increment of arbitrary
+ * multibyte characters for the given locale, in a later iteration of this code
+ */
+static APR_INLINE int fnmatch_ch(const char **pattern, const char **string, int flags)
+{
+ const char * const mismatch = *pattern;
+ const int nocase = !!(flags & APR_FNM_CASE_BLIND);
+ const int escape = !(flags & APR_FNM_NOESCAPE);
+ const int slash = !!(flags & APR_FNM_PATHNAME);
+ int result = APR_FNM_NOMATCH;
+ const char *startch;
+ int negate;
+
+ if (**pattern == '[')
+ {
+ ++*pattern;
+
+ /* Handle negation, either leading ! or ^ operators (never both) */
+ negate = ((**pattern == '!') || (**pattern == '^'));
+ if (negate)
+ ++*pattern;
+
+ /* ']' is an ordinary character at the start of the range pattern */
+ if (**pattern == ']')
+ goto leadingclosebrace;
+
+ while (**pattern)
+ {
+ if (**pattern == ']') {
+ ++*pattern;
+ /* XXX: Fix for MBCS character width */
+ ++*string;
+ return (result ^ negate);
+ }
+
+ if (escape && (**pattern == '\\')) {
+ ++*pattern;
+
+ /* Patterns must be terminated with ']', not EOS */
+ if (!**pattern)
+ break;
+ }
+
+ /* Patterns must be terminated with ']' not '/' */
+ if (slash && (**pattern == '/'))
+ break;
+
+leadingclosebrace:
+ /* Look at only well-formed range patterns;
+ * "x-]" is not allowed unless escaped ("x-\]")
+ * XXX: Fix for locale/MBCS character width
+ */
+ if (((*pattern)[1] == '-') && ((*pattern)[2] != ']'))
+ {
+ startch = *pattern;
+ *pattern += (escape && ((*pattern)[2] == '\\')) ? 3 : 2;
+
+ /* NOT a properly balanced [expr] pattern, EOS terminated
+ * or ranges containing a slash in FNM_PATHNAME mode pattern
+ * fall out to to the rewind and test '[' literal code path
+ */
+ if (!**pattern || (slash && (**pattern == '/')))
+ break;
+
+ /* XXX: handle locale/MBCS comparison, advance by MBCS char width */
+ if ((**string >= *startch) && (**string <= **pattern))
+ result = 0;
+ else if (nocase && (isupper(**string) || isupper(*startch)
+ || isupper(**pattern))
+ && (tolower(**string) >= tolower(*startch))
+ && (tolower(**string) <= tolower(**pattern)))
+ result = 0;
+
+ ++*pattern;
+ continue;
+ }
+
+ /* XXX: handle locale/MBCS comparison, advance by MBCS char width */
+ if ((**string == **pattern))
+ result = 0;
+ else if (nocase && (isupper(**string) || isupper(**pattern))
+ && (tolower(**string) == tolower(**pattern)))
+ result = 0;
+
+ ++*pattern;
+ }
+
+ /* NOT a properly balanced [expr] pattern; Rewind
+ * and reset result to test '[' literal
+ */
+ *pattern = mismatch;
+ result = APR_FNM_NOMATCH;
+ }
+ else if (**pattern == '?') {
+ /* Optimize '?' match before unescaping **pattern */
+ if (!**string || (slash && (**string == '/')))
+ return APR_FNM_NOMATCH;
+ result = 0;
+ goto fnmatch_ch_success;
+ }
+ else if (escape && (**pattern == '\\') && (*pattern)[1]) {
+ ++*pattern;
+ }
+
+ /* XXX: handle locale/MBCS comparison, advance by the MBCS char width */
+ if (**string == **pattern)
+ result = 0;
+ else if (nocase && (isupper(**string) || isupper(**pattern))
+ && (tolower(**string) == tolower(**pattern)))
+ result = 0;
+
+ /* Refuse to advance over trailing slash or nulls
+ */
+ if (!**string || !**pattern || (slash && ((**string == '/') || (**pattern == '/'))))
+ return result;
+
+fnmatch_ch_success:
+ ++*pattern;
+ ++*string;
+ return result;
+}
+
+
+APR_DECLARE(int) apr_fnmatch(const char *pattern, const char *string, int flags)
+{
+ static const char dummystring[2] = {' ', 0};
+ const int escape = !(flags & APR_FNM_NOESCAPE);
+ const int slash = !!(flags & APR_FNM_PATHNAME);
+ const char *strendseg;
+ const char *dummyptr;
+ const char *matchptr;
+ int wild;
+ /* For '*' wild processing only; surpress 'used before initialization'
+ * warnings with dummy initialization values;
+ */
+ const char *strstartseg = NULL;
+ const char *mismatch = NULL;
+ int matchlen = 0;
+
+ if (*pattern == '*')
+ goto firstsegment;
+
+ while (*pattern && *string)
+ {
+ /* Pre-decode "\/" which has no special significance, and
+ * match balanced slashes, starting a new segment pattern
+ */
+ if (slash && escape && (*pattern == '\\') && (pattern[1] == '/'))
+ ++pattern;
+ if (slash && (*pattern == '/') && (*string == '/')) {
+ ++pattern;
+ ++string;
+ }
+
+firstsegment:
+ /* At the beginning of each segment, validate leading period behavior.
+ */
+ if ((flags & APR_FNM_PERIOD) && (*string == '.'))
+ {
+ if (*pattern == '.')
+ ++pattern;
+ else if (escape && (*pattern == '\\') && (pattern[1] == '.'))
+ pattern += 2;
+ else
+ return APR_FNM_NOMATCH;
+ ++string;
+ }
+
+ /* Determine the end of string segment
+ *
+ * Presumes '/' character is unique, not composite in any MBCS encoding
+ */
+ if (slash) {
+ strendseg = strchr(string, '/');
+ if (!strendseg)
+ strendseg = strchr(string, '\0');
+ }
+ else {
+ strendseg = strchr(string, '\0');
+ }
+
+ /* Allow pattern '*' to be consumed even with no remaining string to match
+ */
+ while (*pattern)
+ {
+ if ((string > strendseg)
+ || ((string == strendseg) && (*pattern != '*')))
+ break;
+
+ if (slash && ((*pattern == '/')
+ || (escape && (*pattern == '\\')
+ && (pattern[1] == '/'))))
+ break;
+
+ /* Reduce groups of '*' and '?' to n '?' matches
+ * followed by one '*' test for simplicity
+ */
+ for (wild = 0; ((*pattern == '*') || (*pattern == '?')); ++pattern)
+ {
+ if (*pattern == '*') {
+ wild = 1;
+ }
+ else if (string < strendseg) { /* && (*pattern == '?') */
+ /* XXX: Advance 1 char for MBCS locale */
+ ++string;
+ }
+ else { /* (string >= strendseg) && (*pattern == '?') */
+ return APR_FNM_NOMATCH;
+ }
+ }
+
+ if (wild)
+ {
+ strstartseg = string;
+ mismatch = pattern;
+
+ /* Count fixed (non '*') char matches remaining in pattern
+ * excluding '/' (or "\/") and '*'
+ */
+ for (matchptr = pattern, matchlen = 0; 1; ++matchlen)
+ {
+ if ((*matchptr == '\0')
+ || (slash && ((*matchptr == '/')
+ || (escape && (*matchptr == '\\')
+ && (matchptr[1] == '/')))))
+ {
+ /* Compare precisely this many trailing string chars,
+ * the resulting match needs no wildcard loop
+ */
+ /* XXX: Adjust for MBCS */
+ if (string + matchlen > strendseg)
+ return APR_FNM_NOMATCH;
+
+ string = strendseg - matchlen;
+ wild = 0;
+ break;
+ }
+
+ if (*matchptr == '*')
+ {
+ /* Ensure at least this many trailing string chars remain
+ * for the first comparison
+ */
+ /* XXX: Adjust for MBCS */
+ if (string + matchlen > strendseg)
+ return APR_FNM_NOMATCH;
+
+ /* Begin first wild comparison at the current position */
+ break;
+ }
+
+ /* Skip forward in pattern by a single character match
+ * Use a dummy fnmatch_ch() test to count one "[range]" escape
+ */
+ /* XXX: Adjust for MBCS */
+ if (escape && (*matchptr == '\\') && matchptr[1]) {
+ matchptr += 2;
+ }
+ else if (*matchptr == '[') {
+ dummyptr = dummystring;
+ fnmatch_ch(&matchptr, &dummyptr, flags);
+ }
+ else {
+ ++matchptr;
+ }
+ }
+ }
+
+ /* Incrementally match string against the pattern
+ */
+ while (*pattern && (string < strendseg))
+ {
+ /* Success; begin a new wild pattern search
+ */
+ if (*pattern == '*')
+ break;
+
+ if (slash && ((*string == '/')
+ || (*pattern == '/')
+ || (escape && (*pattern == '\\')
+ && (pattern[1] == '/'))))
+ break;
+
+ /* Compare ch's (the pattern is advanced over "\/" to the '/',
+ * but slashes will mismatch, and are not consumed)
+ */
+ if (!fnmatch_ch(&pattern, &string, flags))
+ continue;
+
+ /* Failed to match, loop against next char offset of string segment
+ * until not enough string chars remain to match the fixed pattern
+ */
+ if (wild) {
+ /* XXX: Advance 1 char for MBCS locale */
+ string = ++strstartseg;
+ if (string + matchlen > strendseg)
+ return APR_FNM_NOMATCH;
+
+ pattern = mismatch;
+ continue;
+ }
+ else
+ return APR_FNM_NOMATCH;
+ }
+ }
+
+ if (*string && !(slash && (*string == '/')))
+ return APR_FNM_NOMATCH;
+
+ if (*pattern && !(slash && ((*pattern == '/')
+ || (escape && (*pattern == '\\')
+ && (pattern[1] == '/')))))
+ return APR_FNM_NOMATCH;
+ }
+
+ /* Where both pattern and string are at EOS, declare success
+ */
+ if (!*string && !*pattern)
+ return 0;
+
+ /* pattern didn't match to the end of string */
+ return APR_FNM_NOMATCH;
+}
+
+
+/* This function is an Apache addition
+ * return non-zero if pattern has any glob chars in it
+ * @bug Function does not distinguish for FNM_PATHNAME mode, which renders
+ * a false positive for test[/]this (which is not a range, but
+ * seperate test[ and ]this segments and no glob.)
+ * @bug Function does not distinguish for non-FNM_ESCAPE mode.
+ * @bug Function does not parse []] correctly
+ * Solution may be to use fnmatch_ch() to walk the patterns?
+ */
+APR_DECLARE(int) apr_fnmatch_test(const char *pattern)
+{
+ int nesting;
+
+ nesting = 0;
+ while (*pattern) {
+ switch (*pattern) {
+ case '?':
+ case '*':
+ return 1;
+
+ case '\\':
+ if (*++pattern == '\0') {
+ return 0;
+ }
+ break;
+
+ case '[': /* '[' is only a glob if it has a matching ']' */
+ ++nesting;
+ break;
+
+ case ']':
+ if (nesting) {
+ return 1;
+ }
+ break;
+ }
+ ++pattern; }
+ return 0;
+}
+
+
+/* Find all files matching the specified pattern */
+APR_DECLARE(apr_status_t) apr_match_glob(const char *pattern,
+ apr_array_header_t **result,
+ apr_pool_t *p)
+{
+ apr_dir_t *dir;
+ apr_finfo_t finfo;
+ apr_status_t rv;
+ char *path;
+
+ /* XXX So, this is kind of bogus. Basically, I need to strip any leading
+ * directories off the pattern, but there is no portable way to do that.
+ * So, for now we just find the last occurance of '/' and if that doesn't
+ * return anything, then we look for '\'. This means that we could
+ * screw up on unix if the pattern is something like "foo\.*" That '\'
+ * isn't a directory delimiter, it is a part of the filename. To fix this,
+ * we really need apr_filepath_basename, which will be coming as soon as
+ * I get to it. rbb
+ */
+ char *idx = strrchr(pattern, '/');
+
+ if (idx == NULL) {
+ idx = strrchr(pattern, '\\');
+ }
+ if (idx == NULL) {
+ path = ".";
+ }
+ else {
+ path = apr_pstrndup(p, pattern, idx - pattern);
+ pattern = idx + 1;
+ }
+
+ *result = apr_array_make(p, 0, sizeof(char *));
+ rv = apr_dir_open(&dir, path, p);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+
+ while (apr_dir_read(&finfo, APR_FINFO_NAME, dir) == APR_SUCCESS) {
+ if (apr_fnmatch(pattern, finfo.name, 0) == APR_SUCCESS) {
+ *(const char **)apr_array_push(*result) = apr_pstrdup(p, finfo.name);
+ }
+ }
+ apr_dir_close(dir);
+ return APR_SUCCESS;
+}
diff --git a/strings/apr_snprintf.c b/strings/apr_snprintf.c
new file mode 100644
index 0000000..6a689a6
--- /dev/null
+++ b/strings/apr_snprintf.c
@@ -0,0 +1,1408 @@
+/* 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_private.h"
+
+#include "apr_lib.h"
+#include "apr_strings.h"
+#include "apr_network_io.h"
+#include "apr_portable.h"
+#include "apr_errno.h"
+#include <math.h>
+#if APR_HAVE_CTYPE_H
+#include <ctype.h>
+#endif
+#if APR_HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+#if APR_HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+#if APR_HAVE_ARPA_INET_H
+#include <arpa/inet.h>
+#endif
+#if APR_HAVE_LIMITS_H
+#include <limits.h>
+#endif
+#if APR_HAVE_STRING_H
+#include <string.h>
+#endif
+
+typedef enum {
+ NO = 0, YES = 1
+} boolean_e;
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+#ifndef TRUE
+#define TRUE 1
+#endif
+#define NUL '\0'
+
+static const char null_string[] = "(null)";
+#define S_NULL ((char *)null_string)
+#define S_NULL_LEN 6
+
+#define FLOAT_DIGITS 6
+#define EXPONENT_LENGTH 10
+
+/*
+ * NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
+ *
+ * NOTICE: this is a magic number; do not decrease it
+ */
+#define NUM_BUF_SIZE 512
+
+/*
+ * cvt - IEEE floating point formatting routines.
+ * Derived from UNIX V7, Copyright(C) Caldera International Inc.
+ */
+
+/*
+ * apr_ecvt converts to decimal
+ * the number of digits is specified by ndigit
+ * decpt is set to the position of the decimal point
+ * sign is set to 0 for positive, 1 for negative
+ */
+
+#define NDIG 80
+
+/* buf must have at least NDIG bytes */
+static char *apr_cvt(double arg, int ndigits, int *decpt, int *sign,
+ int eflag, char *buf)
+{
+ register int r2;
+ double fi, fj;
+ register char *p, *p1;
+
+ if (ndigits >= NDIG - 1)
+ ndigits = NDIG - 2;
+ r2 = 0;
+ *sign = 0;
+ p = &buf[0];
+ if (arg < 0) {
+ *sign = 1;
+ arg = -arg;
+ }
+ arg = modf(arg, &fi);
+ p1 = &buf[NDIG];
+ /*
+ * Do integer part
+ */
+ if (fi != 0) {
+ p1 = &buf[NDIG];
+ while (p1 > &buf[0] && fi != 0) {
+ fj = modf(fi / 10, &fi);
+ *--p1 = (int) ((fj + .03) * 10) + '0';
+ r2++;
+ }
+ while (p1 < &buf[NDIG])
+ *p++ = *p1++;
+ }
+ else if (arg > 0) {
+ while ((fj = arg * 10) < 1) {
+ arg = fj;
+ r2--;
+ }
+ }
+ p1 = &buf[ndigits];
+ if (eflag == 0)
+ p1 += r2;
+ if (p1 < &buf[0]) {
+ *decpt = -ndigits;
+ buf[0] = '\0';
+ return (buf);
+ }
+ *decpt = r2;
+ while (p <= p1 && p < &buf[NDIG]) {
+ arg *= 10;
+ arg = modf(arg, &fj);
+ *p++ = (int) fj + '0';
+ }
+ if (p1 >= &buf[NDIG]) {
+ buf[NDIG - 1] = '\0';
+ return (buf);
+ }
+ p = p1;
+ *p1 += 5;
+ while (*p1 > '9') {
+ *p1 = '0';
+ if (p1 > buf)
+ ++ * --p1;
+ else {
+ *p1 = '1';
+ (*decpt)++;
+ if (eflag == 0) {
+ if (p > buf)
+ *p = '0';
+ p++;
+ }
+ }
+ }
+ *p = '\0';
+ return (buf);
+}
+
+static char *apr_ecvt(double arg, int ndigits, int *decpt, int *sign, char *buf)
+{
+ return (apr_cvt(arg, ndigits, decpt, sign, 1, buf));
+}
+
+static char *apr_fcvt(double arg, int ndigits, int *decpt, int *sign, char *buf)
+{
+ return (apr_cvt(arg, ndigits, decpt, sign, 0, buf));
+}
+
+/*
+ * apr_gcvt - Floating output conversion to
+ * minimal length string
+ */
+
+static char *apr_gcvt(double number, int ndigit, char *buf, boolean_e altform)
+{
+ int sign, decpt;
+ register char *p1, *p2;
+ register int i;
+ char buf1[NDIG];
+
+ p1 = apr_ecvt(number, ndigit, &decpt, &sign, buf1);
+ p2 = buf;
+ if (sign)
+ *p2++ = '-';
+ for (i = ndigit - 1; i > 0 && p1[i] == '0'; i--)
+ ndigit--;
+ if ((decpt >= 0 && decpt - ndigit > 4)
+ || (decpt < 0 && decpt < -3)) { /* use E-style */
+ decpt--;
+ *p2++ = *p1++;
+ *p2++ = '.';
+ for (i = 1; i < ndigit; i++)
+ *p2++ = *p1++;
+ *p2++ = 'e';
+ if (decpt < 0) {
+ decpt = -decpt;
+ *p2++ = '-';
+ }
+ else
+ *p2++ = '+';
+ if (decpt / 100 > 0)
+ *p2++ = decpt / 100 + '0';
+ if (decpt / 10 > 0)
+ *p2++ = (decpt % 100) / 10 + '0';
+ *p2++ = decpt % 10 + '0';
+ }
+ else {
+ if (decpt <= 0) {
+ if (*p1 != '0')
+ *p2++ = '.';
+ while (decpt < 0) {
+ decpt++;
+ *p2++ = '0';
+ }
+ }
+ for (i = 1; i <= ndigit; i++) {
+ *p2++ = *p1++;
+ if (i == decpt)
+ *p2++ = '.';
+ }
+ if (ndigit < decpt) {
+ while (ndigit++ < decpt)
+ *p2++ = '0';
+ *p2++ = '.';
+ }
+ }
+ if (p2[-1] == '.' && !altform)
+ p2--;
+ *p2 = '\0';
+ return (buf);
+}
+
+/*
+ * The INS_CHAR macro inserts a character in the buffer and writes
+ * the buffer back to disk if necessary
+ * It uses the char pointers sp and bep:
+ * sp points to the next available character in the buffer
+ * bep points to the end-of-buffer+1
+ * While using this macro, note that the nextb pointer is NOT updated.
+ *
+ * NOTE: Evaluation of the c argument should not have any side-effects
+ */
+#define INS_CHAR(c, sp, bep, cc) \
+{ \
+ if (sp) { \
+ if (sp >= bep) { \
+ vbuff->curpos = sp; \
+ if (flush_func(vbuff)) \
+ return -1; \
+ sp = vbuff->curpos; \
+ bep = vbuff->endpos; \
+ } \
+ *sp++ = (c); \
+ } \
+ cc++; \
+}
+
+#define NUM(c) (c - '0')
+
+#define STR_TO_DEC(str, num) \
+ num = NUM(*str++); \
+ while (apr_isdigit(*str)) \
+ { \
+ num *= 10 ; \
+ num += NUM(*str++); \
+ }
+
+/*
+ * This macro does zero padding so that the precision
+ * requirement is satisfied. The padding is done by
+ * adding '0's to the left of the string that is going
+ * to be printed. We don't allow precision to be large
+ * enough that we continue past the start of s.
+ *
+ * NOTE: this makes use of the magic info that s is
+ * always based on num_buf with a size of NUM_BUF_SIZE.
+ */
+#define FIX_PRECISION(adjust, precision, s, s_len) \
+ if (adjust) { \
+ apr_size_t p = (precision + 1 < NUM_BUF_SIZE) \
+ ? precision : NUM_BUF_SIZE - 1; \
+ while (s_len < p) \
+ { \
+ *--s = '0'; \
+ s_len++; \
+ } \
+ }
+
+/*
+ * Macro that does padding. The padding is done by printing
+ * the character ch.
+ */
+#define PAD(width, len, ch) \
+do \
+{ \
+ INS_CHAR(ch, sp, bep, cc); \
+ width--; \
+} \
+while (width > len)
+
+/*
+ * Prefix the character ch to the string str
+ * Increase length
+ * Set the has_prefix flag
+ */
+#define PREFIX(str, length, ch) \
+ *--str = ch; \
+ length++; \
+ has_prefix=YES;
+
+
+/*
+ * Convert num to its decimal format.
+ * Return value:
+ * - a pointer to a string containing the number (no sign)
+ * - len contains the length of the string
+ * - is_negative is set to TRUE or FALSE depending on the sign
+ * of the number (always set to FALSE if is_unsigned is TRUE)
+ *
+ * The caller provides a buffer for the string: that is the buf_end argument
+ * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
+ * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
+ *
+ * Note: we have 2 versions. One is used when we need to use quads
+ * (conv_10_quad), the other when we don't (conv_10). We're assuming the
+ * latter is faster.
+ */
+static char *conv_10(register apr_int32_t num, register int is_unsigned,
+ register int *is_negative, char *buf_end,
+ register apr_size_t *len)
+{
+ register char *p = buf_end;
+ register apr_uint32_t magnitude = num;
+
+ if (is_unsigned) {
+ *is_negative = FALSE;
+ }
+ else {
+ *is_negative = (num < 0);
+
+ /*
+ * On a 2's complement machine, negating the most negative integer
+ * results in a number that cannot be represented as a signed integer.
+ * Here is what we do to obtain the number's magnitude:
+ * a. add 1 to the number
+ * b. negate it (becomes positive)
+ * c. convert it to unsigned
+ * d. add 1
+ */
+ if (*is_negative) {
+ apr_int32_t t = num + 1;
+ magnitude = ((apr_uint32_t) -t) + 1;
+ }
+ }
+
+ /*
+ * We use a do-while loop so that we write at least 1 digit
+ */
+ do {
+ register apr_uint32_t new_magnitude = magnitude / 10;
+
+ *--p = (char) (magnitude - new_magnitude * 10 + '0');
+ magnitude = new_magnitude;
+ }
+ while (magnitude);
+
+ *len = buf_end - p;
+ return (p);
+}
+
+static char *conv_10_quad(apr_int64_t num, register int is_unsigned,
+ register int *is_negative, char *buf_end,
+ register apr_size_t *len)
+{
+ register char *p = buf_end;
+ apr_uint64_t magnitude = num;
+
+ /*
+ * We see if we can use the faster non-quad version by checking the
+ * number against the largest long value it can be. If <=, we
+ * punt to the quicker version.
+ */
+ if ((magnitude <= APR_UINT32_MAX && is_unsigned)
+ || (num <= APR_INT32_MAX && num >= APR_INT32_MIN && !is_unsigned))
+ return(conv_10((apr_int32_t)num, is_unsigned, is_negative, buf_end, len));
+
+ if (is_unsigned) {
+ *is_negative = FALSE;
+ }
+ else {
+ *is_negative = (num < 0);
+
+ /*
+ * On a 2's complement machine, negating the most negative integer
+ * results in a number that cannot be represented as a signed integer.
+ * Here is what we do to obtain the number's magnitude:
+ * a. add 1 to the number
+ * b. negate it (becomes positive)
+ * c. convert it to unsigned
+ * d. add 1
+ */
+ if (*is_negative) {
+ apr_int64_t t = num + 1;
+ magnitude = ((apr_uint64_t) -t) + 1;
+ }
+ }
+
+ /*
+ * We use a do-while loop so that we write at least 1 digit
+ */
+ do {
+ apr_uint64_t new_magnitude = magnitude / 10;
+
+ *--p = (char) (magnitude - new_magnitude * 10 + '0');
+ magnitude = new_magnitude;
+ }
+ while (magnitude);
+
+ *len = buf_end - p;
+ return (p);
+}
+
+static char *conv_in_addr(struct in_addr *ia, char *buf_end, apr_size_t *len)
+{
+ unsigned addr = ntohl(ia->s_addr);
+ char *p = buf_end;
+ int is_negative;
+ apr_size_t sub_len;
+
+ p = conv_10((addr & 0x000000FF) , TRUE, &is_negative, p, &sub_len);
+ *--p = '.';
+ p = conv_10((addr & 0x0000FF00) >> 8, TRUE, &is_negative, p, &sub_len);
+ *--p = '.';
+ p = conv_10((addr & 0x00FF0000) >> 16, TRUE, &is_negative, p, &sub_len);
+ *--p = '.';
+ p = conv_10((addr & 0xFF000000) >> 24, TRUE, &is_negative, p, &sub_len);
+
+ *len = buf_end - p;
+ return (p);
+}
+
+
+/* Must be passed a buffer of size NUM_BUF_SIZE where buf_end points
+ * to 1 byte past the end of the buffer. */
+static char *conv_apr_sockaddr(apr_sockaddr_t *sa, char *buf_end, apr_size_t *len)
+{
+ char *p = buf_end;
+ int is_negative;
+ apr_size_t sub_len;
+ char *ipaddr_str;
+
+ p = conv_10(sa->port, TRUE, &is_negative, p, &sub_len);
+ *--p = ':';
+ ipaddr_str = buf_end - NUM_BUF_SIZE;
+ if (apr_sockaddr_ip_getbuf(ipaddr_str, sa->addr_str_len, sa)) {
+ /* Should only fail if the buffer is too small, which it
+ * should not be; but fail safe anyway: */
+ *--p = '?';
+ *len = buf_end - p;
+ return p;
+ }
+ sub_len = strlen(ipaddr_str);
+#if APR_HAVE_IPV6
+ if (sa->family == APR_INET6 &&
+ !IN6_IS_ADDR_V4MAPPED(&sa->sa.sin6.sin6_addr)) {
+ *(p - 1) = ']';
+ p -= sub_len + 2;
+ *p = '[';
+ memcpy(p + 1, ipaddr_str, sub_len);
+ }
+ else
+#endif
+ {
+ p -= sub_len;
+ memcpy(p, ipaddr_str, sub_len);
+ }
+
+ *len = buf_end - p;
+ return (p);
+}
+
+
+
+#if APR_HAS_THREADS
+static char *conv_os_thread_t(apr_os_thread_t *tid, char *buf_end, apr_size_t *len)
+{
+ union {
+ apr_os_thread_t tid;
+ apr_uint64_t u64;
+ apr_uint32_t u32;
+ } u;
+ int is_negative;
+
+ u.tid = *tid;
+ switch(sizeof(u.tid)) {
+ case sizeof(apr_int32_t):
+ return conv_10(u.u32, TRUE, &is_negative, buf_end, len);
+ case sizeof(apr_int64_t):
+ return conv_10_quad(u.u64, TRUE, &is_negative, buf_end, len);
+ default:
+ /* not implemented; stick 0 in the buffer */
+ return conv_10(0, TRUE, &is_negative, buf_end, len);
+ }
+}
+#endif
+
+
+
+/*
+ * Convert a floating point number to a string formats 'f', 'e' or 'E'.
+ * The result is placed in buf, and len denotes the length of the string
+ * The sign is returned in the is_negative argument (and is not placed
+ * in buf).
+ */
+static char *conv_fp(register char format, register double num,
+ boolean_e add_dp, int precision, int *is_negative,
+ char *buf, apr_size_t *len)
+{
+ register char *s = buf;
+ register char *p;
+ int decimal_point;
+ char buf1[NDIG];
+
+ if (format == 'f')
+ p = apr_fcvt(num, precision, &decimal_point, is_negative, buf1);
+ else /* either e or E format */
+ p = apr_ecvt(num, precision + 1, &decimal_point, is_negative, buf1);
+
+ /*
+ * Check for Infinity and NaN
+ */
+ if (apr_isalpha(*p)) {
+ *len = strlen(p);
+ memcpy(buf, p, *len + 1);
+ *is_negative = FALSE;
+ return (buf);
+ }
+
+ if (format == 'f') {
+ if (decimal_point <= 0) {
+ *s++ = '0';
+ if (precision > 0) {
+ *s++ = '.';
+ while (decimal_point++ < 0)
+ *s++ = '0';
+ }
+ else if (add_dp)
+ *s++ = '.';
+ }
+ else {
+ while (decimal_point-- > 0)
+ *s++ = *p++;
+ if (precision > 0 || add_dp)
+ *s++ = '.';
+ }
+ }
+ else {
+ *s++ = *p++;
+ if (precision > 0 || add_dp)
+ *s++ = '.';
+ }
+
+ /*
+ * copy the rest of p, the NUL is NOT copied
+ */
+ while (*p)
+ *s++ = *p++;
+
+ if (format != 'f') {
+ char temp[EXPONENT_LENGTH]; /* for exponent conversion */
+ apr_size_t t_len;
+ int exponent_is_negative;
+
+ *s++ = format; /* either e or E */
+ decimal_point--;
+ if (decimal_point != 0) {
+ p = conv_10((apr_int32_t) decimal_point, FALSE, &exponent_is_negative,
+ &temp[EXPONENT_LENGTH], &t_len);
+ *s++ = exponent_is_negative ? '-' : '+';
+
+ /*
+ * Make sure the exponent has at least 2 digits
+ */
+ if (t_len == 1)
+ *s++ = '0';
+ while (t_len--)
+ *s++ = *p++;
+ }
+ else {
+ *s++ = '+';
+ *s++ = '0';
+ *s++ = '0';
+ }
+ }
+
+ *len = s - buf;
+ return (buf);
+}
+
+
+/*
+ * Convert num to a base X number where X is a power of 2. nbits determines X.
+ * For example, if nbits is 3, we do base 8 conversion
+ * Return value:
+ * a pointer to a string containing the number
+ *
+ * The caller provides a buffer for the string: that is the buf_end argument
+ * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
+ * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
+ *
+ * As with conv_10, we have a faster version which is used when
+ * the number isn't quad size.
+ */
+static char *conv_p2(register apr_uint32_t num, register int nbits,
+ char format, char *buf_end, register apr_size_t *len)
+{
+ register int mask = (1 << nbits) - 1;
+ register char *p = buf_end;
+ static const char low_digits[] = "0123456789abcdef";
+ static const char upper_digits[] = "0123456789ABCDEF";
+ register const char *digits = (format == 'X') ? upper_digits : low_digits;
+
+ do {
+ *--p = digits[num & mask];
+ num >>= nbits;
+ }
+ while (num);
+
+ *len = buf_end - p;
+ return (p);
+}
+
+static char *conv_p2_quad(apr_uint64_t num, register int nbits,
+ char format, char *buf_end, register apr_size_t *len)
+{
+ register int mask = (1 << nbits) - 1;
+ register char *p = buf_end;
+ static const char low_digits[] = "0123456789abcdef";
+ static const char upper_digits[] = "0123456789ABCDEF";
+ register const char *digits = (format == 'X') ? upper_digits : low_digits;
+
+ if (num <= APR_UINT32_MAX)
+ return(conv_p2((apr_uint32_t)num, nbits, format, buf_end, len));
+
+ do {
+ *--p = digits[num & mask];
+ num >>= nbits;
+ }
+ while (num);
+
+ *len = buf_end - p;
+ return (p);
+}
+
+#if APR_HAS_THREADS
+static char *conv_os_thread_t_hex(apr_os_thread_t *tid, char *buf_end, apr_size_t *len)
+{
+ union {
+ apr_os_thread_t tid;
+ apr_uint64_t u64;
+ apr_uint32_t u32;
+ } u;
+ int is_negative;
+
+ u.tid = *tid;
+ switch(sizeof(u.tid)) {
+ case sizeof(apr_int32_t):
+ return conv_p2(u.u32, 4, 'x', buf_end, len);
+ case sizeof(apr_int64_t):
+ return conv_p2_quad(u.u64, 4, 'x', buf_end, len);
+ default:
+ /* not implemented; stick 0 in the buffer */
+ return conv_10(0, TRUE, &is_negative, buf_end, len);
+ }
+}
+#endif
+
+/*
+ * Do format conversion placing the output in buffer
+ */
+APR_DECLARE(int) apr_vformatter(int (*flush_func)(apr_vformatter_buff_t *),
+ apr_vformatter_buff_t *vbuff, const char *fmt, va_list ap)
+{
+ register char *sp;
+ register char *bep;
+ register int cc = 0;
+ register apr_size_t i;
+
+ register char *s = NULL;
+ char *q;
+ apr_size_t s_len = 0;
+
+ register apr_size_t min_width = 0;
+ apr_size_t precision = 0;
+ enum {
+ LEFT, RIGHT
+ } adjust;
+ char pad_char;
+ char prefix_char;
+
+ double fp_num;
+ apr_int64_t i_quad = 0;
+ apr_uint64_t ui_quad;
+ apr_int32_t i_num = 0;
+ apr_uint32_t ui_num = 0;
+
+ char num_buf[NUM_BUF_SIZE];
+ char char_buf[2]; /* for printing %% and %<unknown> */
+
+ enum var_type_enum {
+ IS_QUAD, IS_LONG, IS_SHORT, IS_INT
+ };
+ enum var_type_enum var_type = IS_INT;
+
+ /*
+ * Flag variables
+ */
+ boolean_e alternate_form;
+ boolean_e print_sign;
+ boolean_e print_blank;
+ boolean_e adjust_precision;
+ boolean_e adjust_width;
+ int is_negative;
+
+ sp = vbuff->curpos;
+ bep = vbuff->endpos;
+
+ while (*fmt) {
+ if (*fmt != '%') {
+ INS_CHAR(*fmt, sp, bep, cc);
+ }
+ else {
+ /*
+ * Default variable settings
+ */
+ boolean_e print_something = YES;
+ adjust = RIGHT;
+ alternate_form = print_sign = print_blank = NO;
+ pad_char = ' ';
+ prefix_char = NUL;
+
+ fmt++;
+
+ /*
+ * Try to avoid checking for flags, width or precision
+ */
+ if (!apr_islower(*fmt)) {
+ /*
+ * Recognize flags: -, #, BLANK, +
+ */
+ for (;; fmt++) {
+ if (*fmt == '-')
+ adjust = LEFT;
+ else if (*fmt == '+')
+ print_sign = YES;
+ else if (*fmt == '#')
+ alternate_form = YES;
+ else if (*fmt == ' ')
+ print_blank = YES;
+ else if (*fmt == '0')
+ pad_char = '0';
+ else
+ break;
+ }
+
+ /*
+ * Check if a width was specified
+ */
+ if (apr_isdigit(*fmt)) {
+ STR_TO_DEC(fmt, min_width);
+ adjust_width = YES;
+ }
+ else if (*fmt == '*') {
+ int v = va_arg(ap, int);
+ fmt++;
+ adjust_width = YES;
+ if (v < 0) {
+ adjust = LEFT;
+ min_width = (apr_size_t)(-v);
+ }
+ else
+ min_width = (apr_size_t)v;
+ }
+ else
+ adjust_width = NO;
+
+ /*
+ * Check if a precision was specified
+ */
+ if (*fmt == '.') {
+ adjust_precision = YES;
+ fmt++;
+ if (apr_isdigit(*fmt)) {
+ STR_TO_DEC(fmt, precision);
+ }
+ else if (*fmt == '*') {
+ int v = va_arg(ap, int);
+ fmt++;
+ precision = (v < 0) ? 0 : (apr_size_t)v;
+ }
+ else
+ precision = 0;
+ }
+ else
+ adjust_precision = NO;
+ }
+ else
+ adjust_precision = adjust_width = NO;
+
+ /*
+ * Modifier check. In same cases, APR_OFF_T_FMT can be
+ * "lld" and APR_INT64_T_FMT can be "ld" (that is, off_t is
+ * "larger" than int64). Check that case 1st.
+ * Note that if APR_OFF_T_FMT is "d",
+ * the first if condition is never true. If APR_INT64_T_FMT
+ * is "d' then the second if condition is never true.
+ */
+ if ((sizeof(APR_OFF_T_FMT) > sizeof(APR_INT64_T_FMT)) &&
+ ((sizeof(APR_OFF_T_FMT) == 4 &&
+ fmt[0] == APR_OFF_T_FMT[0] &&
+ fmt[1] == APR_OFF_T_FMT[1]) ||
+ (sizeof(APR_OFF_T_FMT) == 3 &&
+ fmt[0] == APR_OFF_T_FMT[0]) ||
+ (sizeof(APR_OFF_T_FMT) > 4 &&
+ strncmp(fmt, APR_OFF_T_FMT,
+ sizeof(APR_OFF_T_FMT) - 2) == 0))) {
+ /* Need to account for trailing 'd' and null in sizeof() */
+ var_type = IS_QUAD;
+ fmt += (sizeof(APR_OFF_T_FMT) - 2);
+ }
+ else if ((sizeof(APR_INT64_T_FMT) == 4 &&
+ fmt[0] == APR_INT64_T_FMT[0] &&
+ fmt[1] == APR_INT64_T_FMT[1]) ||
+ (sizeof(APR_INT64_T_FMT) == 3 &&
+ fmt[0] == APR_INT64_T_FMT[0]) ||
+ (sizeof(APR_INT64_T_FMT) > 4 &&
+ strncmp(fmt, APR_INT64_T_FMT,
+ sizeof(APR_INT64_T_FMT) - 2) == 0)) {
+ /* Need to account for trailing 'd' and null in sizeof() */
+ var_type = IS_QUAD;
+ fmt += (sizeof(APR_INT64_T_FMT) - 2);
+ }
+ else if (*fmt == 'q') {
+ var_type = IS_QUAD;
+ fmt++;
+ }
+ else if (*fmt == 'l') {
+ var_type = IS_LONG;
+ fmt++;
+ }
+ else if (*fmt == 'h') {
+ var_type = IS_SHORT;
+ fmt++;
+ }
+ else {
+ var_type = IS_INT;
+ }
+
+ /*
+ * Argument extraction and printing.
+ * First we determine the argument type.
+ * Then, we convert the argument to a string.
+ * On exit from the switch, s points to the string that
+ * must be printed, s_len has the length of the string
+ * The precision requirements, if any, are reflected in s_len.
+ *
+ * NOTE: pad_char may be set to '0' because of the 0 flag.
+ * It is reset to ' ' by non-numeric formats
+ */
+ switch (*fmt) {
+ case 'u':
+ if (var_type == IS_QUAD) {
+ i_quad = va_arg(ap, apr_uint64_t);
+ s = conv_10_quad(i_quad, 1, &is_negative,
+ &num_buf[NUM_BUF_SIZE], &s_len);
+ }
+ else {
+ if (var_type == IS_LONG)
+ i_num = (apr_int32_t) va_arg(ap, apr_uint32_t);
+ else if (var_type == IS_SHORT)
+ i_num = (apr_int32_t) (unsigned short) va_arg(ap, unsigned int);
+ else
+ i_num = (apr_int32_t) va_arg(ap, unsigned int);
+ s = conv_10(i_num, 1, &is_negative,
+ &num_buf[NUM_BUF_SIZE], &s_len);
+ }
+ FIX_PRECISION(adjust_precision, precision, s, s_len);
+ break;
+
+ case 'd':
+ case 'i':
+ if (var_type == IS_QUAD) {
+ i_quad = va_arg(ap, apr_int64_t);
+ s = conv_10_quad(i_quad, 0, &is_negative,
+ &num_buf[NUM_BUF_SIZE], &s_len);
+ }
+ else {
+ if (var_type == IS_LONG)
+ i_num = va_arg(ap, apr_int32_t);
+ else if (var_type == IS_SHORT)
+ i_num = (short) va_arg(ap, int);
+ else
+ i_num = va_arg(ap, int);
+ s = conv_10(i_num, 0, &is_negative,
+ &num_buf[NUM_BUF_SIZE], &s_len);
+ }
+ FIX_PRECISION(adjust_precision, precision, s, s_len);
+
+ if (is_negative)
+ prefix_char = '-';
+ else if (print_sign)
+ prefix_char = '+';
+ else if (print_blank)
+ prefix_char = ' ';
+ break;
+
+
+ case 'o':
+ if (var_type == IS_QUAD) {
+ ui_quad = va_arg(ap, apr_uint64_t);
+ s = conv_p2_quad(ui_quad, 3, *fmt,
+ &num_buf[NUM_BUF_SIZE], &s_len);
+ }
+ else {
+ if (var_type == IS_LONG)
+ ui_num = va_arg(ap, apr_uint32_t);
+ else if (var_type == IS_SHORT)
+ ui_num = (unsigned short) va_arg(ap, unsigned int);
+ else
+ ui_num = va_arg(ap, unsigned int);
+ s = conv_p2(ui_num, 3, *fmt,
+ &num_buf[NUM_BUF_SIZE], &s_len);
+ }
+ FIX_PRECISION(adjust_precision, precision, s, s_len);
+ if (alternate_form && *s != '0') {
+ *--s = '0';
+ s_len++;
+ }
+ break;
+
+
+ case 'x':
+ case 'X':
+ if (var_type == IS_QUAD) {
+ ui_quad = va_arg(ap, apr_uint64_t);
+ s = conv_p2_quad(ui_quad, 4, *fmt,
+ &num_buf[NUM_BUF_SIZE], &s_len);
+ }
+ else {
+ if (var_type == IS_LONG)
+ ui_num = va_arg(ap, apr_uint32_t);
+ else if (var_type == IS_SHORT)
+ ui_num = (unsigned short) va_arg(ap, unsigned int);
+ else
+ ui_num = va_arg(ap, unsigned int);
+ s = conv_p2(ui_num, 4, *fmt,
+ &num_buf[NUM_BUF_SIZE], &s_len);
+ }
+ FIX_PRECISION(adjust_precision, precision, s, s_len);
+ if (alternate_form && ui_num != 0) {
+ *--s = *fmt; /* 'x' or 'X' */
+ *--s = '0';
+ s_len += 2;
+ }
+ break;
+
+
+ case 's':
+ s = va_arg(ap, char *);
+ if (s != NULL) {
+ if (!adjust_precision) {
+ s_len = strlen(s);
+ }
+ else {
+ /* From the C library standard in section 7.9.6.1:
+ * ...if the precision is specified, no more then
+ * that many characters are written. If the
+ * precision is not specified or is greater
+ * than the size of the array, the array shall
+ * contain a null character.
+ *
+ * My reading is is precision is specified and
+ * is less then or equal to the size of the
+ * array, no null character is required. So
+ * we can't do a strlen.
+ *
+ * This figures out the length of the string
+ * up to the precision. Once it's long enough
+ * for the specified precision, we don't care
+ * anymore.
+ *
+ * NOTE: you must do the length comparison
+ * before the check for the null character.
+ * Otherwise, you'll check one beyond the
+ * last valid character.
+ */
+ const char *walk;
+
+ for (walk = s, s_len = 0;
+ (s_len < precision) && (*walk != '\0');
+ ++walk, ++s_len);
+ }
+ }
+ else {
+ s = S_NULL;
+ s_len = S_NULL_LEN;
+ }
+ pad_char = ' ';
+ break;
+
+
+ case 'f':
+ case 'e':
+ case 'E':
+ fp_num = va_arg(ap, double);
+ /*
+ * We use &num_buf[ 1 ], so that we have room for the sign
+ */
+ s = NULL;
+#ifdef HAVE_ISNAN
+ if (isnan(fp_num)) {
+ s = "nan";
+ s_len = 3;
+ }
+#endif
+#ifdef HAVE_ISINF
+ if (!s && isinf(fp_num)) {
+ s = "inf";
+ s_len = 3;
+ }
+#endif
+ if (!s) {
+ s = conv_fp(*fmt, fp_num, alternate_form,
+ (int)((adjust_precision == NO) ? FLOAT_DIGITS : precision),
+ &is_negative, &num_buf[1], &s_len);
+ if (is_negative)
+ prefix_char = '-';
+ else if (print_sign)
+ prefix_char = '+';
+ else if (print_blank)
+ prefix_char = ' ';
+ }
+ break;
+
+
+ case 'g':
+ case 'G':
+ if (adjust_precision == NO)
+ precision = FLOAT_DIGITS;
+ else if (precision == 0)
+ precision = 1;
+ /*
+ * * We use &num_buf[ 1 ], so that we have room for the sign
+ */
+ s = apr_gcvt(va_arg(ap, double), (int) precision, &num_buf[1],
+ alternate_form);
+ if (*s == '-')
+ prefix_char = *s++;
+ else if (print_sign)
+ prefix_char = '+';
+ else if (print_blank)
+ prefix_char = ' ';
+
+ s_len = strlen(s);
+
+ if (alternate_form && (q = strchr(s, '.')) == NULL) {
+ s[s_len++] = '.';
+ s[s_len] = '\0'; /* delimit for following strchr() */
+ }
+ if (*fmt == 'G' && (q = strchr(s, 'e')) != NULL)
+ *q = 'E';
+ break;
+
+
+ case 'c':
+ char_buf[0] = (char) (va_arg(ap, int));
+ s = &char_buf[0];
+ s_len = 1;
+ pad_char = ' ';
+ break;
+
+
+ case '%':
+ char_buf[0] = '%';
+ s = &char_buf[0];
+ s_len = 1;
+ pad_char = ' ';
+ break;
+
+
+ case 'n':
+ if (var_type == IS_QUAD)
+ *(va_arg(ap, apr_int64_t *)) = cc;
+ else if (var_type == IS_LONG)
+ *(va_arg(ap, long *)) = cc;
+ else if (var_type == IS_SHORT)
+ *(va_arg(ap, short *)) = cc;
+ else
+ *(va_arg(ap, int *)) = cc;
+ print_something = NO;
+ break;
+
+ /*
+ * This is where we extend the printf format, with a second
+ * type specifier
+ */
+ case 'p':
+ switch(*++fmt) {
+ /*
+ * If the pointer size is equal to or smaller than the size
+ * of the largest unsigned int, we convert the pointer to a
+ * hex number, otherwise we print "%p" to indicate that we
+ * don't handle "%p".
+ */
+ case 'p':
+#if APR_SIZEOF_VOIDP == 8
+ if (sizeof(void *) <= sizeof(apr_uint64_t)) {
+ ui_quad = (apr_uint64_t) va_arg(ap, void *);
+ s = conv_p2_quad(ui_quad, 4, 'x',
+ &num_buf[NUM_BUF_SIZE], &s_len);
+ }
+#else
+ if (sizeof(void *) <= sizeof(apr_uint32_t)) {
+ ui_num = (apr_uint32_t) va_arg(ap, void *);
+ s = conv_p2(ui_num, 4, 'x',
+ &num_buf[NUM_BUF_SIZE], &s_len);
+ }
+#endif
+ else {
+ s = "%p";
+ s_len = 2;
+ prefix_char = NUL;
+ }
+ pad_char = ' ';
+ break;
+
+ /* print an apr_sockaddr_t as a.b.c.d:port */
+ case 'I':
+ {
+ apr_sockaddr_t *sa;
+
+ sa = va_arg(ap, apr_sockaddr_t *);
+ if (sa != NULL) {
+ s = conv_apr_sockaddr(sa, &num_buf[NUM_BUF_SIZE], &s_len);
+ if (adjust_precision && precision < s_len)
+ s_len = precision;
+ }
+ else {
+ s = S_NULL;
+ s_len = S_NULL_LEN;
+ }
+ pad_char = ' ';
+ }
+ break;
+
+ /* print a struct in_addr as a.b.c.d */
+ case 'A':
+ {
+ struct in_addr *ia;
+
+ ia = va_arg(ap, struct in_addr *);
+ if (ia != NULL) {
+ s = conv_in_addr(ia, &num_buf[NUM_BUF_SIZE], &s_len);
+ if (adjust_precision && precision < s_len)
+ s_len = precision;
+ }
+ else {
+ s = S_NULL;
+ s_len = S_NULL_LEN;
+ }
+ pad_char = ' ';
+ }
+ break;
+
+ /* print the error for an apr_status_t */
+ case 'm':
+ {
+ apr_status_t *mrv;
+
+ mrv = va_arg(ap, apr_status_t *);
+ if (mrv != NULL) {
+ s = apr_strerror(*mrv, num_buf, NUM_BUF_SIZE-1);
+ s_len = strlen(s);
+ }
+ else {
+ s = S_NULL;
+ s_len = S_NULL_LEN;
+ }
+ pad_char = ' ';
+ }
+ break;
+
+ case 'T':
+#if APR_HAS_THREADS
+ {
+ apr_os_thread_t *tid;
+
+ tid = va_arg(ap, apr_os_thread_t *);
+ if (tid != NULL) {
+ s = conv_os_thread_t(tid, &num_buf[NUM_BUF_SIZE], &s_len);
+ if (adjust_precision && precision < s_len)
+ s_len = precision;
+ }
+ else {
+ s = S_NULL;
+ s_len = S_NULL_LEN;
+ }
+ pad_char = ' ';
+ }
+#else
+ char_buf[0] = '0';
+ s = &char_buf[0];
+ s_len = 1;
+ pad_char = ' ';
+#endif
+ break;
+
+ case 't':
+#if APR_HAS_THREADS
+ {
+ apr_os_thread_t *tid;
+
+ tid = va_arg(ap, apr_os_thread_t *);
+ if (tid != NULL) {
+ s = conv_os_thread_t_hex(tid, &num_buf[NUM_BUF_SIZE], &s_len);
+ if (adjust_precision && precision < s_len)
+ s_len = precision;
+ }
+ else {
+ s = S_NULL;
+ s_len = S_NULL_LEN;
+ }
+ pad_char = ' ';
+ }
+#else
+ char_buf[0] = '0';
+ s = &char_buf[0];
+ s_len = 1;
+ pad_char = ' ';
+#endif
+ break;
+
+ case 'B':
+ case 'F':
+ case 'S':
+ {
+ char buf[5];
+ apr_off_t size = 0;
+
+ if (*fmt == 'B') {
+ apr_uint32_t *arg = va_arg(ap, apr_uint32_t *);
+ size = (arg) ? *arg : 0;
+ }
+ else if (*fmt == 'F') {
+ apr_off_t *arg = va_arg(ap, apr_off_t *);
+ size = (arg) ? *arg : 0;
+ }
+ else {
+ apr_size_t *arg = va_arg(ap, apr_size_t *);
+ size = (arg) ? *arg : 0;
+ }
+
+ s = apr_strfsize(size, buf);
+ s_len = strlen(s);
+ pad_char = ' ';
+ }
+ break;
+
+ case NUL:
+ /* if %p ends the string, oh well ignore it */
+ continue;
+
+ default:
+ s = "bogus %p";
+ s_len = 8;
+ prefix_char = NUL;
+ (void)va_arg(ap, void *); /* skip the bogus argument on the stack */
+ break;
+ }
+ break;
+
+ case NUL:
+ /*
+ * The last character of the format string was %.
+ * We ignore it.
+ */
+ continue;
+
+
+ /*
+ * The default case is for unrecognized %'s.
+ * We print %<char> to help the user identify what
+ * option is not understood.
+ * This is also useful in case the user wants to pass
+ * the output of format_converter to another function
+ * that understands some other %<char> (like syslog).
+ * Note that we can't point s inside fmt because the
+ * unknown <char> could be preceded by width etc.
+ */
+ default:
+ char_buf[0] = '%';
+ char_buf[1] = *fmt;
+ s = char_buf;
+ s_len = 2;
+ pad_char = ' ';
+ break;
+ }
+
+ if (prefix_char != NUL && s != S_NULL && s != char_buf) {
+ *--s = prefix_char;
+ s_len++;
+ }
+
+ if (adjust_width && adjust == RIGHT && min_width > s_len) {
+ if (pad_char == '0' && prefix_char != NUL) {
+ INS_CHAR(*s, sp, bep, cc);
+ s++;
+ s_len--;
+ min_width--;
+ }
+ PAD(min_width, s_len, pad_char);
+ }
+
+ /*
+ * Print the string s.
+ */
+ if (print_something == YES) {
+ for (i = s_len; i != 0; i--) {
+ INS_CHAR(*s, sp, bep, cc);
+ s++;
+ }
+ }
+
+ if (adjust_width && adjust == LEFT && min_width > s_len)
+ PAD(min_width, s_len, pad_char);
+ }
+ fmt++;
+ }
+ vbuff->curpos = sp;
+
+ return cc;
+}
+
+
+static int snprintf_flush(apr_vformatter_buff_t *vbuff)
+{
+ /* if the buffer fills we have to abort immediately, there is no way
+ * to "flush" an apr_snprintf... there's nowhere to flush it to.
+ */
+ return -1;
+}
+
+
+APR_DECLARE_NONSTD(int) apr_snprintf(char *buf, apr_size_t len,
+ const char *format, ...)
+{
+ int cc;
+ va_list ap;
+ apr_vformatter_buff_t vbuff;
+
+ if (len == 0) {
+ /* NOTE: This is a special case; we just want to return the number
+ * of chars that would be written (minus \0) if the buffer
+ * size was infinite. We leverage the fact that INS_CHAR
+ * just does actual inserts iff the buffer pointer is non-NULL.
+ * In this case, we don't care what buf is; it can be NULL, since
+ * we don't touch it at all.
+ */
+ vbuff.curpos = NULL;
+ vbuff.endpos = NULL;
+ } else {
+ /* save one byte for nul terminator */
+ vbuff.curpos = buf;
+ vbuff.endpos = buf + len - 1;
+ }
+ va_start(ap, format);
+ cc = apr_vformatter(snprintf_flush, &vbuff, format, ap);
+ va_end(ap);
+ if (len != 0) {
+ *vbuff.curpos = '\0';
+ }
+ return (cc == -1) ? (int)len - 1 : cc;
+}
+
+
+APR_DECLARE(int) apr_vsnprintf(char *buf, apr_size_t len, const char *format,
+ va_list ap)
+{
+ int cc;
+ apr_vformatter_buff_t vbuff;
+
+ if (len == 0) {
+ /* See above note */
+ vbuff.curpos = NULL;
+ vbuff.endpos = NULL;
+ } else {
+ /* save one byte for nul terminator */
+ vbuff.curpos = buf;
+ vbuff.endpos = buf + len - 1;
+ }
+ cc = apr_vformatter(snprintf_flush, &vbuff, format, ap);
+ if (len != 0) {
+ *vbuff.curpos = '\0';
+ }
+ return (cc == -1) ? (int)len - 1 : cc;
+}
diff --git a/strings/apr_strings.c b/strings/apr_strings.c
new file mode 100644
index 0000000..d20004e
--- /dev/null
+++ b/strings/apr_strings.c
@@ -0,0 +1,468 @@
+/* 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.
+ */
+/*
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "apr.h"
+#include "apr_strings.h"
+#include "apr_general.h"
+#include "apr_private.h"
+#include "apr_lib.h"
+#define APR_WANT_STDIO
+#define APR_WANT_STRFUNC
+#include "apr_want.h"
+
+#ifdef HAVE_STDDEF_H
+#include <stddef.h> /* NULL */
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h> /* strtol and strtoll */
+#endif
+
+/** this is used to cache lengths in apr_pstrcat */
+#define MAX_SAVED_LENGTHS 6
+
+APR_DECLARE(char *) apr_pstrdup(apr_pool_t *a, const char *s)
+{
+ char *res;
+ apr_size_t len;
+
+ if (s == NULL) {
+ return NULL;
+ }
+ len = strlen(s) + 1;
+ res = apr_palloc(a, len);
+ memcpy(res, s, len);
+ return res;
+}
+
+APR_DECLARE(char *) apr_pstrndup(apr_pool_t *a, const char *s, apr_size_t n)
+{
+ char *res;
+ const char *end;
+
+ if (s == NULL) {
+ return NULL;
+ }
+ end = memchr(s, '\0', n);
+ if (end != NULL)
+ n = end - s;
+ res = apr_palloc(a, n + 1);
+ memcpy(res, s, n);
+ res[n] = '\0';
+ return res;
+}
+
+APR_DECLARE(char *) apr_pstrmemdup(apr_pool_t *a, const char *s, apr_size_t n)
+{
+ char *res;
+
+ if (s == NULL) {
+ return NULL;
+ }
+ res = apr_palloc(a, n + 1);
+ memcpy(res, s, n);
+ res[n] = '\0';
+ return res;
+}
+
+APR_DECLARE(void *) apr_pmemdup(apr_pool_t *a, const void *m, apr_size_t n)
+{
+ void *res;
+
+ if (m == NULL)
+ return NULL;
+ res = apr_palloc(a, n);
+ memcpy(res, m, n);
+ return res;
+}
+
+APR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *a, ...)
+{
+ char *cp, *argp, *res;
+ apr_size_t saved_lengths[MAX_SAVED_LENGTHS];
+ int nargs = 0;
+
+ /* Pass one --- find length of required string */
+
+ apr_size_t len = 0;
+ va_list adummy;
+
+ va_start(adummy, a);
+
+ while ((cp = va_arg(adummy, char *)) != NULL) {
+ apr_size_t cplen = strlen(cp);
+ if (nargs < MAX_SAVED_LENGTHS) {
+ saved_lengths[nargs++] = cplen;
+ }
+ len += cplen;
+ }
+
+ va_end(adummy);
+
+ /* Allocate the required string */
+
+ res = (char *) apr_palloc(a, len + 1);
+ cp = res;
+
+ /* Pass two --- copy the argument strings into the result space */
+
+ va_start(adummy, a);
+
+ nargs = 0;
+ while ((argp = va_arg(adummy, char *)) != NULL) {
+ if (nargs < MAX_SAVED_LENGTHS) {
+ len = saved_lengths[nargs++];
+ }
+ else {
+ len = strlen(argp);
+ }
+
+ memcpy(cp, argp, len);
+ cp += len;
+ }
+
+ va_end(adummy);
+
+ /* Return the result string */
+
+ *cp = '\0';
+
+ return res;
+}
+
+APR_DECLARE(char *) apr_pstrcatv(apr_pool_t *a, const struct iovec *vec,
+ apr_size_t nvec, apr_size_t *nbytes)
+{
+ apr_size_t i;
+ apr_size_t len;
+ const struct iovec *src;
+ char *res;
+ char *dst;
+
+ /* Pass one --- find length of required string */
+ len = 0;
+ src = vec;
+ for (i = nvec; i; i--) {
+ len += src->iov_len;
+ src++;
+ }
+ if (nbytes) {
+ *nbytes = len;
+ }
+
+ /* Allocate the required string */
+ res = (char *) apr_palloc(a, len + 1);
+
+ /* Pass two --- copy the argument strings into the result space */
+ src = vec;
+ dst = res;
+ for (i = nvec; i; i--) {
+ memcpy(dst, src->iov_base, src->iov_len);
+ dst += src->iov_len;
+ src++;
+ }
+
+ /* Return the result string */
+ *dst = '\0';
+
+ return res;
+}
+
+#if (!APR_HAVE_MEMCHR)
+void *memchr(const void *s, int c, size_t n)
+{
+ const char *cp;
+
+ for (cp = s; n > 0; n--, cp++) {
+ if (*cp == c)
+ return (char *) cp; /* Casting away the const here */
+ }
+
+ return NULL;
+}
+#endif
+
+#ifndef INT64_MAX
+#define INT64_MAX APR_INT64_C(0x7fffffffffffffff)
+#endif
+#ifndef INT64_MIN
+#define INT64_MIN (-APR_INT64_C(0x7fffffffffffffff) - APR_INT64_C(1))
+#endif
+
+APR_DECLARE(apr_status_t) apr_strtoff(apr_off_t *offset, const char *nptr,
+ char **endptr, int base)
+{
+ errno = 0;
+ *offset = APR_OFF_T_STRFN(nptr, endptr, base);
+ return APR_FROM_OS_ERROR(errno);
+}
+
+APR_DECLARE(apr_int64_t) apr_strtoi64(const char *nptr, char **endptr, int base)
+{
+#ifdef APR_INT64_STRFN
+ errno = 0;
+ return APR_INT64_STRFN(nptr, endptr, base);
+#else
+ const char *s;
+ apr_int64_t acc;
+ apr_int64_t val;
+ int neg, any;
+ char c;
+
+ errno = 0;
+ /*
+ * Skip white space and pick up leading +/- sign if any.
+ * If base is 0, allow 0x for hex and 0 for octal, else
+ * assume decimal; if base is already 16, allow 0x.
+ */
+ s = nptr;
+ do {
+ c = *s++;
+ } while (apr_isspace(c));
+ if (c == '-') {
+ neg = 1;
+ c = *s++;
+ } else {
+ neg = 0;
+ if (c == '+')
+ c = *s++;
+ }
+ if ((base == 0 || base == 16) &&
+ c == '0' && (*s == 'x' || *s == 'X')) {
+ c = s[1];
+ s += 2;
+ base = 16;
+ }
+ if (base == 0)
+ base = c == '0' ? 8 : 10;
+ acc = any = 0;
+ if (base < 2 || base > 36) {
+ errno = EINVAL;
+ if (endptr != NULL)
+ *endptr = (char *)(any ? s - 1 : nptr);
+ return acc;
+ }
+
+ /* The classic bsd implementation requires div/mod operators
+ * to compute a cutoff. Benchmarking proves that is very, very
+ * evil to some 32 bit processors. Instead, look for underflow
+ * in both the mult and add/sub operation. Unlike the bsd impl,
+ * we also work strictly in a signed int64 word as we haven't
+ * implemented the unsigned type in win32.
+ *
+ * Set 'any' if any `digits' consumed; make it negative to indicate
+ * overflow.
+ */
+ val = 0;
+ for ( ; ; c = *s++) {
+ if (c >= '0' && c <= '9')
+ c -= '0';
+#if (('Z' - 'A') == 25)
+ else if (c >= 'A' && c <= 'Z')
+ c -= 'A' - 10;
+ else if (c >= 'a' && c <= 'z')
+ c -= 'a' - 10;
+#elif APR_CHARSET_EBCDIC
+ else if (c >= 'A' && c <= 'I')
+ c -= 'A' - 10;
+ else if (c >= 'J' && c <= 'R')
+ c -= 'J' - 19;
+ else if (c >= 'S' && c <= 'Z')
+ c -= 'S' - 28;
+ else if (c >= 'a' && c <= 'i')
+ c -= 'a' - 10;
+ else if (c >= 'j' && c <= 'r')
+ c -= 'j' - 19;
+ else if (c >= 's' && c <= 'z')
+ c -= 'z' - 28;
+#else
+#error "CANNOT COMPILE apr_strtoi64(), only ASCII and EBCDIC supported"
+#endif
+ else
+ break;
+ if (c >= base)
+ break;
+ val *= base;
+ if ( (any < 0) /* already noted an over/under flow - short circuit */
+ || (neg && (val > acc || (val -= c) > acc)) /* underflow */
+ || (!neg && (val < acc || (val += c) < acc))) { /* overflow */
+ any = -1; /* once noted, over/underflows never go away */
+#ifdef APR_STRTOI64_OVERFLOW_IS_BAD_CHAR
+ break;
+#endif
+ } else {
+ acc = val;
+ any = 1;
+ }
+ }
+
+ if (any < 0) {
+ acc = neg ? INT64_MIN : INT64_MAX;
+ errno = ERANGE;
+ } else if (!any) {
+ errno = EINVAL;
+ }
+ if (endptr != NULL)
+ *endptr = (char *)(any ? s - 1 : nptr);
+ return (acc);
+#endif
+}
+
+APR_DECLARE(apr_int64_t) apr_atoi64(const char *buf)
+{
+ return apr_strtoi64(buf, NULL, 10);
+}
+
+APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n)
+{
+ const int BUFFER_SIZE = sizeof(int) * 3 + 2;
+ char *buf = apr_palloc(p, BUFFER_SIZE);
+ char *start = buf + BUFFER_SIZE - 1;
+ int negative;
+ if (n < 0) {
+ negative = 1;
+ n = -n;
+ }
+ else {
+ negative = 0;
+ }
+ *start = 0;
+ do {
+ *--start = '0' + (n % 10);
+ n /= 10;
+ } while (n);
+ if (negative) {
+ *--start = '-';
+ }
+ return start;
+}
+
+APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n)
+{
+ const int BUFFER_SIZE = sizeof(long) * 3 + 2;
+ char *buf = apr_palloc(p, BUFFER_SIZE);
+ char *start = buf + BUFFER_SIZE - 1;
+ int negative;
+ if (n < 0) {
+ negative = 1;
+ n = -n;
+ }
+ else {
+ negative = 0;
+ }
+ *start = 0;
+ do {
+ *--start = (char)('0' + (n % 10));
+ n /= 10;
+ } while (n);
+ if (negative) {
+ *--start = '-';
+ }
+ return start;
+}
+
+APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n)
+{
+ const int BUFFER_SIZE = sizeof(apr_off_t) * 3 + 2;
+ char *buf = apr_palloc(p, BUFFER_SIZE);
+ char *start = buf + BUFFER_SIZE - 1;
+ int negative;
+ if (n < 0) {
+ negative = 1;
+ n = -n;
+ }
+ else {
+ negative = 0;
+ }
+ *start = 0;
+ do {
+ *--start = '0' + (char)(n % 10);
+ n /= 10;
+ } while (n);
+ if (negative) {
+ *--start = '-';
+ }
+ return start;
+}
+
+APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf)
+{
+ const char ord[] = "KMGTPE";
+ const char *o = ord;
+ int remain;
+
+ if (size < 0) {
+ return strcpy(buf, " - ");
+ }
+ if (size < 973) {
+ if (apr_snprintf(buf, 5, "%3d ", (int) size) < 0)
+ return strcpy(buf, "****");
+ return buf;
+ }
+ do {
+ remain = (int)(size & 1023);
+ size >>= 10;
+ if (size >= 973) {
+ ++o;
+ continue;
+ }
+ if (size < 9 || (size == 9 && remain < 973)) {
+ if ((remain = ((remain * 5) + 256) / 512) >= 10)
+ ++size, remain = 0;
+ if (apr_snprintf(buf, 5, "%d.%d%c", (int) size, remain, *o) < 0)
+ return strcpy(buf, "****");
+ return buf;
+ }
+ if (remain >= 512)
+ ++size;
+ if (apr_snprintf(buf, 5, "%3d%c", (int) size, *o) < 0)
+ return strcpy(buf, "****");
+ return buf;
+ } while (1);
+}
+
diff --git a/strings/apr_strnatcmp.c b/strings/apr_strnatcmp.c
new file mode 100644
index 0000000..0e960e8
--- /dev/null
+++ b/strings/apr_strnatcmp.c
@@ -0,0 +1,149 @@
+/* -*- mode: c; c-file-style: "k&r" -*-
+
+ strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
+ Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include <ctype.h>
+#include <string.h>
+#include "apr_strings.h"
+#include "apr_lib.h" /* for apr_is*() */
+
+#if defined(__GNUC__)
+# define UNUSED __attribute__((__unused__))
+#else
+# define UNUSED
+#endif
+
+/* based on "strnatcmp.c,v 1.6 2000/04/20 07:30:11 mbp Exp $" */
+
+static int
+compare_right(char const *a, char const *b)
+{
+ int bias = 0;
+
+ /* The longest run of digits wins. That aside, the greatest
+ value wins, but we can't know that it will until we've scanned
+ both numbers to know that they have the same magnitude, so we
+ remember it in BIAS. */
+ for (;; a++, b++) {
+ if (!apr_isdigit(*a) && !apr_isdigit(*b))
+ break;
+ else if (!apr_isdigit(*a))
+ return -1;
+ else if (!apr_isdigit(*b))
+ return +1;
+ else if (*a < *b) {
+ if (!bias)
+ bias = -1;
+ } else if (*a > *b) {
+ if (!bias)
+ bias = +1;
+ } else if (!*a && !*b)
+ break;
+ }
+
+ return bias;
+}
+
+
+static int
+compare_left(char const *a, char const *b)
+{
+ /* Compare two left-aligned numbers: the first to have a
+ different value wins. */
+ for (;; a++, b++) {
+ if (!apr_isdigit(*a) && !apr_isdigit(*b))
+ break;
+ else if (!apr_isdigit(*a))
+ return -1;
+ else if (!apr_isdigit(*b))
+ return +1;
+ else if (*a < *b)
+ return -1;
+ else if (*a > *b)
+ return +1;
+ }
+
+ return 0;
+}
+
+
+static int strnatcmp0(char const *a, char const *b, int fold_case)
+{
+ int ai, bi;
+ char ca, cb;
+ int fractional, result;
+ ai = bi = 0;
+ while (1) {
+ ca = a[ai]; cb = b[bi];
+
+ /* skip over leading spaces or zeros */
+ while (apr_isspace(ca))
+ ca = a[++ai];
+
+ while (apr_isspace(cb))
+ cb = b[++bi];
+
+ /* process run of digits */
+ if (apr_isdigit(ca) && apr_isdigit(cb)) {
+ fractional = (ca == '0' || cb == '0');
+
+ if (fractional) {
+ if ((result = compare_left(a+ai, b+bi)) != 0)
+ return result;
+ } else {
+ if ((result = compare_right(a+ai, b+bi)) != 0)
+ return result;
+ }
+ }
+
+ if (!ca && !cb) {
+ /* The strings compare the same. Perhaps the caller
+ will want to call strcmp to break the tie. */
+ return 0;
+ }
+
+ if (fold_case) {
+ ca = apr_toupper(ca);
+ cb = apr_toupper(cb);
+ }
+
+ if (ca < cb)
+ return -1;
+ else if (ca > cb)
+ return +1;
+
+ ++ai; ++bi;
+ }
+}
+
+
+
+APR_DECLARE(int) apr_strnatcmp(char const *a, char const *b)
+{
+ return strnatcmp0(a, b, 0);
+}
+
+
+/* Compare, recognizing numeric string and ignoring case. */
+APR_DECLARE(int) apr_strnatcasecmp(char const *a, char const *b)
+{
+ return strnatcmp0(a, b, 1);
+}
diff --git a/strings/apr_strtok.c b/strings/apr_strtok.c
new file mode 100644
index 0000000..517b319
--- /dev/null
+++ b/strings/apr_strtok.c
@@ -0,0 +1,56 @@
+/* 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.
+ */
+
+#ifdef HAVE_STDDEF_H
+#include <stddef.h> /* for NULL */
+#endif
+
+#include "apr.h"
+#include "apr_strings.h"
+
+#define APR_WANT_STRFUNC /* for strchr() */
+#include "apr_want.h"
+
+APR_DECLARE(char *) apr_strtok(char *str, const char *sep, char **last)
+{
+ char *token;
+
+ if (!str) /* subsequent call */
+ str = *last; /* start where we left off */
+
+ /* skip characters in sep (will terminate at '\0') */
+ while (*str && strchr(sep, *str))
+ ++str;
+
+ if (!*str) /* no more tokens */
+ return NULL;
+
+ token = str;
+
+ /* skip valid token characters to terminate token and
+ * prepare for the next call (will terminate at '\0)
+ */
+ *last = token + 1;
+ while (**last && !strchr(sep, **last))
+ ++*last;
+
+ if (**last) {
+ **last = '\0';
+ ++*last;
+ }
+
+ return token;
+}