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