mirror of https://github.com/c-ares/c-ares.git
Expose library/utility functions to tools (#860)
The tools like `adig` and `ahost` can take advantage of some of the library functions within c-ares. We can't really split these library functions into a helper library without possibly breaking users. Technically if we could guarantee they rely on pkg-config or cmake helpers we could make it work ... we can revisit that in the future, maybe if we make a c-ares 2.0 where people might expect more breakage (but I still wouldn't want to break API/ABI). So what this does is it just exports some symbols in headers that aren't distributed so end users aren't meant to use the symbols, but any utilities or tests built by c-ares can. It does clutter up some of the namespace, but all these symbols are guaranteed to be prefixed with `ares_` so there shouldn't ever be symbol clashes due to this for end users and its not so many symbols that it should affect any load times. There will be **zero** API/ABI guarantees for these symbols. They can change from release to release, this is ok since they are not public. I'm not entirely thrilled with doing this solution, but I want to avoid thing like hand-written parsers, such as is used in #856. Authored-By: Brad House (@bradh352)pull/863/head
parent
538a4866be
commit
7f19f162db
51 changed files with 920 additions and 736 deletions
@ -0,0 +1,8 @@ |
||||
# Semi-public headers |
||||
The headers listed here all export symbols into the ares namespace as public |
||||
symbols, but these headers are NOT included in the distribution. They are |
||||
meant to be used by other tools such as `adig` and `ahost`. |
||||
|
||||
These are most likely going to be general purpose library functions such |
||||
as data structures and algorithms. |
||||
|
@ -0,0 +1,185 @@ |
||||
/* MIT License
|
||||
* |
||||
* Copyright (c) 1998 Massachusetts Institute of Technology |
||||
* Copyright (c) The c-ares project and its contributors |
||||
* |
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
* of this software and associated documentation files (the "Software"), to deal |
||||
* in the Software without restriction, including without limitation the rights |
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
* copies of the Software, and to permit persons to whom the Software is |
||||
* furnished to do so, subject to the following conditions: |
||||
* |
||||
* The above copyright notice and this permission notice (including the next |
||||
* paragraph) shall be included in all copies or substantial portions of the |
||||
* Software. |
||||
* |
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
* SOFTWARE. |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
#ifndef __ARES_STR_H |
||||
#define __ARES_STR_H |
||||
|
||||
CARES_EXTERN char *ares_strdup(const char *s1); |
||||
|
||||
CARES_EXTERN size_t ares_strlen(const char *str); |
||||
|
||||
/*! Copy string from source to destination with destination buffer size
|
||||
* provided. The destination is guaranteed to be null terminated, if the |
||||
* provided buffer isn't large enough, only those bytes from the source that |
||||
* will fit will be copied. |
||||
* |
||||
* \param[out] dest Destination buffer |
||||
* \param[in] src Source to copy |
||||
* \param[in] dest_size Size of destination buffer |
||||
* \return String length. Will be at most dest_size-1 |
||||
*/ |
||||
CARES_EXTERN size_t ares_strcpy(char *dest, const char *src, size_t dest_size); |
||||
|
||||
CARES_EXTERN ares_bool_t ares_str_isnum(const char *str); |
||||
|
||||
CARES_EXTERN void ares__str_ltrim(char *str); |
||||
CARES_EXTERN void ares__str_rtrim(char *str); |
||||
CARES_EXTERN void ares__str_trim(char *str); |
||||
|
||||
CARES_EXTERN unsigned char ares__tolower(unsigned char c); |
||||
CARES_EXTERN ares_bool_t ares__memeq_ci(const unsigned char *ptr, |
||||
const unsigned char *val, size_t len); |
||||
|
||||
CARES_EXTERN ares_bool_t ares__isspace(int ch); |
||||
CARES_EXTERN ares_bool_t ares__isprint(int ch); |
||||
CARES_EXTERN ares_bool_t ares__is_hostnamech(int ch); |
||||
|
||||
CARES_EXTERN ares_bool_t ares__is_hostname(const char *str); |
||||
|
||||
/*! Validate the string provided is printable. The length specified must be
|
||||
* at least the size of the buffer provided. If a NULL-terminator is hit |
||||
* before the length provided is hit, this will not be considered a valid |
||||
* printable string. This does not validate that the string is actually |
||||
* NULL terminated. |
||||
* |
||||
* \param[in] str Buffer containing string to evaluate. |
||||
* \param[in] len Number of characters to evaluate within provided buffer. |
||||
* If 0, will return TRUE since it did not hit an exception. |
||||
* \return ARES_TRUE if the entire string is printable, ARES_FALSE if not. |
||||
*/ |
||||
CARES_EXTERN ares_bool_t ares__str_isprint(const char *str, size_t len); |
||||
|
||||
/* We only care about ASCII rules */ |
||||
#define ares__isascii(x) (((unsigned char)x) <= 127) |
||||
#define ares__isdigit(x) \ |
||||
(((unsigned char)x) >= '0' && ((unsigned char)x) <= '9') |
||||
#define ares__isxdigit(x) \ |
||||
(ares__isdigit(x) || \
|
||||
(((unsigned char)x) >= 'a' && ((unsigned char)x) <= 'f') || \
|
||||
(((unsigned char)x) >= 'A' && ((unsigned char)x) <= 'F')) |
||||
#define ares__isupper(x) \ |
||||
(((unsigned char)x) >= 'A' && ((unsigned char)x) <= 'Z') |
||||
#define ares__islower(x) \ |
||||
(((unsigned char)x) >= 'a' && ((unsigned char)x) <= 'z') |
||||
#define ares__isalpha(x) (ares__islower(x) || ares__isupper(x)) |
||||
|
||||
/*! Compare two strings (for sorting)
|
||||
* |
||||
* Treats NULL and "" strings as equivalent |
||||
* |
||||
* \param[in] a First String |
||||
* \param[in] b Second String |
||||
* \return < 0 if First String less than Second String, |
||||
* 0 if First String equal to Second String, |
||||
* > 0 if First String greater than Second String |
||||
*/ |
||||
CARES_EXTERN int ares_strcmp(const char *a, const char *b); |
||||
|
||||
/*! Compare two strings up to specified length (for sorting)
|
||||
* |
||||
* Treats NULL and "" strings as equivalent |
||||
* |
||||
* \param[in] a First String |
||||
* \param[in] b Second String |
||||
* \param[in] n Length |
||||
* \return < 0 if First String less than Second String, |
||||
* 0 if First String equal to Second String, |
||||
* > 0 if First String greater than Second String |
||||
*/ |
||||
CARES_EXTERN int ares_strncmp(const char *a, const char *b, size_t n); |
||||
|
||||
|
||||
/*! Compare two strings in a case-insensitive manner (for sorting)
|
||||
* |
||||
* Treats NULL and "" strings as equivalent |
||||
* |
||||
* \param[in] a First String |
||||
* \param[in] b Second String |
||||
* \return < 0 if First String less than Second String, |
||||
* 0 if First String equal to Second String, |
||||
* > 0 if First String greater than Second String |
||||
*/ |
||||
CARES_EXTERN int ares_strcasecmp(const char *a, const char *b); |
||||
|
||||
/*! Compare two strings in a case-insensitive manner up to specified length
|
||||
* (for sorting) |
||||
* |
||||
* Treats NULL and "" strings as equivalent |
||||
* |
||||
* \param[in] a First String |
||||
* \param[in] b Second String |
||||
* \param[in] n Length |
||||
* \return < 0 if First String less than Second String, |
||||
* 0 if First String equal to Second String, |
||||
* > 0 if First String greater than Second String |
||||
*/ |
||||
CARES_EXTERN int ares_strncasecmp(const char *a, const char *b, size_t n); |
||||
|
||||
/*! Compare two strings for equality
|
||||
* |
||||
* Treats NULL and "" strings as equivalent |
||||
* |
||||
* \param[in] a First String |
||||
* \param[in] b Second String |
||||
* \return ARES_TRUE on match, or ARES_FALSE if no match |
||||
*/ |
||||
CARES_EXTERN ares_bool_t ares_streq(const char *a, const char *b); |
||||
|
||||
/*! Compare two strings for equality up to specified length
|
||||
* |
||||
* Treats NULL and "" strings as equivalent |
||||
* |
||||
* \param[in] a First String |
||||
* \param[in] b Second String |
||||
* \param[in] n Length |
||||
* \return ARES_TRUE on match, or ARES_FALSE if no match |
||||
*/ |
||||
CARES_EXTERN ares_bool_t ares_streq_max(const char *a, const char *b, size_t n); |
||||
|
||||
/*! Compare two strings for equality in a case insensitive manner
|
||||
* |
||||
* Treats NULL and "" strings as equivalent |
||||
* |
||||
* \param[in] a First String |
||||
* \param[in] b Second String |
||||
* \return ARES_TRUE on match, or ARES_FALSE if no match |
||||
*/ |
||||
CARES_EXTERN ares_bool_t ares_strcaseeq(const char *a, const char *b); |
||||
|
||||
/*! Compare two strings for equality up to specified length in a case
|
||||
* insensitive manner |
||||
* |
||||
* Treats NULL and "" strings as equivalent |
||||
* |
||||
* \param[in] a First String |
||||
* \param[in] b Second String |
||||
* \param[in] n Length |
||||
* \return ARES_TRUE on match, or ARES_FALSE if no match |
||||
*/ |
||||
CARES_EXTERN ares_bool_t ares_strcaseeq_max(const char *a, const char *b, |
||||
size_t n); |
||||
|
||||
#endif /* __ARES_STR_H */ |
@ -1,89 +0,0 @@ |
||||
/* MIT License
|
||||
* |
||||
* Copyright (c) 1998 Massachusetts Institute of Technology |
||||
* Copyright (c) The c-ares project and its contributors |
||||
* |
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
* of this software and associated documentation files (the "Software"), to deal |
||||
* in the Software without restriction, including without limitation the rights |
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
* copies of the Software, and to permit persons to whom the Software is |
||||
* furnished to do so, subject to the following conditions: |
||||
* |
||||
* The above copyright notice and this permission notice (including the next |
||||
* paragraph) shall be included in all copies or substantial portions of the |
||||
* Software. |
||||
* |
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
* SOFTWARE. |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
#ifndef __ARES_STR_H |
||||
#define __ARES_STR_H |
||||
|
||||
char *ares_strdup(const char *s1); |
||||
|
||||
size_t ares_strlen(const char *str); |
||||
|
||||
/*! Copy string from source to destination with destination buffer size
|
||||
* provided. The destination is guaranteed to be null terminated, if the |
||||
* provided buffer isn't large enough, only those bytes from the source that |
||||
* will fit will be copied. |
||||
* |
||||
* \param[out] dest Destination buffer |
||||
* \param[in] src Source to copy |
||||
* \param[in] dest_size Size of destination buffer |
||||
* \return String length. Will be at most dest_size-1 |
||||
*/ |
||||
size_t ares_strcpy(char *dest, const char *src, size_t dest_size); |
||||
|
||||
ares_bool_t ares_str_isnum(const char *str); |
||||
|
||||
void ares__str_ltrim(char *str); |
||||
void ares__str_rtrim(char *str); |
||||
void ares__str_trim(char *str); |
||||
|
||||
unsigned char ares__tolower(unsigned char c); |
||||
ares_bool_t ares__memeq_ci(const unsigned char *ptr, const unsigned char *val, |
||||
size_t len); |
||||
|
||||
ares_bool_t ares__isspace(int ch); |
||||
ares_bool_t ares__isprint(int ch); |
||||
ares_bool_t ares__is_hostnamech(int ch); |
||||
|
||||
ares_bool_t ares__is_hostname(const char *str); |
||||
|
||||
/*! Validate the string provided is printable. The length specified must be
|
||||
* at least the size of the buffer provided. If a NULL-terminator is hit |
||||
* before the length provided is hit, this will not be considered a valid |
||||
* printable string. This does not validate that the string is actually |
||||
* NULL terminated. |
||||
* |
||||
* \param[in] str Buffer containing string to evaluate. |
||||
* \param[in] len Number of characters to evaluate within provided buffer. |
||||
* If 0, will return TRUE since it did not hit an exception. |
||||
* \return ARES_TRUE if the entire string is printable, ARES_FALSE if not. |
||||
*/ |
||||
ares_bool_t ares__str_isprint(const char *str, size_t len); |
||||
|
||||
/* We only care about ASCII rules */ |
||||
#define ares__isascii(x) (((unsigned char)x) <= 127) |
||||
#define ares__isdigit(x) \ |
||||
(((unsigned char)x) >= '0' && ((unsigned char)x) <= '9') |
||||
#define ares__isxdigit(x) \ |
||||
(ares__isdigit(x) || \
|
||||
(((unsigned char)x) >= 'a' && ((unsigned char)x) <= 'f') || \
|
||||
(((unsigned char)x) >= 'A' && ((unsigned char)x) <= 'F')) |
||||
#define ares__isupper(x) \ |
||||
(((unsigned char)x) >= 'A' && ((unsigned char)x) <= 'Z') |
||||
#define ares__islower(x) \ |
||||
(((unsigned char)x) >= 'a' && ((unsigned char)x) <= 'z') |
||||
#define ares__isalpha(x) (ares__islower(x) || ares__isupper(x)) |
||||
|
||||
#endif /* __ARES_STR_H */ |
@ -1,79 +0,0 @@ |
||||
/* MIT License
|
||||
* |
||||
* Copyright (c) 1998 Massachusetts Institute of Technology |
||||
* Copyright (c) The c-ares project and its contributors |
||||
* |
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
* of this software and associated documentation files (the "Software"), to deal |
||||
* in the Software without restriction, including without limitation the rights |
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
* copies of the Software, and to permit persons to whom the Software is |
||||
* furnished to do so, subject to the following conditions: |
||||
* |
||||
* The above copyright notice and this permission notice (including the next |
||||
* paragraph) shall be included in all copies or substantial portions of the |
||||
* Software. |
||||
* |
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
* SOFTWARE. |
||||
* |
||||
* SPDX-License-Identifier: MIT |
||||
*/ |
||||
|
||||
#include "ares_private.h" |
||||
#include "ares_strcasecmp.h" |
||||
|
||||
#ifndef HAVE_STRCASECMP |
||||
int ares_strcasecmp(const char *a, const char *b) |
||||
{ |
||||
# if defined(HAVE_STRCMPI) |
||||
return strcmpi(a, b); |
||||
# elif defined(HAVE_STRICMP) |
||||
return stricmp(a, b); |
||||
# else |
||||
size_t i; |
||||
|
||||
for (i = 0; i < (size_t)-1; i++) { |
||||
int c1 = ares__tolower(a[i]); |
||||
int c2 = ares__tolower(b[i]); |
||||
if (c1 != c2) { |
||||
return c1 - c2; |
||||
} |
||||
if (!c1) { |
||||
break; |
||||
} |
||||
} |
||||
return 0; |
||||
# endif |
||||
} |
||||
#endif |
||||
|
||||
#ifndef HAVE_STRNCASECMP |
||||
int ares_strncasecmp(const char *a, const char *b, size_t n) |
||||
{ |
||||
# if defined(HAVE_STRNCMPI) |
||||
return strncmpi(a, b, n); |
||||
# elif defined(HAVE_STRNICMP) |
||||
return strnicmp(a, b, n); |
||||
# else |
||||
size_t i; |
||||
|
||||
for (i = 0; i < n; i++) { |
||||
int c1 = ares__tolower(a[i]); |
||||
int c2 = ares__tolower(b[i]); |
||||
if (c1 != c2) { |
||||
return c1 - c2; |
||||
} |
||||
if (!c1) { |
||||
break; |
||||
} |
||||
} |
||||
return 0; |
||||
# endif |
||||
} |
||||
#endif |
@ -1,7 +1,5 @@ |
||||
# Copyright (C) The c-ares project and its contributors |
||||
# SPDX-License-Identifier: MIT |
||||
SAMPLESOURCES = ares_getopt.c \ |
||||
../lib/str/ares_strcasecmp.c |
||||
SAMPLESOURCES = ares_getopt.c |
||||
|
||||
SAMPLEHEADERS = ares_getopt.h \ |
||||
../lib/str/ares_strcasecmp.h |
||||
SAMPLEHEADERS = ares_getopt.h |
||||
|
Loading…
Reference in new issue