XorDataIO.h

00001 /* This file is Copyright 2000-2008 Meyer Sound Laboratories Inc.  See the included LICENSE.txt file for details. */
00002 
00003 #ifndef MuscleXorDataIO_h
00004 #define MuscleXorDataIO_h
00005 
00006 #include "dataio/DataIO.h"
00007 #include "util/ByteBuffer.h"
00008 
00009 BEGIN_NAMESPACE(muscle);
00010  
00016 class XorDataIO : public DataIO
00017 {
00018 public:
00022    XorDataIO() {/* empty */}
00023 
00028    XorDataIO(const DataIORef & childIO) : _childIO(childIO) {/* empty */}
00029 
00031    virtual ~XorDataIO() {/* empty */}
00032 
00034    virtual int32 Read(void * buffer, uint32 size)
00035    {  
00036       int32 ret = _childIO() ? _childIO()->Read(buffer, size) : -1;
00037       if (ret > 0) XorCopy(buffer, buffer, size);
00038       return ret;
00039    }
00040 
00042    virtual int32 Write(const void * buffer, uint32 size)
00043    {
00044       if ((_childIO() == NULL)||(_tempBuf.SetNumBytes(buffer, size) != B_NO_ERROR)) return B_ERROR;
00045       XorCopy(_tempBuf.GetBuffer(), buffer, size);
00046       return _childIO()->Write(_tempBuf.GetBuffer(), size);
00047    }
00048 
00049    virtual status_t Seek(int64 offset, int whence) {return _childIO() ? _childIO()->Seek(offset, whence) : B_ERROR;}
00050    virtual int64 GetPosition() const {return _childIO() ? _childIO()->GetPosition() : -1;}
00051    virtual uint64 GetOutputStallLimit() const {return _childIO() ? _childIO()->GetOutputStallLimit() : MUSCLE_TIME_NEVER;}
00052    virtual void FlushOutput() {if (_childIO()) _childIO()->FlushOutput();}
00053    virtual void Shutdown() {if (_childIO()) _childIO()->Shutdown(); _childIO.Reset();}
00054    virtual const SocketRef & GetSelectSocket() const {return _childIO() ? _childIO()->GetSelectSocket() : GetNullSocket();}
00055    virtual status_t GetReadByteTimeStamp(int32 whichByte, uint64 & retStamp) const {return _childIO() ? _childIO()->GetReadByteTimeStamp(whichByte, retStamp) : B_ERROR;}
00056 
00057    virtual bool HasBufferedOutput() const {return _childIO() ? _childIO()->HasBufferedOutput() : false;}
00058    virtual void WriteBufferedOutput() {if (_childIO()) _childIO()->WriteBufferedOutput();}
00059 
00061    const DataIORef & GetChildDataIO() const {return _childIO;}
00062 
00064    void SetChildDataIO(const DataIORef & childDataIO) {_childIO = childDataIO;}
00065 
00066 private:
00067    void XorCopy(const void * to, const void * from, uint32 numBytes)
00068    {
00069       // Do the bulk of the copy a word at a time, for faster speed
00070       unsigned long * uto = (unsigned long *) to;
00071       const unsigned long * ufrom = (const unsigned long *) from;
00072       for (int32 i=(numBytes/sizeof(unsigned long))-1; i>=0; i--) *uto++ = ~(*ufrom++);
00073 
00074       // The last few bytes we have to do a byte a time though
00075       uint8 * cto = (uint8 *) uto;
00076       const uint8 * cfrom = (const uint8 *) ufrom;
00077       for (int32 i=(numBytes%sizeof(unsigned long))-1; i>=0; i--) *cto++ = ~(*cfrom++);
00078    }
00079 
00080    DataIORef _childIO;
00081    ByteBuffer _tempBuf;   // holds the XOR'd bytes temporarily for us
00082 };
00083 
00084 END_NAMESPACE(muscle);
00085 
00086 #endif

Generated on Thu Jun 5 17:47:54 2008 for MUSCLE by  doxygen 1.5.1