Blame src/meta/pe_get_image_stack_heap_info.c

feffc7
/*****************************************************************************/
feffc7
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
b6ea9a
/*  Copyright (C) 2013--2017  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
f8d0c8
static uint32_t pe_read_raw_data_32(unsigned char * addr)
f8d0c8
{
f8d0c8
	uint32_t * p32;
f8d0c8
f8d0c8
	p32 = (uint32_t *)addr;
f8d0c8
	return *p32;
f8d0c8
}
f8d0c8
f8d0c8
static uint64_t pe_read_raw_data_64(unsigned char * addr)
f8d0c8
{
f8d0c8
	uint64_t * p64;
f8d0c8
f8d0c8
	p64 = (uint64_t *)addr;
f8d0c8
	return *p64;
f8d0c8
}
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:
f8d0c8
			stack_heap_info->size_of_stack_reserve = pe_read_raw_data_32(hdr->opt_hdr_32.coh_size_of_stack_reserve);
f8d0c8
			stack_heap_info->size_of_stack_commit  = pe_read_raw_data_32(hdr->opt_hdr_32.coh_size_of_stack_commit);
f8d0c8
			stack_heap_info->size_of_heap_reserve  = pe_read_raw_data_32(hdr->opt_hdr_32.coh_size_of_heap_reserve);
f8d0c8
			stack_heap_info->size_of_heap_commit   = pe_read_raw_data_32(hdr->opt_hdr_32.coh_size_of_heap_commit);
feffc7
			break;
feffc7
feffc7
		case PE_MAGIC_PE32_PLUS:
f8d0c8
			stack_heap_info->size_of_stack_reserve = pe_read_raw_data_64(hdr->opt_hdr_64.coh_size_of_stack_reserve);
f8d0c8
			stack_heap_info->size_of_stack_commit  = pe_read_raw_data_64(hdr->opt_hdr_64.coh_size_of_stack_commit);
f8d0c8
			stack_heap_info->size_of_heap_reserve  = pe_read_raw_data_64(hdr->opt_hdr_64.coh_size_of_heap_reserve);
f8d0c8
			stack_heap_info->size_of_heap_commit   = pe_read_raw_data_64(hdr->opt_hdr_64.coh_size_of_heap_commit);
feffc7
			break;
feffc7
feffc7
		default:
feffc7
			return -1;
feffc7
	}
feffc7
feffc7
	return 0;
feffc7
}