uuencode.c revision 255767
1114402Sru/* $OpenBSD: uuencode.c,v 1.27 2013/05/17 00:13:14 djm Exp $ */ 2151497Sru/* 3114402Sru * Copyright (c) 2000 Markus Friedl. All rights reserved. 4114402Sru * 5114402Sru * Redistribution and use in source and binary forms, with or without 6114402Sru * modification, are permitted provided that the following conditions 7114402Sru * are met: 8114402Sru * 1. Redistributions of source code must retain the above copyright 9114402Sru * notice, this list of conditions and the following disclaimer. 10114402Sru * 2. Redistributions in binary form must reproduce the above copyright 11114402Sru * notice, this list of conditions and the following disclaimer in the 12114402Sru * documentation and/or other materials provided with the distribution. 13114402Sru * 14114402Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15114402Sru * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16114402Sru * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17114402Sru * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18114402Sru * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19151497Sru * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20114402Sru * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21114402Sru * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22114402Sru * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23114402Sru * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24114402Sru */ 25114402Sru 26114402Sru#include "includes.h" 27114402Sru 28114402Sru#include <sys/types.h> 29114402Sru#include <netinet/in.h> 30151497Sru#include <resolv.h> 31114402Sru#include <stdio.h> 32151497Sru#include <stdlib.h> 33151497Sru 34114402Sru#include "xmalloc.h" 35114402Sru#include "uuencode.h" 36114402Sru 37114402Sru/* 38114402Sru * Encode binary 'src' of length 'srclength', writing base64-encoded text 39114402Sru * to 'target' of size 'targsize'. Will always nul-terminate 'target'. 40114402Sru * Returns the number of bytes stored in 'target' or -1 on error (inc. 41114402Sru * 'targsize' too small). 42151497Sru */ 43114402Sruint 44114402Sruuuencode(const u_char *src, u_int srclength, 45151497Sru char *target, size_t targsize) 46114402Sru{ 47114402Sru return __b64_ntop(src, srclength, target, targsize); 48151497Sru} 49151497Sru 50151497Sru/* 51114402Sru * Decode base64-encoded 'src' into buffer 'target' of 'targsize' bytes. 52151497Sru * Will skip leading and trailing whitespace. Returns the number of bytes 53151497Sru * stored in 'target' or -1 on error (inc. targsize too small). 54114402Sru */ 55151497Sruint 56151497Sruuudecode(const char *src, u_char *target, size_t targsize) 57151497Sru{ 58151497Sru int len; 59151497Sru char *encoded, *p; 60151497Sru 61151497Sru /* copy the 'readonly' source */ 62151497Sru encoded = xstrdup(src); 63114402Sru /* skip whitespace and data */ 64114402Sru for (p = encoded; *p == ' ' || *p == '\t'; p++) 65114402Sru ; 66151497Sru for (; *p != '\0' && *p != ' ' && *p != '\t'; p++) 67114402Sru ; 68114402Sru /* and remove trailing whitespace because __b64_pton needs this */ 69114402Sru *p = '\0'; 70114402Sru len = __b64_pton(encoded, target, targsize); 71114402Sru free(encoded); 72114402Sru return len; 73114402Sru} 74151497Sru 75151497Sruvoid 76151497Srudump_base64(FILE *fp, const u_char *data, u_int len) 77114402Sru{ 78151497Sru char *buf; 79114402Sru int i, n; 80151497Sru 81114402Sru if (len > 65536) { 82114402Sru fprintf(fp, "dump_base64: len > 65536\n"); 83151497Sru return; 84114402Sru } 85114402Sru buf = xmalloc(2*len); 86114402Sru n = uuencode(data, len, buf, 2*len); 87114402Sru for (i = 0; i < n; i++) { 88151497Sru fprintf(fp, "%c", buf[i]); 89151497Sru if (i % 70 == 69) 90151497Sru fprintf(fp, "\n"); 91151497Sru } 92151497Sru if (i % 70 != 69) 93114402Sru fprintf(fp, "\n"); 94151497Sru free(buf); 95151497Sru} 96114402Sru