/*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111, USA.
*
*/
/*
* Base64 handling elsewhere in mutt should be modified to call
* these routines. These routines were written because IMAP's
* AUTHENTICATE protocol required them, and base64 handling
* elsewhere wasn't sufficiently generic.
*
*/
/*
* This code is heavily modified from fetchmail (also GPL'd, of
* course) by Brendan Cully <brendan@kublai.com>.
*
* Original copyright notice:
*
* The code in the fetchmail distribution is Copyright 1997 by Eric
* S. Raymond. Portions are also copyrighted by Carl Harris, 1993
* and 1995. Copyright retained for the purpose of protecting free
* redistribution of source.
*
*/
/* This file was taken from Mutt, and modified where needed.
* Mutt is Copyright Notice follows:
*
* The following copyright notices apply to most of the program. Some
* modules are under different licenses, or in the public domain.
*
* Please note that this is by no means an exhaustive list of all the
* persons who have been contributing to this program. Please see the
* manual for a (probably still non complete) list of the persons who
* have been helpful with the development of this program.
*
* Copyright (C) 1996-2001 Michael R. Elkins <me@cs.hmc.edu>
* Copyright (C) 1996-2001 Brandon Long <blong@fiction.net>
* Copyright (C) 1997-2001 Thomas Roessler <roessler@guug.de>
* Copyright (C) 1998-2001 Werner Koch <wk@isil.d.shuttle.de>
* Copyright (C) 1999-2001 Brendan Cully <brendan@kublai.com>
* Copyright (C) 1999-2001 Tommi Komulainen <Tommi.Komulainen@iki.fi>
* Copyright (C) 2000-2001 Edmund Grimley Evans <edmundo@rano.org>
*
*/
#include <sys/types.h>
#include "mime.h"
#define BAD -1
char B64Chars[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '+', '/'
};
int Index_64[128] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
};
/* raw bytes to null-terminated base 64 string */
void raw_to_base64 (unsigned char *out, const unsigned char *in, size_t len,
size_t olen)
{
int linelen = 0;
while (len >= 3 && olen > 10)
{
if ( linelen >= 72 ) {
*out++ = '\n';
olen--;
linelen=0;
}
*out++ = B64Chars[in[0] >> 2];
*out++ = B64Chars[((in[0] << 4) & 0x30) | (in[1] >> 4)];
*out++ = B64Chars[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
*out++ = B64Chars[in[2] & 0x3f];
olen -= 4;
len -= 3;
in += 3;
linelen += 4;
}
/* clean up remainder */
if (len > 0 && olen > 4)
{
unsigned char fragment;
*out++ = B64Chars[in[0] >> 2];
fragment = (in[0] << 4) & 0x30;
if (len > 1)
fragment |= in[1] >> 4;
*out++ = B64Chars[fragment];
*out++ = (len < 2) ? '=' : B64Chars[(in[1] << 2) & 0x3c];
*out++ = '=';
}
*out = '\0';
}
/* Convert '\0'-terminated base 64 string to raw bytes.
* Returns length of returned buffer, or -1 on error */
int raw_from_base64 (char *out, const char *in)
{
int len = 0;
register unsigned char digit1, digit2, digit3, digit4;
do
{
digit1 = in[0];
if (digit1 > 127 || base64val (digit1) == BAD)
return -1;
digit2 = in[1];
if (digit2 > 127 || base64val (digit2) == BAD)
return -1;
digit3 = in[2];
if (digit3 > 127 || ((digit3 != '=') && (base64val (digit3) == BAD)))
return -1;
digit4 = in[3];
if (digit4 > 127 || ((digit4 != '=') && (base64val (digit4) == BAD)))
return -1;
in += 4;
/* digits are already sanity-checked */
*out++ = (base64val(digit1) << 2) | (base64val(digit2) >> 4);
len++;
if (digit3 != '=')
{
*out++ = ((base64val(digit2) << 4) & 0xf0) | (base64val(digit3) >> 2);
len++;
if (digit4 != '=')
{
*out++ = ((base64val(digit3) << 6) & 0xc0) | base64val(digit4);
len++;
}
}
}
while (*in && digit4 != '=');
return len;
}
syntax highlighted by Code2HTML, v. 0.9.1