xref: /titanic_50/usr/src/cmd/ssh/libssh/common/uuencode.c (revision 442d23f49355a5d0694c758975be57af39f91a61)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
57c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
67c478bd9Sstevel@tonic-gate  * are met:
77c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
87c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
97c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
107c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
117c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
147c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
157c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
167c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
177c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
187c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
197c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
207c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
227c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include "includes.h"
267c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: uuencode.c,v 1.16 2002/09/09 14:54:15 markus Exp $");
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "xmalloc.h"
317c478bd9Sstevel@tonic-gate #include "uuencode.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate int
uuencode(u_char * src,u_int srclength,char * target,size_t targsize)347c478bd9Sstevel@tonic-gate uuencode(u_char *src, u_int srclength,
357c478bd9Sstevel@tonic-gate     char *target, size_t targsize)
367c478bd9Sstevel@tonic-gate {
377c478bd9Sstevel@tonic-gate 	return __b64_ntop(src, srclength, target, targsize);
387c478bd9Sstevel@tonic-gate }
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate int
uudecode(const char * src,u_char * target,size_t targsize)417c478bd9Sstevel@tonic-gate uudecode(const char *src, u_char *target, size_t targsize)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	int len;
447c478bd9Sstevel@tonic-gate 	char *encoded, *p;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	/* copy the 'readonly' source */
477c478bd9Sstevel@tonic-gate 	encoded = xstrdup(src);
487c478bd9Sstevel@tonic-gate 	/* skip whitespace and data */
497c478bd9Sstevel@tonic-gate 	for (p = encoded; *p == ' ' || *p == '\t'; p++)
507c478bd9Sstevel@tonic-gate 		;
517c478bd9Sstevel@tonic-gate 	for (; *p != '\0' && *p != ' ' && *p != '\t'; p++)
527c478bd9Sstevel@tonic-gate 		;
537c478bd9Sstevel@tonic-gate 	/* and remove trailing whitespace because __b64_pton needs this */
547c478bd9Sstevel@tonic-gate 	*p = '\0';
55*442d23f4Sjp161948 	len = __b64_pton((u_char *) encoded, target, targsize);
567c478bd9Sstevel@tonic-gate 	xfree(encoded);
577c478bd9Sstevel@tonic-gate 	return len;
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate void
dump_base64(FILE * fp,u_char * data,u_int len)617c478bd9Sstevel@tonic-gate dump_base64(FILE *fp, u_char *data, u_int len)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	char *buf = xmalloc(2*len);
647c478bd9Sstevel@tonic-gate 	int i, n;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	n = uuencode(data, len, buf, 2*len);
677c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
687c478bd9Sstevel@tonic-gate 		fprintf(fp, "%c", buf[i]);
697c478bd9Sstevel@tonic-gate 		if (i % 70 == 69)
707c478bd9Sstevel@tonic-gate 			fprintf(fp, "\n");
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 	if (i % 70 != 69)
737c478bd9Sstevel@tonic-gate 		fprintf(fp, "\n");
747c478bd9Sstevel@tonic-gate 	xfree(buf);
757c478bd9Sstevel@tonic-gate }
76