3
0

uudecode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. /* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/uudecode.html
  141. * https://pubs.opengroup.org/onlinepubs/9699919799/utilities/uuencode.html
  142. * The above says that output file name specified in input file
  143. * or overridden by -o OUTFILE can be special "/dev/stdout" string.
  144. * This usually works "implicitly": many systems have /dev/stdout.
  145. * If ENABLE_DESKTOP, support that explicitly:
  146. */
  147. && (!ENABLE_DESKTOP || strcmp(outname, "/dev/stdout") != 0)
  148. ) {
  149. dst_stream = xfopen_for_write(outname);
  150. fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
  151. }
  152. free(line);
  153. decode_fn_ptr(src_stream, dst_stream, /*flags:*/ BASE64_FLAG_UU_STOP + BASE64_FLAG_NO_STOP_CHAR);
  154. /* fclose_if_not_stdin(src_stream); - redundant */
  155. return EXIT_SUCCESS;
  156. }
  157. bb_simple_error_msg_and_die("no 'begin' line");
  158. }
  159. #endif
  160. //config:config BASE32
  161. //config: bool "base32 (4.9 kb)"
  162. //config: default y
  163. //config: help
  164. //config: Base32 encode and decode
  165. //config:config BASE64
  166. //config: bool "base64 (4.9 kb)"
  167. //config: default y
  168. //config: help
  169. //config: Base64 encode and decode
  170. //usage:#define base32_trivial_usage
  171. //usage: "[-d] [-w COL] [FILE]"
  172. //usage:#define base32_full_usage "\n\n"
  173. //usage: "Base32 encode or decode FILE to standard output\n"
  174. //usage: "\n -d Decode data"
  175. //usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)"
  176. ////usage: "\n -i When decoding, ignore non-alphabet characters"
  177. //usage:#define base64_trivial_usage
  178. //usage: "[-d] [-w COL] [FILE]"
  179. //usage:#define base64_full_usage "\n\n"
  180. //usage: "Base64 encode or decode FILE to standard output\n"
  181. //usage: "\n -d Decode data"
  182. //usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)"
  183. ///////: "\n -i When decoding, ignore non-alphabet characters"
  184. // -i is accepted but has no effect: currently, decode_base32/64() functions
  185. // (called via read_base64()) skip invalid chars unconditionally.
  186. // APPLET_ODDNAME:name main location suid_type help
  187. //applet:IF_BASE32(APPLET_ODDNAME(base32, baseNUM, BB_DIR_BIN, BB_SUID_DROP, base32))
  188. //applet:IF_BASE64(APPLET_ODDNAME(base64, baseNUM, BB_DIR_BIN, BB_SUID_DROP, base64))
  189. //kbuild:lib-$(CONFIG_BASE64) += uudecode.o
  190. //kbuild:lib-$(CONFIG_BASE32) += uudecode.o
  191. #if ENABLE_BASE32 || ENABLE_BASE64
  192. # if ENABLE_BASE32
  193. static void bb_b32encode(char *p, const void *src, int length)
  194. {
  195. #define tbl bb_uuenc_tbl_base32
  196. const unsigned char *s = src;
  197. /* Transform 5x8 bits to 8x5 bits */
  198. while (length > 0) {
  199. unsigned cur, next;
  200. length--;
  201. cur = *s++;
  202. *p++ = tbl[cur >> 3]; // xxxxx--- -------- -------- -------- --------
  203. cur &= 7;
  204. next = 0;
  205. if (--length >= 0)
  206. next = *s++;
  207. *p++ = tbl[(cur << 2) + (next >> 6)]; // -----xxx xx------ -------- -------- --------
  208. cur = next & 0x3f;
  209. *p++ = tbl[cur >> 1]; // -------- --xxxxx- -------- -------- --------
  210. cur &= 1;
  211. next = 0;
  212. if (--length >= 0)
  213. next = *s++;
  214. *p++ = tbl[(cur << 4) + (next >> 4)]; // -------- -------x xxxx---- -------- --------
  215. cur = next & 0xf;
  216. next = 0;
  217. if (--length >= 0)
  218. next = *s++;
  219. *p++ = tbl[(cur << 1) + (next >> 7)]; // -------- -------- ----xxxx x------- --------
  220. cur = next & 0x7f;
  221. *p++ = tbl[cur >> 2]; // -------- -------- -------- -xxxxx-- --------
  222. cur &= 3;
  223. next = 0;
  224. if (--length >= 0)
  225. next = *s++;
  226. *p++ = tbl[(cur << 3) + (next >> 5)]; // -------- -------- -------- ------xx xxx-----
  227. cur = next & 0x1f;
  228. *p++ = tbl[cur]; // -------- -------- -------- -------- ---xxxxx
  229. }
  230. #undef tbl
  231. /* Zero-terminate */
  232. *p = '\0';
  233. /* Pad as necessary */
  234. length = ((-length) * 3) >> 1; /* -4 => 6 pad chars, -3 => 4, -2 => 3, -1 => 1 */
  235. while (length--) {
  236. *--p = '=';
  237. }
  238. }
  239. # else
  240. void bb_b32encode(char *p, const void *src, int length); /* undefined */
  241. # endif
  242. int baseNUM_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  243. int baseNUM_main(int argc UNUSED_PARAM, char **argv)
  244. {
  245. FILE *src_stream;
  246. unsigned opts;
  247. unsigned col = 76;
  248. opts = getopt32(argv, "^" "diw:+" "\0" "?1"/* 1 arg max*/, &col);
  249. argv += optind;
  250. if (!argv[0])
  251. *--argv = (char*)"-";
  252. src_stream = xfopen_stdin(argv[0]);
  253. if (opts & 1) {
  254. /* -d: decode */
  255. int flags = (unsigned char)EOF;
  256. if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3'))
  257. flags = ((unsigned char)EOF) | BASE64_32;
  258. read_base64(src_stream, stdout, flags);
  259. } else {
  260. enum {
  261. SRC_BUF_SIZE = 3 * 5 * 32, /* this *MUST* be a multiple of 3 and 5 */
  262. DST_BUF_SIZE = 8 * ((SRC_BUF_SIZE + 4) / 5), /* max growth on encode (base32 case) */
  263. };
  264. /* Use one buffer for both input and output:
  265. * encoding reads input "left-to-right",
  266. * it's safe to place source at the end of the buffer and
  267. * overwrite it while encoding, just be careful to have a gap.
  268. */
  269. char dst_buf[((DST_BUF_SIZE + /*gap:*/ 16) /*round up to 16:*/ | 0xf) + 1];
  270. #define src_buf (dst_buf + sizeof(dst_buf) - SRC_BUF_SIZE)
  271. int src_fd, rem;
  272. src_fd = fileno(src_stream);
  273. rem = 0;
  274. while (1) {
  275. size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE);
  276. if ((ssize_t)size < 0)
  277. bb_simple_perror_msg_and_die(bb_msg_read_error);
  278. if (size == 0) {
  279. if (rem != 0) bb_putchar('\n');
  280. break;
  281. }
  282. /* Encode the buffer we just read in */
  283. if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) {
  284. bb_b32encode(dst_buf, src_buf, size);
  285. size = 8 * ((size + 4) / 5);
  286. } else {
  287. bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
  288. size = 4 * ((size + 2) / 3);
  289. }
  290. if (col == 0) {
  291. fputs_stdout(dst_buf);
  292. } else {
  293. char *result = dst_buf;
  294. if (rem == 0)
  295. rem = col;
  296. while (1) {
  297. int out = size < rem ? size : rem;
  298. rem -= out;
  299. printf(rem != 0 ? "%.*s" : "%.*s\n", out, result);
  300. if (rem != 0)
  301. break;
  302. size -= out;
  303. if (size == 0)
  304. break;
  305. result += out;
  306. rem = col;
  307. }
  308. }
  309. }
  310. #undef src_buf
  311. }
  312. fflush_stdout_and_exit_SUCCESS();
  313. }
  314. #endif
  315. /* Test script.
  316. Put this into an empty dir with busybox binary, an run.
  317. #!/bin/sh
  318. test -x busybox || { echo "No ./busybox?"; exit; }
  319. ln -sf busybox uudecode
  320. ln -sf busybox uuencode
  321. >A_null
  322. echo -n A >A
  323. echo -n AB >AB
  324. echo -n ABC >ABC
  325. echo -n ABCD >ABCD
  326. echo -n ABCDE >ABCDE
  327. echo -n ABCDEF >ABCDEF
  328. cat busybox >A_bbox
  329. for f in A*; do
  330. echo uuencode $f
  331. ./uuencode $f <$f >u_$f
  332. ./uuencode -m $f <$f >m_$f
  333. done
  334. mkdir unpk_u unpk_m 2>/dev/null
  335. for f in u_*; do
  336. ./uudecode <$f -o unpk_u/${f:2}
  337. diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
  338. echo uudecode $f: $?
  339. done
  340. for f in m_*; do
  341. ./uudecode <$f -o unpk_m/${f:2}
  342. diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
  343. echo uudecode $f: $?
  344. done
  345. */