forked from pspdev/prxtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.h
162 lines (120 loc) · 2.81 KB
/
types.h
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/***************************************************************
* PRXTool : Utility for PSP executables.
* (c) TyRaNiD 2k5
*
* types.h - Definition of basic cross platform types.
***************************************************************/
#ifndef __TYPES_H__
#define __TYPES_H__
#ifdef HAVE_CONFIG_H
#include "config.h"
#include "_stdint.h"
#else
#include <sys/types.h>
#define uint8_t u_int8_t
#define uint16_t u_int16_t
#define uint32_t u_int32_t
#define uint64_t u_int64_t
#endif
/* Re-define some system types */
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
#ifdef WORDS_BIGENDIAN
inline u32 lw_le(u32 data)
{
u8 *ptr;
u32 val;
ptr = (u8*) &data;
val = ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
return val;
}
inline u16 lh_le(u16 data)
{
u8 *ptr;
u16 val;
ptr = (u8*) &data;
val = ptr[0] | (ptr[1] << 8);
return val;
}
#define LW_LE(x) (lw_le((x)))
#define LW_BE(x) (x)
#define LH_LE(x) (lh_le((x)))
#define LH_BE(x) (x)
#else
inline u32 lw_be(u32 data)
{
u8 *ptr;
u32 val;
ptr = (u8*) &data;
val = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
return val;
}
inline u16 lh_be(u16 data)
{
u8 *ptr;
u16 val;
ptr = (u8*) &data;
val = (ptr[0] << 16) | ptr[1];
return val;
}
#define LW_LE(x) (x)
#define LW_BE(x) (lw_be((x)))
#define LH_LE(x) (x)
#define LH_BE(x) (lh_be((x)))
#endif
#define LW(x) (LW_LE(x))
#define LH(x) (LH_LE(x))
#ifdef WORDS_BIGENDIAN
inline void sw_le(u32 *data, u32 val)
{
u8* ptr = (u8*) data;
ptr[0] = (u8) (val & 0xFF);
ptr[1] = (u8) ((val >> 8) & 0xFF);
ptr[2] = (u8) ((val >> 16) & 0xFF);
ptr[3] = (u8) ((val >> 24) & 0xFF);
}
inline void sh_le(u16 *data, u16 val)
{
u8 *ptr = (u8*) data;
ptr[0] = (u8) (val & 0xFF);
ptr[1] = (u8) ((val >> 8) & 0xFF);
}
#define SW_LE(x, v) (sw_le((u32*) &(x), (v)))
#define SW_BE(x, v) ((x) = (v))
#define SH_LE(x, v) (sh_le((u16*) &(x), (v)))
#define SH_BE(x, v) ((x) = (v))
#else
inline void sw_be(u32 *data, u32 val)
{
u8 *ptr = (u8*) data;
ptr[0] = (u8) ((val >> 24) & 0xFF);
ptr[1] = (u8) ((val >> 16) & 0xFF);
ptr[2] = (u8) ((val >> 8) & 0xFF);
ptr[3] = (u8) (val & 0xFF);
}
inline void sh_be(u16 *data, u16 val)
{
u8* ptr = (u8*) data;
ptr[0] = (u8) ((val >> 8) & 0xFF);
ptr[1] = (u8) (val & 0xFF);
}
#define SW_LE(x, v) ((x) = (v))
#define SW_BE(x, v) (sw_be((u32*) &(x), (v)))
#define SH_LE(x, v) ((x) = (v))
#define SH_BE(x, v) (sh_be((u16*) &(x), (v)))
#endif
#define SW(x, v) (SW_LE(x, v))
#define SH(x, v) (SH_LE(x, v))
/* Do a safe alloc which should work on vc6 or latest gcc etc */
/* If alloc fails will always return NULL */
#define SAFE_ALLOC(p, t) try { (p) = new t; } catch(...) { (p) = NULL; }
#ifndef MAXPATH
#define MAXPATH 256
#endif
#endif