uudecode.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 source tree.
  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. //usage:#define uudecode_trivial_usage
  14. //usage: "[-o OUTFILE] [INFILE]"
  15. //usage:#define uudecode_full_usage "\n\n"
  16. //usage: "Uudecode a file\n"
  17. //usage: "Finds OUTFILE in uuencoded source unless -o is given"
  18. //usage:
  19. //usage:#define uudecode_example_usage
  20. //usage: "$ uudecode -o busybox busybox.uu\n"
  21. //usage: "$ ls -l busybox\n"
  22. //usage: "-rwxr-xr-x 1 ams ams 245264 Jun 7 21:35 busybox\n"
  23. #include "libbb.h"
  24. #if ENABLE_UUDECODE
  25. static void FAST_FUNC read_stduu(FILE *src_stream, FILE *dst_stream, int flags UNUSED_PARAM)
  26. {
  27. char *line;
  28. for (;;) {
  29. int encoded_len, str_len;
  30. char *line_ptr, *dst;
  31. size_t line_len;
  32. line_len = 64 * 1024;
  33. line = xmalloc_fgets_str_len(src_stream, "\n", &line_len);
  34. if (!line)
  35. break;
  36. /* Handle both Unix and MSDOS text, and stray trailing spaces */
  37. str_len = line_len;
  38. while (--str_len >= 0 && isspace(line[str_len]))
  39. line[str_len] = '\0';
  40. if (strcmp(line, "end") == 0) {
  41. return; /* the only non-error exit */
  42. }
  43. line_ptr = line;
  44. while (*line_ptr) {
  45. *line_ptr = (*line_ptr - 0x20) & 0x3f;
  46. line_ptr++;
  47. }
  48. str_len = line_ptr - line;
  49. encoded_len = line[0] * 4 / 3;
  50. /* Check that line is not too short. (we tolerate
  51. * overly _long_ line to accommodate possible extra '`').
  52. * Empty line case is also caught here. */
  53. if (str_len <= encoded_len) {
  54. break; /* go to bb_error_msg_and_die("short file"); */
  55. }
  56. if (encoded_len <= 0) {
  57. /* Ignore the "`\n" line, why is it even in the encode file ? */
  58. free(line);
  59. continue;
  60. }
  61. if (encoded_len > 60) {
  62. bb_error_msg_and_die("line too long");
  63. }
  64. dst = line;
  65. line_ptr = line + 1;
  66. do {
  67. /* Merge four 6 bit chars to three 8 bit chars */
  68. *dst++ = line_ptr[0] << 2 | line_ptr[1] >> 4;
  69. encoded_len--;
  70. if (encoded_len == 0) {
  71. break;
  72. }
  73. *dst++ = line_ptr[1] << 4 | line_ptr[2] >> 2;
  74. encoded_len--;
  75. if (encoded_len == 0) {
  76. break;
  77. }
  78. *dst++ = line_ptr[2] << 6 | line_ptr[3];
  79. line_ptr += 4;
  80. encoded_len -= 2;
  81. } while (encoded_len > 0);
  82. fwrite(line, 1, dst - line, dst_stream);
  83. free(line);
  84. }
  85. bb_error_msg_and_die("short file");
  86. }
  87. #endif
  88. #if ENABLE_UUDECODE
  89. int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  90. int uudecode_main(int argc UNUSED_PARAM, char **argv)
  91. {
  92. FILE *src_stream;
  93. char *outname = NULL;
  94. char *line;
  95. opt_complementary = "?1"; /* 1 argument max */
  96. getopt32(argv, "o:", &outname);
  97. argv += optind;
  98. if (!argv[0])
  99. *--argv = (char*)"-";
  100. src_stream = xfopen_stdin(argv[0]);
  101. /* Search for the start of the encoding */
  102. while ((line = xmalloc_fgetline(src_stream)) != NULL) {
  103. void FAST_FUNC (*decode_fn_ptr)(FILE *src, FILE *dst, int flags);
  104. char *line_ptr;
  105. FILE *dst_stream;
  106. int mode;
  107. if (is_prefixed_with(line, "begin-base64 ")) {
  108. line_ptr = line + 13;
  109. decode_fn_ptr = read_base64;
  110. } else if (is_prefixed_with(line, "begin ")) {
  111. line_ptr = line + 6;
  112. decode_fn_ptr = read_stduu;
  113. } else {
  114. free(line);
  115. continue;
  116. }
  117. /* begin line found. decode and exit */
  118. mode = bb_strtou(line_ptr, NULL, 8);
  119. if (outname == NULL) {
  120. outname = strchr(line_ptr, ' ');
  121. if (!outname)
  122. break;
  123. outname++;
  124. trim(outname); /* remove trailing space (and '\r' for DOS text) */
  125. if (!outname[0])
  126. break;
  127. }
  128. dst_stream = stdout;
  129. if (NOT_LONE_DASH(outname)) {
  130. dst_stream = xfopen_for_write(outname);
  131. fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
  132. }
  133. free(line);
  134. decode_fn_ptr(src_stream, dst_stream, /*flags:*/ BASE64_FLAG_UU_STOP + BASE64_FLAG_NO_STOP_CHAR);
  135. /* fclose_if_not_stdin(src_stream); - redundant */
  136. return EXIT_SUCCESS;
  137. }
  138. bb_error_msg_and_die("no 'begin' line");
  139. }
  140. #endif
  141. //applet:IF_BASE64(APPLET(base64, BB_DIR_BIN, BB_SUID_DROP))
  142. //kbuild:lib-$(CONFIG_BASE64) += uudecode.o
  143. //config:config BASE64
  144. //config: bool "base64"
  145. //config: default y
  146. //config: help
  147. //config: Base64 encode and decode
  148. //usage:#define base64_trivial_usage
  149. //usage: "[-d] [FILE]"
  150. //usage:#define base64_full_usage "\n\n"
  151. //usage: "Base64 encode or decode FILE to standard output"
  152. //usage: "\n -d Decode data"
  153. ////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)"
  154. ////usage: "\n -i When decoding, ignore non-alphabet characters"
  155. #if ENABLE_BASE64
  156. int base64_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  157. int base64_main(int argc UNUSED_PARAM, char **argv)
  158. {
  159. FILE *src_stream;
  160. unsigned opts;
  161. opt_complementary = "?1"; /* 1 argument max */
  162. opts = getopt32(argv, "d");
  163. argv += optind;
  164. if (!argv[0])
  165. *--argv = (char*)"-";
  166. src_stream = xfopen_stdin(argv[0]);
  167. if (opts) {
  168. read_base64(src_stream, stdout, /*flags:*/ (char)EOF);
  169. } else {
  170. enum {
  171. SRC_BUF_SIZE = 76/4*3, /* This *MUST* be a multiple of 3 */
  172. DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
  173. };
  174. char src_buf[SRC_BUF_SIZE];
  175. char dst_buf[DST_BUF_SIZE + 1];
  176. int src_fd = fileno(src_stream);
  177. while (1) {
  178. size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
  179. if (!size)
  180. break;
  181. if ((ssize_t)size < 0)
  182. bb_perror_msg_and_die(bb_msg_read_error);
  183. /* Encode the buffer we just read in */
  184. bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
  185. xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3));
  186. bb_putchar('\n');
  187. fflush(stdout);
  188. }
  189. }
  190. fflush_stdout_and_exit(EXIT_SUCCESS);
  191. }
  192. #endif
  193. /* Test script.
  194. Put this into an empty dir with busybox binary, an run.
  195. #!/bin/sh
  196. test -x busybox || { echo "No ./busybox?"; exit; }
  197. ln -sf busybox uudecode
  198. ln -sf busybox uuencode
  199. >A_null
  200. echo -n A >A
  201. echo -n AB >AB
  202. echo -n ABC >ABC
  203. echo -n ABCD >ABCD
  204. echo -n ABCDE >ABCDE
  205. echo -n ABCDEF >ABCDEF
  206. cat busybox >A_bbox
  207. for f in A*; do
  208. echo uuencode $f
  209. ./uuencode $f <$f >u_$f
  210. ./uuencode -m $f <$f >m_$f
  211. done
  212. mkdir unpk_u unpk_m 2>/dev/null
  213. for f in u_*; do
  214. ./uudecode <$f -o unpk_u/${f:2}
  215. diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
  216. echo uudecode $f: $?
  217. done
  218. for f in m_*; do
  219. ./uudecode <$f -o unpk_m/${f:2}
  220. diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
  221. echo uudecode $f: $?
  222. done
  223. */