Blame src/internal/perk_impl.h

c0fbae
#ifndef PERK_IMPL_H
c0fbae
#define PERK_IMPL_H
c0fbae
c0fbae
#include <stdint.h>
c0fbae
#include <endian.h>
c0fbae
c0fbae
/* internal structures */
c0fbae
struct perk_ctx {
c0fbae
	int			argc;
c0fbae
	char **			argv;
c0fbae
	char **			envp;
c0fbae
	const char *		fname;
c0fbae
	int			status;
c0fbae
	int			flags;
c0fbae
	int			fd;
c0fbae
	FILE *			fout;
c0fbae
	FILE *			tmp;
c0fbae
	struct pe_raw_image	map;
c0fbae
};
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