Blame src/ldr/ntapi_ldr_create_state_snapshot.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
dde53a
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <pemagine/pemagine.h>
dd89bb
#include <dalist/dalist.h>
dd89bb
#include <ntapi/ntapi.h>
766a63
#include "ntapi_impl.h"
dd89bb
dd89bb
struct callback_ctx {
dd89bb
	struct dalist_ex *	ldr_state_snapshot;
dd89bb
	int32_t			status;
dd89bb
};
dd89bb
dd89bb
static int __cdecl __add_module_base_address_to_list(
dd89bb
	struct pe_ldr_tbl_entry *	ldr_tbl_entry,
dd89bb
	enum pe_callback_reason		int_callback_reason,
dd89bb
	void *				context)
dd89bb
{
dd89bb
	struct dalist_node *	node;
dd89bb
	struct callback_ctx *	ctx;
dd89bb
dd89bb
	ctx = (struct callback_ctx *)context;
dd89bb
dd89bb
	if (int_callback_reason == PE_CALLBACK_REASON_ERROR) {
dd89bb
		ctx->status = NT_STATUS_UNSUCCESSFUL;
dd89bb
		return ctx->status;
dd89bb
	} else if (int_callback_reason != PE_CALLBACK_REASON_ITEM) {
dd89bb
		ctx->status = NT_STATUS_SUCCESS;
dd89bb
		return 1;
dd89bb
	} else if (!ldr_tbl_entry->dll_base) {
dd89bb
		ctx->status = NT_STATUS_SUCCESS;
dd89bb
		return 1;
dd89bb
	}
dd89bb
dd89bb
	ctx->status = dalist_get_node_by_key(
dd89bb
		ctx->ldr_state_snapshot,
dd89bb
		(struct dalist_node_ex **)&node,
dd89bb
		(uintptr_t)ldr_tbl_entry->dll_base,
dd89bb
		DALIST_NODE_TYPE_NEW,
dd89bb
		0);
dd89bb
dd89bb
	if (ctx->status != DALIST_OK)
dd89bb
		return -1;
dd89bb
	else
dd89bb
		return 1;
dd89bb
}
dd89bb
dd89bb
dd89bb
int __cdecl __ntapi_ldr_create_state_snapshot(
dd89bb
	__out	struct dalist_ex * ldr_state_snapshot)
dd89bb
{
dd89bb
	struct callback_ctx ctx;
dd89bb
dd89bb
	if (!ldr_state_snapshot->free && !ldr_state_snapshot->memfn_ptr)
dd89bb
		return NT_STATUS_BUFFER_TOO_SMALL;
dd89bb
	else if (ldr_state_snapshot->info.list_nodes)
dd89bb
		return NT_STATUS_INVALID_USER_BUFFER;
dd89bb
dd89bb
	ctx.ldr_state_snapshot = ldr_state_snapshot;
dd89bb
dd89bb
	pe_enum_modules_in_load_order(
dd89bb
		__add_module_base_address_to_list,
dd89bb
		&ctx;;
dd89bb
dd89bb
	return ctx.status;
dd89bb
}