Blame src/headers/pe_get_image_section_tbl_addr.c

e2475c
/*****************************************************************************/
e2475c
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
b6ea9a
/*  Copyright (C) 2013--2017  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
{
02863d
	struct pe_raw_coff_image_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
6e18ff
	mark  = opt->opt_hdr_32.coh_magic;
12bebb
	mark += coff->cfh_size_of_opt_hdr[1] << 8;
12bebb
	mark += coff->cfh_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;
02863d
	struct pe_raw_coff_image_hdr *	coff;
ab043c
	char *				ch;
5d9c48
	size_t				len;
ab043c
	uint32_t			pos;
ab043c
	uint64_t			sname;
f3c99b
	uint64_t *			shname;
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
12bebb
	count  = coff->cfh_num_of_sections[1] << 8;
12bebb
	count += coff->cfh_num_of_sections[0];
e2475c
e2475c
	if ((len = pe_impl_strlen_ansi(name)) > 8) {
e2475c
		/* todo: long name support */
e2475c
		return 0;
f3c99b
f3c99b
	} else if (len == 0) {
f3c99b
		return 0;
f3c99b
e2475c
	} else {
ab043c
		sname = 0;
ab043c
		ch    = (char *)&sname;
ab043c
ab043c
		for (pos=0; pos
e2475c
			ch[pos] = name[pos];
e2475c
f3c99b
		for (; count; hdr++,count--) {
f3c99b
			shname = (uint64_t *)&hdr->sh_name[0];
f3c99b
f3c99b
			if (*shname == sname)
e2475c
				return hdr;
f3c99b
		}
e2475c
	}
e2475c
e2475c
	return 0;
e2475c
}