Blob Blame History Raw
/*****************************************************************************/
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
/*  Copyright (C) 2013,2014,2015  Z. Gilboa                                  */
/*  Released under GPLv2 and GPLv3; see COPYING.PEMAGINE.                    */
/*****************************************************************************/

#include <psxtypes/psxtypes.h>
#include <pemagine/pe_consts.h>
#include <pemagine/pe_structs.h>
#include <pemagine/pemagine.h>
#include "pe_impl.h"


struct pe_raw_sec_hdr * pe_get_image_section_tbl_addr(const void * base)
{
	struct pe_raw_coff_file_hdr *	coff;
	union  pe_raw_opt_hdr *		opt;
	unsigned char *			mark;

	if (!(coff = pe_get_image_coff_hdr_addr(base)))
		return 0;

	if (!(opt = pe_get_image_opt_hdr_addr(base)))
		return 0;

	mark  = opt->opt_hdr_32.magic;
	mark += coff->size_of_opt_hdr[1] << 8;
	mark += coff->size_of_opt_hdr[0];

	return (struct pe_raw_sec_hdr *)mark;
}


struct pe_raw_sec_hdr * pe_get_image_named_section_addr(const void * base, const char * name)
{
	uint16_t			count;
	struct pe_raw_sec_hdr *		hdr;
	struct pe_raw_coff_file_hdr *	coff;
	char *				ch;
	uint32_t			len;
	uint32_t			pos;
	uint64_t			sname;

	if (!(hdr = pe_get_image_section_tbl_addr(base)))
		return 0;

	if (!(coff = pe_get_image_coff_hdr_addr(base)))
		return 0;

	count  = coff->num_of_sections[1] << 8;
	count += coff->num_of_sections[0];

	if ((len = pe_impl_strlen_ansi(name)) > 8) {
		/* todo: long name support */
		return 0;
	} else {
		sname = 0;
		ch    = (char *)&sname;

		for (pos=0; pos<len; pos++)
			ch[pos] = name[pos];

		for (; count; hdr++,count--)
			if (*(uint64_t *)hdr->name == sname)
				return hdr;
	}

	return 0;
}