uuencode.c revision 162853
1276577Sdelphij/* $OpenBSD: uuencode.c,v 1.24 2006/08/03 03:34:42 deraadt Exp $ */ 2276577Sdelphij/* 3276577Sdelphij * Copyright (c) 2000 Markus Friedl. All rights reserved. 4276577Sdelphij * 5276577Sdelphij * Redistribution and use in source and binary forms, with or without 6276577Sdelphij * modification, are permitted provided that the following conditions 7276577Sdelphij * are met: 8276577Sdelphij * 1. Redistributions of source code must retain the above copyright 9276577Sdelphij * notice, this list of conditions and the following disclaimer. 10276577Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 11276577Sdelphij * notice, this list of conditions and the following disclaimer in the 12276577Sdelphij * documentation and/or other materials provided with the distribution. 13276577Sdelphij * 14276577Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15276577Sdelphij * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16276577Sdelphij * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17276577Sdelphij * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18276577Sdelphij * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19276577Sdelphij * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20276577Sdelphij * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21276577Sdelphij * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22276577Sdelphij * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23276577Sdelphij * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24276577Sdelphij */ 25276577Sdelphij 26276577Sdelphij#include "includes.h" 27275698Sdelphij 28275698Sdelphij#include <sys/types.h> 29275698Sdelphij#include <netinet/in.h> 30275698Sdelphij#include <resolv.h> 31275698Sdelphij#include <stdio.h> 32275698Sdelphij 33275698Sdelphij#include "xmalloc.h" 34275698Sdelphij#include "uuencode.h" 35275698Sdelphij 36275698Sdelphijint 37275698Sdelphijuuencode(const u_char *src, u_int srclength, 38275698Sdelphij char *target, size_t targsize) 39275698Sdelphij{ 40275698Sdelphij return __b64_ntop(src, srclength, target, targsize); 41275698Sdelphij} 42275698Sdelphij 43275698Sdelphijint 44275698Sdelphijuudecode(const char *src, u_char *target, size_t targsize) 45275698Sdelphij{ 46275698Sdelphij int len; 47275698Sdelphij char *encoded, *p; 48275698Sdelphij 49275698Sdelphij /* copy the 'readonly' source */ 50275698Sdelphij encoded = xstrdup(src); 51275698Sdelphij /* skip whitespace and data */ 52276577Sdelphij for (p = encoded; *p == ' ' || *p == '\t'; p++) 53276577Sdelphij ; 54275698Sdelphij for (; *p != '\0' && *p != ' ' && *p != '\t'; p++) 55275698Sdelphij ; 56275698Sdelphij /* and remove trailing whitespace because __b64_pton needs this */ 57275698Sdelphij *p = '\0'; 58275698Sdelphij len = __b64_pton(encoded, target, targsize); 59275698Sdelphij xfree(encoded); 60275698Sdelphij return len; 61275698Sdelphij} 62275698Sdelphij 63275698Sdelphijvoid 64275698Sdelphijdump_base64(FILE *fp, u_char *data, u_int len) 65275698Sdelphij{ 66275698Sdelphij char *buf; 67275698Sdelphij int i, n; 68275698Sdelphij 69275698Sdelphij if (len > 65536) { 70275698Sdelphij fprintf(fp, "dump_base64: len > 65536\n"); 71275698Sdelphij return; 72275698Sdelphij } 73275698Sdelphij buf = xmalloc(2*len); 74275698Sdelphij n = uuencode(data, len, buf, 2*len); 75275698Sdelphij for (i = 0; i < n; i++) { 76275698Sdelphij fprintf(fp, "%c", buf[i]); 77275698Sdelphij if (i % 70 == 69) 78275698Sdelphij fprintf(fp, "\n"); 79275698Sdelphij } 80275698Sdelphij if (i % 70 != 69) 81275698Sdelphij fprintf(fp, "\n"); 82275698Sdelphij xfree(buf); 83275698Sdelphij} 84275698Sdelphij