uudecode.c 6.6 KB

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