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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds