//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// 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-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/string
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include "string_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	str_app_decode_base64
//
// purpose	Append to given target the decoded string from a base 64
//		encoded input string.
//
//              Do append only if all source strings can be appended.
//
// arguments    1 (char *) pointer to target string
//              2 (size_t) maximum space in target string
//		3 (const char *) source string
//
// returns	(size_t) final length if target string
//		(size_t) ~0 : error
//-----------------------------------------------------------------------------
size_t
str_app_decode_base64 (
    char *		arg_target
    ,
    size_t		arg_length
    ,
    const char *	arg_source
    )
__PROTO_END__
{
    static const unsigned char	base64	[256] = {
	128,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
	255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
	 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,255,255,255,
	255,  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,255,255,255,255,255,
	255, 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,255,255,255,255,255,
	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
    };

    unsigned char *		target_ptr	;
    unsigned char *		target_end	;
    unsigned char *		target_undo	;
    const unsigned char *	source_ptr	;


    //-- Determine end of string and check for space.
    target_ptr = (unsigned char *) arg_target;
    target_end = target_ptr + arg_length - 1;
    while ( target_ptr < target_end && * target_ptr ) ++ target_ptr;
    if ( target_ptr >= target_end ) return ~ (size_t) 0;
    target_undo = target_ptr;

    //-- Decode encoded bits.
    source_ptr = (const unsigned char *) arg_source;
    for (;;) {
	register unsigned char b8;
	register unsigned char b6;

	if ( ( b6 = base64[ * source_ptr ++ ] ) >= 64 ) break;
	b8 = b6 << 2;
	if ( ( b6 = base64[ * source_ptr ++ ] ) >= 64 ) break;
	b8 |= ( b6 >> 4 );
	if ( target_ptr == target_end ) break;
	* target_ptr ++ = b8;
	b8 = ( ( b6 & 0x0f ) << 4 );
	if ( ( b6 = base64[ * source_ptr ++ ] ) >= 64 ) break;
	b8 |= ( b6 >> 2 );
	if ( target_ptr == target_end ) break;
	* target_ptr ++ = b8;
	b8 = ( ( b6 & 0x03 ) << 6 );
	if ( ( b6 = base64[ * source_ptr ++ ] ) >= 64 ) break;
	b8 |= b6;
	if ( target_ptr == target_end ) break;
	* target_ptr ++ = b8;
    }

    //-- If the string did not fit, undo and abort.
    if ( target_ptr >= target_end ) {
        * target_undo = 0;
        return ~ (size_t) 0;
    }
    * target_ptr = 0;

    //-- Return length of target.
    return target_ptr - (unsigned char *) arg_target;
}

