00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <spl/io/DesStream.h>
00018
00019 using namespace spl;
00020
00021 DesStream::DesStream(const String& password, IStreamPtr stream)
00022 : m_password(password), m_stream(stream), m_des(password)
00023 {
00024 }
00025
00026 DesStream::~DesStream()
00027 {
00028 }
00029
00030 void DesStream::Close()
00031 {
00032 m_stream->Close();
00033 }
00034
00035 void DesStream::Flush()
00036 {
00037 m_stream->Flush();
00038 }
00039
00040 int DesStream::Read(Array<byte>& output, const int offset, int count)
00041 {
00042 Array<byte> b(8);
00043 int readCount = 0;
00044 int len = (count - offset);
00045 int outpos = offset;
00046
00047 if ( (len % 8) != 0 )
00048 {
00049 throw new InvalidArgumentException("DES: read length must be a multiple of 8");
00050 }
00051
00052 for ( int x = 0; x < len/8; x++ )
00053 {
00054 int srlen = m_stream->Read(b, 0, 8);
00055 if ( srlen != 8 )
00056 {
00057 throw new IOException("DES: short read, could be caused by non-blocking IO");
00058 }
00059 m_des.DecryptBinary(b);
00060 Array<byte>::Copy(b, 0, output, outpos, 8);
00061 readCount += srlen;
00062 outpos += srlen;
00063 }
00064 return readCount;
00065 }
00066
00067 int DesStream::ReadByte()
00068 {
00069 throw new Exception("DES I/O must be multiples of 8 bytes");
00070 }
00071
00072 long DesStream::Seek(const long offset, const SeekOrigin origin)
00073 {
00074 return m_stream->Seek(offset, origin);
00075 }
00076
00077 void DesStream::Write(const Array<byte>& buffer, const int offset, const int count)
00078 {
00079 int len = (count - offset);
00080
00081 if ( (len % 8) != 0 )
00082 {
00083 throw new InvalidArgumentException("DES: Write length must be a multiple of 8");
00084 }
00085
00086 Array<byte> output;
00087 m_des.EncryptBinary(buffer, output);
00088 ASSERT(output.Length() == buffer.Length());
00089
00090 m_stream->Write(output);
00091 }
00092
00093 void DesStream::WriteByte(byte value)
00094 {
00095 throw new Exception("DES I/O must be multiples of 8 bytes");
00096 }
00097
00098 bool DesStream::CanRead() const
00099 {
00100 return m_stream->CanRead();
00101 }
00102
00103 bool DesStream::CanSeek() const
00104 {
00105 return m_stream->CanSeek();
00106 }
00107
00108 bool DesStream::CanWrite() const
00109 {
00110 return m_stream->CanWrite();
00111 }
00112
00113 long DesStream::Length() const
00114 {
00115 return m_stream->Length();
00116 }
00117
00118 long DesStream::Position() const
00119 {
00120 return m_stream->Position();
00121 }
00122
00123 #ifdef DEBUG
00124 void DesStream::ValidateMem() const
00125 {
00126 m_password.ValidateMem();
00127 m_stream.ValidateMem();
00128 m_stream->ValidateMem();
00129 }
00130
00131 void DesStream::CheckMem() const
00132 {
00133 m_password.CheckMem();
00134 m_stream.CheckMem();
00135 m_stream->CheckMem();
00136 }
00137 #endif