Blame src/headers/pe_get_image_section_tbl_addr.c

e2475c
/*****************************************************************************/
e2475c
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
59fd65
/*  Copyright (C) 2013--2016  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
a2cf44
a2cf44
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;
a2cf44
	unsigned char *			mark;
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
a2cf44
	mark  = opt->opt_hdr_32.magic;
a2cf44
	mark += coff->size_of_opt_hdr[1] << 8;
a2cf44
	mark += coff->size_of_opt_hdr[0];
a2cf44
a2cf44
	return (struct pe_raw_sec_hdr *)mark;
e2475c
}
e2475c
ab043c
ab043c
struct pe_raw_sec_hdr * pe_get_image_named_section_addr(const void * base, const char * name)
e2475c
{
ab043c
	uint16_t			count;
ab043c
	struct pe_raw_sec_hdr *		hdr;
ab043c
	struct pe_raw_coff_file_hdr *	coff;
ab043c
	char *				ch;
ab043c
	uint32_t			len;
ab043c
	uint32_t			pos;
ab043c
	uint64_t			sname;
e2475c
e2475c
	if (!(hdr = pe_get_image_section_tbl_addr(base)))
e2475c
		return 0;
e2475c
ab043c
	if (!(coff = pe_get_image_coff_hdr_addr(base)))
ab043c
		return 0;
ab043c
ab043c
	count  = coff->num_of_sections[1] << 8;
ab043c
	count += coff->num_of_sections[0];
e2475c
e2475c
	if ((len = pe_impl_strlen_ansi(name)) > 8) {
e2475c
		/* todo: long name support */
e2475c
		return 0;
e2475c
	} else {
ab043c
		sname = 0;
ab043c
		ch    = (char *)&sname;
ab043c
ab043c
		for (pos=0; 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
}