xref: /onnv-gate/usr/src/cmd/ssh/libssh/common/uuencode.c (revision 5243:903f035961d7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
50Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
60Sstevel@tonic-gate  * are met:
70Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
80Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
90Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
100Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
110Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
190Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include "includes.h"
260Sstevel@tonic-gate RCSID("$OpenBSD: uuencode.c,v 1.16 2002/09/09 14:54:15 markus Exp $");
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include "xmalloc.h"
310Sstevel@tonic-gate #include "uuencode.h"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate int
uuencode(u_char * src,u_int srclength,char * target,size_t targsize)340Sstevel@tonic-gate uuencode(u_char *src, u_int srclength,
350Sstevel@tonic-gate     char *target, size_t targsize)
360Sstevel@tonic-gate {
370Sstevel@tonic-gate 	return __b64_ntop(src, srclength, target, targsize);
380Sstevel@tonic-gate }
390Sstevel@tonic-gate 
400Sstevel@tonic-gate int
uudecode(const char * src,u_char * target,size_t targsize)410Sstevel@tonic-gate uudecode(const char *src, u_char *target, size_t targsize)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	int len;
440Sstevel@tonic-gate 	char *encoded, *p;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	/* copy the 'readonly' source */
470Sstevel@tonic-gate 	encoded = xstrdup(src);
480Sstevel@tonic-gate 	/* skip whitespace and data */
490Sstevel@tonic-gate 	for (p = encoded; *p == ' ' || *p == '\t'; p++)
500Sstevel@tonic-gate 		;
510Sstevel@tonic-gate 	for (; *p != '\0' && *p != ' ' && *p != '\t'; p++)
520Sstevel@tonic-gate 		;
530Sstevel@tonic-gate 	/* and remove trailing whitespace because __b64_pton needs this */
540Sstevel@tonic-gate 	*p = '\0';
55*5243Sjp161948 	len = __b64_pton((u_char *) encoded, target, targsize);
560Sstevel@tonic-gate 	xfree(encoded);
570Sstevel@tonic-gate 	return len;
580Sstevel@tonic-gate }
590Sstevel@tonic-gate 
600Sstevel@tonic-gate void
dump_base64(FILE * fp,u_char * data,u_int len)610Sstevel@tonic-gate dump_base64(FILE *fp, u_char *data, u_int len)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	char *buf = xmalloc(2*len);
640Sstevel@tonic-gate 	int i, n;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	n = uuencode(data, len, buf, 2*len);
670Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
680Sstevel@tonic-gate 		fprintf(fp, "%c", buf[i]);
690Sstevel@tonic-gate 		if (i % 70 == 69)
700Sstevel@tonic-gate 			fprintf(fp, "\n");
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 	if (i % 70 != 69)
730Sstevel@tonic-gate 		fprintf(fp, "\n");
740Sstevel@tonic-gate 	xfree(buf);
750Sstevel@tonic-gate }
76