masqmail-0.2

diff src/base64/base64.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/base64/base64.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,128 @@
     1.4 +/* base64.c, Copyright 2000 (C) Oliver Kurth,
     1.5 + *
     1.6 + * This program is free software; you can redistribute it and/or modify
     1.7 + * it under the terms of the GNU General Public License as published by
     1.8 + * the Free Software Foundation; either version 2 of the License, or
     1.9 + * (at your option) any later version.
    1.10 + * 
    1.11 + * This program is distributed in the hope that it will be useful,
    1.12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.14 + * GNU General Public License for more details.
    1.15 + *
    1.16 + * You should have received a copy of the GNU General Public License
    1.17 + * along with this program; if not, write to the Free Software
    1.18 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    1.19 + */
    1.20 +
    1.21 +/*
    1.22 + send bugs to: kurth@innominate.de
    1.23 +*/
    1.24 +
    1.25 +/* see also RFC 1341 */
    1.26 +
    1.27 +#include <glib.h>
    1.28 +#include <string.h>
    1.29 +#include "base64.h"
    1.30 +
    1.31 +gchar *base64_encode(guchar *buf, gint len)
    1.32 +{
    1.33 +  guchar *outbuf, *q;
    1.34 +  gchar enc[64];
    1.35 +  gint i = 0, j = 0;
    1.36 +  guint in0, in1, in2;
    1.37 +
    1.38 +  for(; i < 26; i++) enc[i] = (gchar)('A' + j++); j = 0;
    1.39 +  for(; i < 52; i++) enc[i] = (gchar)('a' + j++); j = 0;
    1.40 +  for(; i < 62; i++) enc[i] = (gchar)('0' + j++);
    1.41 +  enc[i++] = '+';
    1.42 +  enc[i++] = '/';
    1.43 +
    1.44 +  outbuf = g_malloc(((len+3) * 8)/6);
    1.45 +  q = outbuf;
    1.46 +
    1.47 +  i = 0;
    1.48 +  while(i < len-2){
    1.49 +    in0 = buf[i++];
    1.50 +    in1 = buf[i++];
    1.51 +    in2 = buf[i++];
    1.52 +
    1.53 +    *(q++) = enc[(in0 >> 2) & 0x3f];
    1.54 +    *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
    1.55 +    *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
    1.56 +    *(q++) = enc[in2 & 0x3f];
    1.57 +  }
    1.58 +  if((len - i) == 1){
    1.59 +    in0 = buf[i++];
    1.60 +    *(q++) = enc[(in0 >> 2) & 0x3f];
    1.61 +    *(q++) = enc[(in0 << 4) & 0x3f];
    1.62 +    *(q++) = '=';
    1.63 +    *(q++) = '=';
    1.64 +  }else if((len - i) == 2){
    1.65 +    in0 = buf[i++];
    1.66 +    in1 = buf[i++];
    1.67 +    *(q++) = enc[(in0 >> 2) & 0x3f];
    1.68 +    *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
    1.69 +    *(q++) = enc[(in1 << 2) & 0x3f];
    1.70 +    *(q++) = '=';
    1.71 +  }
    1.72 +  *q = 0;
    1.73 +  
    1.74 +  return outbuf;
    1.75 +}
    1.76 +    
    1.77 +gchar *base64_decode(gchar *buf, gint *size)
    1.78 +{
    1.79 +  guchar *p = buf, *q;
    1.80 +  guint in[4];
    1.81 +  //  gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1);
    1.82 +  gchar *out = g_malloc((strlen(buf)+3) + 1);
    1.83 +
    1.84 +  q = out;
    1.85 +  *size = 0;
    1.86 +
    1.87 +  *q = 0;
    1.88 +
    1.89 +  while(*p){
    1.90 +    int i = 0;
    1.91 +    while(i < 4){
    1.92 +      if(!*p) break;
    1.93 +      if((*p >= 'A') && (*p <= 'Z'))
    1.94 +	 in[i++] = *p - 'A';
    1.95 +      else if((*p >= 'a') && (*p <= 'z'))
    1.96 +	in[i++] = (*p - 'a') + 26;
    1.97 +      else if((*p >= '0') && (*p <= '9'))
    1.98 +	in[i++] = (*p - '0') + 52;
    1.99 +      else if(*p == '+')
   1.100 +	in[i++] = 62;
   1.101 +      else if(*p == '/')
   1.102 +	in[i++] = 63;
   1.103 +      else if(*p == '='){
   1.104 +	in[i++] = 0;
   1.105 +	p++;
   1.106 +	break;
   1.107 +      }else if((*p != '\r') && (*p != '\n')){
   1.108 +	p++;
   1.109 +	break;
   1.110 +      }
   1.111 +      p++;
   1.112 +    }
   1.113 +    if((i == 4) || (p[-1] == '=')){
   1.114 +      *(q++) = ((in[0] << 2) | (in[1] >> 4));
   1.115 +      *(q++) = ((in[1] << 4) | (in[2] >> 2));
   1.116 +      *(q++) = ((in[2] << 6) | in[3]);
   1.117 +      if(p[-1] == '='){
   1.118 +	if(i == 3){
   1.119 +	  (*size)++;
   1.120 +	}
   1.121 +	else if(i == 4){
   1.122 +	  (*size) += 2;
   1.123 +	}
   1.124 +      }else{
   1.125 +	*size += 3;
   1.126 +      }
   1.127 +    }
   1.128 +  }
   1.129 +  return out;
   1.130 +}
   1.131 +