uuencode.c revision 181110
11590Srgrimes/* $OpenBSD: uuencode.c,v 1.24 2006/08/03 03:34:42 deraadt Exp $ */ 21590Srgrimes/* 31590Srgrimes * Copyright (c) 2000 Markus Friedl. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 141590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 151590Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 161590Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 171590Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 181590Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 191590Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 201590Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 211590Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 221590Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 231590Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 241590Srgrimes */ 251590Srgrimes 261590Srgrimes#include "includes.h" 271590Srgrimes 281590Srgrimes#include <sys/types.h> 291590Srgrimes#include <netinet/in.h> 301590Srgrimes#include <resolv.h> 3127648Scharnier#include <stdio.h> 321590Srgrimes 331590Srgrimes#include "xmalloc.h" 341590Srgrimes#include "uuencode.h" 351590Srgrimes 361590Srgrimesint 3727648Scharnieruuencode(const u_char *src, u_int srclength, 381590Srgrimes char *target, size_t targsize) 3927648Scharnier{ 401590Srgrimes return __b64_ntop(src, srclength, target, targsize); 411590Srgrimes} 4294503Scharnier 4394503Scharnierint 4494503Scharnieruudecode(const char *src, u_char *target, size_t targsize) 4527648Scharnier{ 46106293Stjr int len; 471590Srgrimes char *encoded, *p; 4827648Scharnier 4933648Sjb /* copy the 'readonly' source */ 501590Srgrimes encoded = xstrdup(src); 511590Srgrimes /* skip whitespace and data */ 521590Srgrimes for (p = encoded; *p == ' ' || *p == '\t'; p++) 531590Srgrimes ; 541590Srgrimes for (; *p != '\0' && *p != ' ' && *p != '\t'; p++) 551590Srgrimes ; 561590Srgrimes /* and remove trailing whitespace because __b64_pton needs this */ 571590Srgrimes *p = '\0'; 581590Srgrimes len = __b64_pton(encoded, target, targsize); 591590Srgrimes xfree(encoded); 601590Srgrimes return len; 611590Srgrimes} 621590Srgrimes 631590Srgrimesvoid 641590Srgrimesdump_base64(FILE *fp, u_char *data, u_int len) 651590Srgrimes{ 661590Srgrimes char *buf; 671590Srgrimes int i, n; 681590Srgrimes 691590Srgrimes if (len > 65536) { 701590Srgrimes fprintf(fp, "dump_base64: len > 65536\n"); 711590Srgrimes return; 721590Srgrimes } 731590Srgrimes buf = xmalloc(2*len); 741590Srgrimes n = uuencode(data, len, buf, 2*len); 751590Srgrimes for (i = 0; i < n; i++) { 761590Srgrimes fprintf(fp, "%c", buf[i]); 771590Srgrimes if (i % 70 == 69) 781590Srgrimes fprintf(fp, "\n"); 791590Srgrimes } 801590Srgrimes if (i % 70 != 69) 811590Srgrimes fprintf(fp, "\n"); 8292921Simp xfree(buf); 8392921Simp} 8495642Smarkm