I use Crypto++ 5.6.3 and iI need the FileSource "skip(...) function. Unfortunately this function does nothing!
I was able to duplicate this using strings under Master, 5.6.3, and 5.6.2 on OS X 10.8.5 and Ubuntu 14.04.
$ cat test.cxx
#include <string>
#include <iostream>
using namespace std;
#include <filters.h>
#include <hex.h>
using namespace CryptoPP;
int main(int argc, char* argv[])
{
string str1, str2;
HexEncoder enc(new StringSink(str1));
for(unsigned int i=0; i < 32; i++)
enc.Put((byte)i);
enc.MessageEnd();
cout << "str1: " << str1 <<endl;
StringSource ss(str1, false, new StringSink(str2));
ss.Skip(10);
ss.PumpAll();
cout << "str2: " << str2 << endl;
return 0;
}
And:
$ ./test.exe
str1: 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
str2: 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
Crypto++ 5.6.2 is significant because it was the last version Wei worked on before turning the library over to the community. An issue in 5.6.2 is just a latent bug and we encounter them on occasion, just like any other project. ("Wei" bugs are actually kind of rare, and they are closer to "Knuth" bugs in his Art of Computer Programming).
If its a 5.6.3 and above problem, then it means the community broke it. If the community broke it, then we need to perform a post-mortem and analyze how/why we managed to break something that used to work.
Here's the bug report for the library: Issue 248: Skip'ing on a Source does not work. We are trying to determine if its a bug; and if so, then how to proceed.
EDIT 1: I was able to investigate the issue a little more. You can read the analysis at Comment 242890863. The short of it is, Skip
is used to discard bytes on an output buffer (an AttachedTransformation()
), so things are somewhat working as expected. However, there's nothing intuitive about Skip
not working on the Source
, and only working on the attached Filter
(q.v., we're here).
I also asked for some feedback on the mailing list at Issue 248: Skip'ing on a Source does not work. DB and WD spotted it right away - its a design issue in the library.
Here's the workaround you can use for the moment. Effectively, you Pump()
into a null
Filter
which discards the input as expected. Then you attach the real filter chain to handle the real processing.
#include <string>
#include <iostream>
using namespace std;
#include <filters.h>
#include <hex.h>
using namespace CryptoPP;
int main(int argc, char* argv[])
{
string str1, str2;
HexEncoder enc(new StringSink(str1));
for(unsigned int i=0; i < 32; i++)
enc.Put((byte)i);
enc.MessageEnd();
cout << "str1: " << str1 <<endl;
// 'ss' has a NULL AttachedTransformation()
StringSource ss(str1, false);
ss.Pump(10);
// Attach the real filter chain to 'ss'
ss.Attach(new StringSink(str2));
ss.PumpAll();
cout << "str2: " << str2 << endl;
return 0;
}
It produces the expected output:
$ ./test.exe
str1: 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
str2: 05060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
In your sample program, I believe the workaround would be:
FileSource file(filename, false);
file.Pump(24);
file.Attach(new HexEncoder(new StringSink(str)));
file.PumpAll();
EDIT 2: Here's a slightly more verbose way to achieve the work around (thanks DB). It stresses the point that bytes are being discarded. TheBitBucket()
is simply a discard filter, and it serves the same purpose as a null
AttachedTransformation()
.
int main(int argc, char* argv[])
{
string str1, str2;
HexEncoder enc(new StringSink(str1));
for(unsigned int i=0; i < 32; i++)
enc.Put((byte)i);
enc.MessageEnd();
cout << "str1: " << str1 <<endl;
StringSource ss(str1, false, new Redirector(TheBitBucket()));
ss.Pump(10);
ss.Detach(new StringSink(str2));
ss.PumpAll();
cout << "str2: " << str2 << endl;
return 0;
}
There's another subtle difference in the program above: It calls Detach
, which free's the former filter chain. If you called Attach
, then the former chain would be detached, returned to the caller but not free'd.