uudecode.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright 2003, Glenn McGrath
  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 "libbb.h"
  14. static void 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;
  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 void read_base64(FILE *src_stream, FILE *dst_stream)
  59. {
  60. int term_count = 1;
  61. while (1) {
  62. char translated[4];
  63. int count = 0;
  64. while (count < 4) {
  65. char *table_ptr;
  66. int ch;
  67. /* Get next _valid_ character.
  68. * global vector bb_uuenc_tbl_base64[] contains this string:
  69. * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"
  70. */
  71. do {
  72. ch = fgetc(src_stream);
  73. if (ch == EOF) {
  74. bb_error_msg_and_die("short file");
  75. }
  76. table_ptr = strchr(bb_uuenc_tbl_base64, ch);
  77. } while (table_ptr == NULL);
  78. /* Convert encoded character to decimal */
  79. ch = table_ptr - bb_uuenc_tbl_base64;
  80. if (*table_ptr == '=') {
  81. if (term_count == 0) {
  82. translated[count] = '\0';
  83. break;
  84. }
  85. term_count++;
  86. } else if (*table_ptr == '\n') {
  87. /* Check for terminating line */
  88. if (term_count == 5) {
  89. return;
  90. }
  91. term_count = 1;
  92. continue;
  93. } else {
  94. translated[count] = ch;
  95. count++;
  96. term_count = 0;
  97. }
  98. }
  99. /* Merge 6 bit chars to 8 bit */
  100. if (count > 1) {
  101. fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
  102. }
  103. if (count > 2) {
  104. fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
  105. }
  106. if (count > 3) {
  107. fputc(translated[2] << 6 | translated[3], dst_stream);
  108. }
  109. }
  110. }
  111. int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  112. int uudecode_main(int argc, char **argv)
  113. {
  114. FILE *src_stream = stdin;
  115. char *outname = NULL;
  116. char *line;
  117. opt_complementary = "?1"; /* 1 argument max */
  118. getopt32(argv, "o:", &outname);
  119. argv += optind;
  120. if (argv[0])
  121. src_stream = xfopen(argv[0], "r");
  122. /* Search for the start of the encoding */
  123. while ((line = xmalloc_getline(src_stream)) != NULL) {
  124. void (*decode_fn_ptr)(FILE * src, FILE * dst);
  125. char *line_ptr;
  126. FILE *dst_stream;
  127. int mode;
  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. /* begin line found. decode and exit */
  139. mode = strtoul(line_ptr, NULL, 8);
  140. if (outname == NULL) {
  141. outname = strchr(line_ptr, ' ');
  142. if ((outname == NULL) || (*outname == '\0')) {
  143. break;
  144. }
  145. outname++;
  146. }
  147. dst_stream = stdout;
  148. if (NOT_LONE_DASH(outname)) {
  149. dst_stream = xfopen(outname, "w");
  150. chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
  151. }
  152. free(line);
  153. decode_fn_ptr(src_stream, dst_stream);
  154. /* fclose_if_not_stdin(src_stream); - redundant */
  155. return EXIT_SUCCESS;
  156. }
  157. bb_error_msg_and_die("no 'begin' line");
  158. }
  159. /* Test script.
  160. Put this into an empty dir with busybox binary, an run.
  161. #!/bin/sh
  162. test -x busybox || { echo "No ./busybox?"; exit; }
  163. ln -sf busybox uudecode
  164. ln -sf busybox uuencode
  165. >A_null
  166. echo -n A >A
  167. echo -n AB >AB
  168. echo -n ABC >ABC
  169. echo -n ABCD >ABCD
  170. echo -n ABCDE >ABCDE
  171. echo -n ABCDEF >ABCDEF
  172. cat busybox >A_bbox
  173. for f in A*; do
  174. echo uuencode $f
  175. ./uuencode $f <$f >u_$f
  176. ./uuencode -m $f <$f >m_$f
  177. done
  178. mkdir unpk_u unpk_m 2>/dev/null
  179. for f in u_*; do
  180. ./uudecode <$f -o unpk_u/${f:2}
  181. diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
  182. echo uudecode $f: $?
  183. done
  184. for f in m_*; do
  185. ./uudecode <$f -o unpk_m/${f:2}
  186. diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
  187. echo uudecode $f: $?
  188. done
  189. */