In the Pipelining paradigm, Sources are the origin of data. They are attached to data, like a byte array or
std::string, and the data flows from them into Filters. Sources serve the opposite role of Sinks.
Sources exist for different types of objects. Crypto++ provides the following stock Sources:
You can swap different
Sources in and out. If you are reading data from a file through a
FileSource, then you can swap in a
StringSource to read the data from memory.
StringSource requires no additional information to originate data. Other objects, such as
FileSources require additional information such as a filename. Still others, such as a
RandomNumberSource require a
RandomNumberGenerator and byte count.
Source takes a pointer to a
BufferedTransformation. Because a pointer is taken, the
Source owns the attached transformation, and therefore will destroy it. See ownership for more details.
The following example demonstrates creation of a FileSource.
FileSource file( filename );
The following example demonstrates reading a file, and placing the contents of the file in a string. This is known as pipelining.
string s; FileSource file( filename, new StringSink( s ) ); cout << s << endl;
The following example performs the same operation as above, but without the variable
string s; FileSource( filename, true, new StringSink( s ) ); cout << s << endl;
A slightly more complicated example of pipelining is below. Before the
FileSource is placed in the string, it is hex decoded.
string s; FileSource( filename, new HexDecoder( new StringSink( s ) ) ); cout << s << endl;
Note that the
StringSink created with
new do not require explicit destruction - the
FileSource will call
delete on the
HexDecoder, which in turns calls
delete on the
StringSink when it (the
FileSource) is destroyed.
Finally, the example below places 4 random bytes of data into a
StringSink after hex encoding using a random number source. As the chaining gets longer, nesting the chaining structure as with
if statements offers readability.
AutoSeededRandomPool rng; RandomNumberSource( rng, 4, true, new HexEncoder( new ArraySink( s ) ) // HexEncoder ); // RandomNumberSource
No downloads available.