2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: base64.c,v 1.2.4.3 2009-03-15 21:37:55 zer0 Exp $
24 char my_base64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
26 /* return -1 if out buffer is too small */
27 /* else return number of written bytes in out buffer */
28 int base64_to_raw(char * in, int len_in, char * out, int len_out)
35 for (n_in=0 ; n_in<len_in && n_out<len_out ; n_in++) {
36 /* this is the end as it is padding */
40 c = strchr(my_base64, in[n_in]);
42 /* ignore bad chars */
47 buf += (int)(c-my_base64);
56 out[n_out++] = (buf>>(counter-8)) & 0xFF;
60 /* no padding at the end, ok */
61 if (counter == 0 && (n_in+1 >= len_in || in[n_in] != '=')) {
65 /* one '=' padding, ok */
66 if (counter == 2 && (in[n_in] == '=') &&
67 (n_in+1 >= len_in || in[n_in] != '=')) {
71 /* two '=' padding, ok */
72 if (counter == 4 && (in[n_in] == '=') &&
73 n_in+1 < len_in && in[n_in+1] == '=') {
82 /* return -1 if out buffer is too small */
83 /* else return number of written bytes in out buffer */
84 int raw_to_base64(char *in, int len_in, char * out, int len_out)
91 for (n_in=0 ; n_in<len_in && n_out<len_out ; n_in++) {
93 buf += (unsigned char)(in[n_in]);
99 out[n_out++] = my_base64[(buf>>(counter-6)) & 0x3F];
104 /* should we add padding ? */
106 if (n_out >= len_out)
109 out[n_out++] = my_base64[(buf<<(6-counter)) & 0x3F];
111 if (n_out >= len_out)
114 /* one '=' if counter==4 (counter can be 4 or 2) */
117 if (n_out >= len_out)
120 /* two '=' if cpt==2 */