2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Swen Schillig 2019
6 * ** NOTE! The following LGPL license applies to this file.
7 * ** This does NOT imply that all of Samba is released
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 3 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "smb_strtox.h"
28 * Convert a string to an unsigned long integer
30 * @param nptr pointer to string which is to be converted
31 * @param endptr [optional] reference to remainder of the string
32 * @param base base of the numbering scheme
33 * @param err error occured during conversion
34 * @flags controlling conversion feature
35 * @result result of the conversion as provided by strtoul
37 * The following flags are supported
38 * SMB_STR_STANDARD # raise error if negative or non-numeric
39 * SMB_STR_ALLOW_NEGATIVE # allow strings with a leading "-"
40 * SMB_STR_FULL_STR_CONV # entire string must be converted
41 * SMB_STR_ALLOW_NO_CONVERSION # allow empty strings or non-numeric
42 * SMB_STR_GLIBC_STANDARD # act exactly as the standard glibc strtoul
44 * The following errors are detected
47 * - string with a leading "-" indicating a negative number
48 * - no conversion due to empty string or not representing a number
51 smb_strtoul(const char *nptr, char **endptr, int base, int *err, int flags)
53 unsigned long int val;
54 int saved_errno = errno;
56 char *tmp_endptr = NULL;
61 val = strtoul(nptr, &tmp_endptr, base);
73 if ((flags & SMB_STR_ALLOW_NO_CONVERSION) == 0) {
74 /* got an invalid number-string resulting in no conversion */
75 if (nptr == tmp_endptr) {
81 if ((flags & SMB_STR_ALLOW_NEGATIVE ) == 0) {
82 /* did we convert a negative "number" ? */
83 needle = strchr(nptr, '-');
84 if (needle != NULL && needle < tmp_endptr) {
90 if ((flags & SMB_STR_FULL_STR_CONV) != 0) {
91 /* did we convert the entire string ? */
92 if (tmp_endptr[0] != '\0') {
104 * Convert a string to an unsigned long long integer
106 * @param nptr pointer to string which is to be converted
107 * @param endptr [optional] reference to remainder of the string
108 * @param base base of the numbering scheme
109 * @param err error occured during conversion
110 * @flags controlling conversion feature
111 * @result result of the conversion as provided by strtoull
113 * The following flags are supported
114 * SMB_STR_STANDARD # raise error if negative or non-numeric
115 * SMB_STR_ALLOW_NEGATIVE # allow strings with a leading "-"
116 * SMB_STR_FULL_STR_CONV # entire string must be converted
117 * SMB_STR_ALLOW_NO_CONVERSION # allow empty strings or non-numeric
118 * SMB_STR_GLIBC_STANDARD # act exactly as the standard glibc strtoul
120 * The following errors are detected
123 * - string with a leading "-" indicating a negative number
124 * - no conversion due to empty string or not representing a number
126 unsigned long long int
127 smb_strtoull(const char *nptr, char **endptr, int base, int *err, int flags)
129 unsigned long long int val;
130 int saved_errno = errno;
132 char *tmp_endptr = NULL;
137 val = strtoull(nptr, &tmp_endptr, base);
139 if (endptr != NULL) {
140 *endptr = tmp_endptr;
149 if ((flags & SMB_STR_ALLOW_NO_CONVERSION) == 0) {
150 /* got an invalid number-string resulting in no conversion */
151 if (nptr == tmp_endptr) {
157 if ((flags & SMB_STR_ALLOW_NEGATIVE ) == 0) {
158 /* did we convert a negative "number" ? */
159 needle = strchr(nptr, '-');
160 if (needle != NULL && needle < tmp_endptr) {
166 if ((flags & SMB_STR_FULL_STR_CONV) != 0) {
167 /* did we convert the entire string ? */
168 if (tmp_endptr[0] != '\0') {