Blame src/reader/pe_read_dos_header.c

e2e2c2
/***************************************************************/
e2e2c2
/*  perk: PE Resource Kit                                      */
e2e2c2
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
e2e2c2
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
e2e2c2
/***************************************************************/
e2e2c2
c0fbae
#include <string.h>
c0fbae
c0fbae
#include <perk/perk.h>
ee94ee
#include "perk_endian_impl.h"
413f56
#include "perk_reader_impl.h"
c0fbae
c0fbae
int pe_read_dos_header(const struct pe_image_dos_hdr * p, struct pe_meta_image_dos_hdr * m)
c0fbae
{
c0fbae
	if ((p->dos_magic[0] != 'M') || (p->dos_magic[1] != 'Z'))
ff121d
		return PERK_ERR_BAD_DOS_HEADER;
c0fbae
ee94ee
	if (PERK_LITTLE_ENDIAN) {
c0fbae
ee94ee
		memcpy(m,p,sizeof(*p));
c0fbae
ee94ee
	} else {
c0fbae
ee94ee
		int i;
b5f7f5
ee94ee
		m->dos_magic[0] = p->dos_magic[0];
ee94ee
		m->dos_magic[1] = p->dos_magic[1];
c0fbae
ee94ee
		m->dos_cblp	= pe_read_short(p->dos_cblp);
ee94ee
		m->dos_cp	= pe_read_short(p->dos_cp);
ee94ee
		m->dos_crlc	= pe_read_short(p->dos_crlc);
ee94ee
		m->dos_cparhdr	= pe_read_short(p->dos_cparhdr);
ee94ee
		m->dos_minalloc	= pe_read_short(p->dos_minalloc);
ee94ee
		m->dos_maxalloc	= pe_read_short(p->dos_maxalloc);
ee94ee
		m->dos_ss	= pe_read_short(p->dos_ss);
ee94ee
		m->dos_sp	= pe_read_short(p->dos_sp);
ee94ee
		m->dos_csum	= pe_read_short(p->dos_csum);
ee94ee
		m->dos_ip	= pe_read_short(p->dos_ip);
ee94ee
		m->dos_cs	= pe_read_short(p->dos_cs);
ee94ee
		m->dos_lfarlc	= pe_read_short(p->dos_lfarlc);
ee94ee
		m->dos_ovno	= pe_read_short(p->dos_ovno);
c0fbae
c0fbae
ee94ee
		for (i=0; i<4; i++)
ee94ee
			m->dos_res[i] = pe_read_short(&p->dos_res[2*i]);
c0fbae
ee94ee
		m->dos_oemid	= pe_read_short(p->dos_oemid);
ee94ee
		m->dos_oeminfo	= pe_read_short(p->dos_oeminfo);
c0fbae
ee94ee
		for (i=0; i<10; i++)
ee94ee
			m->dos_res2[i] = pe_read_short(&p->dos_res2[2*i]);
c0fbae
ee94ee
		m->dos_lfanew	= pe_read_long(p->dos_lfanew);
c0fbae
ee94ee
	}
c0fbae
c0fbae
	return 0;
b5f7f5
}