Blame src/internal/perk_reader_impl.h

e2e2c2
/***************************************************************/
e2e2c2
/*  perk: PE Resource Kit                                      */
e2e2c2
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
e2e2c2
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
e2e2c2
/***************************************************************/
e2e2c2
61bf9c
#ifndef PERK_READER_IMPL_H
61bf9c
#define PERK_READER_IMPL_H
c0fbae
c0fbae
#include <stdint.h>
c0fbae
#include <endian.h>
c0fbae
c0fbae
#if (BYTE_ORDER == LITTLE_ENDIAN)
c0fbae
c0fbae
static inline uint16_t pe_read_short(const unsigned char * raw)
c0fbae
{
c0fbae
	return *(uint16_t *)raw;
c0fbae
}
c0fbae
c0fbae
static inline uint32_t pe_read_long(const unsigned char * raw)
c0fbae
{
c0fbae
	return *(uint32_t *)raw;
c0fbae
}
c0fbae
c0fbae
static inline uint64_t pe_read_quad(const unsigned char * raw)
c0fbae
{
c0fbae
	return *(uint64_t *)raw;
c0fbae
}
c0fbae
c0fbae
#else
c0fbae
b5f7f5
static inline uint16_t pe_read_short(const unsigned char * raw)
c0fbae
{
c0fbae
	uint16_t x = *(uint16_t *)raw;
c0fbae
	return x<<8 | x>>8;
c0fbae
}
c0fbae
c0fbae
static inline uint32_t pe_swap_long(uint32_t x)
c0fbae
{
c0fbae
	return x<<24 | (x<<8) & 0xff0000 | (x>>8) & 0xff00 | x>>24;
c0fbae
}
c0fbae
b5f7f5
static inline uint32_t pe_read_long(const unsigned char * raw)
c0fbae
{
c0fbae
	return pe_swap_long(*(uint32_t *)raw);
c0fbae
}
c0fbae
b5f7f5
static inline uint64_t pe_read_quad(const unsigned char * raw)
c0fbae
{
c0fbae
	uint64_t x = *(uint64_t *)raw;
c0fbae
	return ((uint64_t)pe_swap_long(x)<<32) | pe_swap_long(x>>32);
c0fbae
}
c0fbae
c0fbae
#endif
c0fbae
c0fbae
#endif