1999-02-05 04:57:20 +00:00
|
|
|
/*
|
2016-06-27 14:56:38 +10:00
|
|
|
* Copyright (C) 1999-2001, 2003-2007, 2011, 2012, 2014-2016 Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
1999-02-05 04:57:20 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-09 22:02:06 -08:00
|
|
|
/*
|
|
|
|
* 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.
|
2014-01-24 09:46:00 -08:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2014-01-09 22:02:06 -08:00
|
|
|
* 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.
|
|
|
|
*/
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2000-04-28 22:40:10 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1999-02-05 04:57:20 +00:00
|
|
|
#include <ctype.h>
|
2000-04-28 22:40:10 +00:00
|
|
|
|
2005-05-14 22:11:56 +00:00
|
|
|
#include <isc/mem.h>
|
2006-10-02 01:29:27 +00:00
|
|
|
#include <isc/print.h>
|
2005-05-14 22:11:56 +00:00
|
|
|
#include <isc/region.h>
|
2000-04-28 22:40:10 +00:00
|
|
|
#include <isc/string.h>
|
2005-05-14 22:11:56 +00:00
|
|
|
#include <isc/util.h>
|
1999-02-05 04:57:20 +00:00
|
|
|
|
2015-11-20 18:38:24 +11:00
|
|
|
static const char digits[] = "0123456789abcdefghijklmnoprstuvwxyz";
|
1999-02-05 04:57:20 +00:00
|
|
|
|
|
|
|
isc_uint64_t
|
2000-04-28 22:40:10 +00:00
|
|
|
isc_string_touint64(char *source, char **end, int base) {
|
1999-02-05 04:57:20 +00:00
|
|
|
isc_uint64_t tmp;
|
|
|
|
isc_uint64_t overflow;
|
|
|
|
char *s = source;
|
2015-11-20 18:38:24 +11:00
|
|
|
const char *o;
|
1999-02-05 04:57:20 +00:00
|
|
|
char c;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-02-05 04:57:20 +00:00
|
|
|
if ((base < 0) || (base == 1) || (base > 36)) {
|
|
|
|
*end = source;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*s != 0 && isascii(*s&0xff) && isspace(*s&0xff))
|
|
|
|
s++;
|
|
|
|
if (*s == '+' /* || *s == '-' */)
|
|
|
|
s++;
|
|
|
|
if (base == 0) {
|
|
|
|
if (*s == '0' && (*(s+1) == 'X' || *(s+1) == 'x')) {
|
|
|
|
s += 2;
|
|
|
|
base = 16;
|
|
|
|
} else if (*s == '0')
|
|
|
|
base = 8;
|
|
|
|
else
|
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
if (*s == 0) {
|
|
|
|
*end = source;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
overflow = ~0;
|
|
|
|
overflow /= base;
|
|
|
|
tmp = 0;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-02-05 04:57:20 +00:00
|
|
|
while ((c = *s) != 0) {
|
2004-09-16 01:01:27 +00:00
|
|
|
c = tolower(c&0xff);
|
1999-02-05 04:57:20 +00:00
|
|
|
/* end ? */
|
|
|
|
if ((o = strchr(digits, c)) == NULL) {
|
|
|
|
*end = s;
|
|
|
|
return (tmp);
|
|
|
|
}
|
|
|
|
/* end ? */
|
|
|
|
if ((o - digits) >= base) {
|
|
|
|
*end = s;
|
|
|
|
return (tmp);
|
|
|
|
}
|
|
|
|
/* overflow ? */
|
|
|
|
if (tmp > overflow) {
|
|
|
|
*end = source;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
tmp *= base;
|
|
|
|
/* overflow ? */
|
|
|
|
if ((tmp + (o - digits)) < tmp) {
|
|
|
|
*end = source;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
tmp += o - digits;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*end = s;
|
|
|
|
return (tmp);
|
|
|
|
}
|
2000-04-28 22:40:10 +00:00
|
|
|
|
2005-05-14 22:11:56 +00:00
|
|
|
isc_result_t
|
2005-06-19 22:56:03 +00:00
|
|
|
isc_string_copy(char *target, size_t size, const char *source) {
|
2005-08-23 04:05:50 +00:00
|
|
|
REQUIRE(size > 0U);
|
2005-05-14 22:11:56 +00:00
|
|
|
|
|
|
|
if (strlcpy(target, source, size) >= size) {
|
|
|
|
memset(target, ISC_STRING_MAGIC, size);
|
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
ENSURE(strlen(target) < size);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-06-19 22:56:03 +00:00
|
|
|
isc_string_copy_truncate(char *target, size_t size, const char *source) {
|
2005-08-23 04:05:50 +00:00
|
|
|
REQUIRE(size > 0U);
|
2005-05-14 22:11:56 +00:00
|
|
|
|
|
|
|
strlcpy(target, source, size);
|
|
|
|
|
|
|
|
ENSURE(strlen(target) < size);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2005-06-19 22:56:03 +00:00
|
|
|
isc_string_append(char *target, size_t size, const char *source) {
|
2005-08-23 04:05:50 +00:00
|
|
|
REQUIRE(size > 0U);
|
2005-05-14 22:11:56 +00:00
|
|
|
REQUIRE(strlen(target) < size);
|
|
|
|
|
|
|
|
if (strlcat(target, source, size) >= size) {
|
|
|
|
memset(target, ISC_STRING_MAGIC, size);
|
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
ENSURE(strlen(target) < size);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-06-19 22:56:03 +00:00
|
|
|
isc_string_append_truncate(char *target, size_t size, const char *source) {
|
2005-08-23 04:05:50 +00:00
|
|
|
REQUIRE(size > 0U);
|
2005-05-14 22:11:56 +00:00
|
|
|
REQUIRE(strlen(target) < size);
|
|
|
|
|
|
|
|
strlcat(target, source, size);
|
|
|
|
|
|
|
|
ENSURE(strlen(target) < size);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2005-06-19 22:56:03 +00:00
|
|
|
isc_string_printf(char *target, size_t size, const char *format, ...) {
|
2005-05-14 22:11:56 +00:00
|
|
|
va_list args;
|
|
|
|
size_t n;
|
|
|
|
|
2005-08-23 04:05:50 +00:00
|
|
|
REQUIRE(size > 0U);
|
2005-05-14 22:11:56 +00:00
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
n = vsnprintf(target, size, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (n >= size) {
|
|
|
|
memset(target, ISC_STRING_MAGIC, size);
|
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
ENSURE(strlen(target) < size);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-11 06:11:27 +00:00
|
|
|
isc_string_printf_truncate(char *target, size_t size, const char *format, ...)
|
|
|
|
{
|
2005-05-14 22:11:56 +00:00
|
|
|
va_list args;
|
|
|
|
|
2005-08-23 04:05:50 +00:00
|
|
|
REQUIRE(size > 0U);
|
2005-05-14 22:11:56 +00:00
|
|
|
|
|
|
|
va_start(args, format);
|
2011-03-11 06:11:27 +00:00
|
|
|
/* check return code? */
|
|
|
|
(void)vsnprintf(target, size, format, args);
|
2005-05-14 22:11:56 +00:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
ENSURE(strlen(target) < size);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2005-06-19 22:56:03 +00:00
|
|
|
isc_string_regiondup(isc_mem_t *mctx, const isc_region_t *source) {
|
2005-05-14 22:11:56 +00:00
|
|
|
char *target;
|
|
|
|
|
|
|
|
REQUIRE(mctx != NULL);
|
|
|
|
REQUIRE(source != NULL);
|
|
|
|
|
|
|
|
target = (char *) isc_mem_allocate(mctx, source->length + 1);
|
|
|
|
if (target != NULL) {
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(source->base, target, source->length);
|
2005-05-14 22:11:56 +00:00
|
|
|
target[source->length] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (target);
|
|
|
|
}
|
|
|
|
|
2000-04-28 22:40:10 +00:00
|
|
|
char *
|
|
|
|
isc_string_separate(char **stringp, const char *delim) {
|
|
|
|
char *string = *stringp;
|
|
|
|
char *s;
|
|
|
|
const char *d;
|
|
|
|
char sc, dc;
|
|
|
|
|
|
|
|
if (string == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
for (s = string; (sc = *s) != '\0'; s++)
|
|
|
|
for (d = delim; (dc = *d) != '\0'; d++)
|
|
|
|
if (sc == dc) {
|
|
|
|
*s++ = '\0';
|
|
|
|
*stringp = s;
|
|
|
|
return (string);
|
|
|
|
}
|
|
|
|
*stringp = NULL;
|
|
|
|
return (string);
|
|
|
|
}
|
2003-04-10 04:47:56 +00:00
|
|
|
|
|
|
|
size_t
|
2003-11-17 01:19:52 +00:00
|
|
|
isc_string_strlcpy(char *dst, const char *src, size_t size)
|
2003-04-10 04:47:56 +00:00
|
|
|
{
|
|
|
|
char *d = dst;
|
|
|
|
const char *s = src;
|
|
|
|
size_t n = size;
|
|
|
|
|
|
|
|
/* Copy as many bytes as will fit */
|
2004-03-18 02:58:08 +00:00
|
|
|
if (n != 0U && --n != 0U) {
|
2003-04-10 04:47:56 +00:00
|
|
|
do {
|
|
|
|
if ((*d++ = *s++) == 0)
|
|
|
|
break;
|
2004-03-18 02:58:08 +00:00
|
|
|
} while (--n != 0U);
|
2003-04-10 04:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Not enough room in dst, add NUL and traverse rest of src */
|
2004-03-18 02:58:08 +00:00
|
|
|
if (n == 0U) {
|
|
|
|
if (size != 0U)
|
2003-04-10 04:47:56 +00:00
|
|
|
*d = '\0'; /* NUL-terminate dst */
|
|
|
|
while (*s++)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(s - src - 1); /* count does not include NUL */
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2003-11-17 01:19:52 +00:00
|
|
|
isc_string_strlcat(char *dst, const char *src, size_t size)
|
2003-04-10 04:47:56 +00:00
|
|
|
{
|
|
|
|
char *d = dst;
|
|
|
|
const char *s = src;
|
|
|
|
size_t n = size;
|
|
|
|
size_t dlen;
|
|
|
|
|
|
|
|
/* Find the end of dst and adjust bytes left but don't go past end */
|
2004-03-18 02:58:08 +00:00
|
|
|
while (n-- != 0U && *d != '\0')
|
2003-04-10 04:47:56 +00:00
|
|
|
d++;
|
|
|
|
dlen = d - dst;
|
|
|
|
n = size - dlen;
|
|
|
|
|
2004-03-18 02:58:08 +00:00
|
|
|
if (n == 0U)
|
2003-04-10 04:47:56 +00:00
|
|
|
return(dlen + strlen(s));
|
|
|
|
while (*s != '\0') {
|
2004-03-18 02:58:08 +00:00
|
|
|
if (n != 1U) {
|
2003-04-10 04:47:56 +00:00
|
|
|
*d++ = *s;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*d = '\0';
|
|
|
|
|
|
|
|
return(dlen + (s - src)); /* count does not include NUL */
|
|
|
|
}
|
2014-01-09 22:02:06 -08:00
|
|
|
|
|
|
|
char *
|
|
|
|
isc_string_strcasestr(const char *str, const char *search) {
|
|
|
|
char c, sc, *s;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if ((c = *search++) != 0) {
|
|
|
|
c = tolower((unsigned char) c);
|
|
|
|
len = strlen(search);
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
if ((sc = *str++) == 0)
|
|
|
|
return (NULL);
|
|
|
|
} while ((char) tolower((unsigned char) sc) != c);
|
|
|
|
} while (strncasecmp(str, search, len) != 0);
|
|
|
|
str--;
|
|
|
|
}
|
|
|
|
DE_CONST(str, s);
|
|
|
|
return (s);
|
|
|
|
|
|
|
|
}
|