Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/coreutils/uudecode.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/coreutils/uudecode.c
r821 r1770 1 /* vi: set sw=4 ts=4: */ 1 2 /* 2 3 * Copyright 2003, Glenn McGrath <bug1@iinet.net.au> … … 12 13 13 14 14 #include <stdio.h> 15 #include <errno.h> 16 #include <getopt.h> /* optind */ 17 #include <string.h> 18 #include <stdlib.h> 19 #include "busybox.h" 20 21 static int read_stduu(FILE *src_stream, FILE *dst_stream) 15 #include "libbb.h" 16 17 static void read_stduu(FILE *src_stream, FILE *dst_stream) 22 18 { 23 19 char *line; 24 20 25 while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {21 while ((line = xmalloc_getline(src_stream)) != NULL) { 26 22 int length; 27 23 char *line_ptr = line; 28 24 29 25 if (strcmp(line, "end") == 0) { 30 return (EXIT_SUCCESS);26 return; 31 27 } 32 28 length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3; … … 37 33 } 38 34 if (length > 60) { 39 bb_error_msg_and_die(" Line too long");35 bb_error_msg_and_die("line too long"); 40 36 } 41 37 42 38 line_ptr++; 43 /* Tolerate an overly long line to ac omadate a possible exta '`' */39 /* Tolerate an overly long line to accomodate a possible exta '`' */ 44 40 if (strlen(line_ptr) < (size_t)length) { 45 bb_error_msg_and_die(" Short file");41 bb_error_msg_and_die("short file"); 46 42 } 47 43 … … 68 64 free(line); 69 65 } 70 bb_error_msg_and_die(" Short file");66 bb_error_msg_and_die("short file"); 71 67 } 72 68 73 static intread_base64(FILE *src_stream, FILE *dst_stream)69 static void read_base64(FILE *src_stream, FILE *dst_stream) 74 70 { 75 static const char base64_table[] = 76 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"; 77 char term_count = 0; 71 int term_count = 1; 78 72 79 73 while (1) { … … 85 79 int ch; 86 80 87 /* Get next _valid_ character */ 81 /* Get next _valid_ character. 82 * global vector bb_uuenc_tbl_base64[] contains this string: 83 * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n" 84 */ 88 85 do { 89 86 ch = fgetc(src_stream); 90 87 if (ch == EOF) { 91 bb_error_msg_and_die(" Short file");88 bb_error_msg_and_die("short file"); 92 89 } 93 } while ((table_ptr = strchr(base64_table, ch)) == NULL); 94 95 /* Convert encoded charcter to decimal */ 96 ch = table_ptr - base64_table; 90 table_ptr = strchr(bb_uuenc_tbl_base64, ch); 91 } while (table_ptr == NULL); 92 93 /* Convert encoded character to decimal */ 94 ch = table_ptr - bb_uuenc_tbl_base64; 97 95 98 96 if (*table_ptr == '=') { 99 97 if (term_count == 0) { 100 translated[count] = 0;98 translated[count] = '\0'; 101 99 break; 102 100 } 103 101 term_count++; 104 } 105 else if (*table_ptr == '\n') { 102 } else if (*table_ptr == '\n') { 106 103 /* Check for terminating line */ 107 104 if (term_count == 5) { 108 return (EXIT_SUCCESS);105 return; 109 106 } 110 107 term_count = 1; … … 118 115 119 116 /* Merge 6 bit chars to 8 bit */ 120 fputc(translated[0] << 2 | translated[1] >> 4, dst_stream); 117 if (count > 1) { 118 fputc(translated[0] << 2 | translated[1] >> 4, dst_stream); 119 } 121 120 if (count > 2) { 122 121 fputc(translated[1] << 4 | translated[2] >> 2, dst_stream); … … 128 127 } 129 128 129 int uudecode_main(int argc, char **argv); 130 130 int uudecode_main(int argc, char **argv) 131 131 { 132 int (*decode_fn_ptr) (FILE * src, FILE * dst); 133 FILE *src_stream; 132 FILE *src_stream = stdin; 134 133 char *outname = NULL; 135 134 char *line; 136 int opt; 137 138 opt = bb_getopt_ulflags(argc, argv, "o:", &outname); 139 140 if (optind == argc) { 141 src_stream = stdin; 142 } else if (optind + 1 == argc) { 143 src_stream = bb_xfopen(argv[optind], "r"); 144 } else { 145 bb_show_usage(); 146 } 135 136 opt_complementary = "?1"; /* 1 argument max */ 137 getopt32(argv, "o:", &outname); 138 argv += optind; 139 140 if (argv[0]) 141 src_stream = xfopen(argv[0], "r"); 147 142 148 143 /* Search for the start of the encoding */ 149 while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) { 150 char *line_ptr = NULL; 151 152 if (line == NULL) { 153 break; 154 } else if (strncmp(line, "begin-base64 ", 13) == 0) { 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; 149 150 if (strncmp(line, "begin-base64 ", 13) == 0) { 155 151 line_ptr = line + 13; 156 152 decode_fn_ptr = read_base64; … … 158 154 line_ptr = line + 6; 159 155 decode_fn_ptr = read_stduu; 160 } 161 162 if (line_ptr) { 163 FILE *dst_stream; 164 int mode; 165 int ret; 166 167 mode = strtoul(line_ptr, NULL, 8); 168 if (outname == NULL) { 169 outname = strchr(line_ptr, ' '); 170 if ((outname == NULL) || (*outname == '\0')) { 171 break; 172 } 173 outname++; 174 } 175 if (strcmp(outname, "-") == 0) { 176 dst_stream = stdout; 177 } else { 178 dst_stream = bb_xfopen(outname, "w"); 179 chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)); 180 } 156 } else { 181 157 free(line); 182 ret = decode_fn_ptr(src_stream, dst_stream); 183 bb_fclose_nonstdin(src_stream); 184 return(ret); 158 continue; 159 } 160 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; 167 } 168 outname++; 169 } 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)); 185 174 } 186 175 free(line); 176 decode_fn_ptr(src_stream, dst_stream); 177 /* fclose_if_not_stdin(src_stream); - redundant */ 178 return EXIT_SUCCESS; 187 179 } 188 bb_error_msg_and_die(" No `begin' line");180 bb_error_msg_and_die("no 'begin' line"); 189 181 } 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 */
Note:
See TracChangeset
for help on using the changeset viewer.