1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12 /*
13 * Copyright (c) 1990, 1993
14 * The Regents of the University of California. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 /*! \file */
42 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
44 #endif /* LIBC_SCCS and not lint */
45
46 #include <config.h>
47
48 #include <limits.h>
49 #include <ctype.h>
50 #include <errno.h>
51
52 #include <lwres/stdlib.h>
53 #include <lwres/string.h>
54
55 #define DE_CONST(konst, var) \
56 do { \
57 union { const void *k; void *v; } _u; \
58 _u.k = konst; \
59 var = _u.v; \
60 } while (0)
61
62 /*!
63 * Convert a string to an unsigned long integer.
64 *
65 * Ignores `locale' stuff. Assumes that the upper and lower case
66 * alphabets and digits are each contiguous.
67 */
68 unsigned long
lwres_strtoul(const char * nptr,char ** endptr,int base)69 lwres_strtoul(const char *nptr, char **endptr, int base) {
70 const char *s = nptr;
71 unsigned long acc;
72 unsigned char c;
73 unsigned long cutoff;
74 int neg = 0, any, cutlim;
75
76 /*
77 * See strtol for comments as to the logic used.
78 */
79 do {
80 c = *s++;
81 } while (isspace(c));
82 if (c == '-') {
83 neg = 1;
84 c = *s++;
85 } else if (c == '+')
86 c = *s++;
87 if ((base == 0 || base == 16) &&
88 c == '0' && (*s == 'x' || *s == 'X')) {
89 c = s[1];
90 s += 2;
91 base = 16;
92 }
93 if (base == 0)
94 base = c == '0' ? 8 : 10;
95 cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
96 cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
97 for (acc = 0, any = 0;; c = *s++) {
98 if (!isascii(c))
99 break;
100 if (isdigit(c))
101 c -= '0';
102 else if (isalpha(c))
103 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
104 else
105 break;
106 if (c >= base)
107 break;
108 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
109 any = -1;
110 else {
111 any = 1;
112 acc *= base;
113 acc += c;
114 }
115 }
116 if (any < 0) {
117 acc = ULONG_MAX;
118 errno = ERANGE;
119 } else if (neg)
120 /* XXX: acc was declared unsigned! */
121 acc = -acc;
122 if (endptr != 0)
123 DE_CONST(any ? s - 1 : nptr, *endptr);
124 return (acc);
125 }
126
127 size_t
lwres_strlcpy(char * dst,const char * src,size_t size)128 lwres_strlcpy(char *dst, const char *src, size_t size) {
129 char *d = dst;
130 const char *s = src;
131 size_t n = size;
132
133 /* Copy as many bytes as will fit */
134 if (n != 0U && --n != 0U) {
135 do {
136 if ((*d++ = *s++) == 0)
137 break;
138 } while (--n != 0U);
139 }
140
141 /* Not enough room in dst, add NUL and traverse rest of src */
142 if (n == 0U) {
143 if (size != 0U)
144 *d = '\0'; /* NUL-terminate dst */
145 while (*s++)
146 ;
147 }
148
149 return(s - src - 1); /* count does not include NUL */
150 }
151