The base encoder class.
Encoders read data from an input buffer, transform it in some way, then write the results to an output buffer. The resulting data may be of a different size or data type, and may or may not depend on previous data.
Encoders may or may not possess the following characteristics:
- Statefulness: encoding of successive input elements may depend on previous one
- Error states: encoding may enter an error state indicated by isok() == false due to problems detected in the in encoder has been used
- Minimum input block size: data will not be drawn from the input buffer until enough is available or the encoder is flushed
- Minimum output block size: data will not be written to the output buffer until enough free space is available
- Synchronization boundaries: data is process or generated in chunks which can be manipulated independently of any others, in which case flush() may cause the encoder to produce such a boundary in its output
- Recognition of end-of-data mark: a special sequence marks the end of input, after which the encoder transitions to isfinished() == true
- Generation of end-of-data mark: a special sequence marks the end of output when the encoder transitions to isfinished() == true, usually by an explicit call to finish()
- Reset support: the encoder may be reset to its initial state and thereby recycled at minimum cost
Helper functions are provided for encoding data from plain memory buffers and from strings. Some have no encode(...) equivalent because they cannot incrementally encode from the input, hence they always use the flush option.
The 'mem' suffix has been tacked on to these functions to resolve ambiguities dealing with 'char *' that should be promoted to WvString. For instance, consider the signatures of strflushmem(const void*, size_t) and strflushstr(WvStringParm, bool).
Another reason for these suffixes is to simplify overloading the basic methods in subclasses since C++ would require the subclass to redeclare all of the other signatures for an overloaded method.
Contains functions for encoding and decoding strings in MIME Base64 notation.
Converts a string to base64 encoding.
Converts a string from base64 format into a normal string format.
An example of WvBase64Encoder:
/*
* A WvBase64 example.
*
*/
#include "wvbase64.h"
#include "wvstream.h"
#include "wvstreamlist.h"
#include "wvencoderstream.h"
#include "wvbufbase.h"
int main()
{
WvEncoder *enc;
enc = new WvBase64Encoder();
WvInPlaceBuf to_encode(100);
WvInPlaceBuf encoded(100);
to_encode.put("123",3);
//to_encode contains the string to be encoded in base64
if (enc->encode(to_encode, encoded, true,true))
printf ("This is the result: %s\n", (char *) encoded.get(1));
//it will display on screen:
//This is the result: MTIz
WvEncoder *dec;
dec = new WvBase64Decoder();
WvInPlaceBuf to_decode(100);
WvInPlaceBuf decoded(100);
to_decode.put("MTIz",4);
//to_encode contains the string to be encoded in base64
if (dec->encode(to_decode, decoded, true))
printf ("This is the result: %s\n", (char *) decoded.get(1));
//it will display on screen:
//This is the result: 123
return 0;
}