uudecode.c 6.0 KB

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