1 /* $NetBSD: string.c,v 1.6 2014/12/10 04:37:59 christos Exp $ */
4 * Copyright (C) 2004-2007, 2011, 2012, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
21 * Copyright (c) 1990, 1993
22 * The Regents of the University of California. All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. Neither the name of the University nor the names of its contributors
33 * may be used to endorse or promote products derived from this software
34 * without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 #include <isc/print.h>
57 #include <isc/region.h>
58 #include <isc/string.h>
61 static char digits
[] = "0123456789abcdefghijklmnoprstuvwxyz";
64 isc_string_touint64(char *source
, char **end
, int base
) {
66 isc_uint64_t overflow
;
71 if ((base
< 0) || (base
== 1) || (base
> 36)) {
76 while (*s
!= 0 && isascii(*s
&0xff) && isspace(*s
&0xff))
78 if (*s
== '+' /* || *s == '-' */)
81 if (*s
== '0' && (*(s
+1) == 'X' || *(s
+1) == 'x')) {
97 while ((c
= *s
) != 0) {
100 if ((o
= strchr(digits
, c
)) == NULL
) {
105 if ((o
- digits
) >= base
) {
110 if (tmp
> overflow
) {
116 if ((tmp
+ (o
- digits
)) < tmp
) {
128 isc_string_copy(char *target
, size_t size
, const char *source
) {
131 if (strlcpy(target
, source
, size
) >= size
) {
132 memset(target
, ISC_STRING_MAGIC
, size
);
133 return (ISC_R_NOSPACE
);
136 ENSURE(strlen(target
) < size
);
138 return (ISC_R_SUCCESS
);
142 isc_string_copy_truncate(char *target
, size_t size
, const char *source
) {
145 strlcpy(target
, source
, size
);
147 ENSURE(strlen(target
) < size
);
151 isc_string_append(char *target
, size_t size
, const char *source
) {
153 REQUIRE(strlen(target
) < size
);
155 if (strlcat(target
, source
, size
) >= size
) {
156 memset(target
, ISC_STRING_MAGIC
, size
);
157 return (ISC_R_NOSPACE
);
160 ENSURE(strlen(target
) < size
);
162 return (ISC_R_SUCCESS
);
166 isc_string_append_truncate(char *target
, size_t size
, const char *source
) {
168 REQUIRE(strlen(target
) < size
);
170 strlcat(target
, source
, size
);
172 ENSURE(strlen(target
) < size
);
176 isc_string_printf(char *target
, size_t size
, const char *format
, ...) {
182 va_start(args
, format
);
183 n
= vsnprintf(target
, size
, format
, args
);
187 memset(target
, ISC_STRING_MAGIC
, size
);
188 return (ISC_R_NOSPACE
);
191 ENSURE(strlen(target
) < size
);
193 return (ISC_R_SUCCESS
);
197 isc_string_printf_truncate(char *target
, size_t size
, const char *format
, ...)
203 va_start(args
, format
);
204 /* check return code? */
205 (void)vsnprintf(target
, size
, format
, args
);
208 ENSURE(strlen(target
) < size
);
212 isc_string_regiondup(isc_mem_t
*mctx
, const isc_region_t
*source
) {
215 REQUIRE(mctx
!= NULL
);
216 REQUIRE(source
!= NULL
);
218 target
= (char *) isc_mem_allocate(mctx
, source
->length
+ 1);
219 if (target
!= NULL
) {
220 memmove(source
->base
, target
, source
->length
);
221 target
[source
->length
] = '\0';
228 isc_string_separate(char **stringp
, const char *delim
) {
229 char *string
= *stringp
;
237 for (s
= string
; (sc
= *s
) != '\0'; s
++)
238 for (d
= delim
; (dc
= *d
) != '\0'; d
++)
249 isc_string_strlcpy(char *dst
, const char *src
, size_t size
)
255 /* Copy as many bytes as will fit */
256 if (n
!= 0U && --n
!= 0U) {
258 if ((*d
++ = *s
++) == 0)
263 /* Not enough room in dst, add NUL and traverse rest of src */
266 *d
= '\0'; /* NUL-terminate dst */
271 return(s
- src
- 1); /* count does not include NUL */
275 isc_string_strlcat(char *dst
, const char *src
, size_t size
)
282 /* Find the end of dst and adjust bytes left but don't go past end */
283 while (n
-- != 0U && *d
!= '\0')
289 return(dlen
+ strlen(s
));
299 return(dlen
+ (s
- src
)); /* count does not include NUL */
303 isc_string_strcasestr(const char *str
, const char *search
) {
307 if ((c
= *search
++) != 0) {
308 c
= tolower((unsigned char) c
);
309 len
= strlen(search
);
312 if ((sc
= *str
++) == 0)
314 } while ((char) tolower((unsigned char) sc
) != c
);
315 } while (strncasecmp(str
, search
, len
) != 0);