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: 307dc76ff264021d52363af2bcebab1fa525c747 (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
// Copyright 2005-2019 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 <codecvt>
#include <locale>

static inline std::string utf16ToUtf8(const std::wstring &wstr) {
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
	return conv.to_bytes(wstr);
}

// 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 void escape(char *str, 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 = ' ';
        } else if (*c > 126) {
            *c = ' ';
        }

        c += 1;
    }
}

#endif