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