VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Platform
diff options
context:
space:
mode:
Diffstat (limited to 'src/Platform')
-rw-r--r--src/Platform/Buffer.cpp2
-rw-r--r--src/Platform/Buffer.h26
-rw-r--r--src/Platform/FileStream.h2
-rw-r--r--src/Platform/Memory.h28
-rw-r--r--src/Platform/MemoryStream.cpp2
-rw-r--r--src/Platform/MemoryStream.h2
-rw-r--r--src/Platform/PlatformBase.h4
-rw-r--r--src/Platform/PlatformTest.cpp6
-rw-r--r--src/Platform/Serializer.cpp26
-rw-r--r--src/Platform/Serializer.h4
-rw-r--r--src/Platform/SharedPtr.h2
-rw-r--r--src/Platform/StringConverter.cpp4
-rw-r--r--src/Platform/TextReader.cpp2
-rw-r--r--src/Platform/Unix/File.cpp11
-rw-r--r--src/Platform/Unix/Process.cpp2
15 files changed, 67 insertions, 56 deletions
diff --git a/src/Platform/Buffer.cpp b/src/Platform/Buffer.cpp
index 82c2a3f1..5829b1d9 100644
--- a/src/Platform/Buffer.cpp
+++ b/src/Platform/Buffer.cpp
@@ -17,61 +17,61 @@ namespace VeraCrypt
{
Buffer::Buffer () : DataPtr (nullptr), DataSize (0), DataAlignment (0)
{
}
Buffer::Buffer (size_t size, size_t alignment) : DataPtr (nullptr), DataSize (0), DataAlignment (0)
{
Allocate (size, alignment);
}
Buffer::~Buffer ()
{
if (DataPtr != nullptr)
Free ();
}
void Buffer::Allocate (size_t size, size_t alignment)
{
if (size < 1)
throw ParameterIncorrect (SRC_POS);
if (DataPtr != nullptr)
{
if ((DataSize == size) && (DataAlignment == alignment))
return;
Free();
}
try
{
- DataPtr = static_cast<byte *> ((alignment > 0)? Memory::AllocateAligned (size, alignment) : Memory::Allocate (size));
+ DataPtr = static_cast<uint8 *> ((alignment > 0)? Memory::AllocateAligned (size, alignment) : Memory::Allocate (size));
DataSize = size;
DataAlignment = alignment;
}
catch (...)
{
DataPtr = nullptr;
DataSize = 0;
DataAlignment = 0;
throw;
}
}
void Buffer::CopyFrom (const ConstBufferPtr &bufferPtr, size_t alignment)
{
if (!IsAllocated () || ((bufferPtr.Size()) && (DataAlignment != alignment)))
{
if (IsAllocated ())
Free ();
if (bufferPtr.Size())
Allocate (bufferPtr.Size(), alignment);
}
else if (bufferPtr.Size() > DataSize)
throw ParameterTooLarge (SRC_POS);
if (bufferPtr.Size())
Memory::Copy (DataPtr, bufferPtr.Get(), bufferPtr.Size());
}
void Buffer::Erase ()
diff --git a/src/Platform/Buffer.h b/src/Platform/Buffer.h
index 1150fcf5..a1eb0918 100644
--- a/src/Platform/Buffer.h
+++ b/src/Platform/Buffer.h
@@ -1,121 +1,121 @@
/*
Derived from source code of TrueCrypt 7.1a, which is
Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
by the TrueCrypt License 3.0.
Modifications and additions to the original source code (contained in this file)
and all other portions of this file are Copyright (c) 2013-2017 IDRIX
and are governed by the Apache License 2.0 the full text of which is
contained in the file License.txt included in VeraCrypt binary and source
code distribution packages.
*/
#ifndef TC_HEADER_Platform_Buffer
#define TC_HEADER_Platform_Buffer
#include "PlatformBase.h"
#include "Memory.h"
namespace VeraCrypt
{
class ConstBufferPtr
{
public:
ConstBufferPtr ()
: DataPtr (nullptr), DataSize (0) { }
- ConstBufferPtr (const byte *data, size_t size)
+ ConstBufferPtr (const uint8 *data, size_t size)
: DataPtr (data), DataSize (size) { }
virtual ~ConstBufferPtr () { }
- operator const byte * () const { return DataPtr; }
+ operator const uint8 * () const { return DataPtr; }
bool IsDataEqual (const ConstBufferPtr &other) const { return Memory::Compare (DataPtr, DataSize, other.DataPtr, other.DataSize) == 0; }
- const byte *Get () const { return DataPtr; }
+ const uint8 *Get () const { return DataPtr; }
ConstBufferPtr GetRange (size_t offset, size_t size) const;
- void Set (const byte *data, size_t size) { DataPtr = data; DataSize = size; }
+ void Set (const uint8 *data, size_t size) { DataPtr = data; DataSize = size; }
size_t Size () const { return DataSize; }
protected:
- const byte *DataPtr;
+ const uint8 *DataPtr;
size_t DataSize;
};
class BufferPtr
{
public:
BufferPtr ()
: DataPtr (nullptr), DataSize (0) { }
- BufferPtr (byte *data, size_t size)
+ BufferPtr (uint8 *data, size_t size)
: DataPtr (data), DataSize (size) { }
virtual ~BufferPtr () { }
- operator byte * () const { return DataPtr; }
+ operator uint8 * () const { return DataPtr; }
void CopyFrom (const ConstBufferPtr &bufferPtr) const;
void Erase () const { Zero(); }
- byte *Get () const { return DataPtr; }
+ uint8 *Get () const { return DataPtr; }
BufferPtr GetRange (size_t offset, size_t size) const;
- void Set (byte *data, size_t size) { DataPtr = data; DataSize = size; }
+ void Set (uint8 *data, size_t size) { DataPtr = data; DataSize = size; }
size_t Size () const { return DataSize; }
void Zero () const { Memory::Zero (DataPtr, DataSize); }
operator ConstBufferPtr () const { return ConstBufferPtr (DataPtr, DataSize); }
protected:
- byte *DataPtr;
+ uint8 *DataPtr;
size_t DataSize;
};
class Buffer
{
public:
Buffer ();
Buffer (size_t size, size_t alignment = 0);
Buffer (const ConstBufferPtr &bufferPtr, size_t alignment = 0) { CopyFrom (bufferPtr, alignment); }
virtual ~Buffer ();
virtual void Allocate (size_t size, size_t alignment = 0);
virtual void CopyFrom (const ConstBufferPtr &bufferPtr, size_t alignment = 0);
- virtual byte *Ptr () const { return DataPtr; }
+ virtual uint8 *Ptr () const { return DataPtr; }
virtual void Erase ();
virtual void Free ();
virtual BufferPtr GetRange (size_t offset, size_t size) const;
virtual size_t Size () const { return DataSize; }
virtual size_t Alignment () const { return DataAlignment; }
virtual bool IsAllocated () const { return DataSize != 0; }
virtual void Zero ();
- virtual operator byte * () const { return DataPtr; }
+ virtual operator uint8 * () const { return DataPtr; }
virtual operator BufferPtr () const { return BufferPtr (DataPtr, DataSize); }
virtual operator ConstBufferPtr () const { return ConstBufferPtr (DataPtr, DataSize); }
protected:
- byte *DataPtr;
+ uint8 *DataPtr;
size_t DataSize;
size_t DataAlignment;
private:
Buffer (const Buffer &);
Buffer &operator= (const Buffer &);
};
class SecureBuffer : public Buffer
{
public:
SecureBuffer () { }
SecureBuffer (size_t size, size_t alignment = 0);
SecureBuffer (const ConstBufferPtr &bufferPtr) { CopyFrom (bufferPtr); }
virtual ~SecureBuffer ();
virtual void Allocate (size_t size, size_t alignment = 0);
virtual void Free ();
private:
SecureBuffer (const SecureBuffer &);
SecureBuffer &operator= (const SecureBuffer &);
};
}
#endif // TC_HEADER_Platform_Buffer
diff --git a/src/Platform/FileStream.h b/src/Platform/FileStream.h
index 0ef40862..66df1999 100644
--- a/src/Platform/FileStream.h
+++ b/src/Platform/FileStream.h
@@ -16,47 +16,47 @@
#include "PlatformBase.h"
#include "File.h"
#include "SharedPtr.h"
#include "Stream.h"
namespace VeraCrypt
{
class FileStream : public Stream
{
public:
FileStream (shared_ptr <File> file) : DataFile (file) { }
FileStream (File::SystemFileHandleType openFileHandle) { DataFile.reset (new File ()); DataFile->AssignSystemHandle (openFileHandle); }
virtual ~FileStream () { }
virtual uint64 Read (const BufferPtr &buffer)
{
return DataFile->Read (buffer);
}
virtual void ReadCompleteBuffer (const BufferPtr &buffer)
{
DataFile->ReadCompleteBuffer (buffer);
}
virtual string ReadToEnd ()
{
string str;
vector <char> buffer (4096);
uint64 len;
- while ((len = DataFile->Read (BufferPtr (reinterpret_cast <byte *> (&buffer[0]), buffer.size()))) > 0)
+ while ((len = DataFile->Read (BufferPtr (reinterpret_cast <uint8 *> (&buffer[0]), buffer.size()))) > 0)
str.insert (str.end(), buffer.begin(), buffer.begin() + static_cast <int> (len));
return str;
}
virtual void Write (const ConstBufferPtr &data)
{
DataFile->Write (data);
}
protected:
shared_ptr <File> DataFile;
};
}
#endif // TC_HEADER_Platform_FileStream
diff --git a/src/Platform/Memory.h b/src/Platform/Memory.h
index e0d4bfe3..b124022f 100644
--- a/src/Platform/Memory.h
+++ b/src/Platform/Memory.h
@@ -58,123 +58,123 @@
# define LITTLE_ENDIAN __LITTLE_ENDIAN
# endif
# ifndef BIG_ENDIAN
# define BIG_ENDIAN __BIG_ENDIAN
# endif
#endif // !BYTE_ORDER
#if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
# error Unsupported byte ordering detected.
#endif
namespace VeraCrypt
{
class Memory
{
public:
static void *Allocate (size_t size);
static void *AllocateAligned (size_t size, size_t alignment);
static int Compare (const void *memory1, size_t size1, const void *memory2, size_t size2);
static void Copy (void *memoryDestination, const void *memorySource, size_t size);
static void Free (void *memory);
static void FreeAligned (void *memory);
static void Zero (void *memory, size_t size);
};
class Endian
{
public:
- static byte Big (const byte &x)
+ static uint8 Big (const uint8 &x)
{
return x;
}
static uint16 Big (const uint16 &x)
{
#if BYTE_ORDER == BIG_ENDIAN
return x;
#else
return MirrorBytes (x);
#endif
}
static uint32 Big (const uint32 &x)
{
#if BYTE_ORDER == BIG_ENDIAN
return x;
#else
return MirrorBytes (x);
#endif
}
static uint64 Big (const uint64 &x)
{
#if BYTE_ORDER == BIG_ENDIAN
return x;
#else
return MirrorBytes (x);
#endif
}
- static byte Little (const byte &x)
+ static uint8 Little (const uint8 &x)
{
return x;
}
static uint16 Little (const uint16 &x)
{
#if BYTE_ORDER == LITTLE_ENDIAN
return x;
#else
return MirrorBytes (x);
#endif
}
static uint32 Little (const uint32 &x)
{
#if BYTE_ORDER == LITTLE_ENDIAN
return x;
#else
return MirrorBytes (x);
#endif
}
static uint64 Little (const uint64 &x)
{
#if BYTE_ORDER == LITTLE_ENDIAN
return x;
#else
return MirrorBytes (x);
#endif
}
protected:
static uint16 MirrorBytes (const uint16 &x)
{
return (x << 8) | (x >> 8);
}
static uint32 MirrorBytes (const uint32 &x)
{
- uint32 n = (byte) x;
- n <<= 8; n |= (byte) (x >> 8);
- n <<= 8; n |= (byte) (x >> 16);
- return (n << 8) | (byte) (x >> 24);
+ uint32 n = (uint8) x;
+ n <<= 8; n |= (uint8) (x >> 8);
+ n <<= 8; n |= (uint8) (x >> 16);
+ return (n << 8) | (uint8) (x >> 24);
}
static uint64 MirrorBytes (const uint64 &x)
{
- uint64 n = (byte) x;
- n <<= 8; n |= (byte) (x >> 8);
- n <<= 8; n |= (byte) (x >> 16);
- n <<= 8; n |= (byte) (x >> 24);
- n <<= 8; n |= (byte) (x >> 32);
- n <<= 8; n |= (byte) (x >> 40);
- n <<= 8; n |= (byte) (x >> 48);
- return (n << 8) | (byte) (x >> 56);
+ uint64 n = (uint8) x;
+ n <<= 8; n |= (uint8) (x >> 8);
+ n <<= 8; n |= (uint8) (x >> 16);
+ n <<= 8; n |= (uint8) (x >> 24);
+ n <<= 8; n |= (uint8) (x >> 32);
+ n <<= 8; n |= (uint8) (x >> 40);
+ n <<= 8; n |= (uint8) (x >> 48);
+ return (n << 8) | (uint8) (x >> 56);
}
};
}
#endif // TC_HEADER_Platform_Memory
diff --git a/src/Platform/MemoryStream.cpp b/src/Platform/MemoryStream.cpp
index 8bf229fe..1bd090b9 100644
--- a/src/Platform/MemoryStream.cpp
+++ b/src/Platform/MemoryStream.cpp
@@ -1,51 +1,51 @@
/*
Derived from source code of TrueCrypt 7.1a, which is
Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
by the TrueCrypt License 3.0.
Modifications and additions to the original source code (contained in this file)
and all other portions of this file are Copyright (c) 2013-2017 IDRIX
and are governed by the Apache License 2.0 the full text of which is
contained in the file License.txt included in VeraCrypt binary and source
code distribution packages.
*/
#include "Exception.h"
#include "MemoryStream.h"
namespace VeraCrypt
{
MemoryStream::MemoryStream (const ConstBufferPtr &data) :
ReadPosition (0)
{
- Data = vector <byte> (data.Size());
+ Data = vector <uint8> (data.Size());
BufferPtr (&Data[0], Data.size()).CopyFrom (data);
}
uint64 MemoryStream::Read (const BufferPtr &buffer)
{
if (Data.size() == 0)
throw ParameterIncorrect (SRC_POS);
ConstBufferPtr streamBuf (*this);
size_t len = buffer.Size();
if (streamBuf.Size() - ReadPosition < len)
len = streamBuf.Size() - ReadPosition;
BufferPtr(buffer).CopyFrom (streamBuf.GetRange (ReadPosition, len));
ReadPosition += len;
return len;
}
void MemoryStream::ReadCompleteBuffer (const BufferPtr &buffer)
{
if (Read (buffer) != buffer.Size())
throw InsufficientData (SRC_POS);
}
void MemoryStream::Write (const ConstBufferPtr &data)
{
for (uint64 i = 0; i < data.Size(); i++)
Data.push_back (data[i]);
}
}
diff --git a/src/Platform/MemoryStream.h b/src/Platform/MemoryStream.h
index 3df0bd13..068e6d6e 100644
--- a/src/Platform/MemoryStream.h
+++ b/src/Platform/MemoryStream.h
@@ -5,36 +5,36 @@
Modifications and additions to the original source code (contained in this file)
and all other portions of this file are Copyright (c) 2013-2017 IDRIX
and are governed by the Apache License 2.0 the full text of which is
contained in the file License.txt included in VeraCrypt binary and source
code distribution packages.
*/
#ifndef TC_HEADER_Platform_MemoryStream
#define TC_HEADER_Platform_MemoryStream
#include "PlatformBase.h"
#include "Stream.h"
namespace VeraCrypt
{
class MemoryStream : public Stream
{
public:
MemoryStream () : ReadPosition (0) { }
MemoryStream (const ConstBufferPtr &data);
virtual ~MemoryStream () { }
operator ConstBufferPtr () const { return ConstBufferPtr (&Data[0], Data.size()); }
virtual uint64 Read (const BufferPtr &buffer);
virtual void ReadCompleteBuffer (const BufferPtr &buffer);
virtual void Write (const ConstBufferPtr &data);
protected:
- vector <byte> Data;
+ vector <uint8> Data;
size_t ReadPosition;
};
}
#endif // TC_HEADER_Platform_MemoryStream
diff --git a/src/Platform/PlatformBase.h b/src/Platform/PlatformBase.h
index 22022dc7..e378704c 100644
--- a/src/Platform/PlatformBase.h
+++ b/src/Platform/PlatformBase.h
@@ -16,71 +16,71 @@
#include <cstddef>
#include <list>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#ifndef _MSC_VER
#include <inttypes.h>
#endif
using namespace std;
#ifdef nullptr
#undef nullptr
#endif
#if !(defined(_MSC_VER) && _MSC_VER >= 1600)
#define nullptr 0
#endif
namespace VeraCrypt
{
#ifdef _MSC_VER
# ifndef TC_INT_TYPES_DEFINED
typedef __int8 int8;
typedef __int16 int16;
typedef __int32 int32;
typedef __int64 int64;
- typedef unsigned __int8 byte;
+ typedef unsigned __int8 uint8;
typedef unsigned __int16 uint16;
typedef unsigned __int32 uint32;
typedef unsigned __int64 uint64;
# endif
#else
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
- typedef uint8_t byte;
+ typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
#endif
}
#if (defined(_WIN32) || defined(_WIN64)) && !defined(TC_WINDOWS)
# define TC_WINDOWS
#endif
#if defined(_DEBUG) && !defined(DEBUG)
# define DEBUG
#endif
#ifndef TC_TO_STRING
# define TC_TO_STRING2(n) #n
# define TC_TO_STRING(n) TC_TO_STRING2(n)
#endif
#define TC_JOIN_ARGS(a,b) a##b
#define TC_JOIN(a,b) TC_JOIN_ARGS(a,b)
#ifdef __GNUC__
template <class T> string GetFunctionName (T pos)
{
string s (pos);
size_t p = s.find ('(');
if (p == string::npos)
return s;
s = s.substr (0, p);
diff --git a/src/Platform/PlatformTest.cpp b/src/Platform/PlatformTest.cpp
index 7362d6bf..9a3faae2 100644
--- a/src/Platform/PlatformTest.cpp
+++ b/src/Platform/PlatformTest.cpp
@@ -49,61 +49,61 @@ namespace VeraCrypt
uint64 i64 = 0x0123456789abcdefULL;
string str = "string test";
wstring wstr = L"wstring test";
string convStr = "test";
StringConverter::ToSingle (wstr, convStr);
if (convStr != "wstring test")
throw TestFailed (SRC_POS);
StringConverter::Erase (convStr);
if (convStr != " ")
throw TestFailed (SRC_POS);
wstring wEraseTest = L"erase test";
StringConverter::Erase (wEraseTest);
if (wEraseTest != L" ")
throw TestFailed (SRC_POS);
list <string> stringList;
stringList.push_back (str + "1");
stringList.push_back (str + "2");
stringList.push_back (str + "3");
list <wstring> wstringList;
wstringList.push_back (wstr + L"1");
wstringList.push_back (wstr + L"2");
wstringList.push_back (wstr + L"3");
Buffer buffer (10);
for (size_t i = 0; i < buffer.Size(); i++)
- buffer[i] = (byte) i;
+ buffer[i] = (uint8) i;
ser.Serialize ("int32", i32);
ser.Serialize ("int64", i64);
ser.Serialize ("string", str);
ser.Serialize ("wstring", wstr);
ser.Serialize ("stringList", stringList);
ser.Serialize ("wstringList", wstringList);
ser.Serialize ("buffer", ConstBufferPtr (buffer));
ExecutedProcessFailed ex (SRC_POS, "cmd", -123, "error output");
ex.Serialize (stream);
list < shared_ptr <ExecutedProcessFailed> > exList;
exList.push_back (make_shared <ExecutedProcessFailed> (ExecutedProcessFailed (SRC_POS, "cmd", -123, "error output1")));
exList.push_back (make_shared <ExecutedProcessFailed> (ExecutedProcessFailed (SRC_POS, "cmd", -234, "error output2")));
exList.push_back (make_shared <ExecutedProcessFailed> (ExecutedProcessFailed (SRC_POS, "cmd", -567, "error output3")));
Serializable::SerializeList (stream, exList);
#if 0
if (file->IsOpen())
file->SeekAt (0);
#endif
uint32 di32;
ser.Deserialize ("int32", di32);
if (i32 != di32)
throw TestFailed (SRC_POS);
uint64 di64;
ser.Deserialize ("int64", di64);
@@ -114,61 +114,61 @@ namespace VeraCrypt
ser.Deserialize ("string", dstr);
if (str != dstr)
throw TestFailed (SRC_POS);
wstring dwstr;
ser.Deserialize ("wstring", dwstr);
if (str != dstr)
throw TestFailed (SRC_POS);
int i = 1;
foreach (string item, ser.DeserializeStringList ("stringList"))
{
stringstream s;
s << str << i++;
if (item != s.str())
throw TestFailed (SRC_POS);
}
i = 1;
foreach (wstring item, ser.DeserializeWStringList ("wstringList"))
{
wstringstream s;
s << wstr << i++;
if (item != s.str())
throw TestFailed (SRC_POS);
}
Buffer dbuffer (10);
ser.Deserialize ("buffer", buffer);
for (size_t i = 0; i < buffer.Size(); i++)
- if (buffer[i] != (byte) i)
+ if (buffer[i] != (uint8) i)
throw TestFailed (SRC_POS);
shared_ptr <ExecutedProcessFailed> dex = Serializable::DeserializeNew <ExecutedProcessFailed> (stream);
if (!dex
|| dex->GetCommand() != "cmd"
|| dex->GetExitCode() != -123
|| dex->GetErrorOutput() != "error output")
throw TestFailed (SRC_POS);
list < shared_ptr <ExecutedProcessFailed> > dexList;
Serializable::DeserializeList (stream, dexList);
i = 1;
foreach_ref (const ExecutedProcessFailed &ex, dexList)
{
stringstream s;
s << "error output" << i++;
if (ex.GetErrorOutput() != s.str())
throw TestFailed (SRC_POS);
}
}
// shared_ptr, Mutex, ScopeLock, SyncEvent, Thread
static struct
{
shared_ptr <int> SharedIntPtr;
Mutex IntMutex;
SyncEvent ExitAllowedEvent;
} ThreadTestData;
void PlatformTest::ThreadTest ()
@@ -211,61 +211,61 @@ namespace VeraCrypt
}
if (*ThreadTestData.SharedIntPtr != 0)
throw TestFailed (SRC_POS);
}
TC_THREAD_PROC PlatformTest::ThreadTestProc (void *arg)
{
if (arg != (void *) &ThreadTestData)
return 0;
{
ScopeLock sl (ThreadTestData.IntMutex);
++(*ThreadTestData.SharedIntPtr);
}
ThreadTestData.ExitAllowedEvent.Wait();
{
ScopeLock sl (ThreadTestData.IntMutex);
--(*ThreadTestData.SharedIntPtr);
}
return 0;
}
bool PlatformTest::TestAll ()
{
// Integer types
- if (sizeof (byte) != 1 || sizeof (int8) != 1 || sizeof (__int8) != 1) throw TestFailed (SRC_POS);
+ if (sizeof (uint8) != 1 || sizeof (int8) != 1 || sizeof (__int8) != 1) throw TestFailed (SRC_POS);
if (sizeof (uint16) != 2 || sizeof (int16) != 2 || sizeof (__int16) != 2) throw TestFailed (SRC_POS);
if (sizeof (uint32) != 4 || sizeof (int32) != 4 || sizeof (__int32) != 4) throw TestFailed (SRC_POS);
if (sizeof (uint64) != 8 || sizeof (int64) != 8) throw TestFailed (SRC_POS);
// Exception handling
TestFlag = false;
try
{
try
{
throw TestFailed (SRC_POS);
}
catch (...)
{
throw;
}
return false;
}
catch (Exception &)
{
TestFlag = true;
}
if (!TestFlag)
return false;
// RTTI
RttiTest rtti;
RttiTestBase &rttiBaseRef = rtti;
RttiTestBase *rttiBasePtr = &rtti;
diff --git a/src/Platform/Serializer.cpp b/src/Platform/Serializer.cpp
index b69fe891..6f428b15 100644
--- a/src/Platform/Serializer.cpp
+++ b/src/Platform/Serializer.cpp
@@ -1,74 +1,74 @@
/*
Derived from source code of TrueCrypt 7.1a, which is
Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
by the TrueCrypt License 3.0.
Modifications and additions to the original source code (contained in this file)
and all other portions of this file are Copyright (c) 2013-2017 IDRIX
and are governed by the Apache License 2.0 the full text of which is
contained in the file License.txt included in VeraCrypt binary and source
code distribution packages.
*/
#include "Exception.h"
#include "ForEach.h"
#include "Memory.h"
#include "Serializer.h"
namespace VeraCrypt
{
template <typename T>
T Serializer::Deserialize ()
{
uint64 size;
- DataStream->ReadCompleteBuffer (BufferPtr ((byte *) &size, sizeof (size)));
+ DataStream->ReadCompleteBuffer (BufferPtr ((uint8 *) &size, sizeof (size)));
if (Endian::Big (size) != sizeof (T))
throw ParameterIncorrect (SRC_POS);
T data;
- DataStream->ReadCompleteBuffer (BufferPtr ((byte *) &data, sizeof (data)));
+ DataStream->ReadCompleteBuffer (BufferPtr ((uint8 *) &data, sizeof (data)));
return Endian::Big (data);
}
void Serializer::Deserialize (const string &name, bool &data)
{
ValidateName (name);
- data = Deserialize <byte> () == 1;
+ data = Deserialize <uint8> () == 1;
}
- void Serializer::Deserialize (const string &name, byte &data)
+ void Serializer::Deserialize (const string &name, uint8 &data)
{
ValidateName (name);
- data = Deserialize <byte> ();
+ data = Deserialize <uint8> ();
}
void Serializer::Deserialize (const string &name, int32 &data)
{
ValidateName (name);
data = (int32) Deserialize <uint32> ();
}
void Serializer::Deserialize (const string &name, int64 &data)
{
ValidateName (name);
data = (int64) Deserialize <uint64> ();
}
void Serializer::Deserialize (const string &name, uint32 &data)
{
ValidateName (name);
data = Deserialize <uint32> ();
}
void Serializer::Deserialize (const string &name, uint64 &data)
{
ValidateName (name);
data = Deserialize <uint64> ();
}
void Serializer::Deserialize (const string &name, string &data)
{
ValidateName (name);
data = DeserializeString ();
@@ -100,129 +100,129 @@ namespace VeraCrypt
int32 Serializer::DeserializeInt32 (const string &name)
{
ValidateName (name);
return Deserialize <uint32> ();
}
int64 Serializer::DeserializeInt64 (const string &name)
{
ValidateName (name);
return Deserialize <uint64> ();
}
uint32 Serializer::DeserializeUInt32 (const string &name)
{
ValidateName (name);
return Deserialize <uint32> ();
}
uint64 Serializer::DeserializeUInt64 (const string &name)
{
ValidateName (name);
return Deserialize <uint64> ();
}
string Serializer::DeserializeString ()
{
uint64 size = Deserialize <uint64> ();
vector <char> data ((size_t) size);
- DataStream->ReadCompleteBuffer (BufferPtr ((byte *) &data[0], (size_t) size));
+ DataStream->ReadCompleteBuffer (BufferPtr ((uint8 *) &data[0], (size_t) size));
return string (&data[0]);
}
string Serializer::DeserializeString (const string &name)
{
ValidateName (name);
return DeserializeString ();
}
list <string> Serializer::DeserializeStringList (const string &name)
{
ValidateName (name);
list <string> deserializedList;
uint64 listSize = Deserialize <uint64> ();
for (size_t i = 0; i < listSize; i++)
deserializedList.push_back (DeserializeString ());
return deserializedList;
}
wstring Serializer::DeserializeWString ()
{
uint64 size = Deserialize <uint64> ();
vector <wchar_t> data ((size_t) size / sizeof (wchar_t));
- DataStream->ReadCompleteBuffer (BufferPtr ((byte *) &data[0], (size_t) size));
+ DataStream->ReadCompleteBuffer (BufferPtr ((uint8 *) &data[0], (size_t) size));
return wstring (&data[0]);
}
list <wstring> Serializer::DeserializeWStringList (const string &name)
{
ValidateName (name);
list <wstring> deserializedList;
uint64 listSize = Deserialize <uint64> ();
for (size_t i = 0; i < listSize; i++)
deserializedList.push_back (DeserializeWString ());
return deserializedList;
}
wstring Serializer::DeserializeWString (const string &name)
{
ValidateName (name);
return DeserializeWString ();
}
template <typename T>
void Serializer::Serialize (T data)
{
uint64 size = Endian::Big (uint64 (sizeof (data)));
- DataStream->Write (ConstBufferPtr ((byte *) &size, sizeof (size)));
+ DataStream->Write (ConstBufferPtr ((uint8 *) &size, sizeof (size)));
data = Endian::Big (data);
- DataStream->Write (ConstBufferPtr ((byte *) &data, sizeof (data)));
+ DataStream->Write (ConstBufferPtr ((uint8 *) &data, sizeof (data)));
}
void Serializer::Serialize (const string &name, bool data)
{
SerializeString (name);
- byte d = data ? 1 : 0;
+ uint8 d = data ? 1 : 0;
Serialize (d);
}
- void Serializer::Serialize (const string &name, byte data)
+ void Serializer::Serialize (const string &name, uint8 data)
{
SerializeString (name);
Serialize (data);
}
void Serializer::Serialize (const string &name, const char *data)
{
Serialize (name, string (data));
}
void Serializer::Serialize (const string &name, int32 data)
{
SerializeString (name);
Serialize ((uint32) data);
}
void Serializer::Serialize (const string &name, int64 data)
{
SerializeString (name);
Serialize ((uint64) data);
}
void Serializer::Serialize (const string &name, uint32 data)
{
SerializeString (name);
Serialize (data);
}
void Serializer::Serialize (const string &name, uint64 data)
{
@@ -255,49 +255,49 @@ namespace VeraCrypt
Serialize (listSize);
foreach (const string &item, stringList)
SerializeString (item);
}
void Serializer::Serialize (const string &name, const list <wstring> &stringList)
{
SerializeString (name);
uint64 listSize = stringList.size();
Serialize (listSize);
foreach (const wstring &item, stringList)
SerializeWString (item);
}
void Serializer::Serialize (const string &name, const ConstBufferPtr &data)
{
SerializeString (name);
uint64 size = data.Size();
Serialize (size);
DataStream->Write (data);
}
void Serializer::SerializeString (const string &data)
{
Serialize ((uint64) data.size() + 1);
- DataStream->Write (ConstBufferPtr ((byte *) (data.data() ? data.data() : data.c_str()), data.size() + 1));
+ DataStream->Write (ConstBufferPtr ((uint8 *) (data.data() ? data.data() : data.c_str()), data.size() + 1));
}
void Serializer::SerializeWString (const wstring &data)
{
uint64 size = (data.size() + 1) * sizeof (wchar_t);
Serialize (size);
- DataStream->Write (ConstBufferPtr ((byte *) (data.data() ? data.data() : data.c_str()), (size_t) size));
+ DataStream->Write (ConstBufferPtr ((uint8 *) (data.data() ? data.data() : data.c_str()), (size_t) size));
}
void Serializer::ValidateName (const string &name)
{
string dName = DeserializeString();
if (dName != name)
{
throw ParameterIncorrect (SRC_POS);
}
}
}
diff --git a/src/Platform/Serializer.h b/src/Platform/Serializer.h
index c2dac324..3617a83b 100644
--- a/src/Platform/Serializer.h
+++ b/src/Platform/Serializer.h
@@ -1,78 +1,78 @@
/*
Derived from source code of TrueCrypt 7.1a, which is
Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
by the TrueCrypt License 3.0.
Modifications and additions to the original source code (contained in this file)
and all other portions of this file are Copyright (c) 2013-2017 IDRIX
and are governed by the Apache License 2.0 the full text of which is
contained in the file License.txt included in VeraCrypt binary and source
code distribution packages.
*/
#ifndef TC_HEADER_Platform_Serializer
#define TC_HEADER_Platform_Serializer
#include "PlatformBase.h"
#include "Buffer.h"
#include "SharedPtr.h"
#include "Stream.h"
namespace VeraCrypt
{
class Serializer
{
public:
Serializer (shared_ptr <Stream> stream) : DataStream (stream) { }
virtual ~Serializer () { }
void Deserialize (const string &name, bool &data);
- void Deserialize (const string &name, byte &data);
+ void Deserialize (const string &name, uint8 &data);
void Deserialize (const string &name, int32 &data);
void Deserialize (const string &name, int64 &data);
void Deserialize (const string &name, uint32 &data);
void Deserialize (const string &name, uint64 &data);
void Deserialize (const string &name, string &data);
void Deserialize (const string &name, wstring &data);
void Deserialize (const string &name, const BufferPtr &data);
bool DeserializeBool (const string &name);
int32 DeserializeInt32 (const string &name);
int64 DeserializeInt64 (const string &name);
uint32 DeserializeUInt32 (const string &name);
uint64 DeserializeUInt64 (const string &name);
string DeserializeString (const string &name);
list <string> DeserializeStringList (const string &name);
wstring DeserializeWString (const string &name);
list <wstring> DeserializeWStringList (const string &name);
void Serialize (const string &name, bool data);
- void Serialize (const string &name, byte data);
+ void Serialize (const string &name, uint8 data);
void Serialize (const string &name, const char *data);
void Serialize (const string &name, int32 data);
void Serialize (const string &name, int64 data);
void Serialize (const string &name, uint32 data);
void Serialize (const string &name, uint64 data);
void Serialize (const string &name, const string &data);
void Serialize (const string &name, const wstring &data);
void Serialize (const string &name, const wchar_t *data);
void Serialize (const string &name, const list <string> &stringList);
void Serialize (const string &name, const list <wstring> &stringList);
void Serialize (const string &name, const ConstBufferPtr &data);
protected:
template <typename T> T Deserialize ();
string DeserializeString ();
wstring DeserializeWString ();
template <typename T> void Serialize (T data);
void SerializeString (const string &data);
void SerializeWString (const wstring &data);
void ValidateName (const string &name);
shared_ptr <Stream> DataStream;
private:
Serializer (const Serializer &);
Serializer &operator= (const Serializer &);
};
}
#endif // TC_HEADER_Platform_Serializer
diff --git a/src/Platform/SharedPtr.h b/src/Platform/SharedPtr.h
index 29669714..f80b2167 100644
--- a/src/Platform/SharedPtr.h
+++ b/src/Platform/SharedPtr.h
@@ -1,54 +1,54 @@
/*
Derived from source code of TrueCrypt 7.1a, which is
Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
by the TrueCrypt License 3.0.
Modifications and additions to the original source code (contained in this file)
and all other portions of this file are Copyright (c) 2013-2017 IDRIX
and are governed by the Apache License 2.0 the full text of which is
contained in the file License.txt included in VeraCrypt binary and source
code distribution packages.
*/
#ifndef TC_HEADER_Platform_SharedPtr
#define TC_HEADER_Platform_SharedPtr
#include <stdexcept>
#include <memory>
#include "SharedVal.h"
#ifdef nullptr
namespace VeraCrypt
{
-#if (__cplusplus >= 201103L)
+#if (__cplusplus >= 201103L) || defined(__GXX_EXPERIMENTAL_CXX0X__)
#define VC_USE_NATIVE_PTR 1
#endif
#ifdef VC_USE_NATIVE_PTR
#define shared_ptr std::shared_ptr
#define make_shared std::make_shared
#define move_ptr std::move
#else
template <class T>
class SharedPtr
{
public:
explicit SharedPtr ()
: Pointer (nullptr), UseCount (nullptr) { }
explicit SharedPtr (T *pointer)
: Pointer (pointer), UseCount (new SharedVal <uint64> (1)) { }
SharedPtr (const SharedPtr &source)
{
CopyFrom (source);
}
~SharedPtr ()
{
Release();
}
diff --git a/src/Platform/StringConverter.cpp b/src/Platform/StringConverter.cpp
index e1a6df98..7b3134ee 100644
--- a/src/Platform/StringConverter.cpp
+++ b/src/Platform/StringConverter.cpp
@@ -358,62 +358,62 @@ namespace VeraCrypt
if (size == (size_t) -1)
throw StringConversionFailed (SRC_POS);
vector <wchar_t> buf (size + 1);
Memory::Zero (&mbState, sizeof (mbState));
if ((size = mbsrtowcs (&buf[0], &src, buf.size(), &mbState)) == (size_t) -1)
throw StringConversionFailed (SRC_POS);
wstring s;
s.insert (s.begin(), buf.begin(), buf.begin() + size);
return s;
}
catch (...)
{
if (noThrow)
return L"";
throw;
}
}
void StringConverter::ToWideBuffer (const wstring &str, wchar_t *buffer, size_t bufferSize)
{
if (str.length() < 1)
{
buffer[0] = 0;
return;
}
BufferPtr (
- (byte *) buffer,
+ (uint8 *) buffer,
bufferSize).CopyFrom (
- ConstBufferPtr ((byte *) (wstring (str).c_str()),
+ ConstBufferPtr ((uint8 *) (wstring (str).c_str()),
(str.length() + 1) * sizeof (wchar_t)
)
);
}
string StringConverter::Trim (const string &str)
{
size_t start = 0;
size_t end = str.size();
if (end < 1)
return str;
foreach (char c, str)
{
if (c > ' ')
break;
++start;
}
foreach_reverse (char c, str)
{
if (c > ' ')
break;
--end;
}
return str.substr (start, end - start);
}
}
diff --git a/src/Platform/TextReader.cpp b/src/Platform/TextReader.cpp
index 7d6a0c64..bdaf19c2 100644
--- a/src/Platform/TextReader.cpp
+++ b/src/Platform/TextReader.cpp
@@ -1,41 +1,41 @@
/*
Derived from source code of TrueCrypt 7.1a, which is
Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
by the TrueCrypt License 3.0.
Modifications and additions to the original source code (contained in this file)
and all other portions of this file are Copyright (c) 2013-2017 IDRIX
and are governed by the Apache License 2.0 the full text of which is
contained in the file License.txt included in VeraCrypt binary and source
code distribution packages.
*/
#include "TextReader.h"
namespace VeraCrypt
{
TextReader::TextReader (const FilePath &path)
{
InputFile.reset (new File);
InputFile->Open (path);
InputStream = shared_ptr <Stream> (new FileStream (InputFile));
}
bool TextReader::ReadLine (string &outputString)
{
outputString.erase();
char c;
- while (InputStream->Read (BufferPtr ((byte *) &c, sizeof (c))) == sizeof (c))
+ while (InputStream->Read (BufferPtr ((uint8 *) &c, sizeof (c))) == sizeof (c))
{
if (c == '\r')
continue;
if (c == '\n')
return true;
outputString += c;
}
return !outputString.empty();
}
}
diff --git a/src/Platform/Unix/File.cpp b/src/Platform/Unix/File.cpp
index d3413800..207efb4e 100644
--- a/src/Platform/Unix/File.cpp
+++ b/src/Platform/Unix/File.cpp
@@ -195,60 +195,71 @@ namespace VeraCrypt
#else
throw NotImplemented (SRC_POS);
#endif
}
uint64 File::Length () const
{
if_debug (ValidateState());
// BSD does not support seeking to the end of a device
#ifdef TC_BSD
if (Path.IsBlockDevice() || Path.IsCharacterDevice())
{
# ifdef TC_MACOSX
uint32 blockSize;
uint64 blockCount;
throw_sys_sub_if (ioctl (FileHandle, DKIOCGETBLOCKSIZE, &blockSize) == -1, wstring (Path));
throw_sys_sub_if (ioctl (FileHandle, DKIOCGETBLOCKCOUNT, &blockCount) == -1, wstring (Path));
return blockCount * blockSize;
# elif TC_OPENBSD
struct disklabel dl;
throw_sys_sub_if (ioctl (FileHandle, DIOCGPDINFO, &dl) == -1, wstring (Path));
return DL_GETDSIZE(&dl);
# else
uint64 mediaSize;
throw_sys_sub_if (ioctl (FileHandle, DIOCGMEDIASIZE, &mediaSize) == -1, wstring (Path));
return mediaSize;
# endif
}
#endif
+#ifdef TC_LINUX
+ // On Linux, try to use BLKGETSIZE64 for devices
+ if (Path.IsDevice())
+ {
+ uint64 mediaSize;
+ if (ioctl (FileHandle, BLKGETSIZE64, &mediaSize) != -1)
+ {
+ return mediaSize;
+ }
+ }
+#endif
off_t current = lseek (FileHandle, 0, SEEK_CUR);
throw_sys_sub_if (current == -1, wstring (Path));
SeekEnd (0);
uint64 length = lseek (FileHandle, 0, SEEK_CUR);
SeekAt (current);
return length;
}
void File::Open (const FilePath &path, FileOpenMode mode, FileShareMode shareMode, FileOpenFlags flags)
{
#ifdef TC_LINUX
int sysFlags = O_LARGEFILE;
#else
int sysFlags = 0;
#endif
switch (mode)
{
case CreateReadWrite:
sysFlags |= O_CREAT | O_TRUNC | O_RDWR;
break;
case CreateWrite:
sysFlags |= O_CREAT | O_TRUNC | O_WRONLY;
break;
case OpenRead:
sysFlags |= O_RDONLY;
break;
diff --git a/src/Platform/Unix/Process.cpp b/src/Platform/Unix/Process.cpp
index 36b01e6b..46b14a1e 100644
--- a/src/Platform/Unix/Process.cpp
+++ b/src/Platform/Unix/Process.cpp
@@ -147,59 +147,59 @@ namespace VeraCrypt
stdOutput.insert (stdOutput.end(), buffer.begin(), buffer.begin() + bytesRead);
else if (fd == errPipe.GetReadFD())
errOutput.insert (errOutput.end(), buffer.begin(), buffer.begin() + bytesRead);
else if (fd == exceptionPipe.GetReadFD())
exOutput.insert (exOutput.end(), buffer.begin(), buffer.begin() + bytesRead);
}
}
if (bytesRead == 0)
{
waitRes = waitpid (forkedPid, &status, 0);
break;
}
}
catch (TimeOut&)
{
timeTaken += pollTimeout;
if (timeOut >= 0 && timeTaken >= timeOut)
throw;
}
} while ((waitRes = waitpid (forkedPid, &status, WNOHANG)) == 0);
throw_sys_if (waitRes == -1);
if (!exOutput.empty())
{
unique_ptr <Serializable> deserializedObject;
Exception *deserializedException = nullptr;
try
{
- shared_ptr <Stream> stream (new MemoryStream (ConstBufferPtr ((byte *) &exOutput[0], exOutput.size())));
+ shared_ptr <Stream> stream (new MemoryStream (ConstBufferPtr ((uint8 *) &exOutput[0], exOutput.size())));
deserializedObject.reset (Serializable::DeserializeNew (stream));
deserializedException = dynamic_cast <Exception*> (deserializedObject.get());
}
catch (...) { }
if (deserializedException)
deserializedException->Throw();
}
int exitCode = (WIFEXITED (status) ? WEXITSTATUS (status) : 1);
if (exitCode != 0)
{
string strErrOutput;
if (!errOutput.empty())
strErrOutput.insert (strErrOutput.begin(), errOutput.begin(), errOutput.end());
throw ExecutedProcessFailed (SRC_POS, processName, exitCode, strErrOutput);
}
string strOutput;
if (!stdOutput.empty())
strOutput.insert (strOutput.begin(), stdOutput.begin(), stdOutput.end());
return strOutput;
}
}