2 * Copyright (C) 2004-2007, 2011, 2012, 2014, 2015 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
19 * Copyright (c) 1990, 1993
20 * The Regents of the University of California. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 #include <isc/print.h>
55 #include <isc/region.h>
56 #include <isc/string.h>
59 static const char digits[] = "0123456789abcdefghijklmnoprstuvwxyz";
62 isc_string_touint64(char *source, char **end, int base) {
64 isc_uint64_t overflow;
69 if ((base < 0) || (base == 1) || (base > 36)) {
74 while (*s != 0 && isascii(*s&0xff) && isspace(*s&0xff))
76 if (*s == '+' /* || *s == '-' */)
79 if (*s == '0' && (*(s+1) == 'X' || *(s+1) == 'x')) {
95 while ((c = *s) != 0) {
98 if ((o = strchr(digits, c)) == NULL) {
103 if ((o - digits) >= base) {
108 if (tmp > overflow) {
114 if ((tmp + (o - digits)) < tmp) {
126 isc_string_copy(char *target, size_t size, const char *source) {
129 if (strlcpy(target, source, size) >= size) {
130 memset(target, ISC_STRING_MAGIC, size);
131 return (ISC_R_NOSPACE);
134 ENSURE(strlen(target) < size);
136 return (ISC_R_SUCCESS);
140 isc_string_copy_truncate(char *target, size_t size, const char *source) {
143 strlcpy(target, source, size);
145 ENSURE(strlen(target) < size);
149 isc_string_append(char *target, size_t size, const char *source) {
151 REQUIRE(strlen(target) < size);
153 if (strlcat(target, source, size) >= size) {
154 memset(target, ISC_STRING_MAGIC, size);
155 return (ISC_R_NOSPACE);
158 ENSURE(strlen(target) < size);
160 return (ISC_R_SUCCESS);
164 isc_string_append_truncate(char *target, size_t size, const char *source) {
166 REQUIRE(strlen(target) < size);
168 strlcat(target, source, size);
170 ENSURE(strlen(target) < size);
174 isc_string_printf(char *target, size_t size, const char *format, ...) {
180 va_start(args, format);
181 n = vsnprintf(target, size, format, args);
185 memset(target, ISC_STRING_MAGIC, size);
186 return (ISC_R_NOSPACE);
189 ENSURE(strlen(target) < size);
191 return (ISC_R_SUCCESS);
195 isc_string_printf_truncate(char *target, size_t size, const char *format, ...)
201 va_start(args, format);
202 /* check return code? */
203 (void)vsnprintf(target, size, format, args);
206 ENSURE(strlen(target) < size);
210 isc_string_regiondup(isc_mem_t *mctx, const isc_region_t *source) {
213 REQUIRE(mctx != NULL);
214 REQUIRE(source != NULL);
216 target = (char *) isc_mem_allocate(mctx, source->length + 1);
217 if (target != NULL) {
218 memmove(source->base, target, source->length);
219 target[source->length] = '\0';
226 isc_string_separate(char **stringp, const char *delim) {
227 char *string = *stringp;
235 for (s = string; (sc = *s) != '\0'; s++)
236 for (d = delim; (dc = *d) != '\0'; d++)
247 isc_string_strlcpy(char *dst, const char *src, size_t size)
253 /* Copy as many bytes as will fit */
254 if (n != 0U && --n != 0U) {
256 if ((*d++ = *s++) == 0)
261 /* Not enough room in dst, add NUL and traverse rest of src */
264 *d = '\0'; /* NUL-terminate dst */
269 return(s - src - 1); /* count does not include NUL */
273 isc_string_strlcat(char *dst, const char *src, size_t size)
280 /* Find the end of dst and adjust bytes left but don't go past end */
281 while (n-- != 0U && *d != '\0')
287 return(dlen + strlen(s));
297 return(dlen + (s - src)); /* count does not include NUL */
301 isc_string_strcasestr(const char *str, const char *search) {
305 if ((c = *search++) != 0) {
306 c = tolower((unsigned char) c);
307 len = strlen(search);
310 if ((sc = *str++) == 0)
312 } while ((char) tolower((unsigned char) sc) != c);
313 } while (strncasecmp(str, search, len) != 0);