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