/* Base64-encode character string. */ static char *base64enc(const char *str) { unsigned len = strlen(str); if (len > sizeof(G.wget_buf)/4*3 - 10) /* paranoia */ len = sizeof(G.wget_buf)/4*3 - 10; bb_uuencode(G.wget_buf, str, len, bb_uuenc_tbl_base64); return G.wget_buf; }
/* Base64-encode character string. buf is assumed to be char buf[512]. */ static char *base64enc_512(char buf[512], const char *str) { unsigned len = strlen(str); if (len > 512/4*3 - 10) /* paranoia */ len = 512/4*3 - 10; bb_uuencode(buf, str, len, bb_uuenc_tbl_base64); return buf; }
void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol) { enum { SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */ DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), }; #define src_buf text char src[SRC_BUF_SIZE]; FILE *fp = fp; ssize_t len = len; char dst_buf[DST_BUF_SIZE + 1]; if (fname) { fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text; src_buf = src; } else if (text) { // though we do not call uuencode(NULL, NULL) explicitly // still we do not want to break things suddenly len = strlen(text); } else return; while (1) { size_t size; if (fname) { size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp); if ((ssize_t)size < 0) bb_perror_msg_and_die(bb_msg_read_error); } else { size = len; if (len > SRC_BUF_SIZE) size = SRC_BUF_SIZE; } if (!size) break; // encode the buffer we just read in bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); if (fname) { printf("%s\n", eol); } else { src_buf += size; len -= size; } fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout); } if (fname && NOT_LONE_DASH(fname)) fclose(fp); #undef src_buf }
int uuencode_main(int argc UNUSED_PARAM, char **argv) { struct stat stat_buf; int src_fd = STDIN_FILENO; const char *tbl; mode_t mode; char src_buf[SRC_BUF_SIZE]; char dst_buf[DST_BUF_SIZE + 1]; tbl = bb_uuenc_tbl_std; mode = 0666 & ~umask(0666); opt_complementary = "-1:?2"; /* must have 1 or 2 args */ if (getopt32(argv, "m")) { tbl = bb_uuenc_tbl_base64; } argv += optind; if (argv[1]) { src_fd = xopen(*argv, O_RDONLY); fstat(src_fd, &stat_buf); mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); argv++; } printf("begin%s %o %s", tbl == bb_uuenc_tbl_std ? "" : "-base64", mode, *argv); while (1) { size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE); if (!size) break; if ((ssize_t)size < 0) bb_perror_msg_and_die(bb_msg_read_error); /* Encode the buffer we just read in */ bb_uuencode(dst_buf, src_buf, size, tbl); bb_putchar('\n'); if (tbl == bb_uuenc_tbl_std) { bb_putchar(tbl[size]); } fflush(stdout); xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3)); } printf(tbl == bb_uuenc_tbl_std ? "\n`\nend\n" : "\n====\n"); fflush_stdout_and_exit(EXIT_SUCCESS); }