xref: /freebsd/contrib/libfido2/src/util.c (revision 2ccfa855b2fc331819953e3de1b1c15ce5b95a7e)
1*2ccfa855SEd Maste /*
2*2ccfa855SEd Maste  * Copyright (c) 2022 Yubico AB. All rights reserved.
3*2ccfa855SEd Maste  * Use of this source code is governed by a BSD-style
4*2ccfa855SEd Maste  * license that can be found in the LICENSE file.
5*2ccfa855SEd Maste  * SPDX-License-Identifier: BSD-2-Clause
6*2ccfa855SEd Maste  */
7*2ccfa855SEd Maste 
8*2ccfa855SEd Maste #include <errno.h>
9*2ccfa855SEd Maste #include <stdint.h>
10*2ccfa855SEd Maste #include <stdlib.h>
11*2ccfa855SEd Maste 
12*2ccfa855SEd Maste #include "fido.h"
13*2ccfa855SEd Maste 
14*2ccfa855SEd Maste int
fido_to_uint64(const char * str,int base,uint64_t * out)15*2ccfa855SEd Maste fido_to_uint64(const char *str, int base, uint64_t *out)
16*2ccfa855SEd Maste {
17*2ccfa855SEd Maste 	char *ep;
18*2ccfa855SEd Maste 	unsigned long long ull;
19*2ccfa855SEd Maste 
20*2ccfa855SEd Maste 	errno = 0;
21*2ccfa855SEd Maste 	ull = strtoull(str, &ep, base);
22*2ccfa855SEd Maste 	if (str == ep || *ep != '\0')
23*2ccfa855SEd Maste 		return -1;
24*2ccfa855SEd Maste 	else if (ull == ULLONG_MAX && errno == ERANGE)
25*2ccfa855SEd Maste 		return -1;
26*2ccfa855SEd Maste 	else if (ull > UINT64_MAX)
27*2ccfa855SEd Maste 		return -1;
28*2ccfa855SEd Maste 	*out = (uint64_t)ull;
29*2ccfa855SEd Maste 
30*2ccfa855SEd Maste 	return 0;
31*2ccfa855SEd Maste }
32