summaryrefslogtreecommitdiffstats
path: root/pki/base/native-tools/src/setpin/b64.c
blob: 1c20f3792063cea603f428093b0329d459a83c75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* --- BEGIN COPYRIGHT BLOCK ---
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Copyright (C) 2007 Red Hat, Inc.
 * All rights reserved.
 * --- END COPYRIGHT BLOCK ---
 */







static char nib2b64[0x40f] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";



static int
ldif_base64_encode_internal( unsigned char *src, char *dst, int srclen, int lenused, int wraplen )
{
    unsigned char   *byte, *stop;
    unsigned char   buf[3];
    char        *out;
    unsigned long   bits;
    int     i, pad, len;

    len = 0;
    out = dst;
    stop = src + srclen;

    /* convert to base 64 (3 bytes => 4 base 64 digits) */
    for ( byte = src; byte < stop - 2; byte += 3 ) {
        bits = (byte[0] & 0xff) << 16;
        bits |= (byte[1] & 0xff) << 8;
        bits |= (byte[2] & 0xff);

        for ( i = 0; i < 4; i++, bits <<= 6 ) {
            if ( wraplen != -1 &&  lenused >= 0 && lenused++ > wraplen ) {
                *out++ = '\n';
                *out++ = ' ';
                lenused = 2;
            }

            /* get b64 digit from high order 6 bits */
            *out++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
        }
    }
    /* add padding if necessary */
    if ( byte < stop ) {
        for ( i = 0; byte + i < stop; i++ ) {
            buf[i] = byte[i];
        }
        for ( pad = 0; i < 3; i++, pad++ ) {
            buf[i] = '\0';
        }
        byte = buf;
        bits = (byte[0] & 0xff) << 16;
        bits |= (byte[1] & 0xff) << 8;
        bits |= (byte[2] & 0xff);

        for ( i = 0; i < 4; i++, bits <<= 6 ) {
            if ( wraplen != -1 && lenused >= 0 && lenused++ > wraplen ) {
                *out++ = '\n';
                *out++ = ' ';
                lenused = 2;
            }

            if (( i == 3 && pad > 0 ) || ( i == 2 && pad == 2 )) {
                /* Pad as appropriate */
                *out++ = '=';
            } else {
                /* get b64 digit from low order 6 bits */
                *out++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
            }
        }
    }

    *out = '\0';

    return( out - dst );
}


int
ldif_base64_encode( unsigned char *src, char *dst, int srclen, int lenused )
{
    return ldif_base64_encode_internal( src, dst, srclen, lenused, 200);
}