Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mumble_plugin_utils.h « plugins - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 355239d2c67debf63cfe739dc0c88ee7ea8452be (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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2016-2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_MUMBLE_PLUGIN_UTILS_H_
#define MUMBLE_MUMBLE_PLUGIN_UTILS_H_

#include <cmath>
#include <codecvt>
#include <fstream>
#include <locale>
#include <sstream>
#include <vector>

#ifdef OS_LINUX
#	include <fenv.h>
#endif

#ifdef _MSC_VER
#	include <corecrt_math_defines.h>
#	include <intrin.h>
#endif

/// This union is used by isBigEndian() to determine the endianness.
union SingleSplit4Bytes {
	uint32_t single;
	uint8_t split[4];

	constexpr SingleSplit4Bytes(const uint32_t value) : single(value) {}
};

/// Converts from UTF-8 to UTF-16.
static inline std::wstring utf8ToUtf16(const std::string &str) {
	try {
		std::wstring_convert< std::codecvt_utf8_utf16< wchar_t > > conv;
		return conv.from_bytes(str);
	} catch (std::range_error &) {
		return {};
	}
}

/// Converts from UTF-16 to UTF-8.
/// Meant to be used when "wchar_t" is 2 bytes, usually with Windows processes.
static inline std::string utf16ToUtf8(const std::u16string &str) {
	try {
		std::wstring_convert< std::codecvt_utf8_utf16< char16_t >, char16_t > conv;
		return conv.to_bytes(str);
	} catch (std::range_error &) {
		return {};
	}
}

/// Converts from UTF-16 to UTF-8.
/// Meant to be used when "wchar_t" is 4 bytes, usually with Linux processes.
static inline std::string utf16ToUtf8(const std::u32string &str) {
	try {
		std::wstring_convert< std::codecvt_utf8_utf16< char32_t >, char32_t > conv;
		return conv.to_bytes(str);
	} catch (std::range_error &) {
		return {};
	}
}

// escape lossily converts the given
// string to ASCII, replacing any
// character not within the printable
// ASCII region (32-126) with an ASCII
// space character.
//
// escape also replaces any double quote
// characters with an ASCII space. This
// allows the string to be safely used
// when constructing JSON documents via
// string concatenation.
//
// Finally, escape ensures that the given
// string is NUL-terminated by always
// setting the last byte of the input
// string to the value 0.
static inline void escape(char *str, const size_t &size) {
	// Ensure the input string is properly NUL-terminated.
	str[size - 1] = 0;
	char *c       = str;

	while (*c != '\0') {
		// For JSON compatibility, the string
		// can't contain double quotes.
		// If a double quote is found, replace
		// it with an ASCII space.
		if (*c == '"') {
			*c = ' ';
		}

		// Ensure the string is within printable
		// ASCII. If not, replace the offending
		// byte with an ASCII space.
		if (*c < 32 || *c > 126) {
			*c = ' ';
		}

		c += 1;
	}
}

/// Reads the file's content and returns it in a std::string.
static inline std::string readFile(const std::string &path) {
	std::ifstream ifs(path, std::ifstream::binary);
	std::string content;
	char buf[256];

	while (ifs.good()) {
		ifs.read(&buf[0], sizeof(buf));
		size_t read = ifs.gcount();
		if (read > 0) {
			content.append(&buf[0], read);
		}
	}

	return content;
}

/// Reads characters from the stream until \p character is encountered.
/// @return Read characters and whether the stream is still good.
static inline std::pair< std::string, bool > readUntil(std::stringstream &stream,
													   const std::stringstream::int_type character) {
	std::string buf;

	for (auto input = stream.get(); input != character; input = stream.get()) {
		if (!stream) {
			return { buf, false };
		}

		buf.push_back(input);
	};

	return { buf, true };
}

/// Discards characters in the stream until the specified one is encountered.
/// @param[inverted] Inverts the behavior.
/// @return Whether the stream is still good.
static inline bool skipUntil(std::stringstream &stream, const std::stringstream::int_type character,
							 const bool inverted) {
	for (auto input = stream.get(); inverted ? (input == character) : (input != character); input = stream.get()) {
		if (!stream) {
			return false;
		}
	}

	return true;
}

/// Returns the index of the specified pattern in the specified buffer.
/// "?" is used as wildcard character.
/// If the pattern is not found, the function returns SIZE_MAX.
static inline size_t searchInBuffer(const std::vector< uint8_t > &pattern, const std::vector< uint8_t > &buffer) {
	for (size_t i = 0; i < buffer.size() - pattern.size(); ++i) {
		const auto *buf = &buffer[i];
		bool match      = true;

		for (size_t j = 0; j < pattern.size(); ++j) {
			if (pattern[j] != '?' && buf[j] != pattern[j]) {
				match = false;
				break;
			}
		}

		if (match) {
			return buf - &buffer[0];
		}
	}

	return SIZE_MAX;
}

/// Calculates sine and cosine of the specified value.
/// On Linux the calculation is guaranteed to be simultaneous.
static inline bool sinCos(const float value, float &outSin, float &outCos) {
#ifdef OS_WINDOWS
	outSin = sin(value);
	outCos = cos(value);
	return true;
#else
	errno = 0;
	feclearexcept(FE_ALL_EXCEPT);

	sincosf(value, &outSin, &outCos);

	return fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) == 0;
#endif
}

/// Converts degrees to radians.
static inline float degreesToRadians(const float degrees) {
	constexpr float piOver180 = M_PI / 180.0f;
	return piOver180 * degrees;
}

/// Detects whether the architecture is big-endian.
static constexpr bool isBigEndian() {
	// An union allows access to a single byte without the need for a cast.
	return SingleSplit4Bytes(0x01020304).split[0] == 1;
}

/// Converts from network byte order to host byte order.
static inline uint16_t networkToHost(const uint16_t value) {
	if (isBigEndian()) {
		return value;
	}
#ifdef _MSC_VER
	return _byteswap_ushort(value);
#else
	return __builtin_bswap16(value);
#endif
}

#endif