Success! I finally managed to get a copy of the buffer as I wanted by using 
a custom filter.

I created a custom filter called DataFilter, that would simply copy the 
information of the buffer into a file (like a FileSink) as follows:

//This is just the "UselessFilter" from the wiki, slightly modified to act 
as some kind of "manual" FileSink
class DataFilter : public Filter
{
public:
    DataFilter(BufferedTransformation* attachment = NULL) : Filter(
attachment)  {};
    
    size_t Put2(const byte* inString, size_t length, int messageEnd, bool 
blocking)
    {
        of.write((char*)inString, length); //<- inString is the buffer I 
needed
        return AttachedTransformation()->Put2(inString, length, messageEnd, 
blocking );
    }
    
    bool IsolatedFlush(bool hardFlush, bool blocking)
    {
        return false;
    }
    
};

Just to check if I could get a copy of the file I was decompressing, in 
which case it would be a success. In my decompressing function, I created 
an instance of this DataFilter, and attached it to my chain of filters.

DataFilter dat;        

//...

source.Attach(new Redirector(filter));
filter.Attach(new Redirector(meter));
meter.Attach(new Redirector(dat));
dat.Attach(new Redirector(sink)); //<- Also I dont need this sink anymore, 
but I will do it only as a test

I opened the file stream beforehand, of course, with a file called 
"copy.mp4" in output, binary mode, and then closed it after the pump loop. 
After running the program, I successfully got a file named copy.mp4 that I 
can play using VLC, as well as the result.mp4 file resultant from the pump.

Thank you for your time and help. Very much appreciated.

El martes, 2 de mayo de 2017, 18:30:33 (UTC+2), Aarón CdC escribió:
>
> I found examples on the wiki about how to use crypto++ to compress and 
> decompress files using Crypto++'s Gzip algorithm. But all examples I found 
> process the whole file at once. Imagine I need to compress a 1GB+ file. 
> Maybe now we have computers with large amounts of memory, but this is 
> completely out of the question.
>
> What I'm trying to do is to process a file using buffers to load small 
> chunks of data trough a File Source. This is what I got so far:
>
>     ofstream output;
>     
>     //Compression
>     output.open("TEST/out.a", fstream::binary);
>     int got,ncomp,diff = 0;
>         char* ib = new char [BUFSIZE];
>         char* ob = new char [BUFSIZE];
>         
>         for(int x = 0; x < BUFSIZE; x++)
>         {
>             ib[x] |= 0x0;
>             ob[x] |= 0x0;
>         }
>         
>         
>         
>         FileSource fsr("TEST/a.jpg", false, new Gzip(new ArraySink((byte*)
> ob, BUFSIZE)));
>         while(!fsr.SourceExhausted())
>         {
>             got = fsr.Pump(BUFSIZE);
>             fsr.Flush(false);
>             cout << "Pumped " << got << " bytes" << endl;
>             if(got == 0)
>                 break;
>             output.write(ob, got);
>         }
>         
>         output.close();
>         
>         //Decompression
>         
>         for(int x = 0; x < BUFSIZE; x++)
>         {
>             ib[x] |= 0x0;
>             ob[x] |= 0x0;
>         }
>         
>         cout << "Decript" << endl;
>         output.open("TEST/test.jpg", fstream::binary);
>         
>         FileSource fir("TEST/out.a", false, new Gunzip(new ArraySink((byte
> *)ib, BUFSIZE)));
>         while(!fir.SourceExhausted())
>         {
>             got = fir.Pump(BUFSIZE);
>             fir.Flush(false);
>             cout << "Pumped " << got << " bytes" << endl;
>             if(got == 0)
>                 break;
>             output.write(ib, got);
>         }
>         
>         output.close();
>         delete[] ib, ob;
>         cout << "Done" << endl;
>
> I have several problems with this code. First of all, the file is not 
> processed entirely. Instead, one portion is processed, and then it repeats 
> trough the whole file. This is obviously not what I want to do, I need to 
> process the whole file, but in small chunks. Also, SourceExhausted() doesnt 
> return true once it reaches the end of the file (thus why there is a break 
> in this code when there shouldn't be needed).
>
> I know there are ways to do this directly without the need of a buffer, 
> but I need to pass it trough memory because I need to implement this 
> somewhere else, and I need all this data to be processed first in memory, 
> so I cant use a FileSink.
>

-- 
-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to cryptopp-users-unsubscr...@googlegroups.com.
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
--- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cryptopp-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to