VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/Unix/SystemInfo.cpp
blob: d4f648bbc1d9e54ef8bef8b6a4b6d8b45d79a430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 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 "Platform/SystemException.h"
#include "Platform/SystemInfo.h"
#include <sys/utsname.h>

namespace VeraCrypt
{
	wstring SystemInfo::GetPlatformName ()
	{
#ifdef TC_LINUX
		return L"Linux";
#elif defined (TC_MACOSX)
		return L"Mac OS X";
#elif defined (TC_FREEBSD)
		return L"FreeBSD";
#elif defined (TC_SOLARIS)
		return L"Solaris";
#else
#	error GetPlatformName() undefined
#endif

	}

	vector <int> SystemInfo::GetVersion ()
	{
		struct utsname unameData;
		throw_sys_if (uname (&unameData) == -1);

		vector <string> versionStrings = StringConverter::Split (unameData.release, ".");
		vector <int> version;

		for (size_t i = 0; i < versionStrings.size(); ++i)
		{
			string s = versionStrings[i];

			size_t p = s.find_first_not_of ("0123456789");
			if (p != string::npos)
				s = s.substr (0, p);

			if (s.empty())
				break;

			version.push_back (StringConverter::ToUInt32 (s));
		}

		return version;
	}

	bool SystemInfo::IsVersionAtLeast (int versionNumber1, int versionNumber2, int versionNumber3)
	{
		vector <int> osVersionNumbers = GetVersion();

		if (osVersionNumbers.size() < 2)
			throw ParameterIncorrect (SRC_POS);

		if (osVersionNumbers.size() < 3)
			osVersionNumbers[2] = 0;

		return (osVersionNumbers[0] * 10000000 +  osVersionNumbers[1] * 10000 + osVersionNumbers[2]) >=
			(versionNumber1 * 10000000 +  versionNumber2 * 10000 + versionNumber3);
	}
}
pan class="kt">void *memory); static void FreeAligned (void *memory); static void Zero (void *memory, size_t size); }; class Endian { public: static byte Big (const byte &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) { 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); } 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); } }; } #endif // TC_HEADER_Platform_Memory