xref: /openbsd-src/lib/libc/stdlib/a64l.c (revision d8bc04e435448c902a8c004581e7486635bbff02)
1 /*
2  * Written by J.T. Conklin <jtc@netbsd.org>.
3  * Public domain.
4  */
5 
6 #if defined(LIBC_SCCS) && !defined(lint)
7 static char *rcsid = "$OpenBSD: a64l.c,v 1.4 2005/03/30 18:51:49 pat Exp $";
8 #endif /* LIBC_SCCS and not lint */
9 
10 #include <errno.h>
11 #include <stdlib.h>
12 
13 long
14 a64l(const char *s)
15 {
16 	long value, digit, shift;
17 	int i;
18 
19 	if (s == NULL) {
20 		errno = EINVAL;
21 		return(-1L);
22 	}
23 
24 	value = 0;
25 	shift = 0;
26 	for (i = 0; *s && i < 6; i++, s++) {
27 		if (*s >= '.' && *s <= '/')
28 			digit = *s - '.';
29 		else if (*s >= '0' && *s <= '9')
30 			digit = *s - '0' + 2;
31 		else if (*s >= 'A' && *s <= 'Z')
32 			digit = *s - 'A' + 12;
33 		else if (*s >= 'a' && *s <= 'z')
34 			digit = *s - 'a' + 38;
35 		else {
36 			errno = EINVAL;
37 			return(-1L);
38 		}
39 
40 		value |= digit << shift;
41 		shift += 6;
42 	}
43 
44 	return(value);
45 }
46