1139826Simp/* $OpenBSD: uuencode.c,v 1.27 2013/05/17 00:13:14 djm Exp $ */
253541Sshin/*
353541Sshin * Copyright (c) 2000 Markus Friedl.  All rights reserved.
453541Sshin *
553541Sshin * Redistribution and use in source and binary forms, with or without
653541Sshin * modification, are permitted provided that the following conditions
753541Sshin * are met:
853541Sshin * 1. Redistributions of source code must retain the above copyright
953541Sshin *    notice, this list of conditions and the following disclaimer.
1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1153541Sshin *    notice, this list of conditions and the following disclaimer in the
1253541Sshin *    documentation and/or other materials provided with the distribution.
1353541Sshin *
1453541Sshin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1553541Sshin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1653541Sshin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1753541Sshin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1853541Sshin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1953541Sshin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2053541Sshin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2153541Sshin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2253541Sshin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2353541Sshin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2453541Sshin */
2553541Sshin
2653541Sshin#include "includes.h"
2753541Sshin
28174510Sobrien#include <sys/types.h>
29174510Sobrien#include <netinet/in.h>
30174510Sobrien#include <resolv.h>
3153541Sshin#include <stdio.h>
3253541Sshin#include <stdlib.h>
3353541Sshin
3453541Sshin#include "xmalloc.h"
3553541Sshin#include "uuencode.h"
3653541Sshin
3753541Sshin/*
3853541Sshin * Encode binary 'src' of length 'srclength', writing base64-encoded text
3953541Sshin * to 'target' of size 'targsize'. Will always nul-terminate 'target'.
4053541Sshin * Returns the number of bytes stored in 'target' or -1 on error (inc.
4153541Sshin * 'targsize' too small).
4253541Sshin */
4362587Sitojunint
4453541Sshinuuencode(const u_char *src, u_int srclength,
4553541Sshin    char *target, size_t targsize)
4653541Sshin{
4753541Sshin	return __b64_ntop(src, srclength, target, targsize);
4853541Sshin}
4953541Sshin
5053541Sshin/*
5153541Sshin * Decode base64-encoded 'src' into buffer 'target' of 'targsize' bytes.
5253541Sshin * Will skip leading and trailing whitespace. Returns the number of bytes
5353541Sshin * stored in 'target' or -1 on error (inc. targsize too small).
5453541Sshin */
5553541Sshinint
5662587Sitojunuudecode(const char *src, u_char *target, size_t targsize)
5753541Sshin{
5853541Sshin	int len;
5953541Sshin	char *encoded, *p;
6062587Sitojun
6162587Sitojun	/* copy the 'readonly' source */
6253541Sshin	encoded = xstrdup(src);
6353541Sshin	/* skip whitespace and data */
6453541Sshin	for (p = encoded; *p == ' ' || *p == '\t'; p++)
6553541Sshin		;
6662587Sitojun	for (; *p != '\0' && *p != ' ' && *p != '\t'; p++)
6753541Sshin		;
6853541Sshin	/* and remove trailing whitespace because __b64_pton needs this */
6962587Sitojun	*p = '\0';
70	len = __b64_pton(encoded, target, targsize);
71	free(encoded);
72	return len;
73}
74
75void
76dump_base64(FILE *fp, const u_char *data, u_int len)
77{
78	char *buf;
79	int i, n;
80
81	if (len > 65536) {
82		fprintf(fp, "dump_base64: len > 65536\n");
83		return;
84	}
85	buf = xmalloc(2*len);
86	n = uuencode(data, len, buf, 2*len);
87	for (i = 0; i < n; i++) {
88		fprintf(fp, "%c", buf[i]);
89		if (i % 70 == 69)
90			fprintf(fp, "\n");
91	}
92	if (i % 70 != 69)
93		fprintf(fp, "\n");
94	free(buf);
95}
96