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