Home | History | Annotate | Line # | Download | only in libcrypt
      1 /*	$NetBSD: util.c,v 1.4 2025/12/31 13:02:20 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. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 #include <sys/cdefs.h>
     29 #if !defined(lint)
     30 __RCSID("$NetBSD: util.c,v 1.4 2025/12/31 13:02:20 nia Exp $");
     31 #endif /* not lint */
     32 
     33 #include <sys/types.h>
     34 #include <errno.h>
     35 #include <limits.h>
     36 #include <stddef.h>
     37 #include <stdlib.h>
     38 
     39 #include "crypt.h"
     40 
     41 /* traditional unix "B64" encoding */
     42 static const unsigned char itoa64[] =		/* 0 ... 63 => ascii - 64 */
     43 	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     44 
     45 /* standard base64 encoding, used by Argon2 */
     46 static const unsigned char itoabase64[] =
     47 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
     48 
     49 crypt_private int
     50 getnum(const char *str, size_t *num)
     51 {
     52 	char *ep;
     53 	unsigned long rv;
     54 
     55 	if (str == NULL) {
     56 		*num = 0;
     57 		return 0;
     58 	}
     59 
     60 	rv = strtoul(str, &ep, 0);
     61 
     62 	if (str == ep || *ep) {
     63 		errno = EINVAL;
     64 		return -1;
     65 	}
     66 
     67 	if (errno == ERANGE && rv == ULONG_MAX)
     68 		return -1;
     69 	*num = (size_t)rv;
     70 	return 0;
     71 }
     72 
     73 crypt_private void
     74 __crypt_to64(char *s, uint32_t v, int n)
     75 {
     76 
     77 	while (--n >= 0) {
     78 		*s++ = itoa64[v & 0x3f];
     79 		v >>= 6;
     80 	}
     81 }
     82 
     83 crypt_private void
     84 __crypt_tobase64(char *s, uint32_t v, int n)
     85 {
     86 
     87 	while (--n >= 0) {
     88 		*s++ = itoabase64[v & 0x3f];
     89 		v >>= 6;
     90 	}
     91 }
     92