-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_unicode_string_wcsstr.cpp
More file actions
161 lines (138 loc) · 3.34 KB
/
test_unicode_string_wcsstr.cpp
File metadata and controls
161 lines (138 loc) · 3.34 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
/**
* @file Custom UNICODE_STRING substring routine test
* @brief
* @ref
* @author Yonhgwhan, Roh (fixbrain@gmail.com)
* @date 12/28/2023 15:13
* @copyright All rights reserved by Yonghwan, Roh.
**/
#include "stdafx.h"
typedef struct _UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
#ifdef MIDL_PASS
[size_is(MaximumLength / 2), length_is((Length) / 2)] USHORT* Buffer;
#else // MIDL_PASS
_Field_size_bytes_part_opt_(MaximumLength, Length) PWCH Buffer;
#endif // MIDL_PASS
} UNICODE_STRING;
typedef UNICODE_STRING* PUNICODE_STRING;
typedef const UNICODE_STRING* PCUNICODE_STRING;
/// @brief
_Must_inspect_result_
_IRQL_requires_max_(PASSIVE_LEVEL)
wchar_t*
uni_wcsstr(
_In_ const PUNICODE_STRING haystack,
_In_ const PUNICODE_STRING needle,
_In_ bool reverse
)
{
//PAGED_CODE();
if (nullptr == haystack || nullptr == needle)
{
return nullptr;
}
if (haystack->Length == 0 || needle->Length == 0)
{
return nullptr;
}
if (needle->Length > haystack->Length)
{
return nullptr;
}
if (needle->Buffer[0] == L'\0')
{
return haystack->Buffer;
}
USHORT const cnt_haystack = haystack->Length / sizeof(wchar_t);
USHORT const cnt_needle = needle->Length / sizeof(wchar_t);
// needle 의 사이즈를 품을 수 있는 영역까지만 검사하면 되므로.
USHORT const last_index = cnt_haystack - cnt_needle;
if (true != reverse)
{
for (int i_hay = 0; i_hay <= last_index; ++i_hay)
{
USHORT i_needle = 0;
for (; i_needle < cnt_needle; ++i_needle)
{
if ((wchar_t)haystack->Buffer[i_hay + i_needle] != (wchar_t)needle->Buffer[i_needle])
{
// move next character in haystack
break;
}
}
if (i_needle == cnt_needle)
{
return (wchar_t*)&haystack->Buffer[i_hay];
}
}
}
else
{
for (int i_hay = last_index; i_hay >= 0; --i_hay)
{
USHORT i_needle = 0;
for (; i_needle < cnt_needle; ++i_needle)
{
if ((wchar_t)haystack->Buffer[i_hay+i_needle] != (wchar_t)needle->Buffer[i_needle])
{
// move next character in haystack
break;
}
}
if (i_needle == cnt_needle)
{
return (wchar_t*)&haystack->Buffer[i_hay];
}
}
}
return nullptr;
}
/// @brief
/// @return
bool test_uni_wcsstr()
{
struct test_struct
{
const wchar_t* haystack;
const wchar_t* needle;
const bool expected;
}
data[] =
{
{L"012340123401234", L"", true},
{L"012340123401234", L"123", true},
{nullptr, nullptr, false}
};
for (int i = 0; i < sizeof(test_struct); ++i)
{
if (nullptr == data[i].haystack) break;
UNICODE_STRING hay;
hay.Buffer = (wchar_t*)data[i].haystack;
// test 를 위해서 일부러 null 문자 길이를 뺌
hay.Length = hay.MaximumLength = (USHORT)wcslen(data[i].haystack)*sizeof(wchar_t) - sizeof(wchar_t);
UNICODE_STRING needle;
needle.Buffer = (wchar_t*)data[i].needle;
needle.Length = needle.MaximumLength = (USHORT)wcslen(data[i].needle) * sizeof(wchar_t) - sizeof(wchar_t);
bool reverse = true;
wchar_t* result = uni_wcsstr(&hay, &needle, reverse);
log_info
"hay=%ws, needle=%ws, reverse=%s, result=%ws",
hay.Buffer,
needle.Buffer,
reverse ? "true" : "false",
result
log_end;
reverse = false;
result = uni_wcsstr(&hay, &needle, reverse);
log_info
"hay=%ws, needle=%ws, reverse=%s, result=%ws",
hay.Buffer,
needle.Buffer,
reverse ? "true" : "false",
result
log_end;
}
return true;
}