summaryrefslogtreecommitdiff
path: root/src/internal/perk_reader_impl.h
blob: 90985618c5bf15bfbd1ca2c1eb6f001b4ab4f6e7 (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
#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