4 * Copyright (C) 2004-2007 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.
20 /* Id: string.c,v 1.20 2007/06/19 23:47:17 tbox Exp */
29 #include <isc/print.h>
30 #include <isc/region.h>
31 #include <isc/string.h>
34 static char digits
[] = "0123456789abcdefghijklmnoprstuvwxyz";
37 isc_string_touint64(char *source
, char **end
, int base
) {
39 isc_uint64_t overflow
;
44 if ((base
< 0) || (base
== 1) || (base
> 36)) {
49 while (*s
!= 0 && isascii(*s
&0xff) && isspace(*s
&0xff))
51 if (*s
== '+' /* || *s == '-' */)
54 if (*s
== '0' && (*(s
+1) == 'X' || *(s
+1) == 'x')) {
70 while ((c
= *s
) != 0) {
73 if ((o
= strchr(digits
, c
)) == NULL
) {
78 if ((o
- digits
) >= base
) {
89 if ((tmp
+ (o
- digits
)) < tmp
) {
101 isc_string_copy(char *target
, size_t size
, const char *source
) {
104 if (strlcpy(target
, source
, size
) >= size
) {
105 memset(target
, ISC_STRING_MAGIC
, size
);
106 return (ISC_R_NOSPACE
);
109 ENSURE(strlen(target
) < size
);
111 return (ISC_R_SUCCESS
);
115 isc_string_copy_truncate(char *target
, size_t size
, const char *source
) {
118 strlcpy(target
, source
, size
);
120 ENSURE(strlen(target
) < size
);
124 isc_string_append(char *target
, size_t size
, const char *source
) {
126 REQUIRE(strlen(target
) < size
);
128 if (strlcat(target
, source
, size
) >= size
) {
129 memset(target
, ISC_STRING_MAGIC
, size
);
130 return (ISC_R_NOSPACE
);
133 ENSURE(strlen(target
) < size
);
135 return (ISC_R_SUCCESS
);
139 isc_string_append_truncate(char *target
, size_t size
, const char *source
) {
141 REQUIRE(strlen(target
) < size
);
143 strlcat(target
, source
, size
);
145 ENSURE(strlen(target
) < size
);
149 isc_string_printf(char *target
, size_t size
, const char *format
, ...) {
155 va_start(args
, format
);
156 n
= vsnprintf(target
, size
, format
, args
);
160 memset(target
, ISC_STRING_MAGIC
, size
);
161 return (ISC_R_NOSPACE
);
164 ENSURE(strlen(target
) < size
);
166 return (ISC_R_SUCCESS
);
170 isc_string_printf_truncate(char *target
, size_t size
, const char *format
, ...) {
176 va_start(args
, format
);
177 n
= vsnprintf(target
, size
, format
, args
);
180 ENSURE(strlen(target
) < size
);
184 isc_string_regiondup(isc_mem_t
*mctx
, const isc_region_t
*source
) {
187 REQUIRE(mctx
!= NULL
);
188 REQUIRE(source
!= NULL
);
190 target
= (char *) isc_mem_allocate(mctx
, source
->length
+ 1);
191 if (target
!= NULL
) {
192 memcpy(source
->base
, target
, source
->length
);
193 target
[source
->length
] = '\0';
200 isc_string_separate(char **stringp
, const char *delim
) {
201 char *string
= *stringp
;
209 for (s
= string
; (sc
= *s
) != '\0'; s
++)
210 for (d
= delim
; (dc
= *d
) != '\0'; d
++)
221 isc_string_strlcpy(char *dst
, const char *src
, size_t size
)
227 /* Copy as many bytes as will fit */
228 if (n
!= 0U && --n
!= 0U) {
230 if ((*d
++ = *s
++) == 0)
235 /* Not enough room in dst, add NUL and traverse rest of src */
238 *d
= '\0'; /* NUL-terminate dst */
243 return(s
- src
- 1); /* count does not include NUL */
247 isc_string_strlcat(char *dst
, const char *src
, size_t size
)
254 /* Find the end of dst and adjust bytes left but don't go past end */
255 while (n
-- != 0U && *d
!= '\0')
261 return(dlen
+ strlen(s
));
271 return(dlen
+ (s
- src
)); /* count does not include NUL */