Annotation of src/usr.bin/ssh/uuencode.c, Revision 1.1
1.1 ! markus 1: /*
! 2: * base-64 encoding pinched from lynx2-7-2, who pinched it from rpem.
! 3: * Originally written by Mark Riordan 12 August 1990 and 17 Feb 1991
! 4: * and placed in the public domain.
! 5: *
! 6: * Dug Song <dugsong@UMICH.EDU>
! 7: */
! 8:
! 9: #include "includes.h"
! 10: #include "xmalloc.h"
! 11:
! 12: char six2pr[64] = {
! 13: 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
! 14: 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
! 15: 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
! 16: 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
! 17: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
! 18: };
! 19:
! 20: unsigned char pr2six[256];
! 21:
! 22: int
! 23: uuencode(unsigned char *bufin, unsigned int nbytes, char *bufcoded)
! 24: {
! 25: /* ENC is the basic 1 character encoding function to make a char printing */
! 26: #define ENC(c) six2pr[c]
! 27:
! 28: register char *outptr = bufcoded;
! 29: unsigned int i;
! 30:
! 31: for (i = 0; i < nbytes; i += 3) {
! 32: *(outptr++) = ENC(*bufin >> 2); /* c1 */
! 33: *(outptr++) = ENC(((*bufin << 4) & 060) | ((bufin[1] >> 4) & 017)); /* c2 */
! 34: *(outptr++) = ENC(((bufin[1] << 2) & 074) | ((bufin[2] >> 6) & 03)); /* c3 */
! 35: *(outptr++) = ENC(bufin[2] & 077); /* c4 */
! 36: bufin += 3;
! 37: }
! 38: if (i == nbytes + 1) {
! 39: outptr[-1] = '=';
! 40: } else if (i == nbytes + 2) {
! 41: outptr[-1] = '=';
! 42: outptr[-2] = '=';
! 43: }
! 44: *outptr = '\0';
! 45: return (outptr - bufcoded);
! 46: }
! 47:
! 48: int
! 49: uudecode(const char *bufcoded, unsigned char *bufplain, int outbufsize)
! 50: {
! 51: /* single character decode */
! 52: #define DEC(c) pr2six[(unsigned char)c]
! 53: #define MAXVAL 63
! 54:
! 55: static int first = 1;
! 56: int nbytesdecoded, j;
! 57: const char *bufin = bufcoded;
! 58: register unsigned char *bufout = bufplain;
! 59: register int nprbytes;
! 60:
! 61: /* If this is the first call, initialize the mapping table. */
! 62: if (first) {
! 63: first = 0;
! 64: for (j = 0; j < 256; j++)
! 65: pr2six[j] = MAXVAL + 1;
! 66: for (j = 0; j < 64; j++)
! 67: pr2six[(unsigned char) six2pr[j]] = (unsigned char) j;
! 68: }
! 69: /* Strip leading whitespace. */
! 70: while (*bufcoded == ' ' || *bufcoded == '\t')
! 71: bufcoded++;
! 72:
! 73: /*
! 74: * Figure out how many characters are in the input buffer. If this
! 75: * would decode into more bytes than would fit into the output
! 76: * buffer, adjust the number of input bytes downwards.
! 77: */
! 78: bufin = bufcoded;
! 79: while (DEC(*(bufin++)) <= MAXVAL);
! 80: nprbytes = bufin - bufcoded - 1;
! 81: nbytesdecoded = ((nprbytes + 3) / 4) * 3;
! 82: if (nbytesdecoded > outbufsize)
! 83: nprbytes = (outbufsize * 4) / 3;
! 84:
! 85: bufin = bufcoded;
! 86:
! 87: while (nprbytes > 0) {
! 88: *(bufout++) = (unsigned char) (DEC(*bufin) << 2 | DEC(bufin[1]) >> 4);
! 89: *(bufout++) = (unsigned char) (DEC(bufin[1]) << 4 | DEC(bufin[2]) >> 2);
! 90: *(bufout++) = (unsigned char) (DEC(bufin[2]) << 6 | DEC(bufin[3]));
! 91: bufin += 4;
! 92: nprbytes -= 4;
! 93: }
! 94: if (nprbytes & 03) {
! 95: if (DEC(bufin[-2]) > MAXVAL)
! 96: nbytesdecoded -= 2;
! 97: else
! 98: nbytesdecoded -= 1;
! 99: }
! 100: return (nbytesdecoded);
! 101: }
! 102:
! 103: void
! 104: dump_base64(FILE *fp, unsigned char *data, int len)
! 105: {
! 106: unsigned char *buf = xmalloc(2*len);
! 107: int i, n;
! 108: n = uuencode(data, len, buf);
! 109: for (i = 0; i < n; i++) {
! 110: fprintf(fp, "%c", buf[i]);
! 111: if (i % 70 == 69)
! 112: fprintf(fp, "\n");
! 113: }
! 114: if (i % 70 != 69)
! 115: fprintf(fp, "\n");
! 116: xfree(buf);
! 117: }