summaryrefslogtreecommitdiffhomepage
path: root/src/system/ntapi_tt_get_system_info_snapshot.c
blob: fd51f465adcac2e67dd0baed56899b46a818ea89 (plain)
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
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <pemagine/pemagine.h>
#include <ntapi/nt_sysinfo.h>
#include <ntapi/nt_memory.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

int32_t __stdcall __ntapi_tt_get_system_info_snapshot(
	__in_out nt_system_information_snapshot * sys_info_snapshot)
{
	int32_t		status;

	/* pre-allocated buffer? */
	if (sys_info_snapshot->buffer)
		status = __ntapi->zw_query_system_information(
			sys_info_snapshot->sys_info_class,
			sys_info_snapshot->buffer,
			sys_info_snapshot->max_len,
			&sys_info_snapshot->info_len);
	else {
		/* set initial buffer size */
		sys_info_snapshot->max_len = NT_ALLOCATION_GRANULARITY;

		/* allocate initial buffer */
		status = __ntapi->zw_allocate_virtual_memory(
			NT_CURRENT_PROCESS_HANDLE,
			(void **)&sys_info_snapshot->buffer,
			0,
			&sys_info_snapshot->max_len,
			NT_MEM_COMMIT,
			NT_PAGE_READWRITE);

		/* verification */
		if (status != NT_STATUS_SUCCESS)
			return status;

		/* loop until buffer is large enough to satisfy the system */
		while ((status = __ntapi->zw_query_system_information(
					sys_info_snapshot->sys_info_class,
					sys_info_snapshot->buffer,
					sys_info_snapshot->max_len,
					&sys_info_snapshot->info_len))
				== NT_STATUS_INFO_LENGTH_MISMATCH) {

			/* free previously allocated memory */
			status = __ntapi->zw_free_virtual_memory(
					NT_CURRENT_PROCESS_HANDLE,
					(void **)&sys_info_snapshot->buffer,
					&sys_info_snapshot->max_len,
					NT_MEM_RELEASE);

			/* verification */
			if (status != NT_STATUS_SUCCESS)
				return status;

			/* reset buffer and increase buffer size */
			sys_info_snapshot->buffer = (nt_system_information_buffer *)0;
			sys_info_snapshot->max_len += NT_ALLOCATION_GRANULARITY;

			/* reallocate buffer memory */
			status = __ntapi->zw_allocate_virtual_memory(
					NT_CURRENT_PROCESS_HANDLE,
					(void **)&sys_info_snapshot->buffer,
					0,
					&sys_info_snapshot->max_len,
					NT_MEM_COMMIT,
					NT_PAGE_READWRITE);

			/* verification */
			if (status != NT_STATUS_SUCCESS)
				return status;
		}
	}

	/* verification */
	if (status == NT_STATUS_SUCCESS) {
		sys_info_snapshot->pcurrent = &sys_info_snapshot->buffer->mark;
		return NT_STATUS_SUCCESS;
	} else {
		sys_info_snapshot->pcurrent = (void *)0;
		return status;
	}
}