Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 1 | /* uuencode.c -- uuencode utility. |
| 2 | * Copyright (C) 1994, 1995 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This product is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2, or (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | * This product is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this product; see the file COPYING. If not, write to |
| 16 | * the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
| 17 | * 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | /* Copyright (c) 1983 Regents of the University of California. |
| 21 | * All rights reserved. |
| 22 | * |
| 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: |
| 26 | * 1. Redistributions of source code must retain the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer. |
| 28 | * 2. Redistributions in binary form must reproduce the above copyright |
| 29 | * notice, this list of conditions and the following disclaimer in the |
| 30 | * documentation and/or other materials provided with the distribution. |
| 31 | * 3. All advertising materials mentioning features or use of this software |
| 32 | * must display the following acknowledgement: |
| 33 | * This product includes software developed by the University of |
| 34 | * California, Berkeley and its contributors. |
| 35 | * 4. Neither the name of the University nor the names of its contributors |
| 36 | * may be used to endorse or promote products derived from this software |
| 37 | * without specific prior written permission. |
| 38 | * |
| 39 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 40 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 42 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 43 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 44 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 45 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 47 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 48 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 49 | * SUCH DAMAGE. |
| 50 | */ |
| 51 | |
| 52 | /* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93. */ |
| 53 | /* Hacked to work with BusyBox by Alfred M. Szmidt */ |
| 54 | |
| 55 | #include "internal.h" |
| 56 | |
| 57 | #include <stdio.h> |
| 58 | #include <errno.h> |
Eric Andersen | 999bf72 | 2000-07-09 06:59:58 +0000 | [diff] [blame] | 59 | #include <getopt.h> |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 60 | #include <pwd.h> |
| 61 | |
| 62 | #define RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) |
| 63 | |
| 64 | static void encode __P ((void)); |
| 65 | |
| 66 | /* Pointer to the translation table we currently use. */ |
| 67 | const char *trans_ptr; |
| 68 | |
| 69 | /* The two currently defined translation tables. The first is the |
| 70 | standard uuencoding, the second is base64 encoding. */ |
| 71 | const char uu_std[64] = { |
| 72 | '`', '!', '"', '#', '$', '%', '&', '\'', |
| 73 | '(', ')', '*', '+', ',', '-', '.', '/', |
| 74 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 75 | '8', '9', ':', ';', '<', '=', '>', '?', |
| 76 | '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
| 77 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
| 78 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
| 79 | 'X', 'Y', 'Z', '[', '\\', ']', '^', '_' |
| 80 | }; |
| 81 | |
| 82 | const char uu_base64[64] = { |
| 83 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
| 84 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 85 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| 86 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 87 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
| 88 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 89 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
| 90 | '4', '5', '6', '7', '8', '9', '+', '/' |
| 91 | }; |
| 92 | |
| 93 | /* ENC is the basic 1 character encoding function to make a char printing. */ |
| 94 | #define ENC(Char) (trans_ptr[(Char) & 077]) |
| 95 | |
| 96 | /* Copy from IN to OUT, encoding as you go along. */ |
| 97 | static void encode() |
| 98 | { |
| 99 | register int ch, n; |
| 100 | char *p = NULL; |
| 101 | char buf[80]; |
| 102 | |
| 103 | while (1) { |
| 104 | n = 0; |
| 105 | do { |
| 106 | register int m = fread (buf, 1, 45 - n, stdin); |
| 107 | if (m == 0) |
| 108 | break; |
| 109 | n += m; |
| 110 | } |
| 111 | while (n < 45); |
| 112 | |
| 113 | if (n == 0) |
| 114 | break; |
| 115 | |
| 116 | if (trans_ptr == uu_std) |
| 117 | if (putchar (ENC (n)) == EOF) |
| 118 | break; |
| 119 | for (p = buf; n > 2; n -= 3, p += 3) { |
| 120 | ch = *p >> 2; |
| 121 | ch = ENC (ch); |
| 122 | if (putchar (ch) == EOF) |
| 123 | break; |
| 124 | ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017); |
| 125 | ch = ENC (ch); |
| 126 | if (putchar (ch) == EOF) |
| 127 | break; |
| 128 | ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03); |
| 129 | ch = ENC (ch); |
| 130 | if (putchar (ch) == EOF) |
| 131 | break; |
| 132 | ch = p[2] & 077; |
| 133 | ch = ENC (ch); |
| 134 | if (putchar (ch) == EOF) |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | if (n != 0) |
| 139 | break; |
| 140 | |
| 141 | if (putchar ('\n') == EOF) |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | while (n != 0) { |
| 146 | char c1 = *p; |
| 147 | char c2 = n == 1 ? 0 : p[1]; |
| 148 | |
| 149 | ch = c1 >> 2; |
| 150 | ch = ENC (ch); |
| 151 | if (putchar (ch) == EOF) |
| 152 | break; |
| 153 | |
| 154 | ch = ((c1 << 4) & 060) | ((c2 >> 4) & 017); |
| 155 | ch = ENC (ch); |
| 156 | if (putchar (ch) == EOF) |
| 157 | break; |
| 158 | |
| 159 | if (n == 1) |
| 160 | ch = trans_ptr == uu_std ? ENC ('\0') : '='; |
| 161 | else { |
| 162 | ch = (c2 << 2) & 074; |
| 163 | ch = ENC (ch); |
| 164 | } |
| 165 | if (putchar (ch) == EOF) |
| 166 | break; |
| 167 | ch = trans_ptr == uu_std ? ENC ('\0') : '='; |
| 168 | if (putchar (ch) == EOF) |
| 169 | break; |
| 170 | putchar ('\n'); |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | if (ferror (stdin)) |
| 175 | errorMsg("Read error\n"); |
| 176 | |
| 177 | if (trans_ptr == uu_std) { |
| 178 | putchar (ENC ('\0')); |
| 179 | putchar ('\n'); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static const char uuencode_usage[] = |
| 184 | "uuencode [OPTION] [INFILE] REMOTEFILE\n" |
| 185 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 186 | "\nUuencode a file.\n\n" |
| 187 | "Options:\n" |
| 188 | "\t-m\tuse base64 encoding as of RFC1521\n" |
| 189 | #endif |
| 190 | ; |
| 191 | |
| 192 | int uuencode_main (int argc, |
| 193 | char **argv) |
| 194 | { |
| 195 | int opt; |
| 196 | struct stat sb; |
| 197 | int mode; |
| 198 | |
| 199 | trans_ptr = uu_std; /* Standard encoding is old uu format */ |
| 200 | |
| 201 | /* Parse any options */ |
| 202 | while ((opt = getopt (argc, argv, "m")) != EOF) { |
| 203 | switch (opt) { |
| 204 | case 'm': |
| 205 | trans_ptr = uu_base64; |
| 206 | break; |
| 207 | |
| 208 | case 0: |
| 209 | break; |
| 210 | |
| 211 | default: |
| 212 | usage(uuencode_usage); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | switch (argc - optind) { |
| 217 | case 2: |
| 218 | /* Optional first argument is input file. */ |
| 219 | if (!freopen (argv[optind], "r", stdin) || fstat (fileno (stdin), &sb)) { |
| 220 | errorMsg("uuencode: %s: %s\n", argv[optind], strerror(errno)); |
| 221 | exit FALSE; |
| 222 | } |
| 223 | mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 224 | optind++; |
| 225 | break; |
| 226 | |
| 227 | case 1: |
| 228 | mode = RW & ~umask (RW); |
| 229 | break; |
| 230 | |
| 231 | case 0: |
| 232 | default: |
| 233 | usage(uuencode_usage); |
| 234 | } |
| 235 | |
| 236 | printf("begin%s %o %s\n", trans_ptr == uu_std ? "" : "-base64", |
| 237 | mode, argv[optind]); |
| 238 | encode(); |
| 239 | printf(trans_ptr == uu_std ? "end\n" : "====\n"); |
| 240 | if (ferror (stdout)) { |
| 241 | errorMsg("Write error\n"); |
| 242 | exit FALSE; |
| 243 | } |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 244 | return( TRUE); |
Eric Andersen | 2b6ab3c | 2000-06-13 06:54:53 +0000 | [diff] [blame] | 245 | } |