summaryrefslogtreecommitdiff
path: root/src/internal/perk_reader_impl.h
blob: 24fc38c18e912205f0228006fed292fba6e1ddcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/***************************************************************/
/*  perk: PE Resource Kit                                      */
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
/***************************************************************/

#ifndef PERK_READER_IMPL_H
#define PERK_READER_IMPL_H

#include <stdint.h>
#include <endian.h>

#if (BYTE_ORDER == LITTLE_ENDIAN)

static inline uint16_t pe_read_short(const unsigned char * raw)
{
	return *(uint16_t *)raw;
}

static inline uint32_t pe_read_long(const unsigned char * raw)
{
	return *(uint32_t *)raw;
}

static inline uint64_t pe_read_quad(const unsigned char * raw)
{
	return *(uint64_t *)raw;
}

#else

static inline uint16_t pe_read_short(const unsigned char * raw)
{
	uint16_t x = *(uint16_t *)raw;
	return x<<8 | x>>8;
}

static inline uint32_t pe_swap_long(uint32_t x)
{
	return x<<24 | (x<<8) & 0xff0000 | (x>>8) & 0xff00 | x>>24;
}

static inline uint32_t pe_read_long(const unsigned char * raw)
{
	return pe_swap_long(*(uint32_t *)raw);
}

static inline uint64_t pe_read_quad(const unsigned char * raw)
{
	uint64_t x = *(uint64_t *)raw;
	return ((uint64_t)pe_swap_long(x)<<32) | pe_swap_long(x>>32);
}

#endif

#endif