Blame src/meta/pe_get_image_stack_heap_info.c

feffc7
/*****************************************************************************/
feffc7
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
feffc7
/*  Copyright (C) 2013,2014,2015  Z. Gilboa                                  */
feffc7
/*  Released under GPLv2 and GPLv3; see COPYING.PEMAGINE.                    */
feffc7
/*****************************************************************************/
feffc7
feffc7
#include <psxtypes/psxtypes.h>
feffc7
#include <pemagine/pe_consts.h>
feffc7
#include <pemagine/pe_structs.h>
feffc7
#include <pemagine/pemagine.h>
feffc7
b1f838
feffc7
int pe_get_image_stack_heap_info(const void * base, struct pe_stack_heap_info * stack_heap_info)
feffc7
{
feffc7
	uint16_t *		magic;
fad23f
	union pe_raw_opt_hdr *	hdr;
feffc7
feffc7
	if (!(hdr = pe_get_image_opt_hdr_addr(base)))
feffc7
		return 0;
feffc7
feffc7
	magic = (uint16_t *)hdr;
feffc7
feffc7
	switch (*magic) {
feffc7
		case PE_MAGIC_PE32:
feffc7
			stack_heap_info->size_of_stack_reserve = *(uint32_t *)hdr->opt_hdr_32.size_of_stack_reserve;
feffc7
			stack_heap_info->size_of_stack_commit  = *(uint32_t *)hdr->opt_hdr_32.size_of_stack_commit;
feffc7
			stack_heap_info->size_of_heap_reserve  = *(uint32_t *)hdr->opt_hdr_32.size_of_heap_reserve;
feffc7
			stack_heap_info->size_of_heap_commit   = *(uint32_t *)hdr->opt_hdr_32.size_of_heap_commit;
feffc7
			break;
feffc7
feffc7
		case PE_MAGIC_PE32_PLUS:
feffc7
			stack_heap_info->size_of_stack_reserve = *(size_t *)hdr->opt_hdr_64.size_of_stack_reserve;
feffc7
			stack_heap_info->size_of_stack_commit  = *(size_t *)hdr->opt_hdr_64.size_of_stack_commit;
feffc7
			stack_heap_info->size_of_heap_reserve  = *(size_t *)hdr->opt_hdr_64.size_of_heap_reserve;
feffc7
			stack_heap_info->size_of_heap_commit   = *(size_t *)hdr->opt_hdr_64.size_of_heap_commit;
feffc7
			break;
feffc7
feffc7
		default:
feffc7
			return -1;
feffc7
	}
feffc7
feffc7
	return 0;
feffc7
}