[1770] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[821] | 2 | /*
|
---|
| 3 | * Copyright 2003, Glenn McGrath <bug1@iinet.net.au>
|
---|
| 4 | *
|
---|
| 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
| 6 | *
|
---|
| 7 | * Based on specification from
|
---|
| 8 | * http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html
|
---|
| 9 | *
|
---|
| 10 | * Bugs: the spec doesn't mention anything about "`\n`\n" prior to the
|
---|
| 11 | * "end" line
|
---|
| 12 | */
|
---|
| 13 |
|
---|
| 14 |
|
---|
[1770] | 15 | #include "libbb.h"
|
---|
[821] | 16 |
|
---|
[1770] | 17 | static void read_stduu(FILE *src_stream, FILE *dst_stream)
|
---|
[821] | 18 | {
|
---|
| 19 | char *line;
|
---|
| 20 |
|
---|
[1770] | 21 | while ((line = xmalloc_getline(src_stream)) != NULL) {
|
---|
[821] | 22 | int length;
|
---|
| 23 | char *line_ptr = line;
|
---|
| 24 |
|
---|
| 25 | if (strcmp(line, "end") == 0) {
|
---|
[1770] | 26 | return;
|
---|
[821] | 27 | }
|
---|
| 28 | length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
|
---|
| 29 |
|
---|
| 30 | if (length <= 0) {
|
---|
| 31 | /* Ignore the "`\n" line, why is it even in the encode file ? */
|
---|
| 32 | continue;
|
---|
| 33 | }
|
---|
| 34 | if (length > 60) {
|
---|
[1770] | 35 | bb_error_msg_and_die("line too long");
|
---|
[821] | 36 | }
|
---|
| 37 |
|
---|
| 38 | line_ptr++;
|
---|
[1770] | 39 | /* Tolerate an overly long line to accomodate a possible exta '`' */
|
---|
[821] | 40 | if (strlen(line_ptr) < (size_t)length) {
|
---|
[1770] | 41 | bb_error_msg_and_die("short file");
|
---|
[821] | 42 | }
|
---|
| 43 |
|
---|
| 44 | while (length > 0) {
|
---|
| 45 | /* Merge four 6 bit chars to three 8 bit chars */
|
---|
| 46 | fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream);
|
---|
| 47 | line_ptr++;
|
---|
| 48 | length--;
|
---|
| 49 | if (length == 0) {
|
---|
| 50 | break;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | fputc(((line_ptr[0] - 0x20) & 077) << 4 | ((line_ptr[1] - 0x20) & 077) >> 2, dst_stream);
|
---|
| 54 | line_ptr++;
|
---|
| 55 | length--;
|
---|
| 56 | if (length == 0) {
|
---|
| 57 | break;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | fputc(((line_ptr[0] - 0x20) & 077) << 6 | ((line_ptr[1] - 0x20) & 077), dst_stream);
|
---|
| 61 | line_ptr += 2;
|
---|
| 62 | length -= 2;
|
---|
| 63 | }
|
---|
| 64 | free(line);
|
---|
| 65 | }
|
---|
[1770] | 66 | bb_error_msg_and_die("short file");
|
---|
[821] | 67 | }
|
---|
| 68 |
|
---|
[1770] | 69 | static void read_base64(FILE *src_stream, FILE *dst_stream)
|
---|
[821] | 70 | {
|
---|
[1770] | 71 | int term_count = 1;
|
---|
[821] | 72 |
|
---|
| 73 | while (1) {
|
---|
| 74 | char translated[4];
|
---|
| 75 | int count = 0;
|
---|
| 76 |
|
---|
| 77 | while (count < 4) {
|
---|
| 78 | char *table_ptr;
|
---|
| 79 | int ch;
|
---|
| 80 |
|
---|
[1770] | 81 | /* Get next _valid_ character.
|
---|
| 82 | * global vector bb_uuenc_tbl_base64[] contains this string:
|
---|
| 83 | * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"
|
---|
| 84 | */
|
---|
[821] | 85 | do {
|
---|
| 86 | ch = fgetc(src_stream);
|
---|
| 87 | if (ch == EOF) {
|
---|
[1770] | 88 | bb_error_msg_and_die("short file");
|
---|
[821] | 89 | }
|
---|
[1770] | 90 | table_ptr = strchr(bb_uuenc_tbl_base64, ch);
|
---|
| 91 | } while (table_ptr == NULL);
|
---|
[821] | 92 |
|
---|
[1770] | 93 | /* Convert encoded character to decimal */
|
---|
| 94 | ch = table_ptr - bb_uuenc_tbl_base64;
|
---|
[821] | 95 |
|
---|
| 96 | if (*table_ptr == '=') {
|
---|
| 97 | if (term_count == 0) {
|
---|
[1770] | 98 | translated[count] = '\0';
|
---|
[821] | 99 | break;
|
---|
| 100 | }
|
---|
| 101 | term_count++;
|
---|
[1770] | 102 | } else if (*table_ptr == '\n') {
|
---|
[821] | 103 | /* Check for terminating line */
|
---|
| 104 | if (term_count == 5) {
|
---|
[1770] | 105 | return;
|
---|
[821] | 106 | }
|
---|
| 107 | term_count = 1;
|
---|
| 108 | continue;
|
---|
| 109 | } else {
|
---|
| 110 | translated[count] = ch;
|
---|
| 111 | count++;
|
---|
| 112 | term_count = 0;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /* Merge 6 bit chars to 8 bit */
|
---|
[1770] | 117 | if (count > 1) {
|
---|
| 118 | fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
|
---|
| 119 | }
|
---|
[821] | 120 | if (count > 2) {
|
---|
| 121 | fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
|
---|
| 122 | }
|
---|
| 123 | if (count > 3) {
|
---|
| 124 | fputc(translated[2] << 6 | translated[3], dst_stream);
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[1770] | 129 | int uudecode_main(int argc, char **argv);
|
---|
[821] | 130 | int uudecode_main(int argc, char **argv)
|
---|
| 131 | {
|
---|
[1770] | 132 | FILE *src_stream = stdin;
|
---|
[821] | 133 | char *outname = NULL;
|
---|
| 134 | char *line;
|
---|
| 135 |
|
---|
[1770] | 136 | opt_complementary = "?1"; /* 1 argument max */
|
---|
| 137 | getopt32(argv, "o:", &outname);
|
---|
| 138 | argv += optind;
|
---|
[821] | 139 |
|
---|
[1770] | 140 | if (argv[0])
|
---|
| 141 | src_stream = xfopen(argv[0], "r");
|
---|
[821] | 142 |
|
---|
| 143 | /* Search for the start of the encoding */
|
---|
[1770] | 144 | while ((line = xmalloc_getline(src_stream)) != NULL) {
|
---|
| 145 | void (*decode_fn_ptr)(FILE * src, FILE * dst);
|
---|
| 146 | char *line_ptr;
|
---|
| 147 | FILE *dst_stream;
|
---|
| 148 | int mode;
|
---|
[821] | 149 |
|
---|
[1770] | 150 | if (strncmp(line, "begin-base64 ", 13) == 0) {
|
---|
[821] | 151 | line_ptr = line + 13;
|
---|
| 152 | decode_fn_ptr = read_base64;
|
---|
| 153 | } else if (strncmp(line, "begin ", 6) == 0) {
|
---|
| 154 | line_ptr = line + 6;
|
---|
| 155 | decode_fn_ptr = read_stduu;
|
---|
[1770] | 156 | } else {
|
---|
| 157 | free(line);
|
---|
| 158 | continue;
|
---|
[821] | 159 | }
|
---|
| 160 |
|
---|
[1770] | 161 | /* begin line found. decode and exit */
|
---|
| 162 | mode = strtoul(line_ptr, NULL, 8);
|
---|
| 163 | if (outname == NULL) {
|
---|
| 164 | outname = strchr(line_ptr, ' ');
|
---|
| 165 | if ((outname == NULL) || (*outname == '\0')) {
|
---|
| 166 | break;
|
---|
[821] | 167 | }
|
---|
[1770] | 168 | outname++;
|
---|
[821] | 169 | }
|
---|
[1770] | 170 | dst_stream = stdout;
|
---|
| 171 | if (NOT_LONE_DASH(outname)) {
|
---|
| 172 | dst_stream = xfopen(outname, "w");
|
---|
| 173 | chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
|
---|
| 174 | }
|
---|
[821] | 175 | free(line);
|
---|
[1770] | 176 | decode_fn_ptr(src_stream, dst_stream);
|
---|
| 177 | /* fclose_if_not_stdin(src_stream); - redundant */
|
---|
| 178 | return EXIT_SUCCESS;
|
---|
[821] | 179 | }
|
---|
[1770] | 180 | bb_error_msg_and_die("no 'begin' line");
|
---|
[821] | 181 | }
|
---|
[1770] | 182 |
|
---|
| 183 | /* Test script.
|
---|
| 184 | Put this into an empty dir with busybox binary, an run.
|
---|
| 185 |
|
---|
| 186 | #!/bin/sh
|
---|
| 187 | test -x busybox || { echo "No ./busybox?"; exit; }
|
---|
| 188 | ln -sf busybox uudecode
|
---|
| 189 | ln -sf busybox uuencode
|
---|
| 190 | >A_null
|
---|
| 191 | echo -n A >A
|
---|
| 192 | echo -n AB >AB
|
---|
| 193 | echo -n ABC >ABC
|
---|
| 194 | echo -n ABCD >ABCD
|
---|
| 195 | echo -n ABCDE >ABCDE
|
---|
| 196 | echo -n ABCDEF >ABCDEF
|
---|
| 197 | cat busybox >A_bbox
|
---|
| 198 | for f in A*; do
|
---|
| 199 | echo uuencode $f
|
---|
| 200 | ./uuencode $f <$f >u_$f
|
---|
| 201 | ./uuencode -m $f <$f >m_$f
|
---|
| 202 | done
|
---|
| 203 | mkdir unpk_u unpk_m 2>/dev/null
|
---|
| 204 | for f in u_*; do
|
---|
| 205 | ./uudecode <$f -o unpk_u/${f:2}
|
---|
| 206 | diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
|
---|
| 207 | echo uudecode $f: $?
|
---|
| 208 | done
|
---|
| 209 | for f in m_*; do
|
---|
| 210 | ./uudecode <$f -o unpk_m/${f:2}
|
---|
| 211 | diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
|
---|
| 212 | echo uudecode $f: $?
|
---|
| 213 | done
|
---|
| 214 | */
|
---|