File: misc_util.cpp

package info (click to toggle)
libopenmpt 0.4.3-1+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,724 kB
  • sloc: cpp: 99,820; sh: 4,503; ansic: 3,449; makefile: 480
file content (118 lines) | stat: -rw-r--r-- 2,474 bytes parent folder | download | duplicates (2)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * misc_util.cpp
 * -------------
 * Purpose: Various useful utility functions.
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */


#include "stdafx.h"
#include "misc_util.h"


OPENMPT_NAMESPACE_BEGIN



namespace Util
{


static const MPT_UCHAR_TYPE EncodeNibble[16] = {
	UC_('0'), UC_('1'), UC_('2'), UC_('3'),
	UC_('4'), UC_('5'), UC_('6'), UC_('7'),
	UC_('8'), UC_('9'), UC_('A'), UC_('B'),
	UC_('C'), UC_('D'), UC_('E'), UC_('F') };

static inline bool DecodeByte(uint8 &byte, MPT_UCHAR_TYPE c1, MPT_UCHAR_TYPE c2)
{
	byte = 0;
	if(UC_('0') <= c1 && c1 <= UC_('9'))
	{
		byte += static_cast<uint8>((c1 - UC_('0')) << 4);
	} else if(UC_('A') <= c1 && c1 <= UC_('F'))
	{
		byte += static_cast<uint8>((c1 - UC_('A') + 10) << 4);
	} else if(UC_('a') <= c1 && c1 <= UC_('f'))
	{
		byte += static_cast<uint8>((c1 - UC_('a') + 10) << 4);
	} else
	{
		return false;
	}
	if(UC_('0') <= c2 && c2 <= UC_('9'))
	{
		byte += static_cast<uint8>(c2 - UC_('0'));
	} else if(UC_('A') <= c2 && c2 <= UC_('F'))
	{
		byte += static_cast<uint8>(c2 - UC_('A') + 10);
	} else if(UC_('a') <= c2 && c2 <= UC_('f'))
	{
		byte += static_cast<uint8>(c2 - UC_('a') + 10);
	} else
	{
		return false;
	}
	return true;
}

mpt::ustring BinToHex(mpt::const_byte_span src)
{
	mpt::ustring result;
	result.reserve(src.size() * 2);
	for(mpt::byte byte : src)
	{
		result.push_back(EncodeNibble[(mpt::byte_cast<uint8>(byte) & 0xf0) >> 4]);
		result.push_back(EncodeNibble[mpt::byte_cast<uint8>(byte) & 0x0f]);
	}
	return result;
}

std::vector<mpt::byte> HexToBin(const mpt::ustring &src)
{
	std::vector<mpt::byte> result;
	result.reserve(src.size() / 2);
	for(std::size_t i = 0; (i + 1) < src.size(); i += 2)
	{
		uint8 byte = 0;
		if(!DecodeByte(byte, src[i], src[i + 1]))
		{
			return result;
		}
		result.push_back(mpt::byte_cast<mpt::byte>(byte));
	}
	return result;
}


} // namespace Util


#if defined(MODPLUG_TRACKER) || (defined(LIBOPENMPT_BUILD) && defined(LIBOPENMPT_BUILD_TEST))

namespace mpt
{

std::string getenv(const std::string &env_var, const std::string &def)
{
#if MPT_OS_WINDOWS && MPT_OS_WINDOWS_WINRT
	MPT_UNREFERENCED_PARAMETER(env_var);
	return def;
#else
	const char *val = std::getenv(env_var.c_str());
	if(!val)
	{
		return def;
	}
	return val;
#endif
}

} // namespace mpt

#endif // MODPLUG_TRACKER || (LIBOPENMPT_BUILD && LIBOPENMPT_BUILD_TEST)


OPENMPT_NAMESPACE_END