forked from miekg/rdup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase64.c
197 lines (164 loc) · 4.11 KB
/
base64.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (C), 2000-2007 by the monit project group.
* All Rights Reserved.
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "rdup-tr.h"
#include "base64.h"
/* See Section 4 of RFC 3548, be URL safe, modified by Miek Gieben, 2009 */
/* Private prototypes */
static int is_base64(char c);
static char encode(unsigned char u);
static unsigned char decode(char c);
/**
* Implementation of base64 encoding/decoding.
* @author Jan-Henrik Haukeland, <[email protected]>
* @version \$Id: base64.c,v 1.19 2007/07/25 12:54:31 hauk Exp $
*
*/
/**
* Base64 encode and return size data in 'src'. The caller must free the
* returned string.
* @param size The size of the data in src
* @param src The data to be base64 encode
* @return encoded string otherwise NULL
*/
char
*encode_base64(int size, unsigned char *src)
{
int i;
char *out, *p;
if(!src)
return NULL;
if(!size)
size= strlen((char *)src);
out = g_malloc0(size*4/3+4);
p = out;
for(i=0; i<size; i+=3) {
unsigned char b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0;
b1 = src[i];
if(i+1<size)
b2 = src[i+1];
if(i+2<size)
b3 = src[i+2];
b4= b1>>2;
b5= ((b1&0x3)<<4)|(b2>>4);
b6= ((b2&0xf)<<2)|(b3>>6);
b7= b3&0x3f;
*p++= encode(b4);
*p++= encode(b5);
if(i+1<size) {
*p++= encode(b6);
} else {
*p++= '=';
}
if(i+2<size) {
*p++= encode(b7);
} else {
*p++= '=';
}
}
return out;
}
/**
* Decode the base64 encoded string 'src' into the memory pointed to by
* 'dest'. The dest buffer is <b>not</b> NUL terminated.
* @param dest Pointer to memory for holding the decoded string.
* Must be large enough to recieve the decoded string.
* @param src A base64 encoded string.
* @return (the length of the decoded string) if decode
* succeeded otherwise FALSE.
*/
int
decode_base64(unsigned char *dest, const char *src)
{
if(src && *src) {
unsigned char *p= dest;
int k, l= strlen(src)+1;
unsigned char *buf= g_malloc0(l);
/* Ignore non base64 chars as per the POSIX standard */
for(k=0, l=0; src[k]; k++) {
if(is_base64(src[k])) {
buf[l++]= src[k];
}
}
for(k=0; k<l; k+=4) {
char c1='A', c2='A', c3='A', c4='A';
unsigned char b1=0, b2=0, b3=0, b4=0;
c1= buf[k];
if(k+1<l) {
c2= buf[k+1];
}
if(k+2<l) {
c3= buf[k+2];
}
if(k+3<l) {
c4= buf[k+3];
}
b1= decode(c1);
b2= decode(c2);
b3= decode(c3);
b4= decode(c4);
*p++=((b1<<2)|(b2>>4) );
if(c3 != '=') {
*p++=(((b2&0xf)<<4)|(b3>>2) );
}
if(c4 != '=') {
*p++=(((b3&0x3)<<6)|b4 );
}
}
g_free(buf);
return(p-dest);
}
return FALSE;
}
/**
* Base64 encode one byte
*/
static char
encode(unsigned char u)
{
if(u < 26) return 'A'+u;
if(u < 52) return 'a'+(u-26);
if(u < 62) return '0'+(u-52);
if(u == 62) return '-'; /* was + */
/* return '/'; */
return '_';
}
/**
* Decode a base64 character
*/
static unsigned char
decode(char c)
{
if(c >= 'A' && c <= 'Z') return(c - 'A');
if(c >= 'a' && c <= 'z') return(c - 'a' + 26);
if(c >= '0' && c <= '9') return(c - '0' + 52);
if(c == '-') return 62; /* was '+' */
return 63;
}
/**
* Return TRUE if 'c' is a valid base64 character, otherwise FALSE
*/
static int
is_base64(char c)
{
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') || (c == '-') ||
(c == '_') || (c == '=')) {
return TRUE;
}
return FALSE;
}