forked from pig4210/xlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws_s.cpp
More file actions
163 lines (131 loc) · 3.26 KB
/
ws_s.cpp
File metadata and controls
163 lines (131 loc) · 3.26 KB
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
#include "ws_s.h"
#include "xlib_nt.h"
using namespace std;
size_t ws2s(char* s,const size_t max_s,const wchar_t* ws,const size_t ws_len)
{
if(s == nullptr || ws == nullptr || (intptr_t)max_s < 1)
return 0;
const size_t wlen = (((intptr_t)ws_len >= 0) ? ws_len : wcslen(ws)) * sizeof(wchar_t);
size_t wused = 0;
size_t sused = 0;
while((wused < wlen) && (sused < max_s))
{
size_t wnow = wlen - wused;
if(wnow > 0xFFFE) wnow = 0xFFFE;
UNICODE_STRING us;
us.Length = (USHORT)wnow;
us.MaximumLength = (USHORT)wnow;
us.Buffer = (PWCH)&ws[wused / sizeof(wchar_t)];
size_t snow = max_s - sused;
if(snow > 0xFFFF) snow = 0xFFFF;
ANSI_STRING as =
{
(USHORT)0,
(USHORT)snow,
s
};
if(!NT_SUCCESS(RtlUnicodeStringToAnsiString(&as, &us, false)))
{
break;
}
sused += as.Length;
wused += wnow;
}
return sused;
}
string ws2s(const std::wstring& ws)
{
string s;
if(ws.empty()) return s;
const size_t wlen = ws.size() * sizeof(wchar_t);
size_t wused = 0;
while(wused < wlen)
{
size_t wnow = wlen - wused;
if(wnow > 0xFFFE) wnow = 0xFFFE;
UNICODE_STRING us;
us.Length = (USHORT)wnow;
us.MaximumLength = (USHORT)wnow;
us.Buffer = (PWCH)(ws.c_str() + wused / sizeof(wchar_t));
ANSI_STRING as;
as.Length = 0;
as.MaximumLength = 0;
as.Buffer = nullptr;
bool rets = NT_SUCCESS(RtlUnicodeStringToAnsiString(&as, &us, true));
if(rets && as.Length != 0)
{
s.append(as.Buffer, as.Length);
}
RtlFreeAnsiString(&as);
if(!rets)
{
break;
}
wused += wnow;
}
return s;
}
size_t s2ws(wchar_t* ws,const size_t max_ws,const char* s,const size_t s_len)
{
if(ws == nullptr || s == nullptr || (intptr_t)max_ws < 1)
return false;
const size_t slen = ((intptr_t)s_len >= 0) ? s_len : strlen(s);
size_t sused = 0;
size_t wused = 0;
while((sused < slen) && (wused < max_ws))
{
size_t snow = slen - sused;
if(snow > 0x7FFE) snow = 0x7FFE;
ANSI_STRING as;
as.Length = (USHORT)snow;
as.MaximumLength = (USHORT)snow;
as.Buffer = (PCH)&s[sused];
size_t wnow = (max_ws - wused) * sizeof(wchar_t);
if(wnow > 0xFFFE) wnow = 0xFFFE;
UNICODE_STRING us =
{
(USHORT)0,
(USHORT)wnow,
ws
};
if(!NT_SUCCESS(RtlAnsiStringToUnicodeString(&us, &as, false)))
{
break;
}
wused += us.Length / sizeof(wchar_t);
sused += snow;
}
return wused;
}
wstring s2ws(const std::string& s)
{
wstring ws;
if(s.empty()) return ws;
const size_t slen = s.size();
size_t sused = 0;
while(sused < slen)
{
size_t snow = slen - sused;
if(snow > 0x7FFE) snow = 0x7FFE;
ANSI_STRING as;
as.Length = (USHORT)snow;
as.MaximumLength = (USHORT)snow;
as.Buffer = (PCH)(s.c_str() + sused);
UNICODE_STRING us;
us.Length = 0;
us.MaximumLength = 0;
us.Buffer = nullptr;
const bool rets = NT_SUCCESS(RtlAnsiStringToUnicodeString(&us, &as, true));
if(rets && us.Length != 0)
{
ws.append(us.Buffer, us.Length / sizeof(wchar_t));
}
RtlFreeUnicodeString(&us);
if(!rets)
{
break;
}
sused += snow;
}
return ws;
}