summaryrefslogtreecommitdiff
path: root/base64.c
blob: fcc7f5c90e356756d2fdd06a1430576196a94785 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Part of publib.

   Copyright (c) 1994-2006 Lars Wirzenius.  All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
 * File:	base64.c
 * Purpose:	Implementation of MIME's Base64 encoding and decoding.
 * Author:	Lars Wirzenius
 * Version:	$Id: base64.c,v 1.2 2003/11/15 18:24:10 liw Exp $
 */

#include <limits.h>
#include "base64.h"

static const unsigned char sixtet_to_base64[] = 
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


/*
 * Function:	base64_length
 * Purpose:	Compute minimum length of encoded output.
 */
size_t base64_length(size_t n) {
	return 4*(n+3)/3 + 1;
}


/*
 * Function:	base64_encode
 * Purpose:	Convert octets to base64
 * Note:	Output buffer must be at least base64_length(n) chars.
 */
size_t base64_encode(char *to, const char *from, size_t n) {
    	unsigned w;
	char *to_start;
	unsigned char *fromp;
	
    	to_start = to;

    	fromp = (unsigned char *) from;
    	for (; n >= 3; n -= 3, fromp += 3) {
	    w = (fromp[0] << 16) | (fromp[1] << 8) | fromp[2];
	    *to++ = sixtet_to_base64[(w >> 18) & 0x3f];
	    *to++ = sixtet_to_base64[(w >> 12) & 0x3f];
	    *to++ = sixtet_to_base64[(w >> 6) & 0x3f];
	    *to++ = sixtet_to_base64[w & 0x3f];
	}

    	switch (n) {
	case 0:
	    /* Nothing to do */
	    break;
    	
	case 1:
	    w = fromp[0];
	    *to++ = sixtet_to_base64[(w >> 2) & 0x3f];
	    *to++ = sixtet_to_base64[(w << 4) & 0x3f];
	    *to++ = '=';
	    *to++ = '=';
	    break;

	case 2:
	    w = (fromp[0] << 8) | fromp[1];
	    *to++ = sixtet_to_base64[(w >> 10) & 0x3f];
	    *to++ = sixtet_to_base64[(w >> 4) & 0x3f];
	    *to++ = sixtet_to_base64[(w << 2) & 0x3f];
	    *to++ = '=';
	    break;
	}
	
	return to - to_start;
}


/*
 * Function:	base64_decode
 * Purpose:	Convert base64 to octets.
 * Note:	Output buffer must be at least 3*n/4 chars.
 *		The output buffer does not get a '\0' appended.
 */
size_t base64_decode(char *to, const char *from, size_t len) {
    	static int base64_to_sixtet[UCHAR_MAX + 2];
	static int tab_init = 0;
	int i;
	unsigned bitbuf;
	int nbits;
	unsigned char *fromp;
	char *to_start;

    	if (!tab_init) {
	    tab_init = 1;
	    for (i = 0; i <= UCHAR_MAX + 1; ++i)
	    	base64_to_sixtet[i] = -1;
	    for (i = 0; sixtet_to_base64[i] != '\0'; ++i)
	    	base64_to_sixtet[sixtet_to_base64[i]] = i;
	}

    	to_start = to;

    	bitbuf = 0;
	nbits = 0;
	fromp = (unsigned char *) from;
    	for (i = 0; i < len && fromp[i] != '='; ++i) {
	    if (base64_to_sixtet[fromp[i]] != -1) {
		bitbuf = (bitbuf << 6) | base64_to_sixtet[fromp[i]];
		nbits += 6;
		if (nbits >= 8) {
		    *to++ = (bitbuf >> (nbits - 8)) & 0xff;
		    bitbuf >>= 8;
		    nbits -= 8;
		}
	    }
	}
	
	return to - to_start;
}