Blame src/headers/pe_get_image_section_tbl_addr.c

e2475c
/*****************************************************************************/
e2475c
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
e2475c
/*  Copyright (C) 2013,2014,2015  Z. Gilboa                                  */
e2475c
/*  Released under GPLv2 and GPLv3; see COPYING.PEMAGINE.                    */
e2475c
/*****************************************************************************/
e2475c
e2475c
#include <psxtypes/psxtypes.h>
e2475c
#include <pemagine/pe_consts.h>
e2475c
#include <pemagine/pe_structs.h>
e2475c
#include <pemagine/pemagine.h>
e2475c
#include "pe_impl.h"
e2475c
e2475c
pe_api
9089cb
struct pe_raw_sec_hdr * pe_get_image_section_tbl_addr (const void * base)
e2475c
{
945985
	struct pe_raw_coff_file_hdr *	coff;
fad23f
	union  pe_raw_opt_hdr *		opt;
e2475c
e2475c
	if (!(coff = pe_get_image_coff_hdr_addr(base)))
e2475c
		return 0;
e2475c
e2475c
	if (!(opt = pe_get_image_opt_hdr_addr(base)))
e2475c
		return 0;
e2475c
9089cb
	return (struct pe_raw_sec_hdr *)((char *)opt  + *(uint16_t *)coff->size_of_opt_hdr);
e2475c
}
e2475c
e2475c
pe_api
9089cb
struct pe_raw_sec_hdr * pe_get_image_named_section_addr (const void * base, const char * name)
e2475c
{
e2475c
	uint16_t		count;
9089cb
	struct pe_raw_sec_hdr *	hdr;
945985
	struct pe_raw_coff_file_hdr*coff;
e2475c
	char *			ch;
e2475c
	uint32_t		len;
e2475c
	uint32_t		pos;
e2475c
	uint64_t		sname = 0;
e2475c
e2475c
	if (!(hdr = pe_get_image_section_tbl_addr(base)))
e2475c
		return 0;
e2475c
e2475c
	coff  = pe_get_image_coff_hdr_addr(base);
e2475c
	count = *(uint16_t *)coff->num_of_sections;
e2475c
e2475c
	if ((len = pe_impl_strlen_ansi(name)) > 8) {
e2475c
		/* todo: long name support */
e2475c
		return 0;
e2475c
	} else {
e2475c
		for (pos=0, ch=(char *)&sname; pos
e2475c
			ch[pos] = name[pos];
e2475c
e2475c
		for (; count; hdr++,count--)
e2475c
			if (*(uint64_t *)hdr->name == sname)
e2475c
				return hdr;
e2475c
	}
e2475c
e2475c
	return 0;
e2475c
}