One way to do it is exemplified below.
Cheers,
Jorge.
---------------------------------------------------
// You could use std::istringstream (#include <sstream>) but the
// code below avoids some copying
class myistringstream {
size_t cur;
const string *str;
public:
myistringstream (const string *_str) {
setStr(str);
}
void setStr(const string *_str) { str = _str; cur = 0; }
int read(char *buf, size_t len) {
const size_t base = cur;
cur = min(cur+len, str->length());
const size_t readLen = cur-base;
memcpy(buf, str->c_str(), readLen);
buf[readLen] = 0;
return readLen;
}
};
int myread(void *ctx, char *buf, int len) {
return ((myistringstream*)ctx)->read(buf, len);
}
void decryptFile(istream &is) {
// some transformation to perform, for example:
CBC_Mode<DES>::Decryption decipher;
// ... some code to set 'decipher' up ...
string rslt;
{
FileSource fs(is, true /* automatically call PumpAll */,
new Base32Decoder(
new StreamTransformationFilter(decipher,
new StringSink (rslt))));
// don't worry, all those objects are automatically deleted now.
}
// now rslt already has all the input to the xml parser.
myistringstream iss (&rslt);
return xmlReadIO(..., myread, &iss, ...);
}
------------------------------------
kan escreveu:
>
> I have huge lack of understanding source/filters/sink concept. I want
> to parse xml file from crypto filters.
> libxml2 has a function to parse an input from callback-based stream. It
> looks like:
>
> int callbackFunc(void *context, const char *buffer, int len)
> {
> // function must read a data, put it in the buffer (not more than len
> bytes) and return real amount of read bytes.
> // at the end of the stream it must return 0
> // -1 for error indication.
> }
>
> then I call library function:
>
> xmlDocPtr = xmlReadIO(..., callbackFunc, context, ...);
>
> The library invokes the callback as it wants.
> I want to feed a data from crypto++.
> If I use just FileSource as source object, it work fine. Something like
> this:
>
> int callbackFunc(void *context, const char *buffer, int len)
> {
> FileSource * source = (FileSource *)context;
> source->Pump(len);
> return source->Get(buffer, len);
> }
> ...
> FileSource source("a file");
> xmlDocPtr = xmlReadIO(..., callbackFunc, &source, ...);
>
> But if I try to use filters (e.g. encryption+compression) this method
> doesn't work, because I don't know how much I have to Pump, without
> pumping Get doesn't give any data.
>
>
> >
Jorge M. Pelizzoni
ICMC - Universidade de São Paulo
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the "Crypto++ Users"
Google Group.
To unsubscribe, send an email to [EMAIL PROTECTED]
More information about Crypto++ and this group is available at
http://www.cryptopp.com.
-~----------~----~----~----~------~----~------~--~---