1 /* $NetBSD: util.c,v 1.3 2021/10/12 15:25:39 nia Exp $ */ 2 /* 3 * Copyright 1997 Niels Provos <provos@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.3 2021/10/12 15:25:39 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 /* traditional unix "B64" encoding */ 45 static const unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ 46 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 47 48 /* standard base64 encoding, used by Argon2 */ 49 static const unsigned char itoabase64[] = 50 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 51 52 crypt_private int 53 getnum(const char *str, size_t *num) 54 { 55 char *ep; 56 unsigned long rv; 57 58 if (str == NULL) { 59 *num = 0; 60 return 0; 61 } 62 63 rv = strtoul(str, &ep, 0); 64 65 if (str == ep || *ep) { 66 errno = EINVAL; 67 return -1; 68 } 69 70 if (errno == ERANGE && rv == ULONG_MAX) 71 return -1; 72 *num = (size_t)rv; 73 return 0; 74 } 75 76 crypt_private void 77 __crypt_to64(char *s, uint32_t v, int n) 78 { 79 80 while (--n >= 0) { 81 *s++ = itoa64[v & 0x3f]; 82 v >>= 6; 83 } 84 } 85 86 crypt_private void 87 __crypt_tobase64(char *s, uint32_t v, int n) 88 { 89 90 while (--n >= 0) { 91 *s++ = itoabase64[v & 0x3f]; 92 v >>= 6; 93 } 94 } 95