|
| 1 | +/* This file is Copyright 2000-2026 Meyer Sound Laboratories Inc. See the included LICENSE.txt file for details. */ |
| 2 | + |
| 3 | +#ifndef MuscleFlipBitsProxyDataIO_h |
| 4 | +#define MuscleFlipBitsProxyDataIO_h |
| 5 | + |
| 6 | +#include "dataio/ProxyDataIO.h" |
| 7 | +#include "util/ByteBuffer.h" |
| 8 | + |
| 9 | +namespace muscle { |
| 10 | + |
| 11 | +/** This DataIO is a "wrapper" DataIO that adds a bitwise-NOT operation to any data |
| 12 | + * that it reads or writes before passing the call on to the DataIO that it |
| 13 | + * holds internally. This can be useful if you want to obfuscate your data |
| 14 | + * a little bit before sending it out to disk or over the network. |
| 15 | + */ |
| 16 | +class FlipBitsProxyDataIO : public ProxyDataIO |
| 17 | +{ |
| 18 | +public: |
| 19 | + /** Default Constructor. Be sure to set a child dataIO with SetChildDataIO() |
| 20 | + * before using this DataIO, so that this object will do something useful! |
| 21 | + */ |
| 22 | + FlipBitsProxyDataIO() {/* empty */} |
| 23 | + |
| 24 | + /** Constructor. |
| 25 | + * @param childIO Reference to the DataIO to pass calls on through to |
| 26 | + * after the data has been NOT'd. |
| 27 | + */ |
| 28 | + FlipBitsProxyDataIO(const DataIORef & childIO) : ProxyDataIO(childIO) {/* empty */} |
| 29 | + |
| 30 | + /** Virtual destructor, to keep C++ honest. */ |
| 31 | + virtual ~FlipBitsProxyDataIO() {/* empty */} |
| 32 | + |
| 33 | + /** Implemented to use the child DataIO object to read in some bytes, and then |
| 34 | + * un-NOT the read bytes before returning them to the caller. |
| 35 | + * @copydoc ProxyDataIO::Read(void *, uint32) |
| 36 | + */ |
| 37 | + virtual io_status_t Read(void * buffer, uint32 size) |
| 38 | + { |
| 39 | + const io_status_t ret = ProxyDataIO::Read(buffer, size); |
| 40 | + if (ret.GetByteCount() > 0) BitwiseNotMemCpy(buffer, buffer, ret.GetByteCount()); |
| 41 | + return ret; |
| 42 | + } |
| 43 | + |
| 44 | + /** Implemented to make an NOT'd copy of the passed-in bytes, and then use |
| 45 | + * the child DataIO object to transmit the NOT'd bytes. |
| 46 | + * @copydoc ProxyDataIO::Write(const void *, uint32) |
| 47 | + */ |
| 48 | + virtual io_status_t Write(const void * buffer, uint32 size) |
| 49 | + { |
| 50 | + MRETURN_ON_ERROR(_tempBuf.SetNumBytes(size, false)); |
| 51 | + BitwiseNotMemCpy(_tempBuf.GetBuffer(), buffer, size); |
| 52 | + return ProxyDataIO::Write(_tempBuf.GetBuffer(), size); |
| 53 | + } |
| 54 | + |
| 55 | + /** Implemented to use the child DataIO object to read in some bytes, and then |
| 56 | + * un-NOT the read bytes before returning them to the caller. |
| 57 | + * @copydoc ProxyDataIO::ReadFrom(void *, uint32, IPAddressAndPort &) |
| 58 | + */ |
| 59 | + virtual io_status_t ReadFrom(void * buffer, uint32 size, IPAddressAndPort & retPacketSource) |
| 60 | + { |
| 61 | + const io_status_t ret = ProxyDataIO::ReadFrom(buffer, size, retPacketSource); |
| 62 | + if (ret.GetByteCount() > 0) BitwiseNotMemCpy(buffer, buffer, ret.GetByteCount()); |
| 63 | + return ret; |
| 64 | + } |
| 65 | + |
| 66 | + /** Implemented to make an NOT'd copy of the passed-in bytes, and then use |
| 67 | + * the child DataIO object to transmit the NOT'd bytes. |
| 68 | + * @copydoc ProxyDataIO::WriteTo(const void *, uint32, const IPAddressAndPort &) |
| 69 | + */ |
| 70 | + virtual io_status_t WriteTo(const void * buffer, uint32 size, const IPAddressAndPort & packetDest) |
| 71 | + { |
| 72 | + MRETURN_ON_ERROR(_tempBuf.SetNumBytes(size, false)); |
| 73 | + BitwiseNotMemCpy(_tempBuf.GetBuffer(), buffer, size); |
| 74 | + return ProxyDataIO::WriteTo(_tempBuf.GetBuffer(), size, packetDest); |
| 75 | + } |
| 76 | + |
| 77 | +private: |
| 78 | + void BitwiseNotMemCpy(void * to, const void * from, uint32 numBytes) const |
| 79 | + { |
| 80 | + uint8 * cto = (uint8 *) to; |
| 81 | + const uint8 * cfrom = (const uint8 *) from; |
| 82 | + for (uint32 i=0; i<numBytes; i++) cto[i] = (uint8) (~cfrom[i]); |
| 83 | + } |
| 84 | + |
| 85 | + ByteBuffer _tempBuf; // holds the NOT'd bytes temporarily for us |
| 86 | + |
| 87 | + DECLARE_COUNTED_OBJECT(FlipBitsProxyDataIO); |
| 88 | +}; |
| 89 | +DECLARE_REFTYPES(FlipBitsProxyDataIO); |
| 90 | + |
| 91 | +} // end namespace muscle |
| 92 | + |
| 93 | +#endif |
0 commit comments