OpenVPN 3 Core Library
Loading...
Searching...
No Matches
unicode.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_WIN_UNICODE_H
13#define OPENVPN_WIN_UNICODE_H
14
15#include <windows.h>
16
17#include <string>
18#include <memory>
19
22
23namespace openvpn::Win {
24typedef std::unique_ptr<wchar_t[]> UTF16;
25typedef std::unique_ptr<char[]> UTF8;
26
28
29inline wchar_t *utf16(const std::string &str, int cp = CP_UTF8)
30{
31 // first get output length (return value includes space for trailing nul)
32 const int len = ::MultiByteToWideChar(cp,
33 0,
34 str.c_str(),
35 -1,
36 nullptr,
37 0);
38 if (len <= 0)
39 throw win_utf16();
40 UTF16 ret(new wchar_t[len]);
41 const int len2 = ::MultiByteToWideChar(cp,
42 0,
43 str.c_str(),
44 -1,
45 ret.get(),
46 len);
47 if (len != len2)
48 throw win_utf16();
49 return ret.release();
50}
51
52inline size_t utf16_strlen(const wchar_t *str)
53{
54 return ::wcslen(str);
55}
56
57inline char *utf8(wchar_t *str)
58{
59 // first get output length (return value includes space for trailing nul)
60 const int len = ::WideCharToMultiByte(CP_UTF8,
61 0,
62 str,
63 -1,
64 NULL,
65 0,
66 NULL,
67 NULL);
68 if (len <= 0)
69 throw win_utf16();
70 UTF8 ret(new char[len]);
71 const int len2 = ::WideCharToMultiByte(CP_UTF8,
72 0,
73 str,
74 -1,
75 ret.get(),
76 len,
77 NULL,
78 NULL);
79 if (len != len2)
80 throw win_utf16();
81 return ret.release();
82}
83} // namespace openvpn::Win
84#endif
#define OPENVPN_SIMPLE_EXCEPTION(C)
Definition exception.hpp:75
char * utf8(wchar_t *str)
Definition unicode.hpp:57
std::unique_ptr< wchar_t[]> UTF16
Definition unicode.hpp:24
size_t utf16_strlen(const wchar_t *str)
Definition unicode.hpp:52
wchar_t * utf16(const std::string &str, int cp=CP_UTF8)
Definition unicode.hpp:29
std::unique_ptr< char[]> UTF8
Definition unicode.hpp:25
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
std::string ret