[BACK]Return to uuencode.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/uuencode.c, Revision 1.2

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] = '=';
1.2     ! markus     43:        } else if (i == nbytes) {
        !            44:                debug("uuencode: i == nbytes");
        !            45:                *(outptr++) = '=';
1.1       markus     46:        }
                     47:        *outptr = '\0';
                     48:        return (outptr - bufcoded);
                     49: }
                     50:
                     51: int
                     52: uudecode(const char *bufcoded, unsigned char *bufplain, int outbufsize)
                     53: {
                     54:        /* single character decode */
                     55: #define DEC(c) pr2six[(unsigned char)c]
                     56: #define MAXVAL 63
                     57:
                     58:        static int first = 1;
                     59:        int nbytesdecoded, j;
                     60:        const char *bufin = bufcoded;
                     61:        register unsigned char *bufout = bufplain;
                     62:        register int nprbytes;
                     63:
                     64:        /* If this is the first call, initialize the mapping table. */
                     65:        if (first) {
                     66:                first = 0;
                     67:                for (j = 0; j < 256; j++)
                     68:                        pr2six[j] = MAXVAL + 1;
                     69:                for (j = 0; j < 64; j++)
                     70:                        pr2six[(unsigned char) six2pr[j]] = (unsigned char) j;
                     71:        }
                     72:        /* Strip leading whitespace. */
                     73:        while (*bufcoded == ' ' || *bufcoded == '\t')
                     74:                bufcoded++;
                     75:
                     76:        /*
                     77:         * Figure out how many characters are in the input buffer. If this
                     78:         * would decode into more bytes than would fit into the output
                     79:         * buffer, adjust the number of input bytes downwards.
                     80:         */
                     81:        bufin = bufcoded;
1.2     ! markus     82:        while (DEC(*(bufin++)) <= MAXVAL)
        !            83:                ;
1.1       markus     84:        nprbytes = bufin - bufcoded - 1;
                     85:        nbytesdecoded = ((nprbytes + 3) / 4) * 3;
                     86:        if (nbytesdecoded > outbufsize)
                     87:                nprbytes = (outbufsize * 4) / 3;
                     88:
                     89:        bufin = bufcoded;
                     90:
                     91:        while (nprbytes > 0) {
                     92:                *(bufout++) = (unsigned char) (DEC(*bufin)   << 2 | DEC(bufin[1]) >> 4);
                     93:                *(bufout++) = (unsigned char) (DEC(bufin[1]) << 4 | DEC(bufin[2]) >> 2);
                     94:                *(bufout++) = (unsigned char) (DEC(bufin[2]) << 6 | DEC(bufin[3]));
                     95:                bufin += 4;
                     96:                nprbytes -= 4;
                     97:        }
                     98:        if (nprbytes & 03) {
                     99:                if (DEC(bufin[-2]) > MAXVAL)
                    100:                        nbytesdecoded -= 2;
                    101:                else
                    102:                        nbytesdecoded -= 1;
                    103:        }
                    104:        return (nbytesdecoded);
                    105: }
                    106:
                    107: void
                    108: dump_base64(FILE *fp, unsigned char *data, int len)
                    109: {
                    110:        unsigned char *buf = xmalloc(2*len);
                    111:        int i, n;
                    112:        n = uuencode(data, len, buf);
                    113:        for (i = 0; i < n; i++) {
                    114:                fprintf(fp, "%c", buf[i]);
                    115:                if (i % 70 == 69)
                    116:                        fprintf(fp, "\n");
                    117:        }
                    118:        if (i % 70 != 69)
                    119:                fprintf(fp, "\n");
                    120:        xfree(buf);
                    121: }