summaryrefslogtreecommitdiff
path: root/src/internal/perk_reader_impl.h
blob: fc5e3727cd019e5da57d4df646456d4327e5b4d5 (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
/***************************************************************/
/*  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 "perk_endian_impl.h"

static inline uint16_t pe_read_short(const unsigned char * raw)
{
	if (PERK_LITTLE_ENDIAN) {
		return *(uint16_t *)raw;
	} else {
		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)
{
	if (PERK_LITTLE_ENDIAN)
		return *(uint32_t *)raw;
	else
		return pe_swap_long(*(uint32_t *)raw);
}

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

#endif