uuencode.c revision 92555
137Srgrimes/*
237Srgrimes * Copyright (c) 2000 Markus Friedl.  All rights reserved.
337Srgrimes *
4705Swollman * Redistribution and use in source and binary forms, with or without
5705Swollman * modification, are permitted provided that the following conditions
6705Swollman * are met:
739139Sobrien * 1. Redistributions of source code must retain the above copyright
846414Sghelmer *    notice, this list of conditions and the following disclaimer.
980266Sdougb * 2. Redistributions in binary form must reproduce the above copyright
1080945Sdougb *    notice, this list of conditions and the following disclaimer in the
1180266Sdougb *    documentation and/or other materials provided with the distribution.
1239139Sobrien *
1339139Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1439139Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1539139Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
167685Sache * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
177685Sache * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1837Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1950472Speter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20705Swollman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21705Swollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
227685Sache * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237685Sache */
2422009Sphk
257685Sache#include "includes.h"
267685SacheRCSID("$OpenBSD: uuencode.c,v 1.15 2002/03/04 17:27:39 stevesk Exp $");
2722009Sphk
287685Sache#include "xmalloc.h"
297685Sache#include "uuencode.h"
307685Sache
317685Sache#include <resolv.h>
3222009Sphk
337685Sacheint
347685Sacheuuencode(u_char *src, u_int srclength,
3522009Sphk    char *target, size_t targsize)
367685Sache{
377685Sache	return __b64_ntop(src, srclength, target, targsize);
387685Sache}
397685Sache
407685Sacheint
417685Sacheuudecode(const char *src, u_char *target, size_t targsize)
427685Sache{
437685Sache	int len;
447685Sache	char *encoded, *p;
457685Sache
467685Sache	/* copy the 'readonly' source */
477685Sache	encoded = xstrdup(src);
487685Sache	/* skip whitespace and data */
497685Sache	for (p = encoded; *p == ' ' || *p == '\t'; p++)
507685Sache		;
517685Sache	for (; *p != '\0' && *p != ' ' && *p != '\t'; p++)
527685Sache		;
537685Sache	/* and remove trailing whitespace because __b64_pton needs this */
54131994Scperciva	*p = '\0';
55131994Scperciva	len = __b64_pton(encoded, target, targsize);
567685Sache	xfree(encoded);
577685Sache	return len;
587685Sache}
597685Sache
607685Sachevoid
617685Sachedump_base64(FILE *fp, u_char *data, u_int len)
627685Sache{
637685Sache	u_char *buf = xmalloc(2*len);
647685Sache	int i, n;
657685Sache
667685Sache	n = uuencode(data, len, buf, 2*len);
677685Sache	for (i = 0; i < n; i++) {
687685Sache		fprintf(fp, "%c", buf[i]);
697685Sache		if (i % 70 == 69)
707685Sache			fprintf(fp, "\n");
717685Sache	}
727685Sache	if (i % 70 != 69)
737685Sache		fprintf(fp, "\n");
747685Sache	xfree(buf);
757685Sache}
767685Sache