From: Èãîðü Ïàëàãóòà (pala85_at_[hidden])
Date: 2008-07-14 04:06:19


Hello,
I have the following code:

#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>
#include <boost/archive/iterators/transform_width.hpp>

#include <string>
#include <exception>
#include <iostream>

int main()
{
        using namespace boost::archive::iterators;

        typedef
                insert_linebreaks<
                        base64_from_binary<
                                transform_width< std::string::const_iterator, 6, sizeof( char ) * 8 >
> ,72
> to_base64_type;

        typedef
                transform_width<
                        binary_from_base64<
                                        remove_whitespace< std::string::const_iterator >
>, sizeof( char ) * 8, 6
> to_binary_type;
        
        std::string hello_ = "hello";

        std::string base64_hello_( to_base64_type( hello_.begin() )
                , to_base64_type( hello_.end() ) );

        std::cout << base64_hello_ << std::endl;

        try
        {
                std::string hello_recovered_( to_binary_type( base64_hello_.begin() )
                        , to_binary_type( base64_hello_.end() ) );

                std::cout << hello_recovered_ << std::endl;
        }
        catch ( const std::exception& e_ )
        {
                std::cerr << e_.what() << std::endl;
        }

        return 0;
}

During base64 decode we have following exception: "attempt to decode a value not in base64 char set".
We have the problem because source string hello_ = "hello", has length % 3 != 0, otherwise we don't have this exception.
As I know in this case we should add appropriate number of special symbol like '='.
I can add '=' manually, but boost base64 decode can't process them, and we also have exception

If we have insert_linebreaks/remove_whitespace filter perhaps we also should have some symmetric filter for text padding? Do we have padding filter in boost? Or perhaps it will be in future?

Thanks