uudecode.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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.8 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_simple_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_simple_error_msg_and_die("short file");
  99. }
  100. int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  101. int uudecode_main(int argc UNUSED_PARAM, char **argv)
  102. {
  103. FILE *src_stream;
  104. char *outname = NULL;
  105. char *line;
  106. getopt32(argv, "^" "o:" "\0" "?1"/* 1 arg max*/, &outname);
  107. argv += optind;
  108. if (!argv[0])
  109. *--argv = (char*)"-";
  110. src_stream = xfopen_stdin(argv[0]);
  111. /* Search for the start of the encoding */
  112. while ((line = xmalloc_fgetline(src_stream)) != NULL) {
  113. void FAST_FUNC (*decode_fn_ptr)(FILE *src, FILE *dst, int flags);
  114. char *line_ptr;
  115. FILE *dst_stream;
  116. int mode;
  117. if (is_prefixed_with(line, "begin-base64 ")) {
  118. line_ptr = line + 13;
  119. decode_fn_ptr = read_base64;
  120. } else if (is_prefixed_with(line, "begin ")) {
  121. line_ptr = line + 6;
  122. decode_fn_ptr = read_stduu;
  123. } else {
  124. free(line);
  125. continue;
  126. }
  127. /* begin line found. decode and exit */
  128. mode = bb_strtou(line_ptr, NULL, 8);
  129. if (outname == NULL) {
  130. outname = strchr(line_ptr, ' ');
  131. if (!outname)
  132. break;
  133. outname++;
  134. trim(outname); /* remove trailing space (and '\r' for DOS text) */
  135. if (!outname[0])
  136. break;
  137. }
  138. dst_stream = stdout;
  139. if (NOT_LONE_DASH(outname)) {
  140. dst_stream = xfopen_for_write(outname);
  141. fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
  142. }
  143. free(line);
  144. decode_fn_ptr(src_stream, dst_stream, /*flags:*/ BASE64_FLAG_UU_STOP + BASE64_FLAG_NO_STOP_CHAR);
  145. /* fclose_if_not_stdin(src_stream); - redundant */
  146. return EXIT_SUCCESS;
  147. }
  148. bb_simple_error_msg_and_die("no 'begin' line");
  149. }
  150. #endif
  151. //config:config BASE32
  152. //config: bool "base32 (4.9 kb)"
  153. //config: default y
  154. //config: help
  155. //config: Base32 encode and decode
  156. //config:config BASE64
  157. //config: bool "base64 (4.9 kb)"
  158. //config: default y
  159. //config: help
  160. //config: Base64 encode and decode
  161. //usage:#define base32_trivial_usage
  162. //usage: "[-d] [-w COL] [FILE]"
  163. //usage:#define base32_full_usage "\n\n"
  164. //usage: "Base32 encode or decode FILE to standard output"
  165. //usage: "\n -d Decode data"
  166. //usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)"
  167. ////usage: "\n -i When decoding, ignore non-alphabet characters"
  168. //usage:#define base64_trivial_usage
  169. //usage: "[-d] [-w COL] [FILE]"
  170. //usage:#define base64_full_usage "\n\n"
  171. //usage: "Base64 encode or decode FILE to standard output"
  172. //usage: "\n -d Decode data"
  173. //usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)"
  174. ////usage: "\n -i When decoding, ignore non-alphabet characters"
  175. // APPLET_ODDNAME:name main location suid_type help
  176. //applet:IF_BASE32(APPLET_ODDNAME(base32, baseNUM, BB_DIR_BIN, BB_SUID_DROP, base32))
  177. //applet:IF_BASE64(APPLET_ODDNAME(base64, baseNUM, BB_DIR_BIN, BB_SUID_DROP, base64))
  178. //kbuild:lib-$(CONFIG_BASE64) += uudecode.o
  179. //kbuild:lib-$(CONFIG_BASE32) += uudecode.o
  180. #if ENABLE_BASE32 || ENABLE_BASE64
  181. # if ENABLE_BASE32
  182. static void bb_b32encode(char *p, const void *src, int length)
  183. {
  184. #define tbl bb_uuenc_tbl_base32
  185. const unsigned char *s = src;
  186. /* Transform 5x8 bits to 8x5 bits */
  187. while (length > 0) {
  188. unsigned cur, next;
  189. length--;
  190. cur = *s++;
  191. *p++ = tbl[cur >> 3]; // xxxxx--- -------- -------- -------- --------
  192. cur &= 7;
  193. next = 0;
  194. if (--length >= 0)
  195. next = *s++;
  196. *p++ = tbl[(cur << 2) + (next >> 6)]; // -----xxx xx------ -------- -------- --------
  197. cur = next & 0x3f;
  198. *p++ = tbl[cur >> 1]; // -------- --xxxxx- -------- -------- --------
  199. cur &= 1;
  200. next = 0;
  201. if (--length >= 0)
  202. next = *s++;
  203. *p++ = tbl[(cur << 4) + (next >> 4)]; // -------- -------x xxxx---- -------- --------
  204. cur = next & 0xf;
  205. next = 0;
  206. if (--length >= 0)
  207. next = *s++;
  208. *p++ = tbl[(cur << 1) + (next >> 7)]; // -------- -------- ----xxxx x------- --------
  209. cur = next & 0x7f;
  210. *p++ = tbl[cur >> 2]; // -------- -------- -------- -xxxxx-- --------
  211. cur &= 3;
  212. next = 0;
  213. if (--length >= 0)
  214. next = *s++;
  215. *p++ = tbl[(cur << 3) + (next >> 5)]; // -------- -------- -------- ------xx xxx-----
  216. cur = next & 0x1f;
  217. *p++ = tbl[cur]; // -------- -------- -------- -------- ---xxxxx
  218. }
  219. #undef tbl
  220. /* Zero-terminate */
  221. *p = '\0';
  222. /* Pad as necessary */
  223. length = ((-length) * 3) >> 1; /* -4 => 6 pad chars, -3 => 4, -2 => 3, -1 => 1 */
  224. while (length--) {
  225. *--p = '=';
  226. }
  227. }
  228. # else
  229. void bb_b32encode(char *p, const void *src, int length); /* undefined */
  230. # endif
  231. int baseNUM_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  232. int baseNUM_main(int argc UNUSED_PARAM, char **argv)
  233. {
  234. FILE *src_stream;
  235. unsigned opts;
  236. unsigned col = 76;
  237. opts = getopt32(argv, "^" "dw:+" "\0" "?1"/* 1 arg max*/, &col);
  238. argv += optind;
  239. if (!argv[0])
  240. *--argv = (char*)"-";
  241. src_stream = xfopen_stdin(argv[0]);
  242. if (opts & 1) {
  243. /* -d: decode */
  244. int flags = (unsigned char)EOF;
  245. if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3'))
  246. flags = ((unsigned char)EOF) | BASE64_32;
  247. read_base64(src_stream, stdout, flags);
  248. } else {
  249. enum {
  250. SRC_BUF_SIZE = 3 * 5 * 32, /* this *MUST* be a multiple of 3 and 5 */
  251. DST_BUF_SIZE = 8 * ((SRC_BUF_SIZE + 4) / 5), /* max growth on encode (base32 case) */
  252. };
  253. /* Use one buffer for both input and output:
  254. * encoding reads input "left-to-right",
  255. * it's safe to place source at the end of the buffer and
  256. * overwrite it while encoding, just be careful to have a gap.
  257. */
  258. char dst_buf[((DST_BUF_SIZE + /*gap:*/ 16) /*round up to 16:*/ | 0xf) + 1];
  259. #define src_buf (dst_buf + sizeof(dst_buf) - SRC_BUF_SIZE)
  260. int src_fd, rem;
  261. src_fd = fileno(src_stream);
  262. rem = 0;
  263. while (1) {
  264. size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
  265. if ((ssize_t)size < 0)
  266. bb_simple_perror_msg_and_die(bb_msg_read_error);
  267. if (size == 0) {
  268. if (rem != 0) bb_putchar('\n');
  269. break;
  270. }
  271. /* Encode the buffer we just read in */
  272. if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) {
  273. bb_b32encode(dst_buf, src_buf, size);
  274. size = 8 * ((size + 4) / 5);
  275. } else {
  276. bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
  277. size = 4 * ((size + 2) / 3);
  278. }
  279. if (col == 0) {
  280. fputs(dst_buf, stdout);
  281. } else {
  282. char *result = dst_buf;
  283. if (rem == 0)
  284. rem = col;
  285. while (1) {
  286. int out = size < rem ? size : rem;
  287. rem -= out;
  288. printf(rem != 0 ? "%.*s" : "%.*s\n", out, result);
  289. if (rem != 0)
  290. break;
  291. size -= out;
  292. if (size == 0)
  293. break;
  294. result += out;
  295. rem = col;
  296. }
  297. }
  298. }
  299. #undef src_buf
  300. }
  301. fflush_stdout_and_exit(EXIT_SUCCESS);
  302. }
  303. #endif
  304. /* Test script.
  305. Put this into an empty dir with busybox binary, an run.
  306. #!/bin/sh
  307. test -x busybox || { echo "No ./busybox?"; exit; }
  308. ln -sf busybox uudecode
  309. ln -sf busybox uuencode
  310. >A_null
  311. echo -n A >A
  312. echo -n AB >AB
  313. echo -n ABC >ABC
  314. echo -n ABCD >ABCD
  315. echo -n ABCDE >ABCDE
  316. echo -n ABCDEF >ABCDEF
  317. cat busybox >A_bbox
  318. for f in A*; do
  319. echo uuencode $f
  320. ./uuencode $f <$f >u_$f
  321. ./uuencode -m $f <$f >m_$f
  322. done
  323. mkdir unpk_u unpk_m 2>/dev/null
  324. for f in u_*; do
  325. ./uudecode <$f -o unpk_u/${f:2}
  326. diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
  327. echo uudecode $f: $?
  328. done
  329. for f in m_*; do
  330. ./uudecode <$f -o unpk_m/${f:2}
  331. diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
  332. echo uudecode $f: $?
  333. done
  334. */