1 /* @(#) $Id: ./src/addagent/b64.c, 2011/09/08 dcid Exp $
4 * Copyright (C), 2000-2004 by the monit project group.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 char *decode_base64(const char *src);
30 char *encode_base64(int size, char *src);
32 /* Private prototypes */
33 static int is_base64(char c);
34 static char encode(unsigned char u);
35 static unsigned char decode(char c);
39 * Implementation of base64 encoding/decoding.
41 * @author Jan-Henrik Haukeland, <hauk@tildeslash.com>
51 * Base64 encode and return size data in 'src'. The caller must free the
53 * @param size The size of the data in src
54 * @param src The data to be base64 encode
55 * @return encoded string otherwise NULL
57 char *encode_base64(int size, char *src) {
66 size= strlen((char *)src);
68 out = (char *)calloc(sizeof(char), size*4/3+4);
74 for(i = 0; i < size; i+=3) {
76 unsigned char b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0;
87 b5= ((b1&0x3)<<4)|(b2>>4);
88 b6= ((b2&0xf)<<2)|(b3>>6);
113 * Decode the base64 encoded string 'src' into the memory pointed to by
114 * 'dest'. The dest buffer is NUL terminated.
115 * Return NULL in case of error
117 char *decode_base64(const char *src)
123 int k, l = strlen(src)+1;
126 /* The size of the dest will always be less than
129 dest = (char *)calloc(sizeof(char), l + 13);
133 p = (unsigned char *)dest;
139 /* Ignore non base64 chars as per the POSIX standard */
140 for(k=0, l=0; src[k]; k++)
142 if(is_base64(src[k]))
150 char c1='A', c2='A', c3='A', c4='A';
151 unsigned char b1=0, b2=0, b3=0, b4=0;
173 *p++=((b1<<2)|(b2>>4) );
176 *p++=(((b2&0xf)<<4)|(b3>>2) );
180 *p++=(((b3&0x3)<<6)|b4 );
197 /* ----------------------------------------------------------------- Private */
199 static char encode(unsigned char u) {
201 if(u < 26) return 'A'+u;
202 if(u < 52) return 'a'+(u-26);
203 if(u < 62) return '0'+(u-52);
204 if(u == 62) return '+';
212 * Decode a base64 character
214 static unsigned char decode(char c) {
216 if(c >= 'A' && c <= 'Z') return(c - 'A');
217 if(c >= 'a' && c <= 'z') return(c - 'a' + 26);
218 if(c >= '0' && c <= '9') return(c - '0' + 52);
219 if(c == '+') return 62;
227 * Return TRUE if 'c' is a valid base64 character, otherwise FALSE
229 static int is_base64(char c) {
231 if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
232 (c >= '0' && c <= '9') || (c == '+') ||
233 (c == '/') || (c == '=')) {
245 int main(int argc, char **argv)
252 printf("%s string\n",argv[0]);
255 s = encode_base64(strlen(argv[1]), argv[1]);
257 printf("b64:\n%s\n",s);
260 d = decode_base64(s);
261 printf("decode:%s\n",d);