Robert,

Maybe some code will help.... Let's say I write my own ReaderWriter that reads and writes encrypted ive files or "eive" files.

class ReaderWriterEive : public osgDB::ReaderWriter
{

public

  virtual bool acceptsExtension(const std::string& extension) const
  {
    //  Accepts "eive" as an extension.
  }

  // This takes in a .eive file which is an "encrypted ive file".
virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& file,
     const osgDB::ReaderWriter::Options* options) const
  {
     // basic file checking, etc.

     std::ifstream istream(file.c_str(), std::ios::in | std::ios::binary);
     return readNode(istream, options);
  }

virtual osgDB::ReaderWriter::ReadResult readNode(std::istream& encryptedInput,
     const osgDB::ReaderWriter::Options* options) const
  {
     ReadResult rr;
     //
     // 1. decrypt stream.
// 2. call the ive readerWriter's readNode() method on the decrypted stream.
     //

// Create an input stream to store the decrypted data in so the reader can read it. std::istringstream decryptedInputStream(std::ios::in | std::ios::binary);

     // Decrypted the encryptedInput into the decryptedInputStream.
     if(decrypt(encryptedInput, decryptedInputStream))
     {
         // Find the correct reader writer.
         osgDB::ReaderWriter* readerWriter =
osgDB::Registry::instance()->getReaderWriterForExtension(".ive");

         if(readerWriter)
         {
            // Use the reader to read the decrypted data.
rr = readerWriter->readNode(decryptedInputStream, options); }
     }
     return rr;
  }

  virtual osgDB::ReaderWriter::WriteResult writeNode(const osg::Node& node,
const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
  {
     // basic file checking...

std::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary);
     if(!fout)
     {
        return osgDB::ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE;
     }

osgDB::ReaderWriter::WriteResult result = writeNode(node, fout, local_opt.get());
     fout.close();
     return result;
  }


  virtual osgDB::ReaderWriter::WriteResult writeNode(const osg::Node& node,
     std::ostream& fout, const osgDB::ReaderWriter::Options* options) const
  {
    WriteResult wr;
    //
    // 1. call the ive readerWriter's writeNode() method on the stream.
    // 2. encrypt stream.
    //
// Find the correct reader writer.
    osgDB::ReaderWriter* readerWriter =
osgDB::Registry::instance()->getReaderWriterForExtension(".ive"); if(readerWriter)
    {
         // Create an output stream to store the raw ive data.
         std::ostringstream rawIveData(std::ios::out |std::ios::binary);

         // Use the reader to write the decrypted data.
// This is where the problem is. The ive's reader writer will get called // for every proxy node. My reader writer will not get called and therefore all
         // referenced files will be written out as unencrypted ive files.
wr = readerWriter->writeNode(node, rawIveData, options); // Encrypt the decrypted data into the file output.
         encrypt(rawIveData, fileOutputStream);
     }

     return wr;
  }

The problem is in the writeNode() method that writes out to an ofstream. When I give it a stream to write the data to, it does so, but it also writes out all referenced files on its own, as regular .ive's. Perhaps my design is flawed and there is a better way but I'm not seeing it in the curl or osga loader.

Thanks,
Brett


Robert Osfield wrote:
HI Brett,

Please follow the suggestions already made.  The osga plugin already
implementents a custom std::streambuf, and the curl plugin uses a
stingstream to adapt an external stream into something that the other
OSG plugins can use.

Robert.

On Thu, Oct 2, 2008 at 3:47 PM, brettwiesner <[EMAIL PROTECTED]> wrote:
Robert,

OK, I'm reading up on basic_streambuf<char> now. In the meantime, maybe you
can help me understand how I can use a derived streambuf with the existing
IVE loader. Aren't I going to run into the same problem? When the IVE loader
comes across a proxy node, it will write out the node using the IVE loader
and it won't matter that I originally made the call to writeNode() with my
specialized ostream. It's going to create a file stream and write out the
proxy node and there's no way for me to change that.

Thanks,
Brett

Robert Osfield wrote:
Hi Brett,

You need to read up about deriving your own custom std::streambuf,
once you understand this part then adapting existing plugins to use
your encrypted stream will be straightforward.  You could even be lazy
a use a stringstream like the curl plugin does.  Please look into this
stuff - this will answer your need, and there is certainly no need to
subclass from plugins, C++ std::stream streambuf extensibility exist
to help solve problem like yours, and the OSG takes advantage of this.

Robert.

On Wed, Oct 1, 2008 at 9:27 PM, brettwiesner <[EMAIL PROTECTED]>
wrote:

Hey Robert,

I'm confused about what you mean here. Many plugins have read/ write
methods
that take istreams/ ostreams (ReaderWriterIVE being one of them).
However,
since I can't subclass them I can't attach to the stream. For example, in
ReaderWriterIVE::writeNode() a std::ofstream is created but there's no
way
for me to attach it to a stream buffer.

It seems like the osga plugin would have the same problem. The doread()/
dowrite() methods call up to whatever ReaderWriter was passed in and I
can't
subclass those.

Thanks,
Brett

Robert Osfield wrote:

Hi Brett,

A number of the plugins support reading a writing from istream/ostream
which means you can implement your std::streambuffer and attach to the
stream and then pass this to the plugin.  This technique allows you to
do items like compression/decompression/encription/decription.  Have a
look at the osga and curl plugins as they provide examples of using
streams this way.

Robert.

On Wed, Oct 1, 2008 at 5:02 PM, brettwiesner <[EMAIL PROTECTED]>
wrote:


Hi,

I've got a requirement to ship certain 3rd party model data only in an
encrypted format. So I wrote my own loader that does the encryption/
decryption but uses the IVE loader for everything. This works except
for
files that reference other files. The master file is encrypted, but the
referenced files are saved out as .ive's.

Ideally all I would have to do is subclass ReaderWriterIVE and override
the
following stream methods:

 virtual osgDB::ReaderWriter::ReadResult readObject(std::istream& fin,
  const osgDB::ReaderWriter::Options* options) const;

 virtual osgDB::ReaderWriter::ReadResult readImage(std::istream& fin,
  const osgDB::ReaderWriter::Options* options) const;

 virtual osgDB::ReaderWriter::ReadResult readNode(std::istream& fin,
  const osgDB::ReaderWriter::Options* options) const;

 virtual osgDB::ReaderWriter::WriteResult writeObject(const
osg::Object&
object,
  std::ostream& fout, const osgDB::ReaderWriter::Options* options)
const;

 virtual osgDB::ReaderWriter::WriteResult writeImage(const osg::Image&
image,
  std::ostream& fout, const osgDB::ReaderWriter::Options* options)
const;

 virtual osgDB::ReaderWriter::WriteResult writeNode(const osg::Node&
node,
  std::ostream& fout, const osgDB::ReaderWriter::Options* options)
const;

For writing I could get the raw data from the ive loader encrypt it and
write it out. For reading I could decrypt the data, then pass the
unencrypted data up to the ive loader for use.

There is one fatal flaw... I can't subclass the ReaderWriterIVE plugin.
:(

1) Has anyone besides me ever wanted to derive from a file loader?
Would
it
make sense to keep the logic in a lib with headers, then have another
library that is just the plugin?

2) Is there some other mechanism in OSG that will let me do this?

Thanks,
Brett
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org

http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org



_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org

http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to