uuencode.c revision 106121
1170754Sdelphij/* 2170754Sdelphij * Copyright (c) 2000 Markus Friedl. All rights reserved. 3170754Sdelphij * 4170754Sdelphij * Redistribution and use in source and binary forms, with or without 5170754Sdelphij * modification, are permitted provided that the following conditions 6170754Sdelphij * are met: 7170754Sdelphij * 1. Redistributions of source code must retain the above copyright 8170754Sdelphij * notice, this list of conditions and the following disclaimer. 9170754Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 10170754Sdelphij * notice, this list of conditions and the following disclaimer in the 11170754Sdelphij * documentation and/or other materials provided with the distribution. 12170754Sdelphij * 13170754Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14170754Sdelphij * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15170754Sdelphij * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16170754Sdelphij * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17170754Sdelphij * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18170754Sdelphij * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19170754Sdelphij * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20170754Sdelphij * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21170754Sdelphij * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22170754Sdelphij * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23170754Sdelphij */ 24170754Sdelphij 25170754Sdelphij#include "includes.h" 26170754SdelphijRCSID("$OpenBSD: uuencode.c,v 1.16 2002/09/09 14:54:15 markus Exp $"); 27170754Sdelphij 28170754Sdelphij#include "xmalloc.h" 29170754Sdelphij#include "uuencode.h" 30170754Sdelphij 31170754Sdelphijint 32170754Sdelphijuuencode(u_char *src, u_int srclength, 33170754Sdelphij char *target, size_t targsize) 34170754Sdelphij{ 35170754Sdelphij return __b64_ntop(src, srclength, target, targsize); 36170754Sdelphij} 37170754Sdelphij 38170754Sdelphijint 39170754Sdelphijuudecode(const char *src, u_char *target, size_t targsize) 40170754Sdelphij{ 41170754Sdelphij int len; 42170754Sdelphij char *encoded, *p; 43170754Sdelphij 44170754Sdelphij /* copy the 'readonly' source */ 45170754Sdelphij encoded = xstrdup(src); 46170754Sdelphij /* skip whitespace and data */ 47170754Sdelphij for (p = encoded; *p == ' ' || *p == '\t'; p++) 48170754Sdelphij ; 49170754Sdelphij for (; *p != '\0' && *p != ' ' && *p != '\t'; p++) 50170754Sdelphij ; 51170754Sdelphij /* and remove trailing whitespace because __b64_pton needs this */ 52170754Sdelphij *p = '\0'; 53170754Sdelphij len = __b64_pton(encoded, target, targsize); 54170754Sdelphij xfree(encoded); 55170754Sdelphij return len; 56170754Sdelphij} 57170754Sdelphij 58170754Sdelphijvoid 59170754Sdelphijdump_base64(FILE *fp, u_char *data, u_int len) 60170754Sdelphij{ 61170754Sdelphij char *buf = xmalloc(2*len); 62170754Sdelphij int i, n; 63170754Sdelphij 64170754Sdelphij n = uuencode(data, len, buf, 2*len); 65170754Sdelphij for (i = 0; i < n; i++) { 66170754Sdelphij fprintf(fp, "%c", buf[i]); 67 if (i % 70 == 69) 68 fprintf(fp, "\n"); 69 } 70 if (i % 70 != 69) 71 fprintf(fp, "\n"); 72 xfree(buf); 73} 74