masqmail

annotate 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
rev   line source
meillo@0 1 /* base64.c, Copyright 2000 (C) Oliver Kurth,
meillo@0 2 *
meillo@0 3 * This program is free software; you can redistribute it and/or modify
meillo@0 4 * it under the terms of the GNU General Public License as published by
meillo@0 5 * the Free Software Foundation; either version 2 of the License, or
meillo@0 6 * (at your option) any later version.
meillo@0 7 *
meillo@0 8 * This program is distributed in the hope that it will be useful,
meillo@0 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 11 * GNU General Public License for more details.
meillo@0 12 *
meillo@0 13 * You should have received a copy of the GNU General Public License
meillo@0 14 * along with this program; if not, write to the Free Software
meillo@0 15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
meillo@0 16 */
meillo@0 17
meillo@0 18 /*
meillo@0 19 send bugs to: kurth@innominate.de
meillo@0 20 */
meillo@0 21
meillo@0 22 /* see also RFC 1341 */
meillo@0 23
meillo@0 24 #include <glib.h>
meillo@0 25 #include <string.h>
meillo@0 26 #include "base64.h"
meillo@0 27
meillo@0 28 gchar *base64_encode(guchar *buf, gint len)
meillo@0 29 {
meillo@0 30 guchar *outbuf, *q;
meillo@0 31 gchar enc[64];
meillo@0 32 gint i = 0, j = 0;
meillo@0 33 guint in0, in1, in2;
meillo@0 34
meillo@0 35 for(; i < 26; i++) enc[i] = (gchar)('A' + j++); j = 0;
meillo@0 36 for(; i < 52; i++) enc[i] = (gchar)('a' + j++); j = 0;
meillo@0 37 for(; i < 62; i++) enc[i] = (gchar)('0' + j++);
meillo@0 38 enc[i++] = '+';
meillo@0 39 enc[i++] = '/';
meillo@0 40
meillo@0 41 outbuf = g_malloc(((len+3) * 8)/6);
meillo@0 42 q = outbuf;
meillo@0 43
meillo@0 44 i = 0;
meillo@0 45 while(i < len-2){
meillo@0 46 in0 = buf[i++];
meillo@0 47 in1 = buf[i++];
meillo@0 48 in2 = buf[i++];
meillo@0 49
meillo@0 50 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@0 51 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@0 52 *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
meillo@0 53 *(q++) = enc[in2 & 0x3f];
meillo@0 54 }
meillo@0 55 if((len - i) == 1){
meillo@0 56 in0 = buf[i++];
meillo@0 57 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@0 58 *(q++) = enc[(in0 << 4) & 0x3f];
meillo@0 59 *(q++) = '=';
meillo@0 60 *(q++) = '=';
meillo@0 61 }else if((len - i) == 2){
meillo@0 62 in0 = buf[i++];
meillo@0 63 in1 = buf[i++];
meillo@0 64 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@0 65 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@0 66 *(q++) = enc[(in1 << 2) & 0x3f];
meillo@0 67 *(q++) = '=';
meillo@0 68 }
meillo@0 69 *q = 0;
meillo@0 70
meillo@0 71 return outbuf;
meillo@0 72 }
meillo@0 73
meillo@0 74 gchar *base64_decode(gchar *buf, gint *size)
meillo@0 75 {
meillo@0 76 guchar *p = buf, *q;
meillo@0 77 guint in[4];
meillo@0 78 // gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1);
meillo@0 79 gchar *out = g_malloc((strlen(buf)+3) + 1);
meillo@0 80
meillo@0 81 q = out;
meillo@0 82 *size = 0;
meillo@0 83
meillo@0 84 *q = 0;
meillo@0 85
meillo@0 86 while(*p){
meillo@0 87 int i = 0;
meillo@0 88 while(i < 4){
meillo@0 89 if(!*p) break;
meillo@0 90 if((*p >= 'A') && (*p <= 'Z'))
meillo@0 91 in[i++] = *p - 'A';
meillo@0 92 else if((*p >= 'a') && (*p <= 'z'))
meillo@0 93 in[i++] = (*p - 'a') + 26;
meillo@0 94 else if((*p >= '0') && (*p <= '9'))
meillo@0 95 in[i++] = (*p - '0') + 52;
meillo@0 96 else if(*p == '+')
meillo@0 97 in[i++] = 62;
meillo@0 98 else if(*p == '/')
meillo@0 99 in[i++] = 63;
meillo@0 100 else if(*p == '='){
meillo@0 101 in[i++] = 0;
meillo@0 102 p++;
meillo@0 103 break;
meillo@0 104 }else if((*p != '\r') && (*p != '\n')){
meillo@0 105 p++;
meillo@0 106 break;
meillo@0 107 }
meillo@0 108 p++;
meillo@0 109 }
meillo@0 110 if((i == 4) || (p[-1] == '=')){
meillo@0 111 *(q++) = ((in[0] << 2) | (in[1] >> 4));
meillo@0 112 *(q++) = ((in[1] << 4) | (in[2] >> 2));
meillo@0 113 *(q++) = ((in[2] << 6) | in[3]);
meillo@0 114 if(p[-1] == '='){
meillo@0 115 if(i == 3){
meillo@0 116 (*size)++;
meillo@0 117 }
meillo@0 118 else if(i == 4){
meillo@0 119 (*size) += 2;
meillo@0 120 }
meillo@0 121 }else{
meillo@0 122 *size += 3;
meillo@0 123 }
meillo@0 124 }
meillo@0 125 }
meillo@0 126 return out;
meillo@0 127 }
meillo@0 128