Skip to content

Commit ff42c8c

Browse files
committed
Plugins: Update CommonUtil plugin
1 parent f5e5d76 commit ff42c8c

File tree

7 files changed

+178
-7
lines changed

7 files changed

+178
-7
lines changed

plugins/CommonUtil/CommonUtil.vcxproj

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,30 @@
5555
</ImportGroup>
5656
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
5757
<Link>
58-
<AdditionalDependencies>windowscodecs.lib;uxtheme.lib;%(AdditionalDependencies)</AdditionalDependencies>
58+
<AdditionalDependencies>windowscodecs.lib;winhttp.lib;ws2_32.lib;uxtheme.lib;%(AdditionalDependencies)</AdditionalDependencies>
59+
<DelayLoadDLLs>winhttp.dll;ws2_32.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
5960
</Link>
6061
</ItemDefinitionGroup>
6162
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
6263
<Link>
63-
<AdditionalDependencies>windowscodecs.lib;uxtheme.lib;%(AdditionalDependencies)</AdditionalDependencies>
64+
<AdditionalDependencies>windowscodecs.lib;winhttp.lib;ws2_32.lib;uxtheme.lib;%(AdditionalDependencies)</AdditionalDependencies>
65+
<DelayLoadDLLs>winhttp.dll;ws2_32.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
6466
</Link>
6567
</ItemDefinitionGroup>
6668
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
6769
<Link>
68-
<AdditionalDependencies>windowscodecs.lib;uxtheme.lib;%(AdditionalDependencies)</AdditionalDependencies>
70+
<AdditionalDependencies>windowscodecs.lib;winhttp.lib;ws2_32.lib;uxtheme.lib;%(AdditionalDependencies)</AdditionalDependencies>
71+
<DelayLoadDLLs>winhttp.dll;ws2_32.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
6972
</Link>
7073
</ItemDefinitionGroup>
7174
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
7275
<Link>
73-
<AdditionalDependencies>windowscodecs.lib;uxtheme.lib;%(AdditionalDependencies)</AdditionalDependencies>
76+
<AdditionalDependencies>windowscodecs.lib;winhttp.lib;ws2_32.lib;uxtheme.lib;%(AdditionalDependencies)</AdditionalDependencies>
77+
<DelayLoadDLLs>winhttp.dll;ws2_32.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
7478
</Link>
7579
</ItemDefinitionGroup>
7680
<ItemGroup>
81+
<ClCompile Include="http.c" />
7782
<ClCompile Include="json-c\arraylist.c" />
7883
<ClCompile Include="json-c\debug.c" />
7984
<ClCompile Include="json-c\json_c_version.c" />

plugins/CommonUtil/CommonUtil.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
<ClCompile Include="json-c\debug.c">
6868
<Filter>Source Files\json-c</Filter>
6969
</ClCompile>
70+
<ClCompile Include="http.c">
71+
<Filter>Source Files</Filter>
72+
</ClCompile>
7073
</ItemGroup>
7174
<ItemGroup>
7275
<ClInclude Include="main.h">

plugins/CommonUtil/http.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Process Hacker Plugins -
3+
* CommonUtil Plugin
4+
*
5+
* Copyright (C) 2016 dmex
6+
*
7+
* This file is part of Process Hacker.
8+
*
9+
* Process Hacker is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* Process Hacker is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
#include "main.h"
25+
#include <winsock2.h>
26+
#include <ws2tcpip.h>
27+
#include <winhttp.h>
28+
29+
PPH_BYTES UtilReadSocketString(
30+
_In_ SOCKET Handle
31+
)
32+
{
33+
PSTR data;
34+
ULONG allocatedLength;
35+
ULONG dataLength;
36+
ULONG returnLength;
37+
BYTE buffer[PAGE_SIZE];
38+
39+
allocatedLength = sizeof(buffer);
40+
data = (PSTR)PhAllocate(allocatedLength);
41+
dataLength = 0;
42+
43+
// Zero the buffer
44+
memset(buffer, 0, PAGE_SIZE);
45+
46+
while ((returnLength = recv(Handle, buffer, PAGE_SIZE, 0)) != SOCKET_ERROR)
47+
{
48+
if (returnLength == 0)
49+
break;
50+
51+
if (allocatedLength < dataLength + returnLength)
52+
{
53+
allocatedLength *= 2;
54+
data = (PSTR)PhReAllocate(data, allocatedLength);
55+
}
56+
57+
// Copy the returned buffer into our pointer
58+
memcpy(data + dataLength, buffer, returnLength);
59+
// Zero the returned buffer for the next loop
60+
//memset(buffer, 0, returnLength);
61+
62+
dataLength += returnLength;
63+
}
64+
65+
if (allocatedLength < dataLength + 1)
66+
{
67+
allocatedLength++;
68+
data = (PSTR)PhReAllocate(data, allocatedLength);
69+
}
70+
71+
// Ensure that the buffer is null-terminated
72+
data[dataLength] = 0;
73+
74+
return PhCreateBytesEx(data, dataLength);
75+
}
76+
77+
PPH_BYTES UtilReadWinHttpString(
78+
_In_ HINTERNET Handle
79+
)
80+
{
81+
BYTE buffer[PAGE_SIZE];
82+
PSTR data;
83+
ULONG allocatedLength;
84+
ULONG dataLength;
85+
ULONG returnLength;
86+
87+
allocatedLength = sizeof(buffer);
88+
data = (PSTR)PhAllocate(allocatedLength);
89+
dataLength = 0;
90+
91+
// Zero the buffer
92+
memset(buffer, 0, PAGE_SIZE);
93+
94+
while (WinHttpReadData(Handle, buffer, PAGE_SIZE, &returnLength))
95+
{
96+
if (returnLength == 0)
97+
break;
98+
99+
if (allocatedLength < dataLength + returnLength)
100+
{
101+
allocatedLength *= 2;
102+
data = (PSTR)PhReAllocate(data, allocatedLength);
103+
}
104+
105+
// Copy the returned buffer into our pointer
106+
memcpy(data + dataLength, buffer, returnLength);
107+
// Zero the returned buffer for the next loop
108+
//memset(buffer, 0, returnLength);
109+
110+
dataLength += returnLength;
111+
}
112+
113+
if (allocatedLength < dataLength + 1)
114+
{
115+
allocatedLength++;
116+
data = (PSTR)PhReAllocate(data, allocatedLength);
117+
}
118+
119+
// Ensure that the buffer is null-terminated
120+
data[dataLength] = 0;
121+
122+
return PhCreateBytesEx(data, dataLength);
123+
}

plugins/CommonUtil/json.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ PVOID UtilGetJsonObject(
8484
return json_get_object(Object, Key);
8585
}
8686

87+
INT UtilGetJsonObjectLength(
88+
_In_ PVOID Object
89+
)
90+
{
91+
return json_object_object_length(Object);
92+
}
93+
8794
BOOL UtilGetJsonObjectBool(
8895
_In_ PVOID Object,
8996
_In_ PSTR Key
@@ -120,7 +127,7 @@ PSTR UtilGetJsonArrayString(
120127
_In_ PVOID Object
121128
)
122129
{
123-
return _strdup(json_object_to_json_string(Object));
130+
return _strdup( json_object_to_json_string(Object) ); // leak
124131
}
125132

126133
INT UtilGetArrayLength(
@@ -137,3 +144,26 @@ PVOID UtilGetObjectArrayIndex(
137144
{
138145
return json_object_array_get_idx(Object, Index);
139146
}
147+
148+
PPH_LIST UtilGetObjectArrayList(
149+
_In_ PVOID Object
150+
)
151+
{
152+
json_object_iter object_iter;
153+
PPH_LIST listArray;
154+
155+
listArray = PhCreateList(1);
156+
157+
json_object_object_foreachC(Object, object_iter)
158+
{
159+
PJSON_ARRAY_LIST_OBJECT object = PhAllocate(sizeof(JSON_ARRAY_LIST_OBJECT));
160+
memset(object, 0, sizeof(JSON_ARRAY_LIST_OBJECT));
161+
162+
object->Key = object_iter.key;
163+
object->Entry = object_iter.val;
164+
165+
PhAddItemList(listArray, object);
166+
}
167+
168+
return listArray;
169+
}

plugins/CommonUtil/main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ COMMONUTIL_INTERFACE PluginInterface =
3838
UtilGetJsonObjectBool,
3939
UtilCreateJsonObject,
4040
UtilGetJsonObject,
41+
UtilGetJsonObjectLength,
4142
UtilJsonAddObject,
4243
UtilCreateJsonArray,
4344
UtilAddJsonArray,
4445
UtilGetJsonArrayString,
4546
UtilGetArrayLength,
46-
UtilGetObjectArrayIndex
47+
UtilGetObjectArrayIndex,
48+
UtilGetObjectArrayList
4749
};
4850

4951
LOGICAL DllMain(
@@ -63,7 +65,7 @@ LOGICAL DllMain(
6365
if (!PluginInstance)
6466
return FALSE;
6567

66-
info->DisplayName = L"CommonUtil Plugin";
68+
info->DisplayName = L"Common Plugin Extensions";
6769
info->Author = L"dmex";
6870
info->Description = L"Common Plugin Extensions";
6971
info->HasOptions = FALSE;

plugins/CommonUtil/main.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ PVOID UtilGetJsonObject(
126126
_In_ PSTR Key
127127
);
128128

129+
INT UtilGetJsonObjectLength(
130+
_In_ PVOID Object
131+
);
132+
129133
BOOL UtilGetJsonObjectBool(
130134
_In_ PVOID Object,
131135
_In_ PSTR Key
@@ -150,4 +154,8 @@ PVOID UtilGetObjectArrayIndex(
150154
_In_ INT Index
151155
);
152156

157+
PPH_LIST UtilGetObjectArrayList(
158+
_In_ PVOID Object
159+
);
160+
153161
#endif

plugins/CommonUtil/resource.rc

-16 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)