masqmail

diff src/base64/base64.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children 589c365d90b1
line diff
     1.1 --- a/src/base64/base64.c	Mon Oct 27 16:21:27 2008 +0100
     1.2 +++ b/src/base64/base64.c	Mon Oct 27 16:23:10 2008 +0100
     1.3 @@ -4,7 +4,7 @@
     1.4   * it under the terms of the GNU General Public License as published by
     1.5   * the Free Software Foundation; either version 2 of the License, or
     1.6   * (at your option) any later version.
     1.7 - * 
     1.8 + *
     1.9   * This program is distributed in the hope that it will be useful,
    1.10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.12 @@ -25,104 +25,109 @@
    1.13  #include <string.h>
    1.14  #include "base64.h"
    1.15  
    1.16 -gchar *base64_encode(guchar *buf, gint len)
    1.17 +gchar*
    1.18 +base64_encode(guchar * buf, gint len)
    1.19  {
    1.20 -  guchar *outbuf, *q;
    1.21 -  gchar enc[64];
    1.22 -  gint i = 0, j = 0;
    1.23 -  guint in0, in1, in2;
    1.24 +	guchar *outbuf, *q;
    1.25 +	gchar enc[64];
    1.26 +	gint i = 0, j = 0;
    1.27 +	guint in0, in1, in2;
    1.28  
    1.29 -  for(; i < 26; i++) enc[i] = (gchar)('A' + j++); j = 0;
    1.30 -  for(; i < 52; i++) enc[i] = (gchar)('a' + j++); j = 0;
    1.31 -  for(; i < 62; i++) enc[i] = (gchar)('0' + j++);
    1.32 -  enc[i++] = '+';
    1.33 -  enc[i++] = '/';
    1.34 +	for (; i < 26; i++)
    1.35 +		enc[i] = (gchar) ('A' + j++);
    1.36 +	j = 0;
    1.37 +	for (; i < 52; i++)
    1.38 +		enc[i] = (gchar) ('a' + j++);
    1.39 +	j = 0;
    1.40 +	for (; i < 62; i++)
    1.41 +		enc[i] = (gchar) ('0' + j++);
    1.42 +	enc[i++] = '+';
    1.43 +	enc[i++] = '/';
    1.44  
    1.45 -  outbuf = g_malloc(((len+3) * 8)/6);
    1.46 -  q = outbuf;
    1.47 +	outbuf = g_malloc(((len + 3) * 8) / 6);
    1.48 +	q = outbuf;
    1.49  
    1.50 -  i = 0;
    1.51 -  while(i < len-2){
    1.52 -    in0 = buf[i++];
    1.53 -    in1 = buf[i++];
    1.54 -    in2 = buf[i++];
    1.55 +	i = 0;
    1.56 +	while (i < len - 2) {
    1.57 +		in0 = buf[i++];
    1.58 +		in1 = buf[i++];
    1.59 +		in2 = buf[i++];
    1.60  
    1.61 -    *(q++) = enc[(in0 >> 2) & 0x3f];
    1.62 -    *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
    1.63 -    *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
    1.64 -    *(q++) = enc[in2 & 0x3f];
    1.65 -  }
    1.66 -  if((len - i) == 1){
    1.67 -    in0 = buf[i++];
    1.68 -    *(q++) = enc[(in0 >> 2) & 0x3f];
    1.69 -    *(q++) = enc[(in0 << 4) & 0x3f];
    1.70 -    *(q++) = '=';
    1.71 -    *(q++) = '=';
    1.72 -  }else if((len - i) == 2){
    1.73 -    in0 = buf[i++];
    1.74 -    in1 = buf[i++];
    1.75 -    *(q++) = enc[(in0 >> 2) & 0x3f];
    1.76 -    *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
    1.77 -    *(q++) = enc[(in1 << 2) & 0x3f];
    1.78 -    *(q++) = '=';
    1.79 -  }
    1.80 -  *q = 0;
    1.81 -  
    1.82 -  return outbuf;
    1.83 -}
    1.84 -    
    1.85 -gchar *base64_decode(gchar *buf, gint *size)
    1.86 -{
    1.87 -  guchar *p = buf, *q;
    1.88 -  guint in[4];
    1.89 -  //  gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1);
    1.90 -  gchar *out = g_malloc((strlen(buf)+3) + 1);
    1.91 +		*(q++) = enc[(in0 >> 2) & 0x3f];
    1.92 +		*(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
    1.93 +		*(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
    1.94 +		*(q++) = enc[in2 & 0x3f];
    1.95 +	}
    1.96 +	if ((len - i) == 1) {
    1.97 +		in0 = buf[i++];
    1.98 +		*(q++) = enc[(in0 >> 2) & 0x3f];
    1.99 +		*(q++) = enc[(in0 << 4) & 0x3f];
   1.100 +		*(q++) = '=';
   1.101 +		*(q++) = '=';
   1.102 +	} else if ((len - i) == 2) {
   1.103 +		in0 = buf[i++];
   1.104 +		in1 = buf[i++];
   1.105 +		*(q++) = enc[(in0 >> 2) & 0x3f];
   1.106 +		*(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
   1.107 +		*(q++) = enc[(in1 << 2) & 0x3f];
   1.108 +		*(q++) = '=';
   1.109 +	}
   1.110 +	*q = 0;
   1.111  
   1.112 -  q = out;
   1.113 -  *size = 0;
   1.114 -
   1.115 -  *q = 0;
   1.116 -
   1.117 -  while(*p){
   1.118 -    int i = 0;
   1.119 -    while(i < 4){
   1.120 -      if(!*p) break;
   1.121 -      if((*p >= 'A') && (*p <= 'Z'))
   1.122 -	 in[i++] = *p - 'A';
   1.123 -      else if((*p >= 'a') && (*p <= 'z'))
   1.124 -	in[i++] = (*p - 'a') + 26;
   1.125 -      else if((*p >= '0') && (*p <= '9'))
   1.126 -	in[i++] = (*p - '0') + 52;
   1.127 -      else if(*p == '+')
   1.128 -	in[i++] = 62;
   1.129 -      else if(*p == '/')
   1.130 -	in[i++] = 63;
   1.131 -      else if(*p == '='){
   1.132 -	in[i++] = 0;
   1.133 -	p++;
   1.134 -	break;
   1.135 -      }else if((*p != '\r') && (*p != '\n')){
   1.136 -	p++;
   1.137 -	break;
   1.138 -      }
   1.139 -      p++;
   1.140 -    }
   1.141 -    if((i == 4) || (p[-1] == '=')){
   1.142 -      *(q++) = ((in[0] << 2) | (in[1] >> 4));
   1.143 -      *(q++) = ((in[1] << 4) | (in[2] >> 2));
   1.144 -      *(q++) = ((in[2] << 6) | in[3]);
   1.145 -      if(p[-1] == '='){
   1.146 -	if(i == 3){
   1.147 -	  (*size)++;
   1.148 -	}
   1.149 -	else if(i == 4){
   1.150 -	  (*size) += 2;
   1.151 -	}
   1.152 -      }else{
   1.153 -	*size += 3;
   1.154 -      }
   1.155 -    }
   1.156 -  }
   1.157 -  return out;
   1.158 +	return outbuf;
   1.159  }
   1.160  
   1.161 +gchar *base64_decode(gchar * buf, gint * size)
   1.162 +{
   1.163 +	guchar *p = buf, *q;
   1.164 +	guint in[4];
   1.165 +	/* gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); */
   1.166 +	gchar *out = g_malloc((strlen(buf) + 3) + 1);
   1.167 +
   1.168 +	q = out;
   1.169 +	*size = 0;
   1.170 +
   1.171 +	*q = 0;
   1.172 +
   1.173 +	while (*p) {
   1.174 +		int i = 0;
   1.175 +		while (i < 4) {
   1.176 +			if (!*p)
   1.177 +				break;
   1.178 +			if ((*p >= 'A') && (*p <= 'Z'))
   1.179 +				in[i++] = *p - 'A';
   1.180 +			else if ((*p >= 'a') && (*p <= 'z'))
   1.181 +				in[i++] = (*p - 'a') + 26;
   1.182 +			else if ((*p >= '0') && (*p <= '9'))
   1.183 +				in[i++] = (*p - '0') + 52;
   1.184 +			else if (*p == '+')
   1.185 +				in[i++] = 62;
   1.186 +			else if (*p == '/')
   1.187 +				in[i++] = 63;
   1.188 +			else if (*p == '=') {
   1.189 +				in[i++] = 0;
   1.190 +				p++;
   1.191 +				break;
   1.192 +			} else if ((*p != '\r') && (*p != '\n')) {
   1.193 +				p++;
   1.194 +				break;
   1.195 +			}
   1.196 +			p++;
   1.197 +		}
   1.198 +		if ((i == 4) || (p[-1] == '=')) {
   1.199 +			*(q++) = ((in[0] << 2) | (in[1] >> 4));
   1.200 +			*(q++) = ((in[1] << 4) | (in[2] >> 2));
   1.201 +			*(q++) = ((in[2] << 6) | in[3]);
   1.202 +			if (p[-1] == '=') {
   1.203 +				if (i == 3) {
   1.204 +					(*size)++;
   1.205 +				} else if (i == 4) {
   1.206 +					(*size) += 2;
   1.207 +				}
   1.208 +			} else {
   1.209 +				*size += 3;
   1.210 +			}
   1.211 +		}
   1.212 +	}
   1.213 +	return out;
   1.214 +}