STL Compatible Base64 Encoder

Environment: Any C++ compiler with STL support

This is compatible with STL base64 encoder.

It can encode/decode data from/to i(o)streams, streambufs, memory buffers, etc. Encoder uses iterator concept. Iterator must overload the *, ==, !=, and prefics ++ operators.

See encoder using:

1) Declare encoder

#include <base64.hpp>

int _State = 0;
base64<char> encoder; // base64 output/input in chars 
                      // (may be wchar_t, ...) or base64<> encoder;

2) Encode data form cin stream to output file

#include <iostream>
#include <fstream>

//
   ...
//
// encode from cin

istream& istr = cin;
istreambuf_iterator<char> _From(istr.rdbuf());
istreambuf_iterator<char> _To(0);

//_From, _Last, _To may be any iterator class or pointers

// encode to output file
ofstream ostr("test_out.txt");
ostreambuf_iterator<char> _Out(ostr);

// Encode data with CRLF ta the end of line.
// You can use base64<>::noline() or base64<>::crlfsp()
// or other classes.
encoder.put(_From, _To, _Out, _State, base64<>::crlf());

3) Decode data form file stream to console output stream

// decode from file ...
ifstream istr("test_out.txt");
istreambuf_iterator<char> _From(istr.rdbuf());
istreambuf_iterator<char> _To(0);

// ... to console
ostreambuf_iterator<char> _Out(cout);

encoder.get(_From, _To, _Out, _State);

Downloads

Download demo project - 5 Kb


Comments

  • whar_t support broken ?

    Posted by Legacy on 11/13/2003 12:00am

    Originally posted by: Vimal Earnest

    Hi,
    This is what I understood
    base64<char> encoder; //encode/decode 'char' type
    base64<wchar_t> encoder; //encode/decode 'wchar_t' type

    But the code given below doesn't work as expected

    int _State = 0;
    base64<wchar_t> encoder;
    {
    wchar_t buff_in[6] = L"Hello";
    wchar_t buff_out[12] = {0};
    encoder.put(buff_in, buff_in+6, buff_out, _State, base64<>::crlf());
    int len = wcslen(buff_out);
    encoder.get(buff_out, buff_out+len, buff_in, _State);
    assert(wcscmp(buff_in, L"Hello") != 0);
    }

    Is this a bug ?

    ~Vimal

    Reply
  • Good class, but...

    Posted by Legacy on 08/28/2002 12:00am

    Originally posted by: Lionel Zhang

    in get(), shouldn't _To be incremented?
    
    

    i.e., should it look like

    // write to output 3 bytes
    *_To++ = (byte_t) _3to4.get_0();
    *_To++ = (byte_t) _3to4.get_1();
    *_To++ = (byte_t) _3to4.get_2();

    instead of

    *_To = (byte_t) _3to4.get_0();
    *_To = (byte_t) _3to4.get_1();
    *_To = (byte_t) _3to4.get_2();

    there are also a few other lines in get() that need ++

    Reply
  • STL decode/Encode

    Posted by Legacy on 04/03/2002 12:00am

    Originally posted by: jyothi

    How to use ur code.I wanted to have c++ classes for encode and decode the base64 and quoted printable.
    Please help me out.

    Reply
  • Base64 codec

    Posted by Legacy on 03/06/2002 12:00am

    Originally posted by: Fanzhe Cui

    This is great code. It does not depend on any platform.
    Fanzhe

    Reply
  • STL Compatible Base64 Encoder - Excellent.

    Posted by Legacy on 01/26/2002 12:00am

    Originally posted by: James Pannozzi

    Excellent utilization of STL.

    Would it be easy to modify it to decode MHTML (.mht) into text and jpg images?

    Thanks again for interesting code.

    Ochen Xoposho!

    Jim

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds