xref: /freebsd/lib/libcrypt/misc.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4e9a56ad5SMark Murray  * Copyright (c) 1999
5e9a56ad5SMark Murray  *      University of California.  All rights reserved.
6e9a56ad5SMark Murray  *
7e9a56ad5SMark Murray  * Redistribution and use in source and binary forms, with or without
8e9a56ad5SMark Murray  * modification, are permitted provided that the following conditions
9e9a56ad5SMark Murray  * are met:
10e9a56ad5SMark Murray  * 1. Redistributions of source code must retain the above copyright
11e9a56ad5SMark Murray  *    notice, this list of conditions and the following disclaimer.
12e9a56ad5SMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
13e9a56ad5SMark Murray  *    notice, this list of conditions and the following disclaimer in the
14e9a56ad5SMark Murray  *    documentation and/or other materials provided with the distribution.
15e9a56ad5SMark Murray  * 3. Neither the name of the author nor the names of any co-contributors
16e9a56ad5SMark Murray  *    may be used to endorse or promote products derived from this software
17e9a56ad5SMark Murray  *    without specific prior written permission.
18e9a56ad5SMark Murray  *
19e9a56ad5SMark Murray  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20e9a56ad5SMark Murray  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21e9a56ad5SMark Murray  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22e9a56ad5SMark Murray  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
23e9a56ad5SMark Murray  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24e9a56ad5SMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25e9a56ad5SMark Murray  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26e9a56ad5SMark Murray  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27e9a56ad5SMark Murray  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28e9a56ad5SMark Murray  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29e9a56ad5SMark Murray  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30e9a56ad5SMark Murray  */
31e9a56ad5SMark Murray 
32f2ac424aSMark Murray #include <sys/types.h>
33f2ac424aSMark Murray 
34f2ac424aSMark Murray #include "crypt.h"
35f2ac424aSMark Murray 
36f2ac424aSMark Murray static char itoa64[] =		/* 0 ... 63 => ascii - 64 */
37e9a56ad5SMark Murray 	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
38e9a56ad5SMark Murray 
39e9a56ad5SMark Murray void
_crypt_to64(char * s,u_long v,int n)40f2ac424aSMark Murray _crypt_to64(char *s, u_long v, int n)
41e9a56ad5SMark Murray {
42e9a56ad5SMark Murray 	while (--n >= 0) {
43e9a56ad5SMark Murray 		*s++ = itoa64[v&0x3f];
44e9a56ad5SMark Murray 		v >>= 6;
45e9a56ad5SMark Murray 	}
46e9a56ad5SMark Murray }
473d6f63c0SMark Murray 
483d6f63c0SMark Murray void
b64_from_24bit(uint8_t B2,uint8_t B1,uint8_t B0,int n,char ** cp)495f521d7bSEd Schouten b64_from_24bit(uint8_t B2, uint8_t B1, uint8_t B0, int n, char **cp)
503d6f63c0SMark Murray {
513d6f63c0SMark Murray 	uint32_t w;
523d6f63c0SMark Murray 	int i;
533d6f63c0SMark Murray 
543d6f63c0SMark Murray 	w = (B2 << 16) | (B1 << 8) | B0;
553d6f63c0SMark Murray 	for (i = 0; i < n; i++) {
563d6f63c0SMark Murray 		**cp = itoa64[w&0x3f];
573d6f63c0SMark Murray 		(*cp)++;
583d6f63c0SMark Murray 		w >>= 6;
593d6f63c0SMark Murray 	}
603d6f63c0SMark Murray }
61