1 /* 2 Derived from source code of TrueCrypt 7.1a, which is 3 Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed 4 by the TrueCrypt License 3.0. 5 6 Modifications and additions to the original source code (contained in this file) 7 and all other portions of this file are Copyright (c) 2013-2017 IDRIX 8 and are governed by the Apache License 2.0 the full text of which is 9 contained in the file License.txt included in VeraCrypt binary and source 10 code distribution packages. 11 */ 12 13 #include "Exception.h" 14 #include "MemoryStream.h" 15 16 namespace VeraCrypt 17 { 18 MemoryStream::MemoryStream (const ConstBufferPtr &data) : 19 ReadPosition (0) 20 { 21 Data = vector <byte> (data.Size()); 22 BufferPtr (&Data[0], Data.size()).CopyFrom (data); 23 } 24 25 uint64 MemoryStream::Read (const BufferPtr &buffer) 26 { 27 if (Data.size() == 0) 28 throw ParameterIncorrect (SRC_POS); 29 30 ConstBufferPtr streamBuf (*this); 31 size_t len = buffer.Size(); 32 if (streamBuf.Size() - ReadPosition < len) 33 len = streamBuf.Size() - ReadPosition; 34 35 BufferPtr(buffer).CopyFrom (streamBuf.GetRange (ReadPosition, len)); 36 ReadPosition += len; 37 return len; 38 } 39 40 void MemoryStream::ReadCompleteBuffer (const BufferPtr &buffer) 41 { 42 if (Read (buffer) != buffer.Size()) 43 throw InsufficientData (SRC_POS); 44 } 45 46 void MemoryStream::Write (const ConstBufferPtr &data) 47 { 48 for (uint64 i = 0; i < data.Size(); i++) 49 Data.push_back (data[i]); 50 } 51 }