Boost logo

Boost Users :

From: Vinzenz Feenstra (evilissimo_at_[hidden])
Date: 2008-08-27 02:25:44


Hi,

Well I ran into this issue too. This is the way how I've solved this:

     template<typename InputContainerType>
     inline String base64_encode(InputContainerType const & cont)
     {
       using namespace boost::archive::iterators;
       typedef base64_from_binary<
         transform_width<
         typename InputContainerType::const_pointer, 6, 8, boost::uint8_t
>
> base64_t;
       return String(base64_t(&cont[0]), base64_t(&cont[0] + cont.size()));
     }

     template<typename Container, typename CharT>
     inline Container base64_decode(std::basic_string<CharT> str)
     {
       str.append(str.size() % 4, CharT('='));
       typedef boost::archive::iterators::transform_width<
         boost::archive::iterators::binary_from_base64<CharT *>, 8, 6,
boost::uint8_t
> binary_t;

       typename std::basic_string<CharT>::size_type pos =
str.find_last_not_of(CharT('='));
       // calculate how many characters we need to process
       pos = (pos == str.size() -1 ? str.size() : pos );
       return Container(binary_t(&str[0]), binary_t(&str[0] + pos));
     }

So far it seemed to work for me, but still might have a bug.

Regards,
Vinzenz

Terdale, Shantibhushan schrieb:
> I am using this sample to prove myself that base64_from_binary and
> binary_from_base64 can be used in my app. But I am having little problem
> and not able to understand whats missing
>
> Here is the sample
>
>
> #include "boost/archive/iterators/base64_from_binary.hpp"
>
> #include "boost/archive/iterators/binary_from_base64.hpp"
>
> #include "boost/archive/iterators/transform_width.hpp"
>
> #include <string>
>
> #include <iostream>
>
> using namespace std;
>
> using namespace boost::archive::iterators;
>
> typedef
>
> base64_from_binary<
>
> transform_width<std::string::iterator, 6, sizeof(char) * 8>
>
>> base64_t;
>
> typedef
>
> transform_width<
>
> binary_from_base64<std::string::iterator>, sizeof(char) * 8, 6
>
>> binary_t;
>
> int main()
>
> {
>
> string str = "Hello, world!";
>
> cout << str << endl;
>
> string enc(base64_t(str.begin()), base64_t(str.end())); *// Problem
> here. It works when I specify "str.end()-1" instead of "str.end()" but I
> loose last character after decoding.*
>
> cout << enc << endl;
>
> string dec(binary_t(enc.begin()), binary_t(enc.end()));
>
> cout << dec << endl;
>
> return 0;
>
> }
>
> Will appreciate any help. I tried std::copy as well and have same problem.
>
>
>
>
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net