diff options
| author | Glenn L McGrath <bug1@ihug.co.nz> | 2001-07-28 21:06:13 +0000 |
|---|---|---|
| committer | Glenn L McGrath <bug1@ihug.co.nz> | 2001-07-28 21:06:13 +0000 |
| commit | 089decab6543f0570f72e821620aa83165caada3 (patch) | |
| tree | 7ba5c0e418de0aac11ca1132fab6426526ae75b6 | |
| parent | 220238305f8b6604fbc2eca463f9cd0dd755f1ad (diff) | |
| download | busybox-w32-089decab6543f0570f72e821620aa83165caada3.tar.gz busybox-w32-089decab6543f0570f72e821620aa83165caada3.tar.bz2 busybox-w32-089decab6543f0570f72e821620aa83165caada3.zip | |
Rewrite based on wget uuencode function.
Saves aprox. 200 Bytes and in future can use common code between
this and wget if BB_FEATURE_WGET_AUTHENTICATION is defined.
| -rw-r--r-- | coreutils/uuencode.c | 389 | ||||
| -rw-r--r-- | uuencode.c | 389 |
2 files changed, 340 insertions, 438 deletions
diff --git a/coreutils/uuencode.c b/coreutils/uuencode.c index 35a533309..d46fb1588 100644 --- a/coreutils/uuencode.c +++ b/coreutils/uuencode.c | |||
| @@ -1,237 +1,188 @@ | |||
| 1 | /* uuencode.c -- uuencode utility. | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | * Copyright (C) 1994, 1995 Free Software Foundation, Inc. | 2 | /* |
| 3 | * Copyright (C) 2000 by Glenn McGrath | ||
| 3 | * | 4 | * |
| 4 | * This product is free software; you can redistribute it and/or modify | 5 | * based on the function base64_encode from http.c in wget v1.6 |
| 5 | * it under the terms of the GNU General Public License as published by | 6 | * Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc. |
| 6 | * the Free Software Foundation; either version 2, or (at your option) | ||
| 7 | * any later version. | ||
| 8 | * | 7 | * |
| 9 | * This product is distributed in the hope that it will be useful, | 8 | * This program is free software; you can redistribute it and/or modify |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 | * it under the terms of the GNU General Public License as published by |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * GNU General Public License for more details. | 11 | * (at your option) any later version. |
| 13 | * | 12 | * |
| 14 | * You should have received a copy of the GNU General Public License | 13 | * This program is distributed in the hope that it will be useful, |
| 15 | * along with this product; see the file COPYING. If not, write to | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * 02111-1307, USA. | 16 | * GNU Library General Public License for more details. |
| 18 | * | 17 | * |
| 19 | * Original copyright notice is retained at the end of this file. | 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 20 | */ | 21 | */ |
| 21 | /* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93. */ | ||
| 22 | /* Hacked to work with BusyBox by Alfred M. Szmidt */ | ||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | #include <stdio.h> | ||
| 27 | #include <errno.h> | ||
| 28 | #include <getopt.h> | 22 | #include <getopt.h> |
| 23 | #include <stdio.h> | ||
| 29 | #include <stdlib.h> | 24 | #include <stdlib.h> |
| 25 | #include <sys/types.h> | ||
| 26 | #include <sys/stat.h> | ||
| 27 | #include <unistd.h> | ||
| 30 | #include "busybox.h" | 28 | #include "busybox.h" |
| 31 | 29 | ||
| 32 | #define RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) | 30 | /* Conversion table. for base 64 */ |
| 33 | 31 | static char tbl_base64[64] = { | |
| 34 | static void encode __P ((void)); | 32 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
| 35 | 33 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | |
| 36 | /* Pointer to the translation table we currently use. */ | 34 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| 37 | static const char *trans_ptr; | 35 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 38 | 36 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | |
| 39 | /* The two currently defined translation tables. The first is the | 37 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 40 | standard uuencoding, the second is base64 encoding. */ | 38 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
| 41 | static const char uu_std[64] = { | 39 | '4', '5', '6', '7', '8', '9', '+', '/' |
| 42 | '`', '!', '"', '#', '$', '%', '&', '\'', | ||
| 43 | '(', ')', '*', '+', ',', '-', '.', '/', | ||
| 44 | '0', '1', '2', '3', '4', '5', '6', '7', | ||
| 45 | '8', '9', ':', ';', '<', '=', '>', '?', | ||
| 46 | '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', | ||
| 47 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', | ||
| 48 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', | ||
| 49 | 'X', 'Y', 'Z', '[', '\\', ']', '^', '_' | ||
| 50 | }; | 40 | }; |
| 51 | 41 | ||
| 52 | static const char uu_base64[64] = { | 42 | static char tbl_std[64] = { |
| 53 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | 43 | '`', '!', '"', '#', '$', '%', '&', '\'', |
| 54 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | 44 | '(', ')', '*', '+', ',', '-', '.', '/', |
| 55 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', | 45 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 56 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', | 46 | '8', '9', ':', ';', '<', '=', '>', '?', |
| 57 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | 47 | '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
| 58 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', | 48 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
| 59 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', | 49 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
| 60 | '4', '5', '6', '7', '8', '9', '+', '/' | 50 | 'X', 'Y', 'Z', '[', '\\', ']', '^', '_' |
| 61 | }; | 51 | }; |
| 62 | 52 | ||
| 63 | /* ENC is the basic 1 character encoding function to make a char printing. */ | 53 | /* |
| 64 | #define ENC(Char) (trans_ptr[(Char) & 077]) | 54 | * Encode the string S of length LENGTH to base64 format and place it |
| 65 | 55 | * to STORE. STORE will be 0-terminated, and must point to a writable | |
| 66 | /* Copy from IN to OUT, encoding as you go along. */ | 56 | * buffer of at least 1+BASE64_LENGTH(length) bytes. |
| 67 | static void encode() | 57 | * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3)) |
| 58 | */ | ||
| 59 | static void base64_encode (const char *s, const char *store, const int length, const char *tbl) | ||
| 68 | { | 60 | { |
| 69 | register int ch, n; | 61 | int i; |
| 70 | char *p = NULL; | 62 | unsigned char *p = (unsigned char *)store; |
| 71 | char buf[80]; | 63 | |
| 72 | 64 | /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ | |
| 73 | while (1) { | 65 | for (i = 0; i < length; i += 3) { |
| 74 | n = 0; | 66 | *p++ = tbl[s[0] >> 2]; |
| 75 | do { | 67 | *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; |
| 76 | register int m = fread (buf, 1, 45 - n, stdin); | 68 | *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; |
| 77 | if (m == 0) | 69 | *p++ = tbl[s[2] & 0x3f]; |
| 78 | break; | 70 | s += 3; |
| 79 | n += m; | 71 | } |
| 80 | } | 72 | /* Pad the result if necessary... */ |
| 81 | while (n < 45); | 73 | if (i == length + 1) { |
| 82 | 74 | *(p - 1) = '='; | |
| 83 | if (n == 0) | 75 | } |
| 84 | break; | 76 | else if (i == length + 2) { |
| 85 | 77 | *(p - 1) = *(p - 2) = '='; | |
| 86 | if (trans_ptr == uu_std) | 78 | } |
| 87 | if (putchar (ENC (n)) == EOF) | 79 | /* ...and zero-terminate it. */ |
| 88 | break; | 80 | *p = '\0'; |
| 89 | for (p = buf; n > 2; n -= 3, p += 3) { | ||
| 90 | ch = *p >> 2; | ||
| 91 | ch = ENC (ch); | ||
| 92 | if (putchar (ch) == EOF) | ||
| 93 | break; | ||
| 94 | ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017); | ||
| 95 | ch = ENC (ch); | ||
| 96 | if (putchar (ch) == EOF) | ||
| 97 | break; | ||
| 98 | ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03); | ||
| 99 | ch = ENC (ch); | ||
| 100 | if (putchar (ch) == EOF) | ||
| 101 | break; | ||
| 102 | ch = p[2] & 077; | ||
| 103 | ch = ENC (ch); | ||
| 104 | if (putchar (ch) == EOF) | ||
| 105 | break; | ||
| 106 | } | ||
| 107 | |||
| 108 | if (n != 0) | ||
| 109 | break; | ||
| 110 | |||
| 111 | if (putchar ('\n') == EOF) | ||
| 112 | break; | ||
| 113 | } | ||
| 114 | |||
| 115 | while (n != 0) { | ||
| 116 | char c1 = *p; | ||
| 117 | char c2 = n == 1 ? 0 : p[1]; | ||
| 118 | |||
| 119 | ch = c1 >> 2; | ||
| 120 | ch = ENC (ch); | ||
| 121 | if (putchar (ch) == EOF) | ||
| 122 | break; | ||
| 123 | |||
| 124 | ch = ((c1 << 4) & 060) | ((c2 >> 4) & 017); | ||
| 125 | ch = ENC (ch); | ||
| 126 | if (putchar (ch) == EOF) | ||
| 127 | break; | ||
| 128 | |||
| 129 | if (n == 1) | ||
| 130 | ch = trans_ptr == uu_std ? ENC ('\0') : '='; | ||
| 131 | else { | ||
| 132 | ch = (c2 << 2) & 074; | ||
| 133 | ch = ENC (ch); | ||
| 134 | } | ||
| 135 | if (putchar (ch) == EOF) | ||
| 136 | break; | ||
| 137 | ch = trans_ptr == uu_std ? ENC ('\0') : '='; | ||
| 138 | if (putchar (ch) == EOF) | ||
| 139 | break; | ||
| 140 | putchar ('\n'); | ||
| 141 | break; | ||
| 142 | } | ||
| 143 | |||
| 144 | if (ferror (stdin)) | ||
| 145 | error_msg("Read error"); | ||
| 146 | |||
| 147 | if (trans_ptr == uu_std) { | ||
| 148 | putchar (ENC ('\0')); | ||
| 149 | putchar ('\n'); | ||
| 150 | } | ||
| 151 | } | 81 | } |
| 152 | 82 | ||
| 153 | int uuencode_main (int argc, | 83 | int uuencode_main(int argc, char **argv) |
| 154 | char **argv) | ||
| 155 | { | 84 | { |
| 156 | int opt; | 85 | const int src_buf_size = BUFSIZ; // This *MUST* be a multiple of 3 |
| 157 | struct stat sb; | 86 | const int dst_buf_size = 4 * ((src_buf_size + 2) / 3); |
| 158 | int mode; | 87 | RESERVE_BB_BUFFER(src_buf, src_buf_size + 1); |
| 159 | 88 | RESERVE_BB_BUFFER(dst_buf, dst_buf_size + 1); | |
| 160 | trans_ptr = uu_std; /* Standard encoding is old uu format */ | 89 | struct stat stat_buf; |
| 161 | 90 | FILE *src_stream = stdin; | |
| 162 | /* Parse any options */ | 91 | char *tbl = tbl_std; |
| 163 | while ((opt = getopt (argc, argv, "m")) > 0) { | 92 | size_t size; |
| 164 | switch (opt) { | 93 | mode_t mode; |
| 165 | case 'm': | 94 | int opt; |
| 166 | trans_ptr = uu_base64; | 95 | int column = 0; |
| 167 | break; | 96 | int write_size; |
| 168 | 97 | int remaining; | |
| 169 | default: | 98 | int buffer_offset = 0; |
| 170 | show_usage(); | 99 | |
| 171 | } | 100 | while ((opt = getopt(argc, argv, "m")) != -1) { |
| 172 | } | 101 | switch (opt) { |
| 173 | 102 | case 'm': | |
| 174 | switch (argc - optind) { | 103 | tbl = tbl_base64; |
| 175 | case 2: | 104 | break; |
| 176 | /* Optional first argument is input file. */ | 105 | default: |
| 177 | if (!freopen (argv[optind], "r", stdin) || fstat (fileno (stdin), &sb)) { | 106 | show_usage(); |
| 178 | perror_msg("%s", argv[optind]); | 107 | } |
| 179 | return EXIT_FAILURE; | 108 | } |
| 180 | } | 109 | |
| 181 | mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); | 110 | switch (argc - optind) { |
| 182 | optind++; | 111 | case 2: |
| 183 | break; | 112 | src_stream = xfopen(argv[optind], "r"); |
| 184 | 113 | stat(argv[optind], &stat_buf); | |
| 185 | case 1: | 114 | mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 186 | mode = RW & ~umask (RW); | 115 | if (src_stream == stdout) { |
| 187 | break; | 116 | printf("NULL\n"); |
| 188 | 117 | } | |
| 189 | case 0: | 118 | break; |
| 190 | default: | 119 | case 1: |
| 191 | show_usage(); | 120 | mode = umask(0666); |
| 192 | } | 121 | break; |
| 193 | 122 | default: | |
| 194 | printf("begin%s %o %s\n", trans_ptr == uu_std ? "" : "-base64", | 123 | show_usage(); |
| 195 | mode, argv[optind]); | 124 | } |
| 196 | encode(); | 125 | |
| 197 | printf(trans_ptr == uu_std ? "end\n" : "====\n"); | 126 | printf("begin%s %o %s", tbl == tbl_std ? "" : "-base64", mode, argv[argc - 1]); |
| 198 | if (ferror (stdout)) { | 127 | |
| 199 | error_msg("Write error"); | 128 | while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) { |
| 200 | return EXIT_FAILURE; | 129 | /* Encode the buffer we just read in */ |
| 201 | } | 130 | base64_encode(src_buf, dst_buf, size, tbl); |
| 202 | return EXIT_SUCCESS; | 131 | |
| 132 | /* Write the buffer to stdout, wrapping at 60 chars. | ||
| 133 | * This looks overly complex, but it gets tricky as | ||
| 134 | * the line has to continue to wrap correctly if we | ||
| 135 | * have to refill the buffer | ||
| 136 | * | ||
| 137 | * Improvments most welcome | ||
| 138 | */ | ||
| 139 | |||
| 140 | /* Initialise values for the new buffer */ | ||
| 141 | remaining = 4 * ((size + 2) / 3); | ||
| 142 | buffer_offset = 0; | ||
| 143 | if (remaining > (60 - column)) { | ||
| 144 | write_size = 60 - column; | ||
| 145 | } | ||
| 146 | else if (remaining < 60) { | ||
| 147 | write_size = remaining; | ||
| 148 | } else { | ||
| 149 | write_size = 60; | ||
| 150 | } | ||
| 151 | |||
| 152 | /* Write the buffer to stdout, wrapping at 60 chars | ||
| 153 | * starting from the column the last buffer ran out | ||
| 154 | */ | ||
| 155 | do { | ||
| 156 | /* Setup a new row if required */ | ||
| 157 | if (column == 0) { | ||
| 158 | putchar('\n'); | ||
| 159 | if (tbl == tbl_std) { | ||
| 160 | putchar('M'); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | /* Write to the 60th column */ | ||
| 164 | if (fwrite(&dst_buf[buffer_offset], 1, write_size, stdout) != write_size) { | ||
| 165 | perror("Couldnt finish writing"); | ||
| 166 | } | ||
| 167 | /* Update variables based on last write */ | ||
| 168 | buffer_offset += write_size; | ||
| 169 | remaining -= write_size; | ||
| 170 | column += write_size; | ||
| 171 | if (column % 60 == 0) { | ||
| 172 | column = 0; | ||
| 173 | } | ||
| 174 | |||
| 175 | /* working next amount to write */ | ||
| 176 | write_size = (60 - column) % 60; | ||
| 177 | if (write_size < remaining) { | ||
| 178 | write_size = remaining; | ||
| 179 | } | ||
| 180 | if (write_size == 0) { | ||
| 181 | write_size = 60; | ||
| 182 | } | ||
| 183 | } while (remaining > 0); | ||
| 184 | } | ||
| 185 | printf(tbl == tbl_std ? "\n`\nend\n" : "\n====\n"); | ||
| 186 | |||
| 187 | return(EXIT_SUCCESS); | ||
| 203 | } | 188 | } |
| 204 | |||
| 205 | /* Copyright (c) 1983 Regents of the University of California. | ||
| 206 | * All rights reserved. | ||
| 207 | * | ||
| 208 | * Redistribution and use in source and binary forms, with or without | ||
| 209 | * modification, are permitted provided that the following conditions | ||
| 210 | * are met: | ||
| 211 | * 1. Redistributions of source code must retain the above copyright | ||
| 212 | * notice, this list of conditions and the following disclaimer. | ||
| 213 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 214 | * notice, this list of conditions and the following disclaimer in the | ||
| 215 | * documentation and/or other materials provided with the distribution. | ||
| 216 | * | ||
| 217 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change | ||
| 218 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> | ||
| 219 | * | ||
| 220 | * 4. Neither the name of the University nor the names of its contributors | ||
| 221 | * may be used to endorse or promote products derived from this software | ||
| 222 | * without specific prior written permission. | ||
| 223 | * | ||
| 224 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 225 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 226 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 227 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 228 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 229 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 230 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 231 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 232 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 233 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 234 | * SUCH DAMAGE. | ||
| 235 | */ | ||
| 236 | |||
| 237 | |||
diff --git a/uuencode.c b/uuencode.c index 35a533309..d46fb1588 100644 --- a/uuencode.c +++ b/uuencode.c | |||
| @@ -1,237 +1,188 @@ | |||
| 1 | /* uuencode.c -- uuencode utility. | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | * Copyright (C) 1994, 1995 Free Software Foundation, Inc. | 2 | /* |
| 3 | * Copyright (C) 2000 by Glenn McGrath | ||
| 3 | * | 4 | * |
| 4 | * This product is free software; you can redistribute it and/or modify | 5 | * based on the function base64_encode from http.c in wget v1.6 |
| 5 | * it under the terms of the GNU General Public License as published by | 6 | * Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc. |
| 6 | * the Free Software Foundation; either version 2, or (at your option) | ||
| 7 | * any later version. | ||
| 8 | * | 7 | * |
| 9 | * This product is distributed in the hope that it will be useful, | 8 | * This program is free software; you can redistribute it and/or modify |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 | * it under the terms of the GNU General Public License as published by |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * GNU General Public License for more details. | 11 | * (at your option) any later version. |
| 13 | * | 12 | * |
| 14 | * You should have received a copy of the GNU General Public License | 13 | * This program is distributed in the hope that it will be useful, |
| 15 | * along with this product; see the file COPYING. If not, write to | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * 02111-1307, USA. | 16 | * GNU Library General Public License for more details. |
| 18 | * | 17 | * |
| 19 | * Original copyright notice is retained at the end of this file. | 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 20 | */ | 21 | */ |
| 21 | /* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93. */ | ||
| 22 | /* Hacked to work with BusyBox by Alfred M. Szmidt */ | ||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | #include <stdio.h> | ||
| 27 | #include <errno.h> | ||
| 28 | #include <getopt.h> | 22 | #include <getopt.h> |
| 23 | #include <stdio.h> | ||
| 29 | #include <stdlib.h> | 24 | #include <stdlib.h> |
| 25 | #include <sys/types.h> | ||
| 26 | #include <sys/stat.h> | ||
| 27 | #include <unistd.h> | ||
| 30 | #include "busybox.h" | 28 | #include "busybox.h" |
| 31 | 29 | ||
| 32 | #define RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) | 30 | /* Conversion table. for base 64 */ |
| 33 | 31 | static char tbl_base64[64] = { | |
| 34 | static void encode __P ((void)); | 32 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
| 35 | 33 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | |
| 36 | /* Pointer to the translation table we currently use. */ | 34 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| 37 | static const char *trans_ptr; | 35 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 38 | 36 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | |
| 39 | /* The two currently defined translation tables. The first is the | 37 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 40 | standard uuencoding, the second is base64 encoding. */ | 38 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
| 41 | static const char uu_std[64] = { | 39 | '4', '5', '6', '7', '8', '9', '+', '/' |
| 42 | '`', '!', '"', '#', '$', '%', '&', '\'', | ||
| 43 | '(', ')', '*', '+', ',', '-', '.', '/', | ||
| 44 | '0', '1', '2', '3', '4', '5', '6', '7', | ||
| 45 | '8', '9', ':', ';', '<', '=', '>', '?', | ||
| 46 | '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', | ||
| 47 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', | ||
| 48 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', | ||
| 49 | 'X', 'Y', 'Z', '[', '\\', ']', '^', '_' | ||
| 50 | }; | 40 | }; |
| 51 | 41 | ||
| 52 | static const char uu_base64[64] = { | 42 | static char tbl_std[64] = { |
| 53 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | 43 | '`', '!', '"', '#', '$', '%', '&', '\'', |
| 54 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | 44 | '(', ')', '*', '+', ',', '-', '.', '/', |
| 55 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', | 45 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 56 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', | 46 | '8', '9', ':', ';', '<', '=', '>', '?', |
| 57 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | 47 | '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
| 58 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', | 48 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
| 59 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', | 49 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
| 60 | '4', '5', '6', '7', '8', '9', '+', '/' | 50 | 'X', 'Y', 'Z', '[', '\\', ']', '^', '_' |
| 61 | }; | 51 | }; |
| 62 | 52 | ||
| 63 | /* ENC is the basic 1 character encoding function to make a char printing. */ | 53 | /* |
| 64 | #define ENC(Char) (trans_ptr[(Char) & 077]) | 54 | * Encode the string S of length LENGTH to base64 format and place it |
| 65 | 55 | * to STORE. STORE will be 0-terminated, and must point to a writable | |
| 66 | /* Copy from IN to OUT, encoding as you go along. */ | 56 | * buffer of at least 1+BASE64_LENGTH(length) bytes. |
| 67 | static void encode() | 57 | * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3)) |
| 58 | */ | ||
| 59 | static void base64_encode (const char *s, const char *store, const int length, const char *tbl) | ||
| 68 | { | 60 | { |
| 69 | register int ch, n; | 61 | int i; |
| 70 | char *p = NULL; | 62 | unsigned char *p = (unsigned char *)store; |
| 71 | char buf[80]; | 63 | |
| 72 | 64 | /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ | |
| 73 | while (1) { | 65 | for (i = 0; i < length; i += 3) { |
| 74 | n = 0; | 66 | *p++ = tbl[s[0] >> 2]; |
| 75 | do { | 67 | *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; |
| 76 | register int m = fread (buf, 1, 45 - n, stdin); | 68 | *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; |
| 77 | if (m == 0) | 69 | *p++ = tbl[s[2] & 0x3f]; |
| 78 | break; | 70 | s += 3; |
| 79 | n += m; | 71 | } |
| 80 | } | 72 | /* Pad the result if necessary... */ |
| 81 | while (n < 45); | 73 | if (i == length + 1) { |
| 82 | 74 | *(p - 1) = '='; | |
| 83 | if (n == 0) | 75 | } |
| 84 | break; | 76 | else if (i == length + 2) { |
| 85 | 77 | *(p - 1) = *(p - 2) = '='; | |
| 86 | if (trans_ptr == uu_std) | 78 | } |
| 87 | if (putchar (ENC (n)) == EOF) | 79 | /* ...and zero-terminate it. */ |
| 88 | break; | 80 | *p = '\0'; |
| 89 | for (p = buf; n > 2; n -= 3, p += 3) { | ||
| 90 | ch = *p >> 2; | ||
| 91 | ch = ENC (ch); | ||
| 92 | if (putchar (ch) == EOF) | ||
| 93 | break; | ||
| 94 | ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017); | ||
| 95 | ch = ENC (ch); | ||
| 96 | if (putchar (ch) == EOF) | ||
| 97 | break; | ||
| 98 | ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03); | ||
| 99 | ch = ENC (ch); | ||
| 100 | if (putchar (ch) == EOF) | ||
| 101 | break; | ||
| 102 | ch = p[2] & 077; | ||
| 103 | ch = ENC (ch); | ||
| 104 | if (putchar (ch) == EOF) | ||
| 105 | break; | ||
| 106 | } | ||
| 107 | |||
| 108 | if (n != 0) | ||
| 109 | break; | ||
| 110 | |||
| 111 | if (putchar ('\n') == EOF) | ||
| 112 | break; | ||
| 113 | } | ||
| 114 | |||
| 115 | while (n != 0) { | ||
| 116 | char c1 = *p; | ||
| 117 | char c2 = n == 1 ? 0 : p[1]; | ||
| 118 | |||
| 119 | ch = c1 >> 2; | ||
| 120 | ch = ENC (ch); | ||
| 121 | if (putchar (ch) == EOF) | ||
| 122 | break; | ||
| 123 | |||
| 124 | ch = ((c1 << 4) & 060) | ((c2 >> 4) & 017); | ||
| 125 | ch = ENC (ch); | ||
| 126 | if (putchar (ch) == EOF) | ||
| 127 | break; | ||
| 128 | |||
| 129 | if (n == 1) | ||
| 130 | ch = trans_ptr == uu_std ? ENC ('\0') : '='; | ||
| 131 | else { | ||
| 132 | ch = (c2 << 2) & 074; | ||
| 133 | ch = ENC (ch); | ||
| 134 | } | ||
| 135 | if (putchar (ch) == EOF) | ||
| 136 | break; | ||
| 137 | ch = trans_ptr == uu_std ? ENC ('\0') : '='; | ||
| 138 | if (putchar (ch) == EOF) | ||
| 139 | break; | ||
| 140 | putchar ('\n'); | ||
| 141 | break; | ||
| 142 | } | ||
| 143 | |||
| 144 | if (ferror (stdin)) | ||
| 145 | error_msg("Read error"); | ||
| 146 | |||
| 147 | if (trans_ptr == uu_std) { | ||
| 148 | putchar (ENC ('\0')); | ||
| 149 | putchar ('\n'); | ||
| 150 | } | ||
| 151 | } | 81 | } |
| 152 | 82 | ||
| 153 | int uuencode_main (int argc, | 83 | int uuencode_main(int argc, char **argv) |
| 154 | char **argv) | ||
| 155 | { | 84 | { |
| 156 | int opt; | 85 | const int src_buf_size = BUFSIZ; // This *MUST* be a multiple of 3 |
| 157 | struct stat sb; | 86 | const int dst_buf_size = 4 * ((src_buf_size + 2) / 3); |
| 158 | int mode; | 87 | RESERVE_BB_BUFFER(src_buf, src_buf_size + 1); |
| 159 | 88 | RESERVE_BB_BUFFER(dst_buf, dst_buf_size + 1); | |
| 160 | trans_ptr = uu_std; /* Standard encoding is old uu format */ | 89 | struct stat stat_buf; |
| 161 | 90 | FILE *src_stream = stdin; | |
| 162 | /* Parse any options */ | 91 | char *tbl = tbl_std; |
| 163 | while ((opt = getopt (argc, argv, "m")) > 0) { | 92 | size_t size; |
| 164 | switch (opt) { | 93 | mode_t mode; |
| 165 | case 'm': | 94 | int opt; |
| 166 | trans_ptr = uu_base64; | 95 | int column = 0; |
| 167 | break; | 96 | int write_size; |
| 168 | 97 | int remaining; | |
| 169 | default: | 98 | int buffer_offset = 0; |
| 170 | show_usage(); | 99 | |
| 171 | } | 100 | while ((opt = getopt(argc, argv, "m")) != -1) { |
| 172 | } | 101 | switch (opt) { |
| 173 | 102 | case 'm': | |
| 174 | switch (argc - optind) { | 103 | tbl = tbl_base64; |
| 175 | case 2: | 104 | break; |
| 176 | /* Optional first argument is input file. */ | 105 | default: |
| 177 | if (!freopen (argv[optind], "r", stdin) || fstat (fileno (stdin), &sb)) { | 106 | show_usage(); |
| 178 | perror_msg("%s", argv[optind]); | 107 | } |
| 179 | return EXIT_FAILURE; | 108 | } |
| 180 | } | 109 | |
| 181 | mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); | 110 | switch (argc - optind) { |
| 182 | optind++; | 111 | case 2: |
| 183 | break; | 112 | src_stream = xfopen(argv[optind], "r"); |
| 184 | 113 | stat(argv[optind], &stat_buf); | |
| 185 | case 1: | 114 | mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 186 | mode = RW & ~umask (RW); | 115 | if (src_stream == stdout) { |
| 187 | break; | 116 | printf("NULL\n"); |
| 188 | 117 | } | |
| 189 | case 0: | 118 | break; |
| 190 | default: | 119 | case 1: |
| 191 | show_usage(); | 120 | mode = umask(0666); |
| 192 | } | 121 | break; |
| 193 | 122 | default: | |
| 194 | printf("begin%s %o %s\n", trans_ptr == uu_std ? "" : "-base64", | 123 | show_usage(); |
| 195 | mode, argv[optind]); | 124 | } |
| 196 | encode(); | 125 | |
| 197 | printf(trans_ptr == uu_std ? "end\n" : "====\n"); | 126 | printf("begin%s %o %s", tbl == tbl_std ? "" : "-base64", mode, argv[argc - 1]); |
| 198 | if (ferror (stdout)) { | 127 | |
| 199 | error_msg("Write error"); | 128 | while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) { |
| 200 | return EXIT_FAILURE; | 129 | /* Encode the buffer we just read in */ |
| 201 | } | 130 | base64_encode(src_buf, dst_buf, size, tbl); |
| 202 | return EXIT_SUCCESS; | 131 | |
| 132 | /* Write the buffer to stdout, wrapping at 60 chars. | ||
| 133 | * This looks overly complex, but it gets tricky as | ||
| 134 | * the line has to continue to wrap correctly if we | ||
| 135 | * have to refill the buffer | ||
| 136 | * | ||
| 137 | * Improvments most welcome | ||
| 138 | */ | ||
| 139 | |||
| 140 | /* Initialise values for the new buffer */ | ||
| 141 | remaining = 4 * ((size + 2) / 3); | ||
| 142 | buffer_offset = 0; | ||
| 143 | if (remaining > (60 - column)) { | ||
| 144 | write_size = 60 - column; | ||
| 145 | } | ||
| 146 | else if (remaining < 60) { | ||
| 147 | write_size = remaining; | ||
| 148 | } else { | ||
| 149 | write_size = 60; | ||
| 150 | } | ||
| 151 | |||
| 152 | /* Write the buffer to stdout, wrapping at 60 chars | ||
| 153 | * starting from the column the last buffer ran out | ||
| 154 | */ | ||
| 155 | do { | ||
| 156 | /* Setup a new row if required */ | ||
| 157 | if (column == 0) { | ||
| 158 | putchar('\n'); | ||
| 159 | if (tbl == tbl_std) { | ||
| 160 | putchar('M'); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | /* Write to the 60th column */ | ||
| 164 | if (fwrite(&dst_buf[buffer_offset], 1, write_size, stdout) != write_size) { | ||
| 165 | perror("Couldnt finish writing"); | ||
| 166 | } | ||
| 167 | /* Update variables based on last write */ | ||
| 168 | buffer_offset += write_size; | ||
| 169 | remaining -= write_size; | ||
| 170 | column += write_size; | ||
| 171 | if (column % 60 == 0) { | ||
| 172 | column = 0; | ||
| 173 | } | ||
| 174 | |||
| 175 | /* working next amount to write */ | ||
| 176 | write_size = (60 - column) % 60; | ||
| 177 | if (write_size < remaining) { | ||
| 178 | write_size = remaining; | ||
| 179 | } | ||
| 180 | if (write_size == 0) { | ||
| 181 | write_size = 60; | ||
| 182 | } | ||
| 183 | } while (remaining > 0); | ||
| 184 | } | ||
| 185 | printf(tbl == tbl_std ? "\n`\nend\n" : "\n====\n"); | ||
| 186 | |||
| 187 | return(EXIT_SUCCESS); | ||
| 203 | } | 188 | } |
| 204 | |||
| 205 | /* Copyright (c) 1983 Regents of the University of California. | ||
| 206 | * All rights reserved. | ||
| 207 | * | ||
| 208 | * Redistribution and use in source and binary forms, with or without | ||
| 209 | * modification, are permitted provided that the following conditions | ||
| 210 | * are met: | ||
| 211 | * 1. Redistributions of source code must retain the above copyright | ||
| 212 | * notice, this list of conditions and the following disclaimer. | ||
| 213 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 214 | * notice, this list of conditions and the following disclaimer in the | ||
| 215 | * documentation and/or other materials provided with the distribution. | ||
| 216 | * | ||
| 217 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change | ||
| 218 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> | ||
| 219 | * | ||
| 220 | * 4. Neither the name of the University nor the names of its contributors | ||
| 221 | * may be used to endorse or promote products derived from this software | ||
| 222 | * without specific prior written permission. | ||
| 223 | * | ||
| 224 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 225 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 226 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 227 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 228 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 229 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 230 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 231 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 232 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 233 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 234 | * SUCH DAMAGE. | ||
| 235 | */ | ||
| 236 | |||
| 237 | |||
