Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 3 | */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 4 | #include "includes.h" |
| 5 | #include "xmalloc.h" |
| 6 | |
Damien Miller | bf7f466 | 2000-06-23 10:16:38 +1000 | [diff] [blame] | 7 | RCSID("$OpenBSD: uuencode.c,v 1.6 2000/06/22 23:55:00 djm Exp $"); |
| 8 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 9 | int |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 10 | uuencode(unsigned char *src, unsigned int srclength, |
| 11 | char *target, size_t targsize) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 12 | { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 13 | return __b64_ntop(src, srclength, target, targsize); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | int |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 17 | uudecode(const char *src, unsigned char *target, size_t targsize) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 18 | { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 19 | int len; |
| 20 | char *encoded, *p; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 21 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 22 | /* copy the 'readonly' source */ |
| 23 | encoded = xstrdup(src); |
| 24 | /* skip whitespace and data */ |
| 25 | for (p = encoded; *p == ' ' || *p == '\t'; p++) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 26 | ; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 27 | 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 Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void |
| 37 | dump_base64(FILE *fp, unsigned char *data, int len) |
| 38 | { |
| 39 | unsigned char *buf = xmalloc(2*len); |
| 40 | int i, n; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 41 | n = uuencode(data, len, buf, 2*len); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 42 | 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 | } |