summaryrefslogtreecommitdiffstats
path: root/windows/bootpgm/win32/Handle.cpp
blob: f55f3316825e07cfbb0ef306a250b83a4ff4b0e6 (plain) (blame)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "StdAfx.h"
#include "Handle.h"
#include "main.h"

void debug(char *msg)
{
	mainSingleton->get_io().debugout(msg);
}

void handle(ULONG status,char *file,char *line)
{
	if (status!=STATUS_SUCCESS)
	{
		char *buffer = new char[5000];
		sprintf(buffer,"Fehler in %s Zeile %s: 0x%X",file,line,status);
		mainSingleton->get_io().println(buffer);
		mainSingleton->get_io().readln(buffer,sizeof(buffer));
	}
}

WinObject::~WinObject()
{
	if (valid())
	{
		char buffer[500];
		debug((L"Closing Handle to '" + get_name() + L"'" ).chars(buffer,500));
		ULONG status = ZwClose(handle);
		CHECKER(status)
	}
}

UnicodeString WinObject::get_name()
{
	OBJECT_BASIC_INFORMATION info;
	ULONG status = NtQueryObject(
		handle
		,ObjectBasicInformation
		,&info
		,sizeof(info)
		,0);
	
	CHECKER(status);

	int len=info.NameInformationLength;

	len = len==0? 500 : len;
	
	if (len>0)
	{
		OBJECT_NAME_INFORMATION *poni=reinterpret_cast<OBJECT_NAME_INFORMATION*>(_alloca(len));
		status = NtQueryObject(
			handle
			,ObjectNameInformation
			,poni
			,len
			,0);

		CHECKER(status);

		return UnicodeString::from_unicode(poni->Name);
	}
	else
		return UnicodeString(L"");	
}

UnicodeString::UnicodeString(const wchar_t *chars)
{
	unsigned short len = (unsigned short)wcslen(chars);
	wchar_t *buffer=new wchar_t[len];
	string.Length= len * sizeof(wchar_t);
	string.Buffer=buffer;
	string.MaximumLength=string.Length;
	memcpy(buffer,chars,len*2);

	/*debug("UnicodeString created:");
	char buffer2[500];
	debug(this->chars(buffer2,500));*/
}
UnicodeString::UnicodeString(const wchar_t *chars,unsigned int length)
{
	string.Length = (USHORT)length;
	string.MaximumLength = (USHORT)length;
	string.Buffer = new wchar_t[length/2];
	memcpy(string.Buffer,chars,length);
}
UnicodeString::UnicodeString(const char *chars)
{
	unsigned short l=(unsigned short)strlen(chars);
	wchar_t *wcs=new wchar_t[l];
	mbstowcs(wcs,chars,l);
	string.Length = l*2;
	string.MaximumLength = l*2;
	string.Buffer = wcs;
}
/*UnicodeString &UnicodeString::operator=(wchar_t *chars)
{
	char buffer[1000];
	_snprintf(buffer,1000,"UnicodeString = called old = %S, new = %S",string.Buffer,chars);
	debug(buffer);

	unsigned short len= (unsigned short)wcslen(chars) * 2;

	if (len > string.MaximumLength)
	{
		delete string.Buffer;
		string.Buffer = new wchar_t[len/2];
		string.MaximumLength = len;
	}
	
	memcpy(string.Buffer,chars,len);
	string.Length = len;

	return *this;
}*/
UnicodeString::~UnicodeString()
{
	debug("UnicodeString deleted:");
	char buffer[500];
	debug(this->chars(buffer,500));

	delete string.Buffer;
}
UnicodeString UnicodeString::operator+(UnicodeString&str2)
{
	UNICODE_STRING str;

	str.Length = 0;
	str.MaximumLength = string.Length + str2.string.Length;
	str.Buffer = new wchar_t[str.MaximumLength/2];

	NT::RtlAppendUnicodeStringToString(&str,&string);
	NT::RtlAppendUnicodeStringToString(&str,&str2.string);

	return UnicodeString(str);
}

int RegKey::get_value(UnicodeString *path,PULONG type,void *buffer,int length)
{
	int len=length + sizeof(KEY_VALUE_PARTIAL_INFORMATION);
	void *temp=_alloca(len);
	ULONG read;
	KEY_VALUE_PARTIAL_INFORMATION *pinfo = reinterpret_cast<KEY_VALUE_PARTIAL_INFORMATION*>(temp);
	ULONG status=ZwQueryValueKey(
		handle
		,&path->unicode_string()
		,KeyValuePartialInformation
		,pinfo
		,len
		,&read);

	CHECKER(status)

	if (status!=STATUS_SUCCESS)
		return 0;

	memcpy(buffer,pinfo->Data,pinfo->DataLength);
	*type = pinfo->Type;
	return pinfo->DataLength;
}

void RegKey::set_value(UnicodeString *path,ULONG type,void *data,ULONG size)
{
	ULONG status = ZwSetValueKey(
		handle
		,&path->unicode_string()
		,0
		,type
		,data
		,size);
	CHECKER(status)
}

UnicodeString RegKey::get_string_value(UnicodeString *name)
{
	char buffer[500];
	ULONG type;
	ULONG len=get_value(name,&type,buffer,sizeof(buffer));
	
	if (type!=REG_SZ){
		debug("Only REG_SZ allowed in get_string_value");
		return (wchar_t*)0;
	}
	
	wchar_t *value= reinterpret_cast<wchar_t*>(buffer);
	value[min(sizeof(buffer)-1,len)] = 0;
	return value;
}

HANDLE RegKey::open_key(HANDLE parent,UnicodeString &path)
{
	OBJECT_ATTRIBUTES ObjectAttributes;
	
	UnicodeString fullPath = UnicodeString(L"\\Registry\\") + path;
	UnicodeString *pp = parent ? &path : &fullPath;

	InitializeObjectAttributes(
		&ObjectAttributes,
		const_cast<UNICODE_STRING*>(&pp->unicode_string()),
		OBJ_CASE_INSENSITIVE,
		parent,
		NULL);

	HANDLE h;
	ULONG Disposition;
	ULONG status = ZwOpenKey(
		&h,
		KEY_ALL_ACCESS,
		&ObjectAttributes);

	if (status == 0xC000034){
		IO &io=mainSingleton->get_io();
		io.print("Not found: ");
		char buffer[1000];
		io.println(path.chars(buffer,sizeof(buffer)));
	}

	return status == STATUS_SUCCESS? h : 0;
}

void RegKey::flush()
{
	ULONG status=NtFlushKey(handle);
	CHECKER(status);
}

void RegKey::save_to(HANDLE fileHandle)
{
	ULONG status = NtSaveKey(handle,fileHandle);
	CHECKER(status);
}

void RegKey::print_subkeys(IO &io)
{
	ULONG status = STATUS_SUCCESS;
	int i=0;
	char buffer[1000];
	do
	{		
		ULONG res;
		KEY_BASIC_INFORMATION *info = reinterpret_cast<KEY_BASIC_INFORMATION*>(buffer);
		status = ZwEnumerateKey(
			handle
			,i
			,KeyBasicInformation
			,buffer
			,sizeof(buffer)
			,&res);

		if (status == STATUS_SUCCESS)
		{
			UnicodeString name(info->Name,info->NameLength);
			io.println(name.chars(buffer,sizeof(buffer)));
		}

		i++;
	} while (status == STATUS_SUCCESS);
}
void RegKey::print_values(IO &io)
{
	ULONG status = STATUS_SUCCESS;
	int i=0;
	char buffer[1000];
	do
	{
		ULONG len;
		status = ZwEnumerateValueKey(
			handle
			,i
			,KeyValueBasicInformation
			,buffer
			,sizeof(buffer)-2
			,&len);
		
		if (status==STATUS_SUCCESS)
		{
			KEY_VALUE_BASIC_INFORMATION *info = reinterpret_cast<KEY_VALUE_BASIC_INFORMATION*>(buffer);
			info->Name[info->NameLength/2] = 0;
			char buf[100];
			_snprintf(buf,sizeof(buf),"%-30S | %-15s",info->Name,type_to_name(info->Type));
			io.println(buf);			
		}

		i++;
	} while(status == STATUS_SUCCESS);
}
RegKey *RegKey::subkey(UnicodeString&name)
{
	HANDLE h = open_key(handle,name);
	if (h)
		return new RegKey(h);
	else
		return 0;
}