blob: 27ba6559e0d2e37a30e219e1f5b8555463f9fcb3 [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
Damien Millere247cc42000-05-07 12:03:14 +10002 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millereba71ba2000-04-29 23:57:08 +10003 */
Damien Millereba71ba2000-04-29 23:57:08 +10004#include "includes.h"
5#include "xmalloc.h"
6
Damien Millerbf7f4662000-06-23 10:16:38 +10007RCSID("$OpenBSD: uuencode.c,v 1.6 2000/06/22 23:55:00 djm Exp $");
8
Damien Millereba71ba2000-04-29 23:57:08 +10009int
Damien Millere247cc42000-05-07 12:03:14 +100010uuencode(unsigned char *src, unsigned int srclength,
11 char *target, size_t targsize)
Damien Millereba71ba2000-04-29 23:57:08 +100012{
Damien Millere247cc42000-05-07 12:03:14 +100013 return __b64_ntop(src, srclength, target, targsize);
Damien Millereba71ba2000-04-29 23:57:08 +100014}
15
16int
Damien Millere247cc42000-05-07 12:03:14 +100017uudecode(const char *src, unsigned char *target, size_t targsize)
Damien Millereba71ba2000-04-29 23:57:08 +100018{
Damien Millere247cc42000-05-07 12:03:14 +100019 int len;
20 char *encoded, *p;
Damien Millereba71ba2000-04-29 23:57:08 +100021
Damien Millere247cc42000-05-07 12:03:14 +100022 /* copy the 'readonly' source */
23 encoded = xstrdup(src);
24 /* skip whitespace and data */
25 for (p = encoded; *p == ' ' || *p == '\t'; p++)
Damien Millereba71ba2000-04-29 23:57:08 +100026 ;
Damien Millere247cc42000-05-07 12:03:14 +100027 for (; *p != '\0' && *p != ' ' && *p != '\t'; p++)
28 ;
29 /* and remote trailing whitespace because __b64_pton needs this */
30 *p = '\0';
31 len = __b64_pton(encoded, target, targsize);
32 xfree(encoded);
33 return len;
Damien Millereba71ba2000-04-29 23:57:08 +100034}
35
36void
37dump_base64(FILE *fp, unsigned char *data, int len)
38{
39 unsigned char *buf = xmalloc(2*len);
40 int i, n;
Damien Millere247cc42000-05-07 12:03:14 +100041 n = uuencode(data, len, buf, 2*len);
Damien Millereba71ba2000-04-29 23:57:08 +100042 for (i = 0; i < n; i++) {
43 fprintf(fp, "%c", buf[i]);
44 if (i % 70 == 69)
45 fprintf(fp, "\n");
46 }
47 if (i % 70 != 69)
48 fprintf(fp, "\n");
49 xfree(buf);
50}