util.c revision 1.2 1 /* $NetBSD: util.c,v 1.2 2021/10/12 13:24:00 nia Exp $ */
2 /*
3 * Copyright 1997 Niels Provos <provos (at) physnet.uni-hamburg.de>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Niels Provos.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 #if !defined(lint)
33 __RCSID("$NetBSD: util.c,v 1.2 2021/10/12 13:24:00 nia Exp $");
34 #endif /* not lint */
35
36 #include <sys/types.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <stddef.h>
40 #include <stdlib.h>
41
42 #include "crypt.h"
43
44 static const unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
45 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
46
47 crypt_private int
48 getnum(const char *str, size_t *num)
49 {
50 char *ep;
51 unsigned long rv;
52
53 if (str == NULL) {
54 *num = 0;
55 return 0;
56 }
57
58 rv = strtoul(str, &ep, 0);
59
60 if (str == ep || *ep) {
61 errno = EINVAL;
62 return -1;
63 }
64
65 if (errno == ERANGE && rv == ULONG_MAX)
66 return -1;
67 *num = (size_t)rv;
68 return 0;
69 }
70
71 void
72 __crypt_to64(char *s, uint32_t v, int n)
73 {
74
75 while (--n >= 0) {
76 *s++ = itoa64[v & 0x3f];
77 v >>= 6;
78 }
79 }
80