uudecode.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * GPLv2
  3. * Copyright 2003, Glenn McGrath <bug1@iinet.net.au>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation; either version 2 of the License.
  8. *
  9. * This program 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 Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Based on specification from
  19. * http://www.opengroup.org/onlinepubs/007904975/utilities/uuencode.html
  20. *
  21. * Bugs: the spec doesnt mention anything about "`\n`\n" prior to the "end" line
  22. */
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <getopt.h> /* optind */
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "busybox.h"
  29. static int read_stduu(FILE *src_stream, FILE *dst_stream)
  30. {
  31. char *line;
  32. while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
  33. int length;
  34. char *line_ptr = line;
  35. if (strcmp(line, "end") == 0) {
  36. return(EXIT_SUCCESS);
  37. }
  38. length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
  39. if (length <= 0) {
  40. /* Ignore the "`\n" line, why is it even in the encode file ? */
  41. continue;
  42. }
  43. if (length > 60) {
  44. bb_error_msg_and_die("Line too long");
  45. }
  46. line_ptr++;
  47. /* Tolerate an overly long line to acomadate a possible exta '`' */
  48. if (strlen(line_ptr) < (size_t)length) {
  49. bb_error_msg_and_die("Short file");
  50. }
  51. while (length > 0) {
  52. /* Merge four 6 bit chars to three 8 bit chars */
  53. fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream);
  54. line_ptr++;
  55. length--;
  56. if (length == 0) {
  57. break;
  58. }
  59. fputc(((line_ptr[0] - 0x20) & 077) << 4 | ((line_ptr[1] - 0x20) & 077) >> 2, dst_stream);
  60. line_ptr++;
  61. length--;
  62. if (length == 0) {
  63. break;
  64. }
  65. fputc(((line_ptr[0] - 0x20) & 077) << 6 | ((line_ptr[1] - 0x20) & 077), dst_stream);
  66. line_ptr += 2;
  67. length -= 2;
  68. }
  69. free(line);
  70. }
  71. bb_error_msg_and_die("Short file");
  72. }
  73. static int read_base64(FILE *src_stream, FILE *dst_stream)
  74. {
  75. static const char base64_table[] =
  76. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
  77. char term_count = 0;
  78. while (1) {
  79. char translated[4];
  80. int count = 0;
  81. while (count < 4) {
  82. char *table_ptr;
  83. int ch;
  84. /* Get next _valid_ character */
  85. do {
  86. ch = fgetc(src_stream);
  87. if (ch == EOF) {
  88. bb_error_msg_and_die("Short file");
  89. }
  90. } while ((table_ptr = strchr(base64_table, ch)) == NULL);
  91. /* Convert encoded charcter to decimal */
  92. ch = table_ptr - base64_table;
  93. if (*table_ptr == '=') {
  94. if (term_count == 0) {
  95. translated[count] = 0;
  96. break;
  97. }
  98. term_count++;
  99. }
  100. else if (*table_ptr == '\n') {
  101. /* Check for terminating line */
  102. if (term_count == 5) {
  103. return(EXIT_SUCCESS);
  104. }
  105. term_count = 1;
  106. continue;
  107. } else {
  108. translated[count] = ch;
  109. count++;
  110. term_count = 0;
  111. }
  112. }
  113. /* Merge 6 bit chars to 8 bit */
  114. fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
  115. if (count > 2) {
  116. fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
  117. }
  118. if (count > 3) {
  119. fputc(translated[2] << 6 | translated[3], dst_stream);
  120. }
  121. }
  122. }
  123. int uudecode_main(int argc, char **argv)
  124. {
  125. int (*decode_fn_ptr) (FILE * src, FILE * dst);
  126. FILE *src_stream;
  127. char *outname = NULL;
  128. char *line;
  129. int opt;
  130. opt = bb_getopt_ulflags(argc, argv, "o:", &outname);
  131. if (optind == argc) {
  132. src_stream = stdin;
  133. } else if (optind + 1 == argc) {
  134. src_stream = bb_xfopen(argv[optind], "r");
  135. } else {
  136. bb_show_usage();
  137. }
  138. /* Search for the start of the encoding */
  139. while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
  140. char *line_ptr = NULL;
  141. if (line == NULL) {
  142. break;
  143. } else if (strncmp(line, "begin-base64 ", 13) == 0) {
  144. line_ptr = line + 13;
  145. decode_fn_ptr = read_base64;
  146. } else if (strncmp(line, "begin ", 6) == 0) {
  147. line_ptr = line + 6;
  148. decode_fn_ptr = read_stduu;
  149. }
  150. if (line_ptr) {
  151. FILE *dst_stream;
  152. int mode;
  153. int ret;
  154. mode = strtoul(line_ptr, NULL, 8);
  155. if (outname == NULL) {
  156. outname = strchr(line_ptr, ' ');
  157. if ((outname == NULL) || (*outname == '\0')) {
  158. break;
  159. }
  160. outname++;
  161. }
  162. if (strcmp(outname, "-") == 0) {
  163. dst_stream = stdout;
  164. } else {
  165. dst_stream = bb_xfopen(outname, "w");
  166. chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
  167. }
  168. free(line);
  169. ret = decode_fn_ptr(src_stream, dst_stream);
  170. bb_fclose_nonstdin(src_stream);
  171. return(ret);
  172. }
  173. free(line);
  174. }
  175. bb_error_msg_and_die("No `begin' line");
  176. }