uudecode.c 5.6 KB

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