10f66f451Sopenharmony_ci/* uuencode.c - uuencode / base64 encode 20f66f451Sopenharmony_ci * 30f66f451Sopenharmony_ci * Copyright 2013 Erich Plondke <toybox@erich.wreck.org> 40f66f451Sopenharmony_ci * 50f66f451Sopenharmony_ci * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uuencode.html 60f66f451Sopenharmony_ci 70f66f451Sopenharmony_ciUSE_UUENCODE(NEWTOY(uuencode, "<1>2m", TOYFLAG_USR|TOYFLAG_BIN)) 80f66f451Sopenharmony_ci 90f66f451Sopenharmony_ciconfig UUENCODE 100f66f451Sopenharmony_ci bool "uuencode" 110f66f451Sopenharmony_ci default y 120f66f451Sopenharmony_ci help 130f66f451Sopenharmony_ci usage: uuencode [-m] [INFILE] ENCODE_FILENAME 140f66f451Sopenharmony_ci 150f66f451Sopenharmony_ci Uuencode stdin (or INFILE) to stdout, with ENCODE_FILENAME in the output. 160f66f451Sopenharmony_ci 170f66f451Sopenharmony_ci -m Base64 180f66f451Sopenharmony_ci*/ 190f66f451Sopenharmony_ci 200f66f451Sopenharmony_ci#define FOR_uuencode 210f66f451Sopenharmony_ci#include "toys.h" 220f66f451Sopenharmony_ci 230f66f451Sopenharmony_civoid uuencode_main(void) 240f66f451Sopenharmony_ci{ 250f66f451Sopenharmony_ci char *name = toys.optargs[toys.optc-1], buf[(76/4)*3]; 260f66f451Sopenharmony_ci 270f66f451Sopenharmony_ci int i, m = FLAG(m), fd = 0; 280f66f451Sopenharmony_ci 290f66f451Sopenharmony_ci if (toys.optc > 1) fd = xopenro(toys.optargs[0]); 300f66f451Sopenharmony_ci 310f66f451Sopenharmony_ci base64_init(toybuf); 320f66f451Sopenharmony_ci 330f66f451Sopenharmony_ci xprintf("begin%s 744 %s\n", m ? "-base64" : "", name); 340f66f451Sopenharmony_ci for (;;) { 350f66f451Sopenharmony_ci char *in; 360f66f451Sopenharmony_ci 370f66f451Sopenharmony_ci if (!(i = xread(fd, buf, m ? sizeof(buf) : 45))) break; 380f66f451Sopenharmony_ci 390f66f451Sopenharmony_ci if (!m) xputc(i+32); 400f66f451Sopenharmony_ci in = buf; 410f66f451Sopenharmony_ci 420f66f451Sopenharmony_ci for (in = buf; in-buf < i; ) { 430f66f451Sopenharmony_ci int j, x, bytes = i - (in-buf); 440f66f451Sopenharmony_ci 450f66f451Sopenharmony_ci if (bytes > 3) bytes = 3; 460f66f451Sopenharmony_ci 470f66f451Sopenharmony_ci for (j = x = 0; j<4; j++) { 480f66f451Sopenharmony_ci int out; 490f66f451Sopenharmony_ci 500f66f451Sopenharmony_ci if (j < bytes) x |= (*(in++) & 0x0ff) << (8*(2-j)); 510f66f451Sopenharmony_ci out = (x>>((3-j)*6)) & 0x3f; 520f66f451Sopenharmony_ci xputc(m ? (j > bytes ? '=' : toybuf[out]) : (out ? out + 0x20 : 0x60)); 530f66f451Sopenharmony_ci } 540f66f451Sopenharmony_ci } 550f66f451Sopenharmony_ci xputc('\n'); 560f66f451Sopenharmony_ci } 570f66f451Sopenharmony_ci xputs(m ? "====" : "end"); 580f66f451Sopenharmony_ci} 59