2012-08-06 02:55:45 +00:00
|
|
|
/*
|
|
|
|
Serval string primitives
|
|
|
|
Copyright (C) 2012 Serval Project Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
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, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __STR_H__
|
|
|
|
#define __STR_H__
|
|
|
|
|
2012-11-07 06:12:45 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#ifndef __STR_INLINE
|
|
|
|
# if __GNUC__ && !__GNUC_STDC_INLINE__
|
|
|
|
# define __STR_INLINE extern inline
|
|
|
|
# else
|
|
|
|
# define __STR_INLINE inline
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Return true iff 'len' bytes starting at 'text' are hex digits, upper or lower case.
|
|
|
|
* Does not check the following byte.
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
__STR_INLINE int is_xsubstring(const char *text, int len)
|
|
|
|
{
|
|
|
|
while (len--)
|
|
|
|
if (!isxdigit(*text++))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return true iff the nul-terminated string 'text' has length 'len' and consists only of hex
|
|
|
|
* digits, upper or lower case.
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
__STR_INLINE int is_xstring(const char *text, int len)
|
|
|
|
{
|
|
|
|
while (len--)
|
|
|
|
if (!isxdigit(*text++))
|
|
|
|
return 0;
|
|
|
|
return *text == '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
extern char hexdigit[16];
|
|
|
|
char *tohex(char *dstHex, const unsigned char *srcBinary, size_t bytes);
|
|
|
|
size_t fromhex(unsigned char *dstBinary, const char *srcHex, size_t nbinary);
|
|
|
|
int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary);
|
|
|
|
int is_all_matching(const unsigned char *ptr, size_t len, unsigned char value);
|
|
|
|
char *str_toupper_inplace(char *s);
|
|
|
|
|
|
|
|
__STR_INLINE int hexvalue(char c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9') return c - '0';
|
|
|
|
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
|
|
|
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t str_fromprint(unsigned char *dst, const char *src);
|
|
|
|
|
2012-08-06 02:55:45 +00:00
|
|
|
/* Check if a given string starts with a given sub-string. If so, return 1 and, if afterp is not
|
2012-10-31 07:43:33 +00:00
|
|
|
* NULL, set *afterp to point to the character immediately following the substring. Otherwise
|
|
|
|
* return 0.
|
|
|
|
*
|
|
|
|
* This function is used to parse HTTP headers and responses, which are typically not
|
|
|
|
* nul-terminated, but are held in a buffer which has an associated length. To avoid this function
|
|
|
|
* running past the end of the buffer, the caller must ensure that the buffer contains a sub-string
|
|
|
|
* that is not part of the sub-string being sought, eg, "\r\n\r\n" as detected by
|
|
|
|
* http_header_complete(). This guarantees that this function will return nonzero before running
|
|
|
|
* past the end of the buffer.
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
2012-08-06 02:55:45 +00:00
|
|
|
*/
|
|
|
|
int str_startswith(char *str, const char *substring, char **afterp);
|
|
|
|
|
|
|
|
/* Case-insensitive form of str_startswith().
|
|
|
|
*/
|
|
|
|
int strcase_startswith(char *str, const char *substring, char **afterp);
|
|
|
|
|
2012-10-31 07:43:33 +00:00
|
|
|
/* like strstr(3), but doesn't depend on null termination.
|
|
|
|
*
|
|
|
|
* @author Paul Gardner-Stephen <paul@servalproject.org>
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
2012-10-03 07:43:45 +00:00
|
|
|
*/
|
|
|
|
char *str_str(char *haystack, const char *needle, int haystack_len);
|
2012-08-06 02:55:45 +00:00
|
|
|
|
2012-10-31 07:43:33 +00:00
|
|
|
/* Parse a string as an integer in ASCII radix notation in the given 'base' (eg, base=10 means
|
|
|
|
* decimal) and scale the result by a factor given by an optional suffix "scaling" character in the
|
|
|
|
* set {kKmMgG}: 'k' = 1e3, 'K' = 1<<10, 'm' = 1e6, 'M' = 1<<20, 'g' = 1e9, 'G' = * 1<<30.
|
|
|
|
*
|
|
|
|
* Return 1 if a valid scaled integer was parsed, storing the value in *result (unless result is
|
|
|
|
* NULL) and storing a pointer to the immediately succeeding character in *afterp (unless afterp is
|
|
|
|
* NULL, in which case returns 1 only if the immediately succeeding character is a nul '\0').
|
|
|
|
* Returns 0 otherwise, leaving *result and *afterp unchanged.
|
|
|
|
*
|
|
|
|
* NOTE: an argument base > 16 will cause any trailing 'g' or 'G' character to be parsed as part of
|
|
|
|
* the integer, not as a scale suffix. Ditto for base > 20 and 'k' 'K', and base > 22 and 'm' 'M'.
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
int str_to_ll_scaled(const char *str, int base, long long *result, char **afterp);
|
|
|
|
|
|
|
|
|
2012-09-27 06:57:37 +00:00
|
|
|
int parse_argv(char *cmdline, char delim, char **argv, int max_argv);
|
2012-08-06 02:55:45 +00:00
|
|
|
|
2012-08-30 11:18:16 +00:00
|
|
|
#endif
|