File: base64.c

package info (click to toggle)
pam-p11 0.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,592 kB
  • sloc: sh: 9,139; ansic: 670; makefile: 48
file content (178 lines) | stat: -rw-r--r-- 4,090 bytes parent folder | download | duplicates (4)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * base64.c: Base64 converting functions
 *
 * Copyright (C) 2001, 2002  Juha Yrjölä <juha.yrjola@iki.fi>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

extern int sc_base64_decode(const char *in, unsigned char *out, size_t outlen);

static const unsigned char base64_table[66] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "0123456789+/=";

static const unsigned char bin_table[128] = {
	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
	0xFF, 0xE0, 0xD0, 0xFF, 0xFF, 0xD0, 0xFF, 0xFF,
	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
	0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
	0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xF2, 0xFF, 0x3F,
	0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
	0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xC0, 0xFF, 0xFF,
	0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
	0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
	0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
	0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
	0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
	0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
	0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
	0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};

#if 0
static void to_base64(unsigned int i, unsigned char *out, unsigned int fillers)
{
	unsigned int s = 18, c;

	for (c = 0; c < 4; c++) {
		if (fillers >= 4 - c)
			*out = base64_table[64];
		else
			*out = base64_table[(i >> s) & 0x3f];
		out++;
		s -= 6;
	}
}
#endif

static int from_base64(const char *in, unsigned int *out, int *skip)
{
	unsigned int res = 0, c, s = 18;
	const char *in0 = in;

	for (c = 0; c < 4; c++, in++) {
		unsigned char b;
		int k = *in;

		if (k < 0)
			return -1;
		if (k == 0 && c == 0)
			return 0;
		b = bin_table[k];
		if (b == 0xC0)	/* '=' */
			break;
		switch (b) {
		case 0xD0:	/* '\n' or '\r' */
			c--;
			continue;
		}
		if (b > 0x3f)
			return -1;

		res |= b << s;
		s -= 6;
	}
	*skip = in - in0;
	*out = res;
	return c * 6 / 8;
}

#if 0
int sc_base64_encode(const unsigned char *in, size_t len, unsigned char *out,
		     size_t outlen, size_t linelength)
{
	unsigned int chars = 0;
	size_t i, c;

	linelength -= linelength & 0x03;
	if (linelength < 0)
		return -1;
	while (len >= 3) {
		i = in[2] + (in[1] << 8) + (in[0] << 16);
		in += 3;
		len -= 3;
		if (outlen < 4)
			return -1;
		to_base64(i, out, 0);
		out += 4;
		outlen -= 4;
		chars += 4;
		if (chars >= linelength && linelength > 0) {
			if (outlen < 1)
				return -1;
			*out = '\n';
			out++;
			outlen--;
			chars = 0;
		}
	}
	i = c = 0;
	while (c < len)
		i |= *in++ << ((2 - c++) << 3);
	if (len) {
		if (outlen < 4)
			return -1;
		to_base64(i, out, 3 - len);
		out += 4;
		outlen -= 4;
		chars += 4;
	}
	if (chars && linelength > 0) {
		if (outlen < 1)
			return -1;
		*out = '\n';
		out++;
		outlen--;
	}
	if (outlen < 1)
		return -1;
	*out = 0;

	return 0;
}
#endif

int sc_base64_decode(const char *in, unsigned char *out, size_t outlen)
{
	int len = 0, r, skip;
	unsigned int i;

	while ((r = from_base64(in, &i, &skip)) > 0) {
		int finished = 0, s = 16;

		if (r < 3)
			finished = 1;
		while (r--) {
			if (outlen <= 0)
				return -1;
			*out++ = i >> s;
			s -= 8;
			outlen--;
			len++;
		}
		in += skip;
		if (finished || *in == 0)
			return len;
	}
	if (r == 0)
		return len;
	return -1;
}