uudecode.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* vi: set sw=4 ts=4: */
  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. #include "busybox.h"
  14. static int read_stduu(FILE *src_stream, FILE *dst_stream)
  15. {
  16. char *line;
  17. while ((line = xmalloc_getline(src_stream)) != NULL) {
  18. int length;
  19. char *line_ptr = line;
  20. if (strcmp(line, "end") == 0) {
  21. return EXIT_SUCCESS;
  22. }
  23. length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
  24. if (length <= 0) {
  25. /* Ignore the "`\n" line, why is it even in the encode file ? */
  26. continue;
  27. }
  28. if (length > 60) {
  29. bb_error_msg_and_die("line too long");
  30. }
  31. line_ptr++;
  32. /* Tolerate an overly long line to accomodate a possible exta '`' */
  33. if (strlen(line_ptr) < (size_t)length) {
  34. bb_error_msg_and_die("short file");
  35. }
  36. while (length > 0) {
  37. /* Merge four 6 bit chars to three 8 bit chars */
  38. fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream);
  39. line_ptr++;
  40. length--;
  41. if (length == 0) {
  42. break;
  43. }
  44. fputc(((line_ptr[0] - 0x20) & 077) << 4 | ((line_ptr[1] - 0x20) & 077) >> 2, dst_stream);
  45. line_ptr++;
  46. length--;
  47. if (length == 0) {
  48. break;
  49. }
  50. fputc(((line_ptr[0] - 0x20) & 077) << 6 | ((line_ptr[1] - 0x20) & 077), dst_stream);
  51. line_ptr += 2;
  52. length -= 2;
  53. }
  54. free(line);
  55. }
  56. bb_error_msg_and_die("short file");
  57. }
  58. static int read_base64(FILE *src_stream, FILE *dst_stream)
  59. {
  60. static const char base64_table[] =
  61. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
  62. char term_count = 0;
  63. while (1) {
  64. char translated[4];
  65. int count = 0;
  66. while (count < 4) {
  67. char *table_ptr;
  68. int ch;
  69. /* Get next _valid_ character */
  70. do {
  71. ch = fgetc(src_stream);
  72. if (ch == EOF) {
  73. bb_error_msg_and_die("short file");
  74. }
  75. } while ((table_ptr = strchr(base64_table, ch)) == NULL);
  76. /* Convert encoded charcter to decimal */
  77. ch = table_ptr - base64_table;
  78. if (*table_ptr == '=') {
  79. if (term_count == 0) {
  80. translated[count] = 0;
  81. break;
  82. }
  83. term_count++;
  84. }
  85. else if (*table_ptr == '\n') {
  86. /* Check for terminating line */
  87. if (term_count == 5) {
  88. return EXIT_SUCCESS;
  89. }
  90. term_count = 1;
  91. continue;
  92. } else {
  93. translated[count] = ch;
  94. count++;
  95. term_count = 0;
  96. }
  97. }
  98. /* Merge 6 bit chars to 8 bit */
  99. fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
  100. if (count > 2) {
  101. fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
  102. }
  103. if (count > 3) {
  104. fputc(translated[2] << 6 | translated[3], dst_stream);
  105. }
  106. }
  107. }
  108. int uudecode_main(int argc, char **argv)
  109. {
  110. FILE *src_stream;
  111. char *outname = NULL;
  112. char *line;
  113. getopt32(argc, argv, "o:", &outname);
  114. if (optind == argc) {
  115. src_stream = stdin;
  116. } else if (optind + 1 == argc) {
  117. src_stream = xfopen(argv[optind], "r");
  118. } else {
  119. bb_show_usage();
  120. }
  121. /* Search for the start of the encoding */
  122. while ((line = xmalloc_getline(src_stream)) != NULL) {
  123. int (*decode_fn_ptr)(FILE * src, FILE * dst);
  124. char *line_ptr;
  125. FILE *dst_stream;
  126. int mode;
  127. int ret;
  128. if (strncmp(line, "begin-base64 ", 13) == 0) {
  129. line_ptr = line + 13;
  130. decode_fn_ptr = read_base64;
  131. } else if (strncmp(line, "begin ", 6) == 0) {
  132. line_ptr = line + 6;
  133. decode_fn_ptr = read_stduu;
  134. } else {
  135. free(line);
  136. continue;
  137. }
  138. mode = strtoul(line_ptr, NULL, 8);
  139. if (outname == NULL) {
  140. outname = strchr(line_ptr, ' ');
  141. if ((outname == NULL) || (*outname == '\0')) {
  142. break;
  143. }
  144. outname++;
  145. }
  146. if (LONE_DASH(outname)) {
  147. dst_stream = stdout;
  148. } else {
  149. dst_stream = xfopen(outname, "w");
  150. chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
  151. }
  152. free(line);
  153. ret = decode_fn_ptr(src_stream, dst_stream);
  154. fclose_if_not_stdin(src_stream);
  155. return ret;
  156. }
  157. bb_error_msg_and_die("no 'begin' line");
  158. }